Lab 8: Code Generation for x86

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.

Lab Materials

  1. Slides

  2. Source Code

Assignment

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

  1. http://ref.x86asm.net/coder32.html

  2. http://www.cs.virginia.edu/~evans/cs216/guides/x86.html <- Note, this uses Intel syntax, so the instruction argument order is reversed.

  3. http://en.wikibooks.org/wiki/X86_Assembly/GAS_Syntax

  4. 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)

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.

Project Submission

You will write briefly in the report how your program works and any issues you had in writing and testing.