Concepts of Programming Languages

(Sean Pound) #1
10.3 Implementing Subprograms with Stack-Dynamic Local Variables 449

the stack. It is straightforward to modify this approach for parameters being
passed in registers.

10.3.2 An Example Without Recursion


Consider the following skeletal C program:

void fun1(float r) {
int s, t;

... 1
fun2(s);
...
}


void fun2(int x) {
int y;

... 2
fun3(y);
...
}


void fun3(int q) {

... 3
}


void main() {
float p;

...
fun1(p);
...
}


The sequence of function calls in this program is

main calls fun1
fun1 calls fun2
fun2 calls fun3

The stack contents for the points labeled 1, 2, and 3 are shown in
Figure 10.5.
At point 1, only the activation record instances for function main and
function fun1 are on the stack. When fun1 calls fun2, an instance of fun2’s
activation record is created on the stack. When fun2 calls fun3, an instance
of fun3’s activation record is created on the stack. When fun3’s execution
ends, the instance of its activation record is removed from the stack, and the
EP is used to reset the stack top pointer. Similar processes take place when
Free download pdf