(1) implementing subprograms acknowledgement: mehdi emadi dr. abhik roychoudhury and …

52
1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

Upload: lambert-freeman

Post on 18-Dec-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(1)

Implementing Subprograms

Acknowledgement:

Mehdi Emadi

Dr. Abhik Roychoudhury

And …

Page 2: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(2)

Objectives To understand the implementation of subprogram calls•basic procedure of subprogram linkage

•activation records•static and dynamic chaining

Page 3: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(3)

General issues When calling: •Save state of calling procedure •Determine parameter-passing mechanism for each parameter and arrange access appropriately

•Allocate storage for local variables•Initialize local variable values•Arrange transfer of control to procedure

•Arrange return of control to calling procedure

•Arrange access to visible non-local variables

Page 4: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(4)

General issues (cont.) When Returning:•Move final parameter values back to calling arguments (when pass by result)

•Deallocate storage used for local variables

•Undo access to visible non-local variables

•Return control to calling procedure

Page 5: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(5)

Activation Records Programs consist of Code, Data, and book-keeping

Noncode part is organized in an Activation Record ( Code does not change by each call)

Activation record instances can include•Parameter definitions•Local variables•Functional value•Return address•Other data to be discussed

Page 6: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(6)

Fortran 77 A simple situation: •Actual code is fixed at compile time•Local variables/data are also of fixed size

•Subprograms cannot be recursive A simple solution: •Local variables statically allocated (static activation records)

•All referencing of non-local variables is through COMMON: a globally accessible static memory space

Page 7: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(7)

Fortran 77 Procedure call:•Save the execution status of current procedure

•Carry out parameter-passing•Pass return address to callee•Transfer control to callee

Page 8: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(8)

Fortran 77 (cont.) Procedure return:•If using pass-by-value-result, final values of parameters are moved to arguments

•If subprogram is a function, final value is moved back

•Execution status of the caller is restored

•Control is transferred back to caller

Page 9: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(9)

Fortran77 Activation Records

No more than one can exist for a procedure

AR Instances can be statically allocated Either separate or interleave code and data

Code

Return address

Local variables

Code

Main

Procedure A

Parameters

Local variables

Common

Page 10: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(10)

Algol-like Languages New complexities over Fortran 77•Parameters can be passed in different ways (value or reference)

•Subprogram variables may be dynamically allocated

•Recursion legal: multiple activation records may exist simultaneously

•Scope rules (usually static) determine visibility of non-local variables

Page 11: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(11)

Algol-like Languages Activation records•Format and size is known at compile time since all local variables and their size is statically fixed

•Instances of activation records are dynamically generated at run-time

Page 12: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(12)

Contents of Activation Records

Return address•Pointer to code segment of caller and offset address of instruction

Static link:•A pointer to the activation record of static parent

•Used to support non-local variable access Dynamic link:

•A pointer to activation record of caller•Used in static scoped languages to support procedure return (indicates how much of stack to remove)

•Used in dynamic scoped languages for non-local variable access

Page 13: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(13)

Activation Records (continued)

Parameters:• Either values or addresses provided by caller

Local variables:•Scalars stored directly within activation record

•Non-scalars (structures) stored elsewhere•References to locals can be via offset from beginning of record: known at compile time

Functional Value (if a function)•Place to put the returned value of the function

•(In some languages the function name can be treated like a local variable)

Page 14: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(14)

procedure sub (var tot: real); var sum : real; begin : end;

A new activation record is created each time sub is called

Run-time Stack holds activation records •Stack discipline is appropriate for conventional procedures

•But not for co-routines

Activation Record Example

Return Address

Local

Parameter

Dynamic Link

Static Link

sumtot

Start of record

Offset of sum}

Page 15: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(15)

Factorial Example

Return (main)

Functional: ? Parameter n=3 Dynamic Link

Static Link

Local value = ?

Return (main)

Functional: ? Parameter n=3 Dynamic Link

Static Link

Local value = ?

Return (fact)

Functional: ? Parameter n=2 Dynamic Link

Static Link

Return (fact)

Functional: ? Parameter n=1 Dynamic Link

Static Link

Return (main)

Functional: ? Parameter n=3 Dynamic Link

Static Link

Local value = ?

Return (fact)

Functional: ? Parameter n=2 Dynamic Link

Static Link

Page 16: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(16)

Factorial Example

Return (main)

Functional: 6 Parameter n=3 Dynamic Link

Static Link

Local value = ?Return (main)

Functional: ? Parameter n=3 Dynamic Link

Static Link

Local value = ?

