CS 375: Code Generator

Due: April 29, 2024.

Write a code generator, using as input the intermediate code produced by your parser (the previous assignment).

We will assume that the target machine is x86-64. (On the lab machines, you can enter the command whatami to check.) We will assume that C calling conventions will be used. A file genasm.c is provided; this contains many small functions that generate assembly language output (machine language relocatable binary output could be generated from the same calls).

The file codgen.c is an initial version of the code generator that you may use as a starting point; copy this file to codegen.c and expand it for your code generator. The given code generator is sufficient to generate code for the trivial Pascal program triv.pas, producing the file triv.s . Un-comment the call to gencode() in the main() at the bottom of parse.y to call the code generator. There are several makefile options for making the compiler with the starter file and with your version; most people will use make compiler .

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; x86-64 is widely available. If you wish, you can use ssh to remotely login to a lab machine to test your program. Use the commands:

   cc driver.c code.s -lm
   a.out
where code.s is your generated code file.

The files graph1.s and pasrec.s are examples of unoptimized code similar to what your generated code may look like; this is considered a good solution that satisfies the assignment. There is much opportunity to improve this code for extra credit; be sure to mention any optimizations you have done in your readme file.

The 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-gcc.s is graph1.c compiled by the gcc compiler. (You can use gcc to produce .s versions by specifying the -S option.) 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.

Getting Started: