Code Generator Assignment

Finally, 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 32-bit X86 assembly language program that can be assembled, linked, and run correctly.

Your compiler should be invoked as follows:

% cflat -codegen sourcefile.cflat sourcefile.s
If the program passes all earlier phases of compiling -- scanning, parsing, resolving, and typechecking -- then you should print a valid assembly language program to the file specified in the third argument and exit with status zero. If some error is encountered along the way, then you must print a reasonable error and exit with status one.

Your program will need a runtime library to operate correctly. You may use this libcflat.c as a starting point. A print statement in C-Flat should result in one or more calls to the C functions print_string, print_integer, etc, depending on the type of the expression. You may add additional additional runtime calls as you see fit.

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()
{
	print "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    print_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.

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.

As before, the options from the previous three projects must continue to work correctly. You must also turn in a set of test files named good[1-5].cflat and bad[1-5].cflat.

Your grade on this project will be based upon:

  • Correctness of the assembly output. (90 percent)
  • Good programming style. (10 percent)
  • Turn in all of your source code and supporting files to the dropbox directory:
    /afs/nd.edu/coursefa.08/cse/cse40243.01/dropbox/YOURNAME/codegen