Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...

Slides:



Advertisements
Similar presentations
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Advertisements

A little bit on Class-Based OO Languages CS153: Compilers Greg Morrisett.
How C++ Compilers Implement Object Orientation Eric Powders (ejp2127)
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
Compiling Object Oriented Programs Mooly Sagiv Chapter
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CS 536 Spring Run-time organization Lecture 19.
Control Flow Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Generation 2 Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
1 Control Flow Analysis Mooly Sagiv Tel Aviv University Textbook Chapter 3
The Procedure Abstraction Part IV: Run-time Structures for OOLs Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Run time vs. Compile time
CS 536 Spring Code Generation II Lecture 21.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 24: Dynamic Binding COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos.
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.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Compiling Object Oriented Programs Mooly Sagiv Chapter 6.2.9, 6.5.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
OOP Languages: Java vs C++
Comparison of OO Programming Languages © Jason Voegele, 2003.
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 12: Adding Functionality to Your Classes.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:
Programming Languages and Paradigms Object-Oriented Programming (Part II)
The Procedure Abstraction, Part VI: Inheritance in OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Procedure Abstraction VI Run-time structures for OOLs ( II ) and Wrap-up Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
The Procedure Abstraction, Part V: Support for OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
Guided Notes Ch. 9 ADT and Modules Ch. 10 Object-Oriented Programming PHP support for OOP and Assignment 4 Term project proposal C++ and Java Designer.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
COMPILERS Symbol Tables hussein suleman uct csc3003s 2007.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Object Oriented Software Development
Lecture 12 March 16, The Scope of a Variable What if there are two variables with the same name? –A local or block-local variable can have the same.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Compiling Objects in Class-Based Languages CS 153: Compilers.
Object-Oriented Programming Chapter Chapter
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
ISBN Object-Oriented Programming Chapter Chapter
Copyright © 2005 Elsevier Object-Oriented Programming Control or PROCESS abstraction is a very old idea (subroutines!), though few languages provide it.
Compilation /16a Lecture 10 Compiling Object-Oriented Programs Noam Rinetzky 1.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Lecture 2: Review of Object Orientation. © Lethbridge/La ganière 2005 Chapter 2: Review of Object Orientation What is Object Orientation? Procedural.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Overview of C++ Polymorphism
1 Lecture 23: Dynamic Binding (Section ) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos.
ISBN Chapter 12 Support for Object-Oriented Programming.
Program Analysis Last Lesson Mooly Sagiv. Goals u Show the significance of set constraints for CFA of Object Oriented Programs u Sketch advanced techniques.
Design issues for Object-Oriented Languages
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Compiler Design 18. Object Oriented Semantic Analysis (Symbol Tables, Type Checking) Kanat Bolazar March 30, 2010.
The Procedure Abstraction Part V: Run-time Structures for OOLs
Overview of C++ Polymorphism
Inheritance and Polymorphism
Programming in C# CHAPTER 5 & 6
Presentation transcript:

Objects and Classes David Walker CS 320

Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,... –fun to use, but require special techniques to compile and optimize –today will be looking at how to compile objects and classes similar to those found in Java Appel chapter

Object Tiger Add class declarations to Tiger: –classes are a collection of field (variable) declarations method declarations –every class declaration gives a new name to a class –a class may inherit methods and fields from the class it extends –the class “object” sits at the top of the class hierarchy; it has no fields and no methods

An Object Tiger Class let class Vehicle extends Object { var position := start method move (x : int) = (position := position + x) } in... end class name superclass that Vehicle inherits from field declaration method declaration

