1. Trang chủ
  2. » Giáo Dục - Đào Tạo

Chapter 10 implementing subprograms

75 3 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Implementing Subprograms
Trường học Addison-Wesley
Năm xuất bản 2006
Định dạng
Số trang 75
Dung lượng 572,93 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Implementing Subprograms with Dynamic Local Variables cont.Stack-• The compiler generates – A prologue : Entering a subprogram  Get a segment of free stack storage and move the stack po

Trang 1

Chapter 10

Implementing

Subprograms

Trang 2

Chapter 10 Topics

• The General Semantics of Calls and Returns

• Implementing “Simple” Subprograms

• Implementing Subprograms with

Stack-Dynamic Local Variables

• Nested Subprograms

• Blocks

• Implementing Dynamic Scoping

Trang 3

The General Semantics of Calls and Returns

• The subprogram call and return operations of a language are together called its subprogram

linkage

• Any implementation method for subprograms must be based on the semantics of subprogram linkage

• Subprogram calls are strictly nested: the caller

waits until the callee has terminated

Trang 4

The General Semantics … (cont.)

call B

A

call C B

call D

Trang 5

The General Semantics … (cont.)

• A subprogram call has two semantic actions

associated with it

– Call semantics

– Return semantics

Trang 6

Call Semantics

• Save the execution status of the caller

• Carry out the parameter-passing process

• Pass the return address to the callee

• Transfer control to the callee

• The call must cause some mechanism to be

created to provide access to nonlocal variables that are visible to the callee

Trang 7

Return Semantics

• If the subprogram has parameters that are out

mode or inout mode, the first action is to move the local values of the associated formal

parameters to the actual parameters

• If it is a function, move the functional value to a

place the caller can get it

• Deallocate the storage used for local variables

• Restore the execution status of the caller

• Transfer control back to the caller

Trang 8

Implementing “Simple” Subprograms: Parts

• Required Storage: Status information of the

caller, parameters, return address, and

functional value (if it is a function)

• Two separate parts: the actual code and the

noncode part (local variables and data that can

change)

• The format, or layout, of the noncode part of an executing subprogram is called an activation

record (AR)

Trang 9

Parts (cont.)

• An activation record instance (ARI) is a concrete example of an AR

• The AR of the callee is placed at the top of the

run-time stack, directly above the AR of the

Trang 10

An AR for “Simple” Subprograms

[Functional value]

Local variables Parameters Return address

Trang 11

Code & ARs parts for

FORTRAN 77 program

Common storage Local variables Local variables Parameters Return address Local variables Parameters Return address Local variables Parameters Return address

Code

Trang 12

Implementing Subprograms with

Stack-Dynamic Local Variables

• More complex activation record

– The compiler must generate code to cause

implicit allocation and deallocation of local

variables

– Recursion must be supported (adds the possibility

of multiple simultaneous ARIs of a subprogram)

– The AR format is static, but its size may be

dynamic

– An ARI is dynamically created when a subprogram

is called

Trang 13

Typical AR for a Language with

Stack-Dynamic Local Variables

Stack top

Local variables Parameters Dynamic link Static link Return address

Trang 14

Information About the Caller

• The return address consists of:

– A pointer to the code segment of the caller

– An offset address in that code segment of the

instruction following the call statement

• Static link: a pointer to the ARI of the enclosing block

– It is used for accesses to nonlocal variables (static scope)

Trang 15

Information About the Callee

• Dynamic link: A pointer to the top of the caller’s ARI (the next record on the stack)

– This pointer is necessary to handle varying record sizes

– This gives us access to the chain of previous

callers up to the main program

• Formal parameters (values or only pointers)

• Local variables and constants

• Temporary storage (to evaluate expressions)

Trang 16

Local Local Local

total part a[1] a[2] a[3] a[4] a[5] sum

Trang 17

Implementing Subprograms with Dynamic Local Variables (cont.)

Stack-• The compiler generates

– A prologue : Entering a subprogram

 Get a segment of free stack storage and move the stack pointer up

 Put in the new stack frame all data about the caller and the callee

