1 / 30

Implementing subprograms

Implementing subprograms. The environment in block-structured languages The structure of the activation stack Static chain Functions and the stack. A. B. C. D. call C. call B. call D. The environment in block-structured languages.

yoshino-gen
Download Presentation

Implementing subprograms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Implementing subprograms • The environment in block-structured languages • The structure of the activation stack • Static chain • Functions and the stack

  2. A B C D call C call B call D The environmentin block-structured languages Subprogram calls are strictly nested: the caller waits until the callee has terminated.

  3. The environmentin block-structured languages (2) 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.

  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. 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. bottom Activation records top free Information about the callee (2) Memory is allocated to procedure calls in segments of stack storage, as much as is necessary to represent the callee’s block. The compiler generates a prologue, next the translation of the subprogram body and finally an epilogue.

  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. 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. Run-time memory Run-time memory is divided into three parts. • Code area: main program, subprograms. • Data area: the run-time stack (all variables are represented—global variables are local in the activation record of the main program).

  10. Run-time memory (2) Run-time memory -- continued. • 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. Run-time memory (2) 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.

  12. 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. The structure of the activation stack

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

  14. 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. Another example

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

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

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

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

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

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

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

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

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

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

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

  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. Static chains (2) In the situation when Q in Main has been called: Main.Q.B (0, 3) Main.A (1, 3) Main.B unavailable (dynamic link) F1 (static link) (return address) Main A 2 B 7 F2 F1 F1 Q L6m B

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

  29. 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… L1G if N <= 1 then L2G value := 1 L3G goto L7G L4G G(N-1) L5G temp L6G value := temp * N L7G L1M G(3) L2M temp L3M A := temp L4M write(A) L5M Functions and the stack

  30. (dynamic link) F1 Functions and the stack (2) (static link) (return address) Main A ? F2 F1 F1 L2M G value ? G in G in G in Main returns with 1: IP = L3G, EP = F4 N 3 F3 F2 F1 G L5G value ? N 2 F4 F3 F1 L5G G value 1 N 1

More Related