JACCL Compiler Implementation Project
Phase 3.1: Optimization Techniques -- Constant Folding
Due: April. 28, 1999

In theory, you now have a working compiler. Unfortunately, the code it produces is quite inefficient. For the remainder of the semester, what I'll ask you to do is implement some optimization techniques that should greatly improved the quality of the code your compiler produces.

First, if I were you, I would not tamper with a working compiler.

Make a copy of the directory containing the final version of the compiler you produced for phase 2.3 so that you can modify this fresh copy to produce phase 3.1 while leaving the working version alone. Having the old version around may help you track down bugs in the new version. It will also make it possible to compare run-times of generated code to see how much of a difference the optimizations make. You might even want to change the permissions on the files in the "working version" directory so that you can't modify those files accidentally. (If you are wise enough to be using rcs or the equivalent, feel free to ignore this advice.)

Now, as a first step in the optimization process I want you to implement procedures to perform the constant folding/algebraic simplification techniques I have described in class. You should view this processing as a phase between semantic processing and code generation. In particular, don't integrate the constant folding process too tightly with your expression code generation routines. I expect you to write a separate "constantfold" procedure. This procedure will need to be applied to each expression in the syntax tree before code is generated for the tree.

As part of constant folding, make sure that you include a transformation that will convert a refvar node with a base address subtree consisting of an Nplus with a constant operand into a form where the constant operand is used as the displacement in the refvar node and the Nplus node is eliminated.

Feel free to take advantage of constant folding in other imaginative ways. For example, in class someone asked about the impact of a condition in a loop or if statement simplifying to a constant. You could try to be clever enough in such cases to at least generate an unconditional branch and possibly even avoid generating any code for the branch not taken.

You will have to make up your own data types for expressions in canonical form, etc. There is, however, a new subdirectory of "~tom/pub/434" for phase 3 of the project. Make sure to change the PHASE variable in your Makefile so that you will use these definitions and to do a "make depend" afterwards.

The main change in the .h files in the "~tom/pub/434/phase3" subdirectory is the addition of a field to hold a opdesc pointer to the tree node type. This new field will not be used until phase 3.2.


Computer Science 434
Department of Computer Science
Williams College