Documentation
¶
Index ¶
- Constants
- func AddEnd(w *OutputWriter)
- func AfterCompilation(writer *OutputWriter) []byte
- func BeforeCompilation(writer *OutputWriter)
- func BuildLabelCache(writer *OutputWriter)
- func Compile(executable *os.File, options Options) []byte
- func CompileInstruction(writer *OutputWriter, command AssemblyCommand)
- func CompileRegister(w *OutputWriter, argument Argument) string
- func CutAndLink(w *OutputWriter)
- func Emit(w *OutputWriter, nodes ...*IRNode)
- func FindInArray(array []string, target string) int
- func FindLabelAddress(writer *OutputWriter, target string) int
- func GetAllLabels(writer *OutputWriter) []string
- func IncrementFunctionName(name string) string
- func JumpTo(w *OutputWriter, label string, link bool)
- func JumpToIR(w *OutputWriter, cond *IRNode, label string)
- func ReadDirective(directive string) []string
- func UnescapeDirectiveString(raw string) (string, error)
- func WriteIndentedString(writer *OutputWriter, format string, args ...any)
- func WriteString(writer *OutputWriter, format string, args ...any)
- type Argument
- type AssemblyCommand
- type CommandType
- type IRKind
- type IRNode
- func IRBinop(op string, lhs, rhs *IRNode) *IRNode
- func IRCall(fn string, args ...*IRNode) *IRNode
- func IRCast(fn string, expr *IRNode) *IRNode
- func IRField(table *IRNode, field string) *IRNode
- func IRIfExpr(cond, then, els *IRNode) *IRNode
- func IRIndex(table, key *IRNode) *IRNode
- func IRLit(v interface{}) *IRNode
- func IRLitHex(v int) *IRNode
- func IRRawExpr(text string) *IRNode
- func IRReg(index int) *IRNode
- func IRRegName(name string) *IRNode
- func IRStmtAssign(dst, src *IRNode) *IRNode
- func IRStmtAssignComment(dst, src *IRNode, comment string) *IRNode
- func IRStmtCall(fn string, args ...*IRNode) *IRNode
- func IRStmtComment(text string) *IRNode
- func IRStmtDo(body []*IRNode) *IRNode
- func IRStmtError(msg string) *IRNode
- func IRStmtForNum(varName string, start, limit *IRNode, body []*IRNode) *IRNode
- func IRStmtFuncBegin(pc int, label string) *IRNode
- func IRStmtFuncEnd() *IRNode
- func IRStmtIf(cond *IRNode, body []*IRNode, elseBody []*IRNode) *IRNode
- func IRStmtLocal(name, typ string, expr *IRNode) *IRNode
- func IRStmtPCInc() *IRNode
- func IRStmtRaw(text string) *IRNode
- func IRStmtReturn(val bool) *IRNode
- func IRStmtSetPC(expr *IRNode) *IRNode
- func IRStmtWhile(cond *IRNode, body []*IRNode) *IRNode
- func IRSymbol(name string) *IRNode
- func IRUnop(op string, operand *IRNode) *IRNode
- type Options
- type OutputWriter
- type PendingData
- type PendingDataType
Constants ¶
const ( BIT32_BAND = "bit32.band" BIT32_BOR = "bit32.bor" BIT32_BXOR = "bit32.bxor" BIT32_BNOT = "bit32.bnot" BIT32_LSHIFT = "bit32.lshift" BIT32_RSHIFT = "bit32.rshift" BIT32_ARSHIFT = "bit32.arshift" BIT32_LROTATE = "bit32.lrotate" BIT32_RROTATE = "bit32.rrotate" BIT32_COUNTLZ = "bit32.countlz" BIT32_COUNTRZ = "bit32.countrz" MATH_ABS = "math.abs" MATH_SQRT = "math.sqrt" MATH_MIN = "math.min" MATH_MAX = "math.max" MATH_SIGN = "math.sign" MATH_FLOOR = "math.floor" MATH_CEIL = "math.ceil" BUFFER_READI8 = "buffer.readi8" BUFFER_READI16 = "buffer.readi16" BUFFER_READI32 = "buffer.readi32" BUFFER_READU8 = "buffer.readu8" BUFFER_READU16 = "buffer.readu16" BUFFER_READU32 = "buffer.readu32" BUFFER_READF32 = "buffer.readf32" BUFFER_READF64 = "buffer.readf64" BUFFER_WRITEI8 = "buffer.writei8" BUFFER_WRITEI16 = "buffer.writei16" BUFFER_WRITEI32 = "buffer.writei32" BUFFER_WRITEU8 = "buffer.writeu8" BUFFER_WRITEF32 = "buffer.writef32" BUFFER_WRITEF64 = "buffer.writef64" BUFFER_WRITESTR = "buffer.writestring" BUFFER_FILL = "buffer.fill" BUFFER_LEN = "buffer.len" RT_I32 = "i32" RT_U32 = "u32" RT_F32 = "f32" RT_INT_TO_FLOAT = "int_to_float" RT_FLOAT_TO_INT = "float_to_int" RT_IDIV_TRUNC = "idiv_trunc" RT_FCLASS = "fclass" RT_RESET_REGS = "reset_registers" RT_FLUSH_STDOUT = "flush_stdout" SYM_MEMORY = "memory" SYM_PC = "PC" SYM_R2 = "r2" SYM_FFLAGS = "fflags" SYM_FUNCS = "FUNCS" SYM_FUNCTIONS = "functions" )
Variables ¶
This section is empty.
Functions ¶
func AddEnd ¶
func AddEnd(w *OutputWriter)
func AfterCompilation ¶
func AfterCompilation(writer *OutputWriter) []byte
func BeforeCompilation ¶
func BeforeCompilation(writer *OutputWriter)
func BuildLabelCache ¶
func BuildLabelCache(writer *OutputWriter)
func CompileInstruction ¶
func CompileInstruction(writer *OutputWriter, command AssemblyCommand)
main
func CompileRegister ¶
func CompileRegister(w *OutputWriter, argument Argument) string
func CutAndLink ¶
func CutAndLink(w *OutputWriter)
func Emit ¶
func Emit(w *OutputWriter, nodes ...*IRNode)
func FindInArray ¶
func FindLabelAddress ¶
func FindLabelAddress(writer *OutputWriter, target string) int
func GetAllLabels ¶
func GetAllLabels(writer *OutputWriter) []string
func IncrementFunctionName ¶
func JumpTo ¶
func JumpTo(w *OutputWriter, label string, link bool)
func JumpToIR ¶
func JumpToIR(w *OutputWriter, cond *IRNode, label string)
func ReadDirective ¶
func UnescapeDirectiveString ¶
func WriteIndentedString ¶
func WriteIndentedString(writer *OutputWriter, format string, args ...any)
WriteIndentedString prepends the current indentation level then appends the formatted string. The indent string is looked up from a pre-built cache rather than allocated on every call.
func WriteString ¶
func WriteString(writer *OutputWriter, format string, args ...any)
WriteString appends a formatted string to the buffer (no indentation).
Types ¶
type AssemblyCommand ¶
type AssemblyCommand struct {
Type CommandType
Name string
Arguments []Argument
Ignore bool /* usually only used for labels, to exclude ones without instructions */
}
func Parse ¶
func Parse(writer *OutputWriter, command string) AssemblyCommand
func ParseFromElf ¶
func ParseFromElf(f *elf.File) []AssemblyCommand
type CommandType ¶
type CommandType uint8
const ( Instruction CommandType = 0 Label CommandType = 1 Directive CommandType = 2 )
type IRKind ¶
type IRKind uint8
--------------------------------------------------------------------------- IR Node kinds ---------------------------------------------------------------------------
const ( IRAssign IRKind = iota // dst = expr IRIf // if cond { body... } IRDo // do { body... } IRReturn // return <bool> IRSetPC // PC = expr IRSetReg // rN = expr (alias for IRAssign targeting a register) IRComment // -- text IRRaw // raw Luau string (escape hatch for things not worth modelling) IRFuncBegin // FUNCS[n] = function(): boolean -- label IRFuncEnd // end (closes a FUNCS entry) IRLocalDecl // local name: type = expr IRWhile // while cond { body... } IRForNum // for i = start, limit { body... } IRPCInc // PC += 1 IRError // error("…") IRFuncCall // standalone function call statement: fn(args…) IRExprReg // rN IRExprLit // numeric / string literal IRExprSym // symbol name (label, global) IRExprBinop // lhs op rhs IRExprUnop // op operand IRExprCall // fn(args…) IRExprIfExpr // if cond then a else b IRExprCast // i32(x) / u32(x) / f32(x) IRExprIndex // table[key] IRExprField // table.field )
func (IRKind) MarshalJSON ¶
type IRNode ¶
type IRNode struct {
Kind IRKind
Op string // operator text, function name, literal value, symbol name, cast name
Operands []*IRNode // sub-expressions
Body []*IRNode // then-branch / loop body / function body
Else []*IRNode // else branch (IRIf only)
Comment string // inline comment
IntVal int // used by IRFuncBegin, IRReturn (0=false,1=true), IRPCInc
BoolVal bool // used by IRReturn
}
--------------------------------------------------------------------------- IRNode ---------------------------------------------------------------------------
func IRReg ¶
--------------------------------------------------------------------------- Expression constructors ---------------------------------------------------------------------------
func IRStmtAssign ¶
--------------------------------------------------------------------------- Statement constructors ---------------------------------------------------------------------------
func IRStmtAssignComment ¶
func IRStmtCall ¶
func IRStmtComment ¶
func IRStmtError ¶
func IRStmtFuncBegin ¶
func IRStmtFuncEnd ¶
func IRStmtFuncEnd() *IRNode
func IRStmtLocal ¶
func IRStmtPCInc ¶
func IRStmtPCInc() *IRNode
func IRStmtReturn ¶
func IRStmtSetPC ¶
func IRStmtWhile ¶
type OutputWriter ¶
type OutputWriter struct {
Buffer []byte /* the output */
CurrentLabel *AssemblyCommand /* keep track of current label */
MemoryDevelopmentPointer int32 /* used when generating code that propagates memory with strings */
PendingData PendingData /* used for remember data across instructions */
Depth int /* used for indentation */
MaxPC int /* used for counting PC which is hardcoded in */
Commands []AssemblyCommand /* used to check lines in the future */
IRNodes []*IRNode
MemoryMap map[string]int /* map static data keys to addresses */
// LabelPC is the O(1) label-address cache built by BuildLabelCache.
// Keyed by label name; value is the same sequential PC index that
// FindLabelAddress previously computed via a linear scan.
LabelPC map[string]int
Options Options /* user specified options */
InstructionTotal int
InstructionProcessed int
}
type PendingData ¶
type PendingData struct {
Type PendingDataType
Data string
}
type PendingDataType ¶
type PendingDataType int8
const ( PendingDataTypeNone PendingDataType = 0 PendingDataTypeString PendingDataType = 1 /* a string generated via directive */ PendingDataTypeNumeric PendingDataType = 2 /* a numeric value generated via .word for example */ )