Return (fact)

Functional: 2 Parameter n=2 Dynamic Link

Static Link

Return (fact)

Functional: 1 Parameter n=1 Dynamic Link

Static Link

Return (main)

Functional: ? Parameter n=3 Dynamic Link

Static Link

Local value = ?

Return (fact)

Functional: ? Parameter n=2 Dynamic Link

Static Link

Page 17: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(17)

Non-local references Reference to a non-local variable requires a two step process:•Find the activation record instance where the variable was allocated

•Use local offset to access the variable within the record

Finding the ARI (under static scoping) is not straightforward

Page 18: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(18)

Non-local references program Prog; int i=0; procedure A; int i=1; B(); end; procedure B; int i=2; C(); end; procedure C; i=3; end; A(); end;

Show the set of activation records in existence when the statement ‘i=3’ is encountered.

Which declaration of ‘i’ will be updated?

Following the dynamic chain will find the wrong ‘i’!

Page 19: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(19)

Static Chains A link from each procedure’s activation record to its parent’s activation record with respect to static scope

The static chain is different from the dynamic chain:•The static parent of C is Prog.•The dynamic parent of C is B.

Page 20: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(20)

Static Chains (continued) No need to search for variable: number of static links is known at compile time (why?) •Static Depth: nesting level of scope•Chain Offset (Nesting Depth): difference between static depth of reference environment and static depth of declaration environment

•References represented as (chain_offset, local_offset)

•Locals can be handled with chain_offset=0

Page 21: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(21)

Maintaining Static Chains At subprogram call: •Correct parent known at compile time•Must find most recent activation record of parent

