CS 4120 Introduction to Compilers
Andrew Myers Cornell University
Lecture 31: Multiple Inheritance 16 Nov 09
CS 4120 Introduction to Compilers 2
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_); }
- Offsets of fields from beginning are same for all subclasses
- Accesses to fields are indexed loads
ColoredRect x; Ex.c = MEM(Ex + 12) Ex.UR = MEM(Ex + 8)
- Need to know size of superclasses – can be a problem
- e.g., Java – field offsets resolved at dynamic link/load time
CS 4120 Introduction to Compilers 3
Field Alignment
- In many processors, a 32-bit load must be to an address
divisible by 4, address of 64-bit load must be divisible by 8
- In rest (e.g. Pentium), loads are 10× faster if aligned --
avoids extra load Fields should be aligned struct { int x; char c; int y; char d; int z; double e; } x c y d z e
CS 4120 Introduction to Compilers 4
Multiple Inheritance
- Mechanism: a class may declare multiple
superclasses (C++)
- Java: may implement multiple interfaces,
may inherit code from only one superclass
- Two problems: multiple supertypes,
multiple superclasses
- What are implications of multiple