Implementing Subprograms - PowerPoint PPT Presentation

About This Presentation
Title:

Implementing Subprograms

Description:

... Nonlocal References. Nonlocal variable references occur in two steps: 1) find the activation record where the nonlocal ... Implementing Nonlocal References ... – PowerPoint PPT presentation

Number of Views:150
Avg rating:3.0/5.0
Slides: 31
Provided by: eckerdwor
Category:

less

Transcript and Presenter's Notes

Title: Implementing Subprograms


1
Implementing Subprograms
2
Subprogram linkage refers to the subprogram call
and return operations of a programming language.
3
  • The execution of a called subprogram
  • requires many associated actions.
  • parameter passing
  • storage allocation of local variables
  • saving execution status of calling program
  • transfer of control to the subprogram
  • return to calling program
  • access to non local variables

4
Subprogram Activation
The subprogram definition is written by the
programmer. The definition is a static entity.
During the execution of the program, if the
subprogram is called, a subprogram activation is
created.
The definition serves as a template for the
subprogram activation.
5
Subprogram Activation
A subprogram activation has a code segment and an
activation record.
The code segment is statically created and
invariant during execution. The code is shared
by all activations.
The activation record is dynamically created when
the subprogram is called. It is destroyed when
execution of the subprogram terminates.
6
Subprogram Activation
In programming languages which do not allow
recursion, there can be only one activation of a
given subprogram at any time.
FORTRAN 77 Activation Record
7
FORTRAN 77 Code and Activation Records
Common storage
Local variables
MAIN
Local variables
Data
A
Parameters
Return Address
Local variables
B
Parameters
Return Address
MAIN
A
Code
B
8
FORTRAN 77 Code and Activation Records
Common storage
Code
MAIN
Local variables
Code
Local variables
A
Parameters
Return Address
Code
Local variables
B
Parameters
Return Address
9
Implementing Subprograms in ALGOL-like Languages
In ALGOL-like programming languages (i.e.
Pascal, C) subprogram linkage is more complex
  • Different parameter passing methods
  • Local variables of subprograms are often
    dynamically allocated
  • Recursion makes multiple activations of a
    single subprogram possible
  • Implementation of static scoping

10
ALGOL-like Subprogram Activation Record
In programming languages which do allow
recursion, there can be many activations of a
given subprogram at any time.
11
Activation Record for Pascal Procedure
procedure sub(var total real part
integer) var list array 1..5 of integer
sum real begin end
12
Static and Dynamic Links
The static link points to the bottom of the
activation record instance of an activation of
the static parent. It is used for references to
non-local variables.
The dynamic link points to the top of the
activation record instance of the caller. It is
used for destruction of the current activation
record instance.
13
(No Transcript)
14
(No Transcript)
15
(No Transcript)
16
(No Transcript)
17
(No Transcript)
18
(No Transcript)
19
(No Transcript)
20
(No Transcript)
21
Implementing Nonlocal References
Nonlocal variable references occur in two
steps 1) find the activation record where the
nonlocal variable was allocated 2) use the
offset of the variable within that
activation record to access it
22
Implementing Nonlocal References
Static Chain
Display
23
Implementing Nonlocal References
Static Chain - a static chain is a chain of
static links that connects certain activation
record instances in the stack
24
(No Transcript)
25
main
A
C
B
Main calls B B calls A A calls C
26
Implementing Nonlocal References
Display - the static links are collected in a
single array called a display
27
Main - 0
A - 1
C - 2
B - 1
Main calls B B calls A A calls C
28
Main - 0
A - 1
C - 2
B - 1
ARI for B
1
ARI for MAIN
Main calls B B calls A A calls C
0
stack
display
29
Main - 0
A - 1
C - 2
B - 1
ARI for A
ARI for B
1
ARI for MAIN
Main calls B B calls A A calls C
0
stack
display
30
Main - 0
A - 1
C - 2
ARI for C
B - 1
ARI for A
ARI for B
2
1
ARI for MAIN
Main calls B B calls A A calls C
0
stack
display
Write a Comment
User Comments (0)
About PowerShow.com