•Avoid search of dynamic chain by using existing static chain to find parent (treat procedure reference like variable

At return: just remove record from stack

Page 22: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(22)

Cost of Static Chains Static chains allow references to non-local variables to be resolved, but at a cost:•References to non-local variables “above” parent are slow

•Time to resolve a reference is difficult to predict, and sensitive to source code changes

Page 23: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(23)

Summary: MacLennan terminology

State of a procedure:

1. A fixed program part

2. A variable activation part1.The ep (environment part), the

context to be used for this activation of the procedure, local and nonlocal context

2.The ip (instruction part), the current instruction being (or to be) executed in this activation of the procedure

Page 24: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(24)

Review of scope rules

Q and T are declarations of procedures within P, so scope of names Q and T is same as scope of declaration a.

R and S are declarations of procedures in Q. U is a declaration of a procedure in T. Storage managed by adding activation records, when

procedure invoked, on a stack.

Page 25: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(25)

Activation record stack

Page 26: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(26)

Scope rules Scope rules: The scope of a variable are the set of statements where the variable may be accessed (i.e., named) in a program.

Static scope: Scope is dependent on the syntax of the program.

Dynamic scope: Scope is determined by the execution of the program.

Page 27: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(27)

Scope rules

Static nested scope: A variable is accessible in the procedure it is declared in, and all procedures internal to that procedure, except a new declaration of that variable name in an internal procedure will eliminate the new variable's scope from the scope of the outer variable.

A variable declared in a procedure is local in that procedure; otherwise it is global.

Page 28: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(28)

Activation record stack

Page 29: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(29)

Activation record stack

Problem is: How to manage this execution stack? Two pointers perform this function: 1. Dynamic link pointer points to activation record

that called (invoked) the new activation record. It is used for returning from the procedure to the calling procedure.

2. Static link pointer points to the activation record that is global to the current activation record (i.e., points to the activation record of the procedure containing the declaration of this procedure).

Page 30: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

Program a(…); var N: integer; procedure b(sum: real); var i: integer; avg: real; Data: array[1..10] of real; procedure c(val: real); begin … writeln(Data[i]); end {c}; begin … end {b}; begin … end {a}

Page 31: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(31)

Accessing a variable1. At run-time skip down the static chain

(how many skips? Static distance) to get to the activation record of the variable.

2. The address of the variable is obtained by adding the fixed offset of the variable to the address obtained in step 1.

Page 32: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(32)

Static distance(static nesting level:snl) and offset of the variable are computed by the compiler at compile time.

Symbol table ----------------------------------------- Name Type Snl Offset -----------------------------------------

N int 1 1 Sum real 2 1 i int 2 2 Avg real 2 3 Data real array 2 4 Val real 3 1

Page 33: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(33)

Implementation of subprogram storage: more

examples Each subprogram has a block of storage containing such information, called an activation record.

Consider the following C subprogram: float FN( float X, int Y) const initval=2; #define finalval 10 float M(10); int N; N = initval; if(N<finalval){ ... } return (20 * X + M(N)); }

Information about procedure FN is contained in its activation record.

Page 34: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(34)

Activation records

Page 35: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(35)

Dynamic nature of activation records

Each invocation of FN causes a new activation record to be created.

Thus the static code generated by the compiler for FN will be associated with a new activation record, each time FN is called.

A stack structure is used for activation record storage.

Page 36: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(36)

Dynamic nature of activation records

Page 37: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(37)

Activation record structure

Page 38: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(38)

Example of act. record stack

Declarations

Var A in P

B in Q

C in R

Page 39: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(39)

Activation record example 1

Ex 1. In R: C := B+A; C local, A,B global For each variable, get pointer to proper activation

record. Assume AR is current act.record pointer (R). 1. B is one level back:• Follow AR.SL to get AR containing B.

• Get R-value of B from fixed offset L-value

2. A is two levels back:• Follow (AR.SL).SL to get activation record containing A.

• Add R-value of A from fixed offset L-value

3. C is local. AR points to correct act record.• Store sum of B+A into L-value of C

Page 40: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(40)

Activation record example 2

Example 2. Execution in procedure Q: A := B B is local, A global Assume AR is current activation record pointer (Q)

1. B is now local. AR points to activation record• Get R-value from local activation record

Page 41: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(41)

Activation record example 2

2. A is now one level back AR.SL is activation record of P

• Store R-value of B into L-value of A

Compiler knows static structure, so it can generate the number of static link chains it has to access in order to access the correct activation record containing the data object. This is a compile time, not a runtime calculation.

Page 42: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(42)

Setting the static link …

Page 43: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(43)

Displays: an alternativeStatic links are in a separate array

•One element per static depth•Exactly two steps per reference: (display_offset, local_offset)

•When a subprogram at a given static depth is called, save and replace the link at that depth

Efficiency compared to Static Chains:•Deeply nested references more efficient•Maintenance of subprogram calls less efficient

Yet most programs are not deeply nested (three in practice)

Page 44: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(44)

Display Example

Static Depth 0

Display

Static Depth 3 Static Depth 2 Static Depth 1

Sub1

Sub2

MAIN

Static Depth 0

Display

Static Depth 3 Static Depth 2 Static Depth 1

Sub1

Sub5

Sub4

Sub3

Sub2

MAIN

Page 45: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(45)

Dynamic Scoping Deep Access (NOT same as deep binding): •References to non-local variables are be resolved by following dynamic links

•Easy to implement •Search down call chain may be slow•Activation records must store variable names

Page 46: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(46)

Shallow Access(shallow binding method)

•Like FORTRAN, each procedure is statically allocated one copy of its activation record.

•Containing the most recent activation of that procedure.

•Variable access is efficient.•In the case of recursive cal, push the activation record on the stack.

•Activation records are allocated statically, their size is fixed (=> cannot be used for languages with dynamic arrays …)

Page 47: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(47)

Implementing Subprogram as Parameters

Two major problems:1. Static type checking

Full specification for formal parameter shall be present: type of the function, the number, order, and type of each parameter.

2. Nonlocal references (free variables) what is the nonlocal environment

used when a function is invoked?

Page 48: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(48)

Program Main;var X: integer;Procedure Q(var I:integer;function R(J:integer):integer); var X: integer; begin X:=4; I:=R(I); end;Procedure P; var I: integer; function FN(K:integer):integer; begin X:=X+K; FN:=I+K; end; begin I:=2; Q(X,FN); end;begin X:=7; P;End.

Page 49: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(49)

What should be the nonlocal environment used when FN is invoked within Q (Formal parameter R in Q)?

1. The nonlocal environment is the same as the one that would be used if the call R(I,X) were simply replaced by the call FN(I,X) in subprogram Q.

2. The nonlocal environment is the same as the one that would be used if the subprogram FN were invoked at the point where it appears as an actual parameter in the parameter list (in calling Q(X,FN)).

Page 50: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(50)

Procedural parameters are represented by closures.

1. The ip field: entry address of the actual parameter

2. The ep field: a pointer to the environment of definition of the actual procedure

Page 51: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(51)

Statement labels as parameters

Which activation should be used?•jump to the proper label in proper activation

How to implement it?•Pop all the activations to get to the right place

Page 52: (1) Implementing Subprograms Acknowledgement: Mehdi Emadi Dr. Abhik Roychoudhury And …

(52)

Nonlocal GOTOs Nonlocal gotos require environment restoration.