RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.

Slides:



Advertisements
Similar presentations
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Advertisements

Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
CSE 5317/4305 L7: Run-Time Storage Organization1 Run-Time Storage Organization Leonidas Fegaras.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison- Wesley. All rights reserved. 1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
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)
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:
Chapter 9: Subprogram Control
ISBN Chapter 10 Implementing Subprograms –Semantics of Calls and Returns –Implementing “Simple” Subprograms –Implementing Subprograms with.
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.
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
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.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
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.
Storage Bindings Allocation is the process by which the memory cell or collection of memory cells is assigned to a variable. These cells are taken from.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
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,
CSC 8505 Compiler Construction Runtime Environments.
Intermediate Representation II Storage Allocation and Management EECS 483 – Lecture 18 University of Michigan Wednesday, November 8, 2006.
7. Runtime Environments Zhang Zhizheng
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
ISBN Chapter 10 Implementing Subprograms.
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.
Implementing Subprograms
Storage Allocation Mechanisms
Lecture 7 Macro Review Stack Frames
Chapter 10 : Implementing Subprograms
Run-Time Environments Chapter 7
Implementing Subprograms Chapter 10
Implementing Subprograms
Implementing Subprograms
Functions and Procedures
Chapter 10: Implementing Subprograms Sangho Ha
Implementing Subprograms
Implementing Subprograms
The University of Adelaide, School of Computer Science
Understanding Program Address Space
UNIT V Run Time Environments.
Runtime Environments What is in the memory?.
Where is all the knowledge we lost with information? T. S. Eliot
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Implementing Subprograms
Presentation transcript:

RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables

Outline  Lifetime of variables  Memory organization for programs  Stack-based runtime environment  Without local procedure  With local procedure 30/01/ Chapter 6 Code Generation and Data Types

Scope and Lifetime of Variables 30/01/ Chapter 6 Code Generation and Data Types

Scope of Variables 30/01/ Chapter 6 Code Generation and Data Types 4  Static scope: scope of variables are defined by the block location.  Nested static scope: JavaScript, Ada  Non-nested static scope: C, C++  Dynamic scope: scope of variables are defined by the sequence of subprogram calls.  Scope is determined at execution time.  APL, SNOBOL

MAIN Static Scope 30/01/ Chapter 6 Code Generation and Data Types FST SCND IN11 IN12 IN22 IN21 C1 C2 MAINFSTIN11C1C2IN12SCNDIN21IN22 Local variables declared in a child node in the hierarchy can be seen from its parent node.

MAIN Dynamic Scope 30/01/ Chapter 6 Code Generation and Data Types FST SCND IN MAINFSTINSCND Local variables declared in a node can be seen from its predecessors in the path. Call FST Call SCND Call FST Call IN

Lifetime: Categories of Variables 30/01/ Chapter 6 Code Generation and Data Types 7  Static variables  Bound to the same memory location during the whole execution time of the program.  Stack-dynamic variables  Bound to a memory location during one activation of the subprogram.  Explicit heap-dynamic variables  Allocation/deallocation of memory is done explicitly by programmers.  Implicit heap-dynamic variables  Allocation/deallocation of memory is done implicitly when it is needed.

Memory Organization for Programs 30/01/ Chapter 6 Code Generation and Data Types

Memory Area 30/01/ Chapter 6 Code Generation and Data Types Code area Global/static area stack Free space Heap Main memory registers Data area

Code Area 30/01/ Chapter 6 Code Generation and Data Types 10  Addresses in code area are static (i.e. no change during execution) for most programming language.  Addresses are known at compile time.

Data Area 30/01/ Chapter 6 Code Generation and Data Types 11  Addresses in data area are static for some data and dynamic for others.  Static data are located in static area.  Dynamic data are located in stack or heap. Stack (LIFO allocation) for procedure activation record, etc. Heap for user allocated memory, etc.  Activation Record  Collection of data used for each specific subprogram activation.  Contain local variables, parameters, return address, etc.

Memory Areas for Variables 30/01/ Chapter 6 Code Generation and Data Types 12 Static variable In global/static area Stack-dynamic variable In stack area For local variables in subprograms Explicit heap-dynamic variable In heap area Programmers explicitly allocate the memory. Implicit heap-dynamic variable In heap area Systems implicitly allocate the memory.

