Implementing Subprograms Subprogram linkage refers to the subprogram
Implementing Subprograms
Subprogram linkage refers to the subprogram call and return operations of a programming language.
The execution of a called subprogram requires many associated actions. • parameter passing • storage allocation of local variables • saving execution status of calling program • transfer of control to the subprogram • return to calling program • access to non local variables
Subprogram Activation The subprogram definition is written by the programmer. The definition is a static entity. During the execution of the program, if the subprogram is called, a subprogram activation is created. The definition serves as a template for the subprogram activation.
Subprogram Activation A subprogram activation has a code segment and an activation record. The code segment is statically created and invariant during execution. The code is shared by all activations. The activation record is dynamically created when the subprogram is called. It is destroyed when execution of the subprogram terminates.
Subprogram Activation In programming languages which do not allow recursion, there can be only one activation of a given subprogram at any time. Local variables Parameters Return Address FORTRAN 77 Activation Record
FORTRAN 77 Code and Activation Records Common storage MAIN: Local variables A Parameters Return Address Local variables B Parameters Return Address Data MAIN: A B Code
FORTRAN 77 Code and Activation Records Common storage MAIN: Code Local variables A Parameters Return Address Code Local variables B Parameters Return Address
Implementing Subprograms in ALGOL-like Languages In ALGOL-like programming languages (i. e. Pascal, C++) subprogram linkage is more complex • Different parameter passing methods • Local variables of subprograms are often dynamically allocated • Recursion makes multiple activations of a single subprogram possible • Implementation of static scoping
ALGOL-like Subprogram Activation Record In programming languages which do allow recursion, there can be many activations of a given subprogram at any time. Local variables Parameters Dynamic Link Static Link Return Address
Activation Record for Pascal Procedure procedure sub(var total: real; part: integer); var list: array [1. . 5] of integer; sum: real; Local variable begin sum … Local variable list[5] end; list[4] Local variable list[3] Local variable list[2] Local variable list[1] Local variable part Parameters total Parameters Dynamic Link Static Link Return Address
Static and Dynamic Links The static link points to the bottom of the activation record instance of an activation of the static parent. It is used for references to non-local variables. The dynamic link points to the top of the activation record instance of the caller. It is used for destruction of the current activation record instance.
Program main 1; var p: real; procedure A (X: integer); var Y: boolean; procedure C (Q: boolean); begin end; begin {of proc C} C(Y); end; {of proc A} procedure B (R: real); var S, T: integer; begin A(S); end; {of proc B} begin {of MAIN_1 } B(P); end. {of MAIN_1} ARI for MAIN TOP { Local variable P
Program main 1; var p: real; procedure A (X: integer); var Y: boolean; procedure C (Q: boolean); begin end; begin {of proc C} C(Y); end; {of proc A} procedure B (R: real); var S, T: integer; begin A(S); end; {of proc B} begin {of MAIN_1 } B(P); end. {of MAIN_1} TOP Local variable Parameter Dynamic Link ARI for B ARI for MAIN T S R Static Link Return (to main) { Local variable P
Program main 1; var p: real; procedure A (X: integer); var Y: boolean; procedure C (Q: boolean); begin end; begin TOP {of proc C} C(Y); end; {of proc A} procedure B (R: real); var S, T: integer; begin A(S); end; {of proc B} begin {of MAIN_1 } B(P); end. {of MAIN_1} Local variable Parameter Dynamic Link Static Link TOP (to B) Return ARI for A ARI for B ARI for MAIN { Y X Local variable Parameter Dynamic Link Static Link Return (to main) T S Local variable P R
Program main 1; var p: real; procedure A (X: integer); var Y: boolean; procedure C (Q: boolean); begin end; begin TOP {of proc C} C(Y); end; {of proc A} procedure B (R: real); var S, T: integer; begin A(S); end; {of proc B} begin {of MAIN_1 } B(P); end. {of MAIN_1} ARI for A ARI for B ARI for MAIN Local variable Parameter Dynamic Link Static Link Return (to B) Y X Local variable Parameter Dynamic Link T S R Static Link Return (to main) { Local variable P
Program main 1; var p: real; procedure A (X: integer); var Y: boolean; ARI procedure C (Q: boolean); for C begin end; begin {of proc C} C(Y); end; {of proc A} procedure B (R: real); var S, T: integer; begin A(S); end; {of proc B} begin {of MAIN_1 } B(P); end. {of MAIN_1} TOP Parameter Dynamic Link Static Link Return (to A) Local variable Parameter Dynamic Link Static Link Return (to B) ARI for A Local variable Parameter Dynamic Link ARI for B ARI for MAIN Q Y X T S R Static Link Return (to main) { Local variable P
Program main 1; var p: real; procedure A (X: integer); var Y: boolean; procedure C (Q: boolean); begin end; begin TOP {of proc C} C(Y); end; {of proc A} procedure B (R: real); var S, T: integer; begin A(S); end; {of proc B} begin {of MAIN_1 } B(P); end. {of MAIN_1} ARI for A ARI for B ARI for MAIN Local variable Parameter Dynamic Link Static Link Return (to B) Y X Local variable Parameter Dynamic Link T S R Static Link Return (to main) { Local variable P
Program main 1; var p: real; procedure A (X: integer); var Y: boolean; procedure C (Q: boolean); begin end; begin {of proc C} C(Y); end; {of proc A} procedure B (R: real); var S, T: integer; begin A(S); end; {of proc B} begin {of MAIN_1 } B(P); end. {of MAIN_1} TOP Local variable Parameter Dynamic Link ARI for B ARI for MAIN T S R Static Link Return (to main) { Local variable P
Program main 1; var p: real; procedure A (X: integer); var Y: boolean; procedure C (Q: boolean); begin end; begin {of proc C} C(Y); end; {of proc A} procedure B (R: real); var S, T: integer; begin A(S); end; {of proc B} begin {of MAIN_1 } B(P); end. {of MAIN_1} ARI for MAIN TOP { Local variable P
Implementing Nonlocal References Nonlocal variable references occur in two steps: 1) find the activation record where the nonlocal variable was allocated 2) use the offset of the variable within that activation record to access it
Implementing Nonlocal References Static Chain Display
Implementing Nonlocal References Static Chain - a static chain is a chain of static links that connects certain activation record instances in the stack
Program main 1; var p: real; procedure A (X: integer); var Y: boolean; ARI procedure C (Q: boolean); for C begin end; begin {of proc C} C(Y); end; {of proc A} procedure B (R: real); var S, T: integer; begin A(S); end; {of proc B} begin {of MAIN_1 } B(P); end. {of MAIN_1} ARI for A ARI for B ARI for MAIN TOP Parameter Dynamic Link Q Static Link Return (to A) Local variable Parameter Dynamic Link Static Link Return (to B) Y X Local variable Parameter Dynamic Link T S R Static Link Return (to main) { Local variable P
TOP main A C B Main calls B B calls A A calls C ARI for A ARI for B ARI for MAIN Parameter Dynamic Link Q Static Link Return (to A) Local variable Parameter Dynamic Link Static Link Return (to B) Y X Local variable Parameter Dynamic Link T S R Static Link Return (to main) { Local variable P
Implementing Nonlocal References Display - the static links are collected in a single array called a display
Main - 0 A-1 C-2 B-1 Main calls B B calls A A calls C ARI for MAIN stack 0 display
Main - 0 A-1 C-2 B-1 ARI for B Main calls B B calls A A calls C 1 0 ARI for MAIN stack display
Main - 0 A-1 C-2 B-1 ARI for A ARI for B Main calls B B calls A A calls C 1 0 ARI for MAIN stack display
Main - 0 A-1 C-2 ARI for C B-1 ARI for A ARI for B Main calls B B calls A A calls C 2 1 0 ARI for MAIN stack display
- Slides: 30