Implementing Subprograms

Post on 30-Dec-2015

46 views 4 download

Tags:

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

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.

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.

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.

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

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

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}

Local variable P{ ARIfor MAIN

TOP

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

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

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

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

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

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

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

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}

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

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

Implementing Nonlocal References

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

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

ARIfor B

ARIfor MAINMain calls B

B calls AA calls C

Main - 0

B - 1

C - 2A - 1

stack

1

display

0

ARIfor B

ARIfor MAINMain calls B

B calls AA calls C

Main - 0

B - 1

C - 2A - 1

stack

1

ARIfor A

display

0

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