COS 320, Spring 2000. Programming Assignment

Programming Assignment 6:  Code Generation

In this assignment you will compile the Fun intermediate language into MIPS assembly with virtual registers.   We will provide code to do register allocation for you.  The final compiler output can be run using the SPIM simulator.  In order to complete this assignment, you will have to use the SPIM simulator and understand the MIPS assembly language:

The SPIM Simulator.

This is an excellent document describing the MIPS assembly language, calling conventions, and using the SPIM simulator.  See, in particular, chapter A.9 for a description of the MIPS instructions you will use.  spim and xspim have been installed on the Linux OIT machines (eg: boater).  In addition, Guilherme added a symbolic link to spim and xspim in the ml directory that you should already have in your PATH, so that if you invoke spim or xspim on the command line from a Solaris machine (eg: arizona) it should work fine.  If you have difficulties with spim, please send e-mail to the cos320 list.

 

The SPIM Homepage.
 
If you would like to install SPIM on your own machine, source code and binaries can be downloaded from this site.  You can get PCspim for your PC here.  Documentation on PCSpim is here.

Sample Output.

        Here is the factorial function in fun.  Here is some commented output assembly.

Caution, documentation bug!

The first operand of jalr is the register where to put the return address (you must use $ra, which is what gencode_func, described below expects), and the second one is the register containing the address of the function to be executed.  NOTE: the order of the jalr operands is the OPPOSITE of what appears on the SPIM documentation.  [We have notified the implementor of SPIM.]

Instructions

  1. Copy /u/cos320/chap6/* and /u/cos320/chap6/tests/* into a new directory as6
  2. If you have your own working copies of the parser, lexer, etc from assignments 2, 3 and 4 you may use them for this assignment.  If you don't have working versions, then use the ones we provide.
  3. Your main job is to compile Fun language expressions into assembly language with virtual registers.  This involves understanding the mips.sig interface, which describes the subset of the MIPS architecture you will be compiling to, and gencode.sml, which you will need to understand and modify.  In gencode.sml, you will fill in the function gc_exp (a part of gencode_exp) to generate code for expressions.  You will have to figure out how to compile most of the expressions yourself.  However, here are some hints:
  4. To test your code generator on <file>.fun, use Compile.compile <file>.fun. This command will generate two files.  The first is called <file>.fun.noregalloc.s.  It contains the code that you generated.  Since this code contains virtual registers, you cannot run it on the spim simulator. The second file, called <file>.fun.s contains the final assembly output.  Note that the code in this second file will not be identical to the code that you generated because the register allocator has eliminated virtual registers and replaced them with real registers.  It also loads and stores values to and from the stack.  It can be executed using spim.  We are providing you with the binaries for the register allocator and liveness analysis (in the CM directory) -- writing your own version of these will be part of your future assignments.
  5. To test execution of your code, invoke spim in the most abstract mode (with no load/branch delay slots).  This is the default mode in spim and xspim.  To check the mode, go to simulator -> settings, make sure that the only checked boxes under execution are "Allow pseudo instructions" and "Load exception file"
  6. The code we are providing already handles a couple of expression patterns, enough to generate code for tests/test0.fun. You may want to compile this program and check its output (tests/test0.fun.s).
  7. Submit by running submit 6 gencode.sml.