Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.

Similar presentations


Presentation on theme: "CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack."— Presentation transcript:

1 CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack Static chain Functions and the stack

2 CSI 3120, Implementing subprograms, page 2 The environment in block-structured languages Subprogram calls are strictly nested: the caller waits until the callee has terminated. call B A call C B call D CD

3 CSI 3120, Implementing subprograms, page 3 Every subprogram activation is represented as an activation record on the activation stack. The activation record of the callee is placed at the top of stack, directly above the activation record of the caller. Activation records vary in size. Sizes are usually determined at compile time, unless semidynamic arrays are supported, but every activation record contains the same kind of information about the caller and the callee. The environment in block-structured languages (2)

4 CSI 3120, Implementing subprograms, page 4 Information about the caller A pointer to the caller’s activation record (the next record on the stack, but this pointer is necessary to handle varying record sizes); this gives us access to the chain of previous callers up to the main program. The return address (what the caller will do after this subprogram call has terminated). If this is a function, the address where the function value should be stored.

5 CSI 3120, Implementing subprograms, page 5 Information about the callee Scoping information — a pointer to the activation record of the enclosing block (this need not be the same unit as the caller). Local variables and constants. Formal parameters (copies or only pointers). Temporary storage (to evaluate expressions).

6 CSI 3120, Implementing subprograms, page 6 Memory is allocated to procedure calls in segments of stack storage, as much as is necessary to represent the callee’s block. Activation records free bottom top Information about the callee (2) The compiler generates a prologue, next the translation of the subprogram body and finally an epilogue.

7 CSI 3120, Implementing subprograms, page 7 Prologue: entering a subprogram Get a segment of free stack storage — a stack frame — and move the top-of-the-stack pointer up. Put in the new stack frame all data about the caller and the callee.

