CH 7 Ans
CH 7 Ans
by Kenneth C. Louden
Exercise 7.2
(a)
a[9]
.
. Global/static area
.
a[0] <
s: "hello"
i: 1
b:
fp > control link Activation record of f
return address
j: 1
i: 1
c:
sp >
Direction of
free space
stack growth
(b)
a[9]
.
. Global/static area
.
a[0]
s: "hello" <
s:
Exercise 7.4
Activation record of
> main program
access link
control link Activation record of
>
call to c
return address
access link
control link Activation record of
> <
return address call to b
access link
Activation record of
control link call to c
fp >
return address
sp >
free space
Compiler Construction: Principles and Practice Chapter 7 Exercise Answers, Page 3
Exercise 7.6
dolist:<ipd ,_ >
Global/static area
null:<ipn ,_ >
Activation record of
> main program
print:<ipn ,_ >
<no access link>
Activation record of
control link
> return address < call to dolist
x: 1
newprint:<ipp ,ep >
ep 1
1
print:<ipp ,ep >
1
<no access link> Activation record of
control link call to dolist
> return address <
x: 2
newprint:<ipp ,ep >
ep 2
2
print:<ipp ,ep >
2
<no access link>
Activation record of
control link
> call to dolist
return address
x: 2
ep newprint:<ipp ,ep >
3
3
access link
Activation record of
control link
call to print
> return address
access link
control link Activation record of
fp > call to print
return address
sp >
free space
Exercise 7.8
(a) One way to do this would be to switch the locations of the local variables and the arguments
in the activation record. Thus, the local variables would be accessed by positive offset from the
fp, and the arguments would be accessed by negative offset, with the first argument pushed onto
the stack just below the control link. This would require that the return address be stored at the
bottom of the stack (if it is to be pushed using the sp). It also requires that the total size of the
local variables of a procedure be known at its call sites (except of course that indirection could
still be used for variable-length data). With this organization, the calling sequence would
become:
Compiler Construction: Principles and Practice Chapter 7 Exercise Answers, Page 4
On call:
1. Adjust the sp by subtracting from it the size of the local variables of the procedure.
2. Store the fp in the new activation record.
3. Compute the arguments and push them onto the stack.
4. Store the return address onto the stack.
5. Jump to the code of the procedure to be called.
On exit:
(b) One way to use an ap pointer would be to have the ap point to the very top of the activation
record. Thus, a new ap can be formed by copying the sp just before the arguments are computed.
Of course, the old ap must also be stored, and this could be done just before computing the new
ap. Then the old ap would be restored using the current ap, rather than the fp. The organization
of an activation record then looks as follows:
(previous activation)
ap old ap
>
arguments
fp control link
>
return address
local variables
sp >
free space
On call:
On exit:
Exercise 7.11
(a) The standard C parameter passing conventions are to pass all arguments by value, except that
array are considered pointer values, so arrays are actually passed by reference. Thus, the offsets
are as follows:
1. c: 16
2. s[7]: the address of s is at offset 12, then 7 is added to this address to find s[7].
3. y[2]: −14
(b) 1. c: 22
2. s[7]: 19
3. y[2]: −14
(c) 1. c: 12
2. s[7]: the address of s is at offset 8, then 7 is added to this address to find s[7].
3. y[2]: −14
Exercise 7.13
Object of class A:
a
virtual function
table pointer > virtual function table for A
A::f
Object of class B:
a
virtual function
table pointer > virtual function table for B
B::f
b A::g
Compiler Construction: Principles and Practice Chapter 7 Exercise Answers, Page 6
Object of class C:
a
virtual function
table pointer > virtual function table for C
B::f
b C::g
c
Exercise 7.16
Pass by value: 0 1 2 0
Pass by reference: 1 0 2 0
Pass by name: 2 1 −1 0