CS 153: Concepts of Compiler Design October 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Slides:



Advertisements
Similar presentations
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:
Advertisements

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)
Runtime Environments Source language issues Storage organization
Semantics of Calls and Returns
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
CS 153: Concepts of Compiler Design August 25 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
CS 152: Programming Language Paradigms April 9 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Compiler Construction
CS 153: Concepts of Compiler Design September 9 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms April 2 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design August 26 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 16 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Run-Time Environments How do we allocate the space for the generated target code and the data object.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
CS 153: Concepts of Compiler Design September 21 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 10 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design November 23 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design December 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 30 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
7. Runtime Environments Zhang Zhizheng
CS 153: Concepts of Compiler Design November 18 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
CS 152: Programming Language Paradigms April 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 12 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CS 153: Concepts of Compiler Design September 23 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms May 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 28 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Run-Time Environments Presented By: Seema Gupta 09MCA102.
CS 432: Compiler Construction Lecture 9
CS 153: Concepts of Compiler Design September 14 Class Meeting
Run-Time Environments Chapter 7
CS 153: Concepts of Compiler Design October 17 Class Meeting
CS 153: Concepts of Compiler Design October 5 Class Meeting
CS 153: Concepts of Compiler Design October 3 Class Meeting
CS 153: Concepts of Compiler Design November 28 Class Meeting
CMPE 152: Compiler Design February 6 Class Meeting
CMPE 152: Compiler Design September 25 Class Meeting
CMPE 152: Compiler Design September 4 Class Meeting
CMPE 152: Compiler Design August 30 Class Meeting
CMPE 152: Compiler Design September 18 Class Meeting
CMPE 152: Compiler Design October 2 Class Meeting
CMPE 152: Compiler Design October 4 Class Meeting
CMPE 152: Compiler Design October 4 Class Meeting
CMPE 152: Compiler Design August 21/23 Lab
CMPE 152: Compiler Design September 20 Class Meeting
CS 153: Concepts of Compiler Design November 6 Class Meeting
UNIT V Run Time Environments.
CS 432: Compiler Construction Lecture 11
CMPE 152: Compiler Design February 28 Class Meeting
CMPE 152: Compiler Design March 7 Class Meeting
CMPE 152: Compiler Design February 7 Class Meeting
CMPE 152: Compiler Design February 21 Class Meeting
CMPE 152: Compiler Design February 26 Class Meeting
CMPE 152: Compiler Design March 7 Class Meeting
CMPE 152: Compiler Design April 16 Class Meeting
CMPE 152: Compiler Design May 2 Class Meeting
CMPE 152: Compiler Design August 27 Class Meeting
CMPE 152: Compiler Design September 17 Class Meeting
CMPE 152: Compiler Design February 7 Class Meeting
CMPE 152: Compiler Design September 26 Class Meeting
CMPE 152: Compiler Design September 19 Class Meeting
Presentation transcript:

CS 153: Concepts of Compiler Design October 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak 1

Computer Science Dept. Fall 2015: October 7 CS 153: Concepts of Compiler Design © R. Mak 2 Runtime Activation Records  An activation record (AKA stack frame) maintains information about the currently executing routine a procedure a function the main program itself

Computer Science Dept. Fall 2015: October 7 CS 153: Concepts of Compiler Design © R. Mak 3 Runtime Activation Records  In particular, an activation record contains the routine’s local memory. values of local variables values of formal parameters  This local memory is a memory map. Key: The name of the local variable or formal parameter. Value: The current value of the variable or parameter. In other words, local memory is a hash table!

Computer Science Dept. Fall 2015: October 7 CS 153: Concepts of Compiler Design © R. Mak 4 PROGRAM main1; VAR i, j : integer; PROCEDURE proc2a; VAR m : integer; PROCEDURE proc3; VAR j : integer BEGIN j := i + m; END; BEGIN {proc2a} i := 11; m := j; proc3; END; PROCEDURE proc2b; VAR j, m : integer; BEGIN j := 14; m := 5; proc2a; END; BEGIN {main1} i := 33; j := 55; proc2b; END. Runtime Access to Nonlocal Variables RUNTIME STACK AR: main1 i j AR: proc2a m AR: proc3 j AR: proc2b j m  Each parse tree node for a variable contains the variable’s symbol table entry as its VALUE attribute. Each symbol table entry has the variable’s nesting level n.  To access the value of a variable at nesting level n, the value must come from the topmost activation record at level n. Search the runtime stack from top to bottom for the topmost activation record at level n. main1  proc2b  proc2a  proc3 145

Computer Science Dept. Fall 2015: October 7 CS 153: Concepts of Compiler Design © R. Mak 5 The Runtime Display  A vector called the runtime display makes it easier to access nonlocal values. This has nothing to do with video displays!

