isbn 0-321-19362-8 chapter 10 implementing subprograms –semantics of calls and returns...

17
ISBN 0-321-19362-8 Chapter 10 Implementing Subprograms –Semantics of Calls and Returns –Implementing “Simple” Subprograms –Implementing Subprograms with Stack- Dynamic Local Variables

Post on 21-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

ISBN 0-321-19362-8

Chapter 10

Implementing Subprograms–Semantics of Calls and Returns

–Implementing “Simple” Subprograms

–Implementing Subprograms with Stack-Dynamic Local Variables

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 10-2

The General Semantics of Calls and Returns

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

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 10-3

Several Approaches

• Simple Subprograms– No nested subprograms

– Static local variables

• Stack-dynamic local variables• Nested subprograms• Dynamic scope

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 10-4

Semantics for “Simple” Subprograms

• Call Semantics:1. Save the execution status of the caller

2. Carry out the parameter-passing process

3. Pass the return address to the callee

4. Transfer control to the callee

• Return Semantics:1. If pass-by-value-result parameters are used, move the

current values of those parameters to their corresponding actual parameters

2. If it is a function, move the functional value to a place the caller can get it

3. Restore the execution status of the caller

4. Transfer control back to the caller

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 10-5

Implementing “Simple” Subprograms

• Required Storage– Status information of the caller

– Parameters

– Return address

– Functional value (if it is a function)

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 10-6

Activation Records for “Simple” Subprograms

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

• An activation record instance is a concrete example of an activation record (the collection of data for a particular subprogram activation)

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 10-7

Layout of Code and Activation Records for a Program with “Simple” Subprograms

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 10-8

Implementing Subprograms with Stack-Dynamic Local Variables

• More complicated because:– The compiler must generate code to cause implicit

allocation and deallocation of local variables

– Recursion must be supported (adds the possibility of multiple simultaneous activations of a subprogram)

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 10-9

Typical Activation Record for a Language with Stack-Dynamic Local Variables

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 10-10

Implementing Subprograms with Stack-Dynamic Local Variables

• The activation record format is static, but its size may be dynamic

• The dynamic link points to the top of an instance of the activation record of the caller– This cannot be determined at compile time

• An activation record instance is dynamically created when a subprogram is called

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 10-11

An Example C Function

void sub(float total, int part)

{

int list[4];

float sum;

…}

[4]

[3]

[2]

[1]

[0]

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 10-12

An Example C Program Without Recursion

void A(int x) {int y;...C(y);...

}void B(float r) {

int s, t;...A(s);...

}void C(int q) {

...}void main() {

float p;...B(p);...

}

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 10-13

Program without RecursionNote that: main calls B

B calls AA calls C

void A(int x) {int y;...C(y);...

}void B(float r) {

int s, t;...A(s);...

}void C(int q) {

...}void main() {

float p;...B(p);...

}

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 10-14

Implementing Stack-Dynamic Subprograms

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

• Local variables can be accessed by their offset from the beginning of the activation record. This offset is called the local_offset

• The local_offset of a local variable can be determined by the compiler– Assuming all stack positions are the same size, the first local

variable declared has an offset of three plus the number of parameters, e.g., in main, the local_offset of Y in A is 3

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 10-15

Recursion

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

int factorial(int n) { <-----------------------------1 if (n <= 1) return 1; else return (n * factorial(n - 1)); <-----------------------------2 } void main() { int value; value = factorial(3); <-----------------------------3 }

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 10-16

Stack Contents for Recursive Calls

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 10-17

Stack contents during execution of main and factorial