Run-Time Environments Chapter 7

Slides:



Advertisements
Similar presentations
Code Generation.
Advertisements

CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
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:
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
Run-Time Storage Organization
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Run time vs. Compile time
Semantics of Calls and Returns
Chapter 9: Subprogram Control
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
Presented by Dr Ioanna Dionysiou
7/13/20151 Topic 3: Run-Time Environment Memory Model Activation Record Call Convention Storage Allocation Runtime Stack and Heap Garbage Collection.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
Runtime Environments Compiler Construction Chapter 7.
Compiler Construction
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage.
Chapter 7 Runtime Environments. Relationships between names and data objects As execution proceeds, the same name can denote different data objects Procedures,
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 9 Def: The subprogram call and return operations of a language are together called its subprogram.
CPSC 388 – Compiler Design and Construction Runtime Environments.
1 Run-Time Environments. 2 Procedure Activation and Lifetime A procedure is activated when called The lifetime of an activation of a procedure is the.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
ISBN Chapter 10 Implementing Subprograms.
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.
CSC 8505 Compiler Construction Runtime Environments.
1 Chapter 10 © 2002 by Addison Wesley Longman, Inc The General Semantics of Calls and Returns - Def: The subprogram call and return operations of.
7. Runtime Environments Zhang Zhizheng
1 Run-Time Environments Chapter 7 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Implementing Subprograms
Lecture 7 Macro Review Stack Frames
Chapter 10 : Implementing Subprograms
Implementing Subprograms Chapter 10
Functions.
Implementing Subprograms
Implementing Subprograms
Names and Attributes Names are a key programming language feature
Subject Name Compiler Design Subject Code: 10CS63
COMPILERS Activation Records
Implementing Subprograms
Run-Time Storage Organization
Run-Time Storage Organization
Run-Time Environments
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Implementing Subprograms
Implementing Subprograms
Run-Time Environments
Implementing Subprograms
Semantic Analysis Semantic analysis includes
UNIT V Run Time Environments.
Run Time Environments 薛智文
Runtime Environments What is in the memory?.
Implementing Subprograms
Implementing Subprograms
Chapter 10 Def: The subprogram call and return operations of
Presentation transcript:

Run-Time Environments Chapter 7 Lecture # 22 Run-Time Environments Chapter 7

Runtime Environments Before code generation static source text of a program needs to be related to the actions that must occur at runtime to implement the program. As execution proceeds same name in the source text can denote different data objects in the target machine. The allocation and de allocation of data objects is managed by the runtime support package.

7.1 Source Language Issues Each execution of a function is referred to as an activation of the procedure/function If the function is recursive several of its activations may be alive at the same time

Procedure Activation and Lifetime A procedure is activated when called The lifetime of an activation of a procedure is the sequence of steps between the first and last steps in the execution of the procedure body A procedure is recursive if a new activation can begin before an earlier activation of the same procedure has ended

Activation Tree We use an activation tree to depict the way control enters and leaves activations. In an activation tree: Each node represents activation of a procedure The root represents activation of main program The node for a is the parent of the node for b if the control flows from activation a to b The node for a is to the left of node for b if the lifetime of a occurs before the lifetime of b

Procedure Activations: Example program sort(input, output) var a : array [0..10] of integer; procedure readarray; var i : integer; begin for i := 1 to 9 do read(a[i]) end; function partition(y, z : integer) : integer var i, j, x, v : integer; begin … end procedure quicksort(m, n : integer); var i : integer; begin if (n > m) then begin i := partition(m, n); quicksort(m, i - 1); quicksort(i + 1, n) end end; begin a[0] := -9999; a[10] := 9999; readarray; quicksort(1, 9) end. Activations: begin sort enter readarray leave readarray enter quicksort(1,9) enter partition(1,9) leave partition(1,9) enter quicksort(1,3) … leave quicksort(1,3) enter quicksort(5,9) … leave quicksort(5,9) leave quicksort(1,9) end sort.

