lecture #17, march 12, 2007

30
Cse321, Programming Languages and Compilers 1 03/22/22 Lecture #17, March 12, 2007 Procedure Abstraction, Name Spaces, Scoping Rules, Activation Records, Object Oriented Languages, Parameter Passing, Returning Values.

Upload: samson-hansen

Post on 31-Dec-2015

21 views

Category:

Documents


2 download

DESCRIPTION

Lecture #17, March 12, 2007. Procedure Abstraction, Name Spaces, Scoping Rules, Activation Records, Object Oriented Languages, Parameter Passing, Returning Values. Notices. Reading Assignment Read Chapter 6 “The Procedure Abstraction” Pages 251 – 306 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

104/19/23

Lecture #17, March 12, 2007

•Procedure Abstraction,

•Name Spaces,

•Scoping Rules,

•Activation Records,

•Object Oriented Languages,

•Parameter Passing,

•Returning Values.

Page 2: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

204/19/23

Notices• Reading Assignment

– Read Chapter 6 “The Procedure Abstraction”

– Pages 251 – 306

• The Final Exam will be Monday, March 19, 2007.

– Monday, Mar. 19, 2007. Time: 1930 – 2120 (7:30pm – 9:20pm).

– It will NOT start at about 6:00 pm like our regular class meeting.

– I have no control over this.

• Project 3 is due Monday, March 19. I will accept projects until midnight. I must grade exams and projects and get all grades in before I leave on Thursday. So late projects will not be accepted.

Page 3: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

304/19/23

Roles of Procedures

• Control Abstraction– Call and Return

– Depends upon call;ing conventions

» All parties must agree

» Supports separate compilation

• Name Space Management– Separate private name space for each procedure

– Allocation of data is automatic

• External Interface– Name scoping

– Addressability

– Libraries

– Interface with operating system

Page 4: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

404/19/23

Purpose of Procedures

• Create Name spaces and variables• Maps variables onto virtual addresses

– (The operating system maps virtual addresses to physical addresses)

• Establish rules for visibility• Break large systems into smaller

manageable pieces– (Linkers and loaders compose them together)

• Lays out memory

Page 5: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

504/19/23

Control Abstraction• Procedures are a common form of control

abstraction– Call – return– The control flow of a program can be described by a graph of what

procedures can call what other procedures.– A procedure may never return

» Non-termination» Using some other control abstraction to escape

• Other forms of control abstractions– Goto– Break– Co-routines– Exceptions– Continuations

Page 6: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

604/19/23

Name Spaces

• Name space supports a set of names and objects bound to those names

– Variables

– Types

– Labels

• A name space has scope – The region within the program text where the names in a name

space are visible

– Outside of the scope the name and its object are unreachable

• Some languages have several independent name spaces, with incommensurate scopes

– In ML values (functions and variables) and Types live in independent name spaces

– In Fortran labels and variables live in independent name spaces

Page 7: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

704/19/23

Nested Lexical Scopes

• Name spaces can be nested.• Names in an enclosing scope are visible in

the inner scope.– A name refers to its lexically closest declaration

– The most recent enclosing scope

• Many languages support some sort of nested scopes.

• Names in lexically scoped languages can be uniquely determined by a 2 place coordinate.

– (scope, position within that scope)

Page 8: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

804/19/23

Scoping Rules

Every Language has its own scoping rules

• Fortran – 2 levels global & local• Scheme – 1 global, nested lets• ML – structures (libraries) unbounded

nested scopes (functions, let, anonymous functions)

• C – Global, file limited, local procedural, nested blocks

• Java – Global classes, packages, local method scope

Page 9: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

904/19/23

FortranGlobal scope

Foo

variables

parameters

labels

Common Block

data

Bar

variables

parameters

labels

Page 10: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

1004/19/23

CGlobal scope

int a, b

File scope

static int x,y

int Bar()

variables

parameters

labels

File scope

Block

int Foo()

variables

int Foo()

block

block

block

Page 11: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

1104/19/23

Scheme

map

foo

cond

head

cons

let

let

let

Page 12: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

1204/19/23

ML

Struct a =

end

fun foo

datatypefun bar

fun deep

let

let

let

fun baz

