260 likes | 421 Views
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
E N D
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.
Implementing Subprograms in ALGOL-like Languages In programming languages which allow recursion (i.e. Pascal, C++) subprogram linkage is 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
Local variables Parameters Dynamic Link Static Link Return Address Activation Record for Subprogram Allowing Recursion In programming languages which do allow recursion, there can be many activations of a given subprogram at any time.
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 main1; var p: real; procedure A (X: integer); var Y: boolean; procedure C (Q: boolean); begin end; {of proc C} begin 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 ARI for MAIN Local variable { P
Program main1; var p: real; procedure A (X: integer); var Y: boolean; procedure C (Q: boolean); begin end; {of proc C} begin 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 T Local variable S Parameter R ARI for B Dynamic Link Static Link Return (to main) ARI for MAIN Local variable { P
Program main1; var p: real; procedure A (X: integer); var Y: boolean; procedure C (Q: boolean); begin end; {of proc C} begin 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 Y Parameter X Dynamic Link ARI for A Static Link Return (to B) TOP Local variable Local variable T T Local variable Local variable S S Parameter Parameter R R ARI for B ARI for B Dynamic Link Dynamic Link Static Link Static Link Return (to main) Return (to main) TOP ARI for MAIN ARI for MAIN Local variable Local variable { { P P
Program main1; var p: real; procedure A (X: integer); var Y: boolean; procedure C (Q: boolean); begin end; {of proc C} begin 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 Y Parameter X Dynamic Link ARI for A Static Link Return (to B) Local variable T Local variable S Parameter R ARI for B Dynamic Link Static Link Return (to main) TOP ARI for MAIN Local variable { P
Program main1; var p: real; procedure A (X: integer); var Y: boolean; procedure C (Q: boolean); begin end; {of proc C} begin 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} Parameter Q Dynamic Link ARI for C Static Link Return (to A) Local variable Y Parameter X Dynamic Link ARI for A Static Link Return (to B) Local variable T Local variable S Parameter R ARI for B Dynamic Link Static Link Return (to main) TOP ARI for MAIN Local variable { P
Program main1; var p: real; procedure A (X: integer); var Y: boolean; procedure C (Q: boolean); begin end; {of proc C} begin 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 Y Parameter X Dynamic Link ARI for A Static Link Return (to B) Local variable T Local variable S Parameter R ARI for B Dynamic Link Static Link Return (to main) TOP ARI for MAIN Local variable { P
Program main1; var p: real; procedure A (X: integer); var Y: boolean; procedure C (Q: boolean); begin end; {of proc C} begin 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 T Local variable S Parameter R ARI for B Dynamic Link Static Link Return (to main) ARI for MAIN Local variable { P
Program main1; var p: real; procedure A (X: integer); var Y: boolean; procedure C (Q: boolean); begin end; {of proc C} begin 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 ARI for MAIN 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 main1; var p: real; procedure A (X: integer); var Y: boolean; procedure C (Q: boolean); begin end; {of proc C} begin 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} Parameter Q Dynamic Link ARI for C Static Link Return (to A) Local variable Y Parameter X Dynamic Link ARI for A Static Link Return (to B) Local variable T Local variable S Parameter R ARI for B Dynamic Link Static Link Return (to main) TOP ARI for MAIN Local variable { P
Parameter Q Dynamic Link ARI for C Static Link Return (to A) Local variable Y Parameter X Dynamic Link ARI for A Static Link Return (to B) Local variable T Local variable S Parameter R ARI for B Dynamic Link Static Link Return (to main) TOP ARI for MAIN Local variable { P main A C B Main calls B B calls A A calls C
Implementing Nonlocal References Display - the static links are collected in a single array called a display
Each subprogram has a static depth Main - 0 Main A - 1 A C - 2 C B - 1 B 0 display ARI for MAIN stack Main calls B B calls A A calls C
0 display Main - 0 A - 1 C - 2 B - 1 ARI for B 1 ARI for MAIN Main calls B B calls A A calls C stack
0 display Main - 0 A - 1 C - 2 B - 1 ARI for A ARI for B 1 ARI for MAIN Main calls B B calls A A calls C stack
0 display Main - 0 A - 1 C - 2 ARI for C B - 1 ARI for A ARI for B 2 1 ARI for MAIN Main calls B B calls A A calls C stack