Activation Trees: Example q(1,9) p(1,9) q(1,3) q(5,9) p(1,3) q(1,0) q(2,3) p(5,9) q(5,5) q(7,9) p(2,3) q(2,1) q(3,3) p(7,9) q(7,7) q(9,9) Activation tree for the sort program Note: also referred to as the dynamic call graph

Control Stack The flow of control of a program corresponds to depth first traversal of the activation tree Control stack is used to keep track of the live procedure activations. The idea is to push the node when activation begins and pop when it ends

Factorial Example discussed public static int Factorial(int num) { if (num == 1) fact=1; } else fact=fact+num*Factorial(--num); return fact;

Addition of Elements of Array recursively public static int Add(int index) { if (index == 0) sum = arr[index]; } else sum = sum + Add(arr[index]); return sum;

Control Stack Activation tree: Control stack: Activations: begin sort enter readarray leave readarray enter quicksort(1,9) enter partition(1,9) leave partition(1,9) enter quicksort(1,3) enter partition(1,3) leave partition(1,3) enter quicksort(1,0) leave quicksort(1,0) enter quicksort(2,3) … s s q(1,9) q(1,3) q(2,3) r q(1,9) p(1,9) q(1,3) p(1,3) q(1,0) q(2,3)

Scope Rules Environment determines name-to-object bindings: which objects are in scope? program prg; var y : real; function x(a : real) : real; begin … end; procedure p; var x : integer; begin x := 1; … end; begin y := x(0.0); … end. Variable x locally declared in p A function x

Binding of Names If a name is declared once in a program it can hold different values at runtime The term environment refers to a function that maps name to a storage location The term state is referred to a function that maps a storage location to the value held there

Mapping Names to Values environment state name storage value var i; … i := 0; … i := i + 1;

Mapping Names to Values At compile time At run time environment state name storage value var i; i := 0; i := i + 1; Environment and states are different An assignment changes the state but not the environment

Static and Dynamic Notions of Bindings Static Notion Dynamic Notion Definition of a procedure Activations of the procedure Declaration of a name Bindings of the name Scope of a declaration Lifetime of a binding

Storage Organization Runtime storage may be subdivided as: The generated target code Data objects Control stack to keep track of procedure activations The size of target code is fixed at compile time Similarly size of data objects may be known

Typical subdivision of runtime memory A separate area of runtime memory called Heap holds all other runtime information Code Static Data Stack Heap

Stack Allocation Activation records (subroutine frames) on the run-time stack hold the state of a subroutine Calling sequences are code statements to create activations records on the stack and enter data in them Caller’s calling sequence enters actual arguments, control link, access link, and saved machine state Callee’s calling sequence initializes local data Callee’s return sequence enters return value Caller’s return sequence removes activation record

Activation Records (Subroutine Frames) fp (frame pointer) Returned value Actual parameters Optional control link Optional access link Save machine status Local data Temporaries Caller’s responsibility to initialize Callee’s responsibility to initialize

Control Links fp sp Stack growth The control link is the old value of the fp Caller’s activation record fp Callee’s activation record Control link sp Stack growth

Access Links (Static Links) s a x q(1,9) access k v s a x q(1,9) access k v q(1,3) access k v s a x q(1,9) access k v q(1,3) access k v p(1,3) access i j s a x q(1,9) access k v q(1,3) access k v p(1,3) access i j e(1,3) access The access link points to the activation record of the static parent procedure: s is parent of r, e, and q q is parent of p

Accessing Nonlocal Data To implement access to nonlocal data a in procedure p, the compiler generates code to traverse np - na access links to reach the activation record where a resides np is the nesting depth of procedure p na is the nesting depth of the procedure containing a

Parameter Passing Modes Call-by-value: evaluate actual parameters and enter r-values in activation record Call-by-reference: enter pointer to the storage of the actual parameter