1 / 10

Languages and Compiler Design II Project 4 Hints

Languages and Compiler Design II Project 4 Hints. Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring 2010 rev.: 5/19/2010. Agenda. Storage Model Initial Setup Top-Level Routines Fetch-Execute Loop Interpreting Statements Evaluating Expressions

feleti
Download Presentation

Languages and Compiler Design II Project 4 Hints

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Languages and Compiler Design IIProject 4 Hints Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring 2010 rev.: 5/19/2010 CS322

  2. Agenda • Storage Model • Initial Setup • Top-Level Routines • Fetch-Execute Loop • Interpreting Statements • Evaluating Expressions • Handling Call Nodes • TestInterp.java CS322

  3. 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 CS322

  4. Initial Setup public class Interp{ private static int maxMem = 4096, maxStack = 2048, maxTemp = 512, tempOffset = 100, wordSize = 1; private int[] mem; // memory array private int hp, sp, fp; // heap, stack, frame pointers private int retVal; // storage for return value private FUNClist funcs; // input program’s funcs } //end class Interp public Interp( PROG p ){ mem = new int[maxMem]; hp = maxMem - 1; fp = sp = maxStack - 1; retVal = 0; funcs = p.funcs; } //end Interp CS322

  5. Top-Level Routines public void go() throws Exception{ interpFunc("main"); } //end go void interpFunc( String label ) throws Exception{ for( int i = 0; i < funcs.size(); i++ ) { FUNC f = (FUNC) funcs.elementAt(i); if( f.label.equals(label) ) { sp = sp - f.varCnt - f.argCnt - 1; interpStmts(f.stmts); sp = sp + f.varCnt + f.argCnt + 1; return; } //end if }//end for throw new InterpException("Can’t find function: " + label); } //end InterpFunc CS322

  6. Fetch-Execute Loop public void interpStmts(STMTlist sl) throws Exception{ int i = 0; while( i < sl.size() ) { STMT s = (STMT) sl.elementAt( i ); if( s instanceof MOVE ) { interpMove((MOVE) s ); i++; } else if( s instanceof LABEL ) { i++; } else if( s instanceof CALLST ) { interpCallSt((CALLST) s ); i++; } else if( s instanceof RETURN ) { ...; return; } else if( s instanceof JUMP ) { i = genTarget((JUMP) s).target,sl); } else if( s instanceof CJUMP ) { if( evalCond((CJUMP) s ) ) i = getTarget(...); else i++; //end if }else{ throw new InterpException( "Illegal STMT: " + s ); } //end if } //end while } //end interpStmts CS322

  7. Interpreting Statements private void interpMove( MOVE s ) throws Exception{ int idx; if( s.dst instanceof TEMP ) idx = ((TEMP) s.dst).num - tempOffset; else if( s.dst instanceof MEM ) idx = evaluate(((MEM) s.dst).exp); else if( s.dst instanceof MEMBER ) ... else if( s.dst instanceof PARAM ) ... else if( s.dst instanceof VAR ) ... else throw new InterpException("Wrong Address form"); mem[idx] = evaluate(s.src); } //end interpMove // search a STMTlist for a matching LABEL node private int genTarget( NAME n, STMTlist sl ) throws Exception{ for( int i = 0; i < sl.size(); i++ ) { if( ( sl.elementAt(i) instanceof LABEL ) && ...) return i+1; } //end for throw new InterpException( ... ); } //end genTarget // evaluate the CJUMP’s condition to a Boolean value private boolean evalCond( CJUMP s ) throws Exception{ int v1 = evaluate( s.left ), v2 = evaluate( s.right ); switch( s.op ) { ... } } //end evalCond CS322

  8. Evaluating Expressions // Every expression is evaluated to an integer value public int evaluate( EXP e ) throws Exception{ if( e instanceof BINOP ) return evalBinop((BINOP) e); if( e instanceof CALL ) return evalCall((CALL) e); if( e instanceof MEM ) return mem[evaluate(((MEM) e).exp)]; if( e instanceof TEMP ) return mem[...]; if( e instanceof MEMBER ) return mem[...]; if( e instanceof PARAM ) return mem[fp + ...]; if( e instanceof VAR ) return mem[fp - ...]; if( e instanceof CONST ) return ((CONST) e).val; if( (e instanceof NAME ) && ((NAME) e).id.equals("wSZ")) return wordSize; throw new InterpException("Illegal EXP node for evaluate: " + e); } //end evaluate int evalBinop( BINOP e ) throws Exception{ int lval = evaluate(e.left), rval = evaluate(e.right); switch( e.op ) { ... } } //end evalBinop CS322

  9. Handling Call Nodes private void interpCallSt( CALLST s ) throws Exception{ String fname = s.func.id; if( fname.equals("prInt") ) { System.out.println(evaluate(...)); } else if( fname.equals("prString")) { ... } else handleCall( fname, s.args ); } //end interpCallSt // evaluate args, place results in callee’s AR (through sp pointer) private void handleCall( String fname, EXPlist args) throws Exception{ ... mem[sp + ..] = ... mem[sp] = fp; fp = sp; interpFunc( fname ); sp = fp; fp = mem[sp]; ... } //end handleCall int evalCall( CALL e ) throws Exception{ ... return retVal; } //end evalCall CS322

  10. TestInterp.java public class TestInterp{ public static void main( String [] args ) { try { PROG p = new irParser(System.in).Program(); Interp intp = new Interp(p); intp.go(); } //end try catch (InterpException e) { System.err.println(e.toString()); } //end catch catch (Exception e) { System.err.println(e.toString()); } //end catch } //end main } //end TestInterp CS322

More Related