Presentation is loading. Please wait.

Presentation is loading. Please wait.

Subprograms - implementation

Similar presentations


Presentation on theme: "Subprograms - implementation"— Presentation transcript:

1 Subprograms - implementation

2 Calling a subprogram transferring control to a subprogram:
save conditions in calling program pass parameters allocate local variables establish links to non-local variables begin execution of subprogram

3 Returning from a subprogram
returning control to a calling program: pass back parameters and function value deallocate local variables restore caller’s links to non-local variables restore conditions in calling program restart execution of calling program

4 General design flow of control activation record calling program
subprogram return address parameters local variables ... call sub flow of control

5 Using a compiled subprogram
compiled code + activation record parameter space other local data space (function value) return address (in calling program) … more later

6 Simple Example - FORTRAN 77 (Sebesta)
linker puts executable program together: executable code for main program executable code for subprograms data space for main program data space for subprograms (activation records)

7 Simple Example - FORTRAN 77
linked program with one subprogram A data for main at call save status of main put parameters in activation record save ‘return address’ start sub A (*) activation record for sub A execution starts here call A code for main call A at return put parameters (and function value) in main restore status of main restart main at ‘return address’ * code for sub A

8 FORTRAN 77 is simple no nested subprograms no recursion
parameter passing by value - result static memory allocation in subprograms non-local references outside of call-return process (COMMON) example 1. set up memory main data sub activation record main code with line numbers sub code 2. e.g. main make arrays: sub to sort and return max as fn value MAIN x = array call maxx = sortmax(x) y = array call maxy = sortmax(y) SUB sort (arr)

9 FORTRAN 77 <--> Algol, imperative language
no nested subprograms no recursion parameters value-result static memory allocation non-local refs (COMMON) Algol nested recursion and reference dynamic SCOPING

10 Standard imperative language
activation record created dynamically in statically scoped language local variables parameters activation record dynamic link (caller AR) provided by caller static link (scope) return address in caller

11 p.445 void sub(float total, int part) { int list[5]; float sum; }

12 Standard imperative language
page example - no non-local scope and no recursion page example - recursion

13 p.446-7 1 2 3 void fun1(float r){ int s, t; … <------1 fun2(s); … }
… < fun2(s); } void fun2(int x){ int y; … < fun3(y); void fun3(int q){ … < void main(){ float p; fun1(p); } p.446-7 1 2 3

14 Standard imperative language
page example - no non-local scope and no recursion page example - recursion

15 p.448-50 int factorial;(int n){ <------1 if (n<=1) return 1;
else return < } void main(){ int value; value = fact(3); < p

16 ending recursive calls

17 Scoping – non-local references
all references are SOMEWHERE on the run-time stack find the proper activation record instance find the reference (offset) in the ARI two implementation strategies static chains displays (no longer popular)

18 Non-local references by static chaining
uses static link field in activation record static link must refer to static parent of subprogram (not to caller) nested static scopes are found by following the chain of static link fields static depth – depth of nesting of a procedure: main program == 0

19 Non-local references by static chaining: static_depth, nesting_depth
program main var x,y,w procedure sub1 var z,y procedure sub11 var z,w begin z = x + y * w end sub11() sub1() static depth 1 2 nesting depth, offset: z: 0, 3 x: 2, 3 y: 1, 4 w: 0, 4

20 Setting the static link at call time
how to find the activation record of the static parent? search along dynamic links (slow, especially with recursion) – depends on nesting of calls use static links (faster – depends on static nesting only) but HOW?

21 Setting the static link at call time: how to find it?
return address static link dynamic link parameters local variables program main var x,y,w procedure sub1 var y,z begin sub2() end procedure sub2 var x,z sub1() sub1 return address static link dynamic link parameters local variables sub2 return address static link dynamic link parameters local variables sub1 return address static link dynamic link parameters local variables sub2 return address static link dynamic link parameters local variables sub1 return address static link dynamic link parameters local variables main

22 Scope by static chaining
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; -- Sub1 procedure Sub2(X: Integer) is B,E: Integer; procedure Sub3 is C,E: Integer; begin -- Sub3 Sub1; E := B + A; <----2 end; -- Sub3 begin -- Sub2 Sub3; A := D + E; <----3 end; -- Sub2 begin -- Bigsub Sub2(7); end; -- Bigsub begin -- Main_2 BigSub; end; -- Main_2 Scope by static chaining page – Ada Main_2 Bigsub Sub1 Sub2 Sub3

23 Setting static link using caller static links
At call: SUB3 calls SUB1 static depth of caller SUB3 - 3 static depth of parent of SUB1 (BIGSUB) - 1 nesting depth (difference) 3-1 = 2 follow caller’s (SUB3) static chain 2 links to parent (BIGSUB) of called subprogram (SUB1) put address of parent (BIGSUB) in static link field of subprogram (SUB1)

24 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; -- Sub1 procedure Sub2(X: Integer) is B,E: Integer; procedure Sub3 is C,E: Integer; begin -- Sub3 Sub1; E := B + A; <----2 end; -- Sub3 begin -- Sub2 Sub3; A := D + E; <----3 end; -- Sub2 begin -- Bigsub Sub2(7); end; -- Bigsub begin -- Main_2 BigSub; end; -- Main_2

25 Blocks block scopes are like procedures but their order of activation is fixed at compile time consecutive blocks can share space in AR of procedure re-used identifier names distinguished by offset

26 Dynamic scoping ‘deep access’ - follow dynamic chain ‘shallow access’
can be slo-o-o-o-o-ow ‘shallow access’

27 Dynamic scoping ‘deep access’ ‘shallow access’ – local variables are not in activation records; each has its own stack

28 Shallow access by variable stacks
proc A var x=2, y=3, z=4 begin call B end proc B var x=22, w=55, t=66 call C proc C var x=222, y=333, w=555 main var r=0,t=0 call A while in C: r t w x y z 66 555 55 222 22 2 333 3 4


Download ppt "Subprograms - implementation"

Similar presentations


Ads by Google