programming languages chapter 2: syntax4430s001/functions.pdf11/18/2015 8 copyright © 2006 the...

19
11/18/2015 1 Programming Languages 2nd edition Tucker and Noonan Chapter 9 Functions It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures. A. Perlis 9-1 Contents 9.1 Basic Terminology 9.2 Function Call and Return 9.3 Parameters 9.4 Parameter Passing Mechanisms 9.5 Activation Records 9.6 Recursive Functions 9.7 Run Time Stack 9-2

Upload: others

Post on 24-Mar-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming Languages Chapter 2: Syntax4430s001/functions.pdf11/18/2015 8 Copyright © 2006 The McGraw-Hill Companies, Inc. 9.7 Run Time Stack A stack of activation records. • Each

11/18/2015

1

Copyright © 2006 The McGraw-Hill Companies, Inc.

Programming Languages2nd edition

Tucker and Noonan

Chapter 9

Functions

It is better to have 100 functions operate on one data structure

than 10 functions on 10 data structures.

A. Perlis

9-1

Copyright © 2006 The McGraw-Hill Companies, Inc.

Contents

9.1 Basic Terminology

9.2 Function Call and Return

9.3 Parameters

9.4 Parameter Passing Mechanisms

9.5 Activation Records

9.6 Recursive Functions

9.7 Run Time Stack

9-2

Page 2: Programming Languages Chapter 2: Syntax4430s001/functions.pdf11/18/2015 8 Copyright © 2006 The McGraw-Hill Companies, Inc. 9.7 Run Time Stack A stack of activation records. • Each

11/18/2015

2

Copyright © 2006 The McGraw-Hill Companies, Inc.

9.1 Basic Terminology

Value-returning functions:

– known as “non-void functions/methods” in C/C++/Java

– called from within an expression.

e.g., x = (b*b - sqrt(4*a*c))/2*a

Non-value-returning functions:

– known as “procedures” in Ada,

“subroutines” in Fortran,

“void functions/methods” in C/C++/Java

– called from a separate statement.

e.g., strcpy(s1, s2);

9-3

Copyright © 2006 The McGraw-Hill Companies, Inc.

9.2 Function Call and Return

Example C/C++ Program Fig 9.1

int h, i;

void B(int w) {

int j, k;

i = 2*w;

w = w+1;

}

void A(int x, int y) {

bool i, j;

B(h);

}

int main() {

int a, b;

h = 5; a = 3; b = 2;

A(a, b);

}

9-4

Page 3: Programming Languages Chapter 2: Syntax4430s001/functions.pdf11/18/2015 8 Copyright © 2006 The McGraw-Hill Companies, Inc. 9.7 Run Time Stack A stack of activation records. • Each

11/18/2015

3

Copyright © 2006 The McGraw-Hill Companies, Inc.

9.3 Parameters

Definitions

– An argument is an expression that appears in a function

call.

– A parameter is an identifier that appears in a function

declaration.

E.g., in Figure 9.1

The call A(a, b) has arguments a and b.

The function declaration A has parameters x and y.

9-5

Copyright © 2006 The McGraw-Hill Companies, Inc.

Parameter-Argument Matching

Usually by number and by position.

I.e., any call to A must have two arguments, and they must

match the corresponding parameters’ types.

Exceptions:

Perl - parameters aren’t declared in a function header.

Instead, parameters are available in an array @_, and are

accessed using a subscript on this array.

Ada - arguments and parameters can be linked by name. E.g.,

the call A(y=>b, x=>a) is the same as A(a, b)

9-6

Page 4: Programming Languages Chapter 2: Syntax4430s001/functions.pdf11/18/2015 8 Copyright © 2006 The McGraw-Hill Companies, Inc. 9.7 Run Time Stack A stack of activation records. • Each

11/18/2015

4

Copyright © 2006 The McGraw-Hill Companies, Inc.

9.4 Parameter Passing Mechanisms

• By value

• By reference

• By value-result

• By result

• By name

9-7

Copyright © 2006 The McGraw-Hill Companies, Inc.

Pass by Value

Compute the value of the argument at the time of the call and assign that value to the parameter.

E.g., in the call A(a, b) in Fig. 9.1, a and b are passed by

value. So the values of parameters x and y become 3 and 2,

respectively when the call begins.

So passing by value doesn’t normally allow the called function to modify original variables/values.

All arguments in C and Java are passed by value.

But references can be passed to allow argument values to be

modified. E.g., void swap(int *a, int *b) { … }

