Object Oriented Languages. Hwansoo Han

Similar documents
Run Time Environment. Implementing Object-Oriented Languages

The Procedure Abstraction

What about Object-Oriented Languages?

Implementing Object-Oriented Languages. Implementing instance variable access. Implementing dynamic dispatching (virtual functions)

Classes. Compiling Methods. Code Generation for Objects. Implementing Objects. Methods. Fields

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

Runtime Support for OOLs Part II Comp 412

Procedure and Object- Oriented Abstraction

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

Naming in OOLs and Storage Layout Comp 412

Semantics (cont.) Symbol Table. Static Scope. Static Scope. Static Scope. CSE 3302 Programming Languages. Static vs. Dynamic Scope

CSE 504: Compiler Design. Runtime Environments

Semantics. Names. Binding Time

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

Typical Compiler. Ahead- of- time compiler. Compilers... that target interpreters. Interpreter 12/9/15. compile time. run time

Winter Compiler Construction T9 IR part 2 + Runtime organization. Announcements. Today. Know thy group s code

Data Abstraction. Hwansoo Han

Java Object Oriented Design. CSC207 Fall 2014

Subtyping (Dynamic Polymorphism)

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

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

CSE 307: Principles of Programming Languages

Module Mechanisms CS412/413. Modules + abstract types. Abstract types. Multiple Implementations. How to type-check?

Runtime Support for Algol-Like Languages Comp 412

Classes. Code Generation for Objects. Compiling Methods. Dynamic Dispatch. The Need for Dispatching CS412/CS413

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

Optimizer. Defining and preserving the meaning of the program

CS415 Compilers. Procedure Abstractions. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

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

Announcements. CSCI 334: Principles of Programming Languages. Lecture 19: C++

The Procedure Abstraction Part I: Basics

Names, Scopes, and Bindings II. Hwansoo Han

Programming Language Dilemma Fall 2002 Lecture 1 Introduction to Compilation. Compilation As Translation. Starting Point

Implementing Object-Oriented Languages - 2

Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access

Inheritance and Polymorphism

CSc 520. Principles of Programming Languages 45: OO Languages Introduction

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

Inheritance and Polymorphism

Chapter 10. Implementing Subprograms ISBN

CS558 Programming Languages Winter 2013 Lecture 8

Implementing Subprograms

The Procedure Abstraction Part I Basics

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

G Programming Languages - Fall 2012

Compiler construction

C++ (classes) Hwansoo Han

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Method Resolution Approaches. Dynamic Dispatch

Typical Runtime Layout. Tiger Runtime Environments. Example: Nested Functions. Activation Trees. code. Memory Layout

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

Francesco Nidito. Programmazione Avanzata AA 2007/08

Francesco Nidito. Programmazione Avanzata AA 2007/08

Object typing and subtypes

Programming Languages: Lecture 12

Building Interpreters

C++ Part 2 <: <: C++ Run-Time Representation. Smalltalk vs. C++ Dynamic Dispatch. Smalltalk vs. C++ Dynamic Dispatch

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

2.4 Structuring programs

Concepts of Programming Languages

CPS 506 Comparative Programming Languages. Programming Language

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

Dynamic Dispatch and Duck Typing. L25: Modern Compiler Design

StackVsHeap SPL/2010 SPL/20

Object Oriented Programming. Java-Lecture 11 Polymorphism

Run-Time Environments

CS 480 Fall Runtime Environments. Mike Lam, Professor. (a.k.a. procedure calls and heap management)

Simple example. Analysis of programs with pointers. Program model. Points-to relation

Concepts Introduced in Chapter 7

Chapter 6 Introduction to Defining Classes

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction

Information System Design (IT60105)

CS153: Compilers Lecture 11: Compiling Objects

COP 3330 Final Exam Review

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

Implementing Subprograms

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

CSE 401/M501 Compilers

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

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

C++ Yanyan SHEN. slide 1

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

CSE 303: Concepts and Tools for Software Development

