compiler

package
v0.0.0-...-31457c0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 24, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
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 Compile

func Compile(executable *os.File, options Options) []byte

func CompileInstruction

func CompileInstruction(writer *OutputWriter, command AssemblyCommand)

main

func CompileRegister

func CompileRegister(w *OutputWriter, argument Argument) string
func CutAndLink(w *OutputWriter)

func Emit

func Emit(w *OutputWriter, nodes ...*IRNode)

func FindInArray

func FindInArray(array []string, target string) int

func FindLabelAddress

func FindLabelAddress(writer *OutputWriter, target string) int

func GetAllLabels

func GetAllLabels(writer *OutputWriter) []string

func IncrementFunctionName

func IncrementFunctionName(name string) string

func JumpTo

func JumpTo(w *OutputWriter, label string, link bool)

func JumpToIR

func JumpToIR(w *OutputWriter, cond *IRNode, label string)

func ReadDirective

func ReadDirective(directive string) []string

func UnescapeDirectiveString

func UnescapeDirectiveString(raw string) (string, error)

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 Argument

type Argument struct {
	Offset       int
	Register     bool
	Source       string
	Modifier     string /* "lo", "hi", or "" */
	BaseRegister string /* for %lo(sym)(reg) patterns */
}

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

func (k IRKind) MarshalJSON() ([]byte, error)

func (IRKind) String

func (k IRKind) String() string

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 IRBinop

func IRBinop(op string, lhs, rhs *IRNode) *IRNode

func IRCall

func IRCall(fn string, args ...*IRNode) *IRNode

func IRCast

func IRCast(fn string, expr *IRNode) *IRNode

func IRField

func IRField(table *IRNode, field string) *IRNode

func IRIfExpr

func IRIfExpr(cond, then, els *IRNode) *IRNode

func IRIndex

func IRIndex(table, key *IRNode) *IRNode

func IRLit

func IRLit(v interface{}) *IRNode

func IRLitHex

func IRLitHex(v int) *IRNode

func IRRawExpr

func IRRawExpr(text string) *IRNode

func IRReg

func IRReg(index int) *IRNode

--------------------------------------------------------------------------- Expression constructors ---------------------------------------------------------------------------

func IRRegName

func IRRegName(name string) *IRNode

func IRStmtAssign

func IRStmtAssign(dst, src *IRNode) *IRNode

--------------------------------------------------------------------------- Statement constructors ---------------------------------------------------------------------------

func IRStmtAssignComment

func IRStmtAssignComment(dst, src *IRNode, comment string) *IRNode

func IRStmtCall

func IRStmtCall(fn string, args ...*IRNode) *IRNode

func IRStmtComment

func IRStmtComment(text string) *IRNode

func IRStmtDo

func IRStmtDo(body []*IRNode) *IRNode

func IRStmtError

func IRStmtError(msg string) *IRNode

func IRStmtForNum

func IRStmtForNum(varName string, start, limit *IRNode, body []*IRNode) *IRNode

func IRStmtFuncBegin

func IRStmtFuncBegin(pc int, label string) *IRNode

func IRStmtFuncEnd

func IRStmtFuncEnd() *IRNode

func IRStmtIf

func IRStmtIf(cond *IRNode, body []*IRNode, elseBody []*IRNode) *IRNode

func IRStmtLocal

func IRStmtLocal(name, typ string, expr *IRNode) *IRNode

func IRStmtPCInc

func IRStmtPCInc() *IRNode

func IRStmtRaw

func IRStmtRaw(text string) *IRNode

func IRStmtReturn

func IRStmtReturn(val bool) *IRNode

func IRStmtSetPC

func IRStmtSetPC(expr *IRNode) *IRNode

func IRStmtWhile

func IRStmtWhile(cond *IRNode, body []*IRNode) *IRNode

func IRSymbol

func IRSymbol(name string) *IRNode

func IRUnop

func IRUnop(op string, operand *IRNode) *IRNode

type Options

type Options struct {
	Comments   bool
	Trace      bool
	Accurate   bool
	Memory     int
	Mode       string
	MainSymbol string
	Imports    []string
	LogIR      bool
}

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 */
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL