Field O ff sets class Shape { Point LL /* 4 */ , UR; /* 8 */ void setCorner(int which, Point p); CS 4120 } class ColoredRect extends Shape { Introduction to Compilers Color c; /* 12 */ void setColor(Color c_); } Andrew Myers • O ff sets of fields from beginning are same for all subclasses Cornell University • Accesses to fields are indexed loads ColoredRect x; E � x.c � = MEM( E � x � + 12) Lecture 31: Multiple Inheritance E � x.UR � = MEM( E � x � + 8) 16 Nov 09 • Need to know size of superclasses – can be a problem • e.g., Java – field o ff sets resolved at dynamic link/load time CS 4120 Introduction to Compilers 2 Field Alignment Multiple Inheritance • In many processors, a 32-bit load must be to an address • Mechanism: a class may declare multiple divisible by 4, address of 64-bit load must be divisible by 8 superclasses (C++) • In rest (e.g. Pentium), loads are 10 × faster if aligned -- • Java: may implement multiple interfaces, avoids extra load x may inherit code from only one superclass � Fields should be aligned c • Two problems: multiple supertypes, y struct { d multiple superclasses int x; char c; int y; char d; z • What are implications of multiple int z; double e; } supertypes in compiler? e CS 4120 Introduction to Compilers 3 CS 4120 Introduction to Compilers 4
Semantic problems Dispatch vectors break • Problem 1: ambiguity interface Shape { class A { int m(); } void setCorner(int w, Point p); 0 class B { int m(); } } class C extends A, B {} // which m? interface Color { float get(int rgb); 0 • All methods, fields must be uniquely defined void set(int rgb, float value); 1 • Problem 2: field replication } class A { int x; } A class B1 extends A { … } class Blob implements Shape, Color { ... B1 B2 class B2 extends A { … } } class C extends B1, B2 { … } C CS 4120 Introduction to Compilers 5 CS 4120 Introduction to Compilers 6 DV alternatives Inline cache code • Option 1: search with inline cache • Let t o be the receiver object: (Smalltalk, Java) mov t1, [ t o ] cache data – For each class, interface, have table mapping cmp t1, [cacheClass434] (in data segment) method names to method code. Recursively jnz miss walk upward in hierarchy looking for method cacheClass434 call [cacheCode434] name cacheCode434 miss: call slowDispatch – Optimization : at call site, store class and 90% of calls from a site go to code pointer in call site code ( inline object class same code as last call from object caching ). On call, check whether class information same site matches cache. CS 4120 Introduction to Compilers 7 CS 4120 Introduction to Compilers 8
Sparse Dispatch Vectors Option 2: Sparse dispatch vectors interface Shape { • Make sure that two methods never allocated same o ff set: give setCorner void setCorner(int w, Point p); 0 Shape o ff set 0, Color o ff sets 1 and 2. Allow holes in DV! } interface Color { • Some methods can be given same o ff set since they never float get(int rgb); 1 occur in the same DV void set(int rgb, float value); 3 get } • Graph coloring techniques can be used to compute class Blob implements Shape, Color { … } method indices in reasonably optimal way (finding optimum set is NP-complete!) • Advantage: same fast dispatch code as SI case • Disadvantage: requires knowledge of entire type hierarchy (makes separate compilation, dynamic loading di ffi cult) CS 4120 Introduction to Compilers 9 CS 4120 Introduction to Compilers 10 Option 3: Hash tables Dispatch with Hash tables • Idea: don’t try to give all method unique indices; resolve conflicts by checking that entry is correct at dispatch get Fixed # • Use hashing to generate method indices entries set – Precompute hash values! – Some Java implementations setCorner interface Shape { • What if there’s a conflict? Entries containing several methods void setCorner(int w, Point p); 11 point to resolution code } • Basic dispatch code is (almost) identical! interface Color { float get(int rgb); 4 • Advantage: simple, reasonably fast void set(int rgb, float value); 7 • Disadvantage: some wasted space in DV, extra argument for } resolution, slower dispatch if conflict class Blob implements Shape, Color { … } CS 4120 Introduction to Compilers 11 CS 4120 Introduction to Compilers 12
Option 5: Binary decision trees Binary decision tree • Works well if distribution of classes is highly skewed: • Idea: use conditional branches, not indirect jumps branch prediction hardware eliminates branch stall of • Unique class index stored in first object word ~10 cycles Range tests used to select among n possible classes at call site in lg n • time – direct branches to code – Can use profiling to identify common paths for each call site individually Shape x; Color Shape x.SetCorner(…) – 90%/10% : usually a common path 34 to put at top of decision tree … … mov ebx, [eax] RGBColor Blob Rectangle Circle Egg cmp ebx, 1 3 0 1 2 4 jle L1 cmp ebx, 2 • Like sparse DVs: need whole-program analysis je Circle$setCorner Decision tree Circle jmp Egg$setCorner • Indirect jump can have better expected execution L1: cmp ebx, 0 2 je Blob$setCorner time for >2 classes: at most one mispredict jmp Rect$setCorner 0 1 2 3 CS 4120 Introduction to Compilers 13 CS 4120 Introduction to Compilers 14
Recommend
More recommend