CS 358 Assignment 5 - Code Generation

due dates:
Part A: Thursday, 9 April
Part B: Friday, 24 April

This portion of the project is based on the scanner, parser, name-binder, and type-checker that have already been completed. It assumes as input an abstract syntax tree that has been annotated with name-binding links and type information. The purpose of this assignment is to extend the compiler so that it generates assembly code for the MiniJava language.

The starter file contains my version of the compiler through the type-checker. You may use your own version, or you may use mine.

Also, note that we will be using some instance variables in various AST nodes that we have so far not used (as we are discussing in class). Appropriate tree-display methods will display (or print) these values when a (-w, -pp or -p) switch is used.

The starter files also contain:

·       A copy of the mjLib.asm (and mjLibAnn.asm) file that contains the (non-annotating and annotating versions of the) MiniJava/MIPS runtime library code.

·       The source file CodeStream.java, which defines the class for writing text to the assembly language output file.

·       The object file CG2VisitorSimple.class, which performs a simplified version of string-literal generation, inherited through CG2Visitor.java. (It generates a distinct object for each string literal rather than generating a common one for identical string literals.)

First due date (Part A)

There are two due dates for this assignment. On the first due date (Thursday, 9 April), your compiler must produce code that correctly runs the following simple MiniJava program:

class Main extends Lib {
     public void main() {
          int xyz = 123;
          int abc = xyz+30;
          printInt(abc+6);
          printStr("\n");
     }
}

It must also work correctly on substantially similar programs—such as with different constants, different variable names, more than two variables used, and subtraction as well as addition. Your compiler’s performance for this milestone will constitute 20% of your grade for this assignment. For this milestone, no optimizations are expected. Your grade on this part of the assignment is more-or-less binary: does it work?

Final due date (Part B)

The other 80% of the grade depends on the behavior of the final version of your compiler (due Friday, 24 April).

For the basic assignment, you should generate code for the MiniJava language, except that

·       Your compiler does not have to produce the directives that define the unique string literals. (That is, you can use the CG2Visitor class that is supplied.)

·       Your compiler does not have to implement the switch statement.

If you wish, you may enhance your assignment by doing one or both of:

·       Produce a version of CG2Visitor.java that properly generates string literals when the program has equal ones (+0.5 grade).

·       Properly implement the switch statement (+1.0 grade).

·       Applying some optimizations to your code. (The MIPS simulator will report the number of simulated instructions and clock cycles used by your program. You will receive "optimization" credit based in the improvement shown by the code your compiler produces, when compared to that produced by the instructor's compiler. (Mine does effectively no optimization.) Possibilities for simple optimizations include:

o   Evaluate constant expressions (e.g., 3+4) at compile time.

o   Special-case method calls and instance-variable accesses that are prefixed by "this." so that they:

§  Don't test for null.

§  Access the variable directly via the this-pointer (in the case of instance variable access).

o   Eliminate the branch-around-empty-statement that can occur as a result of code like if (...) {...} else {}. (Note that this will happen routinely if the original program contained an if without an else.)

o   If there is only one version of a method (given the compile-time type of the object on which it is called), branch to it directly rather than going through the v-table.

o   (I will likely mention other opportunities for simple optimization in class.)

The grade-improvement for optimizations can be up to 2 letter-grades, and will depend on the improvement of selected correctly-running test cases.

On (or shortly after) the due date, your program will be run with my MiniJava test programs. I will likely supply a few test programs, but they are not likely to be comprehensive. You may share test data (e.g., MiniJava programs) with your classmates, and with me, if you wish.

Debugging

Debugging code that your compiler is generating is a bit different than debugging code that you've written yourself. You will likely want to use the MIPS debugger. You may also want to insert generate extra "print statements" for debugging purposes, so that you can trace what the compiler-generated code is doing. To print an integer (e.g., that is in $t0), you can call the library routine printInt as follows:

subu $sp,$sp,12 # adjust stack
sw $zero,8($sp) # dummy this-pointer
sw $s5,4($sp) # GC tag
sw $t0,($sp) # value to print
jal printInt

To print a (MiniJava-format) string object, you can emit:

subu $sp,$sp,8 # adjust stack
sw $zero,4($sp) # dummy this-pointer
sw $t0,($sp) # assuming $t0 has pointer to String object
jal printStr

If you don't get method-dispatch working, you can get a lot of the functionality by calling the method that corresponds to the compile-time type of the expression. This can be done treating all code generation for Call expressions as the "super" version.

You can get the MIPS simulator from the course web page. The mips.jar file is the Java binary (the name may include a version number); you can run the debugger by double-clicking on the mips.jar icon, or by dragging your asm-file onto the mips.bat icon. You can also run the program directly in a command-line window (without the debugger) by getting into a command-line window and typing mips someFile.asm (to assemble and run) or mips someFile.bin (to run an already-assembled executable).

Working in pairs

If you wish, you can work as part of a two-person team for this assignment. The rules for working in pairs are as follows:

·       Before pair-work starts, both team members should email me, advising me that they will be working as a pair.

·       Students working in a pair can freely share code with one another, examining each others’ code, debugging together, etc. Students should not be examining or sharing code except between members of a team.

·       The “baseline grade” for doing the basic assignment is B rather than A. If you wish to receive an A as part of a pair, you will definitely need to do some enhancements.

·       Students working in a pair will both receive the same grade on the assignment.

Handin

You should hand in both Part A and Part B of your program via learning.up.edu, in the same manner as in previous assignments.

You should hand in a zip-file that contains the following files:

·       Java files: your CG1Visitor.java (for Part B), CG3Visitor.java (for Parts A and B) and (optionally for Part B) CG2Visitor.java files. Also, hand in Java files for any "helper classes" that you may have defined to support these classes. These .java files should be at the top level of your zip-file—that is, your zip-file should contain no folders.

Do not hand in any other files.