runtime environments - stanford universityemail the staff list! midterm graded; will be returned at...
TRANSCRIPT
![Page 1: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/1.jpg)
Runtime Environments Part II
![Page 2: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/2.jpg)
Announcements
● Programming Project 3 Checkpoint due tonight at 11:59PM.● No late submissions accepted.● Ask questions on Piazza!● Stop by office hours!● Email the staff list!
● Midterm graded; will be returned at end of class.● Available outside Gates 178 afterwards.
![Page 3: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/3.jpg)
Where We Are
Lexical Analysis
Semantic Analysis
Syntax Analysis
IR Generation
IR Optimization
Code Generation
Optimization
SourceCode
MachineCode
![Page 4: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/4.jpg)
Implementing Objects
![Page 5: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/5.jpg)
Objects are Hard
● It is difficult to build an expressive and efficient object-oriented language.
● Certain concepts are difficult to implement efficiently:● Dynamic dispatch (virtual functions)● Interfaces● Multiple Inheritance● Dynamic type checking (i.e. instanceof)
● Interfaces are so tricky to get right we won't ask you to implement them in PP4.
![Page 6: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/6.jpg)
Encoding C-Style structs
● A struct is a type containing a collection of named values.
● Most common approach: lay each field out in the order it's declared.
![Page 7: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/7.jpg)
Encoding C-Style structs
● A struct is a type containing a collection of named values.
● Most common approach: lay each field out in the order it's declared.
struct MyStruct { int myInt; char myChar; double myDouble;};
![Page 8: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/8.jpg)
Encoding C-Style structs
● A struct is a type containing a collection of named values.
● Most common approach: lay each field out in the order it's declared.
struct MyStruct { int myInt; char myChar; double myDouble;};
4 Bytes 1 8 Bytes
![Page 9: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/9.jpg)
Encoding C-Style structs
● A struct is a type containing a collection of named values.
● Most common approach: lay each field out in the order it's declared.
struct MyStruct { int myInt; char myChar; double myDouble;};
4 Bytes 1 8 Bytes3 Bytes
![Page 10: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/10.jpg)
Accessing Fields
● Once an object is laid out in memory, it's just a series of bytes.
● How do we know where to look to find a particular field?
● Idea: Keep an internal table inside the
compiler containing the offsets of each field.● To look up a field, start at the base address of
the object and advance forward by the appropriate offset.
4 Bytes 1 8 Bytes3 Bytes
![Page 11: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/11.jpg)
Accessing Fields
● Once an object is laid out in memory, it's just a series of bytes.
● How do we know where to look to find a particular field?
● Idea: Keep an internal table inside the
compiler containing the offsets of each field.● To look up a field, start at the base address of
the object and advance forward by the appropriate offset.
4 Bytes 1 8 Bytes3 Bytes
![Page 12: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/12.jpg)
Field Lookupstruct MyStruct { int x; char y; double z;};
4 Bytes 1 8 Bytes3 Bytes
![Page 13: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/13.jpg)
Field Lookupstruct MyStruct { int x; char y; double z;};
4 Bytes 1 8 Bytes3 Bytes
MyStruct* ms = new MyStruct;ms->x = 137;ms->y = 'A';ms->z = 2.71
![Page 14: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/14.jpg)
Field Lookupstruct MyStruct { int x; char y; double z;};
4 Bytes 1 8 Bytes3 Bytes
MyStruct* ms = new MyStruct;ms->x = 137;ms->y = 'A';ms->z = 2.71
store 137 0 bytes after msstore 'A' 4 bytes after msstore 2.71 8 bytes after ms
![Page 15: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/15.jpg)
OOP without Methods
● Consider the following Decaf code: class Base {
int x;
int y;
}
class Derived extends Base {
int z;
}
● What will Derived look like in memory?
![Page 16: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/16.jpg)
Memory Layouts with Inheritance
![Page 17: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/17.jpg)
Memory Layouts with Inheritance
class Base { int x; int y;};
![Page 18: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/18.jpg)
Memory Layouts with Inheritance
class Base { int x; int y;};
4 Bytes 4 Bytes
![Page 19: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/19.jpg)
Memory Layouts with Inheritance
class Base { int x; int y;};
4 Bytes 4 Bytes
![Page 20: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/20.jpg)
Memory Layouts with Inheritance
class Base { int x; int y;};
4 Bytes 4 Bytes
class Derived extends Base { int z;};
![Page 21: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/21.jpg)
Memory Layouts with Inheritance
class Base { int x; int y;};
4 Bytes 4 Bytes
class Derived extends Base { int z;};
4 Bytes 4 Bytes 4 Bytes
![Page 22: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/22.jpg)
Memory Layouts with Inheritance
class Base { int x; int y;};
4 Bytes 4 Bytes
class Derived extends Base { int z;};
4 Bytes 4 Bytes 4 Bytes
![Page 23: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/23.jpg)
Field Lookup With Inheritance
![Page 24: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/24.jpg)
Field Lookup With Inheritanceclass Base { int x; int y;};
4 Bytes 4 Bytesclass Derived extends Base { int z;};
4 Bytes 4 Bytes 4 Bytes
![Page 25: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/25.jpg)
Field Lookup With Inheritance
Base ms = new Base;ms.x = 137;ms.y = 42;
class Base { int x; int y;};
4 Bytes 4 Bytesclass Derived extends Base { int z;};
4 Bytes 4 Bytes 4 Bytes
![Page 26: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/26.jpg)
Field Lookup With Inheritance
Base ms = new Base;ms.x = 137;ms.y = 42;
class Base { int x; int y;};
4 Bytes 4 Bytesclass Derived extends Base { int z;};
4 Bytes 4 Bytes 4 Bytes
store 137 0 bytes after msstore 42 4 bytes after ms
![Page 27: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/27.jpg)
Field Lookup With Inheritance
Base ms = new Derived;ms.x = 137;ms.y = 42;
class Base { int x; int y;};
4 Bytes 4 Bytesclass Derived extends Base { int z;};
4 Bytes 4 Bytes 4 Bytes
![Page 28: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/28.jpg)
Field Lookup With Inheritance
Base ms = new Derived;ms.x = 137;ms.y = 42;
class Base { int x; int y;};
4 Bytes 4 Bytesclass Derived extends Base { int z;};
4 Bytes 4 Bytes 4 Bytes
![Page 29: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/29.jpg)
Field Lookup With Inheritance
Base ms = new Derived;ms.x = 137;ms.y = 42;
class Base { int x; int y;};
4 Bytes 4 Bytesclass Derived extends Base { int z;};
4 Bytes 4 Bytes 4 Bytes
store 137 0 bytes after msstore 42 4 bytes after ms
![Page 30: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/30.jpg)
Field Lookup With Inheritance
Base ms = new Derived;ms.x = 137;ms.y = 42;
class Base { int x; int y;};
4 Bytes 4 Bytesclass Derived extends Base { int z;};
4 Bytes 4 Bytes 4 Bytes
store 137 0 bytes after msstore 42 4 bytes after ms
Base ms = new Base;ms.x = 137;ms.y = 42;
store 137 0 bytes after msstore 42 4 bytes after ms
![Page 31: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/31.jpg)
Single Inheritance in Decaf
● The memory layout for a class D that extends B is given by the memory layout for B followed by the memory layout for the members of D.● Actually a bit more complex; we'll see why later.
● Rationale: A pointer of type B pointing at a D object still sees the B object at the beginning.
● Operations done on a D object through the B reference guaranteed to be safe; no need to check what B points at dynamically.
![Page 32: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/32.jpg)
What About Member Functions?
● Member functions are mostly like regular functions, but with two complications:● How do we know what receiver object to
use?● How do we know which function to call at
runtime (dynamic dispatch)?
![Page 33: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/33.jpg)
this is Tricky
● Inside a member function, the name this refers to the current receiver object.
● This information (pun intended) needs to be communicated into the function.
● Idea: Treat this as an implicit first parameter.
● Every n-argument member function is really an (n+1)-argument member function whose first parameter is the this pointer.
![Page 34: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/34.jpg)
this is Clever
class MyClass { int x; void myFunction(int arg) { this.x = arg; }}
MyClass m = new MyClass;m.myFunction(137);
![Page 35: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/35.jpg)
this is Clever
class MyClass { int x; void myFunction(int arg) { this.x = arg; }}
MyClass m = new MyClass;m.myFunction(137);
![Page 36: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/36.jpg)
this is Clever
class MyClass { int x;}void MyClass_myFunction(MyClass this, int arg){ this.x = arg;}
MyClass m = new MyClass;m.myFunction(137);
![Page 37: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/37.jpg)
this is Clever
class MyClass { int x;}void MyClass_myFunction(MyClass this, int arg){ this.x = arg;}
MyClass m = new MyClass;m.myFunction(137);
![Page 38: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/38.jpg)
this is Clever
class MyClass { int x;}void MyClass_myFunction(MyClass this, int arg){ this.x = arg;}
MyClass m = new MyClass;MyClass_myFunction(m, 137);
![Page 39: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/39.jpg)
this Rules
● When generating code to call a member function, remember to pass some object as the this parameter representing the receiver object.
● Inside of a member function, treat this as just another parameter to the member function.
● When implicitly referring to a field of this, use this extra parameter as the object in which the field should be looked up.
![Page 40: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/40.jpg)
Implementing Dynamic Dispatch
● Dynamic dispatch means calling a function at runtime based on the dynamic type of an object, rather than its static type.
● How do we set up our runtime environment so that we can efficiently support this?
![Page 41: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/41.jpg)
An Initial Idea
● At compile-time, get a list of every defined class.● To compile a dynamic dispatch, emit IR code for
the following logic:
sif (the object has type A)
call A's version of the function
else if (the object has type B)
call B's version of the function
…
else if (the object has type N)
call N's version of the function.
![Page 42: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/42.jpg)
Analyzing our Approach
● This previous idea has several serious problems.
● What are they?● It's slow.
● Number of checks is O(C), where C is the number of classes the dispatch might refer to.
● Gets slower the more classes there are.
● It's infeasible in most languages.● What if we link across multiple source files?● What if we support dynamic class loading?
![Page 43: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/43.jpg)
An Observation
● When laying out fields in an object, we gave every field an offset.
● Derived classes have the base class fields in the same order at the beginning.
● Can we do something similar with functions?
Base.x
Derived.z
Base.y
Base.x Base.y
Layout of Base
Layout of Derived
![Page 44: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/44.jpg)
Virtual Function Tables class Base { class Derived extends Base { int x; int y; void sayHi() { void sayHi() { Print("Base"); Print("Derived"); } } } }
![Page 45: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/45.jpg)
Virtual Function Tables class Base { class Derived extends Base { int x; int y; void sayHi() { void sayHi() { Print("Base"); Print("Derived"); } } } }
Code forBase.sayHi
Code forDerived.sayHi
![Page 46: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/46.jpg)
Virtual Function Tables class Base { class Derived extends Base { int x; int y; void sayHi() { void sayHi() { Print("Base"); Print("Derived"); } } } }
Code forBase.sayHi
Code forDerived.sayHi
Base.x
![Page 47: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/47.jpg)
Virtual Function Tables class Base { class Derived extends Base { int x; int y; void sayHi() { void sayHi() { Print("Base"); Print("Derived"); } } } }
Code forBase.sayHi
Code forDerived.sayHi
Base.x
Base.x Derived.y
![Page 48: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/48.jpg)
Virtual Function Tables class Base { class Derived extends Base { int x; int y; void sayHi() { void sayHi() { Print("Base"); Print("Derived"); } } } }
Code forBase.sayHi
Code forDerived.sayHi
Base.x
Base.x Derived.y
sayHi
sayHi
![Page 49: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/49.jpg)
Virtual Function Tables class Base { class Derived extends Base { int x; int y; void sayHi() { void sayHi() { Print("Base"); Print("Derived"); } } } }
Code forBase.sayHi
Code forDerived.sayHi
Base.x
Base.x Derived.y
sayHi
sayHi
![Page 50: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/50.jpg)
Virtual Function Tables class Base { class Derived extends Base { int x; int y; void sayHi() { void sayHi() { Print("Base"); Print("Derived"); } } } }
Code forBase.sayHi
Code forDerived.sayHi
Base.x
Base.x Derived.y
sayHi
sayHi
Base b = new Base;b.sayHi();
![Page 51: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/51.jpg)
Virtual Function Tables class Base { class Derived extends Base { int x; int y; void sayHi() { void sayHi() { Print("Base"); Print("Derived"); } } } }
Code forBase.sayHi
Code forDerived.sayHi
Base.x
Base.x Derived.y
sayHi
sayHi
Base b = new Base;b.sayHi();
Let fn = the pointer 0 bytes after bCall fn(b)
![Page 52: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/52.jpg)
Virtual Function Tables class Base { class Derived extends Base { int x; int y; void sayHi() { void sayHi() { Print("Base"); Print("Derived"); } } } }
Code forBase.sayHi
Code forDerived.sayHi
Base.x
Base.x Derived.y
sayHi
sayHi
Base b = new Derived;b.sayHi();
![Page 53: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/53.jpg)
Virtual Function Tables class Base { class Derived extends Base { int x; int y; void sayHi() { void sayHi() { Print("Base"); Print("Derived"); } } } }
Code forBase.sayHi
Code forDerived.sayHi
Base.x
Base.x Derived.y
sayHi
sayHi
Base b = new Derived;b.sayHi();
![Page 54: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/54.jpg)
Virtual Function Tables class Base { class Derived extends Base { int x; int y; void sayHi() { void sayHi() { Print("Base"); Print("Derived"); } } } }
Code forBase.sayHi
Code forDerived.sayHi
Base.x
Base.x Derived.y
sayHi
sayHi
Base b = new Derived;b.sayHi();
Let fn = the pointer 0 bytes after bCall fn(b)
![Page 55: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/55.jpg)
More Virtual Function Tables class Base { class Derived extends Base { int x; int y; void sayHi() { Print("Hi Mom!"); } Base clone() { Derived clone() { return new Base; return new Derived; } } } }
![Page 56: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/56.jpg)
More Virtual Function Tables class Base { class Derived extends Base { int x; int y; void sayHi() { Print("Hi Mom!"); } Base clone() { Derived clone() { return new Base; return new Derived; } } } }
Code forBase.sayHi
Code forBase.clone
Code forDerived.clone
![Page 57: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/57.jpg)
More Virtual Function Tables class Base { class Derived extends Base { int x; int y; void sayHi() { Print("Hi Mom!"); } Base clone() { Derived clone() { return new Base; return new Derived; } } } }
Code forBase.sayHi
Code forBase.clone
Code forDerived.clone
Base.x
sayHi
clone
![Page 58: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/58.jpg)
More Virtual Function Tables class Base { class Derived extends Base { int x; int y; void sayHi() { Print("Hi Mom!"); } Base clone() { Derived clone() { return new Base; return new Derived; } } } }
Code forBase.sayHi
Code forBase.clone
Code forDerived.clone
Base.x
sayHi
clone
![Page 59: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/59.jpg)
More Virtual Function Tables class Base { class Derived extends Base { int x; int y; void sayHi() { Print("Hi Mom!"); } Base clone() { Derived clone() { return new Base; return new Derived; } } } }
Code forBase.sayHi
Code forBase.clone
Code forDerived.clone
Base.x
Base.x
Derived.y
sayHi
sayHi
clone
clone
![Page 60: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/60.jpg)
More Virtual Function Tables class Base { class Derived extends Base { int x; int y; void sayHi() { Print("Hi Mom!"); } Base clone() { Derived clone() { return new Base; return new Derived; } } } }
Code forBase.sayHi
Code forBase.clone
Code forDerived.clone
Base.x
Base.x
Derived.y
sayHi
sayHi
clone
clone
![Page 61: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/61.jpg)
Virtual Function Tables
● A virtual function table (or vtable) is an array of pointers to the member function implementations for a particular class.
● To invoke a member function:● Determine (statically) its index in the vtable.● Follow the pointer at that index in the
object's vtable to the code for the function.● Invoke that function.
![Page 62: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/62.jpg)
Analyzing our Approach
● Advantages:● Time to determine function to call is O(1).● (and a good O(1) too!)
● What are the disadvantages?● Object sizes are larger.
● Each object needs to have space for O(M) pointers.
● Object creation is slower.● Each new object needs to have O(M) pointers set,
where M is the number of member functions.
![Page 63: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/63.jpg)
Analyzing our Approach
● Advantages:● Time to determine function to call is O(1).● (and a good O(1) too!)
● What are the disadvantages?● Object sizes are larger.
● Each object needs to have space for O(M) pointers.
● Object creation is slower.● Each new object needs to have O(M) pointers set,
where M is the number of member functions.
![Page 64: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/64.jpg)
A Common Optimization class Base { class Derived extends Base { int x; int y; void sayHi() { Print("Base"); } Base clone() { Derived clone() { return new Base; return new Derived; } } } }
Code forBase.sayHi
Code forBase.clone
Code forDerived.clone
Base.x
Base.x
Derived.y
sayHi
sayHi
clone
clone
![Page 65: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/65.jpg)
A Common Optimization class Base { class Derived extends Base { int x; int y; void sayHi() { Print("Base"); } Base clone() { Derived clone() { return new Base; return new Derived; } } } }
Code forBase.sayHi
Code forBase.clone
Code forDerived.clone
Base.x
Derived.y
sayHi
sayHi
clone
clone
Base.x
Vtable*
Vtable*
![Page 66: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/66.jpg)
Objects in Memory
Code forBase.sayHi
Code forBase.clone
Code forDerived.clone
Base.x
Derived.y
sayHi
sayHi
clone
clone
Base.x
Vtable*
Vtable*
Base.x
Vtable*
Base.x
Derived.y
Vtable*
![Page 67: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/67.jpg)
Dynamic Dispatch in O(1)
● Create a single instance of the vtable for each class.
● Have each object store a pointer to the vtable.● Can follow the pointer to the table in O(1).● Can index into the table in O(1).● Can set the vtable pointer of a new object in
O(1).● Increases the size of each object by O(1).● This is the solution used in most C++ and
Java implementations.
![Page 68: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/68.jpg)
Vtable Requirements● We've made implicit assumptions about our
language that allow vtables to work correctly.● What are they?● Method calls known statically.
● We can determine at compile-time which methods are intended at each call (even if we're not sure which method is ultimately invoked).
● Single inheritance.● Don't need to worry about building a single vtable
for multiple different classes.
![Page 69: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/69.jpg)
Inheritance in PHPclass Base { public function sayHello() { echo "Hi! I'm Base."; }}
class Derived extends Base { public function sayHello() { echo "Hi! I'm Derived."; }}
![Page 70: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/70.jpg)
Inheritance in PHPclass Base { public function sayHello() { echo "Hi! I'm Base."; }}
class Derived extends Base { public function sayHello() { echo "Hi! I'm Derived."; }}
>
![Page 71: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/71.jpg)
Inheritance in PHPclass Base { public function sayHello() { echo "Hi! I'm Base."; }}
class Derived extends Base { public function sayHello() { echo "Hi! I'm Derived."; }}
$b = new Base();$b->sayHello();
>
![Page 72: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/72.jpg)
Inheritance in PHPclass Base { public function sayHello() { echo "Hi! I'm Base."; }}
class Derived extends Base { public function sayHello() { echo "Hi! I'm Derived."; }}
$b = new Base();$b->sayHello();
>
![Page 73: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/73.jpg)
Inheritance in PHPclass Base { public function sayHello() { echo "Hi! I'm Base."; }}
class Derived extends Base { public function sayHello() { echo "Hi! I'm Derived."; }}
$b = new Base();$b->sayHello();
> Hi! I'm Base.
![Page 74: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/74.jpg)
Inheritance in PHPclass Base { public function sayHello() { echo "Hi! I'm Base."; }}
class Derived extends Base { public function sayHello() { echo "Hi! I'm Derived."; }}
$b = new Base();$b->sayHello();
> Hi! I'm Base.
![Page 75: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/75.jpg)
Inheritance in PHPclass Base { public function sayHello() { echo "Hi! I'm Base."; }}
class Derived extends Base { public function sayHello() { echo "Hi! I'm Derived."; }}
$b = new Base();$b->sayHello();
$d = new Derived();$d->sayHello(); > Hi! I'm Base.
![Page 76: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/76.jpg)
Inheritance in PHPclass Base { public function sayHello() { echo "Hi! I'm Base."; }}
class Derived extends Base { public function sayHello() { echo "Hi! I'm Derived."; }}
$b = new Base();$b->sayHello();
$d = new Derived();$d->sayHello(); > Hi! I'm Base.
![Page 77: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/77.jpg)
Inheritance in PHPclass Base { public function sayHello() { echo "Hi! I'm Base."; }}
class Derived extends Base { public function sayHello() { echo "Hi! I'm Derived."; }}
$b = new Base();$b->sayHello();
$d = new Derived();$d->sayHello(); > Hi! I'm Base.
Hi! I'm Derived.
![Page 78: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/78.jpg)
Inheritance in PHPclass Base { public function sayHello() { echo "Hi! I'm Base."; }}
class Derived extends Base { public function sayHello() { echo "Hi! I'm Derived."; }}
$b = new Base();$b->sayHello();
$d = new Derived();$d->sayHello(); > Hi! I'm Base.
Hi! I'm Derived.
![Page 79: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/79.jpg)
Inheritance in PHPclass Base { public function sayHello() { echo "Hi! I'm Base."; }}
class Derived extends Base { public function sayHello() { echo "Hi! I'm Derived."; }}
$b = new Base();$b->sayHello();
$d = new Derived();$d->sayHello();
$b->missingFunction();
> Hi! I'm Base. Hi! I'm Derived.
![Page 80: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/80.jpg)
Inheritance in PHPclass Base { public function sayHello() { echo "Hi! I'm Base."; }}
class Derived extends Base { public function sayHello() { echo "Hi! I'm Derived."; }}
$b = new Base();$b->sayHello();
$d = new Derived();$d->sayHello();
$b->missingFunction();
> Hi! I'm Base. Hi! I'm Derived.
![Page 81: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/81.jpg)
Inheritance in PHPclass Base { public function sayHello() { echo "Hi! I'm Base."; }}
class Derived extends Base { public function sayHello() { echo "Hi! I'm Derived."; }}
$b = new Base();$b->sayHello();
$d = new Derived();$d->sayHello();
$b->missingFunction();
> Hi! I'm Base. Hi! I'm Derived. ERROR: Base::missingFunction
is not defined
![Page 82: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/82.jpg)
Inheritance in PHPclass Base { public function sayHello() { echo "Hi! I'm Base."; }}
class Derived extends Base { public function sayHello() { echo "Hi! I'm Derived."; }}
$b = new Base();$b->sayHello();
$d = new Derived();$d->sayHello();
$b->missingFunction();
> Hi! I'm Base. Hi! I'm Derived. ERROR: Base::missingFunction
is not defined
![Page 83: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/83.jpg)
Inheritance in PHPclass Base { public function sayHello() { echo "Hi! I'm Base."; }}
class Derived extends Base { public function sayHello() { echo "Hi! I'm Derived."; }}
$b = new Base();$b->sayHello();
$d = new Derived();$d->sayHello();
$b->missingFunction();
$fnName = "sayHello";$b->$fnName();
> Hi! I'm Base. Hi! I'm Derived. ERROR: Base::missingFunction
is not defined
![Page 84: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/84.jpg)
Inheritance in PHPclass Base { public function sayHello() { echo "Hi! I'm Base."; }}
class Derived extends Base { public function sayHello() { echo "Hi! I'm Derived."; }}
$b = new Base();$b->sayHello();
$d = new Derived();$d->sayHello();
$b->missingFunction();
$fnName = "sayHello";$b->$fnName();
> Hi! I'm Base. Hi! I'm Derived. ERROR: Base::missingFunction
is not defined
![Page 85: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/85.jpg)
Inheritance in PHPclass Base { public function sayHello() { echo "Hi! I'm Base."; }}
class Derived extends Base { public function sayHello() { echo "Hi! I'm Derived."; }}
$b = new Base();$b->sayHello();
$d = new Derived();$d->sayHello();
$b->missingFunction();
$fnName = "sayHello";$b->$fnName();
> Hi! I'm Base. Hi! I'm Derived. ERROR: Base::missingFunction
is not defined Hi! I'm Base.
![Page 86: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/86.jpg)
Inheritance in PHPclass Base { public function sayHello() { echo "Hi! I'm Base."; }}
class Derived extends Base { public function sayHello() { echo "Hi! I'm Derived."; }}
$b = new Base();$b->sayHello();
$d = new Derived();$d->sayHello();
$b->missingFunction();
$fnName = "sayHello";$b->$fnName();
> Hi! I'm Base. Hi! I'm Derived. ERROR: Base::missingFunction
is not defined Hi! I'm Base.
![Page 87: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/87.jpg)
PHP Inhibits Vtables
● Call-by-string bypasses the vtable optimization.● Impossible to statically determine contents of any
string.● Would have to determine index into vtable at runtime.
● No static type information on objects.● Impossible to statically determine whether a given
method exists at all.
● Plus a few others:● eval keyword executes arbitrary PHP code; could
introduce new classes or methods.
![Page 88: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/88.jpg)
Inheritance without Vtables class Base { class Derived extends Base { int x; int y; void sayHi() { Derived clone() { Print("Hi!"); return new Derived; } } Base clone() { } return new Base; } }
![Page 89: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/89.jpg)
Inheritance without Vtables
Code forBase.sayHi
Code forBase.clone
Code forDerived.clone
class Base { class Derived extends Base { int x; int y; void sayHi() { Derived clone() { Print("Hi!"); return new Derived; } } Base clone() { } return new Base; } }
![Page 90: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/90.jpg)
Inheritance without Vtables
Code forBase.sayHi
Code forBase.clone
Code forDerived.clone
Base.x
Vtable*
class Base { class Derived extends Base { int x; int y; void sayHi() { Derived clone() { Print("Hi!"); return new Derived; } } Base clone() { } return new Base; } }
![Page 91: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/91.jpg)
Inheritance without Vtables
Code forBase.sayHi
Code forBase.clone
Code forDerived.clone
sayHi
cloneBase.x
Vtable*
class Base { class Derived extends Base { int x; int y; void sayHi() { Derived clone() { Print("Hi!"); return new Derived; } } Base clone() { } return new Base; } }
![Page 92: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/92.jpg)
Inheritance without Vtables
Code forBase.sayHi
Code forBase.clone
Code forDerived.clone
sayHi
cloneBase.x
Vtable*
class Base { class Derived extends Base { int x; int y; void sayHi() { Derived clone() { Print("Hi!"); return new Derived; } } Base clone() { } return new Base; } }
![Page 93: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/93.jpg)
Inheritance without Vtables
Code forBase.sayHi
Code forBase.clone
Code forDerived.clone Base.x
Derived.y
sayHi
cloneBase.x
Vtable*
Vtable*
class Base { class Derived extends Base { int x; int y; void sayHi() { Derived clone() { Print("Hi!"); return new Derived; } } Base clone() { } return new Base; } }
![Page 94: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/94.jpg)
Inheritance without Vtables
Code forBase.sayHi
Code forBase.clone
Code forDerived.clone Base.x
Derived.y
sayHi
sayHi
clone
clone
Base.x
Vtable*
Vtable*
class Base { class Derived extends Base { int x; int y; void sayHi() { Derived clone() { Print("Hi!"); return new Derived; } } Base clone() { } return new Base; } }
![Page 95: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/95.jpg)
Inheritance without Vtables
Code forBase.sayHi
Code forBase.clone
Code forDerived.clone Base.x
Derived.y
sayHi
sayHi
clone
clone
Base.x
Vtable*
Vtable*
class Base { class Derived extends Base { int x; int y; void sayHi() { Derived clone() { Print("Hi!"); return new Derived; } } Base clone() { } return new Base; } }
![Page 96: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/96.jpg)
Code forBase.sayHi
Code forBase.clone
Code forDerived.clone Base.x
Derived.y
"sayHi"
"sayHi"
"clone"
"clone"
Base.x
Info*
Info*
Method Table
Class Info
Class Info
Method Table
Inheritance without Vtables class Base { class Derived extends Base { int x; int y; void sayHi() { Derived clone() { Print("Hi!"); return new Derived; } } Base clone() { } return new Base; } }
![Page 97: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/97.jpg)
Code forBase.sayHi
Code forBase.clone
Code forDerived.clone Base.x
Derived.y
"sayHi"
"clone"
"clone"
Base.x
Info*
Info*
Method Table
Class Info
Class Info
Method Table
Inheritance without Vtables class Base { class Derived extends Base { int x; int y; void sayHi() { Derived clone() { Print("Hi!"); return new Derived; } } Base clone() { } return new Base; } }
![Page 98: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/98.jpg)
Code forBase.sayHi
Code forBase.clone
Code forDerived.clone Base.x
Derived.y
"sayHi"
"clone"
"clone"
Base.x
Info*
Info*
Method Table
Class Info
Class Info
Method Table
Parent Class
Inheritance without Vtables class Base { class Derived extends Base { int x; int y; void sayHi() { Derived clone() { Print("Hi!"); return new Derived; } } Base clone() { } return new Base; } }
![Page 99: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/99.jpg)
A General Inheritance Framework
● Each object stores a pointer to a descriptor for its class.
● Each class descriptor stores● A pointer to the base class descriptor(s).● A pointer to a method lookup table.
● To invoke a method:● Follow the pointer to the method table.● If the method exists, call it.● Otherwise, navigate to the base class and repeat.
● This is slow but can be optimized in many cases; we'll see this later.
![Page 100: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/100.jpg)
Vtables and Interfacesinterface Engine { void vroom();}interface Visible { void draw();}class PaintedEngine implements Engine, Visible { void vroom() { /* … */ } void draw() { /* … */ }}class JetEngine implements Engine { void vroom() { /* … */ }}class Paint implements Visible { void draw() { /* … */ }}
Engine e1 = new PaintedEngine;Engine e2 = new JetEngine;e1.vroom();e2.vroom();Visible v1 = new PaintedEngine;Visibie v2 = new Paint;v1.draw();v2.draw();
![Page 101: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/101.jpg)
Vtables and Interfacesinterface Engine { void vroom();}interface Visible { void draw();}class PaintedEngine implements Engine, Visible { void vroom() { /* … */ } void draw() { /* … */ }}class JetEngine implements Engine { void vroom() { /* … */ }}class Paint implements Visible { void draw() { /* … */ }}
Engine e1 = new PaintedEngine;Engine e2 = new JetEngine;e1.vroom();e2.vroom();Visible v1 = new PaintedEngine;Visibie v2 = new Paint;v1.draw();v2.draw();
vroom drawPaintedEngine vtable
![Page 102: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/102.jpg)
Vtables and Interfacesinterface Engine { void vroom();}interface Visible { void draw();}class PaintedEngine implements Engine, Visible { void vroom() { /* … */ } void draw() { /* … */ }}class JetEngine implements Engine { void vroom() { /* … */ }}class Paint implements Visible { void draw() { /* … */ }}
Engine e1 = new PaintedEngine;Engine e2 = new JetEngine;e1.vroom();e2.vroom();Visible v1 = new PaintedEngine;Visibie v2 = new Paint;v1.draw();v2.draw();
vroom drawPaintedEngine vtable
vroomJetEngine vtable
![Page 103: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/103.jpg)
Vtables and Interfacesinterface Engine { void vroom();}interface Visible { void draw();}class PaintedEngine implements Engine, Visible { void vroom() { /* … */ } void draw() { /* … */ }}class JetEngine implements Engine { void vroom() { /* … */ }}class Paint implements Visible { void draw() { /* … */ }}
Engine e1 = new PaintedEngine;Engine e2 = new JetEngine;e1.vroom();e2.vroom();Visible v1 = new PaintedEngine;Visibie v2 = new Paint;v1.draw();v2.draw();
vroom drawPaintedEngine vtable
vroomJetEngine vtable
(empty) drawPaint vtable
![Page 104: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/104.jpg)
Interfaces with Vtables
● Interfaces complicate vtable layouts because they require interface methods to have consistent positions across all vtables.
● This can fill vtables with useless entries.● For this reason, interfaces are typically
not implemented using pure vtables.● We'll see two approaches for
implementing interfaces efficiently.
![Page 105: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/105.jpg)
Interfaces via String Lookup
● Idea: A hybrid approach.● Use vtables for standard (non-interface)
dispatch.● Use the more general, string-based
lookup for interfaces.
![Page 106: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/106.jpg)
Object Layout with Interfaces class Kitty implements Adorable { interface Adorable { int cuteness; void awww(); void awww() { } Print("Meow"); } void purr() { Print("Purr"); } }
![Page 107: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/107.jpg)
Code forKitty.awww
Code forKitty.purr
awww
purr cuteness
Vtable*
Object Layout with Interfaces class Kitty implements Adorable { interface Adorable { int cuteness; void awww(); void awww() { } Print("Meow"); } void purr() { Print("Purr"); } }
![Page 108: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/108.jpg)
Object Layout with Interfaces class Kitty implements Adorable { interface Adorable { int cuteness; void awww(); void awww() { } Print("Meow"); } void purr() { Print("Purr"); } }
NameTableCode for
Kitty.awww
Code forKitty.purr
awww
purr cuteness
Vtable*
![Page 109: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/109.jpg)
Object Layout with Interfaces class Kitty implements Adorable { interface Adorable { int cuteness; void awww(); void awww() { } Print("Meow"); } void purr() { Print("Purr"); } }
NameTable
"awww"
Code forKitty.awww
Code forKitty.purr
awww
purr cuteness
Vtable*
![Page 110: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/110.jpg)
Object Layout with Interfaces class Kitty implements Adorable { interface Adorable { int cuteness; void awww(); void awww() { } Print("Meow"); } void purr() { Print("Purr"); } }
NameTable
"awww"
Code forKitty.awww
Code forKitty.purr
awww
purr cuteness
Vtable*
![Page 111: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/111.jpg)
Analysis of the Approach
● Dynamic dispatch through object types still O(1).
● Interface dispatches take O(Mn), where M is the number of methods and n is the length of the method name.
● Can easily speed up to O(n) expected by replacing a list of strings with a hash table.
![Page 112: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/112.jpg)
Optimizing Interface Dispatch
● Assign a unique number to each interface method.● Replace hash table of strings with hash table of
integers.● Vtable effectively now a hash table instead of an array.
● Cost to do an interface dispatch now O(1).● (But still more expensive than a standard dynamic
dispatch.)
● Would this work in PHP?● No; can still do string-based lookups directly.
![Page 113: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/113.jpg)
Optimizing Interface Dispatch
● Assign a unique number to each interface method.● Replace hash table of strings with hash table of
integers.● Vtable effectively now a hash table instead of an array.
● Cost to do an interface dispatch now O(1).● (But still more expensive than a standard dynamic
dispatch.)
● Would this work in PHP?● No; can still do string-based lookups directly.
![Page 114: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/114.jpg)
Optimizing Even Further
● Vtable lookups are (comparatively) fast compared to hash table lookups.● Can often do vtable lookup in two
instructions!
● Hashing strings is (comparatively) very slow; hashing integers is (comparatively) slow.
● Can we eliminate the hash lookups in some cases?
![Page 115: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/115.jpg)
An Observationinterface Kitty {
void meow();}
class NyanCat implements Kitty { class Garfield implements Kitty { void meow() { void meow() { Print("Nyan!"); Print("I hate Mondays"); } }} }
Kitty n = new NyanCat;for (i = 0; i < 100; ++i) n.meow();
Kitty g = new Garfield;for (i = 0; i < 100; ++i) g.meow();
![Page 116: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/116.jpg)
An Observation
Kitty n = new NyanCat;for (i = 0; i < 100; ++i) n.meow();
Kitty g = new Garfield;for (i = 0; i < 100; ++i) g.meow();
interface Kitty { void meow();
}
class NyanCat implements Kitty { class Garfield implements Kitty { void meow() { void meow() { Print("Nyan!"); Print("I hate Mondays"); } }} }
Code forNyanCat.meow
Code forGarfield.meow
![Page 117: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/117.jpg)
An Observation
Kitty n = new NyanCat;for (i = 0; i < 100; ++i) n.meow();
Kitty g = new Garfield;for (i = 0; i < 100; ++i) g.meow();
Code forNyanCat.meow
Code forGarfield.meow
interface Kitty { void meow();
}
class NyanCat implements Kitty { class Garfield implements Kitty { void meow() { void meow() { Print("Nyan!"); Print("I hate Mondays"); } }} }
![Page 118: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/118.jpg)
Remembering Dispatches
● A particular interface dispatch site often refers to the same method on the majority of its calls.
● Idea: Have a global variable for each call site that stores● The type of the last object used there, and● What method the call resolved to.
● When doing an interface dispatch, check whether the type of the receiver matches the cached type.● If so, just use the known method.● If not, fall back to the standard string-based dispatch.
● This is called inline caching.
![Page 119: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/119.jpg)
Tradeoffs in Inline Caching
● For monomorphic call sites (only one object type actually getting used), inline caching can be a huge performance win.
● For polymorphic call sites (multiple object types getting used), inline caching can slow down the program.● (Why?)
● A more advanced technique called polymorphic inline caching tries to balance the two by maintaining a small collection of known types.● This optimization is used by many JITs for object-
oriented languages, including Java.
![Page 120: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/120.jpg)
Summary of String-Based Lookup
● Preserves the runtime speed of dispatch through objects.
● Allows flexible dispatch through interfaces.
● Can be optimized if method calls can be resolved statically.
● Can be optimized further using variants of inline caching.
![Page 121: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/121.jpg)
Vtables Revisited
● Recall: Why do interfaces complicate vtable layouts?
● Answer: Interface methods must have a consistent position in all vtables.
● Idea: What if we have multiple vtables per object, one for each interface?● Allows interface methods to be positioned
independently of one another.● Allows for fast vtable lookups relative to string-based
approach.
● This is tricky but effective; most C++ implementations use this approach.
![Page 122: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/122.jpg)
Interfaces and Vtablesinterface Meme {
void run();}interface Cat { void meow();}
class Nyan implements Meme, Cat { void run() { Print("Nyan!"); } void meow() { Print("Nyan"); } void eat() { Print("Yummy Pop Tart!"); }}
![Page 123: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/123.jpg)
Interfaces and Vtablesinterface Meme {
void run();}interface Cat { void meow();}
class Nyan implements Meme, Cat { void run() { Print("Nyan!"); } void meow() { Print("Nyan"); } void eat() { Print("Yummy Pop Tart!"); }}
eat
run
Code forNyan.run
Code forNyan.meow
Code forNyan.eat
Vtable*
meow
![Page 124: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/124.jpg)
Interfaces and Vtablesinterface Meme {
void run();}interface Cat { void meow();}
class Nyan implements Meme, Cat { void run() { Print("Nyan!"); } void meow() { Print("Nyan"); } void eat() { Print("Yummy Pop Tart!"); }}
eat
run
Code forNyan.run
Code forNyan.meow
Code forNyan.eat
Vtable*
meow
run
![Page 125: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/125.jpg)
Interfaces and Vtablesinterface Meme {
void run();}interface Cat { void meow();}
class Nyan implements Meme, Cat { void run() { Print("Nyan!"); } void meow() { Print("Nyan"); } void eat() { Print("Yummy Pop Tart!"); }}
eat
run
Code forNyan.run
Code forNyan.meow
Code forNyan.eat
Vtable*
meow
run
meow
![Page 126: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/126.jpg)
Interfaces and Vtablesinterface Meme {
void run();}interface Cat { void meow();}
class Nyan implements Meme, Cat { void run() { Print("Nyan!"); } void meow() { Print("Nyan"); } void eat() { Print("Yummy Pop Tart!"); }}
eat
run
Code forNyan.run
Code forNyan.meow
Code forNyan.eat
Nyan Vtable
meow
run
meow
Meme Vtable
Cat Vtable
![Page 127: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/127.jpg)
Interfaces and Vtablesinterface Meme {
void run();}interface Cat { void meow();}
class Nyan implements Meme, Cat { void run() { Print("Nyan!"); } void meow() { Print("Nyan"); } void eat() { Print("Yummy Pop Tart!"); }}
eat
run
Code forNyan.run
Code forNyan.meow
Code forNyan.eat
Nyan Vtable
meow
run
meow
Meme Vtable
Cat Vtable
![Page 128: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/128.jpg)
The Problemclass Nyan implements Meme, Cat { /* … */}
class Garfield implements Cat { /* … */}
Cat c1 = new Nyan;Cat c2 = new Garfield;
c1.meow();c2.meow();
![Page 129: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/129.jpg)
The Problemclass Nyan implements Meme, Cat { /* … */}
class Garfield implements Cat { /* … */}
Cat c1 = new Nyan;Cat c2 = new Garfield;
c1.meow();c2.meow();
![Page 130: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/130.jpg)
The Problemclass Nyan implements Meme, Cat { /* … */}
class Garfield implements Cat { /* … */}
Cat c1 = new Nyan;Cat c2 = new Garfield;
c1.meow();c2.meow();
Nyan Vtable
Meme Vtable
Cat Vtable
![Page 131: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/131.jpg)
The Problemclass Nyan implements Meme, Cat { /* … */}
class Garfield implements Cat { /* … */}
Cat c1 = new Nyan;Cat c2 = new Garfield;
c1.meow();c2.meow();
Nyan Vtable
Meme Vtable
Cat Vtable
![Page 132: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/132.jpg)
The Problemclass Nyan implements Meme, Cat { /* … */}
class Garfield implements Cat { /* … */}
Cat c1 = new Nyan;Cat c2 = new Garfield;
c1.meow();c2.meow();
Nyan Vtable
Meme Vtable
Cat Vtable
Garfield Vtable
Cat Vtable
![Page 133: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/133.jpg)
The Problemclass Nyan implements Meme, Cat { /* … */}
class Garfield implements Cat { /* … */}
Cat c1 = new Nyan;Cat c2 = new Garfield;
c1.meow();c2.meow();
Nyan Vtable
Meme Vtable
Cat Vtable
Garfield Vtable
Cat Vtable
![Page 134: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/134.jpg)
The Problemclass Nyan implements Meme, Cat { /* … */}
class Garfield implements Cat { /* … */}
Cat c1 = new Nyan;Cat c2 = new Garfield;
c1.meow();c2.meow();
Nyan Vtable
Meme Vtable
Cat Vtable
Garfield Vtable
Cat Vtable
![Page 135: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/135.jpg)
The Problem
● The offset from the base of the object to a particular interface vtable depends on the dynamic type of the object.
● We cannot generate IR code to do an interface dispatch without knowing where the vtable is.
● We don't seem to have gotten anywhere...
![Page 136: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/136.jpg)
A Partial Solutionclass Nyan implements Meme, Cat { /* … */}
class Garfield implements Cat { /* … */}
Cat c1 = new Nyan;Cat c2 = new Garfield;
c1.meow();c2.meow();
![Page 137: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/137.jpg)
A Partial Solutionclass Nyan implements Meme, Cat { /* … */}
class Garfield implements Cat { /* … */}
Cat c1 = new Nyan;Cat c2 = new Garfield;
c1.meow();c2.meow();
![Page 138: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/138.jpg)
A Partial Solutionclass Nyan implements Meme, Cat { /* … */}
class Garfield implements Cat { /* … */}
Cat c1 = new Nyan;Cat c2 = new Garfield;
c1.meow();c2.meow();
Nyan Vtable
Meme Vtable
Cat Vtable
![Page 139: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/139.jpg)
A Partial Solutionclass Nyan implements Meme, Cat { /* … */}
class Garfield implements Cat { /* … */}
Cat c1 = new Nyan;Cat c2 = new Garfield;
c1.meow();c2.meow();
Nyan Vtable
Meme Vtable
Cat Vtable
![Page 140: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/140.jpg)
A Partial Solutionclass Nyan implements Meme, Cat { /* … */}
class Garfield implements Cat { /* … */}
Cat c1 = new Nyan;Cat c2 = new Garfield;
c1.meow();c2.meow();
Nyan Vtable
Meme Vtable
Cat Vtable
![Page 141: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/141.jpg)
A Partial Solutionclass Nyan implements Meme, Cat { /* … */}
class Garfield implements Cat { /* … */}
Cat c1 = new Nyan;Cat c2 = new Garfield;
c1.meow();c2.meow();
Nyan Vtable
Meme Vtable
Cat Vtable
Garfield Vtable
Cat Vtable
![Page 142: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/142.jpg)
A Partial Solutionclass Nyan implements Meme, Cat { /* … */}
class Garfield implements Cat { /* … */}
Cat c1 = new Nyan;Cat c2 = new Garfield;
c1.meow();c2.meow();
Nyan Vtable
Meme Vtable
Cat Vtable
Garfield Vtable
Cat Vtable
![Page 143: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/143.jpg)
A Partial Solution
● When upcasting an object to an interface type, change where the pointer points so that it sees the vtable pointer for that interface.
● We can now assume an interface reference refers directly to the vtable.
● But there's a serious problem with this implementation...
![Page 144: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/144.jpg)
Looking in the Wrong Placeinterface Cat { void meow();}class Garfield implements Cat { int totalSleep; void meow() { totalSleep --; Print("I'm tired."); }}
Cat g = new Garfield;g.meow();
![Page 145: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/145.jpg)
Looking in the Wrong Placeinterface Cat { void meow();}class Garfield implements Cat { int totalSleep; void meow() { totalSleep --; Print("I'm tired."); }}
Cat g = new Garfield;g.meow();
![Page 146: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/146.jpg)
Looking in the Wrong Placeinterface Cat { void meow();}class Garfield implements Cat { int totalSleep; void meow() { totalSleep --; Print("I'm tired."); }}
Cat g = new Garfield;g.meow();
Garfield Vtable
Cat Vtable
totalSleep
![Page 147: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/147.jpg)
Looking in the Wrong Placeinterface Cat { void meow();}class Garfield implements Cat { int totalSleep; void meow() { totalSleep --; Print("I'm tired."); }}
Cat g = new Garfield;g.meow();
Garfield Vtable
Cat Vtable
totalSleep
![Page 148: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/148.jpg)
Looking in the Wrong Placeinterface Cat { void meow();}class Garfield implements Cat { int totalSleep; void meow() { totalSleep --; Print("I'm tired."); }}
Cat g = new Garfield;g.meow();
Garfield Vtable
Cat Vtable
totalSleep
![Page 149: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/149.jpg)
Looking in the Wrong Placeinterface Cat { void meow();}class Garfield implements Cat { int totalSleep; void meow() { totalSleep --; Print("I'm tired."); }}
Cat g = new Garfield;g.meow();
Code for Garfield::meow(Garfield* this) Look up the integer 8 bytes past 'this' Read its value into memory Subtract one from the value Store the value back into memory
Garfield Vtable
Cat Vtable
totalSleep
![Page 150: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/150.jpg)
Looking in the Wrong Placeinterface Cat { void meow();}class Garfield implements Cat { int totalSleep; void meow() { totalSleep --; Print("I'm tired."); }}
Cat g = new Garfield;g.meow();
Code for Garfield::meow(Garfield* this) Look up the integer 8 bytes past 'this' Read its value into memory Subtract one from the value Store the value back into memory
Garfield Vtable
Cat Vtable
totalSleep
![Page 151: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/151.jpg)
Looking in the Wrong Placeinterface Cat { void meow();}class Garfield implements Cat { int totalSleep; void meow() { totalSleep --; Print("I'm tired."); }}
Cat g = new Garfield;g.meow();
Code for Garfield::meow(Garfield* this) Look up the integer 8 bytes past 'this' Read its value into memory Subtract one from the value Store the value back into memory
Garfield Vtable
Cat Vtable
totalSleep
>:-(
![Page 152: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/152.jpg)
Looking in the Wrong Place
● Interface pointers cannot be used directly as the this pointer in methods calls.● Pointing into the middle of an object, not
the base of the object.● All field offsets will refer to the wrong parts
of memory.
● How can we correct this?
![Page 153: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/153.jpg)
Adding in Deltas
Cat g = new Garfield;g.meow();
Code for Garfield::meow(Garfield* this) Look up the integer 8 bytes past 'this' Read its value into memory Subtract one from the value Store the value back into memory
Garfield Vtable
Cat Vtable
totalSleep
meow
Code forGarfield.meow
![Page 154: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/154.jpg)
Adding in Deltas
Cat g = new Garfield;g.meow();
Code for Garfield::meow(Garfield* this) Look up the integer 8 bytes past 'this' Read its value into memory Subtract one from the value Store the value back into memory
Garfield Vtable
Cat Vtable
totalSleep
meow
Delta: -4 Code forGarfield.meow
![Page 155: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/155.jpg)
Vtable Deltas
● Augment each interface vtable with the offset in bytes the pointer must be corrected to get back to the base of the object.
● A dynamic dispatch then looks like this:● Look up the address of the function to call by following
the vtable pointer and looking at the recovered address.● Look up the amount to adjust the object pointer in the
vtable.● Update the object pointer by adding in the given delta.● Call the function indicated in the vtable.
![Page 156: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/156.jpg)
Analysis of Vtable Deltas
● Cost to invoke a method is O(1) regardless of the number of interfaces.
● Also a fast O(1); typically much better than a hash table lookup.
● Size of an object increases by O(I), where I is the number of interfaces.
● Cost to create an object is O(I), where I is the number of interfaces.● (Why?)
![Page 157: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/157.jpg)
Comparison of Approaches
● String-based lookups have small objects and fast object creation but slow dispatch times.● Only need to set one vtable pointer in the generated
object.● Dispatches require some type of string comparisons.
● Vtable-based lookups have larger objects and slower object creation but faster dispatch times.● Need to set multiple vtable pointers in the generated
object.● Dispatches can be done using simple arithmetic.
![Page 158: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/158.jpg)
Implementing Dynamic Type Checks
![Page 159: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/159.jpg)
Dynamic Type Checks
● Many languages require some sort of dynamic type checking.● Java's instanceof, C++'s dynamic_cast, any
dynamically-typed language.
● May want to determine whether the dynamic type is convertible to some other type, not whether the type is equal.
● How can we implement this?
![Page 160: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/160.jpg)
A Pretty Good Approachclass A { void f() {}}
class B extends A { void f() {}}
class C extends A { void f() {}}
class D extends B { void f() {}}
class E extends C { void f() {}}
![Page 161: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/161.jpg)
A Pretty Good Approachclass A { void f() {}}
class B extends A { void f() {}}
class C extends A { void f() {}}
class D extends B { void f() {}}
class E extends C { void f() {}}
A.f
B.f
C.f
D.f
E.f
![Page 162: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/162.jpg)
A Pretty Good Approachclass A { void f() {}}
class B extends A { void f() {}}
class C extends A { void f() {}}
class D extends B { void f() {}}
class E extends C { void f() {}}
A.f
B.f
C.f
D.f
E.f
Parent
Parent
Parent
Parent
Parent
![Page 163: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/163.jpg)
A Pretty Good Approachclass A { void f() {}}
class B extends A { void f() {}}
class C extends A { void f() {}}
class D extends B { void f() {}}
class E extends C { void f() {}}
A.f
B.f
C.f
D.f
E.f
Parent
Parent
Parent
Parent
Parent
![Page 164: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/164.jpg)
Simple Dynamic Type Checking
● Have each object's vtable store a pointer to its base class.
● To check if an object is convertible to type S at runtime, follow the pointers embedded in the object's vtable upward until we find S or reach a type with no parent.
● Runtime is O(d), where d is the depth of the class in the hierarchy.
● Can we make this faster?
![Page 165: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/165.jpg)
A Marvelous Idea
● There is a fantastically clever way of checking convertibility at runtime in O(1), assuming there are O(1) classes in a hierarchy.
● Assume:● There aren't “too many” classes derived from any
one class (say, 10).● A runtime check of whether an object that is
statically of type A is dynamically of type B is only possible if A ≤ B.
● All types are known statically.
![Page 166: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/166.jpg)
A Marvelous Idea
A.f
B.f C.f
D.f F.f
Parent
Parent Parent
Parent Parent
E.f
Parent
G.f
Parent
![Page 167: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/167.jpg)
A Marvelous Idea
A.f
B.f C.f
D.f F.f
Parent
Parent Parent
Parent Parent
E.f
Parent
G.f
Parent
![Page 168: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/168.jpg)
A Marvelous Idea
A.f
B.f C.f
D.f F.f
1
2 3
2 * 5 3 * 11
E.f
2 * 7
G.f
3 * 13
![Page 169: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/169.jpg)
A Marvelous Idea
A.f
B.f C.f
D.f F.f
1
2 3
10 33
E.f
14
G.f
39
![Page 170: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/170.jpg)
A Marvelous Idea
A.f
B.f C.f
D.f F.f
1
2 3
10 33
E.f
14
G.f
39
A myObject = /* … */if (myObject instanceof C) { /* … */}
![Page 171: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/171.jpg)
A Marvelous Idea
A.f
B.f C.f
D.f F.f
1
2 3
10 33
E.f
14
G.f
39
A myObject = /* … */if (myObject instanceof C) { /* … */}
![Page 172: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/172.jpg)
A Marvelous Idea
A.f
B.f C.f
D.f F.f
1
2 3
10 33
E.f
14
G.f
39
A myObject = /* … */if (myObject->vtable.key % 3 == 0) { /* … */}
![Page 173: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/173.jpg)
Dynamic Typing through Primes
● Assign each class a unique prime number.● (Can reuse primes across unrelated type hierarchies.)
● Set the key of that class to be the product of its prime and all the primes of its superclasses.
● To check at runtime if an object is convertible to type T:● Look up the object's key.● If T's key divides the object's key, the object is convertible to T.● Otherwise, it is not.
● Assuming product of primes fits into an integer, can do this check in O(1).
● Also works with multiple inheritance; prototype C++ implementations using this techinique exist.
![Page 174: Runtime Environments - Stanford UniversityEmail the staff list! Midterm graded; will be returned at end of class. Available outside Gates 178 afterwards. Where We Are Lexical Analysis](https://reader036.vdocuments.us/reader036/viewer/2022063018/5fdbfe3fdc408d6bfa2ceddc/html5/thumbnails/174.jpg)
Next Time
● Three-Address Code IR.● IR Generation.