(fn x =>

(fn y =>

Page 13: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

1304/19/23

JavaPublic classes

Package A

Class M

static int x

Package B

Method

Parameters

Local vars

class N

Package C

Page 14: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

1404/19/23

Dynamic Scopingfun map f x = if null x then [ ] else (f (hd x))::(map f (tl x))

fun add x = (fn y => y + x)map (add 5) [0,0] = map (fn y => y + x) [0,0] = where x = 5

map (fn y => y + x) [0,0] = if null x where x = [0,0], then [ ] f = (fn y => y + x) else (f (hd x))::(map f (tl x))

((fn y => y + x) (hd x))::(map f (tl x))

Page 15: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

1504/19/23

Activation Records

• Created every time a procedure is called• Must be accessible to both the caller and

the callee• Allocates space for

– Parameters

– Local variables

– Return address

– Other links and pointers to provide access to non-local data

• Other issues– Initializing local variables

– Stack vs. heap allocated

– Optimizing activation records by coalescing

Page 16: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

1604/19/23

Creating Activation Records

• AR creation is a cooperative effort

• Shared by the caller and the callee

• Has 4 parts– Precall

– Postreturn

– Prolog

– Epilog

• Precall & Postreturn can be split

prolog

precall

postcall

epilog

prolog

epilog

Call

Return

Page 17: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

1704/19/23

Object Oriented Languages

• Control oriented around the data, not the procedures

• Name spaces are fundamentally different– Procedural languages are almost always lexically scoped

» Names linked to position in source of the text executing

– Object-oriented languages are object based

» Names are linked to dynamic objects

» The scope rules are linked to the class of the current object, not the lexical position of the code executing.

» subtyping & inheritance make the type of the “current object” impossible to determine in general

• Inheritance– Imposes an hierarchy on the structure of data

» Classes, superclasses, subclasses

– Classes definition is the mechanism to create the hierarchy

Page 18: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

1804/19/23

Terminology• Instance

– An object

• Object Record– Concrete representation – contains its members (or pointers)

• Instance Variable– A variable local to a single object

• Method– Code associated with an object

– Full fledged procedure (parameters, local variables, return values)

• Receiver– Methods are always invoked relative to some object. The receiver

» When activation of the method begins the receiver becomes the current object.

• Class– An object to describe the structure of other objects

• Class Variable– A variable with only one instance per class. Shared amongst all instances

Page 19: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

1904/19/23

Mapping Names to Methods

• Inheritance allows methods to be over-ridden

• Thus more than one set of executable instructions can have the same name.

• Method disambiguation follows the class hierarchy

• Start with the receiver (or current object)– If it has a method with the correct name, then use it

– If not look for a method with that name further up the hierarchy in the super class of the receiver

• Optimization.– All members of a class share the same methods

– Methods are usually implemented via indirection (pointers)

Page 20: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

2004/19/23

Exampleclass threeint nint fee(int n)bool fum()

class two extends threefloat xfloat yint fee(int n)float foe()

class one extends twoone zint fee(int n)float fie()

Page 21: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

2104/19/23

class three

fee

fum

class two

fee

foe

class one

fee

fie

Object

Obj a

X=2.0

Y=0.1

Z =

N =1

Obj c

X=5.0

Y=3.1

N =1

Obj b

X=5.0

Y=3.0

Z =

N =1

Page 22: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

2204/19/23

class three

fee

fum

Object

class two

fee

fum

foe class one

fee

fum

foe

fie

fum

fee

fee

foe

fee

fie

Obj c

X=5.0

Y=3.1

N =1

Obj a

X=2.0

Y=0.1

Z =

N =1

Obj b

X=5.0

Y=3.0

Z =

N =1

Page 23: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

2304/19/23

Communicating between procedures

• Common global variables– Simple, but prone to abuse

– Recursion becomes problematic

• Passing parameters– Call by value

– Call by reference

– Call by copy return

– Call by name

• Returning values

Page 24: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

2404/19/23

Addressability

• Base addresses (variables at a constant address)

• Indirection (pointers)• Stack based

– Push – pop

• Variables in activation records– Parameters

– Local variables

– Local variables of other procedures in enclosing scopes

– Global variables

• Access links– Static links

– Display

Page 25: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

2504/19/23

Static linksfun f(x)int y

fun g(z)int abegin h(z,3,5) end

fun h(a,b,c)int ibegin g(i) end

bool bbegin g(b) end Ap

register

x

Ret addr

Static link

y

f

dyn link

z

Ret addr

Static link

a

g

dyn link

a

Ret addr

Static link

i

b

ch

dyn link

Page 26: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

2604/19/23

Display inside ffun f(x)int y

fun g(z)int abegin h(z,3,5) end

fun h(a,b,c)int ibegin g(i) end

bool bbegin g(b) end

0 1 2 3

x

Ret addr

Static link

y

f

Old display

Page 27: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

2704/19/23

Display inside gfun f(x)int y

fun g(z)int abegin h(z,3,5) end

fun h(a,b,c)int ibegin g(i) end

bool bbegin g(b) end

0 1 2 3

x

Ret addr

Static link

y

f

Old display

z

Ret addr

Static link

a

g

display

Page 28: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

2804/19/23

Display inside h called from gfun f(x)int y

fun g(z)int abegin h(z,3,5) end

fun h(a,b,c)int ibegin g(i) end

bool bbegin g(b) end

0 1 2 3

x

Ret addr

Static link

y

f

Old display

z

Ret addr

Static link

a

g

display

a

Ret addr

Static link

i

b

ch

display

Page 29: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

2904/19/23

Costs

• Costs to access a variable– Static link

– Display

• Cost to maintain the structure– Static link

– Display

• Cache Costs

Page 30: Lecture #17,  March 12, 2007

Cse321, Programming Languages and Compilers

3004/19/23

Next-time

• Intro to what we’ll cover next semester• Summary of concepts possible on the final

exam• Help-session for project 3.