1 CIS 461 Compiler Design and Construction Winter 2012 Lecture-Module #16 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon.

Slides:



Advertisements
Similar presentations
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Advertisements

Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Code Shape IV Procedure Calls & Dispatch Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
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)
1 Handling nested procedures Method 1 : static (access) links –Reference to the frame of the lexically enclosing procedure –Static chains of such links.
CS 536 Spring Run-time organization Lecture 19.
The Procedure Abstraction Part III: Allocating Storage & Establishing Addressability Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all.
ISBN Chapter 10 Implementing Subprograms.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
ISBN Chapter 10 Implementing Subprograms.
Runtime Environments Source language issues Storage organization
Lecture 18 Procedures Topics Procedural Abstraction Activation records Readings: 7.4 March 20, 2006 CSCE 531 Compiler Construction.
Honors Compilers Addressing of Local Variables Mar 19 th, 2002.
The Procedure Abstraction Part I: Basics Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Run time vs. Compile time
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Run-time Environment and Program Organization
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.
The Procedure Abstraction Part II: Symbol Tables, Storage Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled.
Chapter 8 :: Subroutines and Control Abstraction
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
The Procedure Abstraction Part I: Basics Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
Intermediate Representations. Front end - produces an intermediate representation (IR) Middle end - transforms the IR into an equivalent IR that runs.
Runtime Environments Compiler Construction Chapter 7.
Compiler Construction
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
The Procedure Abstraction, Part V: Support for OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
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.
Basic Semantics Associating meaning with language entities.
Activation Records (in Tiger) CS 471 October 24, 2007.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Code Shape IV Procedure Calls & Dispatch Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
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.
The Procedure Abstraction, Part IV Addressability Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp.
國立台灣大學 資訊工程學系 薛智文 98 Spring Run Time Environments (textbook ch# 7.1–7.3 )
CSC 8505 Compiler Construction Runtime Environments.
7. Runtime Environments Zhang Zhizheng
1 Run-Time Environments Chapter 7 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
The Procedure Abstraction Part III: Symbol Tables, Storage Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
The Procedure Abstraction, Part III Storage Layout & Addressability Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
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.
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?
The Procedure Abstraction Part III: Symbol Tables, Storage
Implementing Subprograms
Run-time organization
The Procedure Abstraction Part IV: Allocating Storage & Establishing Addressability Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights.
The Procedure Abstraction Part II: Symbol Tables, Storage
Chapter 9 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Code Shape IV Procedure Calls & Dispatch
Lecture 13: The Procedure Abstraction; Run-Time Storage Organisation
UNIT V Run Time Environments.
Run Time Environments 薛智文
Runtime Environments What is in the memory?.
Presentation transcript:

1 CIS 461 Compiler Design and Construction Winter 2012 Lecture-Module #16 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon

2 The Procedure Abstraction Procedure is an abstraction (illusion) provided by programming languages –Control abstraction: the transfer of control between caller and callee –Namespace (scope): caller and callee have different namespaces –External interface: passing arguments between caller and callee Procedure abstraction –establishes a separation of concerns –enables modular design –enables separate compilation Compiler translates procedure abstraction to something that hardware can understand

3 Pascal Example program Main(input, output); var x, y, z: integer; procedure Fee; var x: integer; begin { Fee } x := 1; y := x * end; procedure Fie; var y: real; procedure Foe; var z: real; procedure Fum; var y: real; begin { Fum } x := 1.25 * z; Fee; writeln(‘x = ‘, x) end; begin { Foe } z := 1; Fee; Fum end; begin { Fie } Foe; writeln(‘x= ‘, x) end; begin { Main } x := 0; Fie end. Main FeeFie Fum Nesting relationships (lexical scoping uses this) Foe Main Fie Fum Foe Activation Tree shows the transfer of control between different procedures (dynamic scoping uses this) Fee

4 Control and Data To deal with procedures we need to keep track of two basic things Transfer of control between procedures: What is the return address, –Where is the context for the caller? We need a control link which will point to the context of the caller –What is the return address? In languages such as C and Pascal callee cannot outlive its caller, this means that we can store the return addresses in a stack Where is the data: –Where is the local data for the procedure? Again we can use a stack –Where is the non-local data? We need an access link which will point to the next lexical scope Where do we keep this information: in an Activation Record

5 Activation Record Basics parameters register save area return value return address access link control link local variables ARP Space for parameters of the procedure Saved register contents (to restore the machine status for the caller on return) Space for return value Address to resume caller Link for non-local access Stores caller’s ARP to restore caller’s AR on return Space for local values & variables (including temporaries) One AR for each invocation of a procedure (one AR for each node in the activation tree)

6 Variables in the Activation Record How do we find the variables in the activation record at run-time? They are at known offsets from the ARP Access the variables by (offset is a constant, also assume ARP is stored in R0): Variable-length data If AR can be extended, put variable-length data below local variables Leave a pointer at a known offset from ARP Otherwise, put variable-length data on the heap Initializing local variables Must generate explicit code to store the values which will be executed in each activation Among the procedure’s first actions MOV offset(R0), R1 MOV R1, offset(R0) This loads the value of the variable to register R1 This stores the value of R1 to the variable

7 Where Do We Store the Activation Record? Where do activation records live? If lifetime of AR matches lifetime of invocation, AND If code normally executes a “return”  Keep AR s on a stack If a procedure can outlive its caller, OR If it can return an object that can reference its execution state  AR s must be kept in the heap If a procedure makes no calls OR If language does not allow recursion  AR can be allocated statically

8 Placing Run-time Data Structures Classic Organization Code, static, and global data have known size > Use symbolic labels in the code Heap and stack both grow and shrink over time This is a virtual address space, it is relocatable Better utilization if stack and heap grow toward each other Uses address space, not allocated memory CodeCode S G t l a & o t b i a c l HeapHeap StackStack Single Logical Address Space 0 high

9 How Does This Really Work? The Big Picture CodeCode S G t l a & o t b i a c l HeapHeap StackStack CodeCode S G t l a & o t b i a c l HeapHeap StackStack CodeCode S G t l a & o t b i a c l HeapHeap StackStack CodeCode S G t l a & o t b i a c l HeapHeap StackStack... Hardware’s view Compiler’s view OS’s view Physical address space_ virtual address spaces 0high

10 Memory Allocation How does the compiler represent memory location for a specific instance of variable x ? Name is translated into a static distance coordinate – pair –“level” is lexical nesting level –“offset” is unique within that scope –“offset” is assigned at compile time and it is used to generate code that executes at run-time Static distance coordinate is used to generate addresses –For each lexical scope level we have to generate a base address –offset gives the location of a variable relative to that base address

11 Memory Allocation Local to procedure keep them in the AR or in a register lifetime matches procedure’s lifetime For Static and Global Variables Lifetime is entire execution, can be stored statically Generate assembly language labels for the base addresses (relocatable code) Static Procedure scope  mangle procedure name to generate a label File scope  mangle file name to generate a label Global One or more named global data areas One per variable, or per file, or per program, …

12 Establishing Addressability Must create base addresses Global and static variables –Construct a label by mangling names (i.e., &_fee) Local variables –Convert to static distance coordinate and use ARP + offset –ARP becomes the base address Local variables of other procedures –Convert to static distance coordinate –Find appropriate ARP –Use that ARP + offset { Must find the right AR Need links to other AR s

13 Establishing Addressability Using access links Each AR has a pointer to AR of lexical ancestor Lexical ancestor need not be the caller Reference to runs up access link chain to p and adds 16 Cost of access is proportional to lexical distance parameters register save area return value return address access link caller’s ARP local variables A RP parameters register save area return value return address access link caller’s ARP local variables parameters register save area return value return address access link caller’s ARP local variables Some setup cost on each call These are activation records, NOT symbol tables symbol tables: compile time activation records: run time

14 Establishing Addressability Using access links Access and maintenance cost varies with level All accesses are relative to ARP Assume Current lexical level is 2 Access link is at ARP – 4 We load the value of the variable to register R2 We assume that ARP is always stored in R0 Maintaining access link Calling level k+1  Use current ARP as link Calling level j < k  Find ARP for j +1  Use that ARP as link SDCGenerated Code MOV 8(R0), R2 MOV -4(R0), R1 MOV 12(R1), R2 MOV -4(R0), R1 MOV -4(R1), R1 MOV 16(R1), R2

15 Establishing Addressability Using a display Global array of pointer to nameable AR s Needed ARP is an array access away Reference to looks up p’s ARP in display & adds 16 Cost of access is constant ( ARP + offset) ARP parameters register save area return value return address saved ptr. caller’s ARP local variables parameters register save area return value return address saved ptr. caller’s ARP local variables level 0 level 1 level 2 level 3 Display parameters register save area return value return address saved ptr. caller’s ARP local variables Some setup cost on each call

16 Establishing Addressability Using a display Access and maintenance costs are fixed Address of display may consume a register Assume Current lexical level is 2 Display is at label _DISP Maintaining access link On entry to level j  Save level j entry into AR  Store AR in level j slot On exit from level j  Restore level j entry Desired AR is at _DISP + 4  level SDCGenerated Code MOV 8(R0), R2 MOV _DISP, R1 MOV 4(R1), R1 MOV 12(R1), R2 MOV _DISP, R1 MOV *R1, R1 MOV 16(R1), R2

17 Establishing Addressability Access links versus Display Each adds some overhead to each call Access links costs vary with level of reference –Overhead only incurred on references and calls –If AR s outlive the procedure, access links still work Display costs are fixed for all references –References and calls must load display address –Typically, this requires a register Relative performance Depends on ratio of non-local accesses to calls Extra register can make a difference in overall speed For either scheme to work, the compiler must insert code into each procedure call & return

18 Parameter Passing Call-by-value (used in C) –formal parameter has its distinct storage –caller copies the value of the actual parameter to the appropriate parameter slot in callee’s AR –do not restore on return –arrays, structures, strings are problem Call-by-reference (used in PL/I ) –caller stores a pointer in the AR slot for each parameter if the actual parameter is a variable caller stores its address if the actual parameter is an expression, caller evaluates the expression, stores its results in its own AR, and stores a pointer to that results in the appropriate parameter slot in callee’s AR Call-by-value-result (copy-in/copy-out, copy/restore) (used in Fortran) –copy the values of formal parameters back to the actual parameters (except when the actuals are expressions) –arrays, structures are problem Call-by-name (used in Algol) –behaves as if the actual parameters are substituted in place of the formal parameters in the procedure text (like macro-expansion) –build and pass thunks (a function to compute a pointer for an argument)

19 Procedure Linkages How do procedure calls actually work? At compile time, caller may not know the callee’s code and visa versa –Different calls may be in different compilation units –Compiler may not know system code from user code –All calls must use the same protocol Compiler must use a standard sequence of operations Enforces control and data abstractions Divides responsibility between caller and callee Usually a system-wide agreement (for interoperability)

20 Procedure Linkages Standard procedure linkage procedure p prolog epilog pre-call post-return procedure q prolog epilog Procedure has standard prolog standard epilog Each call involves a pre-call sequence post-return sequence These operations are completely predictable from the call site  depend on the number and type of the actual parameters

21 Procedure Linkages Pre-call Sequence Sets up callee’s basic AR Helps preserve caller’s environment The details Allocate space for the callee’s AR Evaluates each parameter and stores value or address Saves return address into callee’s AR Saves caller’s ARP into callee’s AR (control link) If access links are used –Find appropriate lexical ancestor and copy into callee’s AR Save any caller-save registers –Save into space in caller’s AR Jump to address of callee’s prolog code

22 Procedure Linkages Post-return Sequence Finish restoring caller’s environment Place any value back where it belongs The details Copy return value from callee’s AR, if necessary Free the callee’s AR Restore any caller-save registers Restore any call-by-reference parameters to registers, if needed Continue execution after the call

23 Procedure Linkages Prolog Code Finish setting up the callee’s environment Preserve parts of the caller’s environment that will be disturbed The Details Preserve any callee-save registers If display is being used –Save display entry for current lexical level –Store current AR into display for current lexical level Allocate space for local data –Easiest scenario is to extend the AR Find any static data areas referenced in the callee (load the base address to a register) Handle any local variable initializations With heap allocated AR, may need to use a separate heap object for local variables

24 Procedure Linkages Eplilog Code Finish the business of the callee Start restoring the caller’s environment The Details Store return value? No, this happens on the return statement Restore callee-save registers Free space for local data, if necessary Load return address from AR Restore caller’s ARP Jump to the return address If AR s are stack allocated, this may not be necessary. (Caller can reset stacktop to its pre-call value.)