Compilation of Object-Oriented Features
1. New Challenges
OO adds classes, inheritance, dynamic dispatch, overriding, constructors — compiler must lay out objects, resolve methods at run time, and enforce access.
2. Object Layout + VTables
class B extends A { int y; void foo(); }
object: [vtable ptr][A fields][B fields y]
vtable(B): [typeinfo][A::bar][B::foo (override)][B::baz]
call x.foo(): load vptr → index foo → CALL *addr (dynamic)
- Inheritance: base prefix preserved (upcast = no-op).
- Override: vtable slot replaced; overload resolved statically by signature.
- super/this: this passed as hidden first param.
3. Other OO Tasks
| Feature | Compile strategy |
|---|---|
| Construction | chain base→derived ctors; vptr set per level |
| instanceof/cast | RTTI tag check; downcast verified |
| Interface | fat vtable or itable search |
| Multiple inheritance | multiple base prefixes + thunks adjusting this |
| GC | vtable holds mark/scan descriptors |
4. Optimizations
Devirtualize (final/sealed or CHA proves one target → direct call + inline); inline caches remember last class at call site.
5. Exam Points
- Draw object + vtable for inheritance/override — 7 marks, very frequent.
- Dynamic dispatch steps — 5 marks, frequent.
- Constructor/RTTI/multiple inheritance handling — 3-5 marks, frequent.