Presentation is loading. Please wait.

Presentation is loading. Please wait.

Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.

Similar presentations


Presentation on theme: "Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate."— 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 calling program call sub subprogram return address parameters local variables... activation record

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 (Sebesta)  linked program with one subprogram A data for main activation record for sub A code for main code for sub A execution starts here call A at call  save status of main  put parameters in activation record  save ‘return address’  start sub A (*) at return  put parameters (and function value) in main  restore status of main  restart main at ‘return address’ *

8 Sebesta’s first simple example: FORTRAN 77  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)

9 FORTRAN 77 to standard imperative language (Algol)  FORTRAN 77 Algol no nested subprograms nested no recursion recursion parameter passing by value - result and by reference static memory allocation dynamic non-local references (COMMON) SCOPING

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

11 Standard imperative language  page 406-407 example - no non-local scope and no recursion  page 408-410 example - recursion

12 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

13 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

14 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 begin sub11() end begin sub1() end static depth0 1 2 nesting depth, offset: z: 0, 3 x: 2, 3 y: 1, 4 w: 0, 4

15 Scope by static chaining  page 412-414 – Pascal example with static link chains MAIN_2 BIGSUB SUB_1 SUB_2 SUB_3

16 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?

17 Setting the static link at call time: illustration program main var x,y,w procedure sub1 var y,z begin sub2() end procedure sub2 var x,z begin sub1() end begin sub1() end return address static link dynamic link parameters local variables return address static link dynamic link parameters local variables return address static link dynamic link parameters local variables return address static link dynamic link parameters local variables return address static link dynamic link parameters local variables return address static link dynamic link parameters local variables main sub1 sub2 sub1 sub2 sub1

18 Setting static link using caller static links, p. 412-414  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)

19 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

20 Non-local references by display  uses a display -array of references to activation records (no static links)  gives constant time access to non- local identifiers even with deeply nested subprogram scopes  static depth is index into display array

21 Non-local references by display (1)  to access non-local variable use static depth of scope (procedure) of variable to find reference in display follow reference to activation record of procedure use offset to find variable

22 Non-local references by display (2)  to update display when calling procedure A (static depth a)  make activation record for A on stack  store current contents of display[a] in activation record  put pointer to activation record of A in display[a]

23 Non-local references by display (3)  to update display when terminating procedure A  restore contents of display[a] as saved in activation record  remove activation record of A

24 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

25 Procedure Parameters – binding to an environment (eg p.379) return address static link dynamic link parameters local variables return address static link dynamic link parameters local variables return address static link dynamic link parameters local variables return address static link dynamic link parameters local variables SUB1 SUB3 SUB4 SUB2 SUB1 SUB2 SUB3 SUB4 shallow binding – for dynamic scope deep binding – appropriate for static scope

26 Procedure Parameters – binding to an environment  problem for deep binding: receiving procedure (SUB4) may not be in path of parent (SUB3) of actual parameter procedure (SUB2) SUB1 SUB3 SUB4 SUB2  solution: caller (SUB3) passes pointer to parent (SUB3) with actual parameter (SUB2)

27 Procedure parameters  with static chaining – only need reference to parent of actual parameter to set static link  with displays – may force more than one change to display array -> activation record must save multiple changes (or save entire display)

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

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

30 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 begin call C end proc C var x=222, y=333, w=555 begin call B end main var r=0,t=0 begin call A end rtwxyzrtwxyz while in C: 222 22 2 66 0 555 55 333 3 4 0


Download ppt "Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate."

Similar presentations


Ads by Google