Operating System Design :: Projects :: Virtual Machine Part 1
Problem
The files for this project are in the projects directory under 07 when you downloaded the class software.
When you compile a Java or C# program, the compiler generates code written in an intermediate language called Bytecode (or IL for C#). In theory, this code is designed to run in a virtual machine environment like the JVM (or CLR), but before the VM code can be executed on a real computer it first has to be translated into the native code of that computer. The programs that carries out this latter translation is called VM Translator.
Write a VM-to-Hack translator, conforming to the VM Specification, Part I (book section 7.2) and to the Standard VM-on-Hack Mapping, Part I (book section 7.3.1). Use your VM translator to translate and test given VM programs, yielding corresponding programs written in the Hack assembly language. When executed on the supplied CPU Emulator, the translated code generated by your translator should deliver the results mandated by the test scripts and compare files supplied below.
The relevant reading for this project is Chapter 7. You will need two tools: the programming language with which you will implement your VM translator, and the supplied CPU Emulator. This emulator will allow you to execute, and test, the machine code generated by your VM translator. Another tool that comes handy in this project is the supplied visual VM Emulator. The emulator allows experimenting with a working VM implementation and with the given VM programs before you set out to translate them.
Instructions
Chapter 7 includes a proposed, language-independent VM Translator API, which can serve as your implementation's blueprint.You should build your VM Translator in two stages. This will allow you to unit-test your implementation incrementally, using the test programs supplied. In what follows, when it says "your VM Translator should implement some VM command" it means "your VM Translator should translate the given VM command into a sequence of Hack assembly commands that accomplish the same task".
Stage I: Handling stack arithmetic commands: The first version of your VM translator should implement the nine arithmetic / logical commands of the VM language as well as the push constant x command (which, among other things, will help testing the nine former commands). Note that the latter is the generic push command for the special case where the first argument is constant and the second argument is some decimal constant.
Stage II: Handling memory access commands: The next version of your VM Translator should include a full implementation of the VM language's push and pop commands, handling the eight memory segments described in Chapter 7. We suggest breaking this stage into the following sub-stages:
- You have already handled the constant segment;
- Next, handle the segments local, argument, and that;
- Next, handle the pointer and temp segments, in particular allowing modification of the bases of the this and that segments;
- Finally, handle the static segment.
Testing
Five VM programs are supplied that are designed to unit-test the staged implementation proposed above. For each program Xxx there are four files, as follows. The Xxx.vm file contains the program's VM code. The XxxVME.tst script allows running the program on the supplied VM Emulator to glean the program’s intended operation. After translating the program using your VM Translator, the supplied Xxx.tst script and Xxx.cmp compare file allow testing the translated assembly code on the supplied CPU Emulator.
In order for any translated VM program to start running, the translated program (written in Hack assembly code) must include a preamble startup code that forces the VM implementation to start executing it on the host platform. In addition, in order for the translated code to operate properly, the base addresses of the virtual segments must be stored in the correct locations in the host RAM. Both issues, startup code and segments initializations, are implemented in the next project. The difficulty of course is that you need these initializations in place in order to run the test programs of this project. The good news is that you should not worry about these issues at all since the supplied test scripts carry out all the necessary initializations in a manual fashion (for the purpose of this project only).
For each one of the five test programs supplied, follow these steps:
- To get acquainted with the intended behavior of the supplied test program Xxx.vm, run it on the supplied VM Emulator using the supplied XxxVME.tst test script.
- Use your VM translator to translate the supplied Xxx.vm file. The result should be a new text file containing Hack assembly code. The name of this file should be Xxx.asm.
- Inspect the translated Xxx.asm program. If there are visible syntax (or any other) errors, debug and fix your VM translator.
- To check if the translated code performs properly, use the supplied Xxx.tst and Xxx.cmp files to run your translated Xxx.asm program on the supplied CPU Emulator. If there are run-time errors, keep working on your VM translator.
Implementation Order: The supplied test programs were carefully planned to test the specific features of each stage in your VM implementation. Therefore, it's important to implement your translator in the proposed order, and to test it using the appropriate test programs at each stage. Implementing a later stage before an early one may cause the test programs to fail.