Download presentation
Presentation is loading. Please wait.
1
Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #17, March 12, 2007 Procedure Abstraction, Name Spaces, Scoping Rules, Activation Records, Object Oriented Languages, Parameter Passing, Returning Values.
2
Cse321, Programming Languages and Compilers 2 6/12/2015 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.
3
Cse321, Programming Languages and Compilers 3 6/12/2015 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
4
Cse321, Programming Languages and Compilers 4 6/12/2015 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
5
Cse321, Programming Languages and Compilers 5 6/12/2015 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
6
Cse321, Programming Languages and Compilers 6 6/12/2015 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
7
Cse321, Programming Languages and Compilers 7 6/12/2015 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)
8
Cse321, Programming Languages and Compilers 8 6/12/2015 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
9
Cse321, Programming Languages and Compilers 9 6/12/2015 Fortran Global scope Foo variables parameters labels Common Block data Bar variables parameters labels
10
Cse321, Programming Languages and Compilers 10 6/12/2015 C Global scope int a, b File scope static int x,y int Bar() variables parameters labels File scope Block int Foo() variables int Foo() block
11
Cse321, Programming Languages and Compilers 11 6/12/2015 Scheme map foo cond head cons let
12
Cse321, Programming Languages and Compilers 12 6/12/2015 ML Struct a = end fun foo datatype fun bar fun deep let fun baz (fn x => (fn y =>
13
Cse321, Programming Languages and Compilers 13 6/12/2015 Java Public classes Package A Class M static int x Package B Method Parameters Local vars class N Package C
14
Cse321, Programming Languages and Compilers 14 6/12/2015 Dynamic Scoping fun 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))
15
Cse321, Programming Languages and Compilers 15 6/12/2015 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
16
Cse321, Programming Languages and Compilers 16 6/12/2015 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
17
Cse321, Programming Languages and Compilers 17 6/12/2015 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
18
Cse321, Programming Languages and Compilers 18 6/12/2015 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
19
Cse321, Programming Languages and Compilers 19 6/12/2015 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)
20
Cse321, Programming Languages and Compilers 20 6/12/2015 Example class three int n int fee(int n) bool fum() class two extends three float x float y int fee(int n) float foe() class one extends two one z int fee(int n) float fie()
21
Cse321, Programming Languages and Compilers 21 6/12/2015 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
22
Cse321, Programming Languages and Compilers 22 6/12/2015 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
23
Cse321, Programming Languages and Compilers 23 6/12/2015 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
24
Cse321, Programming Languages and Compilers 24 6/12/2015 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
25
Cse321, Programming Languages and Compilers 25 6/12/2015 Static links fun f(x) int y fun g(z) int a begin h(z,3,5) end fun h(a,b,c) int i begin g(i) end bool b begin 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 c h dyn link
26
Cse321, Programming Languages and Compilers 26 6/12/2015 Display inside f fun f(x) int y fun g(z) int a begin h(z,3,5) end fun h(a,b,c) int i begin g(i) end bool b begin g(b) end 0 1 2 3 x Ret addr Static link y f Old display
27
Cse321, Programming Languages and Compilers 27 6/12/2015 Display inside g fun f(x) int y fun g(z) int a begin h(z,3,5) end fun h(a,b,c) int i begin g(i) end bool b begin g(b) end 0 1 2 3 x Ret addr Static link y f Old display z Ret addr Static link a g display
28
Cse321, Programming Languages and Compilers 28 6/12/2015 Display inside h called from g fun f(x) int y fun g(z) int a begin h(z,3,5) end fun h(a,b,c) int i begin g(i) end bool b begin 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 c h display
29
Cse321, Programming Languages and Compilers 29 6/12/2015 Costs Costs to access a variable –Static link –Display Cost to maintain the structure –Static link –Display Cache Costs
30
Cse321, Programming Languages and Compilers 30 6/12/2015 Next-time Intro to what we’ll cover next semester Summary of concepts possible on the final exam Help-session for project 3.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.