Sunteți pe pagina 1din 34

UNVA – CSCI524

CSCI524
Principles of Programming Languages
Chapter 9
Prof. Ellick Chen
University of Northern Virginia
UNVA – CSCI524
9.2 Implementing FORTRAN 77 Subprograms

• The semantics of a FORTRAN 77 subprogram


call requires the following actions:
1. Save the execution status of the current
program unit.
2. Carry out the parameter-passing process.
3. Pass the return address to the callee
4. Transfer control to the callee.

2
UNVA – CSCI524
9.2 Implementing FORTRAN 77 Subprograms

• The semantics of a FORTRAN 77 subprogram


return requires the following actions:
1. If pass-by-value-result parameter are used, the current
values of those parameters are moved to the
corresponding actual parameters.
2. If the subprogram is a function, the functional values is
moved to a place accessible to the caller.
3. The execution status of the caller is restored.
4. Control is transferred back to the caller.

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

• The format of an activation record is


static. (determined before run-time)
Functional value

Local variables

Parameters

Return address

* Fig. 9.1, p380 A FORTRAN 77 activation record

5
UNVA – CSCI524
9.2 Implementing FORTRAN 77 Subprograms

Common storage

Fig. 9.2, p380 MAIN Local variables

The code and Local variables


A Parameters
activation Return address
records of a Data Local variables

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

• Figure 9.2 shows a FORTRAN 77 program


consisting of COMMON storage, a main program,
and three subprograms: A, B, and C.
• The construction of the complete FORTRAN 77
program shown in Figure 9.2 is not done entirely
by the compiler. In fact, they might be compiled
separately, and put together by the linker, which
is part of the operating system.

7
UNVA – CSCI524
9.3 Implementing Subprograms in ALGOL-Like Languages

• ALGOL-Like static-scoped languages, such as Pascal, Ada,


and Delphi.
• Subprogram linkage in ALGOL-like language is more complex
than the linkage of FORTRAN 77 subprogram for the following
reasons:
– Parameters can usually be passed by two different methods: Pass
by value or Pass by reference.
– Variables declared in subprograms are often dynamically allocated.
– Recursion adds the possibility of multiple simultaneous activations
of a subprogram.
– ALGOL-like languages use static scoping to provide access to non-
local variables.

8
UNVA – CSCI524
9.3.1 More Complex Activation Records

• The return address often consists


Fig. 9.3, p382, for a
typical activation of a pointer to the code segment
record for an ALGOL- of the caller and an offset address
like language. in that code segment of the
instruction following the call.
Local variables • The static link, (also called static
scope pointer), points to the
Parameters bottom of the activation record
instance of an activation of the
Dynamic link
static parent. It is used for
accesses to non-local variables,
Static link
and will be discussed in detail in
Return address 9.3.4.

9
UNVA – CSCI524
9.3.1 More Complex Activation Records

Fig. 9.3, p382, for a • Dynamic link is a pointer to the


typical activation top of an instance of the activation
record for an ALGOL- record of the caller. This link is
like language.
used in the destruction of the
current activation record instance
Local variables when the procedure completes its
execution.
Parameters • Local variables are allocated and
Dynamic link possibly initialized in the called
subprogram, so they appear last.
Static link • See example, a Pascal procedure
on next slide:
Return address

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])

Local (list [3])


sum : real ;
Local (list [2])
begin Local (list [1])
….
Parameter (part)
end ;
Parameter (total)

The activation record for the sub Dynamic link


is shown right, and also called
run-time stack. Static link

Return address

11
UNVA – CSCI524
9.3.2 An Example Without Recursion and Non-local References

• The example program on p384. The MAIN_1 calls B


sequence of procedure calls in this B calls A
program is
A calls C
program MAIN_1 ;
var P : real ;
procedure B ( R : real ) ;
procedure A ( X : integer) ;
var S, T : integer ;
var Y : boolean ;
begin { B } B
procedure C ( Q : boolean ) ;
A(S) 1
begin { C } C
3 A end ; { B }
end ; { C }
begin { MAIN_1 }
begin { A }
2 B(P);
C(Y);
end. { MAIN_1 }
end ; { A }

