Download Procedure Abstraction - Compiler Design - Lecture Slides and more Slides Computer Science in PDF only on Docsity!
Lecture #17, March 12, 2007
- Procedure Abstraction,
- Name Spaces,
- Scoping Rules,
- Activation Records,
- Object Oriented Languages,
- Parameter Passing,
- Returning Values.
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
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
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
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
Fortran
Global scope
Foo variables
parameters
labels
Common Block data
Bar
variables
parameters
labels
Scheme
map
foo
cond
head
cons
let let let
ML
Struct a =
end
fun foo
datatype
fun bar
fun deep
let let let
fun baz
(fn x => (fn y =>
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))
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
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
Terminology
- Instance
- 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 Docsity.com
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()
class three fee fum
class two fee foe
class one fee fie
Object
⊥
Obj a
X=2. Y=0. Z =
N =
Obj c
X=5. Y=3.
N =
Obj b
X=5. Y=3. Z =
N =
Docsity.com