CS153: Compilers Lecture 11: Compiling Objects

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

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

CSE Lecture 3: Objects 2 September Nate Nystrom University of Texas at Arlington

CS558 Programming Languages Winter 2013 Lecture 8

Object typing and subtypes

Compiler Construction Lent Term 2015 Lectures 10, 11 (of 16)

Data Abstraction. Hwansoo Han

CS558 Programming Languages

CS152: Programming Languages. Lecture 23 Advanced Concepts in Object-Oriented Programming. Dan Grossman Spring 2011

Java Object Oriented Design. CSC207 Fall 2014

CS-XXX: Graduate Programming Languages. Lecture 23 Types for OOP; Static Overloading and Multimethods. Dan Grossman 2012

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

CMSC 132: Object-Oriented Programming II

Introducing C++ David Chisnall. March 15, 2011

CPS 506 Comparative Programming Languages. Programming Language

Java: introduction to object-oriented features

Chapter 5 Object-Oriented Programming

CSE 307: Principles of Programming Languages

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

Programming Languages and Techniques (CIS120)

G Programming Languages - Fall 2012

CS 32. Lecture 1: oops

Objects and Classes. Lecture 10 of TDA 540 (Objektorienterad Programmering) Chalmers University of Technology Gothenburg University Fall 2017

Making Inheritance Work: C++ Issues

Making Inheritance Work: C++ Issues

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

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

ITI Introduction to Computing II

Overview. Elements of Programming Languages. Objects. Self-Reference

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

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

What are the characteristics of Object Oriented programming language?

PROGRAMMING LANGUAGE 2

CS-XXX: Graduate Programming Languages. Lecture 22 Class-Based Object-Oriented Programming. Dan Grossman 2012

CSE 401/M501 Compilers

Object-Oriented Programming Concepts

ITI Introduction to Computing II

Data Structures (list, dictionary, tuples, sets, strings)

Subtyping (Dynamic Polymorphism)

ECE 122. Engineering Problem Solving with Java

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

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

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

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

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Don t Believe the Hype. CS152: Programming Languages. Lecture 21 Object-Oriented Programming. Class-based OOP. So what is OOP?

Compiler construction 2009

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

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Programming Languages and Techniques (CIS120)

C++ Yanyan SHEN. slide 1

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

JAVA MOCK TEST JAVA MOCK TEST II

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

Objects, Encapsulation, Inheritance (2)

Concepts of Programming Languages

Procedure and Object- Oriented Abstraction

Introduction to Programming Using Java (98-388)

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

CSE341, Fall 2011, Lecture 26 Summary

Programming Languages and Techniques (CIS120)

C++ Programming: Polymorphism

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Chapter 1: Object-Oriented Programming Using C++

Overview. Elements of Programming Languages. Objects. Self-Reference

Lecturer: William W.Y. Hsu. Programming Languages

What about Object-Oriented Languages?

Inheritance -- Introduction

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Java and C CSE 351 Spring

Practice for Chapter 11

Object Oriented Programming

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Lecture 15: Object-Oriented Programming

Inheritance and Polymorphism

Inheritance, Polymorphism and the Object Memory Model

2. The object-oriented paradigm!

Object Oriented Programming. Java-Lecture 11 Polymorphism

Programming Languages and Techniques (CIS120)

Chapter 10 :: Data Abstraction and Object Orientation

Topics. Modularity and Object-Oriented Programming. Dijkstra s Example (1969) Stepwise Refinement. Modular program development

CLASSES AND OBJECTS IN JAVA

Today s lecture. CS 314 fall 01 C++ 1, page 1

Basics of Object Oriented Programming. Visit for more.

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

OO Technology: Properties and Limitations for Component-Based Design

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

Programming II (CS300)

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

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Object Model. Object Oriented Programming Spring 2015

Programming Languages and Techniques (CIS120)

Object Orientation. Chapter Sixteen Modern Programming Languages, 2nd ed. 1

C++ Important Questions with Answers

