A little bit on Class-Based OO Languages CS153: Compilers Greg Morrisett.

Slides:



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

Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
This lecture is a bit of a departure in that we’ll cover how C++’s features are actually implemented. This implementation will look suspiciously similar.
Programming Languages and Paradigms
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
1 Compiler Construction Intermediate Code Generation.
1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #17, March 12, 2007 Procedure Abstraction, Name Spaces, Scoping Rules, Activation Records,
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
Run time vs. Compile time
Static and Dynamic Behavior Fall 2005 OOPD John Anthony.
Run-time Environment and Program Organization
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
“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.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Adapted from Prof. Necula UCB CS 1641 Overview of COOL ICOM 4029 Lecture 2 ICOM 4029 Fall 2008.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
© A+ Computer Science - Inheritance © A+ Computer Science - Lab 20.
The Procedure Abstraction, Part VI: Inheritance in OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 9 Def: The subprogram call and return operations of a language are together called its subprogram.
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.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Advanced Java Programming CS 537 – Data Structures and Algorithms.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
Type == Class  reference type -- store only the object’s address  simple (primitive) type -- for storing small data values Other options Wrapper classes.
Compiling Objects in Class-Based Languages CS 153: Compilers.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
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.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
ISBN Object-Oriented Programming Chapter Chapter
1 Chapter 10 © 2002 by Addison Wesley Longman, Inc The General Semantics of Calls and Returns - Def: The subprogram call and return operations of.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
CSE 3341/655; Part 3 35 This is all very wrong! What would the SW1/SW2 (RESOLVE) people say if they saw this? Problem: Lack of data abstraction; What would.
Chapter 10 Chapter 10 Implementing Subprograms. Implementing Subprograms  The subprogram call and return operations are together called subprogram linkage.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 20 – C++ Subclasses and Inheritance.
Object Model Baojian Hua What ’ s an Object Model? An object model dictates how to represent an object in memory A good object.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Design issues for Object-Oriented Languages
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Section 11: Comparing Java and C
Implementing Subprograms Chapter 10
Scope and Code Generation
Methods Attributes Method Modifiers ‘static’
CSC 533: Programming Languages Spring 2015
Implementing Subprograms
ICOM 4029 Fall 2003 Lecture 2 (Adapted from Prof. Necula UCB CS 164)
CSC 533: Programming Languages Spring 2018
CSC 533: Programming Languages Spring 2019
SPL – PS3 C++ Classes.
Chapter 10 Def: The subprogram call and return operations of
Presentation transcript:

A little bit on Class-Based OO Languages CS153: Compilers Greg Morrisett

Java Objects Representing Objects –1st field is a pointer to a vtable vtable: virtual method table. each method is a procedure that takes an extra (implicit) argument corresponding to self. –Remaining fields are instance variables.

In Pictures: address of method 1 address of method 2 … address of method n Shared Vtable for Class vtable pointer instance variable 1 instance variable 2 … instance variable m vtable pointer instance variable 1 instance variable 2 … instance variable m

Simple Inheritance class Pt2d extends Object { int x; int y; void movex(int i) { x = x + i; } void movey(int i) { y = y + i; } } class Pt3d extends Pt2d { int z; void movez(int i) { z = z + i; } }

Same as: class Pt2d { int x; int y; void movex(int i) { x = x + i; } void movey(int i) { y = y + i; } } class Pt3d { int x; int y; int z; void movex(int i) { x = x + i; } void movey(int i) { y = y + i; } void movez(int i) { z = z + i; } }

At Run-Time: address of movex address of movey Pt2d Vtable vtable pointer x y x y z address of movex address of movey address of movez Pt2d Object Pt3d Object Pt3d Vtable

