Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction

Similar documents
Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access

Procedure and Function Calls, Part II. Comp 412 COMP 412 FALL Chapter 6 in EaC2e. target code. source code Front End Optimizer Back End

The Procedure Abstraction

Concepts Introduced in Chapter 7

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

CS415 Compilers. Procedure Abstractions. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Run-Time Environments

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

Optimizer. Defining and preserving the meaning of the program

Runtime Support for Algol-Like Languages Comp 412

Intermediate Representations & Symbol Tables

Memory Management and Run-Time Systems

Typical Runtime Layout. Tiger Runtime Environments. Example: Nested Functions. Activation Trees. code. Memory Layout

CS 314 Principles of Programming Languages

CSE 504: Compiler Design. Runtime Environments

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Run-time Environments

Run-time Environments

Programming Languages

12/4/18. Outline. Implementing Subprograms. Semantics of a subroutine call. Storage of Information. Semantics of a subroutine return

Naming in OOLs and Storage Layout Comp 412

Chapter 3:: Names, Scopes, and Bindings

Lecture 9: Procedures & Functions. CS 540 George Mason University

Chapter 3:: Names, Scopes, and Bindings (cont.)

Chapter 3:: Names, Scopes, and Bindings (cont.)

Chapter 9. Def: The subprogram call and return operations of a language are together called its subprogram linkage

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

Today's Topics. CISC 458 Winter J.R. Cordy

Implementing Subprograms

G Programming Languages - Fall 2012

Procedure and Object- Oriented Abstraction

Run Time Environments

Runtime Environments I. Basilio B. Fraguela

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

CS 314 Principles of Programming Languages. Lecture 13

Compilers. 8. Run-time Support. Laszlo Böszörmenyi Compilers Run-time - 1

Arrays and Functions

Run-time Environments - 2

Compiler Design. Spring Run-Time Environments. Sample Exercises and Solutions. Prof. Pedro C. Diniz

System Software Assignment 1 Runtime Support for Procedures

The basic operations defined on a symbol table include: free to remove all entries and free the storage of a symbol table

Chapter 10 Implementing Subprograms

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Control Abstraction. Hwansoo Han

Special Topics: Programming Languages

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

Run-time Environment

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

THEORY OF COMPILATION

Programming Languages

Chapter 10. Implementing Subprograms ISBN

Runtime management. CS Compiler Design. The procedure abstraction. The procedure abstraction. Runtime management. V.

Run-Time Environments

Chap. 8 :: Subroutines and Control Abstraction

Storage in Programs. largest. address. address

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

Assembly Language: Function Calls

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

The Procedure Abstraction Part I: Basics

Weeks 6&7: Procedures and Parameter Passing

Run-Time Environments

Assembly Language: Function Calls" Goals of this Lecture"

Topic 3-a. Calling Convention 2/29/2008 1

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Assembly Language: Function Calls" Goals of this Lecture"

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

Programming Languages: Lecture 12

CSCI Compiler Design

Qualifying Exam in Programming Languages and Compilers

G Programming Languages - Fall 2012

CA Compiler Construction

CSC 2400: Computing Systems. X86 Assembly: Function Calls

Chapter 9 :: Subroutines and Control Abstraction

CSC 2400: Computer Systems. Using the Stack for Function Calls

UNIT-4 (COMPILER DESIGN)

Chapter 8 :: Subroutines and Control Abstraction. Final Test. Final Test Review Tomorrow

Principles of Programming Languages

Names, Bindings, Scopes

Topic 7: Activation Records

Binding and Variables

The Procedure Abstraction Part I Basics

NOTE: Answer ANY FOUR of the following 6 sections:

Attributes of Variable. Lecture 13: Run-Time Storage Management. Lifetime. Value & Location

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

CS 406/534 Compiler Construction Intermediate Representation and Procedure Abstraction

Implementing Subroutines. Outline [1]

Run-Time Data Structures

Compiler Design. Code Shaping. Hwansoo Han

CSC 533: Organization of Programming Languages. Spring 2005

CS 480 Fall Runtime Environments. Mike Lam, Professor. (a.k.a. procedure calls and heap management)

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

Implementing Subprograms

CS 4100 Block Structured Languages. Fixed vs Variable. Activation Record. Activation Records. State of an Activation

Imperative Programming

Module 27 Switch-case statements and Run-time storage management

CSC 8400: Computer Systems. Using the Stack for Function Calls

