What about Object-Oriented Languages?

Similar documents
Runtime Support for OOLs Object Records, Code Vectors, Inheritance Comp 412

The Procedure Abstraction

Naming in OOLs and Storage Layout Comp 412

Runtime Support for Algol-Like Languages Comp 412

Object Oriented Languages. Hwansoo Han

CSE 504: Compiler Design. Runtime Environments

Procedure and Function Calls, Part II. Comp 412 COMP 412 FALL Chapter 6 in EaC2e. target code. source code Front End Optimizer Back End

Code Shape Comp 412 COMP 412 FALL Chapters 4, 5, 6 & 7 in EaC2e. source code. IR IR target. code. Front End Optimizer Back End

Optimizer. Defining and preserving the meaning of the program

Data Abstraction. Hwansoo Han

CPS 506 Comparative Programming Languages. Programming Language

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

Procedure and Object- Oriented Abstraction

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Chapter 6 Introduction to Defining Classes

Programming II (CS300)

Introduction to Programming Using Java (98-388)

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

The Procedure Abstraction Part I: Basics

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

ECE 3574: Dynamic Polymorphism using Inheritance


Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Names, Scopes, and Bindings II. Hwansoo Han

Implementing Subprograms

Object-Oriented Design

Chapter 1: Object-Oriented Programming Using C++

CSCI312 Principles of Programming Languages!

Subtyping (Dynamic Polymorphism)

10. Abstract Data Types

CSE 504: Compiler Design. Intermediate Representations Symbol Table

12/4/18. Outline. Implementing Subprograms. Semantics of a subroutine call. Storage of Information. Semantics of a subroutine return

CMSC 132: Object-Oriented Programming II

Run-time Environments

Chapter 4 Java Language Fundamentals

Run-time Environments

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

CSE 307: Principles of Programming Languages

Programming II (CS300)

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

More On inheritance. What you can do in subclass regarding methods:

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Programmiersprachen (Programming Languages)

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Names, Bindings, Scopes

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

The Java Programming Language

Chapter 5 Object-Oriented Programming

Java Object Oriented Design. CSC207 Fall 2014

Chapter 5. Names, Bindings, and Scopes

OBJECT ORIENTED PROGRAMMING USING C++

2.4 Structuring programs

Object-oriented Programming. Object-oriented Programming

Francesco Nidito. Programmazione Avanzata AA 2007/08

Francesco Nidito. Programmazione Avanzata AA 2007/08

Implementing Subroutines. Outline [1]

Scope. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

Forth Meets Smalltalk. A Presentation to SVFIG October 23, 2010 by Douglas B. Hoffman

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Lecture 23: Object Lifetime and Garbage Collection

Concepts Introduced in Chapter 7

Object typing and subtypes

OBJECT ORİENTATİON ENCAPSULATİON

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Abstract Data Types and Encapsulation Concepts

The compilation process is driven by the syntactic structure of the program as discovered by the parser

1 Shyam sir JAVA Notes

Comp 311 Principles of Programming Languages Lecture 21 Semantics of OO Languages. Corky Cartwright Mathias Ricken October 20, 2010

Lecture Notes on Programming Languages

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language


The Procedure Abstraction Part I Basics

Properties of an identifier (and the object it represents) may be set at

CSE 307: Principles of Programming Languages

Today's Topics. CISC 458 Winter J.R. Cordy

CSE 401/M501 Compilers

Object-Oriented Design

Implementing Subprograms

Object Oriented Programming. Solved MCQs - Part 2

Self-review Questions

Practice for Chapter 11

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Programming Languages

CSCE3193: Programming Paradigms

Inheritance -- Introduction

CMSC 132: Object-Oriented Programming II

Programming Languages Third Edition. Chapter 7 Basic Semantics

Chapter 5 Names, Binding, Type Checking and Scopes

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Declarations and Access Control SCJP tips

A Short Summary of Javali

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Fundamental Concepts and Definitions

Week 5-1: ADT Design

Concept as a Generalization of Class and Principles of the Concept-Oriented Programming

Transcription:

What about Object-Oriented Languages? What is an OOL? A language that supports object-oriented programming How does an OOL differ from an ALL? (ALGOL-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 1

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 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 2 *

What is an Object? The Concept: An object is an abstract data type that encapsulates data, operations and internal state behind a simple, consistent interface. x Data Code Data Code 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 y z Data Code Data members, variables Code members, or methods 3

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 In some OOLs, everything is an object. In others, variables co-exist with objects & inside objects. 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 4

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 fundamental question The method might need the address for any of these objects Inheritance adds some twists. 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 5

class hierarchy lexical 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 Both lexical nesting & class hierarchy at play Superclass is an ancestor in the inheritance hierarchy 6

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

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. 8

OOL Symbol Tables Conceptually Lexical Hierarchy Class Hierarchy Global Scope Search Order: lexical, class, global 9

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. 10

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 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 11

Runtime Structures for OOLs Object lifetimes are independent of method lifetimes, of lifetimes of other objects 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 x The Concept fee() fie() foe() count 0 4 8 12 12

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 record in the symbol table, 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 symbol Handle nested classes as we would nested blocks in an ALL 13

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 Class Point { public int x, y; } self OR for a Point class x y Class ColorPoint extends Point { Color c; } What happens if we cast a ColorPoint to a Point? self OR for a ColorPoint Take the word extends literally. class x y c 14

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