12
UNVA – CSCI524
Fig 9.5 Stack contents for three points in the 3
Example
top Parameter Q
Dynamic link
ARI

2 for C Static link


ARI = activation record instance Return (to A)
top
Local Local Y
Y
Parameter ARI
Parameter X
X
1 ARI
for A Dynamic link for A Dynamic link
Static link Static link
Return (to B) Return (to B)
top
Local T Local T Local T

Local S Local S Local S

ARI Parameter R ARI Parameter R ARI Parameter R


for B
for B
Dynamic link for B
Dynamic link Dynamic link
Static link Static link Static link
Return (to MAIN) Return (to MAIN) Return (to MAIN)
ARI for ARI for ARI for
Local P Local P MAIN Local P
MAIN MAIN

13
UNVA – CSCI524
9.3.2 An Example Without Recursion and Non-local References

• The collection of dynamic links present in the stack at a


given time is called dynamic chain, or call chain.
– It represents the dynamic history of how execution got to its
current position, which is always in the procedure code whose
activation record instance is on top of the stack.
– References to local variables can be represented in the code
as offsets from the beginning of the activation record of the
local scope. Such an offset is called a local_offset.
– The local_offset of a variable in an activation record can be
determined at compile time, using the order, types, and sizes of
variables declared in the procedure associated with the
activation record.

14
UNVA – CSCI524
9.3.4 Mechanisms for Implementing Non-local References

• There are two major implementation techniques for creating


accesses to non-local variables in a static-scoped language:
static chains and displays.
• A reference to a non-local variables requires a two-step
access process.
– The first step of the access process is to find the instance of
the activation record in the stack in which the variable was
allocated.
– The second part is to use the local_offset of the variable to
access it.

15
UNVA – CSCI524
9.3.4.1 Static Chains - I

• A static chain is a chain of static links that connect certain


activation record instances in the stack.
• During the execution of a procedure P, the static link of its
activation record instance points to the bottom of an
activation record instance of P’s static parent program unit.
That instance’s static link points, in turn, to P’s static
grandparent program unit’s bottom of activation record
instance.
• So the static chain links all the static ancestors of an
executing subprogram, in order of static parent first.
• This chain can obviously be used to implement the
accesses to non-local variables in static-scoped languages.

16
UNVA – CSCI524
9.3.4.1 Static Chains - II

• Finding the correct activation record instance of a non-local


variable using static links is relatively straightforward. When a
reference is made to a non-local variable, the activation record
instance containing the variable can be found by searching the
static chain until a static ancestor activation record instance is
found that contains the variable.
• Static_depth is an integer associated with a static scope that
indicates how deeply it is nested in the outermost scope. For
example,
Program A;
Procedure B ;
The static_depths of A, B, and
Procedure C ;
C are 0, 1, and 2, respectively.
end ; {C}
end ; {B}
end. {A}

17
UNVA – CSCI524
9.3.4.1 Static Chains - III

• The length of the static chain needed to reach the correct


activation record instance for a non-local reference to a
variable x is exactly the difference between the static depth
of the procedure containing the reference to x and the static
depth of the procedure containing the declaration for x.

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

 This difference is called the nesting_depth, or


chain_offset, of the reference.
 The actual reference can be represented by an ordered pair
of integers (chain_offset, local_offset).

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

• The only widely used alternative to static chains is to use a


display.
• For this approach, the static links are collected in a single
array called a display, rather than being stored in the
activation records.
• The contents of the display at any time is a list of addresses
of the accessible activation record instances in the stack –
one for each active scope – in the order in which they are
nested.
• Access to non-local using a display require exactly two
steps for every access, regardless of the number of scope
levels between the reference and the declaration of the
variable being accessed.

21
UNVA – CSCI524
9.3.4.2 Displays - II

• These two steps are accomplished as follows:


– The link to the correct activation record, which resides in
the display, is found using a statically computed value
called the display_offset, which is closely related to the
static chain chain_offset.
– The local_offset within the activation record instance is
computed and used exactly as with static chain
implementation.
– A non-local reference is represented by an ordered pair
of integers, (display_offset, local_offset).
– See example, fig. 9.10, p395 next slide.

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 SUB2 ; MAIN_3 calls BIGSUB, BIGSUB calls SUB2

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

