PSUCS322 HM 1 Languages and Compiler Design II Project 4 Hints Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
The University of Adelaide, School of Computer Science
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Intro to Procedures CS153: Compilers Greg Morrisett.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Generation I Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
Computer Architecture CSCE 350
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
The University of Adelaide, School of Computer Science
CSE 5317/4305 L7: Run-Time Storage Organization1 Run-Time Storage Organization Leonidas Fegaras.
Subprograms The basic abstraction mechanism. Functions correspond to the mathematical notion of computation input output procedures affect the environment,
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
PSUCS322 HM 1 Languages and Compiler Design II Runtime System Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring.
PSUCS322 HM 1 Languages and Compiler Design II Project 5 Hints Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
PSUCS322 HM 1 Languages and Compiler Design II Re-Introduction from CS 321 Material provided by Prof. Jingke Li Stolen with pride and modified by Herb.
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
PSUCS322 HM 1 Languages and Compiler Design II Basic Blocks Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring.
PSUCS322 HM 1 Languages and Compiler Design II Program Interpretation Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Generation 2 Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
Cse322, Programming Languages and Compilers 1 6/21/2015 Lecture #5, April 17, 2007 Array Access Case stmt Jump tables Procedure call Machine dependent.
Run-Time Storage Organization
PSUCS322 HM 1 Languages and Compiler Design II Parameter Passing Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
PSUCS322 HM 1 Languages and Compiler Design II IR Canonicalization Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
PSUCS322 HM 1 Languages and Compiler Design II Project 2 Hints Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring.
Run-time Environment and Program Organization
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Runtime Environments Compiler Construction Chapter 7.
Compiler Run-time Organization Lecture 7. 2 What we have covered so far… We have covered the front-end phases –Lexical analysis –Parsing –Semantic analysis.
Compiler Construction
CPSC 388 – Compiler Design and Construction Runtime Environments.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Lesson 13 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Activation Records CS 671 February 7, CS 671 – Spring The Compiler So Far Lexical analysis Detects inputs with illegal tokens Syntactic analysis.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
CPSC 388 – Compiler Design and Construction Code Generation.
1 9/6/05CS360 Windows Programming CS360 Windows Programming.
COMPILERS Intermediate Code hussein suleman uct csc3003s 2007.
CSC 8505 Compiler Construction Runtime Environments.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Intermediate Representation II Storage Allocation and Management EECS 483 – Lecture 18 University of Michigan Wednesday, November 8, 2006.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
Stack Machine Stack Architecture –“ Zero-address” –Processor/stack + Memory –Memory: Data and Program –“Reverse Polish notation” programming style –Stack.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
Object Oriented Programming Lecture 2: BallWorld.
Run-Time Environments Chapter 7
Procedures (Functions)
Programming Languages (CS 550) Mini Language Compiler
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
PZ09A - Activation records
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Languages and Compilers (SProg og Oversættere)
Runtime Environments What is in the memory?.
Computer Architecture
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Programming Languages (CS 360) Mini Language Compiler
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Presentation transcript:

PSUCS322 HM 1 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

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

PSUCS322 HM 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

PSUCS322 HM 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

PSUCS322 HM 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

PSUCS322 HM 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

PSUCS322 HM 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

PSUCS322 HM 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

PSUCS322 HM 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

PSUCS322 HM 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