9-8

Page 5: Programming Languages Chapter 2: Syntax4430s001/functions.pdf11/18/2015 8 Copyright © 2006 The McGraw-Hill Companies, Inc. 9.7 Run Time Stack A stack of activation records. • Each

11/18/2015

5

Copyright © 2006 The McGraw-Hill Companies, Inc.

Pass by Reference

Compute the address of the argument at the time of the call and assign it to the parameter.

Example

Fig 9.3

Since h is passed by

reference, its value changes

during the call to B.

int h, i;

void B(int* w) {

int j, k;

i = 2*(*w);

*w = *w+1;

}

void A(int* x, int* y) {

bool i, j;

B(&h);

}

int main() {

int a, b;

h = 5; a = 3; b = 2;

A(&a, &b);

}

9-9

Copyright © 2006 The McGraw-Hill Companies, Inc.

Pass by Value-Result and ResultPass by value at the time of the call and/or copy the

result back to the argument at the end of the call.

– E.g., Ada’s in out parameter can be implemented as value-

result.

– Value-result is often called copy-in-copy-out.

Reference and value-result are the same, except when aliasing occurs. That is, when:

– the same variable is both passed and globally referenced

from the called function, or

– the same variable is passed for two different parameters.

Aliasing occurs when, within a function or procedure, the same memory location can be accessed using different names.

9-10

Page 6: Programming Languages Chapter 2: Syntax4430s001/functions.pdf11/18/2015 8 Copyright © 2006 The McGraw-Hill Companies, Inc. 9.7 Run Time Stack A stack of activation records. • Each

11/18/2015

6

Copyright © 2006 The McGraw-Hill Companies, Inc.

Pass by Name

Textually substitute the argument for every instance of its corresponding parameter in the function body.

– Exemplifies late binding, since evaluation of the argument

is delayed until its occurrence in the function body is

actually executed.

procedure swap(a, b); swap(a[i], i)

integer a, b; swap(i, a[i])

begin integer t;

t := a;

a := b;

b := t

end;

9-11

Copyright © 2006 The McGraw-Hill Companies, Inc.

9-22

Parameter Passing Methods of Major Languages

C

– Pass-by-value

– Pass-by-reference is simulated by using pointers as parameters

C++

– A special pointer type called reference type for pass-by-reference

Java

– All parameters are passed are passed by value

– Object parameters are references passed by value

Concepts of Programming Languages, 9th ed., by Robert W. Sebesta. Addison Wesley, 2010. 9-13

Page 7: Programming Languages Chapter 2: Syntax4430s001/functions.pdf11/18/2015 8 Copyright © 2006 The McGraw-Hill Companies, Inc. 9.7 Run Time Stack A stack of activation records. • Each

11/18/2015

7

Copyright © 2006 The McGraw-Hill Companies, Inc.

9.5 Activation Records

A block of information associated with each function call, which includes:

• Parameters and local variables

• Return address

• Saved registers

• Temporary variables

• Return value

• Static link - to the function’s static parent

• Dynamic link - to the activation record of the caller

9-15

Copyright © 2006 The McGraw-Hill Companies, Inc.

9.6 Recursive Functions

A function that can call itself, either directly or indirectly, is a recursive function. E.g.,

int factorial (int n) {

if (n < 2)

return 1;

else return n*factorial(n-1);

}

self-call

9-16

Page 8: Programming Languages Chapter 2: Syntax4430s001/functions.pdf11/18/2015 8 Copyright © 2006 The McGraw-Hill Companies, Inc. 9.7 Run Time Stack A stack of activation records. • Each

11/18/2015

8

Copyright © 2006 The McGraw-Hill Companies, Inc.

9.7 Run Time Stack

A stack of activation records.

• Each new call pushes an activation record, and

each completing call pops the topmost one.

• So, the topmost record is the most recent call, and

the stack has all active calls at any run-time moment.

For example, consider the call factorial(3). This

places one activation record onto the stack and generates a

second call factorial(2). This call generates the call

factorial(1), so that the stack gains three activation records.

9-17

Copyright © 2006 The McGraw-Hill Companies, Inc.

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

Concepts of Programming Languages, 9th ed., by Robert W. Sebesta. Addison Wesley, 2010. 9-18

The return address is usually a pointer to the instruction following the

call in the code segment of the calling program.

The dynamic link is a pointer to the base of the activation record

instance of the caller. In static-scoped languages, the link is used to

provide traceback information when a runtime error occurs.