Jish Abstract Syntax type tipe = Int_t|Bool_t |Class_t of class_name type exp = Var of var | Int of int | Nil | Var of var | Assign of var * exp | New of class_name | Invoke of exp * var * (exp list) |... type stmt = Exp of exp | Seq of stmt*stmt |... type method = Method of {mname:var, mret_tipe:tipe option, margs:var*tipe list, mbody:stmt} type class = Class of {cname:class_name, csuper:class_name, cinstance_vars:var*tipe list, cmethods:method list}

Compiling to Cish For every method m(x 1,…,x n ), generate a Cish function m(self,vtables,x 1,…,x n ). At startup, for every class C, create a record of C's methods (the vtable.) Collect all of the vtables into a big record. –we will pass this data structure to each method as the vtables argument. –wouldn't need this if we had a global variable in Cish for storing the vtables. Create a Main object and invoke its main() method.

Operations: new C –create a record big enough to hold a C object –initialize the object's vtable pointer using vtables. –initialize instance variables with default values 0 is default for int, false for bool, nil for classes. –return pointer to object as result e.m(e 1,…,e n ) –evaluate e to an object. –extract a pointer to the m method from e's vtable –invoke m, passing to it e,vtables,e 1,…,e n e is passed as self. vtables is threaded through to every method. –in a real system, must check that e isn't nil!

Operations Continued: x, x := e –read or write a variable. –the variable could be a local or an instance variable. –if it's an instance variable, we must use the "self" pointer to access the value. –Real Java provides e.x. Do we need this? (C)e -- type casts –if e has type D and D  C, succeeds. –if e has type D and C  D, performs a run-time check to make sure the object is actually (at least) a C. –if e has type D, and C is unrelated to D, then generates a compile-time error.

Subtleties in Type-Checking: Every object has a run-time type. –essentially, its vtable The type-checker tracks a static type. –some super-type of the object. –NB: Java confuses super-types and super- classes. In reality, if e is of type C, then e could be nil or a C object. –Java "C" = ML "C option"

Subtyping vs. Inheritance Inheritance is a way to assemble classes Simple inheritance: –D extends C implies D  C –a read of instance variable x defined in C? okay because D has it too. –an invocation of method m defined in C? okay because D has it too. –m : (C self,T 1,…,T n )  T What can m do to self? Read C instance variables, invoke C methods.

Overriding: class List { int hd; List tl; void append(List y) { if (tl == Nil) tl := y; else tl.append(y); } class DList extends List { DList prev; void append(DList y) { if (tl == Nil) { tl := y; if (y != Nil) y.prev := self; } else { tl.append(y); } Java won't let you say this…

Best you can do: class List { int hd; List tl; void append(List y) { if (tl == Nil) tl := y; else tl.append(y); } class DList extends List { DList prev; void append(List y) { if (tl == Nil) { tl := y; if (y != Nil) ((DList)y).prev := self; } else { tl.append(y); } Run-time type-check

What We Wish we Had… Don't just "copy" when inheriting: –Also replace super-class name with sub-class name. –That is, we need a "self" type as much as a self value. –But this will not, in general, give you that the sub-class is a sub-type of the super-class. –why?

Run-time Type Checks: Given an object x, how do we (quickly) determine if it has a run-time type D that is a sub-class of C? option 1: Have a link to the parent's vtable in the child's vtable. –crawl up the chain until you reach the parent (or Object). –disadvantage? other options?

Displays: address of method 1 address of method 2 … address of method n num ancestors level 0 level 1 … level m Just have a pointer to all ancestors. To check if C is a super-class: statically calculate depth of C check that num ancestors is at least that depth. check that this ancestor is C.

Interfaces Consider an interface I = { void foo(); void bar(); } Any object of a class C that implements methods named foo and bar can be treated as if it has interface type I. Can we use C's vtable? –no. –In general, C may have defined methods before, between, or after foo and bar or may have defined them in a different order. So to support interfaces, we need a level of indirection…

Interfaces: address of method 1 address of method 2 … address of method n Shared Vtable for Interface vtable pointer instance variable 1 instance variable 2 … instance variable m Actual Object vtable pointer actual object Wrapper Object