8 CSI 3120, Implementing subprograms, page 8 Epilogue: exiting a subprogram Return a value (if the subprogram is a function). Remove the segment from the stack, move the top-of-the-stack pointer down. Jump to the return address (continue the statements in the caller's body).

9 CSI 3120, Implementing subprograms, page 9 Run-time memory Run-time memory is divided into three parts. 1.Code area: main program, subprograms. 2.Data area: the run-time stack (all variables are represented—global variables are local in the activation record of the main program).

10 CSI 3120, Implementing subprograms, page 10 Run-time memory (2) Run-time memory -- continued. 3.Control information: Current instruction pointer IP indicates the instruction about to be executed. Current environment pointer EP shows the activation record of the current block, giving access to local and non-local data.

11 CSI 3120, Implementing subprograms, page 11 In the following examples, we will assume a simple model: the pointer to the caller’s activation record, the pointer to the enclosing block, the return address, local data (if any), actual parameters (if any). Return addresses will be symbolic—see the boxes on the next page. Run-time memory (2)

12 CSI 3120, Implementing subprograms, page 12 The structure of the activation stack program M( input,output); var A, B: integer; procedure P( C: integer; var D: integer); begin {P} {P1} C := C + 2; {P2} D := D + 3; {P3} write('P:',A,B,C, D); {P4} end; procedure Q( var C:integer); var B: integer; procedure R( C: integer); begin {R} {R1} C := 29; {R2} P(B, C); {R3} write('R:',A, B, C); {R4} end; {continued} begin {Q} {Q1} B := 23; {Q2} R(A); {Q3} P(B, C); {Q4} write('Q:', A, B, C); {Q5} end; begin {M} {M1} A := 6; {M2} B := 17; {M3} P(B, A); {M4} write('M:', A, B); {M5} Q(A); {M6} write('M:', A, B); {M7} end.

13 CSI 3120, Implementing subprograms, page 13 The structure of the activation stack (2) (dynamic link) (static link) (return address) A’ 9 B’ 17 F1 M6 C’’  A’ B’’ 23 F2 Q3 C’’’ 29 F3 F1 R3 D’’’’  C’’’ C’’’’ 23 situation after P in R in Q in M called: IP = P1, EP = F4 F1 F2 F3 F4 M Q R P stack frame program unit dynamic link static link

14 CSI 3120, Implementing subprograms, page 14 Another example program Main; var A, B: integer; procedure P; begin {P} {L1P} A := A + 1; {L2P} B := B + 1; {L3P} end; procedure Q; var B: integer; procedure R; var A: integer; begin {R} {L1R} A := 16; {L2R} P; {L3R} write(A, B); {L4R} end; {continued} begin {Q} {L1Q} B := 11; {L2Q} R; {L3Q} P; {L4Q} write(A, B); {L5Q} end; begin {Main} {L1m} A := 1; {L2m} B := 6; {L3m} P; {L4m} write(A, B); {L5m} Q; {L6m} write(A, B); {L7m} end.

15 CSI 3120, Implementing subprograms, page 15 Another example (2) (dynamic link) (static link) (return address) A 1 B 6 situation in Main just before call to P: IP = L3m, EP = F1 F1 Main

16 CSI 3120, Implementing subprograms, page 16 Another example (3) (dynamic link) (static link) (return address) A 1 B 6 F1 L4m F1 F2 Main P situation after P in Main called: IP = L1P, EP = F2

17 CSI 3120, Implementing subprograms, page 17 Another example (4) (dynamic link) (static link) (return address) A 2 B 7 situation after P in Main terminated: IP = L4m, EP = F1 F1 Main

18 CSI 3120, Implementing subprograms, page 18 Another example (5) (dynamic link) (static link) (return address) A 2 B 7 F1 L6m B F1 F2 Main Q situation after Q in Main called: IP = L1Q, EP = F2

19 CSI 3120, Implementing subprograms, page 19 Another example (6) (dynamic link) (static link) (return address) A 2 B 7 F1 L6m B 11 F2 L3Q A F1 F2 F3 Main Q R situation after R in Q in Main called: IP = L1R, EP = F3

20 CSI 3120, Implementing subprograms, page 20 Another example (7) (dynamic link) (static link) (return address) A 2 B 7 F1 L6m B 11 F2 L3Q A 16 F3 F1 L3R F1 F2 F3 F4 Main Q R P situation after P in R in Q in Main called: IP = L1P, EP = F4

21 CSI 3120, Implementing subprograms, page 21 Another example (8) (dynamic link) (static link) (return address) A 3 B 8 F1 L6m B 11 F2 L3Q A 16 F1 F2 F3 Main Q R situation after P in R in Q in Main terminated: IP = L4R, EP = F3

22 CSI 3120, Implementing subprograms, page 22 Another example (9) (dynamic link) (static link) (return address) A 3 B 8 F1 L6m B 11 F1 F2 Main Q situation after P in Q in Main terminated: IP = L3Q, EP = F2

23 CSI 3120, Implementing subprograms, page 23 Another example (10) (dynamic link) (static link) (return address) A 3 B 8 F1 L6m B 11 F2 F1 L4Q F1 F2 F3 Main Q P situation after P in Q in Main called: IP = L1P, EP = F3

24 CSI 3120, Implementing subprograms, page 24 Another example (11) (dynamic link) (static link) (return address) A 4 B 9 F1 L6m B 11 F1 F2 Main Q situation after P in Q in Main terminated: IP = L4Q, EP = F2

25 CSI 3120, Implementing subprograms, page 25 Another example (12) (dynamic link) (static link) (return address) A 4 B 9 situation after Q in Main terminated: IP = L6m, EP = F1 F1 Main

26 CSI 3120, Implementing subprograms, page 26 Static chains Variables represented on the stack are not accessed by name. In a language with static scoping, a variable must, however, be located by moving up the chain of static nesting. An address of variable V on the stack is composed of two numbers that tell us: how many activation records up the chain is the record R that contains V, how far V is from the beginning of R.

27 CSI 3120, Implementing subprograms, page 27 In the situation when Q in Main has been called: Main.Q.B(0, 3) Main.A(1, 3) Main.Bunavailable (dynamic link) (static link) (return address) A 2 B 7 F1 L6m B F1 F2 Main Q Static chains (2)

28 CSI 3120, Implementing subprograms, page 28 In the situation when P in R in Q in Main has been called: Main.Q.R.Aunavailable Main.Q.Bunavailable Main.A(1, 3) Main.B(1, 4) (dynamic link) (static link) (return address) A 2 B 7 F1 L6m B 11 F2 L3Q A 16 F3 F1 L3R F1 F2 F3 F4 Main Q R P Static chains (3)

29 CSI 3120, Implementing subprograms, page 29 Functions and the stack program Main; var A: integer; function G(N: integer): integer; begin if N <= 1 then G := 1 else G := G(N-1) * N end; begin A := G(3); write(A) end. Assigning locations to program fragments must be a little more elaborate… L1Gif N <= 1 then L2Gvalue := 1 L3Ggoto L7G L4GG(N-1) L5G  temp L6Gvalue := temp * N L7G L1MG(3) L2M  temp L3MA := temp L4Mwrite(A) L5M

30 CSI 3120, Implementing subprograms, page 30 Functions and the stack (2) (dynamic link) (static link) (return address) A ? F1F1 F1F1 L2M N 3 value ? F2F2 F1F1 L5G value ? F3F3 F1F1 L5G N 1 value 1 G in G in G in Main returns with 1: IP = L3G, EP = F 4 F1F1 F2 F3 F4 Main G G G N 2


Download ppt "CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack."

Similar presentations


Ads by Google