1 / 30

Lecture #17, March 12, 2007

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

Download Presentation

Lecture #17, March 12, 2007

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture #17, March 12, 2007 • Procedure Abstraction, • Name Spaces, • Scoping Rules, • Activation Records, • Object Oriented Languages, • Parameter Passing, • Returning Values.

  2. 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. 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. 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. 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. 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. 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. 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. Fortran Global scope Common Block data Bar variables parameters labels Foo variables parameters labels

  10. C Global scope int a, b File scope int Foo() File scope static int x,y block block int Bar() variables parameters labels block Block int Foo() variables

  11. Scheme map cons cond foo head let let let

  12. Struct a = end fun deep datatype let fun foo let let ML fun bar fun baz (fn x => (fn y =>

  13. Java Public classes Package B Package A Class M static int x Method Parameters Local vars Package C class N

  14. 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. 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. prolog Call prolog precall postcall Return epilog epilog 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

  17. 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. 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. 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. 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. Obj c class three N =1 fee X=5.0 fum Y=3.1 class two fee foe class one fee Obj a Obj b fie N =1 N =1 X=2.0 X=5.0 Y=0.1 Y=3.0 Z = Z = Object 

  22. Obj c class three N =1 fee X=5.0 fum Y=3.1 class two fee fum foe class one fee Obj a Obj b fum N =1 N =1 foe X=2.0 X=5.0 fie Y=0.1 Y=3.0 Z = Z = Object fee … fee … fum … fee … foe … fie … 

  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

  24. 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. f x Ret addr Static link dyn link y g z Ret addr h Static link c dyn link b a a Ret addr Static link dyn link i 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

  26. f x Ret addr Static link Old display y Display inside f 2 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 1 3 0  

  27. f x Ret addr Static link Old display y g z Ret addr Static link display  a Display inside g 2 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 1 3 0

  28. f x Ret addr Static link Old display y g z Ret addr h Static link c display  b a a Ret addr Static link display i Display inside h called from g 2 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 1 3 0

  29. Costs • Costs to access a variable • Static link • Display • Cost to maintain the structure • Static link • Display • Cache Costs

  30. Next-time • Intro to what we’ll cover next semester • Summary of concepts possible on the final exam • Help-session for project 3.

More Related