In this laboratory, you will write a program to generate x86 assembly from the csem IR.
Submission Due: the beginning of your lab session two weeks from the day it was assigned.
Begin the project by downloading the source code linked above. This template contains a lexer and parser for the csem intermediate form and contains many support routines for code generation. For this lab, you will need to generate working code for the test example named test.t. The provided makefile will build the example intermediate form into an executable by combining its resulting assembly with the file test.c and compiling both using GCC (using make test).
After the test program has been built, you can test it by simply executing the test program like any other program on your machine. If your program crashes, due to incorrect code generation, you can debug your program using the standard GDB debugger.
For code generation itself, see the listed assembly references below. Additionally, you can see the assembly generated by GCC by using the -S flag. For instance, executing gcc -m32 -S -o example.S example.c will produce an assembly file named example.S that is the result of compiling the C file example.c into x86 32-bit assembly code. You can use this flag to help you construct the correct code during code generation.
Reference Material for x86 Assembly
http://www.cs.virginia.edu/~evans/cs216/guides/x86.html <- Note, this uses Intel syntax, so the instruction argument order is reversed.
http://en.wikibooks.org/wiki/X86_Disassembly/Calling_Conventions
Once you have completed the first part of the laboratory, your code generator should be able to correctly generate code for all of the following integer operations: (Each operation carries 10% of your grade)
integer add
integer subtract
integer multiply
integer divide
integer modulus
integer arithmetic shift left
integer arithmetic shift right
integer return
You do not need to care about implementation of return until you feel all the values are being returned and printed on console correctly.
integer call
Your code should be able to print this to console Test: 100 10 if you run ./test. To be able to do this, you need to implement integer call and arguments which are defined in test.t file under tstcall. Here, we are making a function call to printf and passing 3 arguments to it using fi t4 3:
func tstcall bgnstmt 1 t1 := 10 t2 := 100 t3 := ``Test: %d %d\n`` argi t1 argi t2 argi t3 t4 := global printf t5 := fi t4 3 reti t1 fend
integer argument
Hint: Check cgen.y file for the implementation of assembly code for add operation.
Note: Because of a configuration issue, this lab does not currently build on cycle2 and, potentially, on some lab machines. It should build correctly on cycle1 and cycle3. If you encounter an issue, please try using one of those servers.
You will write briefly in the report how your program works and any issues you had in writing and testing.