Object-Oriented Languages. CSc 453. Compilers and Systems Software. 23 : OO Languages. Department of Computer Science University of Arizona

Similar documents
CSc 453. OO Languages. Object-Oriented Languages... Object-Oriented Languages. Compiling OO Languages. Compilers and Systems Software

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

CSc 553. Principles of Compilation. 16 : OO Languages Polymorphism. Department of Computer Science University of Arizona

CSc 520. Principles of Programming Languages 46: OO Languages Polymorphism

Java Object Oriented Design. CSC207 Fall 2014

CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages. ameter Passing Reference Parameter. Call-by-Value Parameters. 28 : Control Structures Parameters

Inheritance, Polymorphism and the Object Memory Model

Inheritance and object compatibility

CSc 520 Principles of Programming Languages

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

CS-202 Introduction to Object Oriented Programming

CSc 520 Principles of Programming Languages

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

2. The object-oriented paradigm!

Data Abstraction. Hwansoo Han

Chapter 6 Introduction to Defining Classes

Object typing and subtypes

CSc 453. Semantic Analysis III. Basic Types... Basic Types. Structured Types: Arrays. Compilers and Systems Software. Christian Collberg

C++ Important Questions with Answers

CSE 307: Principles of Programming Languages

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

CSE 431S Type Checking. Washington University Spring 2013

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

CSc 372. Comparative Programming Languages. 36 : Scheme Conditional Expressions. Department of Computer Science University of Arizona

Computer Science II (20073) Week 1: Review and Inheritance

COP 3330 Final Exam Review

CH. 2 OBJECT-ORIENTED PROGRAMMING

Procedure and Object- Oriented Abstraction

ECE 3574: Dynamic Polymorphism using Inheritance

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

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

CS558 Programming Languages Winter 2013 Lecture 8

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Final CSE 131B Spring 2004

Programming Languages and Techniques (CIS120)

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

CSc 520 Principles of Programming Languages

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

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

Lecture Set 4: More About Methods and More About Operators

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

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

STRUCTURING OF PROGRAM

Polymorphism. Agenda

A Short Summary of Javali

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

CSc 520. Principles of Programming Languages 25: Types Introduction

CSc 520 Principles of Programming Languages

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

ITI Introduction to Computing II

Inheritance and Interfaces

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

ITI Introduction to Computing II

Polymorphism Part 1 1

Object Oriented Programming. Java-Lecture 11 Polymorphism

Programming Languages and Techniques (CIS120)

Lecture Set 4: More About Methods and More About Operators

G Programming Languages - Fall 2012

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Memory Management. Memory Management... Memory Management... Interface to Dynamic allocation

Object Oriented Programming: Based on slides from Skrien Chapter 2

Subtyping (Dynamic Polymorphism)

Binghamton University. CS-140 Fall Dynamic Types

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

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

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

index.pdf January 21,

Type Analysis. Type Checking vs. Type Inference

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

Example: Count of Points

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Programming Languages and Techniques (CIS120)

Inheritance -- Introduction

Pascal: operations on basic data structures (selection) (I) 2.3 Accessing and manipulating data

What about Object-Oriented Languages?

C++ Yanyan SHEN. slide 1

Rules and syntax for inheritance. The boring stuff

Mid-Term 2 Grades

CS260 Intro to Java & Android 03.Java Language Basics

Practice for Chapter 11

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding

Introduction to Software Testing Chapter 2.4 Graph Coverage for Design Elements Paul Ammann & Jeff Offutt

Goals of the Lecture OO Programming Principles

Polymorphism. CMSC 330: Organization of Programming Languages. Two Kinds of Polymorphism. Polymorphism Overview. Polymorphism

Computer Science II (20082) Week 1: Review and Inheritance

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

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

CS558 Programming Languages

Object-Oriented Software Engineering Practical Software Development using UML and Java. Chapter 2: Review of Object Orientation

Method Resolution Approaches. Dynamic Dispatch

Transcription:

Object-Oriented Languages CSc 453 Compilers and Systems Software 23 : OO Languages Department of Computer Science University of Arizona Object-oriented languages extend imperative languages with: 1 A classification scheme that allows us to specify is-a as well as has-a relationships. Has-a is supported by Pascal, where we can declare that one data item has another item (a record variable has-a record field). Object-Pascal, Oberon, etc, extends this capability with inheritance which allows us to state that one data item is (an extension of) another item. 2 Late binding, which allows us to select between different implementations of the same abstract data type at run-time. collberg@gmail.com Copyright c 2009 Christian Collberg Object-Oriented Languages... Compiling OO Languages 3 Polymorphism, which is the ability of a variable to store values of different types. OO languages support a special kind of polymorphism, called inclusion polymorphism, that restricts the values that can be stored in a variable of type T to values of type T or subtypes of T. 4 Data encapsulation. Data (instance variables) and operations (methods) are defined together. 5 Templates and objects. A template (class or prototype) describes how to create new objects (instances of abstract data types). Runtime type checking (a variable of type ref T may only reference objects of type T or T s subtypes). Because of the polymorphic nature of OO languages, we can t always know (at compile-time) the type of the object that a given variable will refer to at run-time. When we invoke a method we can t actually know which piece of code we should execute. Finding the right piece of code is called method lookup. It can be done by name (Objective-C) or number (C++). Most OO languages rely on dynamic allocation. Garbage collection is a necessary part of the runtime system of a compiler for an OO language (C++ non-withstanding). This requires runtime type description.

Example TYPE Shape = CLASS x, y : REAL; METHOD draw(); ; METHOD move(x,y:real); x := x+x; TYPE Square = Shape CLASS side : REAL; METHOD draw(); ; TYPE Circle = Shape CLASS radius : REAL; METHOD draw(); ; METHOD area():real; // Example in Java class Shape { double x, y; void draw(); { } void move(double X, double Y); {x = x+x; }} class Square extends Shape { double side; void draw(); { }} class Circle extends Shape { double radius; void draw(); { } double area(); { }} (* Example in Modula-3 *) TYPE Shape = OBJECT x, y : REAL METHODS draw() := DefaultDraw; move(x, Y : REAL):=Move; Square = Shape OBJECT side : REAL METHODS draw() := SquareDraw Circle = Shape OBJECT radius : REAL METHODS draw() := CirlceDraw; area() := ComputeArea

(* Example in Modula-3 (continued) *) PROCEDURE Move (Self : Shape; X, Y : REAL) = END Move; PROCEDURE DefaultDraw (Self : Shape) = END DefaultDraw; PROCEDURE SquareDraw (Self : Square) = END SquareDraw; PROCEDURE CircleDraw (Self : Circle) = END CircleDraw; PROCEDURE ComputeArea (Self : Circle) : REAL = END ComputeArea; Example in Oberon-2 TYPE Shape = RECORD x, y : REAL Square = RECORD (Shape) side : REAL Circle = RECORD (Shape) radius : REAL PROCEDURE (Self : Shape) Move (X, Y : REAL) = END Move; PROCEDURE (Self : Shape) DefaultDraw () = END DefaultDraw; PROCEDURE (Self : Square) SquareDraw () = END SquareDraw; PROCEDURE (Self : Circle) CircleDraw () = END CircleDraw; PROCEDURE (Self : Circle) ComputeArea () : REAL = END ComputeArea; Record Layout Record Layout Single inheritance is implemented by concatenation, i.e. the instance variables of class C are 1 the variables of C s supertype, followed by 2 the variables that C declares itself. Inheritance Hierarchy Record Layout C1 C2 C3 C2 s instance vars C2 s instance vars C3 s instance vars

The offsets of the variables that C inherits from its supertype will be the same as in the supertype itself. In this example, C3 inherits from C2 which inherits from C1. C3 will have the fields from C1 followed by the fields from C2 followed by C3 s own fields. The order is significant. Inheritance Hierarchy Record Layout TYPE Shape = CLASS x,y: REAL; TYPE Square = Shape CLASS side:real; S Q C x:real y:real x:real y:real side:real x:real y:real C1 C2 C3 C2 s instance vars C2 s instance vars C3 s instance vars TYPE Circle = Shape CLASS radius:real; VAR S:Shape; VAR Q:Square; VAR C:Circle; radius:real Inheritance Hierarchy Shape Square Circle An OO language compiler would translate the declarations in the previous slide into something similar to this: TYPE Shape=POINTER TO RECORD x, y: REAL; TYPE Square=POINTER TO RECORD x, y: REAL; side:real; TYPE Circle=POINTER TO RECORD x, y: REAL; radius:real; VAR S:Shape; Q:Square; C:Circle; Templates