Computer Science Dept. Fall 2015: October 7 CS 153: Concepts of Compiler Design © R. Mak 6 The Runtime Display, cont’d  Element n of the display always points to the topmost activation record at scope nesting level n on the runtime stack.  The display must be updated as activation records are pushed onto and popped off the runtime stack.

Computer Science Dept. Fall 2015: October 7 CS 153: Concepts of Compiler Design © R. Mak 7 The Runtime Display, cont’d  Whenever a new activation record at level n is pushed onto the stack, it links to the previous topmost activation record at level n.  This link helps to restore the runtime stack as activation records are popped off when returning from procedures and functions.

Computer Science Dept. Fall 2015: October 7 CS 153: Concepts of Compiler Design © R. Mak 8 Runtime Access to Nonlocal Variables AR: main1 i j AR: proc2a m AR: proc3 j PROGRAM main1; VAR i, j : integer; PROCEDURE proc2a; VAR m : integer; PROCEDURE proc3; VAR j : integer BEGIN j := i + m; END; BEGIN {proc2a} i := 11; m := j; proc3; END; PROCEDURE proc2b; VAR j, m : integer; BEGIN j := 14; m := 5; proc2a; END; BEGIN {main1} i := 33; j := 55; proc2b; END. RUNTIME DISPLAY RUNTIME STACK AR: proc2b j m The runtime display allows faster access to nonlocal values. main1  proc2b  proc2a  proc3

Computer Science Dept. Fall 2015: October 7 CS 153: Concepts of Compiler Design © R. Mak 9 Recursive Calls PROGRAM main1 FUNCTION func2 FUNCTION func3 PROCEDURE proc2 PROCEDURE proc3 RUNTIME STACK RUNTIME DISPLAY AR: main1 AR: proc2 AR: proc3 AR: func3 AR: func main1  proc2  proc3  func2  func3  proc2 AR: proc2

Computer Science Dept. Fall 2015: October 7 CS 153: Concepts of Compiler Design © R. Mak 10 Reminders  A VARIABLE node in the parse tree contains a pointer to the variable name’s symbol table entry. Set in the front end by method VariableParser.parse() The method that takes two parameters.  A symbol table entry points to its parent symbol table. The symbol table that contains the entry.

Computer Science Dept. Fall 2015: October 7 CS 153: Concepts of Compiler Design © R. Mak 11 Reminders, cont’d  Each symbol table has a nesting level field.  Therefore, at run time, for a given VARIABLE node, the executor can determine the nesting level of the variable.

Computer Science Dept. Fall 2015: October 7 CS 153: Concepts of Compiler Design © R. Mak 12 Allocating an Activation Record  The activation record for a routine (procedure, function, or the main program) needs one or more “data cells” to store the value of each of the routine’s local variables and formal parameters. TYPE arr = ARRAY[1..3] OF integer;... PROCEDURE proc(i, j : integer; VAR x, y : real; VAR a : arr; b : arr); VAR k : integer; z : real; i j x y k z b AR: proc a

Computer Science Dept. Fall 2015: October 7 CS 153: Concepts of Compiler Design © R. Mak 13 Allocating an Activation Record  Obtain the names and types of the local variables and formal parameters from the routine’s symbol table. TYPE arr = ARRAY[1..3] OF integer;... PROCEDURE proc(i, j : integer; VAR x, y : real; VAR a : arr; b : arr); VAR k : integer; z : real; i j x y k z b AR: proc a

Computer Science Dept. Fall 2015: October 7 CS 153: Concepts of Compiler Design © R. Mak 14 Allocating an Activation Record  Whenever we call a procedure or function: Create an activation record. Push the activation record onto the runtime stack. Allocate the memory map of data cells based on the symbol table. TYPE arr = ARRAY[1..3] OF integer;... PROCEDURE proc(i, j : integer; VAR x, y : real; VAR a : arr; b : arr); VAR k : integer; z : real; i j x y k z b AR: proc a No more storing runtime values in the symbol table!

Computer Science Dept. Fall 2015: October 7 CS 153: Concepts of Compiler Design © R. Mak 15 Passing Parameters During a Call  Value parameters: A copy of the value is passed.  VAR parameters: A reference to the actual parameter is passed. PROGRAM main; TYPE arr = ARRAY[1..3] OF integer; VAR index, count : integer; epsilon, delta : real; triplet : arr; PROCEDURE proc(i, j : integer; VAR x, y : real; VAR a : arr; b : arr);... BEGIN {main}... proc(index + 2, count, epsilon, delta, triplet, triplet); END. index count epsilon delta AR: main triplet i j x y b AR: proc a RUNTIME STACK

Computer Science Dept. Fall 2015: October 7 CS 153: Concepts of Compiler Design © R. Mak 16 Memory Management Interfaces  Implementations: Runtime stack: ArrayList Runtime display: ArrayList Memory map: HashMap  Class MemoryFactory creates: runtime stack runtime display memory map cell

