CSE 304/504: Compiler Design

Spring 2016

Homework #6, Final Code Generation

Due: May 9


Contents


Homework Description

In this assignment, you will complete your Decaf compiler by generating MIPS assembly code. For this assignment, you may assume that your input Decaf program is syntactically and semantically valid.

As in the past, you will produce a compiler that will take a single command-line argument, which is a file name; if that file contains a syntactically valid Decaf program, then it will construct an AST, perform type checking with name resolution, and intermediate code generation. Finally, (and the main step of this assignment) the compiler will generate final machine code for MIPS. It will output the machine code as a .asm file with the same name as the input Decaf file. For instance, if your compiler were invoked with foo.decaf, it should produce foo.asm.

Main Method:

This assignment will introduce an important assumption: that a program in foo.decaf has a class called foo, which has a public, static, void method called main (that has no parameters).

The main() method will form the entry point to your entire program.

You may assume that every program used with your compiler will satisfy the above requirement, and you do not have to explicitly check for this.

On output, the code for the main method must have as a label "main_entry_point" at its entry point. This is to make sure that we know where to start your program execution from.

In and Out:

For the purposes of this assignment, assume that the input decaf programs do not use the input and output methods in In and Out. Again, you do not have to check to see if the program satisfies this assumption.

CSE 504 vs CSE 304

Available Material

MIPS and SPIM

MIPS has been used for over 25 years in undergraduate Computer Organization and Assembly Language Programming courses. So it also has tons of very good reference material.

Project Path

  1. Start with building control flow graphs for each function in the intermediate code.
  2. Convert the graphs into SSA form.
  3. For each function, generate final code by
    1. Mapping temporary registers of the register abstract machine to MIPS registers.
    2. Translating abstract machine instructions to MIPS instructions.
  4. Go through MIPS emulator SPIM. (You may use MARS if you are more familiar with it, but my personal recommendation is to use SPIM).
    1. First generate code for programs that do not access objects and have no dynamic memory allocation (halloc).
    2. Implement halloc using features of the emulator.
    3. Specify .data layout to implement the static area and heap in MIPS.
    4. Output final code to a .asm file that can be immediately executed in the emulator.
  5. Proceed by adding field accesses first, and finally method invocations and constructors.
  6. For CSE 504 students only: work on conversion to SSA form after all else is completed.

Deliverables

Your Decaf compiler should be organized into clearly labeled files with each files function stated in a Readme file. The top-level will be in decafc.py, a file containing the main python function that ties the modules together, takes command line argument of input file name, and processes the Decaf program in that file.

decafc takes a single command line argument, with the name of the Decaf program to be translated. For example, if the Decaf program is in fum.decaf, then the invocation will be as

       python decafc.py fum  
or
       python decafc.py fum.decaf  
The translated MIPS program will be written to a file with the same base name with ".asm" extension (fum.asm in the above example).

Grading

This maximum score on this assignment will be 40, divided as follows: 20 points for register allocation without spilling, and no method calls. This involves correct construction of register interference graph, which in turn needs correct computation of liveness information; 10 points for correct issue of MIPS instructions; 5 points for correct handling of method calls and parameter passing. 5 points for correct handling of heap access and allocation. In addition, 5 points for SSA form originally allocated for hw5 may be earned here. Make sure you specify this clearly in Readme.

As usual, complicated programs may not get the full grade unless accompanied by comments documenting it.

Submission

Submit your homework using Git, as done for HW1, before midnight of the posted deadline.

You may submit the homework multiple times; the last submission will be graded.

Change Log


C. R. Ramakrishnan
Last modified: Mon May 2 14:41:10 EDT 2016