Code Generation¶
Overview¶
CodeGenerationASTVisitor translates the type-checked Enriched AST into assembly instructions for the Stack-based Virtual Machine (SVM). The generated code is written to a .fool.asm file.
Target Architecture¶
The SVM is a simple stack machine with:
- A stack for operands and activation records.
- A heap for dynamically allocated objects and dispatch tables.
- A frame pointer (fp) and stack pointer (sp).
- A heap pointer (hp) that grows upward.
Activation Record Layout¶
Each function/method call pushes an activation record (AR) onto the stack:
high address
┌──────────────────┐
│ arguments │ (pushed by caller, right-to-left)
├──────────────────┤
│ return address │
├──────────────────┤
│ control link │ (saved fp of caller)
├──────────────────┤
│ access link │ (fp of statically enclosing scope)
├──────────────────┤
│ local variables │ (pushed by callee)
└──────────────────┘
low address
Object Layout (Heap)¶
Each object allocated with new is stored on the heap:
[ dispatch-table pointer ][ field_0 ][ field_1 ] …
The dispatch table is also stored on the heap and contains one entry per method (the code address of each method).
Key Code Patterns¶
Variable Access¶
A local variable at nesting level nl and offset off is reached by following (current_level − nl) access links and then loading from fp + off.
Function Call¶
- Push arguments.
- Push the access link (fp of the statically enclosing scope).
- Jump to the function label.
- On return, pop the AR.
Method Dispatch¶
- Load the object reference.
- Load the dispatch-table pointer from offset 0 of the object.
- Load the method address from the dispatch table at the method's index.
- Call the method address.
Object Creation (new)¶
- Push field values onto the heap.
- Store the dispatch-table pointer at the base of the new object.
- Return the object address.