Programming Assignment 7
Due Thursday, May 8

Introduction

In this assignment you will implement code generation for Cool. This assignment is the end of the line: when completed, you will have a fully functional Cool compiler.

The code generator makes use of the AST constructed in PA3, the static analysis performed in PA4 and PA5 and the global structures created in PA6. Your code generator should produce MIPS assembly code that faithfully implements any (statically) correct Cool program. This assignment is one of the most difficult in the course, but you should be familiar with the AST structure used, especially from PA4. Do not delay starting this assignment.

Files and Directories

To get the assignment type

make -f /afs/cs/users/classes/cs654/assignments/PA7/Makefile
in a directory named PA7. This command copies a number of files to your directory, some of them with read-only permission. As usual, you should not modify files that are read-only. Please read and follow the directions in the README file.

The files that you may need to modify are:

In addition you are given read-only access to the following files:

There will be many other files in your directory containing other pieces of the compiler. These files were described in previous handouts and are listed in the README file. The files we will collect and use to grade your assignment are the first set listed above; you should not modify any other files.

Designing and Testing the Code Generator

You will need a working lexer, parser, and semantic analyzer to test your code generator. See the README file for instructions on how to use either your own components or the components from coolc. It is wise to test your code generator with the coolc lexer, parser, and semantic analyzer at least once, because we will grade your code generator using coolc's version.

See the README file for instructions for compiling and running a compiler with your code generator. For your convenience, a command line debugging flag -c is included in the skeleton, which controls the global variable cgen_debug.

There are a number of things you must keep in mind while designing your code generator. First, your code generator must work correctly with the Cool runtime system, which is explained in the Cool Tour handout. Second, you should have a clear picture of the runtime semantics of Cool programs. The semantics are described informally in the first part of the CoolAid, and a precise description of how Cool programs should behave is given in Section 13 of the manual. Third, you should understand the MIPS instruction set. An overview of MIPS operations is given in the spim documentation, which is on the class Web page.

Code Generator Interface

The semantic analyzer decorates the AST with information that will be useful to you while writing the code generator. The following table summarizes this information:

constructor or phylum attribute meaning
program obj_class AST for class Object
  int_class AST for class Integer
  str_class AST for class String
  bool_class AST for class Boolean
class super AST for super class (or NULL for Object)
method overrides AST of method being overridden (or NULL)
  return_of_class AST for class of return type
attr of_class AST for class of type
formal of_class AST for class of type
branch of_class AST for class of type of branch variable
  type symbol for type of value of expression in branch
Expression type symbol for type of value of expression (or NoType)
let of_class AST for class of type of let variable
object binding description of variable being used
assign binding description of variable being assigned
dispatch method AST for (statically selected) method
static_dispatch method AST for method selected
new_ of_class AST for class being instantiated.

Optimization

In a word: don't. For CompSci 654, we do no optimization. Optimization is a fascinating topic, but with many traps for the unwary. If you are interested in optimization, take an independent study course, or take CompSci 854. Do not attempt optimization for PA7. Instead, use the time writing exhaustive test cases and ensuring your code generator is completely correct. You may even wish to work on other classes!

Garbage Collection

The standard Cool compiler works with garbage collection. Your compiler need not work with garbage collection. If you decide to use the collector, be sure to carefully review the garbage collection interface described in the Cool Tour. Ensuring that your code generator correctly works with the garbage collector in all circumstances is not trivial.

Spim and XSpim

You will find spim and xspim useful for debugging your generated code. xspim works like spim in that it lets you run MIPS assembly programs. However, it has many features that allow you to look at the memory, registers, data segment, and code segment of the code. You can also set breakpoints and single step your program. Look at the documentation for spim/xspim on the class home page.

Warning: One thing that makes debugging with spim difficult is that spim is an interpreter for assembly code and not a true assembler. If your code or data definitions refer to undefined labels, the error shows up only if the executing code actually refers to such a label. Moreover, an error is reported only for undefined labels that appear in the code section of your program. If you have constant data definitions that refer to undefined labels, spim won't tell you anything. It will just assume the value 0 for such undefined labels.

Overview

The basic stages of the code generator for this assignment are as follows:

  1. Compute the number of temporaries for a method, and assign temporaries where they are needed.
  2. Write the function prologue and epilogue code generators
  3. Generate code for each method.
Your test case example.cl should also test all aspects of the code generation.



John Tang Boyland 2008-04-24