Docsity

Code Interpretation in Language Compiler Design - Project | CS 322, Study Guides, Projects, Research of Computer Science

Toggle sidebar
bg1
CS322 Winter’09: Languages and Compiler Design I I 2/12/09
Prof. Jingke Li (FAB 120-06, [email protected]), Classes: TTh 2-3:50pm, URBAN 304, Office Hours: TTh 10-11am.
Project #4: IR Code Interpretation
(Due Tuesday 2/24 @ 5pm)
This project is to implement an interpreter for the IR tree language. The interpreter is to be implemented
as a self-contained Java class (i.e. not as a visitor), Interp, 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 an uniform
access pattern, they are all mapped to a single integer array called mem.
•Temps — mem[0] -- mem[maxTemp-1]
TEMP nodes are mapped to temp memory using their indices (with a fixed offset).
•Stack — mem[maxTemp] -- mem[maxStack-1]
Function activation records are allocated and de-allocated on the stack.
•Heap — mem[maxStack] -- mem[maxMem-1]
Calls to ’malloc’ result in space allocated in the heap.
Interpreting Statements
The core of the interpreter is a while loop executing the statements of a FUNC node.
•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 the following nodes: MEM,MEMBER,PARAM, and VAR.
They should all be evaluated to indices to the mem array (more on this in the expression section below).
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 an mem array index, the rhs to a value, and then assigns the value
to the mem cell. Afterwards, the control moves to interpret the next statement in the list.
•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:
–push parameters onto the stack (at mem[sp+1],mem[sp+2], etc.)
–adjust the frame pointer (save the current fp in mem[sp], and set sp to be the new fp)
–find the routine’s code, and switch there to execute
–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.
1
pf3

Partial preview of the text

Download Code Interpretation in Language Compiler Design - Project | CS 322 and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

CS322 Winter’09: Languages and Compiler Design II 2/12/ Prof. Jingke Li (FAB 120-06, [email protected]), Classes: TTh 2-3:50pm, URBAN 304, Office Hours: TTh 10-11am.

Project #4: IR Code Interpretation

(Due Tuesday 2/24 @ 5pm)

This project is to implement an interpreter for the IR tree language. The interpreter is to be implemented as a self-contained Java class (i.e. not as a visitor), Interp, 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 an uniform access pattern, they are all mapped to a single integer array called mem.

  • Temps — mem[0] -- mem[maxTemp-1] TEMP nodes are mapped to temp memory using their indices (with a fixed offset).
  • Stack — mem[maxTemp] -- mem[maxStack-1] Function activation records are allocated and de-allocated on the stack.
  • Heap — mem[maxStack] -- mem[maxMem-1] Calls to ’malloc’ result in space allocated in the heap.

Interpreting Statements

The core of the interpreter is a while loop executing the statements of a FUNC node.

  • 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 the following nodes: MEM, MEMBER, PARAM, and VAR. They should all be evaluated to indices to the mem array (more on this in the expression section below). 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 an mem array index, the rhs to a value, and then assigns the value to the mem cell. Afterwards, the control moves to interpret the next statement in the list.
  • 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: - push parameters onto the stack (at mem[sp+1], mem[sp+2], etc.) - adjust the frame pointer (save the current fp in mem[sp], and set sp to be the new fp) - find the routine’s code, and switch there to execute - 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 corre- sponding cell; return the value.
  • BINOP — evaluate the operands and apply the binary operation; then return the result.
  • TEMP — translate the temp’s index to an 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.
  • 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 in this case.)
  • 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 deallocating 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; ... }

  • 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]; ... }