Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
CSCI524
Principles of Programming Languages
Chapter 9
Prof. Ellick Chen
University of Northern Virginia
UNVA – CSCI524
9.2 Implementing FORTRAN 77 Subprograms
2
UNVA – CSCI524
9.2 Implementing FORTRAN 77 Subprograms
3
UNVA – CSCI524
9.2 Implementing FORTRAN 77 Subprograms
• The call and return actions require storage for the following:
– Status information about the caller
– Parameters
– Return address
– Functional value for function subprograms
• A FORTRAN 77 subprogram consists of two separate parts:
– The actual code of the subprogram, which is constant, and
– The local variables and data, which can change when the
subprogram is executed.
• The format of the non-code part of a subprogram is called an
activation record, because the data it describes are only
relevant during the activation of the subprogram.
4
UNVA – CSCI524
9.2 Implementing FORTRAN 77 Subprograms
Local variables
Parameters
Return address
5
UNVA – CSCI524
9.2 Implementing FORTRAN 77 Subprograms
Common storage
FORTRAN 77 B Parameters
Return address
program. Local variables
Parameters
C
Return address
MAIN
A
Code
B
6
UNVA – CSCI524
9.2 Implementing FORTRAN 77 Subprograms
7
UNVA – CSCI524
9.3 Implementing Subprograms in ALGOL-Like Languages
8
UNVA – CSCI524
9.3.1 More Complex Activation Records
9
UNVA – CSCI524
9.3.1 More Complex Activation Records
10
UNVA – CSCI524
9.3.1 More Complex Activation Records
Local (sum)
procedure sub(var total: real ; part: integer) ;
Local (list [5])
var list : array [1..5] of integer ; Local (list [4])
Return address
11
UNVA – CSCI524
9.3.2 An Example Without Recursion and Non-local References
12
UNVA – CSCI524
Fig 9.5 Stack contents for three points in the 3
Example
top Parameter Q
Dynamic link
ARI
13
UNVA – CSCI524
9.3.2 An Example Without Recursion and Non-local References
14
UNVA – CSCI524
9.3.4 Mechanisms for Implementing Non-local References
15
UNVA – CSCI524
9.3.4.1 Static Chains - I
16
UNVA – CSCI524
9.3.4.1 Static Chains - II
17
UNVA – CSCI524
9.3.4.1 Static Chains - III
Length of the static chain needed = Static depth of the procedure containing the reference to x –
Static depth of the procedure containing the declaration for x
18
UNVA – CSCI524
9.3.4.1 Static Chains – Example on p390, p391, & p392
static depth
program MAIN_2 ;
0 procedure SUB2 ( X : integer ) ;
2
var X : integer ; var B, E : integer ;
procedure BIGSUB ;
1 procedure SUB3 ;
3
var A, B, C : integer ; var C, E : integer ;
procedure SUB1 ; 2 begin { SUB3 }
var A, D : integer ; SUB1 ;
begin { SUB1 } E := B + A ; 2
A := B + C ; 1 end ; { SUB3 }
end ; { SUB1 } begin { SUB2 }
procedure SUB2 ( X : integer ) ; SUB3 ;
begin { BIGSUB } A := D + E ; 3
SUB2 ( 7 ) ; end ; { SUB2 }
end ; { BIGSUB }
MAIN_2 calls BIGSUB
begin { MAIN_2 }
BIGSUB calls SUB2
BIGSUB ;
SUB2 calls SUB3
end. { MAIN_2 }
SUB3 calls SUB1
19
UNVA – CSCI524
9.3.4.1 Static Chains – Example on p390, p391, & p392
Local D top
The reference to
Local A
Dynamic link the variable A at
SUB1 point 1, 2, and 3,
Static link
Return (to SUB3) would be
represented by
Local E the following
Local C points:
SUB3 Dynamic link
1: (0, 3) (local, 2
Static link -2)
Return (to SUB2)
2: (2, 3) (two
Local E level away, 3 – 1)
Local B
3: (1, 3) (one
SUB2 Parameter 7 X level away, 2 – 1)
Dynamic link
Static link
Return (to BIGSUB)
Local C
Local B
Local A
BIGSUB Dynamic link
Static link
Return (to MAIN_2)
MAIN_2 Local X
20
UNVA – CSCI524
9.3.4.2 Displays - I
21
UNVA – CSCI524
9.3.4.2 Displays - II
22
UNVA – CSCI524
9.3.4.2 Displays - III
ARI for
SUB2
ARI for 2
program MAIN_3 ;
BIGSUB
procedure BIGSUB ; 1
ARI for
MAIN_3 0
procedure SUB1 ;
end ; { SUB1 } Stack Display
procedure SUB3 ;
ARI for
end ; { SUB3 } SUB1
SUB2 calls SUB1
end ; { SUB2 } ARI for
SUB2
end ; { BIGSUB }
ARI for 2
end ; { MAIN_3} BIGSUB
1
ARI for
MAIN_3 0
Stack Display
23
UNVA – CSCI524
9.4 Blocks - I
{ int temp ;
temp = list [ upper ] ;
list [ upper ] = list [ lower ] ;
list [ lower ] = temp ;
}
24
UNVA – CSCI524
9.4 Blocks - II
• This amount of space can be allocated after the local variables in the activation
record. Offset for all block variables can be statically computed, so block
variables can be addressed exactly as if they were local variable. For ex,
26
UNVA – CSCI524
9.5 Implementing Dynamic Scoping
27
UNVA – CSCI524
9.5.1 Deep Access - I
28
UNVA – CSCI524
9.5.1 Deep Access - II
29
UNVA – CSCI524
9.5.1 Deep Access - III
30
UNVA – CSCI524
9.5.1 Deep Access - IV
A
Local V Fig. 9.16, p402
Dynamic link
Return (to MAIN_6)
Local U
MAIN_6
Local V
31
UNVA – CSCI524
9.5.2 Shallow Access - I
32
UNVA – CSCI524
9.5.2 Shallow Access - II
33
UNVA – CSCI524
9.5.2 Shallow Access - III
• Fig. 9.17, p403, shows the variable stacks for the above example
program in the same situation as in Fig. 9.16.
34