<p>CS 322 LANGUAGES & COMPILER DESIGN II PSU HM HW 4 </p><p>HomeWork 4, IR Interpreter, 100 Points (5/12/2010)</p><p>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 </p><p>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.</p><p>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:</p><p> public Interp( PROG p ) {...} --- the constructor takes an IR program as input public void go() {...} --- the main trigger for interpreting the IR program</p><p>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[].</p><p> 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</p><p> Stack area: starts at mem[ maxTemp ], ends at mem[ maxStack-1 ] Function activation records are allocated and de-allocated on the stack</p><p> 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.</p><p>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:</p><p> 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.</p><p> 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.</p><p> 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</p><p>1 HW 4 CS 322 LANGUAGES & COMPILER DESIGN II PSU HM HW 4 </p><p> o after control returns, restore the saved frame pointer value</p><p> 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</p><p> LABEL — No action needed</p><p>Interpreting Expressions Every expression is evaluated to an integer value.</p><p> MEM — evaluate the address expression into an mem[] array index, and fetch the value from the corresponding cell; return the value</p><p> BINOP — evaluate the operands and apply the binary operation; then return the result</p><p> 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</p><p> 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</p><p> MEMBER — evaluate both obj and idx fields to get the member variable’s memory address; then return its value</p><p> 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</p><p> CONST — return the constant’s value</p><p> (NAME "wSZ") — return wordSize’s value</p><p>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.</p><p> 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:</p><p> void interpFunc( String fname ) throws Exception { ... // f = <the FUNC node corresponds to fname> sp = sp - f.varCnt - f.argCnt - 1; interpStmts( f.stmts ); sp = sp + f.varCnt + f.argCnt + 1; ... } //end interpFunc</p><p>2 HW 4 CS 322 LANGUAGES & COMPILER DESIGN II PSU HM HW 4 </p><p> 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:</p><p> void handleCall( String fname ) { ... mem[ sp ] = fp; fp = sp; interpFunc( fname ); sp = fp; fp = mem[ sp ]; ... } //end handleCall</p><p>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:</p><p> 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</p><p>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.</p><p>3 HW 4</p>
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages3 Page
-
File Size-