Decaf

PA5: Decaf ILOC Generator

Objective

The goal of our semester-long project is to gain experience in compiler implementation by constructing a simple compiler.

The goal of this part of the project is to gain experience implementing code generation by converting Decaf ASTs into a linear code (ILOC).

Introduction

The semester-long project for this course is to build a compiler for the Decaf language. In this project, you will implement code generation for Decaf, which will be the fourth phase in our compiler. You will do this by traversing the annotated AST from the fourth phase, generating equivalent ILOC code.

You should download the project starter files from the "Files" tab in Canvas. The starter files include many new .java files as well as .class files representing compiled solutions.

Before you begin writing code, you should spend some time reading through all of the infrastructure files provided in the starter files. Our compiler this semester is a large project. Your solution will need to interface with several other classes, and you will need to thoroughly read through those classes to understand their interactions. Also, there are utility functions that you may find useful.

If you have questions about the existing code, please ask them on the Piazza forum (see the link on the sidebar in Canvas).

For this project, you will be implementing MyILOCGenerator, a subclass of the ILOCGenerator. These classes handle the conversion from an annotated ASTProgram to sequential ILOC code in an ILOCProgram structure.

To help you test your code generation, I wrote a simple ILOC interpreter, which you can find in the ILOCInterpreter class. Its implementation may be of interest to you as you write your code generator. You can also use the interpreter to test your ILOC output, and I have added a final pass to DecafCompiler that invokes the interpreter on your generated ILOC code.

It may also help to turn on tracing in the interpreter, which will print out the system state after each instruction. To enable this, find the following line in DecafCompiler:

        ILOCInterpreter interp = new ILOCInterpreter();

Change it to the following:

        ILOCInterpreter interp = new ILOCInterpreter(true);

Assignment

WARNING: You should only proceed with actual development once you are SURE you understand exactly what your task is and how your code should interact with the rest of the system.

For this particular project, you should pay special attention to the following classes:

  • ILOCGenerator - Base class for ILOC code generators.
  • ILOCProgram - Collection of ILOC functions.
  • ILOCFunction - Collection of ILOC instructions.
  • ILOCInstruction - Single ILOC instruction.
  • ILOCOperand - Single ILOC operand. This class also handles generation of temporary registers and anonymous labels.
  • ILOCInterpreter - Interprets ("executes") an ILOC program.
  • AllocateSymbols - AST traversal that calculates static coordinates and runtime locations for all symbols. This pass must be completed before code generation.

Implement the MyILOCGenerator class. This project is by design more open-ended than any of the previous projects in this course. There are infinitely many ILOC programs that are equivalent to any given Decaf program. Some are more efficient, some are easier to analyze, some are easier to read. Thus, your generated output may not exactly match the sample output below, and that is fine. I will be testing your emitted code for correctness (the majority of your grade), efficiency, and readability.

NOTE: You can ignore the PHI instruction--it's for static single-assignment form, which we will not be using explicitly in this project.

NOTE: You can also ignore the ILOCBasicBlock class--it's for building and analyzing control flow graphs, which we will not be using in this project.

Sample Input

def int add(int x, int y)
{
    return x + y;
}

def int main()
{
    int a;
    a = 3;
    return add(a, 2);
}

Sample Output

add:
  loadAI [bp+8] => r1
  loadAI [bp+12] => r2
  add r1, r2 => r3
  i2i r3 => ret
  jump l1                               // return (x+y)
l1:
  return

main:
  loadI 3 => r4
  storeAI r4 => [bp-4]                  // a = 3
  loadAI [bp-4] => r5
  loadI 2 => r6
  param r6
  param r5
  call add
  i2i ret => r7
  i2i r7 => ret
  jump l2                               // return add(a, 2)
l2:
  return

Result: 5

Submission

DUE DATE: Friday, March 24, 23:59 EDT (11:59PM)

Please submit your MyILOCGenerator.java file to the appropriate assignment on Canvas.