– An epilogue : Exiting a subprogram

 Return a value (if the subprogram is a function)

 Remove the segment from the stack, move the stack pointer down

 Jump to the return address (continue the statements in the caller's body)

Trang 18

An Example Without Recursion

A(S);

… end;

begin

… B(P);

… end.

Trang 19

An Example Without Recursion (cont.)

Local Local Parameter Dynamic link Static link Return to MAIN

R S T

Trang 20

An Example Without Recursion (cont.)

Local Local Parameter Dynamic link Static link Return to MAIN

R S T

R S T

Local

Top

ARI for B

ARI for MAIN

P

Local Parameter Dynamic link Static link Return to B

X

Y

ARI for A

MAIN

A

B

C

Trang 21

An Example Without Recursion (cont.)

Local Local Parameter Dynamic link Static link Return to MAIN

R S T

R S T

Local

Top

ARI for B

ARI for MAIN

P

Local Parameter Dynamic link Static link Return to B

X

Y

ARI for A

Local Local Parameter Dynamic link Static link Return to MAIN

R S T

Local

Top

ARI for B

ARI for MAIN

P

Local Parameter Dynamic link Static link Return to B

X

Y

ARI for A

Parameter Dynamic link Static link Return to A

Q ARI

for C

MAIN

A

B

C

Trang 22

Dynamic Chain and Local Offset

• The collection of dynamic links in the stack at a given time is called the dynamic chain

• Local variables can be accessed by their offset from the beginning of the ARI This offset is

called the local_offset

– The local_offset can be determined by the

compiler at compile time

Trang 23

An Example With Recursion

• The AR used in the previous example supports recursion, e.g

int factorial (int n) {

Trang 24

Functional value Parameter Dynamic link Static link Return (to main)

? First call

Trang 25

Copyright © 2006 Addison-Wesley All rights reserved 1-25

Functional value Parameter Dynamic link Static link Return (to main)

? First call

Functional value Parameter Dynamic link Static link Return (to main)

n

Local

First ARI for factorial

ARI for main

? 3

?

Functional value Parameter Dynamic link Static link Return (to factorial)

n

Top

Second ARI

for factorial

? 2

Trang 26

Copyright © 2006 Addison-Wesley All rights reserved 1-26

Functional value Parameter Dynamic link Static link Return (to main)

? First call

Functional value Parameter Dynamic link Static link Return (to main)

n

Local

First ARI for factorial

ARI for main

? 3

?

Functional value Parameter Dynamic link Static link Return (to factorial)

n

Top

Second ARI

for factorial

? 2

Functional value Parameter Dynamic link Static link Return (to main)

n

Local

First ARI for factoria l ARI for

? 3

? Third call

Functional value Parameter Dynamic link Static link Return (to factorial)

n Second ARI

for factorial

? 2

Functional value Parameter Dynamic link Static link Return (to factorial)

n Top

Third ARI for factorial

? 1

Trang 27

Functional value Parameter Dynamic link Static link Return (to main)

?

At position 2 in factorial third call completed

Functional value Parameter Dynamic link Static link Return (to factorial)

n Second ARI for

factorial

? 2

Functional value Parameter Dynamic link Static link Return (to factorial)

Trang 28

Functional value Parameter Dynamic link Static link Return (to main)

?

At position 2 in factorial third call completed

Functional value Parameter Dynamic link Static link Return (to factorial)

n Second ARI for

factorial

? 2

Functional value Parameter Dynamic link Static link Return (to factorial)

value

Functional value Parameter Dynamic link Static link Return (to main)

? 3

?

At position 2 in factorial second call completed

Functional value Parameter Dynamic link Static link Return (to factorial)

Trang 29

Functional value Parameter Dynamic link Static link Return (to main)

?

At position 2 in factorial third call completed

Functional value Parameter Dynamic link Static link Return (to factorial)

n Second ARI for

factorial

? 2

Functional value Parameter Dynamic link Static link Return (to factorial)

value

Functional value Parameter Dynamic link Static link Return (to main)

? 3

?

At position 2 in factorial second call completed

Functional value Parameter Dynamic link Static link Return (to factorial)

Functional value Parameter Dynamic link Static link Return (to main)

?

At position 2 in factorial

Trang 30

Functional value Parameter Dynamic link Static link Return (to main)

?

At position 2 in factorial third call completed

Functional value Parameter Dynamic link Static link Return (to factorial)

n Second ARI for

factorial

? 2

Functional value Parameter Dynamic link Static link Return (to factorial)

value

Functional value Parameter Dynamic link Static link Return (to main)

? 3

?

At position 2 in factorial second call completed

Functional value Parameter Dynamic link Static link Return (to factorial)

Functional value Parameter Dynamic link Static link Return (to main)

?

At position 2 in factorial

value Local

ARI for

At position 3 in main final results

Top

Trang 31

Nested Subprograms

• Some non-C-based static-scoped languages

(e.g., Fortran 95, Ada, JavaScript) use

stack-dynamic local variables and allow subprograms

to be nested

• All variables that can be non-locally accessed

reside in some ARI in the stack

• The process of locating a nonlocal reference:

1 Find the correct ARI

2 Determine the correct offset within that ARI

Trang 32

Static Scoping

• There are two major implementation techniques for creating accesses to nonlocal variables in a static-scoped language: static chain and display

• Finding the offset is easy

• Finding the correct ARI

– Static semantic rules guarantee that all nonlocal variables that can be referenced have been

allocated in some ARI that is on the stack when

the reference is made

Trang 33

Static Scoping - Concepts

• The static link in an ARI for subprogram A

points to the ARI of A's static parent (enclosing scope)

• A static chain is a chain of static links that

connects certain ARIs

• The static chain from an ARI connects it to all of its static ancestors

Trang 34

Static Scoping – Concepts (cont.)

• To find the declaration for a reference to a

nonlocal variable:

– You could chase the static chain until the ARI that has the variable is found

– Searching each ARI as it is found, if variable

names were stored in the ARI

• Static_depth is an integer associated with a

static scope whose value is the depth of nesting

of that scope

Trang 35

… end;

procedure C ;  static_depth = 1

… end;

end.

Trang 36

Static Scoping – Concepts (cont.)

• The nesting_depth of a (nonlocal) reference is

the difference between:

– The static_depth of the subprogram containing

the reference to nonlocal

– The static_depth of the subprogram containing

the declaration for nonlocal

• A reference can be represented by the pair:

(nesting_depth, local_offset)

where local_offset is the offset in the ARI of the

variable being referenced and nesting_depth is the number of static links to the correct ARI

Trang 37

procedure A

procedure B

procedure C

… referencing a variable declared in A;  1

… referencing a variable declared in B;  2

… referencing a variable declared in C;  3

end C;

end B;

end A;

• Position 1: nesting_depth of this reference would be 2

• Position 2: nesting_depth of this reference would be 1

• Position 3: nesting_depth of this reference would be 0

Trang 38

Example Pascal Program

procedure SUB2 (X : integer);

var B, E : integer;

procedure SUB3 ; var C, E : integer;

begin SUB1; E := B + A: < 2 end;

begin

SUB3; A := D + E; < 3 end;

begin SUB2(7); end;

begin BIGSUB; end;

Trang 39

Stack Contents

Local Local Dynamic link Static link Return (to MAIN)

A

Local

ARI for BIGSUB

Local

B

C Top

1 0

Trang 40

Local

ARI for BIGSUB

Local Parameter Dynamic link Static link Return (to BIGSUB)

X

ARI for SUB2

Local

B C

Local

B E

1 0

1 0 Top

Stack Contents

Trang 41

Local Local Dynamic link Static link Return (to MAIN)

A

Local

ARI for BIGSUB

Local Parameter Dynamic link Static link Return (to BIGSUB)

X

ARI for SUB2

Local Local Dynamic link Static link Return (to SUB2)

C ARI for

SUB3

Local

B C

Local

B E

E Top

1 0

1 0

1 0

Trang 42

Local Local Dynamic link Static link Return (to MAIN)

A

Local

ARI for BIGSUB

Local Parameter Dynamic link Static link Return (to BIGSUB)

X

ARI for SUB2

Local Local Dynamic link Static link Return (to SUB2)

C

Top

ARI for SUB3

Local

B C

Local

B E E

Local Local Dynamic link Static link Return (to SUB2)

A ARI for

SUB1

D

1 0

1 0

1 0

1 0

Trang 43

Example Pascal Program (cont.)

Trang 44

Static Chain Maintenance

• Assume there are no parameters that are

subprograms and no pass-by-name parameters

• At the call

– The ARI must be built

– The dynamic link is just the old stack top pointer – The static link must point to the most recent ARI

of the static parent

• Two methods

1 Search the dynamic chain until the first ARI for the static parent is found (easy, but slow)

Trang 45

Static Chain Maintenance (cont.)

2 When the compiler encounters a procedure call:

 It determines the procedure that declared the callee , which must be a static ancestor of the caller

 It then computes the nesting_depth

 This information is stored and can be accessed by

the procedure call during execution

• At the time of the call, the static link of the

callee’s ARI is determined by moving down the static chain of the caller the number of links

equal to the nesting_depth

Trang 46

Static Chain Maintenance (cont.)

• At the return

– Subprogram’s ARI is removed from the stack

– The new top ARI is that of the unit that called the subprogram whose execution just terminated

– Because the static chain from this ARI was never changed, it works correctly just as it did before

the call to the other subprogram

Trang 47

Evaluation of the Static Chain Method

• This method works for all procedure linkage,

except when parameters that are subprogram

names are involved

• A nonlocal reference is slow if the number of

scopes between the reference and the

declaration of the referenced variable is large

• It is difficult to estimate the costs of nonlocal

references, because the costs of references are not equal, and can change with code upgrades and fixes

Trang 48

• An alternative to static chains

• Static links are stored in a single array called a

display

• The contents of the display at any given time is

a list of addresses of the accessible ARIs

– The entries in the display are pointers to the ARIs that have the variables in the referencing

environment

Trang 49

Mechanics of references

• Represent references as

(display_offset, local_offset) where display_offset plays the role of nesting_depth

• Accesses to nonlocals using a display require

exactly two steps:

– Use the display_offset to get the pointer in the

display into the ARI with the variable

– Use the local_offset to get to the variable within the ARI

Trang 50

Display maintenance

• Assuming no parameters that are subprograms and no pass-by-name parameters

• Note that display_offset depends only on the

static_depth of the procedure whose ARI is

being built

• There are k+1 entries in the display, where k is the static_depth of the currently executing unit (k = 0 is for the main program)

Trang 51

Display maintenance (cont.)

• At the call: For a call to procedure P with a

static_depth of k

– Save, in the new ARI, a copy of the pointer at

position k in the display

– Put the link to the ARI for P at position k in the

display

• At the return

– Move the saved display pointer from the ARI back into the display at position k

Ngày đăng: 23/03/2022, 08:30