Lecture #7
Programming Languages
MISHRA 95
Environment
L-values
Scope.
---Slide 2---
Dangling Reference Problem

type r = record ... end;
t = ^r;
procedure P;
var q: t;
procedure A;
var s: t;
begin
new(s); q:= s; dispose(s);
end;
begin
... A ...;
q := ...; (*L-value whose lifetime has passed*)
end.
---Slide 3---
Static Storage Management
But all data are preserved across successive calls on subroutines.
Forbids both direct and indirect (mutual) recursion.
---Slide 4---
Activation Record
Set of informations necessary for the execution of a subprogram.
When a subroutine executes, it always finds its activation record in the same place.
Needs only to store the ``return address'' in the activation record of the called subroutine.
---Slide 5---
Static Storage Management in FORTRAN
SUBROUTINE P(...) SUBROUTINE Q(...) SUBROUTINE R(...) ... ... ... CALL Q(...) CALL R(...) ... ... ... RETURN RETURN RETURN END END END
COMMON block
---Slide 6---
Stack Based Modern Languages
(ALGOL-like)
The activation record for a subprogram can
only be created, when the subprogram is actually called.
---Slide 7---
Implications of
Call-Time Allocation of AR's
Allocate AR, when the execution begins.
Release that space, when execution
finishes.
P calls a subprogram Q then
P cannot complete before Q.

Q's extent is wholly contained in P's.
---Slide 8---
Procedure Call
Procedure
Call to P
---Slide 9---
Activation Records (AR)
Stack Based Storage Management
Algol and its relatives:
procedure P(...)<-- procedure Q(...)
... \ ...
begin \ begin
... \ ...
Q(...) ----- P(...)
... -> ...
end ------------------/ end
---Slide 10---
Up-Level Addressing and the Display

---Slide 11---
Reference to the Global Variable
---Slide 12---
Up-level Addressing Problem
Intermediate Non-Local Variables
Algol, Pascal, Ada, --- Scope Rules
procedure P;
begin
var x, y: T1;
procedure Q;
begin
var z: T2;
procedure R;
begin
var: a, b: T3;
... <----------z is accessible in Q & R
end;
... <----------x, y are accessible in P, Q & R
end;
...
end;
R tell where x, y and z are
located?
R calls itself recursively.)
---Slide 13---
Displays
An integer value one greater than the lexical level of the procedure in which it is declared.

.
R
.
(Dictated by the static nature of the lexical scope rule.)
---Slide 14---
Displays and Setting Them Up
locations to the AR of R.
then
(P and Q are at the same
level) and
(Q is up-level from P)
---Slide 15---
Setting the Displays
(P and Q are at the same level)

(Q is up-level from P)

---Slide 16---
Sample Display Configuration
---Slide 17---
Displays with
Procedure as Parameters
procedure Q(procedure F); {LexLev(Q) = n}
begin ... F(x) ... end;
procedure P; {LexLev(P) = n}
var a, b: T;
procedure R(y: T); begin ... a ... end;
begin
... P; ... Q(R); ... {LexLev(R) = n+1}
end;
{ Q calls R --- LexLev(R) > LexLev(Q) }
P calls itself recursively
P calls Q and passes R as a
parameter.
P and R have access to a and b; but
not Q.
Q calls R, R cannot establish access
to a and b from the display of Q.
---Slide 18---
Procedure Parameters (Contd.)
R, P must pass
two things
R, and
.
R are established from
P's display (not Q's).
---Slide 19---
Allocation of Assignable Data Types
---Slide 20---
AR for Non-Static Arrays
---Slide 21---
Variant Records
\
---Slide 22---
Parameter Passing
---Slide 23---
Evaluation of Actual Parameter List
---Slide 24---
Call-By-Value
---Slide 25---
Call-By-Reference
A change to i and/or j by the callee
does not change the addresses of the actuals,
A[i,j] or A[i+1,j+1].
---Slide 26---
Call-By-Value/Result
---Slide 27---
Call-By-Name [Algol60]

-- Takes no parameter itself
-- Delivers the address of its corresponding actual in the
environment of the caller
---Slide 28---
Thunks
Note
---Slide 29---
Dynamic Storage
type ref = ^T; var x : ref; procedure P; begin ... new(x) ... end;
---Last Slide---
Heap Allocation
Storage whose extent is not tied to a particular scope cannot be allocated on the stack.
[End of Lecture #7]