GATE / CS ARCHIVE

GATE CSE 1997 / QUESTION 24

Five activation records with nested recursion

Compiler Design · Runtime Environments & Parameter Passing · Full GATE CSE 1997 paper

GATE 1997, Q24 5 marks · Descriptive
Report issue

Runtime Environments & Parameter Passing

Question 24, GATE 1997: Consider this Pascal nesting and call structure, with no calls other than those shown. Main declares X; A, nested in Main, declares Y; B, nested in A, declares Z; C, nested in B, also declares Z. Main calls A; A calls B; B calls C and, after C returns, calls A. C contains no procedure calls. At an instant there are five activation records on the runtime stack. Show its top, static and dynamic links, and local variables. Source skeleton:
program Main; var X: integer;
  procedure A; var Y: integer;
    procedure B; var Z: integer;
      procedure C; var Z: integer;
      begin {no procedure calls} end;
    begin C; A end;
  begin B end;
begin A end.
Ellipses in the source allow other non-call work; the skeleton preserves every shown declaration and call.
Question 24, GATE 1997: Consider this Pascal nesting and call structure, with no calls other than those shown. Main declares X; A, nested in Main, declares Y; B, nested in A, declares Z; C, nested in B, also declares Z. Main calls A; A calls B; B calls C and, after C returns, calls A. C contains no procedure calls. At an instant there are five activation records on the runtime stack. Show its top, static and dynamic links, and local variables. Source skeleton:
program Main; var X: integer;
  procedure A; var Y: integer;
    procedure B; var Z: integer;
      procedure C; var Z: integer;
      begin {no procedure calls} end;
    begin C; A end;
  begin B end;
begin A end.
Ellipses in the source allow other non-call work; the skeleton preserves every shown declaration and call.
Tap to enlarge
Answer & solution

Answer: Bottom to top: Main, A1, B1, A2, B2. Dynamic links follow the call chain; static links B2→A2, A2→Main, B1→A1, A1→Main. (no official key was published)

Distinguish a procedure's lexical parent from the activation that calls it. The dynamic link records the caller; the static link records the active instance of the enclosing lexical scope. Calls to A from inside B are recursive calls to a procedure declared directly inside Main, not declarations of a new A inside B.

Trace the shown calls. Main calls A1, which calls B1. B1's call to C1 creates only the fourth record, and C1 makes no further calls. C1 returns before the following call to A2, so its record has been removed. A2 is then the fourth record, and its call to B2 creates the fifth. At any matching five-record stage of this call pattern, the active chain is therefore:

Stack position   Activation   Local slot   Dynamic link   Static link
TOP              B2           B2.Z         A2             A2
                 A2           A2.Y         B1             Main
                 B1           B1.Z         A1             A1
                 A1           A1.Y         Main           Main
BOTTOM           Main         Main.X       null           null

Other ordinary record fields, such as a return address and saved registers, may exist but are not assigned values by the problem. A1.Y and A2.Y are distinct storage cells, as are B1.Z and B2.Z. No numeric values for X,Y,Z can be recovered from the ellipses. The completed C1 record, including its separate C1.Z slot, is absent.

The dynamic chain is B2→A2→B1→A1→Main. The different static chain from B2 is B2→A2→Main. In particular, A2's static link must not point to B1 merely because B1 called it. To create A2, B1 can follow its own static chain B1→A1→Main to find A's lexical parent. This is the central distinction the example tests.

Original paper, page 10