a brief recap

59
Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 1 A Brief Recap Subprogram benefits, basic characteristics from names to their values names can be occurrences in text scope issues: static vs dynamic subprogram calls parameters as basic mechanism for communicating runtime values to subprograms information exchange between caller and subprogram requires conventions or methods different methods: pass by value, pass by reference, pass by result, pass by value- result, and pass by name

Upload: juliet

Post on 21-Jan-2016

37 views

Category:

Documents


0 download

DESCRIPTION

A Brief Recap. Subprogram benefits, basic characteristics from names to their values names can be occurrences in text scope issues: static vs dynamic subprogram calls parameters as basic mechanism for communicating runtime values to subprograms - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 1

A Brief Recap

Subprogram benefits, basic characteristics from names to their values

names can be occurrences in text scope issues: static vs dynamic

subprogram calls parameters as basic mechanism for communicating

runtime values to subprograms information exchange between caller and subprogram

requires conventions or methods different methods: pass by value, pass by reference,

pass by result, pass by value-result, and pass by name

Page 2: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 2

Today: Continue the Question with how names get their values

in statically scoped language, binding of names to value are divided into 3 stages: compile time: have seen name as occurrences

in text governed by scope rules activation time: how a variable declaration can

be bound to different storage location each time a subprogram is used (today’s focus)

run time: binding locations to values as done dynamically by changing assignments

Page 3: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 3

Activation Records

Activation Record (AR) – storage associated with an activation of a subprogram for variables declared in the subprogram. Lifetime of a subprogram begins when control enters activation and ends when control returns from activationcontrol flows between activations in a LIFO manner: if P calls Q and then Q calls R, then R returns to Q before Q returns to P

Page 4: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 4

Activation Tree

show the flow of control between activationsroot: main programedge/branch: call from one subprogram to another ordered from left to right according to temporal orderingleaves: subprogram that calls no other subprogram

Page 5: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 5

Example: Activation Tree

P is the main programP calls Q, so Q is P’s childQ is called by P before R is called by P, so Q is left of R

P

Q R

Page 6: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 6

Another example: recursive subprogram

program activations;function pred(m: integer): integer;

beginpred := m – 1

end;function f(n: integer): integer;

beginif n =0 then f := 1 else f := n * f(pred(n))

end;

Page 7: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 7

continue

main program: f(3)

f(3)

f(2)

f(1)

f(0)

pred(3)

pred(2)

pred(1)

Page 8: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 8

Recursive vs Non-Recursive Subprograms

non-recursive subprogram can only have single activation at a given time, so only one AR exists per call – so easy to implement (e.g. Fortran)in recursive subprogram multiple activations are possible, so different ARs are required (e.g. C, Pascal) – more difficult to implement, management is required.management of different ARs involve linkages – call actions and return actions

Page 9: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 9

Call Actions/Operations

binding of actuals to formalsallocation of local variable storage (for runtime variables)save any execution state of the calling program unittransfer control to the code in the subprogramenable access to non-local variablesensures that control can return to the proper place

Page 10: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 10

Return Actions/Operations

provide calling program unit with return value (or values if using out mode)

move local values of formals to corresponding actuals

deallocate allocated local variable storage restore any saved execution statedisable access to non-local variables

return to the access configuration that was in place prior to the call

return control to the calling program unit

Page 11: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 11

Implementation of ARs

varies from different languages, traditionally ARs were implemented as stacks, e.g. Pascal and C, but other possibility has been used, e.g. Modula-3 uses heap allocationthe main difference: stack is LIFO (last in first out) – note that although control behave in a LIFO manner, this is insufficient to justify the use of stack. A main reason to favor stack is that storage for a variable declared within a subprogram should be local to the subprogram activation, I.e. the lifetime of the locals should coincide with the lifetime of the AR.

Page 12: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 12

Elements of an AR stack frame

1. pointer to stack of the caller (control/dynamic link)

2. return address (within caller)3. mechanism to find non-local variables

(access/static link)4. stroage for parameters5. storage for local variables6. storage for temporary and final valuesAgain, this is a general description of an AR stack

frame, details differ from language to language, machine to machine.

Page 13: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 13

Control – Access Links and Scopes

program L;var n : char;procedure W;begin writeln(n) end;procedure D;begin n := ‘D’; W end;begin { L }n := ‘L’; W; D end

