CS 374 Compilers
Homework #8

Due: Thursday 4/26 at the beginning of class

Note: This work is to be done in assigned groups.  Each group will submit one assignment.  Although you may divide the work, both team members should be able to present/describe their partner's work upon request. 

Code Generation - Part III

At this point, we can generate code that prints compound arithmetic and logical expressions, create new objects, make simple method calls (without dynamic method lookup), and return results.  Now we extend our implementation to reflect the way Java performs dynamic method lookup.

0. Preparation: Read section 14.2 on static versus dynamic method lookup.  In our previous implementation, a new object has a class identifier (e.g. 0, 1, 2, 3).  We will be replacing these with the address of a lookup table for method addresses, much like those shown in Figure 14.3.

1. Symbol Table Addition: You'll want to add to your symbol table such that it will be easy to:

2. Allocating Tables:

Dynamically allocate c words of memory (using the sbrk syscall) and store the address to your tables variable, where c is the number of classes.
In this table, for each class, dynamically allocate m words of memory and store the address in the main table, where m is the number of methods for each class. That is, class i should have an address at the ith word of the table which points to another table with a size in words equal to the number of methods in class i.
In each class table, load the addresses of the appropriate address labels for each method call.  Your symbol table additions should help you compute these labels.

3. NewObject: Change new object such that the class identifier is replaced by the address of its dynamic method lookup table.

4. Call: Change your call implementation such that it uses the objects dynamic lookup table to retrieve the correct method address and jump to this address.  That is, it loads the table address from offset 0 of the object.  Then it loads the method address from the appropriate offset in the table.  Finally, it jumps unconditionally to that address, placing the next instruction in $ra.  Note: there seems to be a discrepancy between the documentation and the simulator.  If you have your jump address in $v0, use "jalr $ra, $v0".

Now retest your method calls/returns with NewObject.java, Factorial.java, and DynamicMethodLookup.java.

5. While:

Now you can test your code with BinaryTree.java and LinkedList.java

6. NewArray: Allocating a new array is similar to allocating an object.  Allocate an extra word at the beginning which holds the length of the array.

7. ArrayLength: Generate code for the integer array expression. Then load the length into $v0 from the first word of the array.

8. ArrayLookup:

9. ArrayAssign:

Now you can test your code with the remainder of the test cases: SimpleArray.java, BinarySearch.java, BubbleSort.java, LinearSearch.java, QuickSort.java, and TreeVisitor.java.

Congratulations!  You have completed your MiniJava compiler!