CSE 3302 Notes 5: Memory Management

Compiler Theory. (Semantic Analysis and Run-Time Environments)

Transcription:

Procedure Abstraction Run Time Environment Records Procedure Linkage Name Translation and Variable Access Copyright 2010, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University of Southern California have explicit permission to make copies of these materials for their personal use. What is a Procedure? Basic Unit of Abstraction and Program Reasoning Why do We use Them? To allow us to build (very) large programs Conceptually allows us to abstracts from all the details How to Generate Code? Storage Allocation Scoping, i.e., what is visible and where? Control Transfer caller callee 1

Most Languages Allow Recursion Compilation Issues How to Generate Code Storage for Local Variables Transfer of Arguments and Return Results How to Execute a Procedure How to Access Local and Non-Local Variables How to Communicate between Caller and Callee How to Transfer Control between Caller and Callee The Role of the Symbol Table Keep track of where names are defined and declared Scope and Lifetime Run-Time Storage Organization Code Static data & Global data stack heap Classical Organization Code, Static, & Global data have known size Use symbolic labels in the code Heap & stack both grow & shrink over time Stack used for Records (AR) Heap for data (including AR) whose lifetime exts beyond activation. This is a virtual address space Record Basics register save area return address caller s local variables One AR for each invocation of a procedure Space for to the current routine Saved register contents If function, space for Address to resume caller Help with non-local access To restore caller s AR on a return Space for local values & variables (including spills) Records on the Stack What Happens on a Call? Passing of Arguments Transfer of Control What Happens on a Return? Recovery of Results (if any) Transfer of Control Need to Save/Restore Execution Context?, PC, Access Link, Register Values Stack register save area return address caller s local variables register save area return address callee s local variables Caller s Record Callee s Record Call Return Standard procedure linkage procedure p prolog pre-call post-return epilog procedure q prolog epilog Procedure has standard prolog standard epilog Each call involves a pre-call sequence post-return sequence These are completely predictable from the call site Þ dep on the number & type of the actual 2

Pre-call Sequence Sets up Callee s basic AR Helps preserve its own environment Allocate Space for the Callee s AR except space for local variables Evaluates each parameter & stores value or address Saves return address, caller s into callee s AR If s are used Find appropriate lexical ancestor & 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 Post-return Sequence Finish restoring caller s environment Place any value back where it belongs Copy from callee s AR, if necessary Free the callee s AR Restore any caller-save registers Restore any call-by-reference to registers, if needed Also copy back call-by-value/result Continue execution after the call Prolog Code Finish setting up the callee s environment Preserve parts of the caller s environment that will be disturbed Preserve any callee-save registers If display is being used Save display entry for current lexical level Store current into display for current lexical level Allocate space for local data Easiest scenario is to ext the AR Find any static data areas referenced in the callee Handle any local variable initializations With heap allocated AR, may need to use a separate heap object for local variables Epilog Code Wind up the business of the callee Start restoring the caller s environment If ARs are stack allocated, this may not be necessary. (Caller can reset stacktop to its precall value.) Store? No, this happens on the return statement Restore callee-save registers Free space for local data, if necessary (on the heap) Load return address from AR Restore caller s Jump to the return address Simplified Example Record Details 1. program (input, output); 3. x, i: integer; 6. begin a readarray ; 10. exchange ; 11. procedure quick(m, n : integer); 15. begin a 16. v 17. exchange(); 18. partition 19. begin quick 20. begin quick(1,9) quick(1,3) Where do activation records live? If lifetime of AR matches lifetime of invocation, AND S G If code normally executes a return C t l H o a & o e d t b a Keep ARs on a stack e i a p If a procedure can outlive its caller, OR Yes! This stack. If it can return an object that can reference its execution state ARs must be kept in the heap If a procedure makes no calls AR can be allocated statically Efficiency prefers static, stack, then heap c l S t a c k 3

Record Details How does the Compiler find the Variables? They are at known offsets from the AR pointer The static coordinate leads to a loadai operation Level specifies an, offset is the constant Variable-length data If AR can be exted, put it after local variables Leave a pointer at a known offset from Otherwise, put variable-length data on the heap Initializing local variables Must generate explicit code to store the values Among the procedure s first actions Storage for Blocks within a Single Procedure B0: int a, b, c B1: int v, b, x, w B2: int x, y, z. B3: int x, a, v Fixed length data can always be at a constant offset from the beginning of a procedure In our example, the a declared at level 0 will always be the first data element, stored at byte 0 in the fixed-length data area The x declared at level 1 will always be the sixth data item, stored at byte 20 in the fixed data area The x declared at level 2 will always be the eighth data item, stored at byte 28 in the fixed data area But what about the a declared in the second block at level 2? B0: int a, b assign value to a B1: int v(a), b, x B2: int x, y(8). Variable-length Data Arrays If size is fixed at compile time, store in fixedlength data area If size is variable, store descriptor in fixed length area, with pointer to variable length area Variable-length data area is assigned at the of the fixed length area for block in which it is allocated a b v b x x y(8) v(a) Translating Local Names How does the compiler represent a specific instance of x? Name is translated into a static coordinate < level,offset > pair level is lexical nesting level of the procedure offset is unique within that scope Subsequent code will use the static coordinate to generate addresses and references level is a function of the table in which x is found Stored in the entry for each x offset must be assigned and stored in the symbol table Assigned at compile time Known at compile time Used to generate code that executes at run-time Includes variable length data for all blocks in the procedure Variable-length data Scoping Rules Lexical Scoping Example Scoping 1. program (input, output); Call Tree Define which instance each name refers to 3. x, i: integer; Sort a[],x,i Lexical Scoping Look at the source text of the code Determine the closest (nesting structure) name Ex. FORTRAN, C, Pascal. 6. begin a readarray ; 10. exchange ; 11. procedure quick(m, n : integer); readarray partition quick m,n, quick m,n, Dynamic Scoping 15. begin a exchange partition y,z, Check at Run-Time the closest variable with the same name Ex. Scheme, Lisp, Miranda, etc. 16. v 17. exchange(); 18. partition 19. begin quick exchange 20. begin 4

Nested Procedures & Symbol Tables nil 1. program (input, output); var a array 44 integer 0 var x scalar 4 integer 44 3. x, i: integer; var i scalar 4 integer 48 6. begin a readarray ; readarray exchange quick var i scalar 4 integer 0 par i scalar 4 integer 0 par i scalar 4 integer 0 10. exchange ; par j scalar 4 integer 4 par j scalar 4 integer 4 11. procedure quick(m, n : integer); var k scalar 4 integer 8 var v scalar 4 integer 12 15. begin a partition 16. v 17. exchange(); par y scalar 4 integer 0 18. partition par z scalar 4 integer 4 19. begin quick var i scalar 4 integer 8 20. begin var j scalar 4 integer 12 subroutine A() integer i real a(100), b(10,10) do 100 i=1, 10 a(i*10) = b(i,10) 100: continue Code for A Code for B integer i real a(100), b(10,10) integer I, c(10) subroutine B() integer i, c(10) do 200 i=1, 10 c(i) = 0 200: continue Static Allocation record for A record for B Local variables are bound to fixed location in storage Values can be retained across procedure call (static) Save PC in AR but no need for stack Limitations: Fixed size variables only Does not support recursion No dynamic memory allocation Advantages: Simplified code generation Lexical Scopes Without Nested Procedures Lexical Scope With Nested Procedures 1. program (input, output); 3. x: integer; 6. begin a readarray ; 10. exchange ; 11. function partition(y, z : integer) : integer; 12. var i, j : integer; 13. begin a 14. exchange(); 15. partition 16. procedure quick(m, n : integer); 17. var k, v: integer; 1 quick 19. begin Easy location of variables Either local, i.e., in the AR Global, i.e. at specified global offset Why Do with Need a Stack? 1. program (input, output); 3. x: integer; 6. begin a readarray ; 10. exchange ; 11. procedure quick(m, n : integer); 15. begin a 16. v 17. exchange(); 18. partition 19. begin quick 20. begin Problem! Now quick might have to access a, x at Also, partition needs to access k, v at quick But which one? Need to keep Track of Depth (static) Nesting Depth at depth 1 readarray, quick at depth 2 partition at depth 3 Implementation Link Chasing in AR Reflect Nesting Structure During Calls Display indexed by depth Records on the Stack Lexical Scoping Example 1. program (input, output); quick(1,9) quick(1,9) quick(1,3) quick(1,9) quick(1,3) quick(1,9) quick(1,3) exchange(1,3) 3. var x, i: integer; 6. begin a readarray ; 10. exchange ; 11. procedure quick(m, n : integer); 15. begin a 16. v 17. exchange(); 18. partition 19. begin quick 20. begin 5

Access Links and How to Use Them Suppose Procedure p at lexical nesting depth n p refers to non-local variable a at depth n q <= n p, them a can be found: 1. Follow n p - n q s from AR of p 2. Access the variable at offset a in current AR Example: partition code at depth = 3 refers to v and a at depth 2 and 1 for which the code should traverse 1 and 2 s respectively. Since (n p - n q ) can be computed at compile-time this procedure is always feasible. What happens if n q > n p? Access Links and How to Use Them Suppose Procedure p at lexical nesting depth n p refers to non-local variable a at depth n q <= n p, them a can be found: 1. Follow n p - n q s from AR of p 2. Access the variable at offset a in current AR Example: partition code at depth = 3 refers to v and a at depth 2 and 1 for which the code should traverse 1 and 2 s respectively. Since (n p - n q ) can be computed at compile-time this procedure is always feasible. What happens if n q > n p? Variable a is not visible! The compiler will never observe this situation. How to Set Up Access Links? Lexical Scope with Nested Procedures Procedure p at depth n p calls q at depth n q Code generated as part of the calling sequence: Case n p < n q : procedure q is nested more deeply than p; it must be declared within p, i.e. n q = n p + 1; Why? Copy the pointer of the caller s to the callee s Case n p >= n q : all the ARs of the procedures up to p are the same, simply need to access the link of the most recent invocation of p; Follow n q - n p +1 s you reach the correct AR of procedure r that encloses p to set the in the AR of q. quick(1,9) quick(1,9) quick(1,3) quick(1,9) quick(1,3) quick(1,9) quick(1,3) exchange(1,3) calls quick quick calls quick quick calls partition partition calls exchange n q = n p + 1 n q = n p n q = n p + 1 n p = n q + 1 Display Display: Example Following Access Links can take a long Time Solution? Keep an auxiliary array of pointers to AR on the stack Storage for a non-local at depth i is in the activation record pointed to by d[i] called display. Faster because you need to follow a single pointer How to Maintain the display? When AR of procedure at depth i is set up: Save the value of d[i] in the new AR Set d[i] to point to the new AR. Just before an activation s, d[i] is reset to the saved value Values in saved at a specific offset on the AR like and return d[1] d[2] d[3] quick(1,9) saved d[2] quick(1,3) saved d[2] saved d[3] exchange(1,3) saved d[2] P at depth n p calls Q at depth n q Case n p < n q : then n q = n p + 1 First n p elements of the display do not change; just set d[n q ] to new AR. Case n p >= n q : Enclosing procedures at levels 1,, n q -1 are the same; save old d[n q ] in new AR and make d[n q ] point to new AR. 6

Communicating Between Procedures Most languages provide a parameter passing mechanism Expression used at call site becomes variable in callee Two common binding mechanisms Call-by-reference passes a pointer to actual parameter Requires slot in the AR (for address of parameter) Multiple names with the same address? Call-by-value passes a copy of its value at time of call Requires slot in the AR Each name gets a unique location (may have same value) Arrays are mostly passed by reference, not value Can always use global variables Complications Passing Functions as Arguments? Just an address of the code, not that hard to implement Need to verify at run-time the number of arguments. Access Link needs to be passed to understand how to follow it What if AR outlives Execution of Procedure? When is this possible? What to do? Dynamically Linked Libraries What are the issues with regular libraries? Position-Indepent Code Why is this important? Code Sharing Traditionally Link all Libraries with your code Drawbacks: Space as each executable includes the code of all libraries it uses (big as every function needs to be included at link time) Bugs in libraries require recompilation and linking Solution: Dynamically Linked Libraries Loaded and linked on-demand during execution Advantages: Single Copy in the system rather than replicated. Executable has only what is really needs. Bugs can be fixed later not requiring re-linking Shared Libraries Make it Look Like a Statically Linked Linking? Name Resolution: finding bindings for symbols Determine before hand if linking will succeed Check for undefined or multiply defined symbols Create a table of symbols for each shared library Pre-execution linking checks the tables Run-time dynamic linker is guaranteed to fail if and only if the preexecution static linker would. Summary What Have We Learned? AR is a run-time structure to hold state regarding the execution of a procedure AR can be allocated in Static, Stack or even Heap Links to allow Call-Return and Access to Non-local Variables Symbol-table play important role Linkage Conventions Saving Context before call and restoring after the call Need to understand how to generate code for body 7