

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


CIS 631, CSE 691, CIS400 Spring 2008
For the fourth part of the programming project, the code generation, I have made a programming framework for code generation in Java. This can be used directly by students writing their project in Java, with modifications to fit their own Abstract Syntax Tree and Symbol Table designs. It can be used for ideas by students writing their projects in other languages. The code generation operates by traversing the Abstract Syntax Tree and generating MIPS instructions at each node, in an order that produces a complete program. I am providing two (partially written) classes. The CodeGenerator class has a set of recursive methods for traversing the tree and generating code. The Code class appends instructions to the output stream with a set of methods that help you format the output. Primarily, for this part of the project, you will complete the methods in the CodeGenerator class, some of which are either missing or only partially written. In addition, I decided to add the allocation of simple variables to existing code for the Parser and the Symbol Table. This is definitely not the only possible design – you could do the variable allocation directly in the CodeGenerator as you traverse the AST. In my design, the code generation class keeps the stack offset counter and interacts with the Parser and with the Symbol Table in allocating simple variables. Each variable is allocated to the run-time stack as either a global variable or a local variable, local to a procedure as a formal parameter name or local to a block in a variable declaration. The code framework that I have provided does not discuss global variables, but they could be added. In allocating only local variables, I didn’t have to distinguish between local and global offsets in the symbol table. I made two additions to the Parser that help manage the offset counter. When I parse a method declaration, I reset the offset counter to start counting again for the activation record of the newly entered method. // reset offset location counter at procedure entry // offset of location 0 is used for return values, // so offset -4 is first location for formal parameters CodeGenerator.setOffset( -4); Note that I have always left a space on the stack for a return value from a procedure, even if the procedure doesn’t use it, as this simplifies code generation. When I parse a block, I have a flag that keeps track if this is the first block of a method or if it is a local block, and in the former case, I increment the offset counter to skip the standard save parts ($fp and $ra) of the activation record: if (isMethodBlock) { //if it is the first block of a method, must jump the stack allocation offset by 8 CodeGenerator.incrementOffset(-8);
The reason that these offsets are being managed by the parser is so that I can allocate simple variables as the parser inserts them into the symbol table. As part of my insertVariable method, I have a line that sets the location field in the new symbol table entry (type Stentry): public static void insertVariable(Token name, Token type) { String actualName = name.text;
... Stentry actualEntry = new Stentry(actualName, 1, new STtype(type.text)); actualEntry.setLocation(CodeGenerator.useOffset()); ... } In the CodeGenerator, when I write GenerateExpression, whenever the expression is an identifier, then I look up the name in the symbol table and get the value of the location. If this value is n, then the memory location of the variable on the stack is at offset n with respect to the current frame pointer. Note that the offsets are negative already – examples of n are -4, -8, -12,... n($fp) Please take a look at the CodeGenerator and Code classes and don’t hesitate to email me if there are further questions.