1 / 23

CS412/413

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

hyman
Download Presentation

CS412/413

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS412/413 Introduction to Compilers and Translators March 17, 1999 Lecture 20: Implementing Objects

  2. Administration • Programming Assignment 3, Part Idue Friday • Reading: Appel, Chapter 14 CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

  3. 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

  4. 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

  5. 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 (T1T2T3 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

  6. 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

  7. 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

  8. Contravariance/covariance • Function arguments may be contravariant • Function results may be covariant iT’iTi TrT’r T1...TnTrT’1...T’n T’r • Java is conservative! CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

More Related