Semantics of Calls and Returns

Slides:



Advertisements
Similar presentations
Implementing Subprograms
Advertisements

(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
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.
Run time vs. Compile time
The Concept of Variables
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.
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
ISBN Chapter 10 Implementing Subprograms –Nested Subprograms –Blocks –Implementing Dynamic Scoping.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
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.
Runtime Environments Compiler Construction Chapter 7.
Compiler Construction
Chapter 9 Subprograms Fundamentals of Subprograms
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.
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.
April 23, ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)
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.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
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.
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
© G. Drew Kessler and William M. Pottenger1 Subroutines, Part 2 CSE 262, Spring 2003.
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
Chapter Ten: Implementing Subprograms Lesson 10. Implementing?  Previous lesson: parameter passing  In, out, inout  By value  By reference  Passing.
ISBN Chapter 10 Implementing Subprograms.
Chapter 10 Chapter 10 Implementing Subprograms. Implementing Subprograms  The subprogram call and return operations are together called subprogram linkage.
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.
Implementing Subprograms
Chapter 10 : Implementing Subprograms
Run-Time Environments Chapter 7
Implementing Subprograms Chapter 10
Implementing Subprograms
Implementing Subprograms
COMPILERS Activation Records
Implementing Subprograms
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Implementing Subprograms
Implementing Subprograms
Implementing Subprograms
Implementing Subprograms
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
UNIT V Run Time Environments.
Runtime Environments What is in the memory?.
Implementing Subprograms
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Implementing Subprograms
Chapter 10 Def: The subprogram call and return operations of
Presentation transcript:

CS2403 Programming Languages Implementing Subprograms Chung-Ta King Department of Computer Science National Tsing Hua University (Slides are adopted from Concepts of Programming Languages, R.W. Sebesta)

Semantics of Calls and Returns General semantics of subprogram calls Pass parameters Allocate storage of local variables and bind them Save the execution status of calling subprogram Transfer of control and arrange for the return General semantics of subprogram returns: Return values of out- and inout-mode parameters to the corresponding actual parameters Deallocate storage of local variables Restore the execution status Return control to the caller 1 1

Outline Semantics of Calls and Returns (Sec. 10.1) Implementing “Simple” Subprograms (Sec. 10.2) Implementing Subprograms with Stack-Dynamic Local Variables (Sec. 10.3) Nested Subprograms (Sec. 10.4) Blocks (Sec. 10.5) Implementing Dynamic Scoping (Sec. 10.6) 2 2

Implementing “Simple” Subprograms Subprograms cannot be nested All local variables are static Required storage for calls and returns: Status information of caller, parameters, return address, return value for functions A “simple subprogarm” consists of two parts: Subprogram code Non-code part (local variables and above data for calls and returns) 3 3

Implementing “Simple” Subprograms Format, or layout, of non-code part of an executing subprogram is called activation record (AR) For a “simple” subprogram, AR has fixed size, and can be statically allocated (not in stack) Can it support recursion? 4 4

Example Layout Code and activation records of a program with three “simple” subprograms: A, B, C These parts may be separately compiled and put together by linker 5 5

Outline Semantics of Calls and Returns (Sec. 10.1) Implementing “Simple” Subprograms (Sec. 10.2) Implementing Subprograms with Stack-Dynamic Local Variables (Sec. 10.3) Nested Subprograms (Sec. 10.4) Blocks (Sec. 10.5) Implementing Dynamic Scoping (Sec. 10.6) 6 6

Stack-Dynamic Local Variables Allocate local variables on the run-time stack Main advantage: support recursion Why? More complex storage management: Compiler must generate code for implicit allocation and deallocation of local variables on the stack Recursion adds possibility of multiple simultaneous activations of a subprogram  multiple instances of activation records 7 7

Contents of Activation Record What data are needed for the function to run? int AddTwo(int x, y) { int sum; sum = x + y; return sum; } Parameters: x, y Local variable: sum Return address Saved registers sum y x return addr “state” Size can be determined at compile time

Accessing Activation Record When AddTwo is called, its AR is dynamically created and pushed onto the run-time stack How to reference the variables in stack, i.e., x, y, sum? How about SP of caller? sum y x return addr AddTwo PROC mov eax,x add eax,y mov sum,eax ret AddTwo ENDP SP

Accessing Activation Record Idea: use addresses relative to a base address of AR, which does not change during subprog.  base pointer, frame pointer, or dynamic link Dedicate a register to hold this pointer  BP A subprog. can explicitly access stack parameters using constant offsets from BP, e.g. [BP + 8] BP is restored to its original value when subprog. returns BP sum y x return addr [BP+4] [BP+8] [BP+12] 10

Activation Record for Stack-Dyna Base pointer (BP): Always points at the base of the activation record instance of the currently executing program unit When a subprogram is called, the current BP is saved in the new AR instance and the BP is set to point at the base of the new AR instance Upon return from the subprogram, BP is restored from the AR instance of the callee 11 11

Activation Record Example (x86) .data ; Callee sum DWORD ? .code push 6 ; second argument push 5 ; first argument call AddTwo ; EAX = sum mov sum,eax ; save the sum Return value in eax AddTwo PROC push ebp mov ebp,esp .

Activation Record Example (x86) old EBP AddTwo PROC push ebp mov ebp,esp ; base of stack frame mov eax,[ebp + 12]; second argument (6) add eax,[ebp + 8] ; first argument (5) pop ebp ret 8 ; clean up the stack AddTwo ENDP ; EAX contains the sum 13

