

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 Spring 2008
The fourth part of the programming project is to generate code for the Decaf language in MIPS assembly language. Strategy: The code generation functions will also walk the Abstract Syntax Tree and add MIPS instructions to an output stream. As discussed in class on April 10 and 14, there can be a set of code generation functions that operate on the different types of nodes in the Abstract Syntax Tree. The result of code generation should be an output file with the file extension “.s” that can be given to a MIPS simulator or run on a MIPS machine. Expressions: We discussed using the tree labeling technique to optimize the use of registers. We will assume that expressions do not have a depth of more than 8, so we will be able to use registers $t0 through $t7 without “spilling” register contents to memory during expression evaluation. The labeling of the expression nodes in the AST can be done recursively by a separate function, or you can go back and add this functionality to the semantic checker. Each semantic node that is an expression internal node, or a leaf token node, should be given an additional field to hold the expression depth label. We also assume that method calls are not embedded in other expressions. The generate_expression method should take an additional parameter that is the base register where it should begin using registers during evaluation. It should also return an int result that is the register number of the $t register that contains the value that is the result of the expression evaluation. This general generate_expression can then make cases based on whether the semantic type of the node it is generating code for is for a binary expression, a unary expression, or a token node, which can either be an identifier token or some type of constant token. Statements: There can be a general generate_statement method that checks for each type of statement and generates code accordingly. Statements can use the return register number from the generate_expression method to obtain expression results. Otherwise, we assume that values are written to the stack and not left in registers. This simplifies the run-time stack and method call protocols since registers do not have to be saved. Runtime Stack: The MIPS assembly language will allocate space for the run-time stack and has two registers devoted to its use: $sp, the stack pointer, and $fp, the frame pointer. We discussed the design of the following stack frame layout. In MIPS, stack numbers increase in the upwards direction in the diagram:
$fp -> Space for return value, if any Parameter
... Parameter n Frame pointer to previous frame Return address for caller method Local variable ... $sp -> Local variable k Since the Decaf language has only one lexical level for procedure declarations, there is no need to have static links for different lexical levels. Furthermore, we can assume that local blocks can just allocate more local variables onto the stack. Variable allocation: When a variable is declared at the global level in the program, (lexical level 0), it will be allocated in the global data area of MIPS (.data). At the beginning of the processing, an offset generator is initialized and is used to allocate a global offset number that is put into the symbol table for these variables. [But note that we may need to leave out any examples with global variables.] When a variable is declared at a non-global level in the program (lexical level 1 or greater), it will be allocated a stack offset. (So each variable in the program is given an offset number, and the symbol table indicates whether it is in the global data area or in the stack.) Whenever a method declarations start, the offset generator will be reset. The formal parameters are given offset numbers at the top of the frame. Then the offset set number generator is jumped to start allocating local variables in blocks after the saved return address location in the stack frame. The offset numbers are given as negative offsets with respect to the frame pointer $fp. Method declarations and method calls: Generate code at the beginning and end of the body of the method for the callee begin protocol and the callee return protocol, including a return jump. For method calls, generate code for the caller begin protocol (pushes actual parameters onto the stack), the jump to a procedure, and then the caller return protocol, which gets the return value from the stack. Callout method calls: We will support two callout methods “read_int” and “print_int”. These are implemented using the syscall operator of MIPS and the readint and printint built-in functions. Program: At the beginning of the program, the generator should generate code for the MIPS preamble, generating the .data area, up to where the code begins. We will assume that the method “main” is the first one declared and thus the label “main:” that is generated for that method will be taken by MIPS as the starting location to execute the code. A final label “end: “ can also be generated at the end of the code.