Subprograms - implementation - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Subprograms - implementation

Description:

put address of parent (BIGSUB) in static link field of subprogram (SUB1) ... use static depth of scope (procedure) of variable to find reference in display ... – PowerPoint PPT presentation

Number of Views:128
Avg rating:3.0/5.0
Slides: 31
Provided by: csLaur
Category:

less

Transcript and Presenter's Notes

Title: Subprograms - implementation


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 callers links to non-local variables
  • restore conditions in calling program
  • restart execution of calling program

4
General design
activation record
calling program
subprogram
return address parameters local variables ...
call sub
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
  • at call
  • save status of main
  • put parameters in activation record
  • save return address
  • start sub A ()

data for main
activation record for sub A
execution starts here
call A
code for main
  • at return
  • put parameters (and function value) in main
  • restore status of main
  • restart main at return address

call A

code for sub A
8
Sebestas 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

local variables
parameters
activation record
dynamic link (caller AR)
provided by caller
static link (scope)
return address in 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 depth
0
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
sub1
sub2
sub1
sub2
sub1
main
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 callers (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 displaya in
    activation record
  • put pointer to activation record of A in
    displaya

23
Non-local references by display (3)
  • to update display when terminating procedure A
  • restore contents of displaya 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)
SUB1
SUB2
SUB2
SUB4
SUB3
shallow binding for dynamic scope
SUB3
deep binding appropriate for static scope
SUB4
SUB1
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
  • solution caller (SUB3) passes pointer to parent
    (SUB3) with actual parameter (SUB2)

SUB3
SUB2
SUB4
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 -gt 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 x2, y3, z4
  • begin
  • call B
  • end
  • proc B
  • var x22, w55, t66
  • begin
  • call C
  • end
  • proc C
  • var x222, y333, w555
  • begin
  • call B
  • end
  • main
  • var r0,t0
  • begin
  • call A

while in C
r t w x y z
0
0
66
55
555
222
2
22
3
333
4
Write a Comment
User Comments (0)
About PowerShow.com