CS 375: Code Generator

Due: August 14, 2009.

For this assignment, you are to write a code generator, using as input the intermediate code produced by your parser that was written for the previous assignment.

We will assume that the target machine is a SPARC, as incorporated in the Sun workstations. In order to simplify matters, we will assume that C calling conventions will be used. Programs called genasm.c and genasm.lsp are provided; these programs generate assembly language output (although machine language relocatable binary output could be generated from the same calls).

The files codgen.c and codgen.lsp are initial versions of the code generator that you may use as a starting point. This code generator is sufficient to generate code for the trivial Pascal program triv.pas, producing the file triv.s .

Generate code for the input files graph1.pas and pasrec.pas. Extract the code from the output file using a text editor to form the file code.s and run the code with driver.c .

It will be necessary to execute the code on an appropriate machine, such as the Sun workstations. If you wish, you can rlogin or telnet to one of these to test your program (the command cshosts pubsun will give a list of public Sun's). Use the commands:

   cc driver.c code.s -lm
   a.out
where code.s is your generated code file. Note that you are required to use a Sun and its C compiler only for the final run of the generated code; you may use other computers or compilers in writing your programs if you wish. Note the warning at the top of genasm.c if you use Linux.

The files graph1-unopt.s and pasrec-unopt.s are examples of unoptimized code similar to your initial generated code. The files graph1.s and pasrec.s are optimized code; examine the two versions to see how optimization reduces the code size and execution time. The driver program driver.c is used to call the code you generate so that it can be executed. The file driver.s is available for your inspection so that you can see how your program is called. graph1.c is graph1.pas rewritten in C; graph1-cc.s is graph1.c compiled by the cc compiler on the Sun workstation. The file pasrec.c may be useful to help you see the relation between Pascal and C syntax of pointer usage; compiled versions of pasrec are also available.