Class Templates To support late binding, runtime typechecking, etc, each class is represented by a template at runtime. Each template has pointers to the class s methods and supertype. ROOT Shape$Template draw: move: Square$Template draw: move: Circle$Template draw: move: area: Shape$draw Shape$move Square$draw Circle$draw Circle$area Code for methods Square s x,y fields are inherited from Shape. Their offsets are the same as in Shape. TYPE $TemplateT=POINTER TO RECORD parent : $TemplateT; move : ADDRESS; draw : ADDRESS; TYPE Square=POINTER TO RECORD $template : $TemplateT; x, y : REAL; side : REAL; CONST Square$Template:$TemplateT = [ parent= ADDR(Shape$Template); move = ADDR(Shape$move); draw = ADDR(Square$draw); ]; Each method is a procedures with an extra argument (SELF), a pointer to the object through which the method was invoked. TYPE Shape = CLASS x, y : REAL; METHOD draw (); ; METHOD move (X, Y : REAL); x := x+x; PROCEDURE Shape$move (SELF : Shape; X,Y:REAL); SELF^.x := SELF^.x + X; SELF^.y := SELF^.y + X; Method Lookup

Method Invocation Sending the message draw to Q: 1 Get Q s template, T. 2 Get draw s address at offset 4 in T. 3 Jump to draw s address, with Q as the first argument. Shape$Template Shape$move NIL draw: move: Square$Template Square$draw draw: move: $template Q x = 1 y = 3 side = 15 VAR Q : Square; Q := NEW (Square); Q.x := 1; Q.y := 3; Q.side := 15; Q.draw(); Q.move(20, 30); Q := malloc(size(square)); Q^.$template := Square$Template; Q^.x := 1; Q^.y := 3; Q^.side := 15; Q^.$template^.draw(Q); Q^.$template^.move(Q, 20, 30); Inclusion Polymorphism Runtime Type Checking Consider the last two lines of the example in the following slide: In L1, S points to a Shape object, but it could just as well have pointed to an object of any one of Shape s subtypes, Square and Circle. If, for example, S had been a Circle, the assignment C := S would have been perfectly OK. In L2, however, S is a Shape and the assignment C := S is illegal (a Shape isn t a Circle).

Typechecking Rules VAR S : Shape; Q : Square; C : Circle; Q := NEW (Square); C := NEW (Circle); S := Q; (* OK *) S := C; (* OK *) Q := C; (* Compile-time Error *) L1: S := NEW (Shape); L2: C := S; (* Run-time Error *) TYPE T = CLASS U = T CLASS S = T CLASS VAR t,r : T; u : U; s : S; A variable of type T may refer to an object of T or one of T s subtypes. Assignment Compile-time Run-Time t := r; Legal Legal t := u; Legal Legal u := t; Legal Check s := u; Illegal Run-time Type Checking Run-time Type Checking... Modula-3 Type-test Primitives: ISTYPE(object, T) Is object s type a subtype of T? NARROW(object, T) If object s type is not a subtype of T, then issue a run-time type error. Otherwise return object, typecast to T. TYPECASE Expr OF Perform different actions depending on the runtime type of Expr. The assignment s := t is compiled into s := NARROW(t, TYPE(s)). The Modula-3 runtime-system has three functions that are used to implement typetests, casts, and the TYPECASE statement NARROW takes a template and an object as parameter. It checks that the type of the object is a subtype of the type of the template. If it is not, a run-time error message is generated. Otherwise, NARROW returns the object itself. 1 ISTYPE(S,T : Template) : BOOLEAN; 2 NARROW(Object, Template) : Object; 3 TYPECODE(Object) : CARDINAL;

