GATE / CS ARCHIVE

GATE CSE 1992 / QUESTION 10(b)

Activation records and lexical display

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

GATE 1992, Q10(b) Descriptive
Report issue

Runtime Environments & Parameter Passing

Question 10(b), GATE 1992: For the following lexically scoped Pascal-like skeleton, draw activation records and the display just after the procedures invoked at labels x and y begin. Distinguish the two A declarations.
program Test;
  procedure A;                 {outer A}
    procedure B;
      procedure A;             {inner A}
      begin ... end;
    begin
      y: A;
    end;
  begin
    B;
  end;
begin
  x: A;
end.
Tap to enlarge
Answer & solution

Answer: At x: Test→A_outer with display [Test,A_outer]. At y: Test→A_outer→B→A_inner with display [Test,A_outer,B,A_inner]. (no official key was published)

Assign lexical levels Test=0, A_outer=1, B=2 and A_inner=3. The declarations, not the dynamic call order, determine these levels. The call x:A in Test resolves to A_outer. The call y:A in B resolves to the nearest enclosing declaration of A, namely B’s own nested A_inner; it does not recursively invoke A_outer.

Immediately after x’s callee starts, before A_outer executes B, the stack and display are:
Stack bottom→top:
[Test frame: caller/runtime link]
[A_outer frame: dynamic link→Test; static link→Test;
return continuation→after x:A; saved previous display[1]]
Display: D[0]→Test, D[1]→A_outer.
D[2] and D[3] have no active corresponding frames and are undefined/unused. Do not invent B or A_inner frames at this first instant.

A_outer then calls B, whose body calls A_inner at y. Immediately after y’s callee starts:

Stack bottom→top:
[Test]
[A_outer: DL→Test, SL→Test, return→after x:A, saved old D[1]]
[B: DL→A_outer, SL→A_outer, return→after B call, saved old D[2]]
[A_inner: DL→B, SL→B, return→after y:A, saved old D[3]]
Display diagram:
D[0] ──> Test
D[1] ──> A_outer
D[2] ──> B
D[3] ──> A_inner

The dynamic link names the calling activation, while the static link names the activation of the lexical parent. They happen to coincide throughout this simple nested call chain; their meanings remain different. A display-based implementation can replace static-chain access entirely, so the static links shown are explanatory rather than required additional storage. On entry at lexical level ℓ, save old D[ℓ] in the frame and set D[ℓ] to that frame; on return restore the saved value. Return addresses, saved registers and local-storage slots are conventional frame fields, but no particular byte sizes or actual locals are specified. The omitted body of A_inner has not yet executed at the requested second snapshot, so no additional calls or frames may be assumed.

Original paper, page 7