COSC 170 Compiler Construction

Spring 2009

Project #5: Intermediate Representation

Implement a Translator for our dialect of the MiniJava programming language, as detailed below.
Project 5 may be completed in teams of three.
Due: Friday, April 17, 11:59PM CST.
Submit entire Project4 directory via turnin command on Morbius.

Directions

  • Read Chapters 6 and 7 of Appel.
  • Implement a translator that converts our Project 4 Abstract Syntax Tree into an Intermediate Representation (IR) tree.
  • Run the reference implementation of the translator on Morbius with the command: ~brylow/cosc170/Projects/translator program.java
  • Create your translator in a package called "Translate" with a main program in class "Main". My grading protocol will assume that your project can be compiled and run with the following command line: cd Project5; make; java Translate.Main inputfile.java.
  • Build a decent set of MiniJava testcases. Several exist in the book, and on the web. Having a good set of test inputs will be critical to your success in later phases of the project. The majority of project points will be assigned by running diff to compare your output against mine.
  • Debug until done.
  • Specification

  • To create the intermediate representation tree, you will need to create the following packages (refer to Appel chapters 6 and 7 for more information) :
  • Your Frame package should (at least) contain the following classes:
  • Your Mips package should (at least) contain the following classes:
  • Your Temp package should (at least) contain the following classes:
  • Your Translate package should (at least) contain the following classes:
  • The API for the Tree package is here.
  • Hints

    For each method in each class, you will create a Frame.Frame object.

    Since you are compiling for a MIPS processor your frame objects will be of type Mips.MipsFrame. The variables in each method will be referenced as temporaries (Temp.Temp objects) in the IR tree. In the intermediate tree language a temporary is similar to a register on an actual processor. You will need to implement methods in the Frame class to allocate temporaries for variables. There are two such methods: allocFormal() (to allocate temporaries for formal variables) and allocLocal() (to allocate temporaries for local variables). Each of these methods will return a Frame.Access object which stores the name of the temporary allocated. Because we trust a register allocation phase to take care of assigning all of our registers in a later pass, the Frame.Access object returned at this point will always be a Mips.InReg object. You will need to maintain a symbol table to bind the variable names with the corresponding Frame.Access objects.

    The IR Tree is made of objects of two main types: Tree.Exp and Tree.Stm. These classes are both abstract, and the objects in the trees will be made of objects of types which inherit from Tree.Exp or Tree.Stm. Classes that extend Tree.Exp are : BINOP, CALL, CONST, ESEQ, MEM, NAME, TEMP. Classes that extend Tree.Stm are : CJUMP, EXP, JUMP, LABEL, MOVE, SEQ.

    Tree.SEQ will be used to link together a sequence of statements. For example, if you have statements stm1,stm2,stm3. You will link them together by calling:


               SEQ(stm1,SEQ(stm2,stm3)).

    For each method in each class you will create a Frame.Frame and a method body (a tree rooted at a Tree.Stm). The Frame.Frame and the body together form a fragment (in this case a Translate.ProcFrag). Each Translate.ProcFrag will be added to a linked list (called frags) to be printed out when the pass is complete.

    The return type of each visitor is Translate.Exp. Do not confuse this with the Tree.Exp. Translate.Exp is another abstract class, and the four classes that extend Translate.Exp are:

             Translate.Ex (when visiting an expression, i.e. AddExpr)
             Translate.Nx (when visiting a statement, i.e. AssignStmt)
             Translate.Cx (when visiting a conditional, i.e. GreaterExpr)
             Translate.IfThenElseExp (when visiting IfStmt and similar nodes).

    Each of these subclasses of abstract class Translate.Exp have three methods to convert Translate.Exp objects to Tree.Exp and Tree.Stm objects :

             unEx() : converts to Tree.Exp.
             unNx() : converts to Tree.Stm.
             unCx() : converts to Tree.Stm in the form of a conditional statement.

    You will use these methods to convert from Translate.Exp objects to Tree.Exp objects. For example: If you have an Translate.Ex object called "x" and need to convert it to a Tree.Stm, you will need to call x.unNx().


    Back
    [Rev 1.04 2009 Apr 02 14:27 DWB]