The code generator must statically assign a location in the AR for each temporary add $a0 $t1 $a0 ; $a0 = e 1 + e 2 addiu $sp $sp 4 ; adjust $sp (!

Chapter 9. Def: The subprogram call and return operations of a language are together called its subprogram linkage

Supporting Class / C++ Lecture Notes

Chapter 5. Names, Bindings, and Scopes

Thanks! Review. Course Goals. General Themes in this Course. There are many programming languages. Teaching Assistants. John Mitchell.

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

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

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

2. The object-oriented paradigm!

2. The object-oriented paradigm

Java: introduction to object-oriented features

Run-time Environments - 2

Intermediate Code, Object Representation, Type-Based Optimization

Programming in C# Inheritance and Polymorphism

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

VIRTUAL FUNCTIONS Chapter 10

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

Transcription:

Object Oriented Languages Hwansoo Han

Object-Oriented Languages An object is an abstract data tpe Encapsulates data, operations and internal state behind a simple, consistent interface. z Data Code Data Code Data Code Each object needs local storage for its fields Fields are static (lifetime of object ) Often access through methods Methods are interfaces outside objects Some methods are public, others are private More comple linkage convention; locating them is comple 2

Class vs. Object Objects with the same data laout & code members Same names for instance variables & methods But, separate instance variables for each class instance Share codes and static data (a.k.a. class variables) Class is a tpe, object is an instance of the class tpe Benefits of OOL design Allows programmer to write it down once Allows code reuse at both source & implementation level 3

Accessibilit and Inheritance What data an eecuting method can see? instance variables The object s own per-instance variables class variables The class s own per-class variables (static fields) An object defined in the global name space (or scope) Objects ma contain other objects (or references to them) Inheritance Most OOLs support a hierarchical notion of inheritance Some OOLs support multiple inheritance More than one path through the inheritance hierarch 4

Issues in Implementing OOLs Two critical issues in OOL implementation: 5 Object representation Mapping a method invocation name to a method implementation Both are intimatel related to the OOL s name space Object Representation class C fee() fie() foe() count 0 4 8 12 Private storage for instance variables Objects (or instances) allocated in heap Need consistent, fast access : constant offsets Static class storage for class variables accessible b global names Accessible via linkage smbol &_C.count (e.g. class C::count) Nested classes are handled like blocks in Algol-Like-Languages Method code put at fied offset from start of class area Maintain pointers to method codes

Dealing with Single Inheritance Use prefiing of storage Class Point { int, ; Class ColorPoint etends Point { Color c; c Does casting work properl? 6

Resolving Method Names Mapping names to methods <class, method> method implementation Static mapping, known at compile-time (Java, C++) Fied offsets & indirect calls Dnamic mapping, unknown until run-time (Smalltalk) Look up name in class table of methods Dnamic class hierarch This is reall a data-structure problem Build a table of function pointers (method table for each class) Use a standard invocation sequence Read function address from the fied entr of method table Invoke indirect call 7

Per-Class Method Table With static, compile-time mapped classes Class A Class B fee() fie() i j k band() bend() m a b c foe() bind() fum() i j k bond() n a b c z i j k p a b c Message dispatch becomes an indirect call through a method table 8

Dispatching in Single Inheritance Use prefiing of tables : fied entr for same name Class Point { int, ; public void draw(); public void d2o(); table draw d2o Point: draw Point: d20 table draw ColorPoint: draw Class ColorPoint etends Point { Color c; public void draw(); public void rev(); c d2o rev ColorPoint: rev 9

Inheritance Hierarch To simplif object creation, We allow a class to inherit methods from superclass. The descendant class is called the subclass of its ancestor. The Concept: Method tables of B & C are etensions of the table from A a fee() fie() b fee() foe() fie() foe() fum() c d e B z C A Cop from the superclass 10

Static vs. Dnamic Inheritance Two distinct philosophies Visibilit in name space 11 Method can see instance variables of class & superclasses Man different levels where a value can reside In essence, OOL differs from ALL in Static class hierarch Can map <class:method> to code at compile time Leads to 1-level jump vector Cop superclass methods Fied offsets & indirect calls Less fleible & epressive Dnamic class hierarch Cannot map <class:method> to code at compile time Multiple jump vector (one per class) Must search method tables Run-time lookups & caching Much more epensive to run the shape of its name space AND the mechanism used to bind names to implementations

Multiple Inheritance The idea Allow more fleible sharing of methods & attributes Rela the inclusion requirement Need a linguistic mechanism for specifing partial inheritance Problems when C inherits from both A & B C s method table can etend A or B, but not both Laout of an object instance for C becomes trick Other classes, sa D, can inherit from C & B Adjustments to offsets become comple Both A & B might provide fum() with the same name which is seen in C? C++ produces a snta error when fum() is used 12

Multiple Inheritance - fields Use prefiing of storage Class Point { int, ; Class ColoredThing { Color c; c Class ColorPoint etends Point, ColoredThing { Does casting work properl? +=8 c 13

Multiple Inheritance fields & methods Use prefiing of storage Class Point { int, ; void draw(); void d2o(); Class CThing { Color c; void rev(); table table c table draw d2o rev draw Point: draw Point: d20 CThing: rev CPoint:draw Class CPoint etends Point, CThing { void draw() +=12 table d2o rev +=12 Use as CThing c rev 14

Multiple Inheritance (casting & method call) Usage as Point: No etra action (prefiing does everthing) Usage as CThing: Increment b 12 Usage as CPoint: La out Cthing s class pointer and Cthing s data at + 12 When calling rev() All methods has a pointer as an implicit parameter Two possible options Add 12 to in pre-call and restore in post-call sequences The call in class table points to a trampoline function that adds 12 to and calls rev() Ensures that rev(), which assumes that points to a CThing data area, gets the right data 15

Multiple Inheritance (trampoline function) Assume that C inherits fee() from A, fie() from B, & defines both foe() and fum() Object record for an instance of C Method table entr for fie() contains a point to a trampoline function instead of real pointer to B::fie() Trampoline function increases pointer and call B::fie() A vars B vars C vars fee() fie() foe() fum() + offset code code code code fie() Trampoline function 16

What About Calls in an OOL (Dispatch)? In an OOL, most calls are indirect calls 17 Compiled code does not contain address of callee Finds it b indirection through class method table Required to make subclass calls find right methods Code compiled in class C cannot know of subclass methods that override methods in C and C s superclasses In a general case, need dnamic dispatch Map method name to a search ke Perform a run-time search through hierarch Start with object s class, search for 1 st occurrence of ke This can be epensive Use a method cache to speed up the search Cache holds < ke,class,method pointer > How big? Bigger more hits & longer search Smaller fewer hits, faster search

What About Calls in an OOL (Dispatch)? Improvements are possible in special cases If class has no subclasses, can generate direct call Class structure must be static or class must be FINAL If class hierarch is static Can generate complete method table for each class Single indirection through class pointer (1 or 2 operations) Keeps overhead at a low level If class hierarch changes infrequentl Build complete method tables at run time Initialization & an time class hierarch changes If running program can create new classes, Well, not all things can be done quickl 18

Summar OOLs support inheritance Single vs. multiple inheritance Casting to superclasses Issues in implementing OOLs Data laout Method mapping Optimization for method invocation Direct call based on class hierarch analsis 19