7. Runtime Environments Zhang Zhizheng

Slides:



Advertisements
Similar presentations
Intermediate Code Generation
Advertisements

(1) ICS 313: Programming Language Theory 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)
CS 536 Spring Run-time organization Lecture 19.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
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
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
1 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Presented by Dr Ioanna Dionysiou
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
ISBN Chapter 10 Implementing Subprograms.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
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.
Basic Semantics Associating meaning with language entities.
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
1 CIS 461 Compiler Design and Construction Winter 2012 Lecture-Module #16 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon.
ISBN Chapter 10 Implementing Subprograms.
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.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
國立台灣大學 資訊工程學系 薛智文 98 Spring Run Time Environments (textbook ch# 7.1–7.3 )
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.
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.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
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
Procedures and Functions Procedures and Functions – subprograms – are named fragments of program they can be called from numerous places  within a main.
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
Constructs for Data Organization and Program Control, Scope, Binding, and Parameter Passing. Expression Evaluation.
ISBN Chapter 10 Implementing Subprograms.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
ISBN Chapter 10 Implementing Subprograms.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Run-Time Environments Chapter 7
Implementing Subprograms Chapter 10
Names and Attributes Names are a key programming language feature
Subject Name Compiler Design Subject Code: 10CS63
Implementing Subprograms
Run-Time Storage Organization
Run-Time Storage Organization
Run-Time Environments
Implementing Subprograms
Run-Time Environments
Implementing Subprograms
Topic 3-a Calling Convention 1/10/2019.
UNIT V Run Time Environments.
Run Time Environments 薛智文
Implementing Subprograms
Presentation transcript:

7. Runtime Environments Zhang Zhizheng

7. 0 Overview 1.Program vs program execution A program consists of several procedures (functions) An execution of a program is called as a process An execution of a program would cause the activation of the related procedures A name (e.g, a variable name)in a procedure would be related to different data objects

Notes: An activation may manipulate data objects allocated for its use.

2. Allocation and de-allocation of data objects –Managed by the run-time support package –Allocation of a data object is just to allocate storage space to the data object relating to the name in the procedure

Notes: The representation of a data object at run time is determined by its type.

3.Activation Trees A tree to depict the control flow enters and leaves activation of a procedure f(4) f(3) f(2) f(2) f(1) f(1) f(0) f(1) f(0) Enter f(4) Enter f(3) Enter f(2) Enter f(1) Leave f(1) Enter f(0) Leave f(0) Leave f(2) Enter f(1) Leave f(1) Leave f(3) Enter f(2) Enter f(1) Leave f(1) Enter f(0) Leave f(0) Leave f(2) Leave f(4)

Each node represents an activation of a procedure The root represents the activation of the main program The node for a is the parent of the node for b if and only if the control flows from activation of a to b, and The node for a is to the left of the node for b if and only if the lifetime of a occurs before the lifetime of b.

So, the flow of control in a program corresponds to a depth-first traversal of the activation tree that starts at the root. Printing enter when the node for an activation is reached for the first time, and Printing leave after the entire subtree of the node has been visited during the traversal.

4.Control stacks A stack to keep track of live procedure activations: Push the node for an activation onto the control stack as the activation begins and to pop the node when the activation ends. Actually, PUSH=ENTER; POP=LEAVE

5.Bindings of Names When an environment associates storage location s with a name x, we say that x is bound to s; the association itself is referred to as a binding of x.

Notes: 1)Even each name is declared once in a program, the same name may denote different object at run time 2)The “ data object” corresponds to a storage location that can hold values

3)In programming language semantics, the term “environment” refers to a function that maps a name to a storage location, and term “state” refers to a function that maps a storage location to the value held there. environment state name storage value

4)Environments and states are different; an assignment changes the state, but not the environment 5)If x is not of a basic type, the storage s for x may be a collection of memory words 6)A binding is the dynamic counterpart of a declaration

6.Factors that determines the run time environment of a program –Recursive definition –Local name storage space allocation strategy –global name storage space allocation strategy –Parameter passing mechanism –A function is whether allowed to be a parameter of or return value of a function

7. 1 Storage organization 1.Subdivision of Runtime Memory Typical subdivision of runtime memory into code and data areas code Static data stack heap The generated target code; Data objects; A counterpart of the control stack to keep track of procedure activations

2.Activation Records/Frames 1) Definition A contiguous block of storage that stores information needed by a single execution of a procedure

