cse321, programming languages and compilers 1 6/12/2015 lecture #17, march 12, 2007 procedure...
Post on 19-Dec-2015
215 views
TRANSCRIPT
Cse321, Programming Languages and Compilers
104/18/23
Lecture #17, March 12, 2007
•Procedure Abstraction,
•Name Spaces,
•Scoping Rules,
•Activation Records,
•Object Oriented Languages,
•Parameter Passing,
•Returning Values.
Cse321, Programming Languages and Compilers
204/18/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.
Cse321, Programming Languages and Compilers
304/18/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
Cse321, Programming Languages and Compilers
404/18/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
Cse321, Programming Languages and Compilers
504/18/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
Cse321, Programming Languages and Compilers
604/18/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
Cse321, Programming Languages and Compilers
704/18/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)
Cse321, Programming Languages and Compilers
804/18/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
Cse321, Programming Languages and Compilers
904/18/23
FortranGlobal scope
Foo
variables
parameters
labels
Common Block
data
Bar
variables
parameters
labels
Cse321, Programming Languages and Compilers
1004/18/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
Cse321, Programming Languages and Compilers
1204/18/23
ML
Struct a =
end
fun foo
datatypefun bar
fun deep
let
let
let
fun baz
(fn x =>
(fn y =>
Cse321, Programming Languages and Compilers
1304/18/23
JavaPublic classes
Package A
Class M
static int x
Package B
Method
Parameters
Local vars
class N
Package C
Cse321, Programming Languages and Compilers
1404/18/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))
Cse321, Programming Languages and Compilers
1504/18/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
Cse321, Programming Languages and Compilers
1604/18/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
Cse321, Programming Languages and Compilers
1704/18/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
Cse321, Programming Languages and Compilers
1804/18/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
Cse321, Programming Languages and Compilers
1904/18/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)
Cse321, Programming Languages and Compilers
2004/18/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()
Cse321, Programming Languages and Compilers
2104/18/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
Cse321, Programming Languages and Compilers
2204/18/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
Cse321, Programming Languages and Compilers
2304/18/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
Cse321, Programming Languages and Compilers
2404/18/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
Cse321, Programming Languages and Compilers
2504/18/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
Cse321, Programming Languages and Compilers
2604/18/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
Cse321, Programming Languages and Compilers
2704/18/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
Cse321, Programming Languages and Compilers
2804/18/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
Cse321, Programming Languages and Compilers
2904/18/23
Costs
• Costs to access a variable– Static link
– Display
• Cost to maintain the structure– Static link
– Display
• Cache Costs