Another Object Tiger Class let class Vehicle extends Object { var position := start method move (x:int) = (position := position + x) } class Car extends Vehicle { var passengers := 0 method await(v:vehicle) = if (v.position < position) then v.move(position – v.position) else self.move(10) in... end v’s “position” field current object’s “position” field call to inherited method new field declaration new method declaration

Yet Another Object Tiger Class let class Vehicle extends Object { var position := start method move (x:int) = position := position + x } class Car extends Vehicle {... } class Truck extends Vehicle { method move (x:int) = if x <= 55 then position := position * x in... end method override

Using the Classes let class Vehicle extends Object {... } class Car extends Vehicle {... } class Truck extends Vehicle {...} var t := new Truck var c := new Car var v : Vehicle := c in c.passengers := 2; c.move(60); v.move(70); c.await(t); end new object created subtyping allows a car to be viewed and used as a generic vehicle subtyping allows a truck to be viewed and used as a generic vehicle a car calls an inherited method

Implementing Object Tiger Some key problems: –how do we access object fields? both inherited fields and fields for the current object? –how do we access method code? if the current class does not define a particular method, where do we go to get the inherited method code? how do we handle method override?

Class Hierarchy The class hierarchy is the graph of inheritence relationships in a program: –In a single-inheritence (SI) language, the graph is a tree –In a multiple-inheritence (MI) language, the graph is a dag –Multiple-inheritence languages are much trickier to implement than single-inheritence languages Object Vehicle CarTruck

Object Layout (SI) Objects are laid out somewhat like records –each variable has a slot in the record –in order to implement field lookup we need to have a systematic way to find a given field eg: v.position v may be a generic vehicle or it may be a car or a truck we need to put “position” in the same place in the record that implements vehicles, cars and trucks

Object Layout (SI) Solution: extension on the right –lay out the inherited fields first in the same order as in the parent (SI => only 1 parent) –lay out the newly declared to the right

Object Layout (SI) class A extends Object { var a := 0 } class B extends A { var b := 0 var c := 0 } class C extends A { var d := 0 } class D extends B { var e := 0 }

Object Layout (SI) class A extends Object { var a := 0 } class B extends A { var b := 0 var c := 0 } class C extends A { var d := 0 } class D extends B { var e := 0 } A a

Object Layout (SI) class A extends Object { var a := 0 } class B extends A { var b := 0 var c := 0 } class C extends A { var d := 0 } class D extends B { var e := 0 } B a b c A a

Object Layout (SI) class A extends Object { var a := 0 } class B extends A { var b := 0 var c := 0 } class C extends A { var d := 0 } class D extends B { var e := 0 } C a d B a b c A a

Object Layout (SI) class A extends Object { var a := 0 } class B extends A { var b := 0 var c := 0 } class C extends A { var d := 0 } class D extends B { var e := 0 } D a b c e C a d B a b c A a

Static & Dynamic Methods The result of compiling a method is some machine code located at a particular address –at a method invocation point, we need to figure out what code location to jump to Java has static & dynamic methods –to resolve static method calls, we look at the static type of the calling object –to resolve dynamic method calls, we need the dynamic type of the calling object

Static Methods –during semantic analysis, the compiler knows: static type (class) of the object calling the method the list of methods in each class –and determines the closest method (up the class hierarchy) with the given name –and inserts instructions to pass object as self parameter a direct call to the known method let class A extends Object { static method foo (x:int) =... static method bar (x:int) =... } class B extends A { static method foo (x:int) =... } var a : A = new A var b : A = new B var c : B = new B in a.foo(3); (* calls foo in class A *) b.foo(3); (* calls foo in class A *) c.bar(3); (* calls bar in class A *) c.foo(3); (* calls foo in class B *)

Dynamic Methods –Method called depends on object’s dynamic type During semantic analysis, may be unknown –At run-time, we determine which code to jump to object stores a pointer to its method table (v-table) as well as its object vars –At compile-time, we generate code to look up v-table in object extract method from table jump to method body let class A extends Object { method foo (x:int) =... method bar (x:int) =... } class B extends A { method foo (x:int) =... } var a : A = new A var b : A = new B var c : A = if long-and-tricky-computation then a else b in c.foo(3)

Object Layout II (SI) class A extends Object { var a := 0; method f () } class B extends A { method g () } class C extends B { method g () } class D extends C { var b := 0 ; method f () }

Object Layout II (SI) class A extends Object { var a := 0; method f () } class B extends A { method g () } class C extends B { method g () } class D extends C { var b := 0 ; method f () } A a A_f

Object Layout II (SI) class A extends Object { var a := 0; method f () } class B extends A { method g () } class C extends B { method g () } class D extends C { var b := 0 ; method f () } B a A a A_f B_g

Object Layout II (SI) class A extends Object { var a := 0; method f () } class B extends A { method g () } class C extends B { method g () } class D extends C { var b := 0 ; method f () } C a B a A a A_f B_g A_f C_g

Object Layout II (SI) class A extends Object { var a := 0; method f () } class B extends A { method g () } class C extends B { method g () } class D extends C { var b := 0 ; method f () } D a b C a B a A a A_f B_g A_f C_g D_f C_g

Object Layout II (SI) class A extends Object { var a := 0; method f () } class B extends A { method g () } class C extends B { method g () } class D extends C { var b := 0 ; method f () } D a b C a B a A a A_f B_g A_f C_g D_f C_g D a b

Multiple Inheritence Multiple inheritence is trickier to implement than single inheritence because creating objects of a subclass from their subclass by “extension on the right” doesn’t work –if C inherits from both A and B, we can’t put A’s variables at the front and put B’s variables at the front of the object in the same place! –we need to do a global analysis to determine object layout

Object Layout (MI) class A extends Object { var a := 0 } class B extends Object { var b := 0 var c := 0 } class C extends A { var d := 0 } class D extends A,B,C { var e := 0 } D a b c e C a d B b c A a d

Object Layout (MI) class A extends Object { var a := 0 } class B extends Object { var b := 0 var c := 0 } class C extends A { var d := 0 } class D extends A,B,C { var e := 0 } D a b c e C a d B b c A a d Determine object layout by: global graph coloring! a node for each field name an interference edge between names that coexist in the same class (via inheritence or otherwise)

Object Layout (MI) class A extends Object { var a := 0 } class B extends Object { var b := 0 var c := 0 } class C extends A { var d := 0 } class D extends A,B,C { var e := 0 } D a b c e C a d B b c A a d wasted space in every object

Object Layout II (MI) class A extends Object { var a := 0 } class B extends Object { var b := 0 var c := 0 } class C extends A { var d := 0 } class D extends A,B,C { var e := 0 } D a b c e C a d B b c A a d wasted space per class a: b: a: d: a: b: c: d: e:

Object Layout II (MI) class A extends Object { var a := 0 } class B extends Object { var b := 0 var c := 0 } class C extends A { var d := 0 } class D extends A,B,C { var e := 0 } B b c A a a: b: To fetch a field using this representation, we: load the first field of the object to get the class descriptor c load M [c + fieldOffset] Note: fieldOffset can be precomputed using global graph coloring as before

A Problem Global analyses are the bane of modern computing –many applications use dynamically linked libraries, mobile code, get patches to fix bugs, etc. –when we don’t have the whole program at compile- time, we can’t do global analyses! –solution (most of the time): a smart custom linker still tricky when new code is linked dynamically to code that is already running –hence, Java has single inheritence

Other OO Features Down-casts and type tests –Java has casting mechanism “(C) x” to cast variable x to class C at run time we look up x’s dynamic type and determine whether it is a subtype of C these type casts are currently pervasive and a source of both inefficiency and errors soon, Java and C# will be adding parametric polymorphism, a la ML, to make many of these unnecessary casts go away

Other OO Features Protection mechanisms –to encapsulate local state within an object, Java has “private” “protected” and “public” qualifiers private methods/fields can’t be called/used outside of the class in which they are defined –during semantic analysis (type checking), the compiler maintains this information in the symbol table for each class

Summary Object-oriented languages provide new challenges for compiler writers –how to find fields and methods –how to make field and method access just as efficient as ordinary function call and variable lookup –lots of ongoing research in OO language implementation tackles these and other interesting questions