2) A general activation record Returned value Actual parameters Optional control link Optional access link Saved machine status Local data temporaries Such as those values in the evaluation of expressions Data that is local to the execution of a procedure Information about the state of the machine just before the procedure is called Refer to non-local data held in other activation records/fixed place Point to the activation record of the caller Used by the caller to supply parameters to the callee The value that the callee return to caller

7. 2 Storage allocation strategies 1.Storage allocation strategies –Static allocation Lays out storage for all data objects at compile time. –Stack allocation Manages the run-time storage as a stack. –Heap allocation Allocates and de-allocates storage as needed at run time from a heap.

2.Static allocation Names are bound to storage as the program is compiled, so there is no need for a run-time support package. Since the bindings do not change at run time, every time a procedure is activated, its names are bound to the same storage locations.

Some Limitations –The size of a data object and constraints on its position in memory must be known at compile time. –Recursive procedures are restricted, because all activations of a procedure use the same bindings for local name. –Data structures cannot be created dynamically.

3.Heap allocation –A separate area of run-time memory, called a heap. –Heap allocation parcels out pieces of contiguous storage, as needed for activation records or other objects. Pieces may be de-allocated in any order. I -11J -11 X 0 A -21

Heap allocation is useful if either of the following is possible. 1.The value of local names must be retained when an activation ends. 2.A called activation outlives the caller.

4. Stack allocation 1)Main idea –Based on the idea of a control stack; storage is organized as a stack, and activation records are pushed and popped as activations begin and end, respectively. –Storage for the locals in each call of a procedure is contained in the activation record for that call. –Locals are bound to fresh storage in each activation

Position in AT AR on the stack Remark s S a: array S a: array r i: integer s r s r q(1, 9) S a: array q(1 9) i: integer

s r q(1, 9) S a: array q(1 9) i: integer q(1 3) i: integer p(1, 9) q(1, 3) p(1, 3)q(1, 0)

2)Calling sequences –A call sequence allocates an activation record and enters information into its fields –A return sequence restores the state of the machine so the calling procedure can continue execution.

Parameters & returned value Control link links and saved status Temporaries and local data Parameters & returned value Control link links and saved status Temporaries and local data Top-sp Caller’s Responsibility Callee’s Responsibility Caller’s activation record Callee’s activation record Division of tasks between caller and callee The register top-sp points to the end of the machine status field

Steps: (1)The caller evaluates actual parameters. (2)The caller stores a return address and the old value of top-sp into the callee’s activation record. (3)The callee saves register values and other status information (4)the callee initializes its local data and begins execution.

A possible return sequence : –(1)The callee places a return value next to the activation record of the caller –(2)Using the information in the status field, the callee restores top-sp and other registers and branches to a return address in the caller’s code. –(3)Although top-sp has been decremented, the caller can copy the returned value into its own activation record and use it to evaluate an expression.

ACCESS TO NONLOCAL NAMES P Algol (Alan J.Perlis)

3)Stack allocation for non-nested procedure for C In C, a procedure definition cannot appear within another. If there is a nonlocal reference to a name a in some function, then a must be declared outside any function.

–Stack allocation for C –When entering a procedure, the activation record of the procedure is set up on the top of the stack –Initially, put the global(static) variables and the activation record of the Main function

e.g. the calling sequence is like the following: main () {…… q( ); } p( ) {……} q( ) { …… p( ); …… }

Activation record for main Activation record for q Activation record for P TOP Top-SP

Main AR Q AR P AR TOP SP global Var

Caller’s SP Calling Return address Amount of parameters Formal parameters Local data Array data Temporaries Returned value

#include int x,y; int main() { x=5; y=f(x); }

int f(int n) { if (n<=1) return 1; else { int t1,t2,t; t1=f(n-1); t2=f(n-2); t=t1+t2; return t} }

….. Activation Record of the function called by Main function Activation Record of Main function Global Variable Data Area Storage Organization of C Language

The Form of Activation Record of any a function in C Unit for Returned Value of function Internal Temporary Work Units Local Variable Data Storage Units Formal parameter Storage Units Number of Formal Parameters Returned Address Caller’s SP (Start Address of caller’s activation record) TOP SP

1) Here we assume that the caller’s sp of Main function is the start address of global variable data area, and the returned address in the activation record of a function (including Main function) is filled by the operating system automatically, you might not care it. 2) The start address of stack used in the program is K. How about the stack map of the above c program?

