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)

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
CSE 5317/4305 L7: Run-Time Storage Organization1 Run-Time Storage Organization Leonidas Fegaras.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
Implementing Subprograms
(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 Handling nested procedures Method 1 : static (access) links –Reference to the frame of the lexically enclosing procedure –Static chains of such links.
ISBN Chapter 10 Implementing Subprograms.
Honors Compilers Addressing of Local Variables Mar 19 th, 2002.
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:
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
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 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.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
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 )
Subprograms - implementation
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
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
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.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Run-Time Environments Chapter 7
Implementing Subprograms Chapter 10
Implementing Subprograms
Run-Time Storage Organization
Run-Time Storage Organization
Run-Time Environments
Run-Time Environments
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
UNIT V Run Time Environments.
Run Time Environments 薛智文
Runtime Environments What is in the memory?.
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Run-time environments
Presentation transcript:

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) { if (*a > *b) return a; //OK else return b; //OK }

3 Memory of a computer Register RAM - Code Area - Data Area

4 General Organization of Runtime Storage code area stack SP global/static data area free space heap

5 Code Area code for procedure1 code for procedure2 code for procedure3. code for procedureN SP entry point entry point entry point entry point

6 Fully Static Runtime Environments code for main procedure code for procedure 1 ………. code for procedure N global data area activation record for main procedure activation record for proc 1 SP Code Area Data Area ……………. activation record for proc N

7 Activation Record - a contiguous block of storage recording the state of an activation of a function, not all the compilers set the same fields. Each execution of a procedure is referred to as an activation of the procedure. If the procedure is recursive, several of its activation may be alive at the same time.

8 What is the contents of the activation record? temporary values local data saved machine status optional access link to refer to non-local data held in other activation records optional control link to refer to the activation actual parameters returned value

General Organization of a Activation Record Local & temp. data Old SP Return value Return address Argument count Parameter t n Parameter t 2 Parameter t n -1.. SP Direction of Growth caller’s responsibility callee’s duty

10 So, local data are accessed by negative offsets from SP (stack pointer, a register holding a particular position of currently activated procedure). A local name X can be referenced by X[SP] ([SP] means the address of SP), where X is the (negative) offset for the name from the location pointed to by SP. Parameter can be referred to by an positive offset from SP. That is, the ith parameter can be referenced by (4+n-i)[SP] (assume each entry takes one unit of space)

Suppose the Memory structure for C program is organized as below, where function P calls Q and Q calls R 0.. Static area for programs and global (static) data.. 2 n -1 Top Extra storage for R Activation record for R Direction Extra storage for Q Activation record for Q SP of growthExtra storage for P Activation record for P (fp) (sp)

TOP is a register holding the top address of the stack. SP is also a permanently allocated register (stack pointer) holding a specific address in the activation record of the currently active procedure. We use SP to indirectly refer to the local variables, parameters, etc. Extra storage is used for storing pointers on the display, variable-length data such as dynamically allocated arrays, etc.

13

14 Growth direction 2 n -1 0

15

At run-time e.g. p (T1, T2,..., Tn) ==> param T1 => push (T1) param T2 => push (T2)..... param Tn => push (Tn) call p, n => push (n) => push (l 1 ) => push () => push (SP) => goto l 2 l 1 denotes the return address; l 2 denotes the address of the first statement of the called procedure.

17 e.g., p (A+B*C, D) ==> T1 = B * C T2 = A + T1 Param T2 Param D Call p, 2 Temporary variable

At run-time * Assume TOP points to the lowest-numbered used location on the stack and the memory locations are counted by words. 'param T' is translated into 'push(T)' which stands for TOP = TOP - 1; /* now TOP points to an available entry */ *TOP = T; /* save T into the memory 'call p, n' is translated into the following instructions: push (n) /*store the argument count */ push (l 1 ) /* l 1 is the return address */ push () /* leave one space for the return value */ push (SP) /* store the old stack pointer */ goto l 2 /* l 2 is the first statement of the called procedure p */

General Organization of a Activation Record Local & temp. data Old SP Return value Return address Argument count Parameter t n Parameter t 2 Parameter t n -1.. TOP Direction of Growth caller’s responsibility SP

The first statement of the called procedure must be a special three-address code 'procbegin', which (1) sets the stack pointer to the place holding the old SP and (2) sets TOP to the top of the activation record (or the stack), so 'procbegin' stands for: SP = TOP; // now SP points to old SP value TOP = SP + size_of_p; /* size_of_p is the size of p, i.e., the number of words taken by the local data for p */ The first statement of the called procedure

General Organization of a Activation Record Local & temp. data Old SP Return value Return address Argument count Parameter t n Parameter t 2 Parameter t n -1.. SP Direction of Growth caller’s responsibility callee’s duty TOP

The ‘return’ statement The return statement in c can have the form 'return (expression);' This statement can be implemented by three-address code to evaluate the expression into a temporary T followed by: 1[SP] = T /* 1 is the offset for the location of the return value */ TOP = SP + 2 /* TOP now point to the return address */ SP = *SP /* restore SP, SP now points to old location */ L = *TOP /* the value of L is now the return address */ TOP = TOP + 1 /* TOP points to the argument count */ TOP = TOP *TOP /* *TOP is the number of parameter of P. We restore TOP to the top of extra storage for the activation record below */ go to *L

23 Do not expect a procedure to do anything that requires it to know the size of the activation record of another procedure.

24 Control Link & Access Link Control (Dynamic) Link - link the caller’s activation record and thus allows us to restore the state (activation record) of the caller upon procedure exit. Access (Static) Link - a link used to access non-local variables

B P... P Q B P... P P Q Control Links Access and Control Links growth direction P Two procedures P and Q are defined in procedure B; P calls itself recursively several times before it calls Q. Procedure B procedure P { P….. Q….. } procedure Q {…… …… } … P …. Access Links Control Link

Nested Block Structures in C e.g. Int test( ) { int x,y,m; ….. { int m=1, n=2; x = m + n; …. } ….. { int w=2, k =4; m = w * k; … } ….. }

Access variables for activation record with access links For local variables e.g., fetch (-1)[SP] // assume (-1) is the offset of a local variable. For non-local variables with static distance 1 e.g., EP = (1)[SP] //EP is a register; assume access link is stored in (1)[SP]. EP = (-1)[EP] // EP now points to what the SP should point to. fetch (-3)[EP] // (-3) is the offset of the non-local variable. For non-local variables with static distance 2 e.g., EP = (1)[SP] EP = *EP // the content of EP is saved in EP EP = (-1)[EP] // EP now points to what the SP should point to. fetch (-5)[EP] // assume (-5) is the offset of the non-local

28 Steps to access a non-local variable at run-time 1. Skip down the access links given by the static distance to get the activation record in which the variable resides. The static distance between the use and the declaration of the variable is a constant computed by the compiler at compile-time. 2. The address of the variable is obtained by adding the fixed offset of the variable to the address obtained in step 1. The offset is also a constant computed by the compiler at compile-time.

29 Variable-Length Data A common strategy for handling variable-length data is shown below, where procedure p has three local arrays. The storage for these arrays is not part of the activation record for p; only a pointer to the beginning of each array appears in the activation record. The relative addresses of these pointers are known at compile-time, so the target code can access array elements through pointers.

control link Pointer to A Pointer to B array A array B activation record for P arrays of P control link TOP Activation record of Q called by P array of q SP Access to dynamically allocated arrays int x[10]; int A[w]; Int B[m]; …… read w; m= w * 10; w*2

31 Passing function as a parameter

Program HP proc p (func h) h proc q func f p(g) p(f) func g q HP q p f Stack configuration after the call to p(f) and (dashed) after the call to h. call to h After the call to p(f) access links

Program HP proc p (func h) h proc q func f p(g) p(f) func g q HP q p f Stack configuration after the call to p(g) and (dashed) after the call to h. call to h p g after call to p(g)

34 Display An array data structure (array of pointers to entries of activation record, e.g., d[ ]) used to store the specific location of (1) currently activated function’s activation record and (2) the activation records of each level of declared nested functions. Therefore, storage for a non-local variable a at nesting depth i is in the activation record pointed to by display element d[i].

e.g. program s (input, output); var a: array[0..10] of integer; x: integer; procedure r; var i: integer; begin... a... end; /* r */ procedure e (i,j: integer); begin x := a[i]; a[i] = a[j]; a[j] := x end; /* e */ procedure q (m,n: integer); var k,v: integer; function p (y,z: integer):integer; var i,j: integer; begin... a v... e (i,j); end; /* p */ begin end; /* q */ begin... end. /* s */

d [1] d [2] q (1,9) saved d [2] S q (1,9) saved d [2] S d [1] d [2] q(1,3) saved d [2] growth direction (a) (b)

q (1,9) saved d [2] S d [1] d [2] q(1,3) saved d [3] growth direction d [3] p(1,3) saved d [2] ( c )

q (1,9) saved d [2] S d [1] d [2] q(1,3) saved d [3] growth direction d [3] p(1,3) saved d [2] ( d ) e(1,3) saved d[2]

39 Assignment #5b Do exercises 6.2, 6.14, 7.2, 7.10 of the textbook.