Runtime Environment...An activation tree is a tree structure representing all of the function calls...
Transcript of Runtime Environment...An activation tree is a tree structure representing all of the function calls...
![Page 1: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/1.jpg)
Runtime EnvironmentPart II
May 8th 2013
Wednesday, May 8, 13
![Page 2: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/2.jpg)
Where We Are
Lexical Analysis
Semantic Analysis
Syntax Analysis
IR Generation
IR Optimization
Code Generation
Optimization
SourceCode
Machine
Code
![Page 3: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/3.jpg)
Review• IR Generation: why
• Data representations
• primitive types: byte, char, float
• arrays: one-dimensional, multi-dimensional
• Encoding functions
• activation record (stack frame)
• activation tree
• runtime stack
• calling conventionsWednesday, May 8, 13
![Page 4: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/4.jpg)
Why Do IR Generation?
● Simplify certain optimizations.
● Machine code has many constraints that inhibit optimization. (Such as?)
● Working with an intermediate language makes optimizations easier and clearer.
● Have many front-ends into a single back-end.
● gcc can handle C, C++, Java, Fortran, Ada, and many other
languages.
● Each front-end translates source to the GENERIC language.
● Have many back-ends from a single front-end.
● Do most optimization on intermediate representation before emitting code targeted at a single machine.
![Page 5: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/5.jpg)
Encoding Multidimensional Arrays
● Often represented as an array of arrays.
● Shape depends on the array type used.
● C-style arrays:
int a[3][2];
a[0][0] a[0][1] a[1][0] a[1][1] a[2][0] a[2][1]
Array of size 2 Array of size 2 Array of size 2
![Page 6: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/6.jpg)
Review: The Stack
● Function calls are often implemented using a stack of activation records (or stack frames).
● Calling a function pushes a new activation record onto the stack.
● Returning from a function pops the current activation record from the stack.
● Questions:
● Why does this work?
● Does this always work?
![Page 7: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/7.jpg)
Activation Trees
● An activation tree is a tree structure representing all of the function calls made by a program on a particular execution.
● Depends on the runtime behavior of a program; can't always be determined at compile-time.
● (The static equivalent is the call graph).
● Each node in the tree is an activation record.
● Each activation record stores a control link to the activation record of the function that invoked it.
![Page 8: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/8.jpg)
Activation Trees
int main() {
Fib(3);
}
int Fib(int n) {
if (n <= 1) return n;
return Fib(n – 1) + Fib(n – 2);
}
main
Fib
n = 3
Fib
n = 1
Fib
n = 2
Fib
n = 0
Fib
n = 1
![Page 9: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/9.jpg)
The runtime stack is an optimization of this spaghetti stack.
![Page 10: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/10.jpg)
A Logical Decaf Stack Frame
Param N
Param N – 1
...
Param 1
Storage for Locals and Temporaries
Stack frame for function f(a, …, n)
Param M
…
Param 1
Storage for Locals and Temporaries
Stack frame for function
g(a, …, m)
![Page 11: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/11.jpg)
Parameter Passing Approaches
● Two common approaches.
● Call-by-value
● Parameters are copies of the values specified as arguments.
● Call-by-reference:
● Parameters are pointers to values specified as parameters.
![Page 12: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/12.jpg)
Implementing Objects
![Page 13: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/13.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 14: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/14.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 15: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/15.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 16: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/16.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 17: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/17.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 18: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/18.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 19: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/19.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 20: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/20.jpg)
Field Lookupstruct MyStruct {
int x;
char y;
double z;
};
4 Bytes 1 8 Bytes3 Bytes
![Page 21: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/21.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 22: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/22.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 ms
store 'A' 4 bytes after ms
store 2.71 8 bytes after ms
![Page 23: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/23.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 24: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/24.jpg)
Memory Layouts with Inheritance
![Page 25: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/25.jpg)
Memory Layouts with Inheritance
class Base {
int x;
int y;
};
![Page 26: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/26.jpg)
Memory Layouts with Inheritance
class Base {
int x;
int y;
};
4 Bytes 4 Bytes
![Page 27: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/27.jpg)
Memory Layouts with Inheritance
class Base {
int x;
int y;
};
4 Bytes 4 Bytes
![Page 28: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/28.jpg)
Memory Layouts with Inheritance
class Base {
int x;
int y;
};
4 Bytes 4 Bytes
class Derived extends Base {
int z;
};
![Page 29: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/29.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 30: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/30.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 31: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/31.jpg)
Field Lookup With Inheritance
![Page 32: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/32.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 33: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/33.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 34: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/34.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 ms
store 42 4 bytes after ms
![Page 35: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/35.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 36: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/36.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 37: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/37.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 ms
store 42 4 bytes after ms
![Page 38: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/38.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 ms
store 42 4 bytes after ms
Base ms = new Base;ms.x = 137;ms.y = 42;
store 137 0 bytes after ms
store 42 4 bytes after ms
![Page 39: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/39.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 40: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/40.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 41: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/41.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 42: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/42.jpg)
this is Clever
class MyClass { int x; void myFunction(int arg) { this.x = arg; }}
MyClass m = new MyClass;m.myFunction(137);
![Page 43: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/43.jpg)
this is Clever
class MyClass { int x; void myFunction(int arg) { this.x = arg;
}
}
MyClass m = new MyClass;m.myFunction(137);
![Page 44: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/44.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 45: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/45.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 46: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/46.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 47: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/47.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 48: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/48.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 49: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/49.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:
s
if (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 50: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/50.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 51: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/51.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 52: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/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"); } } } }
![Page 53: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/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
![Page 54: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/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
![Page 55: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/55.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 56: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/56.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 57: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/57.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 58: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/58.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 59: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/59.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 b
Call fn(b)
![Page 60: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/60.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 61: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/61.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 62: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/62.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 b
Call fn(b)
![Page 63: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/63.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 64: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/64.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 65: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/65.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 66: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/66.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 67: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/67.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 68: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/68.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 69: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/69.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 70: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/70.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 71: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/71.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 72: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/72.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 73: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/73.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 74: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/74.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 75: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/75.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 76: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/76.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 77: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/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."; }}
![Page 78: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/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."; }}
>
![Page 79: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/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();
>
![Page 80: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/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();
>
![Page 81: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/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();
> Hi! I'm Base.
![Page 82: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/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();
> Hi! I'm Base.
![Page 83: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/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();
> Hi! I'm Base.
![Page 84: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/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();> Hi! I'm Base.
![Page 85: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/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();> Hi! I'm Base.
Hi! I'm Derived.
![Page 86: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/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();
> Hi! I'm Base.
Hi! I'm Derived.
![Page 87: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/87.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 88: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/88.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 89: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/89.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 90: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/90.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 91: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/91.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 92: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/92.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 93: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/93.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 94: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/94.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 95: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/95.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 96: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/96.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 97: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/97.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 98: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/98.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 99: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/99.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 100: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/100.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 101: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/101.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 102: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/102.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 103: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/103.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 104: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/104.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 105: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/105.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 106: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/106.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 107: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/107.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 108: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/108.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 109: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/109.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 draw
PaintedEngine vtable
![Page 110: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/110.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 draw
PaintedEngine vtable
vroom
JetEngine vtable
![Page 111: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/111.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 draw
PaintedEngine vtable
vroom
JetEngine vtable
(empty) draw
Paint vtable
![Page 112: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/112.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.
Wednesday, May 8, 13
![Page 113: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/113.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 114: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/114.jpg)
Object Layout with Interfaces class Kitty implements Adorable { interface Adorable { int cuteness; void awww(); void awww() { } Print("Meow"); } void purr() { Print("Purr"); } }
![Page 115: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/115.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 116: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/116.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 117: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/117.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 118: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/118.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 119: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/119.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 120: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/120.jpg)
Implementing Dynamic Type Checks
![Page 121: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/121.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 122: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/122.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 123: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/123.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 124: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/124.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 125: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/125.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 126: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/126.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?
Wednesday, May 8, 13
![Page 127: Runtime Environment...An activation tree is a tree structure representing all of the function calls made by a program on a particular execution. Depends on the runtime behavior of](https://reader033.fdocuments.in/reader033/viewer/2022042921/5f6beb323eff4250fb59172c/html5/thumbnails/127.jpg)
Next Time
● Three-Address Code IR.
● IR Generation.