Laboratory assignment · 1 October
Lab 8: VM Translator, Part II
Extend the Project 7 translator into a full VM-to-Hack translator by adding program flow, function calling, return handling, multi-file translation, and bootstrap code.
Back to course homeObjective and contract
Complete the VM translator
Retain every Project 7 feature and add the remaining VM commands.
Translate complete VM programs into Hack assembly
The generated assembly must conform to the VM specification and produce the results required by the supplied CPU-emulator tests and compare files.
- Extend
- The Python
VMTranslator.pydeveloped for Project 7. - Add
label,goto,if-goto,function,call, andreturn.- File input
- Translate one
Xxx.vmfile into oneXxx.asmfile. - Directory input
- Translate every
.vmfile in directoryXxxinto one combinedXxx.asmprogram.
Implementation plan
Build it in two stages
Unit-test program flow before adding stack frames and function calls.
Program flow
Implement the VM branching commands:
label labelNamegoto labelNameif-goto labelName
Ensure labels are scoped so that identically named labels in different functions do not collide.
Functions and bootstrap
Implement:
function functionName nVarscall functionName nArgsreturn- Translation of all
.vmfiles in one directory - Bootstrap code that sets
SP=256and callsSys.init
Test programs
Follow the supplied test progression
Each test isolates a new layer of translator behavior.
BasicLoop
Computes 1+2+...+n and pushes the result. Tests goto and if-goto.
FibonacciSeries
Stores the first n Fibonacci numbers in memory. This is the more demanding branching and array-access test.
SimpleFunction
Performs a calculation and returns the result. It is the first focused test of function and return.
FibonacciElement
A full recursive-call test. Compile the entire directory containing Main.vm and Sys.vm into one FibonacciElement.asm file.
StaticsTest
Tests static variables belonging to different VM files. Compile the entire directory into one StaticsTest.asm file.
Do not translate the files in a multi-file program separately. FibonacciElement and StaticsTest must each produce one combined assembly file from the complete directory.
Function-call state
What the generated assembly must preserve
A function call must be able to return to the caller with its memory-segment view restored.
Create the callee's frame
- Push a unique return address.
- Save the caller's
LCL,ARG,THIS, andTHAT. - Reposition
ARGfor the callee's arguments. - Set
LCL=SPand jump to the called function.
Restore the caller's frame
- Recover the return address from the current frame.
- Move the return value to
argument 0. - Restore
SPimmediately above the return value. - Restore
THAT,THIS,ARG, andLCL, then jump to the return address.
Initialization
Know when bootstrap code is expected
The tests deliberately introduce bootstrap responsibilities gradually.
- First three
BasicLoop,FibonacciSeries, andSimpleFunctionuse test scripts that manually provide the initialization they need.- Last two
FibonacciElementandStaticsTestexpect your generated program to contain bootstrap code.- Bootstrap
- Set
SPto 256 and generate the equivalent ofcall Sys.init 0.
Testing and debugging
Test every stage before continuing
Use the VM emulator to understand behavior and the CPU emulator to test generated assembly.
For each test program
- Run the program in the VM emulator with
XxxVME.tstto understand its intended behavior. - Translate the required
.vmfile—or the complete directory for a multi-file program—into one.asmfile. - Inspect the generated assembly for syntax, label-scope, stack-frame, and control-flow errors.
- Run the assembly on the CPU emulator using
Xxx.tst. - Compare the result with
Xxx.cmp. Correct the translator and regenerate the assembly if the test fails.
Implement in the supplied order. Later programs depend on earlier features, so skipping directly to recursion or multi-file translation makes failures much harder to diagnose.