Project 7
Project 7
In this project you will build the first half of the first half of a typical two-tier compiler. Specifically,
the Jack compiler generates VM code for a virtual machine, much like a Java compiler generates
Bytecode. The VM code is then translated further into machine language, using a program named
VM translator. In this project you will develop a basic version of this translator, and in the next
project you will complete it. This VM Translator is sometimes referred to as the compiler’s
backend.
Basically, you have to write a program that reads and parses VM commands, one command at a
time, and generates Hack instructions that execute the command’s semantics on the Hack
computer. For example, how should the VM translator handle an input like "push constant 7"?
Answer: it should output a sequence of Hack assembly instructions that implement this stack
operation on the host RAM. Code generation – coming up with a sequence of Hack instructions
that realize each one of the VM commands – is the very essence of this project.
Objective
Build a basic VM translator that implements the arithmetic-logical and push/pop commands of the
VM language. For the purpose of this project, This version of the VM translator assumes that the
source VM code is error-free. Error checking, reporting and handling can be added to later versions
of the VM translator, but are not part of this project.
Resources
You will need three tools: the programming language in which you will implement your VM
translator, and the supplied VM emulator and CPU emulator, available in your nand2tetris/tools
folder.
The CPU emulator enables executing and testing the assembly code generated by your VM
translator. If the generated assembly code runs correctly in the CPU emulator, we will assume that
your translator performs as expected. This of course is just a partial test of the translator, but it will
suffice for our purposes.
The VM emulator is a given, visual, VM implementation. It can be used to illustrate, and visualize,
how VM commands and programs impact the stack, the memory segments, and the relevant RAM
areas on the host RAM. Watching how VM commands impact the host RAM will help you figure
out how to realize the same impact using assembly code – a critical requirement for writing the
VM translator.
Contract
Write a VM-to-Hack translator, conforming to the Standard VM Mapping on the Hack Platform. Use
your translator to translate the supplied test VM programs, yielding corresponding programs
written in the Hack assembly language. When executed on the supplied CPU emulator, the
assembly programs generated by your translator should deliver the results mandated by the
supplied test scripts and compare files.