CS451/651 Compilers
Bill Campbell
Spring 2008
Programming Exercise 4: Analysis and Code Generation I

Your job here is to implement analysis and code generation for the following constructs, which you parsed in programming exercise 3. You will ant to see Chapter 7 (and other chapters) of the JVM Spec: http://java.sun.com/docs/books/jvms/second_edition/html/VMSpecTOC.doc.html.

1.     The interface type declarations. There are several issues here. 

·      You will want to declare the type defined in the same way we do for class declarations.  That is, JInterfaceDeclaration will ant a preAnalyze(), analize(), and codegen() that do roughly the same things as they do in a class declaration.

·      Note that for the JVM we represent an interface as a class, but with  “interface” and “abstract” modifiers.  Methods must have the “abstract” and “public” modifiers in the JVM.

·      You will want to deal with the implements clause in class declarations.  But there is, in addition, the problem of insuring that the class actually does implement all of the methods declared in the interfaces (or, is indeed itself abstract).

·      What happens when a variable declared with an interface type is the target of a message expression (or field selection); does it affect the code generated, e.g. for invoking the method? 

·      Make sure the proper code is generated.  See what javac does!

2.     The throw-statement and the throws clause.

3.     try-catch-finally statements (ie, syntax for exception handling)

4.     The for-statement, including both the basic for-statement and the enhanced for-statement. Consider a little tree surgery for the latter.

5.     Static blocks (initializers) and instance blocks (initializers).  Again, see what javac produces for code.  You will want to put code into cinit and init methods for these initializers.

It’s probably best to write a set of test programs first. Then you can compile them with javac to see what it generates for code.  And you have some measure as to when you are done.   (Welcome to extreme programming.)

Hand in hard copy of your program, your test program and their results, and your narrative no later than 5:30, Wednesday, April 23.  And, as usual, put your tree in p4/ of your course directory.

Your narrative will be very important in this assignment, since there are things left unsaid. This assignment makes use of many classes, I strongly suggest that you start out on this assignment immediately; there is lots of programming to do, there is lots of testing to do, and probably there will be lots of repairing to do. And, of course there is lots of writing to do. Have fun!

grade sheet