

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Project #5 for cs322 winter’09: languages and compiler design ii, where students are required to implement a sparc assembly code generator for the ir tree language. The operand representation, register management, and implementation hints for various instructions. Students are expected to write a java class, codegen, with the given interface and methods.
Typology: Study Guides, Projects, Research
1 / 2
This page cannot be seen from the preview
Don't miss anything!


CS322 Winter’09: Languages and Compiler Design II 2/24/ Prof. Jingke Li (FAB 120-06, [email protected]), Classes: TTh 2-3:50pm, URBAN 304, Office Hours: TTh 10-11am.
This (last) project is to implement the final component of the MINI compiler, a SPARC assembly code generator for the IR Tree language. This codegen module should be implemented as a self-contained Java class (like the interpreter in Project 4), Codegen, with the following interface:
public Codegen(PROG p) {...} --- the constructor takes an IR program as input public void go() {...} --- the main trigger for the codegen module
In the codegen, all operands, including constants, addresses, and intermediate results, are stored in objects of the class Operand. For each kind of operands, there is a subclass for representing it:
abstract class Operand {}
public class Reg extends Operand // register { static final int RESERVED=-1, FREE=0, USE=1, TEMP=2; String name; int status; } public class RegOffset extends Operand { Reg r; int o; } // Stack location public class Imm13 extends Operand { int i; } // 13-bit immed value public class Imm32 extends Operand { int i; } // 32-bit immed value public class AddrName extends Operand { String s; } // global address name
The codegen uses a simple register allocation scheme. The register allocator keeps track of each register’s status: reserved, free, used, or temp. Upon an allocation request, the allocator simply allocates the next available register. If there is no more registers available, it just throws an exception. When a temp is assigned to a register, it will stay there until the end of the program. A special status, temp, is used to prevent the register from being freed. The register allocator routines are:
// requesting an arbitrary free register; // throws an exception if none is free // (Note that the status of the reg will be set to ’use’; // if it is to be used for a temp, the status needs to be // changed to ’temp’ after the call is returned.) private Reg getReg() throws CodegenException { ... }
// requesting a specific register r; // throws an exception if r is not free private void getReg(Reg r) throws CodegenException { ... }
// freeing a register, skipping if it holds a temp void freeReg(Reg r) { ... }
// freeing all used registers, including those holding temps void freeAllRegs() { ... }
The codegen program should be called Codegen.java and placed in a subdirectory codegen. As usual, a tar file is provided to you, which contains the following:
The quality of the assembly code is measured by two counts: number of instructions and number of registers; both are automatically reported at the end of the assembly code. After making your codegen work correctly, you should try to improve the quality of its output, using the provided .s files as a reference.
Submit your program Codegen.java to [email protected]. Please keep a copy of your submitted files aside, in case there is a need for re-submission.