have both dynamic and static reading of scopesunder dynamic scope control links are used to keep track of callers’ ARsunder static scope access links are used to find the binding of non-local n in W

Page 14: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 14

Control Link

P calls Q, Q calls RR returns to Q, Q returns to Pwhen a subprogram finished control passed back to callerthe sequence of control links is called a dynamic chain. It connects all the current active subprograms

P

Q

R

top of stack

controllinks

framepointer

Page 15: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 15

In addition

the AR must contain an instruction pointer (Program Counter)

points to the next instruction to be executed after exiting the subprogram

when Q calls R, the caller Q saves the next instruction into its AR

Page 16: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 16

How does it work?

Q calls R:

1. save IP in Q’s AR2. create R’s AR3. creates control link

in R’s AR4. current AR

(frame/stack pointer) is R’s AR

Q

Q

R

top of stack

R:…Q:x := x+yR(x)y :=y+1

R:…Q:x := x+yR(x)y :=y+1

Page 17: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 17

When R returns…

1. FP is set to Q’s AR using R’s control link2. R’s AR is popped off the stack3. the code pointed to by the IP in Q’s AR

is executed

Page 18: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 18

Variables Reference Again

local variables: no problem, stored in the current framereferenced via offsets from the frame pointer within the current frame

x

y

control link

return addressprocedure P( … )

var x, y : integer;begin

x := 5;y := 6;

…end;

set contents(FP-4) to 5set contents(FP-8) to 6

memory layout 4 bytes

Page 19: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 19

Parameters

parameters are treated as local variablespassing modes pass by value: copies written in the frame pass by reference: frame holds pointer to

the actual

Page 20: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 20

Pass by Reference Example

procedure R(var x : integer)var z : integer;

begin…end;

procedure Q( … )var y : integer;

begin…R(y); …end;

z

x

control link

return address

y

control link

return address

R

Q

Page 21: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 21

Non-Local Variables

control link

return address

x

control link

return address

R

Q

procedure Q( … )var x : integer;procedure R( … )begin

x := 7;end;

begin…R( … );…end;

How to access Q’s x from internal R?

Page 22: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 22

Control Link Doesn’t Work

Can R use control link to find Q’s AR? ‘No’ because the caller may not be the defining subprogramin this case Q calls P and P calls Rbut R needs Q’s AR

procedure Q( … )var x : integer;procedure R( … )begin

x := 7;end;

procedure P( … )var x : integer;begin…; R( … ); …end;

begin…; P( … ); …

end;

Page 23: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 23

Access Link for Non-Local Variables

The solution is for each frame for subprogram P to contain a pointer to the most recent frame of the subprogram that define PThe pointer is the access linkFor a non-local variable that was defined in the defining subprogram just follow the access linkIf the variable was non-local to the defining subprogram, just follow the access link!

Page 24: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 24

Example Again

procedure Q( … )var x : integer;procedure R( … )begin

x := 7;end;

procedure P( … )var x : integer;begin…; R( … ); …end;

begin…; P( … ); …

end;

controllinks

Q

P

R

top of stack

Page 25: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 25

Chasing Links or Casting Nests?

A sequence of access links is called a access chainevery non-local variable required by a subprogram call can be found in the access chain of the frame (the outer subprogram must be active)How to find the non-local variable? search through the access chain for the name

of the variable? No!!! Answer: need to compute access nesting level

Page 26: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 26

Nesting Level

the access nesting level ANL is the number of surrounding scopes of a definition

program main(…); ANL0var n: integer;procedure P (…); ANL1

procedure Q (…); ANL2

begin ( *Q* )n := 9;

end; ( *Q* ) ANL1begin ( *P* )

n := 7; R(…);end; ( *P* ) ANL0

begin;n := 5; P(…);

end

Page 27: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 27

Access Distance

access distance between two constructs is the difference of their nesting levels – the traversal between them, the number of links they have to pass through to reach each othergenerally we want access distance between a variable definition and a variable referencethe number of access links to follow to find the value of a non-local variable is the access distance between the definition and the reference

Page 28: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 28

Computing Access Distance

can be determined at compile timeif access distance is 0, the variable is local, so access by offset into the current frameotherwise, follow the access chain for the corresponding number of links to find the frame and access by offset into this frame

Page 29: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 29

Example

P

Q

R

top of stack

controllinks

procedure P (…)var x : integer; procedure Q (…) procedure R(…)

