Project 7
Due on December 11th 11:59 PM (Accepted with no penalty).
Accepted late by December 12th 11:59 PM for 1 penalty
Not accepted after December 12th 11:59 PM
Each penalty incurs a 15% stacking reduction on the project grade or uses 1 "penalty token". Penalty tokens are applied by course personnel to your maximum benefit.
Updates

None yet!

Resources
  • Starter code is now available upon which you can build your project 7. Please feel free to change any or all files in the starter code or not to use it at all.
  • The oracle is now ready. Unlike previous oracles, the project 7 oracle prints the correct output of the program being compiled rather than the output of the compiler.
Overview

For this assignment you will write a code generator that generates X64 assembly code (suitable for use on the cycle servers) for A programs.

This project is intended to be an extension of the previous project code, but will be invoked via a new argument. Your code should be built and invoked via the command sequence

make all ./ac <file.a> -o <outfile.s>

Where

  • <file.a> is an input file
  • <outfile.s> is a file containing assembly code for the input.

The only required behavior of your project is that the output file should be able to be linked, assembled, and run on the cycle servers, and that it obeys the semantics of the A language.

Specifications
General Information

The code generator will be implemented by writing codegenX64 member functions for the various kinds of 3AC quads. See the lecture notes for lots of useful details.

Getting Started

As in prior projects, you are encouraged to build the new code atop your prior implementation, but you can use the starter code here (link) to get you started. Feel free to change as much of this code as you want. Please note that this code is designed to be a starting point for working on project 7, not a complete solution to project 6.

Some additional code-generation methods can be found in the file 3ac.hpp file, as well as stubs in the x64_codegen.cpp file.

Auxiliary object files

In addition to the code for your compiler, one additional object file is built by the starter code makefile, called std_alang.o. This object file contains implementations of helper functions (written in C) that are useful in programming the intrinsics of the language (consoleout and consolein).

These object files can be generated from code in the starter files by compiling them with gcc (gcc std_alang.c -c). You are not required to use these auxiliary files, but you are welcome to do so.

Building an executable

The scope of this project is to generate a text file containing valid x64 assembly. In order to translate that file into an executable, there are some additional steps that you can use with existing x64 compiler toolchain tools.

Because std_alang.o expects to be able to call C library functions, if you use it you should make sure that you include the object files to initialize the C runtime. On the cycle servers, you can do this by invoking the assembler and linker as follows:

	as prog.s -o prog.o
        ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 \
	  /usr/lib/x86_64-linux-gnu/crt1.o \
	  /usr/lib/x86_64-linux-gnu/crti.o \
	  -lc \
	  prog.o \
	  std_alang.o \
	  /usr/lib/x86_64-linux-gnu/crtn.o \
        -o prog.exe
	

You should make sure that your program exits gracefully. If you are using the above linker command, simply treat main like any other function with an integer return type. Code included in crt1.o (which contains the _start label), will actually invoke the exit syscall for you.

The other expectation of the C runtime objects is that there will be a public label called simply main that will be a jump target from crt1.o. In your output code make sure to include

	.globl main
	
in the .data section. Do not include a label _start, since there already is one in crt1.o.

Non-obvious semantic issues
  • All parameters should be passed by value.
  • Unlike C, boolean logic is NOT short circuited. That is to say, an expression like a = false && foo() will actually call the function foo
Suggestions for how to work on this assignment
  1. Implement code generation for each of the following features; be sure to test each feature as it is implemented!

    • global variable declarations, function entry, and function exit (write a test program that just declares some global variables and a main function that does nothing)
    • int and bool literals (just load into registers), string literals, and the Syscall in it's WRITE configuration.
    • local SymOpd (code that loads/stores local, and assignments of the form id=literal and id=id (test by assigning then writing)
    • expressions other than calls
    • statements other than calls and returns
    • call statements and expressions, return statements
    • any other quads, such as the quad for consolein.
  2. Be super-ultra sure that consoleout works! Nearly every test program uses it to test the semantics of your program.

As in previous projects, the oracle will serve as a reference implementation. If you are confused about how a certain operation should work, feel free to consult the oracle. If you feel that the oracle is acting in error, report the problem to Piazza ASAP.