Project 4: Code Generation

For the final project of the semester, you will complete your compiler by adding a code generator. We have discussed code generation extensively in class, so this assignment won't specify it in too much detail. The rules are simple: your compiler must read in a C-flat program as specified by the C-flat overview, and emit an X86 assembly language program that can be assembled, linked, and run correctly.

Your compiler should be invoked as follows:

% cflat filename.cflat filename.s
Obviously, filename.cflat is the input file, and filename.s is the output assembly file. It's ok if the compiler prints some information on the standard output as it goes; we will only look at the output assembly code.

The most important requirement is simply that the output code should work correctly. It need not be optimal, it need not be pretty, it need not be simple, but it must work correctly. You may use a simple non-spilling register allocator; it's ok if the compiler stops with an "out of registers" error message on really complicated expressions.

You should start implementing the code generator by using what you submitted as the solution for project 3. For the final project, you may assume that the input C-Flat code is correct, so if your semantic checker isn't perfect, you won't lose points on project 4. It's quite possible that this final stage of the project will require you do go back and make a few changes to earlier stages. For example, you will discover that to properly handle string constants in the middle of expressions:

int main()
{
	write_string("hello");
}
You must first define a global string with an anonymous name like .S0 and then emit it at the top of the program:
.data
.S0:
        .string "hello"
.text
 .global main
main:
        pushl   %ebp
        movl    %esp, %ebp
        pushl   $.LS0
        call    write_string
        leave
        ret
To make this work, you will have to create a table of immediate strings in the semantic checking phase. You will certainly find other examples of this problem.

Grading

The assignment will be graded out of 100 points and is worth 10 percent of the course grade. Grading will be based on functionality, but partial credit will be assigned if, due to clearly formatted and commented code, the grader can determine what is going on. As usual, up to 5 points extra credit will be given for any significant improvement to the compilation process or extension to the language and will be based logarithmically on the difficulty of the addition.

Deliverable

This project is due at 5PM on Wednesday, May 3rd. Project source should be archived into a tar file (compression optional) and placed in the /afs/nd.edu/coursesp.06/cse/cse40243.01/dropbox/NETID/codegen directory, where NETID is your Notre Dame NetID.

An extra credit attempt for this project should be documented in a file called EXTRA.codegen at the root of your submitted project package. The documentation should explain what has been done to extend/enhance the project and how to go about testing this claim. The submitted package should also contain any test code if required by the nature of the extension/improvement.