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);

HINT: You will need to find some way to deal with break and continue statements. Note that they will only occur inside of a loop, but that it might be a nested loop. They should only continue or break with respect to the innermost loop that they are a part of.

HINT: Recall that function parameters should be evaluated in left-to-right order but are passed to the function in right-to-left order.

HINT: Booleans are stored as integers in Decaf/ILOC.

HINT: There is no modulus instruction in ILOC. You must figure out how to calculate it based on the instructions that you do have.

HINT: In this project, the visitor pattern begins to show one of its weaknesses: specifically that you can't write a completely custom visit method that interweaves visitor code with actual child node traversals. Thus, in order to handle all Decaf constructs correctly, you may find that you need to revert back to writing some traditional tree traversal methods.

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. There are a lot of useful helper methods here (especially the emit, base, and offset), so take the time to look through it.
  • 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.
  • AllocateStackSymbols - AST traversal that calculates static coordinates and runtime locations for all symbols. This pass must be completed before code generation. I have provided this code for you, but you should still look it over and make sure you understand what it is doing because you will need to use the results.

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.

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