Calling Sequence  Find the parameters and pass them to the callee.  Save the caller environment, i.e. local variables in activation records, return address.  Create the callee environment, i.e. local variables in activation records, callee’s entry point.  Find the parameters and pass them back to the caller.  Free the callee environment.  Restore the caller environment, including PC. 30/01/ Chapter 6 Code Generation and Data Types Call SequenceReturn Sequence

Static Run-time Environment 30/01/ Chapter 6 Code Generation and Data Types

Static runtime Environments 30/01/ Chapter 6 Code Generation and Data Types 15  Static data  Both local and global variables are allocated once at the beginning and de-allocated at program termination  Fixed address  No dynamic allocation  No recursive call  Procedure calls are allowed, but no recursion.  One activation record for each procedure, allocated statically

Example 30/01/ Chapter 6 Code Generation and Data Types

Without local procedure Stack-based Runtime Environment 30/01/ Chapter 6 Code Generation and Data Types

Local Procedure programmain; {inta[10]; func cal(a[10]:int) {floatans; func sum(e[10]:int) {intt; … return(t); } func ave(b[10],n:int) {intans; ans=sum(b); return(total/n); } ans=ave(a,10); return; } input(a); cal(a); return; } 30/01/ Chapter 6 Code Generation and Data Types main cal sumave

Environment Without Local Procedures 30/01/ Chapter 6 Code Generation and Data Types 19  Run-time environment (i.e. activation record) of each subprogram is pushed into the stack when a subprogram is called.  A stack pointer, pointing to the top of stack, is maintained in an sp register.  A frame pointer, pointing to the current activation record, is maintained in an fp register  In each activation record, a link from an activation record to the activation record of the caller, called a dynamic link or control link is stored.

Example 30/01/ Chapter 6 Code Generation and Data Types main int x, y; call fun1 fun1 int x; call fun2 call fun3 fun2 int y; call fun3 main xy fun3 int y; fun3 y fun2 y fun1 x fun3 y

30/01/ Chapter 6 Code Generation and Data Types parameters activation record of main control link return addr sp fp sp local var.s sp parameters control link return addr sp local var.s sp fp Compute arguments and push into stackStore fp as control linkMove fpPush return addressReserve area for local variables Calling sequenceReturn sequence Load fp into sp (to pop local var.s and return addr) Load control link into fp fp Jump to return addrPop arguments Global area Direction of stack growth

Call Sequence  Push parameters  Push fp as control link  Copy sp to fp  Store return address  Jump to callee  Reserve space for local variables  Copy fp to sp  Load control link into fp  Jump to return address  Change sp to pop parameters 30/01/ Chapter 6 Code Generation and Data Types Calling sequenceReturn sequence

With local procedure Stack-based Runtime Environment 30/01/ Chapter 6 Code Generation and Data Types

Environment with Local Procedures 30/01/ Chapter 6 Code Generation and Data Types  Access link/static link must be included in each activation records  Represents the defining environment  Control link  represents the calling environment  Access link and control link need not be the same

Example 30/01/ Chapter 6 Code Generation and Data Types main int x, y; call fun1 fun1 int x; call fun2 call fun3 fun2 int y; call fun3 main xy fun3 int y; fun3 y fun2 y fun1 x fun3 y

Example 30/01/ Chapter 6 Code Generation and Data Types 26 programmain {inta[10],n ; int cal(int x) {float ans; int func sum(e[10]) { int t; … return(t); } void func ave(b[10]) { ans:int; ans=sum(b); return(ans/n); } ans=ave(x,10)); return; } global area activation record for main access link control link return addr ans a[10] access link control link return addr ans b[10] e[10] access link control link return addr t n=10; input(a); cal(a); return; }

Call Sequence  Push parameters  Push access link  Push fp as control link  Copy sp to fp  Store return address  Jump to callee  Reserve space for local variables  Copy fp to sp  Load control link into fp  Pop access link  Jump to return address  Change sp to pop parameters 30/01/ Chapter 6 Code Generation and Data Types Calling sequenceReturn sequence