2. The object-oriented paradigm

CS152: Programming Languages. Lecture 24 Bounded Polymorphism; Classless OOP. Dan Grossman Spring 2011

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

Lecture Notes on Programming Languages

Transcription:

CS153: Compilers Lecture 11: Compiling Objects Stephen Chong https://www.seas.harvard.edu/courses/cs153

Announcements Project 3 due today Project 4 out Due Thursday Oct 25 (16 days) Project 5 released on Thursday 2

Today Object Oriented programming What is it Dynamic dispatch Code generation for methods and method calls Fields Creating objects Extensions Type system 3

What Is Object-Oriented Programming? Programming based on concept of objects, which are data plus code OOP can be an effective approach to writing large systems Objects naturally model entities OO languages typically support information hiding (aka encapsulation) to support modularity inheritance to support code reuse Several families of OO languages: Prototype-based (e.g. Javascript, Lua) Class-based (e.g. C++, Java, C#) We focus on the compilation of class-based OO languages 4

Brief Incomplete History of OO (Early 60 s) Key concepts emerge in various languages/ programs: sketchpad (Sutherland), SIMSCRIPT (Hoare), and probably many others. (1967) Simula 67 (Dahl, Nygaard) crystalizes many ideas (class, object, subclass, dispatch) into a coherent OO language (1972) Smalltalk (Kay) introduces the concept of objectoriented programming (you should try Squeak!) (1978) Modula-2 (Wirth) (1985) Eiffel (Meyer) (1990 s) OO programming becomes mainstream: C++, Java, C#, 5

Classes What s the difference between a class and an object? A class is a blueprint for objects Class typically contains Declared fields / instance variables Values may differ from object to object Usually mutable Methods Shared by all objects of a class Inherited from superclasses Usually immutable Methods can be overridden, fields (typically) can not 6

Example Java Code class Vehicle extends Object { int position = 0; void move(int x) { this.position += x; class Car extends Vehicle { int passengers = 0; void await(vehicle v) { if (v.position < this.position) { v.move(this.position - v.position); else { this.move(10); class Truck extends Vehicle { void move(int x) { if (x < 55) this.position += x; Every Vehicle is an Object Every Car is a Vehicle, every Truck is a Vehicle Every Vehicle (and thus every Car and Truck) have a position field and a move method Every Car also has a passengers field and an await method 7

Example Java Code class Vehicle extends Object { int position = 0; void move(int x) { this.position += x; class Car extends Vehicle { int passengers = 0; void await(vehicle v) { if (v.position < this.position) { v.move(this.position - v.position); else { this.move(10); class Truck extends Vehicle { void move(int x) { if (x < 55) this.position += x; A Car can be used anywhere a Vehicle is expected (because a Car is a Vehicle!) Class Truck overrides the move method of Vehicle Invoking method o.move(i) will invoke Truck s move method if o s class at run time is Truck 8

Code Generation for Objects Methods How do we generate method body code? How do we invoke methods (dispatching) Challenge: handling inheritance Fields Memory layout Alignment Challenge: handling inheritance 9

Need for Dynamic Dispatch Methods look like functions. Can they be treated the same? Consider the following Java code interface Point { int getx(); float norm(); class ColoredPoint implements Point {... float norm() { return sqrt(x*x+y*y); class 3DPoint implements Point {... float norm() { return sqrt(x*x+y*y+z*z); Point p = foo(); p.norm(); 10

Need for Dynamic Dispatch Methods look like functions. Can they be treated the same? Consider the following Java code interface Point { int getx(); float norm(); class ColoredPoint implements Point {... float norm() { return sqrt(x*x+y*y); class 3DPoint implements Point {... float norm() { return sqrt(x*x+y*y+z*z); Point p = foo(); p.norm(); If p is object of class ColoredPoint, should execute ColoredPoint.norm() If p is object of class 3DPoint, should execute 3DPoint.norm() At run time could be either case! 11

Dynamic Dispatch Solution So we need some way at run time to figure out which code to invoke Solution: dispatch table (aka virtual method table, vtable) Each class has table (array) of function pointers Each method of class is at a known index of table Object o of class 3DPoint class_ptr... more stuff for the representation of an object... Runtime representation of class 3DPoint, including its dispatch table getx norm... more stuff for the representation of class 3DPoint... Code for 3DPoint.norm() 12

What Offset Into the VTable? Want to make sure that every object of class B has same layout of dispatch table Even if object is actually a subclass of B! class A { 1 void foo() {... class B extends A { 2 3 4 void bar() {... void baz() {... List methods in order Ensures that a dispatch table for class C also looks like a dispatch table for class B, and for class A class C extends B { void foo() {... void baz() {... void quux() {... 13

Dispatch Tables Dispatch table for class A Dispatch table for class B Dispatch table for class C A foo &A.foo &A.foo &B.bar &C.foo &B.bar B bar, baz &B.baz &C.baz &C.quux C quux Dispatch table for class C looks like a dispatch table for class B i.e., address for method foo is at index 0 (offset 0 bytes) address for method bar is at index 1 (offset 4 bytes) address for method baz is at index 2 (offset 8 bytes) And it looks like a dispatch table for class A i.e., address for method foo is at index 0 14

Generating Code for Methods For method declarations Methods have implicit argument, the receiver object (i.e., this, self) In essence, method bar declared in class B class B { void bar(int x) {... is translated like a function void bar(b this, int x) For method call o.bar(54) Essentially: void (*f)(obj *,int); f = o->class_ptr->vtable[offset for bar] f(o, 54); i.e., use vtable to get pointer to appropriate function, invoke it with receiver and arguments 15

Fields Same basic idea for fields as for methods! class 2DPoint implements Point { 1 int x; 2 int y;... Representation of object of class 3DPoint has space to store fields of 3DPoint and superclasses class 3DPoint implements Point { 3 int z;... Object o of class 3DPoint class_ptr 2DPoint.x 2DPoint.y 3DPoint.z 16

Generating Code for Field Accesses To access field x.f x will be represented as pointer to object Need to know (static) type of x x.f refers to memory location at appropriate offset from base of object x E.g., reading o.y would translate to dereferencing address o+(offset for y) Object o of class 3DPoint class_ptr 2DPoint.x 2DPoint.y 3DPoint.z 17

Creating Objects new C creates a new object of class C Creates record big enough to hold a C object Initializes instance variables Evaluates to pointer to newly created object 18

Extensions... Multiple inheritance Typically use multiple vtables (one for each base class) and switch between them based on the static type Other approaches possible Separate compilation Don t know how many fields/method in superclass! (Superclass could be recompiled after subclass) Resolve offsets at link or load time 19

Extensions... Prototype based OO languages Similar approach, but vtable belongs with object (no classes!) Objects are created by cloning other objects Many objects will have the same vtable: can share them, with copy-on-write Runtime type check: o instanceof C Each object contains pointer to its class, so can figure out at runtime if a o s class is a subclass of C But how to efficiently store inheritance information in runtime representation of classes? 20

OO Type Systems Visibility To support encapsulation, some OO languages provide visibility restrictions on fields and methods Java has private, protected, public (and some more) private members accessible only to implementation of class public members accessible by any code protected members accessible only to implementation of class and subclasses Subclassing vs inheritance Somewhat conflated in Java Inheritance: reuse code from another class; Subclassing: every object of subclass is a superclass object C++ has visibility restrictions on inheritance 21

OO Type Systems Subclassing vs subtyping Not the same! No contravariance in argument type in Java methods Overriding vs overloading Given C.m(T1, T2,..., Tn) and D.m(S1, S2,..., Sm) where C is subclass of D, C.m overrides D.m only if T1, T2,..., Tn = S1, S2,..., Sm Otherwise, D.m just overloads the method name m... Null values... In Java type C for class C is analogous to C option in ML Since any object value can be null 22