Page 9: Programming Languages Chapter 2: Syntax4430s001/functions.pdf11/18/2015 8 Copyright © 2006 The McGraw-Hill Companies, Inc. 9.7 Run Time Stack A stack of activation records. • Each

11/18/2015

9

Copyright © 2006 The McGraw-Hill Companies, Inc.

Implementing Subprograms with Stack-Dynamic Local Variables: Activation RecordThe activation record format is static, but its size

may be dynamic

The dynamic link points to the base of an instance of the activation record of the caller

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

Activation record instances reside on the run-time stack

The Environment Pointer (EP) must be maintained by the run-time system. It always points at the base of the activation record instance of the currently executing program unit

The runtime stack provides the required separate copies of the parameters, local variables and return address.

Concepts of Programming Languages, 9th ed., by Robert W. Sebesta. Addison Wesley, 2010. 9-19

Copyright © 2006 The McGraw-Hill Companies, Inc.

An Example: C Function

void sub(float total, int part)

{

int list [5];

float sum;

}

[4]

[3]

[2]

[1]

[0]

Concepts of Programming Languages, 9th ed., by Robert W. Sebesta. Addison Wesley, 2010. 9-20

Page 10: Programming Languages Chapter 2: Syntax4430s001/functions.pdf11/18/2015 8 Copyright © 2006 The McGraw-Hill Companies, Inc. 9.7 Run Time Stack A stack of activation records. • Each

11/18/2015

10

Copyright © 2006 The McGraw-Hill Companies, Inc.

An Example Without Recursion

main calls fun1fun1 calls fun2Fun2 calls fun3

Concepts of Programming Languages, 9th ed., by Robert W. Sebesta. Addison Wesley, 2010. 9-21

Copyright © 2006 The McGraw-Hill Companies, Inc.

An Example Without Recursion

Concepts of Programming Languages, 9th ed., by Robert W. Sebesta. Addison Wesley, 2010. 9-22

Page 11: Programming Languages Chapter 2: Syntax4430s001/functions.pdf11/18/2015 8 Copyright © 2006 The McGraw-Hill Companies, Inc. 9.7 Run Time Stack A stack of activation records. • Each

11/18/2015

11

Copyright © 2006 The McGraw-Hill Companies, Inc.

Dynamic Chain and Local Offset

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, whose

address is in the EP. This offset is called the

local_offset

The local_offset of a local variable can be determined

by the compiler at compile time

Concepts of Programming Languages, 9th ed., by Robert W. Sebesta. Addison Wesley, 2010. 9-23

Copyright © 2006 The McGraw-Hill Companies, Inc.

Nested Subprograms

Some non-C-based static-scoped languages (e.g., Fortran 95, Ada, Python, JavaScript, Ruby, and Lua) use stack-dynamic local variables and allow subprograms to be nested

All variables that can be non-locally accessed reside in some activation record instance in the stack

The process of locating a non-local reference:

1. Find the correct activation record instance

2. Determine the correct offset within that activation record instance

Concepts of Programming Languages, 9th ed., by Robert W. Sebesta. Addison Wesley, 2010. 9-24

Page 12: Programming Languages Chapter 2: Syntax4430s001/functions.pdf11/18/2015 8 Copyright © 2006 The McGraw-Hill Companies, Inc. 9.7 Run Time Stack A stack of activation records. • Each

11/18/2015

12

Copyright © 2006 The McGraw-Hill Companies, Inc.

Example Ada Programprocedure Main_2 is

X : Integer;

procedure Bigsub is

A, B, C : Integer;

procedure Sub1 is

A, D : Integer;

begin -- of Sub1

A := B + C; <-----------------------1

end; -- of Sub1

procedure Sub2(X : Integer) is

B, E : Integer;

procedure Sub3 is

C, E : Integer;

begin -- of Sub3

Sub1;

E := B + A: <--------------------2

end; -- of Sub3

begin -- of Sub2

Sub3;

A := D + E; <-----------------------3

end; -- of Sub2

begin -- of Bigsub

Sub2(7);

end; -- of Bigsub

begin

Bigsub;

end; -- of Main_2 Concepts of Programming Languages, 9th ed., by Robert W. Sebesta. Addison Wesley, 2010. 9-25

Copyright © 2006 The McGraw-Hill Companies, Inc.

Locating a Non-local Reference

Finding the offset is easy

Finding the correct activation record instance

– Static semantic rules guarantee that all non-

local variables that can be referenced have been

