Implementing Subprograms

26
Implementing Subprograms

description

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 - PowerPoint PPT Presentation

Transcript of Implementing Subprograms

Page 1: Implementing Subprograms

Implementing Subprograms

Page 2: Implementing Subprograms

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

Page 3: Implementing Subprograms

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

Page 4: Implementing Subprograms

Subprogram Activation

The subprogram definition is written by the programmer. The definition is a static entity.

The definition serves as a template for the subprogram activation.

During the execution of the program, if the subprogram is called, a subprogram activation is created.

Page 5: Implementing Subprograms

Subprogram Activation

The activation record is dynamically created when the subprogram is called. It is destroyed when execution of the subprogram terminates.

The code segment is statically created and invariant during execution. The code is shared by all activations.

A subprogram activation has a code segment and an activation record.

Page 6: Implementing Subprograms

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

Page 7: Implementing Subprograms

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.

Local variables

Parameters

Return Address

Dynamic Link

Static Link

Page 8: Implementing Subprograms

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.

Page 9: Implementing Subprograms

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 P{ ARIfor MAIN

TOP

Page 10: Implementing Subprograms

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

Parameter

Return (to main)

Dynamic Link

Static Link

Local variable

T

S

RARIfor B

Local variable P{ ARIfor MAIN

TOP

Page 11: Implementing Subprograms

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

Parameter

Return (to main)

Dynamic Link

Static Link

Local variable

T

S

RARIfor B

Local variable P{ ARIfor MAIN

TOP

Local variable

Parameter

Return (to main)

Dynamic Link

Static Link

Local variable

T

S

RARIfor B

Local variable P{ ARIfor MAIN

Parameter

Return (to B)

Dynamic Link

Static Link

Local variable

X

Y

ARIfor A

TOP

Page 12: Implementing Subprograms

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

Parameter

Return (to main)

Dynamic Link

Static Link

Local variable

T

S

RARIfor B

Local variable P{ ARIfor MAIN

Parameter

Return (to B)

Dynamic Link

Static Link

Local variable

X

Y

ARIfor A

TOP

Page 13: Implementing Subprograms

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

Parameter

Return (to main)

Dynamic Link

Static Link

Local variable

T

S

RARIfor B

Local variable P{ ARIfor MAIN

Parameter

Return (to B)

Dynamic Link

Static Link

Local variable

X

Y

ARIfor A

TOP

Parameter

Return (to A)

Dynamic Link

Static Link

Q

ARIfor C

Page 14: Implementing Subprograms

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

Parameter

Return (to main)

Dynamic Link

Static Link

Local variable

T

S

RARIfor B

Local variable P{ ARIfor MAIN

Parameter

Return (to B)

Dynamic Link

Static Link

Local variable

X

Y

ARIfor A

TOP

Page 15: Implementing Subprograms

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

Parameter

Return (to main)

Dynamic Link

Static Link

Local variable

T

S

RARIfor B

Local variable P{ ARIfor MAIN

TOP

Page 16: Implementing Subprograms

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 P{ ARIfor MAIN

TOP

Page 17: Implementing Subprograms

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

Page 18: Implementing Subprograms

Implementing Nonlocal References

Static Chain

Display

Page 19: Implementing Subprograms

Implementing Nonlocal References

Static Chain - a static chain is a chain of static links that connects certain activation record instances in the stack

Page 20: Implementing Subprograms

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

Parameter

Return (to main)

Dynamic Link

Static Link

Local variable

T

S

RARIfor B

Local variable P{ ARIfor MAIN

Parameter

Return (to B)

Dynamic Link

Static Link

Local variable

X

Y

ARIfor A

TOP

Parameter

Return (to A)

Dynamic Link

Static Link

Q

ARIfor C

Page 21: Implementing Subprograms

Local variable

Parameter

Return (to main)

Dynamic Link

Static Link

Local variable

T

S

RARIfor B

Local variable P{ ARIfor MAIN

Parameter

Return (to B)

Dynamic Link

Static Link

Local variable

X

Y

ARIfor A

TOP

Parameter

Return (to A)

Dynamic Link

Static Link

Q

ARIfor C

Main calls BB calls AA calls C

main

B

CA

Page 22: Implementing Subprograms

Implementing Nonlocal References

Display - the static links are collected in a single array called a display

Page 23: Implementing Subprograms

Main calls BB calls AA calls C

Main

B

CA

ARIfor MAIN

stack

Each subprogram has a static depth

Main - 0

B - 1

C - 2A - 1

display

0

Page 24: Implementing Subprograms

ARIfor B

ARIfor MAINMain calls B

B calls AA calls C

Main - 0

B - 1

C - 2A - 1

stack

1

display

0

Page 25: Implementing Subprograms

ARIfor B

ARIfor MAINMain calls B

B calls AA calls C

Main - 0

B - 1

C - 2A - 1

stack

1

ARIfor A

display

0

Page 26: Implementing Subprograms

ARIfor B

ARIfor MAIN

ARIfor A

ARIfor C

Main calls BB calls AA calls C

Main - 0

B - 1

C - 2A - 1

stack

2

1

display

0