The Procedure Abstraction, Part V: Support for OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

Lecture 10: Part 1: OO Issues CS 540 George Mason University.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Chapter 12 Support for Object-Oriented Programming.
Chapter 12: Support for Object-Oriented Programming
Code Shape IV Procedure Calls & Dispatch Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #17, March 12, 2007 Procedure Abstraction, Name Spaces, Scoping Rules, Activation Records,
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
CS 536 Spring Run-time organization Lecture 19.
The Procedure Abstraction Part III: Allocating Storage & Establishing Addressability Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
The Procedure Abstraction Part IV: Run-time Structures for OOLs Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
The Procedure Abstraction Part I: Basics Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
Run time vs. Compile time
Semantics of Calls and Returns
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 12 Topics Introduction Object-Oriented Programming.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
ISBN Chapter 12 Support for Object-Oriented Programming.
The Procedure Abstraction Part I: Basics Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
ISBN Chapter 10 Implementing Subprograms.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
The Procedure Abstraction, Part VI: Inheritance in OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
Procedure Abstraction VI Run-time structures for OOLs ( II ) and Wrap-up Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
Basic Semantics Associating meaning with language entities.
1 CIS 461 Compiler Design and Construction Winter 2012 Lecture-Module #16 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon.
ISBN Chapter 10 Implementing Subprograms.
COMP3190: Principle of Programming Languages
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
The Procedure Abstraction, Part IV Addressability Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Chapter 12 Support for Object-Oriented Programming.
Object-Oriented Programming Chapter Chapter
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
ISBN Object-Oriented Programming Chapter Chapter
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
The Procedure Abstraction Part III: Symbol Tables, Storage Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
ISBN Chapter 12 Support for Object-Oriented Programming.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Advanced Programming in C
Name Spaces: ALL versus OOL
Names and Attributes Names are a key programming language feature
Abstract Data Types and Encapsulation Concepts
The Procedure Abstraction Part III: Symbol Tables, Storage
The Procedure Abstraction Part IV: Allocating Storage & Establishing Addressability Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights.
Abstract Data Types and Encapsulation Concepts
Abstract Data Types and Encapsulation Concepts
Scope, Visibility, and Lifetime
The Procedure Abstraction Part V: Run-time Structures for OOLs
The Procedure Abstraction Part I: Basics
PZ09A - Activation records
Code Shape IV Procedure Calls & Dispatch
Support for Object-Oriented Programming
Support for Object-Oriented Programming
Lecture 13: The Procedure Abstraction; Run-Time Storage Organisation
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Lecture 10 Concepts of Programming Languages
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Presentation transcript:

The Procedure Abstraction, Part V: Support for OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit permission to make copies of these materials for their personal use. Faculty from other educational institutions may use these materials for nonprofit educational purposes, provided this copyright notice is preserved. COMP 412 FALL 2010

Comp 412, Fall What about Object-Oriented Languages? What is an OOL? A language that supports “object-oriented programming” How does an OOL differ from an ALL? (A LGOL -Like Language) Data-centric name scopes for values & functions Dynamic resolution of names to their implementations How do we compile OOLs ? Need to define what we mean by an OOL Term is almost meaningless today — —Smalltalk to C++ to Java We will focus on Java and C++ Differences from an ALL lie in naming and addressability OOL ≅ Object-Oriented Language ALL ≅ Algol-Like Language

What Are The Issues? In an ALL, the compiler needs Compile-time mechanism for name resolution Runtime mechanism to compute an address from a name Compiler must emit code that builds & maintains the runtime structures for addressability Comp 412, Fall In an OOL, the compiler needs Compile-time mechanism for name resolution Runtime mechanism to compute an address from a name Compiler must emit code that builds & maintains the runtime structures for addressability This lecture focuses on these three mechanisms. The issue of code generation for a method call is discussed in a later lecture. This lecture focuses on these three mechanisms. The issue of code generation for a method call is discussed in a later lecture. *

Comp 412, Fall What is an Object? An object is an abstract data type that encapsulates data, operations and internal state behind a simple, consistent interface. Elaborating the concepts: Each object has internal state —Data members are static (lifetime of object ) —External access is through code members Each object has a set of associated procedures, or methods —Some methods are public, others are private —Locating a procedure by name is more complex than in an ALL Object’s internal state leads to complex behavior Data Code Data Code x Data Code Data Code y Data Code Data Code z The Concept: Data members, variables Code members, or methods

Comp 412, Fall OOLs & the Procedure Abstraction What is the shape of an OOL’s name space? Local storage in objects ( both public & private ) Storage defined in methods ( they are procedures ) —Local values inside a method —Static values with lifetimes beyond methods Methods shared among multiple objects Global name space for global objects and ( some? ) code Classes Objects with the same state are grouped into a class —Same code, same data, same naming environment —Class members are static & shared among instances of the class Allows abstraction-oriented naming Should foster code reuse in both source & implementation In some OOLs, everything is an object. In others, variables co-exist with objects & inside objects. members

