Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 8-9b spring 2002CSCI337 Organization of Prog. Lang0 A Brief Recap Subprogram benefits, basic characteristics from names to their values  names can.

Similar presentations


Presentation on theme: "Week 8-9b spring 2002CSCI337 Organization of Prog. Lang0 A Brief Recap Subprogram benefits, basic characteristics from names to their values  names can."— Presentation transcript:

1 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang0 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

2 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang1 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

3 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang2 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 activation control 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

4 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang3 Activation Tree show the flow of control between activations root: main program edge/branch: call from one subprogram to another ordered from left to right according to temporal ordering leaves: subprogram that calls no other subprogram

5 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang4 Example: Activation Tree P is the main program P calls Q, so Q is P’s child Q is called by P before R is called by P, so Q is left of R P QR

6 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang5 Another example: recursive subprogram program activations; function pred(m: integer): integer; begin pred := m – 1 end; function f(n: integer): integer; begin if n =0 then f := 1 else f := n * f(pred(n)) end;

7 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang6 continue main program: f(3) f(3) f(2) f(1) f(0) pred(3) pred(2) pred(1)

8 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang7 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

9 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang8 Call Actions/Operations binding of actuals to formals allocation of local variable storage (for runtime variables) save any execution state of the calling program unit transfer control to the code in the subprogram enable access to non-local variables ensures that control can return to the proper place

10 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang9 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 state disable 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

11 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang10 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 allocation the 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.

12 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang11 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 parameters 5. storage for local variables 6. storage for temporary and final values Again, this is a general description of an AR stack frame, details differ from language to language, machine to machine.

13 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang12 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 scopes under dynamic scope control links are used to keep track of callers’ ARs under static scope access links are used to find the binding of non- local n in W

14 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang13 Control Link P calls Q, Q calls R R returns to Q, Q returns to P when a subprogram finished control passed back to caller the sequence of control links is called a dynamic chain. It connects all the current active subprograms P Q R top of stack control links frame pointer

15 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang14 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

16 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang15 How does it work? Q calls R: 1. save IP in Q’s AR 2. create R’s AR 3. creates control link in R’s AR 4. current AR (frame/stack pointer) is R’s AR Q Q R top of stack R: … Q: x := x+y R(x) y :=y+1 R: … Q: x := x+y R(x) y :=y+1

17 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang16 When R returns… 1. FP is set to Q’s AR using R’s control link 2. R’s AR is popped off the stack 3. the code pointed to by the IP in Q’s AR is executed

18 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang17 Variables Reference Again local variables: no problem, stored in the current frame referenced via offsets from the frame pointer within the current frame x y control link return address procedure P( … ) var x, y : integer; begin x := 5; y := 6; … end; set contents(FP-4) to 5 set contents(FP-8) to 6 memory layout 4 bytes

19 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang18 Parameters parameters are treated as local variables passing modes pass by value: copies written in the frame pass by reference: frame holds pointer to the actual

20 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang19 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

21 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang20 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?

22 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang21 Control Link Doesn’t Work Can R use control link to find Q’s AR? ‘No’ because the caller may not be the defining subprogram in this case Q calls P and P calls R but 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;

23 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang22 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 P The pointer is the access link For a non-local variable that was defined in the defining subprogram just follow the access link If the variable was non-local to the defining subprogram, just follow the access link!

24 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang23 Example Again procedure Q( … ) var x : integer; procedure R( … ) begin x := 7; end; procedure P( … ) var x : integer; begin …; R( … ); … end; begin …; P( … ); … end; control links Q P R top of stack

25 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang24 Chasing Links or Casting Nests? A sequence of access links is called a access chain every 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

26 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang25 Nesting Level the access nesting level ANL is the number of surrounding scopes of a definition program main(…); ANL0 var n: integer; procedure P (…); ANL1 procedure Q (…); ANL2 begin ( *Q* ) n := 9; end; ( *Q* ) ANL1 begin ( *P* ) n := 7; R(…); end; ( *P* ) ANL0 begin; n := 5; P(…); end