• In C, C++, and Java, provide for user-specified local scopes


for variables called blocks. For example,

{ int temp ;
temp = list [ upper ] ;
list [ upper ] = list [ lower ] ;
list [ lower ] = temp ;
}

 A block is specified in C as a segment of code that begins


with one or more data definitions and is enclosed in braces.

24
UNVA – CSCI524
9.4 Blocks - II

• The lifetime of the variable temp in the block


above begins when control enters the block and
ends when control exits the block.
• The advantage of using such a local variable is
that it cannot interfere with any other variable with
the same name that is declared elsewhere in the
program.
• The maximum amount of storage required for
block variables at any time during the execution of
a program can be statically determined, because
blocks are entered and exited in strictly textual
order.
25
UNVA – CSCI524
9.4 Blocks - III

• 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,

main_5 ( ) { The static


memory
int x, y, z ; layout shown
in Fig. 9.15, e
while ( … ) { int a, b, c ; p401. d
while ( … ) { int d, e ; c
Note that f and b and g
} g occupy the
a and f
} same memory
locations as a Z
while ( … ) { int f, g ; } and b. Y
X
}
ARI for MAIN_5

26
UNVA – CSCI524
9.5 Implementing Dynamic Scoping

• There are at least two distinct ways in


which non-local references in a dynamic-
scoped language can be implemented:
deep access and shallow access.

27
UNVA – CSCI524
9.5.1 Deep Access - I

• When a program in a language that uses


dynamic scoping refers to a non-local variable,
the reference can be resolved by searching
through the declarations in the other subprograms
that are currently active, beginning with the one
most recently activated.
• This concept is somewhat similar to that of
accessing non-local variables in a static-scoped
language, except that the dynamic chain is
followed.

28
UNVA – CSCI524
9.5.1 Deep Access - II

• The dynamic chain links together all subprogram


activation record instances in the reverse of the
order in which they were activated. Therefore, the
dynamic chain is exactly what is needed to
reference non-local variables in a dynamic-scoped
languages. This method is called deep access
because access may require searches deep in the
stack. For example,

29
UNVA – CSCI524
9.5.1 Deep Access - III

procedure C ; procedure A ; Suppose the following sequence of


procedure calls occurs:
integer x, z ; integer v, w ;
MAIN_6 calls A
begin begin
A calls A
x := u + v ; ……
A calls B
end ; end ;
B calls C
procedure B ; procedure MAIN_6 ;
integer w, x ; integer v, u ;
begin begin
….. …..
end ; end ;

30
UNVA – CSCI524
9.5.1 Deep Access - IV

Local Z Consider the references to the


Local X variables x, u, and v in procedure C.
C
Dynamic link X is found in the ARI for C.
Return (to B)
X U is found by searching all of the
Local
ARI on the stack. Because the only
Local W
B existing variable with that name is in
Dynamic link
MAIN_6. This involves following four
Return (to A) dynamic links and examining ten
Local W variable names.
Local V
A V is found in the most recent ARI for
Dynamic link
the procedure A.
Return (to A)
Local W

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

• Shallow access is an alternative implementation


method, not an alternative semantics.
• In the shallow access method, variables declared in
subprograms are not stored in the activation records
of those subprograms.
• One variation of shallow access is to have a separate
stack for each variable name in a complete program.
• Every time a new variable with a particular name is
created by a declaration at the beginning of a
subprogram activation, the variable is given a cell on
the stack for its name.

32
UNVA – CSCI524
9.5.2 Shallow Access - II

• Every reference to the name is to the variable on top of


the stack associated with that name, because the top one
is the most recently created.
• When a subprogram terminates, the lifetime of its local
variables ends, and the stacks for those variable names
are popped.
• This method allows very fast references to variables, but
maintaining the stacks at the entrances and exits of
subprograms is costly.

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.

The choice between


shallow and deep access to
A B
non-local variables depends
A C A on the relative frequencies
MAIN_6 MAIN_6 B C A of subprogram calls and
non-local references.
u v x z w
The deep access method provides fast subprogram linkage, but references to non-
locals are costly. The shallow access method provides much faster references to non-
locals, but is more costly in terms of subprogram linkages.

34

S-ar putea să vă placă și