Comp 412, Fall Implementing Object-Oriented Languages So, what can an executing method access? Names defined by the method —And its surrounding lexical context The receiving object’s data members —Smalltalk terminology: instance variables The code & data members of the class that defines it —And its context from inheritance —Smalltalk terminology: class variables and methods Any object defined in the global name space The method might need the address for any of these objects An OOL resembles an ALL, with a wildly different name space Scoping is relative to hierarchy in the code of an ALL Scoping is relative to hierarchy in the data of an ALL The fundamental question Inheritance adds some twists.

Comp 412, Fall Concrete Example: The Java Name Space Code within a method M for object O of class C can see: Local variables declared within M ( lexical scoping ) All instance variables & class variables of C All public and protected variables of any superclass of C Classes defined in the same package as C or in any explicitly imported package —public class variables and public instance variables of imported classes —package class and instance variables in the package containing C Class declarations can be nested! —These member declarations hide outer class declarations of the same name (lexical scoping) —Accessibility: public, private, protected, package Superclass is an ancestor in the inheritance hierarchy Both lexical nesting & class hierarchy at play lexical class hierarchy

Comp 412, Fall The Java Name Space Class Point { public int x, y; public void draw(); } Class ColorPoint extends Point { // inherits x, y, & draw() from Point Color c; // local data public void draw() {…} // override (hide) Point’s draw public void test() { y = x; draw(); } // local code } Class C { // independent of Point & ColorPoint int x, y;// local data public void m() // local code { Point p = new ColorPoint(); // uses ColorPoint, and, by inheritance y = p.x; // the definitions from Point p.draw(); } We will use and extend this example

Comp 412, Fall Java Symbol Tables To compile method M of object O in class C, the compiler needs: Lexically scoped symbol table for the current block and its surrounding scopes —Just like ALL — inner declarations hide outer declarations Chain of symbol tables for inheritance —Class C and all of its superclasses —Need to find methods and instance variables in any superclass Symbol tables for all global classes (package scope) —Entries for all members with visibility —Need to construct symbol tables for imported packages and link them into the structure in appropriate places Three sets of tables for name resolution In an ALL, we could combine 1 & 3 for a single unified set of tables. Three sets of tables for name resolution In an ALL, we could combine 1 & 3 for a single unified set of tables.

OOL Symbol Tables Conceptually Comp 412, Fall Lexical HierarchyClass Hierarchy Global Scope Search Order: lexical, class, global

Comp 412, Fall Java Symbol Tables To find the address for a reference to x in method M for an object O of class C, the compiler must: For an unqualified use (i.e., x): —Search the symbol table for the method’s lexical hierarchy —Search the symbol tables for the receiver’s class hierarchy —Search global symbol table (current package and imported) —In each case check visibility attribute of x For a qualified use (i.e.: Q.x): —Find Q by the method above —Search from Q for x  Must be a class or instance variable of Q or some class it extends —Check visibility attribute of x Again, the “sheaf of tables” implementation makes simplifies the conceptual picture.

What Are The Issues? In an ALL, the compiler needs Compile-time mechanism for name resolution Runtime mechanism to compute an address from a name Compiler must emit code that builds & maintains the runtime structures for addressability Comp 412, Fall In an OOL, the compiler needs Compile-time mechanism for name resolution Runtime mechanism to compute an address from a name Compiler must emit code that builds & maintains the runtime structures for addressability ✓ ➙ We need both a representation for each object and a mechanism to establish addressability of each object and its various members

Runtime Structures for OOLs Object lifetimes are independent Each object needs an object record (OR) to hold its state —Independent allocation and deallocation Classes are objects, too —ORs of classes instantiate the class hierarchy Object Records Static private storage for members Need fast, consistent access —Known constant offsets from OR pointer Provision for initialization Comp 412, Fall of method lifetimes, of lifetimes of other objects … fee() fie() foe() count x x The Concept

Object Record Layout Assume a Fixed-size OR Data members are at known fixed offsets from OR pointer Code members occur only in objects of class “class” —Code vector is a data-member of the class —Method pointers are at known fixed offsets in the code vector —Method-local storage kept in method’s AR, as in an ALL Variable-sized members ⇒ store descriptor to space in heap Locating ORs For a receiver, the OR pointer is implicit For a receiver’s class, the receiver’s OR has a class pointer Top-level classes and static classes can be accessed by name —Mangle the class name & use it as a relocatable symbol —Handle nested classes as we would nested blocks in an ALL Comp 412, Fall

What About Inheritance? Impact on OR Layout OR needs slots for each member declared, all the way up the class hierarchy (class, superclass, super-superclass, …) Can use prefixing of storage to lay out the OR Back to Our Java Example — Class Point Comp 412, Fall Class Point { public int x, y; … } Class ColorPoint extends Point { Color c; … } Take the word extends literally. What happens if we cast a ColorPoint to a Point?

Open World versus Closed World Prefixing assumes that the class structure is known when layout is performed. Two common cases occur. Closed-World Assumption ( Compile time) Class structure is known and closed prior to runtime Can lay out ORs in the compiler and/or the linker Open-World Assumption ( Interpreter or JIT ) Class structure can change at runtime Cannot lay out ORs until they are allocated —Walk class hierarchy at allocation C++ has a closed class structure. Java as an open class structure. Comp 412, Fall