compiler

package
v0.4.19 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2026 License: MIT Imports: 10 Imported by: 0

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

View Source
const (
	MagicHeader    = "XXLB" // Xxlang Bytecode
	CurrentVersion = 1
)

Magic header for bytecode files

View Source
const GlobalsSize = 65536

GlobalsSize is the maximum number of global variables

Variables

This section is empty.

Functions

func Make

func Make(op Opcode, operands ...int) []byte

Make creates a bytecode instruction with operands

func ReadOperands

func ReadOperands(def *Definition, ins []byte) ([]int, int)

ReadOperands reads operands from bytecode

func String

func String(ins []byte) string

String prints bytecode as a formatted string

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

func Deserialize(data []byte) (*Bytecode, error)

Deserialize decodes binary data to bytecode. Returns the bytecode or an error.

func DeserializeFromFile

func DeserializeFromFile(path string) (*Bytecode, error)

DeserializeFromFile reads bytecode from a file.

func (*Bytecode) Serialize

func (b *Bytecode) Serialize() ([]byte, error)

Serialize encodes bytecode to a binary format. Returns the serialized bytes or an error.

func (*Bytecode) SerializeToFile

func (b *Bytecode) SerializeToFile(path string) error

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 New

func New() *Compiler

New creates a new compiler with default optimizations

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

func (*Compiler) Bytecode

func (c *Compiler) Bytecode() *Bytecode

Bytecode returns the compiled bytecode

func (*Compiler) Compile

func (c *Compiler) Compile(node parser.Node) error

Compile compiles an AST node into bytecode

func (*Compiler) DefineGlobal added in v0.4.19

func (c *Compiler) DefineGlobal(name string) Symbol

DefineGlobal defines a global variable in the symbol table This is used by runCode to pre-define arguments before compilation

func (*Compiler) ResolveSymbol added in v0.4.19

func (c *Compiler) ResolveSymbol(name string) (Symbol, bool)

ResolveSymbol resolves a symbol by name This is used by runCode to find argument indices after compilation

func (*Compiler) SetSource

func (c *Compiler) SetSource(path string, code string)

SetSource sets the source file path and code for error reporting

type Definition

type Definition struct {
	Name          string // Human-readable name
	OperandWidths []int  // Width in bytes of each operand
}

Definition describes an opcode's format

func Lookup

func Lookup(op byte) (*Definition, error)

Lookup finds an opcode's definition

type EmittedInstruction

type EmittedInstruction struct {
	Opcode   Opcode
	Position int
}

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

	// Exception handling
	OpThrow       // Throw exception (value on stack)
	OpPushHandler // Push exception handler (catchAddr, finallyAddr)
	OpPopHandler  // Pop exception handler
)

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

func NewOptimizer(bytecode *Bytecode) *Optimizer

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

func (o *Optimizer) FoldConstants() *Bytecode

FoldConstants evaluates constant expressions at compile time Pattern: Constant; Constant; BinaryOp → replace with single Constant

func (*Optimizer) GenerateSuperinstructions

func (o *Optimizer) GenerateSuperinstructions() *Bytecode

GenerateSuperinstructions combines common instruction sequences into single instructions

func (*Optimizer) GenerateTypeSpecializations

func (o *Optimizer) GenerateTypeSpecializations() *Bytecode

GenerateTypeSpecializations creates type-specialized instructions for common patterns that can be optimized

func (*Optimizer) InlineFunctions

func (o *Optimizer) InlineFunctions() *Bytecode

InlineFunctions inlines small functions at call sites to reduce call overhead Pattern: OpGetGlobal/OpConstant (callee), args..., OpCall -> inlined body

func (*Optimizer) Optimize

func (o *Optimizer) Optimize() *Bytecode

Optimize runs all optimization passes

type SourceLocation

type SourceLocation struct {
	Line   int
	Column int
}

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

func DeserializeSourceMap(r io.Reader) (*SourceMap, error)

Deserialize reads the source map from a binary format

func NewSourceMap

func NewSourceMap() *SourceMap

NewSourceMap creates a new empty source map

func (*SourceMap) Add

func (sm *SourceMap) Add(offset int, loc SourceLocation)

Add maps an instruction offset to a source location

func (*SourceMap) FormatError

func (sm *SourceMap) FormatError(offset int, message string) string

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) GetLine

func (sm *SourceMap) GetLine(lineNum int) string

GetLine returns a specific source line (1-indexed)

func (*SourceMap) Serialize

func (sm *SourceMap) Serialize(w io.Writer) error

Serialize writes the source map to a binary format

func (*SourceMap) SetSourceFile

func (sm *SourceMap) SetSourceFile(path string, source string)

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 NewSymbolTable

func NewSymbolTable() *SymbolTable

NewSymbolTable creates a new symbol table

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

func (*SymbolTable) Resolve

func (s *SymbolTable) Resolve(name string) (Symbol, bool)

Resolve finds a symbol in the symbol table or outer scopes

Jump to

Keyboard shortcuts

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