Documentation
¶
Overview ¶
pkg/compiler/compiler.go
pkg/compiler/opcode.go
pkg/compiler/optimizer.go Bytecode optimizer for compile-time transformations
pkg/compiler/options.go Configuration options for compiler optimizations
pkg/compiler/serialize.go Bytecode serialization for saving and loading compiled code.
pkg/compiler/sourcemap.go Source map for mapping bytecode positions to source locations
Index ¶
- Constants
- func Make(op Opcode, operands ...int) []byte
- func ReadOperands(def *Definition, ins []byte) ([]int, int)
- func String(ins []byte) string
- type Bytecode
- type CompilationScope
- type CompiledFunction
- type Compiler
- type Definition
- type EmittedInstruction
- type InlineableFuncInfo
- type Opcode
- type OptimizationFlags
- type Optimizer
- type SourceLocation
- type SourceMap
- func (sm *SourceMap) Add(offset int, loc SourceLocation)
- func (sm *SourceMap) FormatError(offset int, message string) string
- func (sm *SourceMap) Get(offset int) (SourceLocation, bool)
- func (sm *SourceMap) GetLine(lineNum int) string
- func (sm *SourceMap) Serialize(w io.Writer) error
- func (sm *SourceMap) SetSourceFile(path string, source string)
- type Symbol
- type SymbolScope
- type SymbolTable
Constants ¶
const ( MagicHeader = "XXLB" // Xxlang Bytecode CurrentVersion = 1 )
Magic header for bytecode files
const GlobalsSize = 65536
GlobalsSize is the maximum number of global variables
Variables ¶
This section is empty.
Functions ¶
func ReadOperands ¶
func ReadOperands(def *Definition, ins []byte) ([]int, int)
ReadOperands reads operands from bytecode
Types ¶
type Bytecode ¶
type Bytecode struct {
Instructions []byte
Constants []objects.Object
SourceMap *SourceMap // Maps instruction positions to source locations
InlineableGlobals map[int]*InlineableFuncInfo // Global index -> inlineable function info
}
Bytecode represents compiled bytecode
func Deserialize ¶
Deserialize decodes binary data to bytecode. Returns the bytecode or an error.
func DeserializeFromFile ¶
DeserializeFromFile reads bytecode from a file.
func (*Bytecode) Serialize ¶
Serialize encodes bytecode to a binary format. Returns the serialized bytes or an error.
func (*Bytecode) SerializeToFile ¶
SerializeToFile writes bytecode to a file. The format is: magic(4) + version(4) + gob-encoded data
type CompilationScope ¶
type CompilationScope struct {
// contains filtered or unexported fields
}
CompilationScope represents a compilation scope
type CompiledFunction ¶
type CompiledFunction struct {
Instructions []byte
NumLocals int
NumParameters int
FreeVariables []Symbol // Free variables captured from outer scope
// Inlining support
IsInlineable bool // True if function body is a single return expression
InlineBody []byte // Inlined bytecode (without return)
}
CompiledFunction represents a compiled function
func (*CompiledFunction) HashKey ¶
func (cf *CompiledFunction) HashKey() objects.HashKey
HashKey returns the hash key
func (*CompiledFunction) Inspect ¶
func (cf *CompiledFunction) Inspect() string
Inspect returns the string representation
func (*CompiledFunction) ToBool ¶
func (cf *CompiledFunction) ToBool() *objects.Bool
ToBool converts to boolean
func (*CompiledFunction) Type ¶
func (cf *CompiledFunction) Type() objects.ObjectType
Type returns the object type
func (*CompiledFunction) TypeTag ¶
func (cf *CompiledFunction) TypeTag() objects.TypeTag
TypeTag returns the type tag for fast type checking
type Compiler ¶
type Compiler struct {
// contains filtered or unexported fields
}
Compiler transforms AST into bytecode
func NewWithOptions ¶
func NewWithOptions(opts OptimizationFlags) *Compiler
NewWithOptions creates a new compiler with custom optimization settings
func NewWithState ¶
func NewWithState(s *SymbolTable, constants []objects.Object) *Compiler
NewWithState creates a new compiler with existing state
type Definition ¶
type Definition struct {
Name string // Human-readable name
OperandWidths []int // Width in bytes of each operand
}
Definition describes an opcode's format
type EmittedInstruction ¶
EmittedInstruction represents the last emitted instruction
type InlineableFuncInfo ¶
type InlineableFuncInfo struct {
ConstIndex int // Index in constants pool
NumParams int
Body []byte
}
InlineableFuncInfo stores information about an inlineable function
type Opcode ¶
type Opcode byte
Opcode represents a single bytecode instruction
const ( // Stack operations OpConstant Opcode = iota // Load constant from constant pool OpPop // Pop value from stack OpDup // Duplicate top of stack // Arithmetic operations OpAdd // Addition OpSub // Subtraction OpMul // Multiplication OpDiv // Division OpMod // Modulo OpNeg // Unary minus (negation) // Comparison operations OpEqual // Equality check OpNotEqual // Inequality check OpLess // Less than OpGreater // Greater than OpLessEqual // Less than or equal OpGreaterEqual // Greater than or equal // Logical operations OpAnd // Logical AND OpOr // Logical OR OpNot // Logical NOT // Variable operations OpGetLocal // Get local variable OpSetLocal // Set local variable OpDefineLocal // Define local variable OpGetGlobal // Get global variable OpSetGlobal // Set global variable OpGetFree // Get free variable (from closure) OpSetFree // Set free variable (in closure) // Scope operations OpPushScope // Push new scope OpPopScope // Pop scope // Superinstructions (combined operations for performance) OpGetLocalAdd // Get two locals and add OpGetLocalSub // Get two locals and subtract OpGetLocalMul // Get two locals and multiply OpConstantAdd // Load two constants and add OpConstantSub // Load two constants and subtract OpConstantMul // Load two constants and multiply // Type-specialized instructions (for hot path optimization) OpIncLocal // Increment local variable by 1 (optimized for loop counters) OpDecLocal // Decrement local variable by 1 OpAddLocalConst // Add constant to local variable // Control flow operations OpJump // Unconditional jump OpJumpIfFalse // Jump if top of stack is false OpJumpIfTrue // Jump if top of stack is true OpCall // Call function OpTailCall // Tail call (reuse current frame) OpReturn // Return from function OpClosure // Create closure with captured variables // Collection operations OpArray // Create array from stack elements OpMap // Create map from stack elements OpIndex // Index into array/map OpSetIndex // Set index in array/map OpIndexSafe // Index into array without bounds check (safe context) // Method/Field operations OpGetMethod // Get method from object OpCallMethod // Call method on object // Built-in operations OpBuiltin // Call built-in function // Literal operations OpNull // Push null onto stack OpTrue // Push true onto stack OpFalse // Push false onto stack // Loop control OpBreak // Break from loop OpContinue // Continue to next iteration // Module operations OpLoadModule // Load module and push onto stack OpGetExport // Get export from module OpModule // Create module from exports OpSetExport // Set export in current module // Class operations OpClass // Create class object OpNew // Create instance OpGetField // Get instance field OpSetField // Set instance field OpSuper // Get superclass method )
type OptimizationFlags ¶
type OptimizationFlags struct {
BytecodeOptimizer bool
InlineFunctions bool // Function inlining at call sites
InlineCache bool
ClosurePool bool
TypeSpecialization bool
Superinstructions bool // Combine common instruction sequences
}
OptimizationFlags controls which optimizations are enabled
func DefaultOptimizations ¶
func DefaultOptimizations() OptimizationFlags
DefaultOptimizations returns flags with all optimizations enabled
func NoInliningOptimizations ¶
func NoInliningOptimizations() OptimizationFlags
NoInliningOptimizations returns flags with inlining disabled (for benchmarking)
type Optimizer ¶
type Optimizer struct {
// contains filtered or unexported fields
}
Optimizer transforms bytecode to reduce instruction count and improve execution patterns
func NewOptimizer ¶
NewOptimizer creates a new optimizer for given bytecode
func NewOptimizerWithFlags ¶
func NewOptimizerWithFlags(bytecode *Bytecode, flags OptimizationFlags) *Optimizer
NewOptimizerWithFlags creates an optimizer with custom flags
func (*Optimizer) FoldConstants ¶
FoldConstants evaluates constant expressions at compile time Pattern: Constant; Constant; BinaryOp → replace with single Constant
func (*Optimizer) GenerateSuperinstructions ¶
GenerateSuperinstructions combines common instruction sequences into single instructions
func (*Optimizer) GenerateTypeSpecializations ¶
GenerateTypeSpecializations creates type-specialized instructions for common patterns that can be optimized
func (*Optimizer) InlineFunctions ¶
InlineFunctions inlines small functions at call sites to reduce call overhead Pattern: OpGetGlobal/OpConstant (callee), args..., OpCall -> inlined body
type SourceLocation ¶
SourceLocation represents a position in source code
type SourceMap ¶
type SourceMap struct {
// Maps instruction offset -> source location
Locations map[int]SourceLocation
// Source file path (if available)
SourceFile string
// Source code lines (for error display)
SourceLines []string
}
SourceMap maps bytecode instruction positions to source locations
func DeserializeSourceMap ¶
Deserialize reads the source map from a binary format
func (*SourceMap) Add ¶
func (sm *SourceMap) Add(offset int, loc SourceLocation)
Add maps an instruction offset to a source location
func (*SourceMap) FormatError ¶
FormatError creates a formatted error message with source context
func (*SourceMap) Get ¶
func (sm *SourceMap) Get(offset int) (SourceLocation, bool)
Get returns the source location for an instruction offset Returns the closest location if exact match not found
func (*SourceMap) SetSourceFile ¶
SetSourceFile sets the source file path and loads source lines
type Symbol ¶
type Symbol struct {
Name string
Scope SymbolScope
Index int
}
Symbol represents a named variable in a scope
type SymbolScope ¶
type SymbolScope string
SymbolScope represents the scope of a symbol
const ( GlobalScope SymbolScope = "GLOBAL" LocalScope SymbolScope = "LOCAL" BuiltinScope SymbolScope = "BUILTIN" FreeScope SymbolScope = "FREE" )
type SymbolTable ¶
type SymbolTable struct {
Outer *SymbolTable
Store map[string]Symbol
NumDefinitions int
FreeSymbols []Symbol
}
SymbolTable manages symbol definitions and resolution
func NewEnclosedSymbolTable ¶
func NewEnclosedSymbolTable(outer *SymbolTable) *SymbolTable
NewEnclosedSymbolTable creates a new symbol table with an outer scope
func (*SymbolTable) Define ¶
func (s *SymbolTable) Define(name string) Symbol
Define adds a new symbol to the symbol table
func (*SymbolTable) DefineBuiltin ¶
func (s *SymbolTable) DefineBuiltin(index int, name string) Symbol
DefineBuiltin adds a built-in function symbol