Activation Record: Local Array void sub(float total, int part) { int list[5]; float sum; … } 14 14

An Example without Recursion void A(int x) { int y; ... C(y); } void B(float r) { int s, t; A(s); void C(int q) { ... } void main() { float p; B(p); main calls B B calls A A calls C 15 15

An Example without Recursion 16 16

Dynamic Chain and Local Offset The collection of dynamic links in the stack at a given time is called the dynamic chain, or call chain Local variables can be accessed by their offset from the beginning of the activation record, whose address is in the BP. This offset is called the local_offset The local offset of a local variable can be determined by the compiler at compile time 17 17

An Example with Recursion int factorial (int n) { <-----------------------------1 if (n <= 1) return 1; else return (n * factorial(n - 1)); <-----------------------------2 } void main() { int value; value = factorial(3); <-----------------3 18 18

Stack at Position 1 in 3 Executions

Stack at Position 2

Outline Semantics of Calls and Returns (Sec. 10.1) Implementing “Simple” Subprograms (Sec. 10.2) Implementing Subprograms with Stack-Dynamic Local Variables (Sec. 10.3) Nested Subprograms (Sec. 10.4) Blocks (Sec. 10.5) Implementing Dynamic Scoping (Sec. 10.6) 21 21

Nested Subprograms Some languages (e.g., Fortran 95, Ada, Python, JavaScript, Ruby) use stack-dynamic local variables and allow subprograms to be nested procedure A is procedure B is procedure C is end; -- of C end; -- of B end; -- of A 22 22

Nested Subprograms How to access variables that are non-local but are defined in outer subprograms? These variables must reside in some AR instances deep in the stack The process of locating a non-local reference: Find the correct activation record instance down in the stack: hard Determine the correct offset within that activation record instance: easy 23 23

Finding Correct AR Instance Static scope semantics: Only variables that are declared in static ancestor scope are visible and can be accessed All non-local variables that can be referenced have been allocated in some AR instance on the stack when the reference is made Idea: chain AR instances of static ancestors 24 24

Static Chain Static link in an AR instance points to bottom of AR instance of the static parent Static chain connects all static ancestors of an executing subprogram, static parent first Can find correct AR instance following the chain But, can be even easier, because nesting of scopes is known at compile time and thus the length of static chain to follow 25 25

Following Static Chain Static_depth: depth of nesting of a static scope Chain_offset or nesting_depth of a nonlocal reference is the difference between static_depth of the reference and that of the declare scope A reference to a variable can be represented by: (chain_offset, local_offset), where local_offset is the offset in the activation record of the variable being referenced

Example Ada Program procedure Main_2 is X : Integer; procedure Bigsub is A, B, C : Integer; procedure Sub1 is A, D : Integer; begin -- of Sub1 A := B + C; <---------------1 end; -- of Sub1 procedure Sub2(X : Integer) is B, E : Integer; procedure Sub3 is C, E : Integer; begin -- of Sub3 Sub1; E := B + A: <---------2 end; -- of Sub3 begin -- of Sub2 Sub3; A := D + E; <-----------3 end; -- of Sub2 } begin -- of Bigsub Sub2(7); end; -- of Bigsub begin Bigsub; end; of Main_2 } 27 27

Stack Contents at Position 1 Main_2 calls Bigsub Bigsub calls Sub2 Sub2 calls Sub3 Sub3 calls Sub1 Reference to variable A: Position 1: (0,3) Position 2: (2,3) Position 3: (1,3) 28 28

Static Chain Maintenance At the call, AR instance must be built The dynamic link is just the old stack top pointer The static link must point to the most recent AR instance of the static parent Two methods: 1. Search the dynamic chain to find the parent scope 2. When compiler encounter a subprogram call, it finds its static parent and records the nesting_depth from that parent to itself. When that subprogram is called, its static link can be found starting from the caller’s static link and the number of nesting_depth 29 29

Evaluation of Static Chains Problems: A nonlocal reference is slow if the nesting depth is large Time-critical code is difficult: Costs of nonlocal references are difficult to determine Code changes can change the nesting depth, and therefore the cost 30 30

Displays An alternative to static chains that solves the problems with that approach Static links are stored in a single array called a display The contents of the display at any given time is a list of addresses of the accessible activation record instances 31 31

Outline Semantics of Calls and Returns (Sec. 10.1) Implementing “Simple” Subprograms (Sec. 10.2) Implementing Subprograms with Stack-Dynamic Local Variables (Sec. 10.3) Nested Subprograms (Sec. 10.4) Blocks (Sec. 10.5) Implementing Dynamic Scoping (Sec. 10.6) 32 32

Blocks User-specified local scopes for variables {int temp; temp = list [upper]; list [upper] = list [lower]; list [lower] = temp } The lifetime of temp in the above example begins when control enters the block An advantage of using a local variable like temp is that it cannot interfere with any other variable with the same name 33 33

Two Methods Implementing Blocks Treat blocks as parameter-less subprograms that are always called from the same location Every block has an activation record; an instance is created every time the block is executed Put locals of a block in the same AR of the containing subprogram Since the maximum storage required for a block can be statically determined, this amount of space can be allocated after the local variables in the activation record 34 34

Summary Subprogram linkage semantics requires many action by the implementation Stack-dynamic languages are more complex and often have two components Actual code Activation record: AR instances contain formal parameters and local variables among other things Static chains are main method of implementing accesses to non-local variables in static-scoped languages with nested subprograms 35 35