CS412/413 - PowerPoint PPT Presentation

About This Presentation
Title:

CS412/413

Description:

Lecture 20: Implementing Objects ... void setElem (Elephant element, int index) ... Argument list: implicit receiver ... – PowerPoint PPT presentation

Number of Views:135
Avg rating:3.0/5.0
Slides: 24
Provided by: andrew433
Category:
Tags: cs412 | elephant | list

less

Transcript and Presenter's Notes

Title: CS412/413


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

3
Outline
  • Checking methods
  • Why Java arrays are broken
  • Compiling methods
  • Inheritance
  • Compiling fields
  • Multiple inheritance
  • of interfaces
  • of implementations

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

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

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 ?

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?

8
Contravariance/covariance
  • Function arguments may be contravariant
  • Function results may be covariant
  • ?i Ti ? Ti
  • Tr ? Tr
  • T1?...?Tn?Tr ? T1?...?Tn ? Tr
  • Java is conservative!

9
Java arrays
  • Java has array type constructor for any type T,
    T is an array of Ts
  • Java also has subtype rule
  • T1 ? T2
  • T1 ? T2
  • Is this rule safe?

10
Java array subtype problems
  • Elephant ? Animal
  • Animal x
  • Elephant y
  • x y
  • x0 new Rhinoceros() // oops!
  • Assignment as method
  • void setElem (Animal element, int index)
  • void setElem (Elephant element, int index)

11
Compiling methods
  • Methods look like functions, type like
    functionswhat is different?
  • Call sequence use dispatch vector
  • Argument list implicit receiver

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
13
Dispatch vector layouts
foo
A
foo
B
bar
baz
foo
C
A foo
bar
B bar,baz
baz
C quux
quux
14
Call sequence
function
method obj.baz()
f(...)
CALL
CALL
MEM
NAME(f)

i 4 ( 24)
MEM
obj
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)

16
Static methods
  • In Java, can declare methods static -- they have
    no receiver object
  • Exactly like normal functions
  • dont need to enter into dispatch vector
  • dont need implicit extra argument for receiver
  • Treated as methods as way of getting functions
    inside the class scope -- not really methods

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)

18
Inheritance
  • Method code copied down from superclass if not
    overridden by 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

19
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
20
Code Sharing
Machine code for Shape.setCorner
DV
setCorner
LL Point
UR Point
  • Dont actuallyhave to copy code
  • Can inherit withoutsuperclass source

DV
setCorner
LL Point
setColor
UR Point
c Color
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
  • Tx.c MEM(Tx 12)
  • Tx.UR MEM(Tx 8)

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
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!
Write a Comment
User Comments (0)
About PowerShow.com