Run-time Checks Algorithm Casts are turned into calls to NARROW, when necessary: VAR S : Shape; VAR C : Circle; S := NEW (Shape); C := S; VAR S : Shape; VAR C : Circle; S := malloc (SIZE(Shape)); C := NARROW(S, Circle$Template); Imlementing ISTYPE Implementing NARROW We follow the object s template pointer, and immediately (through the templates parent pointers) gain access to it s place in the inheritance hierarchy. PROCEDURE ISTYPE (S, T : TemplatePtr) : BOOLEAN; LOOP IF S = T THEN RETURN TRUE; ENDIF; S := S^.parent; IF S = ROOT THEN RETURN FALSE; ENDIF; ENDLOOP END ISTYPE; NARROW uses ISTYPE to check if S is a subtype of T. Of so, S is returned. If not, an exception is thrown. PROCEDURE NARROW(T:TemplatePtr; S:Object):Object; IF ISTYPE(S^.$template, T) THEN RETURN S (* OK *) ELSE WRITE "Type error"; HALT; ENDIF; END NARROW;

Run-time Checks Example Run-time Checks Example... TYPE T = CLASS [ ]; S = T CLASS [ ]; U = T CLASS [ ]; V = U CLASS [ ]; X = S CLASS [ ]; Y = U CLASS [ ]; Z = U CLASS [ ]; VAR x : X; S X V T U Y Z x: template instance vari ables ISTYPE(x, T) ROOT ISTYPE(, ) T$Template... S$Template U$Template...... X$Template V$Template Y$Template Z$Template............ Organizing the Symbol Table Compile-Time Organization In C.M s method body we can refer to 1 M s locals and formals, and M s SELF. 2 C s methods and instance variables. 3 Methods and instance variables of C s superclasses. TYPE T = CLASS [ v : INTEGER; c : CHAR; METHOD P(x:INTEGER); v c METHOD Q(x:CHAR); v c ]; TYPE U = T CLASS [ c : REAL; k : INTEGER; METHOD P(x:INTEGER); v c k METHOD Q(r:REAL); v c k ];

Exam Problem Homework In the following object-oriented program "TYPE U = T CLASS" means that U inherits from T. NEW T means that a new object of type T is created. All methods are virtual, i.e. a method in a subclass overrides a method with the same name in a superclass. PROGRAM X; TYPE T = CLASS [ v : INTEGER;c : CHAR; METHOD P (x:integer); END P; METHOD Q (x:char); END Q; ]; TYPE U = T CLASS [ x : REAL; k : INTEGER; METHOD R(x:INTEGER); END R; METHOD Q(r:REAL); END Q; ]; VAR t : T; u : U; t := NEW T; u := NEW U; END Summary 1 Draw a figure that describes the state of the program at point. It should have one element for each item stored in memory (i.e. global/heap variables, templates, method object code, etc.) and should explicitly describe what each pointer points to.

Readings and References Summary Read the Tiger book: Object-oriented Languages pp. 283 298 For information on constructing layouts for multiple inheritance, see William Pugh and Grant Weddell: Two-directional record layout for multiple inheritance. The time for a type test is proportional to the depth of the inheritance hierarchy. Many algorithms do type tests in constant time: 1 Norman Cohen, Type-Extension Type Tests can be Performed in Constant Time. 2 Paul F.Dietz, Maintaining Order in a Linked List. For single inheritance languages, an instance of a class C consists of (in order): 1 A pointer to C s template. 2 The instance variables of C s ancestors. 3 C s instance variables. For single inheritance languages, subtype checks can be done in O(1) time. Method invocation is transformed to an indirect call through the template. If we can determine the exact type of an object variable at compile time, then method invocations through that variable can be turned into normal procedure calls. Summary... Confused Student Email A template for class C consists of (in order): 1 A pointer to the template of C s parent. 2 The method addresses of C s ancestors. 3 Addresses of C s methods. 4 Other information needed by the runtime system, such as The size of a C instance. C s pre- and postorder numbers, if the O(1) subtype test algorithm is used. C s type code. A type description of C s instance variables. Needed by the garbage collector. What happens when both a class and its subclass have an instance variable with the same name? The subclass gets both variables. You can get at both of them, directly or by casting. Here s an example in Java: class C1 {int a;} class C2 extends C1 {double a;} class C { static public void main(string[] arg) { C1 x = new C1(); C2 y = new C2(); x.a = 5; y.a = 5.5; ((C1)y).a = 5; } }