allocated in some activation record instance

that is on the stack when the reference is made

Concepts of Programming Languages, 9th ed., by Robert W. Sebesta. Addison Wesley, 2010. 9-26

Page 13: Programming Languages Chapter 2: Syntax4430s001/functions.pdf11/18/2015 8 Copyright © 2006 The McGraw-Hill Companies, Inc. 9.7 Run Time Stack A stack of activation records. • Each

11/18/2015

13

Copyright © 2006 The McGraw-Hill Companies, Inc.

Static Scoping

A static chain is a chain of static links that connects certain activation record instances

The static link in an activation record instance for subprogram A points to one of the activation record instances of A's static parent

The static chain from an activation record instance connects it to all of its static ancestors

The static chain must be modified for each for each subprogram call and return.

Static_depth is an integer associated with a static scope whose value is the depth of nesting of that scope

Concepts of Programming Languages, 9th ed., by Robert W. Sebesta. Addison Wesley, 2010. 9-27

Copyright © 2006 The McGraw-Hill Companies, Inc.

Example Ada Programprocedure Main_2 is

X : Integer;

procedure Bigsub is

A, B, C : Integer;

procedure Sub1 is

A, D : Integer;

begin -- of Sub1

A := B + C; <-----------------------1

end; -- of Sub1

procedure Sub2(X : Integer) is

B, E : Integer;

procedure Sub3 is

C, E : Integer;

begin -- of Sub3

Sub1;

E := B + A: <--------------------2

end; -- of Sub3

begin -- of Sub2

Sub3;

A := D + E; <-----------------------3

end; -- of Sub2

begin -- of Bigsub

Sub2(7);

end; -- of Bigsub

begin

Bigsub;

end; -- of Main_2 Concepts of Programming Languages, 9th ed., by Robert W. Sebesta. Addison Wesley, 2010. 9-28

Page 14: Programming Languages Chapter 2: Syntax4430s001/functions.pdf11/18/2015 8 Copyright © 2006 The McGraw-Hill Companies, Inc. 9.7 Run Time Stack A stack of activation records. • Each

11/18/2015

14

Copyright © 2006 The McGraw-Hill Companies, Inc.

Static Scoping (continued)

The chain_offset or nesting_depth of a nonlocal reference is the difference between the static_depth of the reference and that of the scope when it is declared

A reference to a variable can be represented by the pair:

(chain_offset, local_offset),

where local_offset is the offset in the activation

record of the variable being referenced

Concepts of Programming Languages, 9th ed., by Robert W. Sebesta. Addison Wesley, 2010. 9-29

Copyright © 2006 The McGraw-Hill Companies, Inc.

Example Ada Programprocedure Main_2 is

X : Integer;

procedure Bigsub is

A, B, C : Integer;

procedure Sub1 is

A, D : Integer;

begin -- of Sub1

A := B + C; <-----------------------1

end; -- of Sub1

procedure Sub2(X : Integer) is

B, E : Integer;

procedure Sub3 is

C, E : Integer;

begin -- of Sub3

Sub1;

E := B + A: <--------------------2

end; -- of Sub3

begin -- of Sub2

Sub3;

A := D + E; <-----------------------3

end; -- of Sub2

begin -- of Bigsub

Sub2(7);

end; -- of Bigsub

begin

Bigsub;

end; -- of Main_2 Concepts of Programming Languages, 9th ed., by Robert W. Sebesta. Addison Wesley, 2010. 9-30

Page 15: Programming Languages Chapter 2: Syntax4430s001/functions.pdf11/18/2015 8 Copyright © 2006 The McGraw-Hill Companies, Inc. 9.7 Run Time Stack A stack of activation records. • Each

11/18/2015

15

Copyright © 2006 The McGraw-Hill Companies, Inc.

Example Ada Program (continued)

Call sequence for Main_2

Main_2 calls Bigsub

Bigsub calls Sub2

Sub2 calls Sub3

Sub3 calls Sub1

Concepts of Programming Languages, 9th ed., by Robert W. Sebesta. Addison Wesley, 2010. 9-31

Copyright © 2006 The McGraw-Hill Companies, Inc.

10-32

Stack Contents at Position 1procedure Main_2 is

X : Integer;

procedure Bigsub is

A, B, C : Integer;

procedure Sub1 is

A, D : Integer;

begin -- of Sub1

A := B + C; <-----------------1

end; -- of Sub1

procedure Sub2(X : Integer) is

B, E : Integer;

