Reading

Setting Up the Project

Download the file hw7_dist.zip [link]. This distribution contains tree files—TigerC.java, ICodegen.java, and JVMGeneratorV.java. These files to should added to your project as follows:

tigerc
  --> src
        --> tigerc
               -- TigerC.java
               --> codegen
                    -- ICodegen.java
                    --> jvm
                        -- JVMGeneratorV.java

In class this week, I handed out paper copies of these files.

Your job

Software Design Considerations

In class this week, I handed out paper copies of the files. ICodegen is a target machine-agnostic interface for code generators, while JVMGeneratorV is an implementation of this interface that is defined by induction on the structure of a program's AST (and hence also implements the tigerc.syntax.absyn.AbsynVisitorV interface).

The TigerC class is a first attempt at building a main driver program for our compiler. In the version you have (which was my first draft), the code generator is invoked by the line

	prog.accept(new JVMGeneratorV(outp));

The iterative cycle of design and implementation is an inevitable aspect of the software design process, and you'll need to go through a bit of it here. In particular, a top-level call to the expression-level code generation is premature. First, we must generate the target code file's "prelude", build the standard library if it's unavailable, then generate the "main" for our program. The "accept" call really belongs in the emitMain method in JVMGeneratorV.

Next are some modification to the build.xml file. In the interest of getting you more familiar with this essential component of the project, the burden of these changes is on you this time:

Implement a partial code generator

In this assignment, you will implement a code generator for the Tiger constructs that do not involve the declaration or use of variables, functions, or types. The generator will target the Java Virtual machine. In particular, you are to complete the visit methods for the following AST constructs: ExpInt, ExpString, ExpNil, ExpOp, ExpIf, ExpIfElse, ExpSeq, ExpBreak, and ExpWhile

Ideas

For all of the interesting control constructs, you're going to need a notion of unique labels that can be generated as needed. I suggest adding a Label class to the tigerc.codegen package, with a constructor that generates a new, unique label name with each label instance.

John H. E. Lasseter