View the Project on GitHub TiarkRompf/cs352

Project 5: Instruction Selection

Due 11:59pm Sunday Nov 22

Useful Links


You have already implemented a substantial sequence of the major steps in compiling a high-level language into low-level code. At the beginning of this project, we already have intermediate tree code for the input program, which you will now convert into x86-64 assembly-code instructions.

You will implement code generation for the x86-64, and run your compiled programs on the lab machines.


Before starting this project, familiarize yourself with Chapter 8 and 9 of the textbook. In addition to the lectures, this will give you a solid foundation for starting your code generator.

Getting Started

Source code is available in a tarball in the /homes/cs352 directory, which can be copied into your working directory and expanded by doing the following:

cp /homes/cs352/Fall15/project5.tar.gz ./
tar -xvf 

If you wish to integrate the project into an Eclipse project, follow the directions on this blogpost to create an Eclipse project using the included Makefile.

All changes for this project will be done to the translation file x64/Codegen.java in the project5/src subdirectory.

To make the project, type "make" as per usual. To run your project, go into the bin directory.

You can run the full compiler with the command:
 java mojo.Main file.mj
where file.mj is a mojo source code file.

You can also run the compiler with the command:

 java mojo.Main -main file.mj

The file p5.sh will run itself as a command that invokes mojo.Main from java if you wish

The flag -main indicates that the compiled file is to be treated as the "main" module, and will generate a standard "main" function as entry point for running the program.

For input file foo.mj the compiler will also produce output for the various intermediate forms of the translated program:

The compiler will also produce a final assembly code file foo.s which you can run by first compiling:
 gcc -o foo foo.s
and then running:
You may find gdb useful in debugging the code produced by your compiler as it executes.

The command

 gdb foo
will open the executable in the debugger.

The command

 b main
will set a breakpoint at the main function.

The command

will start the program running, stopping at the next breakpoint.

The command

steps to the next instruction.

The command

 display/i $pc
tells gdb to display the assembly code instruction at each program point.

You can find information on these commands and others (such as to examine the contents of registers and memory) using the help command.


You should only need to turn in your x64/Codegen.java file, since that is the only file you need to change. From the src directory submit your solution using the command:

  turnin -c cs352 -p project5 x64/Codegen.java

To verify your turned-in work, do:

turnin -c cs352 -p project5 -v


We will create a set of test cases, and then compare the output of your solution to our reference implementation. Grading test cases are intended to test separable functionality of your code generator. Towards the end of the project period, we may release the results of our own tests to demonstrate what the code generator should produce.

There will be a reconciliation period after initial grading when you will be able to compare outputs of your code generator with those of the reference implementation, and you may then offer simple fixes for the TAs to apply to your implementation to improve your grade. These fixes will be accepted only at the discretion of the TAs and the instructor. We will judge what "simple" means!

The reconciliation period is only intended for you to be able to fix simple problems that you may have mistakenly overlooked. Thus, you must make sure to test your implementation thoroughly.