Computer Science Dept. Fall 2015: October 7 CS 153: Concepts of Compiler Design © R. Mak 17 Class RuntimeStackImpl  In package backend.interpreter.memoryImpl  Extends ArrayList  Methods push(ActivationRecord ar) Push an activation record onto the runtime stack. ActivationRecord pop() Pop off the top activation record. ActivationRecord getTopmost (int nestingLevel) Return the topmost activation record at a given nesting level. (Uses the runtime display!)

Computer Science Dept. Fall 2015: October 7 CS 153: Concepts of Compiler Design © R. Mak 18 Class RuntimeDisplayImpl  In package backend.interpreter.memoryImpl  Extends ArrayList  Methods ActivationRecord getActivationRecord (int nestingLevel) Get the activation record at a given nesting level. callUpdate(int nestingLevel, ActivationRecord ar) Update the display for a call to a routine at a given nesting level. returnUpdate(int nestingLevel) Update the display for a return from a routine at a given nesting level.

Computer Science Dept. Fall 2015: October 7 CS 153: Concepts of Compiler Design © R. Mak 19 Class MemoryMapImpl  In package backend.interpreter.memoryimpl  Extends HashMap  Methods MemoryMapImpl(SymTab symTab) Allocate the memory cells based on the names and types of the local variables and formal parameters in the symbol table. Cell getCell(String name) Return the memory cell with the given name. ArrayList getAllNames() Return the list of all the names in this memory map.

Computer Science Dept. Fall 2015: October 7 CS 153: Concepts of Compiler Design © R. Mak 20 Class ActivationRecordImpl  In package backend.interpreter.memoryimpl  Fields int nestingLevel MemoryMap memoryMap Values of the local variables and formal parameters ActivationRecord link Link to the previous topmost activation record with the same nesting level in the runtime stack  Method Cell getCell(String name) Return a reference to a memory cell in the memory map that is keyed by the name of a local variable or formal parameter.

Computer Science Dept. Fall 2015: October 7 CS 153: Concepts of Compiler Design © R. Mak 21 Executing Procedure and Function Calls

Computer Science Dept. Fall 2015: October 7 CS 153: Concepts of Compiler Design © R. Mak 22 Class CallDeclaredExecutor  Method execute() Create a new activation record based on the called routine’s symbol table. Execute the actual parameter expressions. Initialize the memory map of the new activation record.  The symbol table entry of the name of the called routine points to the routine’s symbol table.  Copy values of actual parameters passed by value.  Set pointers to actual parameters passed by reference.

Computer Science Dept. Fall 2015: October 7 CS 153: Concepts of Compiler Design © R. Mak 23 Class CallDeclaredExecutor cont’d  Method execute() cont’d Push the new activation record onto the runtime stack. Access the root of the called routine’s parse tree.  The symbol table entry of the name of the called routine points to the routine’s parse tree. Execute the routine. Pop the activation record off the runtime stack.

Computer Science Dept. Fall 2015: October 7 CS 153: Concepts of Compiler Design © R. Mak 24  Method executeActualParms() Obtain the formal parameter cell in the new activation record: Class CallDeclaredExecutor Cell formalCell = newAr.getCell (formalId.getName());

Computer Science Dept. Fall 2015: October 7 CS 153: Concepts of Compiler Design © R. Mak 25  Method executeActualParms() cont’d Value parameter: Class CallDeclaredExecutor Set a copy of the value of the actual parameter into the memory cell for the formal parameter. Object value = expressionExecutor.execute(actualNode); assignmentExecutor.assignValue (actualNode, formalId, formalCell, formalType, value, valueType);

Computer Science Dept. Fall 2015: October 7 CS 153: Concepts of Compiler Design © R. Mak 26 Class CallDeclaredExecutor  Method executeActualParms() cont’d VAR (reference) parameter: Method expressionExecutor.executeVariable() executes the parse tree for an actual parameter and returns the reference to the value. Set a reference to the actual parameter into the memory cell for the formal parameter. Cell actualCell= (Cell) ExpressionExecutor.executeVariable (actualNode); formalCell.setValue(actualCell);

Computer Science Dept. Fall 2015: October 7 CS 153: Concepts of Compiler Design © R. Mak 27 Class Cell  Since a memory cell can contain a value of any type or a reference, we implement it simply as a Java object. package wci.backend.interpreter.memoryimpl; public class CellImpl implements Cell { private Object value = null;... public void setValue(Object newValue) { value = newValue; } public Object getValue() { return value; }

Computer Science Dept. Fall 2015: October 7 CS 153: Concepts of Compiler Design © R. Mak 28 Runtime Error Checking  Range error Assign a value to a variable with a subrange type. Verify the value is within range (not less than the minimum value and not greater than the maximum value) Method StatementExecutor.checkRange()  Division by zero error Before executing a division operation, check that the divisor’s value is not zero.

Computer Science Dept. Fall 2015: October 7 CS 153: Concepts of Compiler Design © R. Mak 29 Pascal Interpreter  Now we can execute entire Pascal programs! Demo