Handout 1, CS 133F Introduction to Fortran Programming s1

Total Page:16

File Type:pdf, Size:1020Kb

Handout 1, CS 133F Introduction to Fortran Programming s1

CS 322 LANGUAGES & COMPILER DESIGN II PSU HM HW 4

HomeWork 4, IR Interpreter, 100 Points (5/12/2010)

Due Date: Thursday May 20th, 2010 Subject: IR Interpreter, Interpret IR Code emitted by Gen 2 for MINI compiler Origin: Prof. Li web: http://web.cecs.pdx.edu/~li/cs322/index.html

General Rule: Implement your projects for the MINI compiler in Java. Use the *.tar files provided on the CS 322 website. Turn in your homework before the start of class on the due date.

Summary: In this project you implement an interpreter for the IR tree language as a self- contained Java class Interp, (i.e. not as a visitor), with the following interface:

public Interp( PROG p ) {...} --- the constructor takes an IR program as input public void go() {...} --- the main trigger for interpreting the IR program

Storage Model The interpreter organizes data in three storage categories: temps, stack, and heap. To allow a uniform access pattern, they are all mapped to a single integer array called mem[].

 Temps area: starts at mem[ 0 ], ends at mem[ maxTemp-1 ] TEMP nodes are mapped to temp memory using their indices with a fixed offset

 Stack area: starts at mem[ maxTemp ], ends at mem[ maxStack-1 ] Function activation records are allocated and de-allocated on the stack

 Heap area: starts at mem[ maxTemp + maxStack ], ends at mem[ maxMem-1 ] Calls to malloc() result in space allocated in the heap, with the pointer to the start address being returned.

Interpreting Statements The core of the interpreter is a while loop executing the statements of a FUNC node, until the halt state is reached. During each iteration, one of the below cases is excecuted:

 MOVE — The left-hand-side expression of a MOVE node must represent an address or a temp. An address in the IR tree code is represented by one of the following nodes: MEM, MEMBER, PARAM, and VAR. They should all be evaluated to indices of the mem[] array. Since we model TEMPs by a section of the mem array, TEMP nodes are also evaluated to indices to the mem[] array. The interpreter evaluates the lhs to a mem[] array index, the rhs to a value, and then assigns the value to the cell. Afterwards, the control moves to interpret the next statement in the list, i.e. the next loop iteration progresses.

 JUMP/CJUMP — The interpreter searches for the label node that corresponds to the jump target in the statement list. Once found, the labeled statement becomes the next to be interpreted.

 CALLST — For prInt and prString routines, call System.out.println to print out the argument’s value. Otherwise, take the following steps: o push parameters onto the stack (at mem[sp+1], mem[sp+2], etc.) o adjust the frame pointer: save the current fp in mem[sp], and set sp to be the new fp o find the routine’s code, and switch there to execute o save information to return at a later time

1 HW 4 CS 322 LANGUAGES & COMPILER DESIGN II PSU HM HW 4

o after control returns, restore the saved frame pointer value

 RETURN — If there is an associated expression, evaluate the expression and assign the value to the special retVal variable. Return the control from the current FUNC interpretation environment

 LABEL — No action needed

Interpreting Expressions Every expression is evaluated to an integer value.

 MEM — evaluate the address expression into an mem[] array index, and fetch the value from the corresponding cell; return the value

 BINOP — evaluate the operands and apply the binary operation; then return the result

 TEMP — translate the temp’s index to a mem[] array index and return the value from the cell. Since temps’ index numbers start at 100, we need to make an adjustment when mapping it the mem[] array index: ((TEMP) e).num – 100

 CALL — if the callee is malloc(), allocate space in the mem[] array’s heap area, otherwise take similar steps as in the CALLST case, plus returning retVal as the result. It is not required that you conduct garbage collection of the heap space

 MEMBER — evaluate both obj and idx fields to get the member variable’s memory address; then return its value

 PARAM/VAR — use the index to compute the offset from the frame pointer fp, then return the value stored in the corresponding mem cell. For example, for (VAR 3), the value mem[fp-3] should be returned; for (PARAM 3), the value mem[fp+4] should be returned. Note the index adjustment

 CONST — return the constant’s value

 (NAME "wSZ") — return wordSize’s value

More on the Handling of CALL/CALLST Nodes For each method invocation, a frame is allocated on the stack, and the frame pointer is switched to point to the new frame; when a method returns, the reverse actions are performed.

 Allocating and de-allocating a frame: This corresponds to adjusting the stack pointer’s value and it happens when a FUNC node is being interpreted:

void interpFunc( String fname ) throws Exception { ... // f = sp = sp - f.varCnt - f.argCnt - 1; interpStmts( f.stmts ); sp = sp + f.varCnt + f.argCnt + 1; ... } //end interpFunc

2 HW 4 CS 322 LANGUAGES & COMPILER DESIGN II PSU HM HW 4

 Adjusting the frame pointer: When control is switched from a caller to a callee, the frame pointer fp needs to be adjusted accordingly. This happens at the CALLST/CALL node:

void handleCall( String fname ) { ... mem[ sp ] = fp; fp = sp; interpFunc( fname ); sp = fp; fp = mem[ sp ]; ... } //end handleCall

Code Organization The interpreter program should be called Interp.java and placed in a subdirectory interp. As usual, a tar file is provided for you, which includes the following:

 ir/ — the IR tree node definitions  irpsr/ — an IR tree parser program  interp/— a starting interpreter version Interp0.java and a driver program TestInterp.java; but rename your own solution Interp.java  tst/ — a collection of test programs  runint — a script for running the interpreter

What to turn in and how: Submit program Interp.java file via email. Please keep a copy of the original file in case there is a need to re-submit later.

3 HW 4

Recommended publications