Homework 5: Register allocation and PowerPC/Linux backend Course: CS 380C: Advanced Compiler Techniques (Fall 2007) Instructor: Keshav Pingali Assigned: Thursday, November, 1, 2007 Due: Wednesday, November 14, 2007, 11:59:59 PM You can do this assignment in groups of two. Turn your assignment on time. Turnin will be disabled soon after the deadline. 1. Objective ------------ The goal of this assignment is to implement 1. Register allocation 2. A PowerPC Linux backend. 2. Register Allocation ---------------------- Perform register allocation as taught in class. You will have to consider the following while performing register allocation. 1. You will be using only integer registers r0-r31 2. The PowerPC ABI reserves some special registers. For example, r1 is used as the stack pointer. You should avoid such registers for register allocation. 3. Respecting caller-saved registers at function call boundaries. If you use a caller-saved register for some variable, you will have to store that register's contents in memory before making a function call, and load it back after returning from the call. For this part of the assignment, you do not have to store and load back such registers. But you will have to do it, for the second part of the assignment (when you generate real PowerPC Linux assembly). 4. Whether you are using the conditional register (CR) for storing the output for compare instructions. If so, call these registers cr0, cr1, ... For this part of the assignment you should generate output that resembles 3-address code, but uses PowerPC register names instead of variable names and virtual register names. The only operands that can appear in your code are constants, register names (integer and conditional), branch/function labels, and the special names FP and GP. As usual, your compiler should accept 3-address code from standard input and write register allocated code to standard output. Register allocation will be invoked with the flag -backend=reg. The output format is best illustrated by an example. Please understand this is a pseudo format and does not have a formal specification. The following sequence of code instr 4: move 0 i#-272 instr 5: cmplt i#-272 4 instr 6: blbc (5) [28] instr 7: move 0 j#-280 instr 8: cmplt j#-280 3 instr 9: blbc (8) [24] instr 10: mul i#-272 24 should be translated to look like ; r4 stores i ; Using cr0 to store the result of compares move 0, r4 cr0 := cmplt r4 4 blbc cr0 label_28 move 0, r5 cr0 := cmplt r5 3 blbc cr0 label_24 r6 := mul r4 24 ... ... label_24: instruction 24 goes here ... label_28: instruction 28 goes here. 3. PowerPC backend ------------------ For this part of the assignment your code should generate PowerPC Linux assembly. The PowerPC machine that we will use is a 32-bit machine Note that the csc frontend that was used in the previous assignments assumed that the size of the default integer datatype was 8 bytes. For this assignment we provide a different csc version that assumes that the integer datatype has a size of 4 bytes. Please ensure that you use the new version. PowerPC is a RISC architecture. All instructions in our 3-address code have equivalent PowerPC instructions. It should be straightforward to translate 3-address instructions to PowerPC instructions. The main challenge is in handling following the function call ABI (explained below), and setting up the prologue and epilogue of a function. Your compiler should accept 3-address code from standard input and write PowerPC linux assembly to standard output. The PowerPC backend will be invoked by passing the option -backend=ppc. 4. Handling read, write and wrl instructions in the 3-address code ------------------------------------------------------------------ A file named io.c is provided in the homework tarball. This contains the code for the functions ReadLong, WriteLong, and WriteLine. The functions implement the read, write and wrl instructions respectively. Replace occurrences of these instructions with a call to the corresponding function. Note that the call to these functions should follow the PowerPC ABI (explained below). You should link to this file to generate an executable and test your program. The commands to generate an executable are given below. $ ./csc filename.c > filename.3addr $ ./run.sh -backend=ppc < filename.3addr > filename.s $ gcc filename.s io.c -o filename.bin $ ./filename.bin 5. PowerPC Linux ABI -------------------- In this section, we try to provide a brief overview of the PowerPC Linux Application Binary Interface (ABI). This is not comprehensive, and provides just enough information to do this assignment. You should refer to the actual ABI if you need more information. Looking at the assembly output generated by gcc helps in understanding the ABI. The PowerPC architecture provides 32 general purpose registers, each 32 bits wide. Here are general purpose registers and their usage. -------------------------------------------------------------------------------- Register Name Usage -------------------------------------------------------------------------------- r0 Volatile register which may be modified during function linkage r1 Stack frame pointer, always valid r2 System-reserved register r3-r4 Volatile registers used for parameter passing and return values r5-r10 Volatile registers used for parameter passing r11-r12 Volatile registers which may be modified during function linkage r13 Small data area pointer register r14-r30 Registers used for local variables r31 Used for local variables or "environment pointers" CR0-CR7 Condition Register Fields, each 4 bits wide LR Link Register -------------------------------------------------------------------------------- The stack grows from higher addresses to lower addresses. +------------------+ +-> | Back chain | higher address | +------------------+ | | GPR save area | | +------------------+ | | CR save area | | +------------------+ | | Parameters area | | +------------------+ | | LR save word | | +------------------+ SP -> +-- | Back chain | lower address +------------------+ This ABI is different from what was specified in the first assignment, and the addresses of local variables and formal parameters will be different in this backend. You will have to allocate locations to variables on stack accordingly. Hint: By, shifting the frame pointer (FP) by some offset you will be able to translate from memory locations in the 3-address code, to what you need for this backend. You should follow the ABI for the main function, and calls to ReadLong, WriteLong, and WriteLine. For internal functions in the program, you are free to follow any convention. 5. Output --------- As usual, your compiler should accept 3-address code as input from stdin, and write output to stdout. Your compiler invoked by the script 'run.sh' should accept the following command line arguments. 1. -backend, the backend to be used to write output to. The options to support are -backend=reg and -backend=ppc. 6. Turning in your assignment ----------------------------- Download this tarball. http://www.cs.utexas.edu/users/pingali/CS380C/2007fa/assignments/assignment5/assignment5.tar.gz This is organized similar to pervious homeworks. Your assignment should contain the following: 1. A single tar.gz file named hw5.tar.gz, which, when extracted, creates directory hw5. 2. The hw5 directory can contain sub-directories. 3. The hw5 directory should contain the following files: a. README - Please include your name(s) and UTEID(s) here. b. compile.sh - a script to compile your source code. c. run.sh - a script that runs your compiler. This script should read 3-address code as input from stdin and write output to stdout. The output is specified by the command line arguments described in the previous section. The hw5 directory already exists with these files in the tarball you downloaded. Turn in your assignment by running the following commands on a UTCS Linux machine. $ # Go the parent directory of the hw4 directory. $ tar -zcvf hw5.tar.gz hw5 $ turnin --submit suriya cs380c-hw5 hw5.tar.gz $ turnin --list suriya cs380c-hw5 Please use turnin to submit your assignment. Only homeworks that are turned in using the procedure described above will be accepted. 7. Useful links --------------- 1. Read this links first. Very useful lecture notes: http://www.cs.utexas.edu/users/pingali/CS380C/2007fa/assignments/assignment5/ppc_asm_lecture.pdf Got this from Prof. Whaley's CIS 5930 course at FSU. http://ww2.cs.fsu.edu/~whaley/teach/5930HPO/LEC/ 2. PowerPC Linux ABI: http://www.cs.utexas.edu/users/pingali/CS380C/2007fa/assignments/assignment5/elfspec_ppc.pdf 3. PowerPC assembler language reference. http://www.nersc.gov/vendor_docs/ibm/asm/ 8. Hints -------- 0. Start early :) 1. Watch the clarifications page http://www.cs.utexas.edu/users/pingali/CS380C/2007fa/clarifications.html 2. Use the output of "gcc -O0 -S filename.c" to see how to generate assembly code, and understand the ABI. 3. Suriya will conduct a tutorial session on Wednesday, November 7th, instead of the regular office hours. The location will be announced later.