procedure Sub3 is

C, E : Integer;

begin -- of Sub3

Sub1;

E := B + A: <--------------2

end; -- of Sub3

begin -- of Sub2

Sub3;

A := D + E; <-----------------3

end; -- of Sub2

begin -- of Bigsub

Sub2(7);

end; -- of Bigsub

begin

Bigsub;

end; -- of Main_2

Concepts of Programming Languages, 9th ed., by Robert W. Sebesta. Addison Wesley, 2010. 9-32

Page 16: Programming Languages Chapter 2: Syntax4430s001/functions.pdf11/18/2015 8 Copyright © 2006 The McGraw-Hill Companies, Inc. 9.7 Run Time Stack A stack of activation records. • Each

11/18/2015

16

Copyright © 2006 The McGraw-Hill Companies, Inc.

Static Chain Maintenance

At the call,

- The activation record instance must be built

- The dynamic link is just the old stack base

pointer

- The static link must point to the most recent ARI

of the static parent

- Two methods:

1. Search the dynamic chain at runtime

2. Treat subprogram calls and

definitions like variable references

and definitions – store information about

subprograms at compile time.Concepts of Programming Languages, 9th ed., by Robert W. Sebesta. Addison Wesley, 2010. 9-33

Copyright © 2006 The McGraw-Hill Companies, Inc.

Evaluation of Static Chains

Problems:

1. A nonlocal reference is slow if the

nesting depth is large

2. Time-critical code is difficult:

a. Costs of nonlocal references are

difficult to determine

b. Code changes can change the

nesting depth, and therefore the cost

Concepts of Programming Languages, 9th ed., by Robert W. Sebesta. Addison Wesley, 2010. 9-34

Page 17: Programming Languages Chapter 2: Syntax4430s001/functions.pdf11/18/2015 8 Copyright © 2006 The McGraw-Hill Companies, Inc. 9.7 Run Time Stack A stack of activation records. • Each

11/18/2015

17

Copyright © 2006 The McGraw-Hill Companies, Inc.

Displays

An alternative to static chains that solves

the problems with that approach

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 activation record instances

Concepts of Programming Languages, 9th ed., by Robert W. Sebesta. Addison Wesley, 2010. 9-35

Copyright © 2006 The McGraw-Hill Companies, Inc.

Displays (continued)

Represent references as

(display offset, local_offset)

where display offset is the same as static

depth (0 for outermost enclosing block,

etc.)

Mechanics of references:

– Use the display_offset to get the pointer into

the display to the ARI with the variable

– Use the local_offset to get to the variable within

the ARIConcepts of Programming Languages, 9th ed., by Robert W. Sebesta. Addison Wesley, 2010. 9-36

Page 18: Programming Languages Chapter 2: Syntax4430s001/functions.pdf11/18/2015 8 Copyright © 2006 The McGraw-Hill Companies, Inc. 9.7 Run Time Stack A stack of activation records. • Each

11/18/2015

18

Copyright © 2006 The McGraw-Hill Companies, Inc.

10-31

Implementing Dynamic Scoping

Deep Access: non-local references are found by searching the activation record instances on the dynamic chain- Length of the chain cannot be statically

determined- Every activation record instance must

have variable names

Shallow Access: put locals in a central place

– One stack (called a hidden stack) for each variable name

– Central referencing environment table with an entry for each variable name

Concepts of Programming Languages, 9th ed., by Robert W. Sebesta. Addison Wesley, 2010. 9-39

Copyright © 2006 The McGraw-Hill Companies, Inc.

Using Deep Access to Implement Dynamic Scoping

Concepts of Programming Languages, 9th ed., by Robert W. Sebesta. Addison Wesley, 2010. 9-40

Page 19: Programming Languages Chapter 2: Syntax4430s001/functions.pdf11/18/2015 8 Copyright © 2006 The McGraw-Hill Companies, Inc. 9.7 Run Time Stack A stack of activation records. • Each

11/18/2015

19

Copyright © 2006 The McGraw-Hill Companies, Inc.

10-32

Using Shallow Access to Implement Dynamic Scoping

(defun MAIN_6 ()

(prog (v u) …

(defun A ()

(prog (v w) …

(defun B ()

(prog (w x) …

(defun C ()

(prog (x z)

(setq x (+ u v))

MAIN_6 calls A

A calls A

A calls B

B calls C

Concepts of Programming Languages, 9th ed., by Robert W. Sebesta. Addison Wesley, 2010. 9-41