…; x := 7;…

x is 2 access links away

Page 30: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 30

How to Create Access Links?

When a subprogram is called1. the defining subprogram must be

active (its frame must be on the stack at least once)

2. the defining subprogram must be in the access chain of the calling subprogram

Page 31: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 31

How to (2a)

(2a) if the calling subprogram is the defining subprogram, then access link = control link. So the calling subprogram sets up the access link in the new frame

procedure foo(…); var n : integer; procedure bar (…);

begin … end;…

begin…; bar(…); …end;

foo(…) is both caller and definer, so foo(…) sets up the access links in the new frame for bar(…)

Page 32: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 32

How to (2b)

(2b) if the calling subprogram was defined in the same scope as the callee, then they have identical access links. So the caller copies its access link into the new frame

procedure foo(…); var n : integer;

procedure bar(…);begin … end;procedure gee(…);begin bar(…); end;

gee() called bar(), but they are in the same scope of foo(), so gee() copies its links into new frame

Page 33: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 33

How to (2c)

(2c) otherwise, if the calling subprogram P was defined inside of the called subprogram Q, the access link of P is followed to find the frame of the defining subprogram. So P sets Q’s access link as the current access link followed n times where n is the access distance between P and Q

Page 34: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 34

(2c)

procedure A (…);procedure B (…);

procedure C (…);procedure D (…);

procedure E (…);…; B (…); …

end;end;

end;end;

end;

Page 35: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 35

How to (2d)

(2d) otherwise P doesn’t know the name of Q and therefore cannot call it.

Page 36: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 36

So What’s Going On When You Call?

1. allocate space on top of the stack for a new frame

2. the new frame’s control link is set to current frame

3. pass parameters into new frame4. save instruction pointer (return address) in the

current frame5. the new frame’s access link is set to the frame of

defining subprogram6. increment frame pointer (new frame becomes

current)7. enter the code of the called subprogram

Page 37: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 37

What’s Going On When You Leave?

1. frame pointer is set to frame control link

2. pop top frame off the stack3. enter the code pointed by IP of the

current frame

Page 38: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 38

Implementing Parameter Passing 0

Pass by value copy variable values into proper stack

locations upon subprogram activation to access the values during execution,

indirect addressing is used (via offset from frame pointer)

Pass by result copy variable values to the stack locations in

the caller frame prior to returning

Pass by value result both of the above

Page 39: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 39

Implementing Parameter Passing 1

Pass by Reference copy variable addresses into proper stack locations

upon subprogram activation to access the values during execution, double indirect

addressing is used use an offset from the frame pointer to get address dereference the address to get the value pointed if used frequently, the complier copies the address into a

register so only indirect or direct access is used (this goes for any value used in the stack)

if an expression is sent in, the complier must generate code to evaluate the expression prior to activating the callee

Page 40: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 40

Implementing Parameter Passing 2

Pass by name parameters are implemented as parameter-

less thunks thunks are codes generated at the call site

by the compiler the codes are called each place where the

parameter is used it evaluates the reference given the current

environment

Page 41: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 41

Nesting Again: C vs Pascal

in C nested subprogram declarations are not permitteduse control links but access links are not requiredonly 2 places to look for a declaration of a name – either within the current subprogram or outside all subprograms

in Pascal nested subprogram declarations are permittedboth control and access links are required

Page 42: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 42

Additional Topics: Optimization

Tail-Recursion Elimination – applicable generally to reduce the number of activations, optimize for run timeDisplays – apply to statically scoped language that support nesting of subprogram declarations, e.g. Pascal, to optimize access to nonlocals, optimize for compile time

Page 43: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 43

Tail Recursive Subprogram

when the last statement executed in the body of a subprogram P is a recursive call, the call is said to be tail recursive.reminder: recursive here means either calling itself directly within its own body or indirectly through other subprograms which call the initial subprogram.a subprogram is a tail recursive, if all of its recursive calls are tail recursive.

Page 44: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 44

Tail Elimination vs General Elimination

tail recursive subprograms must be of in a special form, so not all recursive subprograms are tail recursivethere are general techniques to convert any recursive subprograms into non-recursive ones , we’ll focus on tail elimination onlyelimination of recursive calls does not compromise the expressive power of the program, Fortran for instance is non-recursive - has 1 AR frame per declaration, only one call to a given subprogram

Page 45: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 45

