230 likes | 370 Views
CS412/413. Introduction to Compilers and Translators March 17, 1999 Lecture 20: Implementing Objects. Administration. Programming Assignment 3, Part I due Friday Reading: Appel, Chapter 14. Outline. Checking methods Why Java arrays are broken Compiling methods Inheritance
E N D
CS412/413 Introduction to Compilers and Translators March 17, 1999 Lecture 20: Implementing Objects
Administration • Programming Assignment 3, Part Idue Friday • Reading: Appel, Chapter 14 CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
Outline • Checking methods • Why Java arrays are broken • Compiling methods • Inheritance • Compiling fields • Multiple inheritance • of interfaces • of implementations CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
Signature conformance • Subclass method signatures must conform to those of superclass • Argument types • Return type • Exceptions • How much conformance is really needed? • Java rule: arguments and returns must be identical, may remove exceptions CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
Checking conformance • Last time: mutable fields of a record must be invariant, immutable fields may be covariant • Object is essentially a record where methods are immutable fields! • Type of method fields is a function type (T1T2T3 Tn) • Subtyping rules for function types will tell us subtyping rules for methods CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
Function type subtyping class Shape { int setLLCorner(Point p); } class ColoredRectangle extends Shape { int setLLCorner(ColoredPoint p); } • Ignoring Java overloading rules, would this signature refinement be safe? (Eiffel) • Question: ColoredPoint int Point int? CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
Testing function subtypes • If ColoredPoint int Point int, must be able to use former in place of latter. int f(Point x); // f: Point int int g(ColoredPoint x); f = g; // ok? … g = f; // ok? CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
Contravariance/covariance • Function arguments may be contravariant • Function results may be covariant iT’iTi TrT’r T1...TnTrT’1...T’n T’r • Java is conservative! CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
Java arrays • Java has array type constructor: for any type T, T [ ] is an array of T’s • Java also has subtype rule: T1 T2 T1 [ ] T2 [ ] • Is this rule safe? CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
Java array subtype problems Elephant Animal Animal [ ] x; Elephant [ ] y; x = y; x[0] = new Rhinoceros(); // oops! • Assignment as method: void setElem (Animal element, int index) void setElem (Elephant element, int index); CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
Compiling methods • Methods look like functions, type like functions…what is different? • Call sequence: use dispatch vector • Argument list: implicit receiver CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
Method dispatch • Idea: every method has its own small integer index • Index is used to look up method in dispatch vector class C implements B { void quux(); 3 } interface A { void foo(); 0 } interface B extends A { void bar(); 1 void baz(); 2 } A B C CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
Dispatch vector layouts A foo B foo bar baz C foo A foo bar B bar,baz baz quux C quux CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
C foo bar baz quux Call sequence function method obj.baz(…) f(...) CALL CALL MEM NAME(f) + i *4 (= 2*4) MEM obj CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
Method arguments • Methods have a special variable (in Java, this) that is called the receiver object • History (Smalltalk): methods thought of as messages sent to receivers • Receiver object is really an argument to the method class Shape { int setCorner(int which, Point p) { … } } int setCorner(Shape this, int which, Point p) { … } CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
Static methods • In Java, can declare methods static -- they have no receiver object • Exactly like normal functions • don’t need to enter into dispatch vector • don’t need implicit extra argument for receiver • Treated as methods as way of getting functions inside the class scope -- not really methods CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
Inheritance • Three traditional components of object-oriented languages • abstraction/encapsulation/information hiding • subtyping/interface inheritance -- interfaces inherit method signatures from supertypes • inheritance/implementation inheritance -- a class inherits signatures and code from a superclass (possibly “abstract”) CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
Inheritance • Method code copied down from superclass if not overriddenby this class. • Fields also inherited (needed by inherited code in general) • Fields checked just as for records: mutable fields must be invariant, immutable fields may be covariant CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
DV setCorner LL: Point UR: Point Object Layout class Shape { Point LL, UR; void setCorner(int which, Point p); } class ColoredRect extends Shape { Color c; void setColor(Color c_); } DV setCorner LL: Point setColor UR: Point c: Color Shape ColoredRect CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
Code Sharing Machine code for Shape.setCorner DV setCorner LL: Point UR: Point • Don’t actuallyhave to copy code • Can inherit withoutsuperclass source DV setCorner LL: Point setColor UR: Point c: Color CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
Field Offsets class Shape { Point LL /*4*/ , UR; /*8*/ void setCorner(int which, Point p); } class ColoredRect extends Shape { Color c; /* 12 */ void setColor(Color c_); } • Can figure out offsets of fields from beginning of object statically • Accesses to fields are indexed loads ColoredRect x; T[x.c] = MEM(T[x] + 12) T[x.UR] = MEM(T[x] + 8) CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
Field Alignment • In many processors, a 32-bit load must be made to an address divisible by 4, 64-bit load address must be divisible by 8. • In rest (e.g. Pentium), loads are faster if aligned -- avoids extra load • Fields should be aligned x c y struct { int x; char c; int y; char d; int z; double e; } d z e CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
Summary • Rules for method conformance same as function subtyping rules • covariant result type • contravariant argument types • Java subtype checking is overly strict • Java arrays are not type-safe; need run-time checking • Method dispatch accomplished using dispatch vector, implicit method receiver argument • No dispatch of static methods needed • Inheritance causes extension of fields as well as methods; code can be shared • Field alignment: declaration order matters! CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers