CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in...

30
CSI 3120, Implementing subprograms, page 1 Implementing subprograms •The environment in block- structured languages •The structure of the activation stack •Static chain •Functions and the stack

Transcript of CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in...

CSI 3120, Implementing subprograms, page 1

Implementing subprograms

• The environment in block-structured languages

• The structure of the activation stack

• Static chain

• Functions and the stack

CSI 3120, Implementing subprograms, page 2

The environmentin block-structured languages

Subprogram calls are strictly nested: the caller waits until the callee has terminated.

call B

A

call C

B

call D

C D

CSI 3120, Implementing subprograms, page 3

Every subprogram activation is represented as an activation record on the activation stack.

The activation record of the callee is placed at the top of stack, directly above the activation record of the caller.

Activation records vary in size. Sizes are usually determined at compile time, unless semidynamic arrays are supported, but every activation record contains the same kind of information about the caller and the callee.

The environmentin block-structured languages (2)

CSI 3120, Implementing subprograms, page 4

Information about the caller

A pointer to the caller’s activation record (the next record on the stack, but this pointer is necessary to handle varying record sizes); this gives us access to the chain of previous callers up to the main program.

The return address (what the caller will do after this subprogram call has terminated).

If this is a function, the address where the function value should be stored.

CSI 3120, Implementing subprograms, page 5

Information about the callee

Scoping information — a pointer to the activation record of the enclosing block (this need not be the same unit as the caller).

Local variables and constants.

Formal parameters (copies or only pointers).

Temporary storage (to evaluate expressions).

CSI 3120, Implementing subprograms, page 6

Memory is allocated to procedure calls in segments of stack storage, as much as is necessary to represent the callee’s block.

Activation records

free

bottom

top

Information about the callee (2)

The compiler generates a prologue, next the translation of the subprogram body and finally an epilogue.

CSI 3120, Implementing subprograms, page 7

Prologue: entering a subprogram

Get a segment of free stack storage — a stack frame — and move the top-of-the-stack pointer up.

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

CSI 3120, Implementing subprograms, page 8

Epilogue: exiting a subprogram

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

Remove the segment from the stack, move the top-of-the-stack pointer down.

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

CSI 3120, Implementing subprograms, page 9

Run-time memory

Run-time memory is divided into three parts.

1. Code area: main program, subprograms.

2. Data area: the run-time stack (all variables are represented—global variables are local in the activation record of the main program).

CSI 3120, Implementing subprograms, page 10

Run-time memory (2)

Run-time memory -- continued.

3.Control information:

• Current instruction pointer IP indicates the instruction about to be executed.

• Current environment pointer EP shows the activation record of the current block, giving access to local and non-local data.

CSI 3120, Implementing subprograms, page 11

In the following examples, we will assume a simple model:

• the pointer to the caller’s activation record,

• the pointer to the enclosing block,

• the return address,

• local data (if any),

• actual parameters (if any).

Return addresses will be symbolic—see the boxes on the next page.

Run-time memory (2)

CSI 3120, Implementing subprograms, page 12

The structure of the activation stack

program M( input,output); var A, B: integer;  procedure P( C: integer; var D: integer); begin {P}{P1} C := C + 2;{P2} D := D + 3;{P3} write('P:',A,B,C, D);{P4} end;

procedure Q( var C:integer); var B: integer;

procedure R( C: integer); begin {R}{R1} C := 29;{R2} P(B, C);{R3} write('R:',A, B, C);{R4} end;

{continued} begin {Q}{Q1} B := 23;{Q2} R(A);{Q3} P(B, C);{Q4} write('Q:', A, B, C);{Q5} end;  begin {M}{M1} A := 6;{M2} B := 17;{M3} P(B, A);{M4} write('M:', A, B);{M5} Q(A);{M6} write('M:', A, B);{M7} end.

CSI 3120, Implementing subprograms, page 13

The structure of the activation stack (2)(dynamic link)(static link)

(return address)A’ 9B’ 17

F1F1M6

C’’ A’B’’ 23

F2F2Q3

C’’’ 29

F3F1R3

D’’’’ C’’’C’’’’ 23

situation after P in R in Q in M called: IP = P1, EP = F4

F1

F2

F3

F4

M

Q

R

P

stack frame

program unit

dynamic link

static link

CSI 3120, Implementing subprograms, page 14

Another example

program Main; var A, B: integer; procedure P; begin {P}{L1P} A := A + 1;{L2P} B := B + 1;{L3P} end;  procedure Q; var B: integer;  procedure R; var A: integer; begin {R}{L1R} A := 16;{L2R} P;{L3R} write(A, B);{L4R} end;

{continued} begin {Q}{L1Q} B := 11;{L2Q} R;{L3Q} P;{L4Q} write(A, B);{L5Q} end;  begin {Main}{L1m} A := 1;{L2m} B := 6;{L3m} P;{L4m} write(A, B);{L5m} Q;{L6m} write(A, B);{L7m} end.

CSI 3120, Implementing subprograms, page 15

Another example (2)(dynamic link)

(static link)(return address)

A 1B 6

situation in Main just before call to P:

IP = L3m, EP = F1

F1

Main

CSI 3120, Implementing subprograms, page 16

Another example (3)(dynamic link)

(static link)(return address)

A 1B 6

F1F1

L4m

F1

F2

Main

P situation after P in Main called:

IP = L1P, EP = F2

CSI 3120, Implementing subprograms, page 17

Another example (4)(dynamic link)

(static link)(return address)

A 2B 7

situation after P in Main terminated:

IP = L4m, EP = F1

F1

Main

CSI 3120, Implementing subprograms, page 18

Another example (5)(dynamic link)

(static link)(return address)

A 2B 7

F1F1

L6mB

F1

F2

Main

Q situation after Q in Main called:

IP = L1Q, EP = F2

CSI 3120, Implementing subprograms, page 19

Another example (6)(dynamic link)

(static link)(return address)

A 2B 7

F1F1

L6mB 11

F2F2

L3QA

F1

F2

F3

Main

Q

R

situation after R in Q in Main called:

IP = L1R, EP = F3

CSI 3120, Implementing subprograms, page 20

Another example (7)(dynamic link)

(static link)(return address)

A 2B 7

F1F1

L6mB 11

F2F2

L3QA 16

F3F1

L3R

F1

F2

F3

F4

Main

Q

R

P

situation after P in R in Q in Main called:

IP = L1P, EP = F4

CSI 3120, Implementing subprograms, page 21

Another example (8)(dynamic link)

(static link)(return address)

A 3B 8

F1F1

L6mB 11

F2F2

L3QA 16

F1

F2

F3

Main

Q

R

situation after P in R in Q in Main terminated:

IP = L4R, EP = F3

CSI 3120, Implementing subprograms, page 22

Another example (9)(dynamic link)

(static link)(return address)

A 3B 8

F1F1

L6mB 11

F1

F2

Main

Q situation after P in Q in Main terminated:

IP = L3Q, EP = F2

CSI 3120, Implementing subprograms, page 23

Another example (10)(dynamic link)

(static link)(return address)

A 3B 8

F1F1

L6mB 11

F2F1

L4Q

F1

F2

F3

Main

Q

P

situation after P in Q in Main called:

IP = L1P, EP = F3

CSI 3120, Implementing subprograms, page 24

Another example (11)(dynamic link)

(static link)(return address)

A 4B 9

F1F1

L6mB 11

F1

F2

Main

Q situation after P in Q in Main terminated:

IP = L4Q, EP = F2

CSI 3120, Implementing subprograms, page 25

Another example (12)(dynamic link)

(static link)(return address)

A 4B 9

situation after Q in Main terminated:

IP = L6m, EP = F1

F1

Main

CSI 3120, Implementing subprograms, page 26

Static chains

Variables represented on the stack are not accessed by name. In a language with static scoping, a variable must, however, be located by moving up the chain of static nesting.

An address of variable V on the stack is composed of two numbers that tell us:

• how many activation records up the chain is the record R that contains V,

• how far V is from the beginning of R.

CSI 3120, Implementing subprograms, page 27

In the situation when Q in Main has been called:Main.Q.B (0, 3)Main.A (1, 3)Main.B unavailable

(dynamic link)(static link)

(return address)A 2B 7

F1F1

L6mB

F1

F2

Main

Q

Static chains (2)

CSI 3120, Implementing subprograms, page 28

In the situation when P in R in Q in Main has been called:

Main.Q.R.A unavailable

Main.Q.B unavailable

Main.A (1, 3)

Main.B (1, 4)

(dynamic link)(static link)

(return address)A 2B 7

F1F1

L6mB 11

F2F2

L3QA 16

F3F1

L3R

F1

F2

F3

F4

Main

Q

R

P

Static chains (3)

CSI 3120, Implementing subprograms, page 29

Functions and the stack

program Main;var A: integer;function G(N: integer): integer; begin if N <= 1 then G := 1 else G := G(N-1) * N end;begin A := G(3); write(A)end.

Assigning locations to program fragments must be a little more elaborate…

L1G if N <= 1 thenL2G value := 1L3G goto L7GL4G G(N-1)L5G tempL6G value := temp * NL7G

L1M G(3)L2M tempL3M A := tempL4M write(A)L5M

CSI 3120, Implementing subprograms, page 30

Functions and the stack (2)(dynamic link)(static link)

(return address)A ?

F1F1

L2M

N 3value ?

F2F1

L5Gvalue ?

F3F1

L5G

N 1value 1

G in G in G in Main returns with 1:IP = L3G, EP = F4

F1

F2

F3

F4

Main

G

G

G

N 2