27 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang26 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 other generally we want access distance between a variable definition and a variable reference the 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

28 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang27 Computing Access Distance can be determined at compile time if access distance is 0, the variable is local, so access by offset into the current frame otherwise, follow the access chain for the corresponding number of links to find the frame and access by offset into this frame

29 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang28 Example P Q R top of stack control links … … procedure P (…) var x : integer; procedure Q (…) procedure R(…) …; x := 7;… x is 2 access links away

30 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang29 How to Create Access Links? When a subprogram is called 1. 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

31 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang30 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(…)

32 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang31 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

33 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang32 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

34 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang33 (2c) procedure A (…); procedure B (…); procedure C (…); procedure D (…); procedure E (…); …; B (…); … end;

35 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang34 How to (2d) (2d) otherwise P doesn’t know the name of Q and therefore cannot call it.

36 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang35 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 frame 4. save instruction pointer (return address) in the current frame 5. the new frame’s access link is set to the frame of defining subprogram 6. increment frame pointer (new frame becomes current) 7. enter the code of the called subprogram

37 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang36 What’s Going On When You Leave? 1. frame pointer is set to frame control link 2. pop top frame off the stack 3. enter the code pointed by IP of the current frame

38 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang37 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

39 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang38 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

40 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang39 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

41 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang40 Nesting Again: C vs Pascal in C nested subprogram declarations are not permitted use control links but access links are not required only 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 permitted both control and access links are required

42 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang41 Additional Topics: Optimization Tail-Recursion Elimination – applicable generally to reduce the number of activations, optimize for run time Displays – apply to statically scoped language that support nesting of subprogram declarations, e.g. Pascal, to optimize access to nonlocals, optimize for compile time

43 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang42 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.

44 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang43 Tail Elimination vs General Elimination tail recursive subprograms must be of in a special form, so not all recursive subprograms are tail recursive there are general techniques to convert any recursive subprograms into non-recursive ones, we’ll focus on tail elimination only elimination 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

45 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang44 Example Binary Search: in C the example focus on tail recursive calls that call the subprogram itself directly the basic idea: just use goto to repeatedly execute the body of the subprogram whenever the tail-recursive call is made

46 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang45 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

47 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang46 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 eliminated Cost: recursion simplifies the structure of many programs, wholesale elimination of recursion may make programs hard to read or understand compromise: only eliminate recursion in the most frequently executed portion of the program

48 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang47 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 costly solution (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

49 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang48 Display a display is a global array of pointers to stack frames D[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

50 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang49 Example procedure A(…) ANL=1 procedure B(…) ANL=2 procedure C (…)ANL=3 begin B(6); end; begin C(5); end; begin B(4); end; A C B B Display 1 2 3 4

51 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang50 In General When P calls Q at ANL= i: 1. P saves the value of D[i] in Q’s frame 2. P sets D[i] to Q’s frame on return from Q, restore D[i] from the saved value in Q’s frame

52 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang51 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.

53 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang52 The Solution the solution is to pass a closure for a subprogram argument a closure is made up of: 1. code (instruction pointer IP) 2. an environment (environment pointer EP) the environment pointer is here an access link

54 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang53 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;

55 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang54 What’s Going On? R passes a closure (for Parg) to Q; the closure contains: 1. a pointer to the code for Parg 2. 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

56 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang55 In Sum calling a subprogram received as an argument: 1. allocate new space for the new frame 2. new frame’s control link is set to frame pointer of the current frame 3. pass parameters into new frame 4. new frame’s access link is set to closure’s environment pointer 5. save current IP 6. increment frame pointer 7. execute the IP from the closure (which may extend the new frame)

57 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang56 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

58 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang57 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.

59 Week 8-9b spring 2002CSCI337 Organization of Prog. Lang58 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


Download ppt "Week 8-9b spring 2002CSCI337 Organization of Prog. Lang0 A Brief Recap Subprogram benefits, basic characteristics from names to their values  names can."

Similar presentations


Ads by Google