4)Stack allocation for nested procedures (1) the nesting of procedure definitions in the Pascal program P0P0 P1P1 P2P2 Call P 2 Call P 1 P0P0 P1P1 P2P2 Call P 2 Call P 1

(2) nesting depth i-1i-1 P0P0 …… P i-1 PiPi i 0

(3) access links P0P0 P1P1 P2P2 Call P 2 Call P 1 TOP Top-SP P2P2 P1P1 P0P0 Access link

TOP Top-SP P2P2 P1P1 P0P0 Access link P0P0 P1P1 P2P2 Call P 2 Call P 1

(4) displays –Faster access to global than with access links can be obtained using an array d of pointers to activation records, called a display.

j-1j-1 Sp for p 0 …… Sp for p j-1 Sp for p j j 0 d[j+1]

Access non-local name by display. Suppose control is in an activation of a procedure p at nesting depth j. Then, the first j -1 elements of the display point to the most recent activations of the procedure that lexically enclose procedure p, and d [ j ] points to the activation of p.

–When a new activation record for a procedure at nesting depth i is setup, we A) save the value of d[i] in the new activation record; B) set d[i] to point to the new activation record.

Example: Program M; var A,B:integer function F(N:integer):integer; if N<=2 then return(N) else return (N*F(N-1)); begin read(A); /*Let A=5*/ B:=F(A); write (B); end. Please write down the display of the above program

M F(5) Saved d[1] F(4) Saved d[1] F(3) Saved d[1] F(2) Saved d[1] d[1] d[2]

7. 3 Parameter passing 1.Parameter-passing methods Call-by-value Call-by-reference Copy-restore Call-by-name

2.Call-by-value 1) in C and Pascal 2)implementation –A formal parameter is treated just like a local name, so the storage for the formals is in the activation record of the called procedure. –The caller evaluates the actual parameters and places their r-values in the storage for the formals.

3.Call-by-Reference 1) also known as call-by-address or call-by- location. 2)implementation –If an actual parameter is a name or an expression having an l-value, then that l- value itself is passed. –If the actual parameter is an expression that has no l-value, then the expression is evaluated in a new location, and the address of that location is passed.

4.Copy-Restore 1)a hybrid between call-by-value and call-by- reference is copy-restore linkage. 2)implementation –Before control flows to the called procedure, the actual parameters are evaluated.(copy-in) –When control returns,the current r-value of the formal parameters are copied back into the l-values of the actuals, using the l- values computed before the call(copy-out) Notes: Only actuals having l-values are copied.

5.Call-by-Name Traditionally defined by the copy-rule of Algol It is just as macro-expansion or in-line expansion

7. 4 Symbol Tables 1.Functions of symbol tables Keep track of scope and binding information about names Allow us to add new entries and find existing entries efficiently

Notes: 1)Changes to the table occur if a new name or new information about an existing name is discovered 2)The table may be a linear list or a hash table 3)It is useful for a compiler to be able to grow the symbol table dynamically, if necessary, at compile time

2.Symbol-table entries Each entry in the symbol table is for the declaration of a name Each entry can be implemented as a record consisting of a sequence of consecutive words of memory

Notes: 1)Information may be entered into the symbol table at various times 2)Attributes of a name are entered in response to declarations

Exercises 7.6 Addition 1:We assume that the storage organization and the form of activation record used in C language program run- time stack storage allocation are as following. Please construct the run-time stack map (detailed map) for the following C program

Storage Organization of C Language ….. Activation Record of the function called by Main function Activation Record of Main function Global Variable Data Area

The Form of Activation Record of any a function in C Unit for Returned Value of function Internal Temporary Work Units Local Variable Data Storage Units Formal parameter Storage Units Number of Formal Parameters Returned Address Caller’s SP (Start Address of caller’s activation record) TOP SP The C program is as the following:

#include int x,y,z; int main() { x=3; y=9; z=f(x,y); } int f(int m, int n) { if (m>=0 && n>=0) {if (m==0) return n; else if (n==0) return m; else { int t1,t2,t; if (m>=n) { t1=m-n; t2=n } else {t1=n-m; t2=m} t=f(t1,t2); return t }

Notes: 1) Here we assume that the caller’s sp of Main function is the start address of global variable data area, and the returned address in the activation record of a function (including Main function) is filled by the operating system automatically, you might not care it. 2) The start address of stack used in the program is K.