Chapter10-Subprogram Implementation-Dynamic and Static Scoping
Chapter10-Subprogram Implementation-Dynamic and Static Scoping
Implementing
Subprograms
ISBN 0-321-33025-0
Chapter 10 Topics
void A(integer X) {
integer Y;
...
C(Y);
...
}
void B(real R) {
integer S, T;
... main calls B
B calls A
A(S);
...
}
void C(integer Q) { A calls C
...
}
void main() {
real P;
...
B(P);
...
}
procedure C{
procedure A{
declare x, z;
declare w, v;
...
... }
}
MAIN_6 calls A, A calls A, A calls B, B calls C
Copyright © 2006 Addison-Wesley. All rights reserved. 1-21
Using Shallow Access to Implement
Dynamic Scoping
To return:
Use dynamic link in current activation record to get old
activation record.
Use return address in housekeeping area to jump to.
Copyright © 2006 Addison-Wesley. All rights reserved. 1-31
Blocks
• Two Methods:
1. Treat blocks as parameter-less subprograms
that are always called from the same location
– Every block has an activation record; an instance is
created every time the block is executed
2. Since the maximum storage required for a
block can be statically determined, this amount
of space can be allocated after the local
variables in the activation record
General properties
1. Procedure allocations follow general rules of C
• Allocate activation records on a stack
• No need for static links, but need dynamic links
2. Class variables are allocated like structs in C.
[Structs in C++ can have function members. Functions
in structs are by default public, while in a class
are by default private.]
3. Static functions are not represented in runtime
activation records
4. Objects requiring dynamic binding (i.e., virtual
functions) are present in runtime activation
records
Copyright © 2006 Addison-Wesley. All rights reserved. 1-35
Sample C++ allocation example
class A {
public: int x;
virtual void p() {...};
void q() {...p()...};
protected: int y;
private: int Z;
...}
class B:A {
public: int w;
virtual void p() {...};
void r() {...p()...};
private: int v;
virtual void s() {...};
...}
X: R-value X: R-value
P: Addr of P in A P: Addr of P in B
Y: R-value Y: R-value
Z: R-value
Z: R-value
Class A W: R-value
V: R-value
S: Addr of S in B