Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-oriented Languages Compiler Baojian Hua

Similar presentations


Presentation on theme: "Object-oriented Languages Compiler Baojian Hua"— Presentation transcript:

1 Object-oriented Languages Compiler Baojian Hua bjhua@ustc.edu.cn

2 OOP So far, we ’ ve covered most C features expression, statement, function Object-oriented languages are more and more popular due to the industry push this time: compile the class-based OO lang ’ Roadmap: class and objects inheritance & virtual functions RTTI & reflection next time: exception

3 Class “Point” class P2 { int x; int y; P2 (int x, int y) { this.x = x; this.y = y; } void print () { println (this.x, this.y); }

4 Compiling to C // Close all functions: class P2 { int x; int y; P2 (P2 this, int x, int y) { this.x = x; this.y = y; } void print (P2 this) { println (this.x, this.y); }

5 Compiling to C // Hoisting: class P2 { int x; int y; } P2 (P2 this, int x, int y) { this.x = x; this.y = y; } void print (P2 this) { println (this.x, this.y); }

6 Compiling to C // In C’s syntax: struct P2 { int x; int y; }; typedef struct P2 *P2; P2 (P2 this, int x, int y) { this->x = x; this->y = y; } x y P2

7 Client Code // Client code: P2 p = new P2 (3, 4); p.print (); // compiles to: P2 p = malloc (sizeof (*p)); P2(p, 3, 4); print(p); x y P2

8 Moral Difference with C? class = struct + function code object = a region of memory in mem (malloc?) In history, some compilers compile OO to C e.g., Bjarne Stroustrup et.al ’ s Cfront But how does this work with other features? inheritance dynamic dispatch

9 Inheritance // Single inheritance: class P3 extends P2 { int z; P3 (int x, int y, int z) {…} // Note: this is not named “print”. Why? void print3d () { println (this.x, this.y, this.z); }

10 Prefixing // A technique called prefixing: class P3 extends P2 { int x; int y; int z; void print () { … } void print3d () { … } }

11 Compiling to C struct P2 { int x; int y; }; struct P3 { int x; int y; int z; }; void P2_print (P2 this) {…} void P3_print (P2 this) {…} void print3d (P3 this) {…} x y P2 x y P3 z Q: can we put “ z ” before “ x ” or “ y ” ?

12 Virtual functions class P2 { …; void print () {…} } class P3 extends P2 { …; void print () {…} }

13 Dynamic dispatch class P2 { …; void print () {…} } class P3 extends P2 { …; void print () {…} } P2 p; p = new P2 (); p.print (); p = new P3 (); p.print ();

14 Compiling dynamic dispatch Variable ’ s compile-time type is insufficient to determine the specific function to be called We need store in the object the information required The data structure for this purpose is the famous virtual function table or vtable Function call becomes an indirection via vtable

15 Vtable class P2 { …; void print () {…} } class P3 extends P2 { …; void print () {…} } vptr x y P2_print vptr x y P3_print z

16 General Scheme class A { int x1; …; int xn; void f1 (…){} … void fn (…){} }; vptr x1 … A_f1 xn … A_fn vtable

17 Client Code // Client code: P2 p; p = new P2 (3, 4); p.print (); // compiles to: P2 p; p = malloc (sizeof (*p)); // how many bytes? P2(p, 3, 4); ((p->vptr)[0]) (p); // where’s “print”? vptr x y P2_print p

18 Client Code // Client code: P2 p; p = new P2 (3, 4); p.print (); p = new P3 (7, 8, 9); p.print (); vptr x y P2_print p vptr 7 8 9 P3_print

19 Moral Dynamic dispatch is polymorphism: subtyping poly ’ No free lunch: extra space to store vtable dynamic dispatch via indirection (slow?) “ All problems in computer science can be solved by another level of indirection! ” -- Butler Lampson OO = Pointer + Virtual Functions

20 RTTI & reflection RTTI: Run Time Type Identification Ability to identify the type (class) of an object at runtime Reflection: more powerful, can (mostly) do anything at runtime What are they used for? Type identification Inheritance hierarchy string based method invocation exception handling …

21 RTTI P2 p; …; Class c = p.getClass (); println (c.getClassName()); vptr x y P2_print meta name “ P2 ” … …

22 Reflection P2 p; …; Class c = p.getClass (); println (c.getClassName()); c.getMethod (“print”, null); vptr x y P2_print meta name “ P2 ” “ print ”, p …

23 Case study: Visual studio C++ (Microsoft)

24 Multiple Inheritance

25 Template


Download ppt "Object-oriented Languages Compiler Baojian Hua"

Similar presentations


Ads by Google