Assignment 5, Generating Assembly Code
Due Monday, March 27


Having generated intermediate code, you are now faced with actually generating assembly.

Unless it is impossible for you, you should be generating assembly code for an x86 machine running Linux, with GCC installed. If you do not have easy access to such a machine, please contact Ben or Emily.

On the course web page, there are several assembly references. The first one to look at is this one, although the only sections that are relevant are those for GCC and GAS (the Gnu assembler).

Here's the basic idea:



Code generation is divided up into roughly three parts:


  1. The layout/declarative section.

    You'll have one of these at the top of your program, which will have the space for all the global variables. It will also have a global string table, containing all the string constants used in the program, so that you can refer to them. After this, you will have the declaration for each function defined in your program. Functions will consist of the following:



  2. The actual code generated.

    You will need to write a Java function for each kind of intermediate code construct (label, jump, binary op, etc.), which generates an equivalant assembly language statement. Note that these functions will of course have to use the space/type info, in order to calculate offsets into the activation record, etc.. For a label statement, for example, you would have to generate a label. For an if statement, you will have to generate a compare statement and then the appropriate branch statement. Notice that param statements allocate space on the stack before the function call. You will need to keep track of that to be able to get at the arguments to a function.

    You will also need to generate calls to C code, so that whenever the compiler runs into $printf("hello"):Int, it calls printf on "hello", using standard C-calling conventions. It should get the value returned as an int. C-calls don't need to handle arguments or return values that are not 32 bit values. Since pointless didn't provide a built-in print, or any other output, this is not optional!

  3. The details

    This phase also has a number of details to get right, which will likely be the cause of many bugs. Expect to spend significant time debugging your code, and allow for that time. A short list of things to watch for is:

    Note that this is not a complete list of "gotchas". Many others will exist.