1 / 26

Implementing Subprograms

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

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

  2. Subprogram linkage refers to the subprogram call and return operations of a programming language.

  3. 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

  4. 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.

  5. 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.

  6. 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

  7. 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.

  8. 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.

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

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

  15. 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

  16. 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

  17. 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

  18. Implementing Nonlocal References Static Chain Display

  19. Implementing Nonlocal References Static Chain - a static chain is a chain of static links that connects certain activation record instances in the stack

  20. 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

  21. 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

  22. Implementing Nonlocal References Display - the static links are collected in a single array called a display

  23. 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

  24. 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

  25. 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

  26. 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

More Related