Workshop In Computer Construction - From Nand to Tetris
Project 11 - The Compiler II: Code Generation
Deadline June 1st, 2003 at midnight
Description

Objectives: Build a compiler for a modern, object-based, Java-like language. The output of the compiler is VM code that can run on the virtual machine built in projects 7 and 8 defined earlier.

The contract: Write a Jack compiler. Use it to compile the Jack programs given below. Make sure that each translated program executes according to its documentation..

Test programs:
Program Description Source
Seven This trivial program computes the value of (3*2)+1 and prints the result at the top left of the screen. To test if your compiler has translated the program correctly, run the translated code in the VM emulator and make sure that it prints 7 correctly.

The purpose of this program is to test how your compiler handles a simple logic that includes an expression with integers only (no variables), a do statement, and a return statement.
Main.jack
Decimal-to-Binary Conversion This program converts a 16-bit decimal number to its binary representation. The program takes a decimal number from memory address 8000 (i.e. RAM[8000]), converts it to binary, and stores the individual bits in RAM[8001..8016] (each location will contain 0 or 1). Before the conversion starts, the program initializes RAM[8001..8016] to -1. To test if your compiler has translated the program correctly, put the translated code in the VM emulator and go through the following routine:
  • Put a 16-bit decimal value in RAM[8000];
  • Run the program for a few seconds, then stop it;
  • Check that RAM[8001..8016] contain the correct results, and that none of them contains -1.
This program is designed to test how your compiler handles all the procedural elements of the Jack language, i.e. expressions (without arrays or method calls), functions, and all the language statements. The program does not test the handling of methods, constructors, arrays, strings, static variables and field variables.
Main.jack
Square Dance This trivial game program enables moving a black square around the screen using the keyboard s four arrow keys. While moving, the size of the square can be increased and decreased by pressing the z and x keys, respectively. To quit the game, press the q key. To test if your compiler has translated the program correctly, run the translated code in the VM emulator and make sure that it works according to the above description.

This program tests how your compiler handles objects: constructors, methods, fields and expressions that include method calls. The program does not test the handling of static variables.
Main.jack
Square.jack
SquareGame.jack
Average Computes the average of a user-supplied sequence of integers. To test if your compiler has translated the program correctly, run the translated code in the VM emulator and follow the instructions displayed on the screen.

This program tests how your compiler handles arrays and strings.
Main.jack
Pong A ball is moving randomly on the screen, bouncing off the screen walls . The user can move a small bat horizontally by pressing the keyboard s left and right arrow keys. Each time the bat hits the ball, the user scores a point and the bat shrinks a little, to make the game harder. If the user misses and the ball hits the bottom horizontal line, the game is over. To test if your compiler has translated this program correctly, run the translated code in the VM emulator and play the game (make sure to score some points, to test the part of the program that displays the score on the screen).

This program provides a complete test of how your compiler handles objects, including the handling of static variables.
Main.jack
Bat.jack
Ball.jack
PongGame.jack
Complex Arrays Performs five complex calculations using arrays. For each such calculation, the program prints on the screen the required result versus the actual result (as performed by the program). To test if your compiler has translated the program correctly, run the translated code in the VM emulator and make sure that the actual results are identical to the required results.

The purpose of this program is to test how your compiler handles complex array references and expressions.
Main.jack

Important note:The Sack OS - the subject of Chapter 12 - was written in the Jack language. The source OS code was then translated (by an error-free Jack compiler) into a set of VM files, forming the OS program. Each time we want to run a given application program on the VM emulator, we must load into the emulator not only the .vm files comprising the application program, but also all the .vm files comprising the OS program. This way, when an application-level VM function calls some OS-level VM function, they will find each other in the emulator s environment.

Requirements: You should submit an executable called JackCompiler that takes path to a jack file or directory, and produce an xml file as the result of the parsing of the jack file. The usage is:

  • Single file: JackCompiler path/to/file.jack creates a vm file in path/to/file.vm
  • Directory: JackCompiler path/to/dir parses each of the files in the directory, and creates the corresponding vm files in the directory.
You should also supply a makefile to compile your code. Keep your code well organized and documented, as you would in every programming project. Your code should be based on the code you wrote for project 10. If there is any problem to do so, contact the TA ASAP. You should also supply a makefile to compile your code. Keep your code well organized and documented, as you would in every programming project.

Submission Submit a tar file (created by `tar cvf`) contains all your source files (well documented !), the JackCompiler shell script (if needed), the makefile and a README.
Resources
  • project11.zip (download): Extract to an empty directory, and keep the default directory structure as is.
  • Chapter 11 of the book - "The Compiler II: Code Generation".
  • Guidelines.