Top Banner
Object-oriented Languages Compiler Baojian Hua [email protected]
25
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Object-oriented Languages Compiler Baojian Hua bjhua@ustc.edu.cn.

Object-oriented Languages

CompilerBaojian Hua

[email protected]

Page 2: Object-oriented Languages Compiler Baojian Hua bjhua@ustc.edu.cn.

OOP So far, we’ve covered most C features

expression, statement, function Object-oriented languages are more and m

ore 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

Page 3: Object-oriented Languages Compiler Baojian Hua bjhua@ustc.edu.cn.

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); }}

Page 4: Object-oriented Languages Compiler Baojian Hua bjhua@ustc.edu.cn.

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); }}

Page 5: Object-oriented Languages Compiler Baojian Hua bjhua@ustc.edu.cn.

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);}

Page 6: Object-oriented Languages Compiler Baojian Hua bjhua@ustc.edu.cn.

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

Page 7: Object-oriented Languages Compiler Baojian Hua bjhua@ustc.edu.cn.

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

Page 8: Object-oriented Languages Compiler Baojian Hua bjhua@ustc.edu.cn.

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

Page 9: Object-oriented Languages Compiler Baojian Hua bjhua@ustc.edu.cn.

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); }}

Page 10: Object-oriented Languages Compiler Baojian Hua bjhua@ustc.edu.cn.

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

void print3d () { … }}

Page 11: Object-oriented Languages Compiler Baojian Hua bjhua@ustc.edu.cn.

Compiling to Cstruct 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”?

Page 12: Object-oriented Languages Compiler Baojian Hua bjhua@ustc.edu.cn.

Virtual functionsclass P2

{

…;

void print () {…}

}

class P3 extends P2

{

…;

void print () {…}

}

Page 13: Object-oriented Languages Compiler Baojian Hua bjhua@ustc.edu.cn.

Dynamic dispatchclass P2

{

…;

void print () {…}

}

class P3 extends P2

{

…;

void print () {…}

}

P2 p;

p = new P2 ();

p.print ();

p = new P3 ();

p.print ();

Page 14: Object-oriented Languages Compiler Baojian Hua bjhua@ustc.edu.cn.

Compiling dynamic dispatch Variable’s compile-time type is insufficien

t 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

Page 15: Object-oriented Languages Compiler Baojian Hua bjhua@ustc.edu.cn.

Vtableclass P2{ …; void print () {…}}

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

vptrx

y

P2_print

vptrx

y

P3_print

z

Page 16: Object-oriented Languages Compiler Baojian Hua bjhua@ustc.edu.cn.

General Schemeclass A

{

int x1;

…;

int xn;

void f1 (…){}

void fn (…){}

};

vptrx1

A_f1

xn

A_fn

vtable

Page 17: Object-oriented Languages Compiler Baojian Hua bjhua@ustc.edu.cn.

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”?

vptrx

y

P2_print

p

Page 18: Object-oriented Languages Compiler Baojian Hua bjhua@ustc.edu.cn.

Client Code// Client code:P2 p;p = new P2 (3, 4);p.print ();p = new P3 (7, 8, 9);p.print ();

vptrx

y

P2_print

p

vptr7

8

9

P3_print

Page 19: Object-oriented Languages Compiler Baojian Hua bjhua@ustc.edu.cn.

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 solve

d by another level of indirection!” -- Butler Lampson

OO = Pointer + Virtual Functions

Page 20: Object-oriented Languages Compiler Baojian Hua bjhua@ustc.edu.cn.

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 …

Page 21: Object-oriented Languages Compiler Baojian Hua bjhua@ustc.edu.cn.

RTTIP2 p;

…;

Class c = p.getClass ();

println (c.getClassName());

vptrx

y

P2_print

meta name“P2”

Page 22: Object-oriented Languages Compiler Baojian Hua bjhua@ustc.edu.cn.

ReflectionP2 p;

…;

Class c = p.getClass ();

println (c.getClassName());

c.getMethod (“print”, null);

vptrx

y

P2_print

meta name“P2”

“print”, p

Page 23: Object-oriented Languages Compiler Baojian Hua bjhua@ustc.edu.cn.

Case study:Visual studio C++ (Microsoft)

Page 24: Object-oriented Languages Compiler Baojian Hua bjhua@ustc.edu.cn.

Multiple Inheritance

Page 25: Object-oriented Languages Compiler Baojian Hua bjhua@ustc.edu.cn.

Template