Example Binary Search: in C

the example focus on tail recursive calls that call the subprogram itself directlythe basic idea: just use goto to repeatedly execute the body of the subprogram whenever the tail-recursive call is made

Page 46: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 46

Example Binary Search: in C, continue

a tail-recursive call P(a, b) with formal x, y can be replace by

x = a; y = b;goto L;

where the label L is attached to the first executable statement in P

Page 47: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 47

Cost and Benefit of Recursive Calls Elimination

Benefit: in some languages, subprogram calls may be more costly then assignment statements, so a program may have faster run time by a large constant factor if recursive calls can be eliminatedCost: recursion simplifies the structure of many programs, wholesale elimination of recursion may make programs hard to read or understandcompromise: only eliminate recursion in the most frequently executed portion of the program

Page 48: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 48

Display: Optimizing Variable Access

problem: accessing non-locals variables (names) requires traversing links up the access link chain if a subprogram is at nesting depth n, it may have to follow n-1 access links to find variable addresses, this can be costlysolution (for static scope only): maintain a vector of current-active access-chain frames

called the display pioneered in Algol60 makes addresses directly accessible no need to traverse

Page 49: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 49

Display

a display is a global array of pointers to stack framesD[i] points to the most recent frame with ANL=i to access a variable defined at ANL=i: 1. D[i] gives the frame in which the variable is stored;2. performed an offset in this frame to access it

the size of the display is bounded by the maximum ANL (and small generally)note that a display must be maintained with run-time stacks

Page 50: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 50

Example

procedure A(…) ANL=1procedure B(…) ANL=2

procedure C (…)ANL=3

begin B(6); end;begin C(5); end;

begin B(4); end;

A

C

B

B

Display

1234

Page 51: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 51

In General

When P calls Q at ANL= i:1. P saves the value of D[i] in Q’s frame2. P sets D[i] to Q’s frame

on return from Q, restore D[i] from the saved value in Q’s frame

Page 52: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 52

Complication

in some languages, parameters can be subprograms:

procedure P(procedure Q(n: integer));begin … Q(…); … end;

the complier does not know which actual subprogram will be passed to P

there may be several distinct ones the defining environment of the actual subprogram

may not be in P’s access chain.

Page 53: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 53

The Solution

the solution is to pass a closure for a subprogram argumenta closure is made up of:

1. code (instruction pointer IP)2. an environment (environment pointer EP)

the environment pointer is here an access link

Page 54: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 54

Example

procedure P(…);var n : integer;procedure Q(procedure Pparam(…));

var x : integer;begin … Pparam(6); … end;procedure R(…);

var x : integer;procedure Parg(…);begin … x …; end;

begin … Q(Parg); … end;begin… R(…);…end;

Page 55: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 55

What’s Going On?

R passes a closure (for Parg) to Q; the closure contains:

1. a pointer to the code for Parg2. the access link for Parg (which points to R’s frame)

when Pparam(6) is called: a new frame is created the access link from the closure is put into the new

frame the code pointed to by the closure’s IP is executed

Page 56: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 56

In Sum

calling a subprogram received as an argument:1. allocate new space for the new frame2. new frame’s control link is set to frame pointer of the

current frame3. pass parameters into new frame4. new frame’s access link is set to closure’s

environment pointer5. save current IP6. increment frame pointer7. execute the IP from the closure (which may extend

the new frame)

Page 57: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 57

When P calls Q: the General Case

P saves the whole display in its frame if Q is not a parameter, P sets up Q’s display

just as it would set up Q’s access link in the access chain

if Q is a parameter, use the display pointed by Q’s environment pointer

when Q returns, P restores its own display

Page 58: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 58

A Quick Comparison 1

Access Chain: caller is defining, access link is just control

link caller and callee are defined in the same

scope, then access links are identical otherwise, go down (access distance)

access links essentially use linked list to access non-

locals, hence linear time access to non-locals.

Page 59: A Brief Recap

Week 8-9b spring 2002 CSCI337 Organization of Prog. Lang 59

A Quick Comparison 2

Display: caller is defining, display for callee is set to current

display, augmented by an element pointing to the callee

caller and callee are defined in the same scope, displays are identical, except the last element points now to callee

otherwise, remove elements from the display and push a last element pointing to the callee

when building a closure, build it with the current display where elements have been removed

essentially use an array to access non-locals, hence constant time access to non-locals