CS525 -- Programming Project Four



Due Wednesday, July 18th (make an appointment to demo your entire project)

In this final part of the project, you will implement an IC code optimizer and a simple MIPS code generator. This will be for a restricted language which does not include floats or subroutines. You will write your compiler so that it outputs MiniMIPS assembly language to the standard output instead of IC code (I had planned on having you generate the hex code, but this does not seem to be worth it in the time we have left). This code should follow the MiniMIPS language presented in the handout on the website and distributed in class.

Details of the Project

Your project must provide the following functionality:

Source Language

The source language will be the same as for project 3, except that no floats will occur, and no subroutine definitions or calls will occur. Test files are on the web siteand as before you are required to translate only these files correctly.

Target Language

The target language will be MiniMIPS, as defined in the MiniMIPS handout on the web site; You must output to the standard output an assembly language program in MiniMIPS (not hex). The main features of the language to keep in mind are the following:

Code Optimization

You must perform the following kinds of code optimization:

Code Generation

You must generate code using the method shown in class, by calculating next use information for all variables and then allocating registers using the algorithm from lecture (GetReg()).

Error Checking

There is NO requirement to do any kind of error checking in this final project. All test files will be positive tests which exercise your optimizer and code generator.

Friendly Advice

You can proceed one of two basic ways to do this: (1) Keep your IC code generator exactly as it is, and add code to optimize your IC program, and then perform the translation into MiniMIPS, or (2) modify your IC generator so that it generates MiniMIPS code, but without registers.

I did (1), because I had some existing code to work from, but I recommend strongly that you do (2). This is not the way most books explain the phases of the compilation process, but I think it is actually the simplest thing to do for this last project. Here are the steps I suggest you follow:

  1. Rewrite your IC generator to generate code which is just like MiniMIPS code but which uses variables instead of registers:
  2. Add various fields to the data structure for instructions (visited, leader, label, etc.) which will be used during the optimization process. Write your jump folding algorithm and traversal algorithm to find unreachable code.
  3. Determine the basic blocks by finding all leaders; this is quite simple, as you simply run through the code and mark as a leader the first statement, every target of a jump or branch, and every statement following a jump or branch. A basic block is a leader plus every following non-leader.
  4. Add a label for each statement which is its location in the original array; now a jump or branch is a jump to a statement with a particular label, rather than a location in the array. Now you can do the deletions of unreachable code.
  5. Add an array "nextUse" to each entry in the symbol table of some size sufficient to handle any realistic basic block (say 100 entries).
  6. Run through the array, and for each leader, stop and calculate for that basic block nextuse information (entering it into the symbol table arrays just mentioned) and then allocate registers as shown in lecture. During register allocation, you will replace a reference to the constant 0 by the register $0, you will leave a reference to a constant (which can occur only as the third argument to addi) unchanged, and you will replace a reference to a variable by a reference to a register. The latter process may involve generating lw or sw instructions (as discussed in lecture) and so you may need to add instructions to the code array (by moving the rest of the code down to provide room for the insertion). At the end of the basic block, you will have to store all registers back out to memory using a series of sw instructions (since in our naive method, no value in a register is considered valid past the end of the basic block.
  7. Now run through the code array and for each jump and branch, search for the location of the target by scanning the labels and adjust the jump targets. Finally, for each branch, replace the absolute location by an offset from the location of the instruction following the branch.

What to Hand In

Your group must make an appointment with me (call my assistant Dan at 358-2739 or send me email) to demonstrate your project. You will have to demonstrate your project on the test files for Project 3 and for Project 4, so be sure to save your version of Project 3, or provide a way to show me this functionality in some way. There is no other requirement for handing in anything related to the project.


Wayne Snyder