compilation 0368-3133 (semester a, 2013/14)

89
Compilation 0368-3133 (Semester A, 2013/14) Lecture 14: Compiling Object Oriented Programs Noam Rinetzky Slides credit: Mooly Sagiv 1

Upload: radwan

Post on 23-Feb-2016

25 views

Category:

Documents


0 download

DESCRIPTION

Compilation 0368-3133 (Semester A, 2013/14). Lecture 14: Compiling Object O riented Programs Noam Rinetzky. Slides credit: Mooly Sagiv. Stages of compilation. Source code (program). Lexical Analysis. Syntax Analysis Parsing. Context Analysis. Code Generation. Target code - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Compilation   0368-3133 (Semester A, 2013/14)

1

Compilation 0368-3133 (Semester A, 2013/14)

Lecture 14: Compiling Object Oriented Programs

Noam Rinetzky

Slides credit: Mooly Sagiv

Page 2: Compilation   0368-3133 (Semester A, 2013/14)

2

AST

+ Sy

m. T

ab.

Stages of compilationSource code

(program)

LexicalAnalysis

Syntax Analysis

Parsing

Context Analysis

Portable/Retargetable

code generation

Target code

(executable)

Asse

mbl

y

IRText

Toke

n st

ream

AST

CodeGeneration

Page 3: Compilation   0368-3133 (Semester A, 2013/14)

3

Compilation Execution

AST

+ Sy

m. T

ab.

Source code

(program)

LexicalAnalysis

Syntax Analysis

Parsing

Context Analysis

Portable/Retargetable

code generation

Target code

(executable)

IRText

Toke

n st

ream

AST

CodeGeneration

Link

er

Asse

mbl

er

Load

er

Sym

bolic

Add

r

Obj

ect F

ile

Exec

utab

le F

ile

imag

e

Executing program

Runti

me

Syst

em

Page 4: Compilation   0368-3133 (Semester A, 2013/14)

4

Compilation ExecutionAS

T +

Sym

. Tab

.Syntax

Analysis

Parsing

Context Analysis

Portable/Retargetable

code generation

Linking

IRAST

CodeGeneration

Obj

ect F

ile

Imag

e

Exec

utab

le F

ile

“Hel

lo W

orld

Executing program

Runti

me

Syst

em

Loading

Page 5: Compilation   0368-3133 (Semester A, 2013/14)

5

OO: Compilation ExecutionAS

T +

Sym

. Tab

.Syntax

Analysis

Parsing

Context Analysis

Portable/Retargetable

code generation

Linking

IRAST

CodeGeneration

Obj

ect F

ile

Imag

e

Exec

utab

le F

ile

“Hel

lo W

orld

Executing program

Runti

me

Syst

em

Loading

Page 6: Compilation   0368-3133 (Semester A, 2013/14)

6

Runtime Environment• Mediates between the OS and the programming language• Hides details of the machine from the programmer

– Ranges from simple support functions all the way to a full-fledged virtual machine

• Handles common tasks – Runtime stack (activation records)– Memory management

• Runtime type information– Method invocation– Type conversions

Page 7: Compilation   0368-3133 (Semester A, 2013/14)

7

Memory Layoutstack grows down

(towards lower addresses)

heap grows up (towards higher

addresses)

Heap

stack

code

static data

Page 8: Compilation   0368-3133 (Semester A, 2013/14)

8

Memory Layoutstack grows down

(towards lower addresses)

heap grows up (towards higher

addresses)

Heap

stack

code

static data

Runtime type information

Page 9: Compilation   0368-3133 (Semester A, 2013/14)

9

Object Oriented Programs• Simula, Smalltalk, Modula 3, C++, Java, C#, Python

• Objects (usually of type called class) – Code– Data

• Naturally supports Abstract Data Type implementations

• Information hiding• Evolution & reusability

Page 10: Compilation   0368-3133 (Semester A, 2013/14)

10

A Simple Exampleclass Vehicle extends object { int pos = 10; void move(int x) { position = position + x ; }}

class Truck extends Vehicle { void move(int x){ if (x < 55) pos = pos + x; }}

class Car extends Vehicle { int passengers = 0; void await(vehicle v){ if (v.pos < pos) v.move(pos - v.pos); else this.move(10); }}

class main extends object { void main() { Truck t = new Truck(); Car c = new Car(); Vehicle v = c; c.move(60); v.move(70); c.await(t); }}

Page 11: Compilation   0368-3133 (Semester A, 2013/14)

11

A Simple Exampleclass Vehicle extends object { int pos = 10; void move(int x) { position = position + x ; }}

class Truck extends Vehicle { void move(int x){ if (x < 55) pos = pos + x; }}

class Car extends Vehicle { int passengers = 0; void await(vehicle v){ if (v.pos < pos) v.move(pos - v.pos); else this.move(10); }}

class main extends object { void main() { Truck t = new Truck(); Car c = new Car(); Vehicle v = c; c.move(60); v.move(70); c.await(t); }}

pos =10

Truckt

Page 12: Compilation   0368-3133 (Semester A, 2013/14)

12

A Simple Exampleclass Vehicle extends object { int pos = 10; void move(int x) { pos = pos + x ; }}

class Truck extends Vehicle { void move(int x){ if (x < 55) pos = pos + x; }}

class Car extends Vehicle { int passengers = 0; void await(vehicle v){ if (v.pos < pos) v.move(pos - v.pos); else this.move(10); }}

class main extends object { void main() { Truck t = new Truck(); Car c = new Car(); Vehicle v = c; c.move(60); v.move(70); c.await(t); }}

pos=10

Truckt

position=10

Car

c passengers=0

Page 13: Compilation   0368-3133 (Semester A, 2013/14)

13

A Simple Exampleclass Vehicle extends object { int pos = 10; void move(int x) { pos = pos + x ; }}

class Truck extends Vehicle { void move(int x){ if (x < 55) pos = pos + x; }}

class Car extends Vehicle { int passengers = 0; void await(vehicle v){ if (v.pos < pos) v.move(pos - v.pos); else this.move(10); }}

class main extends object { void main() { Truck t = new Truck(); Car c = new Car(); Vehicle v = c; c.move(60); v.move(70); c.await(t); }}

t

v,c

pos=10

Truck

position=10

Car

passengers=0

Page 14: Compilation   0368-3133 (Semester A, 2013/14)

14

A Simple Exampleclass Vehicle extends object { int pos = 10; void move(int x) { pos = pos + x; }}

class Truck extends Vehicle { void move(int x){ if (x < 55) pos = pos + x; }}

class Car extends Vehicle { int passengers = 0; void await(vehicle v){ if (v.pos < pos) v.move(pos - v.pos); else this.move(10); }}

class main extends object { void main() { Truck t = new Truck(); Car c = new Car(); Vehicle v = c; c.move(60); v.move(70); c.await(t); }}

t

v,c

pos=10

Truck

position=70

Car

passengers=0

Page 15: Compilation   0368-3133 (Semester A, 2013/14)

15

A Simple Exampleclass Vehicle extends object { int pos = 10; void move(int x) { position = position + x ; }}

class Truck extends Vehicle { void move(int x){ if (x < 55) pos = pos + x; }}

class Car extends Vehicle { int passengers = 0; void await(vehicle v){ if (v.pos < pos) v.move(pos - v.pos); else this.move(10); }}

class main extends object { void main() { Truck t = new Truck(); Car c = new Car(); Vehicle v = c; c.move(60); v.move(70); c.await(t); }}

t

v,c

pos=10

Truck

position=140

Car

passengers=0

Page 16: Compilation   0368-3133 (Semester A, 2013/14)

16

A Simple Exampleclass Vehicle extends object { int pos = 10; void move(int x) { position = position + x ; }}

class Truck extends Vehicle { void move(int x){ if (x < 55) pos = pos + x; }}

class Car extends Vehicle { int passengers = 0; void await(vehicle v){ if (v.pos < pos) v.move(pos - v.pos); else this.move(10); }}

class main extends object { void main() { Truck t = new Truck(); Car c = new Car(); Vehicle v = c; c.move(60); v.move(70); c.await(t); }}

t

v,c

pos=10

Truck

position=140

Car

passengers=0

Page 17: Compilation   0368-3133 (Semester A, 2013/14)

17

A Simple Exampleclass Vehicle extends object { int pos = 10; void move(int x) { position = position + x ; }}

class Truck extends Vehicle { void move(int x){ if (x < 55) pos = pos + x; }}

class Car extends Vehicle { int passengers = 0; void await(vehicle v){ if (v.pos < pos) v.move(pos - v.pos); else this.move(10); }}

class main extends object { void main() { Truck t = new Truck(); Car c = new Car(); Vehicle v = c; c.move(60); v.move(70); c.await(t); }}

t

v,c

pos=10

Truck

position=140

Car

passengers=0

Page 18: Compilation   0368-3133 (Semester A, 2013/14)

18

Translation into C (Vehicle)class Vehicle extends object { int pos = 10; void move(int x) { pos = pos + x ; }}

struct Vehicle { int pos;}

Page 19: Compilation   0368-3133 (Semester A, 2013/14)

19

Translation into C (Vehicle)class Vehicle extends object { int pos = 10; void move(int x) { pos = pos + x ; }}

typedef struct Vehicle { int pos;} Ve;

Page 20: Compilation   0368-3133 (Semester A, 2013/14)

20

Translation into C (Vehicle)class Vehicle extends object { int pos = 10; void move(int x) { pos = pos + x ; }}

typedef struct Vehicle { int pos;} Ve;

void NewVe(Ve *this){ thispos = 10;}

void moveVe(Ve *this, int x){ thispos = thispos + x;}

Page 21: Compilation   0368-3133 (Semester A, 2013/14)

21

Translation into C (Truck)class Truck extends Vehicle { void move(int x){ if (x < 55) pos = pos + x; }}

typedef struct Truck { int pos;} Tr;

void NewTr(Tr *this){ thispos = 10;}

void moveTr(Ve *this, int x){ if (x<55) thispos = thispos + x;}

Page 22: Compilation   0368-3133 (Semester A, 2013/14)

22

Naïve Translation into C (Car)class Car extends Vehicle { int passengers = 0; void await(vehicle v){ if (v.pos < pos) v.move(pos – v.pos); else this.move(10); }}

typedef struct Car{ int pos; int passengers;} Ca;

void NewCa (Ca *this){ thispos = 10; thispassengers = 0;}

void awaitCa(Ca *this, Ve *v){ if (vpos < thispos) moveVe(thispos - vpos) else MoveCa(this, 10)}

Page 23: Compilation   0368-3133 (Semester A, 2013/14)

23

Naïve Translation into C (Main)class main extends object { void main() { Truck t = new Truck(); Car c = new Car(); Vehicle v = c; c.move(60); v.move(70); c.await(t); }}

void mainMa(){ Tr *t = malloc(sizeof(Tr)); Ca *c = malloc(sizeof(Ca)); Ve *v = (Ve*) c; moveVe(Ve*) c, 60); moveVe(v, 70); awaitCa(c,(Ve*) t);}

Page 24: Compilation   0368-3133 (Semester A, 2013/14)

24

Naïve Translation into C (Main)class main extends object { void main() { Truck t = new Truck(); Car c = new Car(); Vehicle v = c; c.move(60); v.move(70); c.await(t); }}

void mainMa(){ Tr *t = malloc(sizeof(Tr)); Ca *c = malloc(sizeof(Ca)); Ve *v = (Ve*) c; moveVe(Ve*) c, 60); moveVe(v, 70); awaitCa(c,(Ve*) t);}

void moveCa() ?

Page 25: Compilation   0368-3133 (Semester A, 2013/14)

25

Naïve Translation into C (Main)class main extends object { void main() { Truck t = new Truck(); Car c = new Car(); Vehicle v = c; c.move(60); v.move(70); c.await(t); }}

void mainMa(){ Tr *t = malloc(sizeof(Tr)); Ca *c = malloc(sizeof(Ca)); Ve *v = (Ve*) c; moveVe(Ve*) c, 60); moveVe(v, 70); awaitCa(c,(Ve*) t);}

void moveCa() ?

void moveVe(Ve *this, int x){ thispos = thispos + x;}

Page 26: Compilation   0368-3133 (Semester A, 2013/14)

26

Compiling Simple Classes

• Fields are handled as records• Methods have unique names

class A { field a1; field a2; method m1() {…} method m2(int i) {…}}

a1a2

Runtime object

m1Am2A

Compile-Time Table

void m2A(classA *this, int i) { // Body of m2 with any object // field f as thisf …}

Page 27: Compilation   0368-3133 (Semester A, 2013/14)

27

Compiling Simple Classes

• Fields are handled as records• Methods have unique names

class A { field a1; field a2; method m1() {…} method m2(int i) {…}}

a1a2

Runtime object

m1Am2A

Compile-Time Table

void m2_A(classA *this, int i) { // Body of m2 with any object // field f as thisf …}

a.m2(5)

m2A(a,5) //m2A(&a,5)

Page 28: Compilation   0368-3133 (Semester A, 2013/14)

28

Features of OO languages

• Inheritance• Method overriding• Polymorphism• Dynamic binding

Page 29: Compilation   0368-3133 (Semester A, 2013/14)

29

Handling Single Inheritance• Simple type extension• Type checking module checks consistency• Use prefixing to assign fields in a consistent way

class A { field a1; field a2; method m1() {…} method m2() {…}}

class B extends A { field b1; method m3() {…}}

Page 30: Compilation   0368-3133 (Semester A, 2013/14)

30

Method Overriding• Redefines functionality

– More specific– Can access additional fields

class A { field a1; field a2; method m1() {…} method m2() {…}}

class B extends A { field b1; method m2() { … b1 … } method m3() {…}}

Page 31: Compilation   0368-3133 (Semester A, 2013/14)

31

Method Overriding• Redefines functionality

– More specific– Can access additional fields

class A { field a1; field a2; method m1() {…} method m2() {…}}

class B extends A { field a3; method m2() { … a3 … } method m3() {…}}m2 is declared and

defined

m2 is redefined

Page 32: Compilation   0368-3133 (Semester A, 2013/14)

32

Method Overriding• Redefines functionality• Affects semantic analysis

a1a2

Runtime object

m1A_Am2A_A

Compile-Time Table a1a2

Runtime object

b1

m1A_Am2A_B

Compile-Time Table

m3B_B

class A { field a1; field a2; method m1() {…} method m2() {…}}

class B extends A { field a3; method m2() { … a3 … } method m3() {…}}

Page 33: Compilation   0368-3133 (Semester A, 2013/14)

33

Method Overriding• Redefines functionality• Affects semantic analysis

a1a2

Runtime object

m1A_Am2A_A

Compile-Time Table a1a2

Runtime object

b1

m1A_Am2A_B

Compile-Time Table

m3B_B

class A { field a1; field a2; method m1() {…} method m2() {…}}

class B extends A { field b1; method m2() { … b1 … } method m3() {…}}

declared

defined

Page 34: Compilation   0368-3133 (Semester A, 2013/14)

34

Method Overriding

a1a2

Runtime object

m1A_Am2A_A

Compile-Time Table a1a2

Runtime object

b1

m1A_Am2A_B

Compile-Time Table

m3B_B

class A { field a1; field a2; method m1() {…} method m2() {…}}

class B extends A { field b1; method m2() { … b1 … } method m3() {…}}

a.m2(5) // class(a) = A

m2A_A(a, 5)

b.m2(5) // class(b) = B

m2A_B(b, 5)

Page 35: Compilation   0368-3133 (Semester A, 2013/14)

35

Method Overriding

a1a2

Runtime object

m1A_Am2A_A

Compile-Time Table a1a2

Runtime object

b1

m1A_Am2A_B

Compile-Time Table

m3B_B

class A { field a1; field a2; method m1() {…} method m2() {…}}

class B extends A { field b1; method m2() { … b1 … } method m3() {…}}

typedef struct { field a1; field a2;} A;

void m1A_A(A* this){…}void m2A_A(A* this){…}

typedef struct { field a1; field a2; field b1;} B;

void m2A_B(B* this) {…}void m3B_B(B* this) {…}

Page 36: Compilation   0368-3133 (Semester A, 2013/14)

36

Method Overriding

a1a2

Runtime object

m1A_Am2A_A

Compile-Time Table a1a2

Runtime object

b1

m1A_Am2A_B

Compile-Time Table

m3B_B

a.m2(5) // class(a) = A

m2A_A(a, 5)

b.m2(5) // class(b) = B

m2A_B(b, 5)

typedef struct { field a1; field a2;} A;

void m1A_A(A* this){…}void m2A_A(A* this){…}

typedef struct { field a1; field a2; field b1;} B;

void m2A_B(B* this) {…}void m3B_B(B* this) {…}

Page 37: Compilation   0368-3133 (Semester A, 2013/14)

37

Abstract Methods

• Declared separately – Defined in child classes – E.g., Java abstract classes

• Abstract classes cannot be instantiated

• Handled similarly• Textbook uses “virtual” for abstract

Page 38: Compilation   0368-3133 (Semester A, 2013/14)

38

Handling Polymorphism

• When a class B extends a class A– variable of type pointer to A may actually refer to

object of type B• Upcasting from a subclass to a superclass• Prefixing guarantees validity

class B *b = …;

class A *a = b ;

a1a2b1

Pointer to BPointer to A inside B

(also)

classA *a = convert_ptr_to_B_to_ptr_A(b) ;

AB

Page 39: Compilation   0368-3133 (Semester A, 2013/14)

39

Dynamic Binding

• An object (“pointer”) o declared to be of class A can actually be (“refer”) to a class B

• What does ‘o.m()’ mean?– Static binding – Dynamic binding

• Depends on the programming language rules

• How to implement dynamic binding?– The invoked function is not known at compile time– Need to operate on data of the B and A in consistent way

Page 40: Compilation   0368-3133 (Semester A, 2013/14)

40

Conceptual Impl. of Dynamic Binding

a1a2

Runtime object

m1A_Am2A_A

Compile-Time Table a1a2

Runtime object

b1

m1A_Am2A_B

Compile-Time Table

m3B_B

class A { field a1; field a2; method m1() {…} method m2() {…}}

class B extends A { field b1; method m2() { … a3 … } method m3() {…}}

typedef struct { field a1; field a2;} A;

void m1A_A(A* this){…}void m2A_A(A* this){…}

typedef struct { field a1; field a2; field b1;} B;

void m2A_B(B* this) {…}void m3B_B(B* this) {…}

Page 41: Compilation   0368-3133 (Semester A, 2013/14)

41

Conceptual Impl. of Dynamic Binding

a1a2

Runtime object

m1A_Am2A_A

Compile-Time Table a1a2

Runtime object

b1

m1A_Am2A_B

Compile-Time Table

m3B_B

switch(dynamic_type(p)) { case Dynamic_class_A: m2_A_A(p, 3); case Dynamic_class_B:m2_A_B(convert_ptr_to_A_to_ptr_B(p), 3);}

typedef struct { field a1; field a2;} A;

void m1A_A(A* this){…}void m2A_A(A* this){…}

typedef struct { field a1; field a2; field b1;} B;

void m2A_B(B* this) {…}void m3B_B(B* this) {…}

Page 42: Compilation   0368-3133 (Semester A, 2013/14)

42

Conceptual Impl. of Dynamic Binding

a1a2

Runtime object

m1A_Am2A_A

Compile-Time Table a1a2

Runtime object

b1

m1A_Am2A_B

Compile-Time Table

m3B_B

switch(dynamic_type(p)) { case Dynamic_class_A: m2_A_A(p, 3); case Dynamic_class_B:m2_A_B(convert_ptr_to_A_to_ptr_B(p), 3);}

?

typedef struct { field a1; field a2;} A;

void m1A_A(A* this){…}void m2A_A(A* this){…}

typedef struct { field a1; field a2; field b1;} B;

void m2A_B(B* this) {…}void m3B_B(B* this) {…}

Page 43: Compilation   0368-3133 (Semester A, 2013/14)

43

More efficient implementation

• Apply pointer conversion in sublasses– Use dispatch table to invoke functions– Similar to table implementation of case

void m2A_B(classA *this_A) { Class_B *this = convert_ptr_to_A_ptr_to_A_B(this_A); …}

Page 44: Compilation   0368-3133 (Semester A, 2013/14)

44

More efficient implementationtypedef struct { field a1; field a2;} A;

void m1A_A(A* this){…}void m2A_A(A* this, int x){…}

typedef struct { field a1; field a2; field b1;} B;

void m2A_B(A* thisA, int x){ Class_B *this = convert_ptr_to_A_to_ptr_to_B(thisA); … }

void m3B_B(B* this){…}

Page 45: Compilation   0368-3133 (Semester A, 2013/14)

45

More efficient implementationtypedef struct { field a1; field a2;} A;

void m1A_A(A* this){…}void m2A_A(A* this, int x){…}

typedef struct { field a1; field a2; field b1;} B;

void m2A_B(A* thisA, int x){ Class_B *this = convert_ptr_to_A_to_ptr_to_B(thisA); … }

void m3B_B(B* this){…}

a1

a2

Runtime objectm1A_A

m2A_A

(Runtime) Dispatch Table

vtablep

classA *p;

Page 46: Compilation   0368-3133 (Semester A, 2013/14)

46

More efficient implementationtypedef struct { field a1; field a2;} A;

void m1A_A(A* this){…}void m2A_A(A* this, int x){…}

typedef struct { field a1; field a2; field b1;} B;

void m2A_B(A* thisA, int x){ Class_B *this = convert_ptr_to_A_to_ptr_to_B(thisA); … }

void m3B_B(B* this){…}

a1

a2

Runtime objectm1A

m2A

(Runtime) Dispatch Table

vtablep

classA *p;

m1A_A

m2A_A

Code

Page 47: Compilation   0368-3133 (Semester A, 2013/14)

47

More efficient implementationtypedef struct { field a1; field a2;} A;

void m1A_A(A* this){…}void m2A_A(A* this, int x){…}

typedef struct { field a1; field a2; field b1;} B;

void m2A_B(A* thisA, int x){ Class_B *this = convert_ptr_to_A_to_ptr_to_B(thisA); … }

void m3B_B(B* this){…}

p.m2(3); p dispatch_table m2A(p, 3);

a1

a2

Runtime objectm1A_A

m2A_A

(Runtime) Dispatch Table

vtablep

classA *p;

Page 48: Compilation   0368-3133 (Semester A, 2013/14)

48

More efficient implementationtypedef struct { field a1; field a2;} A;

void m1A_A(A* this){…}void m2A_A(A* this, int x){…}

typedef struct { field a1; field a2; field b1;} B;

void m2A_B(A* thisA, int x){ Class_B *this = convert_ptr_to_A_to_ptr_to_B(thisA); … }

void m3B_B(B* this){…}

a1

a2

Runtime object

b1

m1A_A

m2A_B

(Runtime) Dispatch Table

m3B_B

vtablep

classB *p;

Page 49: Compilation   0368-3133 (Semester A, 2013/14)

49

More efficient implementationtypedef struct { field a1; field a2;} A;

void m1A_A(A* this){…}void m2A_A(A* this, int x){…}

typedef struct { field a1; field a2; field b1;} B;

void m2A_B(A* thisA, int x){ Class_B *this = convert_ptr_to_A_to_ptr_to_B(thisA); … }

void m3B_B(B* this){…}

p.m2(3);

a1

a2

Runtime object

b1

vtablep

classB *p;

m1A_A

m2A_B

(Runtime) Dispatch Table

m3B_B

Page 50: Compilation   0368-3133 (Semester A, 2013/14)

50

More efficient implementationtypedef struct { field a1; field a2;} A;

void m1A_A(A* this){…}void m2A_A(A* this, int x){…}

typedef struct { field a1; field a2; field b1;} B;

void m2A_B(A* thisA, int x){ Class_B *this = convert_ptr_to_A_to_ptr_to_B(thisA); … }

void m3B_B(B* this){…}

p.m2(3); p dispatch_table m2A(p, 3);

a1

a2

Runtime object

b1

vtablep m1A_A

m2A_B

(Runtime) Dispatch Table

m3B_B

Page 51: Compilation   0368-3133 (Semester A, 2013/14)

51

More efficient implementationtypedef struct { field a1; field a2;} A;

void m1A_A(A* this){…}void m2A_A(A* this, int x){…}

typedef struct { field a1; field a2; field b1;} B;

void m2A_B(A* thisA, int x){ Class_B *this = convert_ptr_to_A_to_ptr_to_B(thisA); … }

void m3B_B(B* this){…}

p.m2(3); p dispatch_table m2A( , 3);

a1

a2

Runtime object

b1

vtablep

convert_ptr_to_B_to_ptr_to_A(p)

m1A_A

m2A_B

(Runtime) Dispatch Table

m3B_B

Page 52: Compilation   0368-3133 (Semester A, 2013/14)

52

Multiple Inheritanceclass C { field c1; field c2; method m1(){…} method m2(){…}}

class D { field d1;

method m3() {…} method m4(){…}}

class E extends C, D { field e1;

method m2() {…} method m4() {…} method m5(){…}}

Page 53: Compilation   0368-3133 (Semester A, 2013/14)

53

Multiple Inheritance

• Allows unifying behaviors• But raises semantic difficulties

– Ambiguity of classes– Repeated inheritance

• Hard to implement– Semantic analysis– Code generation

• Prefixing no longer work• Need to generate code for downcasts

• Hard to use

Page 54: Compilation   0368-3133 (Semester A, 2013/14)

54

A simple implementation

• Merge dispatch tables of superclases • Generate code for upcasts and downcasts

Page 55: Compilation   0368-3133 (Semester A, 2013/14)

55

A simple implementation class C { field c1; field c2; method m1(){…} method m2(){…}}

class D { field d1;

method m3() {…} method m4(){…}}

class E extends C, D { field e1;

method m2() {…} method m4() {…} method m5(){…}}

a1

a2

Runtime object

m3D_D

m4D_E

(Runtime) Dispatch Table

m5E_E

vtable

a1

a2

vtable

Pointer to - E - C inside E

Pointer to - D inside E

m1C_C

m2C_E

Page 56: Compilation   0368-3133 (Semester A, 2013/14)

56

Downcasting (EC,D)class C { field c1; field c2; method m1(){…} method m2(){…}}

class D { field d1;

method m3() {…} method m4(){…}}

class E extends C, D { field e1;

method m2() {…} method m4() {…} method m5(){…}}

a1

a2

Runtime object

vtable

a1

a2

vtable

Pointer to - E - C inside E

Pointer to - D inside E

convert_ptr_to_E_to_ptr_to_C(e) = e;

convert_ptr_to_E_to_ptr_to_D(e) = e + sizeof(C);

m3D_D

m4D_E

(Runtime) Dispatch Table

m5E_E

m1C_C

m2C_E

Page 57: Compilation   0368-3133 (Semester A, 2013/14)

57

Upcasting (C,DE)class C { field c1; field c2; method m1(){…} method m2(){…}}

class D { field d1;

method m3() {…} method m4(){…}}

class E extends C, D { field e1;

method m2() {…} method m4() {…} method m5(){…}}

a1

a2

Runtime object

vtable

a1

a2

vtable

Pointer to - E - C inside E

Pointer to - D inside E

convert_ptr_to_C_to_ptr_to_E(c) = c;

convert_ptr_to_D_to_ptr_to_E(d) = d - sizeof(C);

m3D_D

m4D_E

(Runtime) Dispatch Table

m5E_E

m1C_C

m2C_E

Page 58: Compilation   0368-3133 (Semester A, 2013/14)

58

Multiple Inheritance

class C extends A { field c1; field c2; method m1(){…} method m2(){…}}

class D extends A { field d1;

method m3(){…} method m4(){…}}

class E extends C, D { field e1;

method m2() {…} method m4() {…} method m5(){…}}

class A{ field a1; field a2; method m1(){…} method m3(){…}}

Page 59: Compilation   0368-3133 (Semester A, 2013/14)

59

Multiple Inheritance

class C extends A { field c1; field c2; method m1(){…} method m2(){…}}

class D extends A { field d1;

method m3(){…} method m4(){…}}

class E extends C, D { field e1;

method m2() {…} method m4() {…} method m5(){…}}

class A{ field a1; field a2; method m1(){…} method m3(){…}}

Page 60: Compilation   0368-3133 (Semester A, 2013/14)

60

Dependent Multiple Inheritance

class C extends A { field c1; field c2; method m1(){…} method m2(){…}}

class D extends A { field d1;

method m3(){…} method m4(){…}}

class E extends C, D { field e1;

method m2() {…} method m4() {…} method m5(){…}}

class A{ field a1; field a2; method m1(){…} method m3(){…}}

Page 61: Compilation   0368-3133 (Semester A, 2013/14)

61

Dependent Inheritance

• The simple solution does not work• The positions of nested fields do not agree

Page 62: Compilation   0368-3133 (Semester A, 2013/14)

62

Independent Inheritanceclass C extends A{ field c1; field c2; method m1(){…} method m2(){…}}

class D extends A{ field d1;

method m3(){…} method m4(){…}}

class E extends C,D{ field e1;

method m2() {…} method m4() {…} method m5(){…}}

class A{ field a1; field a2; method m1(){…} method m3(){…}}

e1

Runtime E object

m2C_E

m1A_A

(Runtime) Dispatch Table

m3A_D

d1

Pointer to - E - C inside E

Pointer to - D inside E

m1A_C

m3A_A

a2

a2

c1

a1

vtable

c2

a1 m4D_E

m5E_E

vtable

Page 63: Compilation   0368-3133 (Semester A, 2013/14)

63

Implementation

• Use an index table to access fields• Access offsets indirectly

Page 64: Compilation   0368-3133 (Semester A, 2013/14)

64

Implementationclass C extends A{ field c1; field c2; method m1(){…} method m2(){…}}

class D extends A{ field d1;

method m3(){…} method m4(){…}}

class E extends C,D{ field e1;

method m2() {…} method m4() {…} method m5(){…}}

class A{ field a1; field a2; method m1(){…} method m3(){…}}

64

d1

e1

Runtime E object

m2C_E

m1A_A

(Runtime) Dispatch Table

m3A_D

Index tab

Pointer to - E - C inside E

Pointer to - D inside E

m1A_C

m3A_A

vtable

a1

a2

Index tab

vtable

c1

c2

2 3 4 5 8 9 -4 -3 2

m4D_E

m5E_EIndex tables

Page 65: Compilation   0368-3133 (Semester A, 2013/14)

65

Class Descriptors

• Runtime information associated with instances

• Dispatch tables– Invoked methods

• Index tables• Shared between instances of the same class

• Can have more (reflection)

Page 66: Compilation   0368-3133 (Semester A, 2013/14)

66

Interface Types• Java supports limited form of multiple inheritance• Interface consists of several methods but no fields

• A class can implement multiple interfacesSimpler to implement/understand/use

• Implementation: record with 2 pointers:– A separate dispatch table per interface – A pointer to the object

public interface Comparable { public int compare(Comparable o);}

Page 67: Compilation   0368-3133 (Semester A, 2013/14)

68

Dynamic Class Loading• Supported by some OO languages (Java)• At compile time

– the actual class of a given object at a given program point may not be known

• Some addresses have to be resolved at runtime• Compiling c.f() when f is dynamically loaded:

– Fetch the class descriptor d at offset 0 from c– Fetch the address of the method-instance f from

(constant) f offset at d into p– Jump to the routine at address p (saving return address)

Page 68: Compilation   0368-3133 (Semester A, 2013/14)

69

Other OO Features

• Information hiding– private/public/protected fields– Semantic analysis (context handling)

• Testing class membership

Page 69: Compilation   0368-3133 (Semester A, 2013/14)

70

Optimizing OO languages

• Hide additional costs– Replace dynamic by static binding when possible– Eliminate runtime checks– Eliminate dead fields

• Simultaneously generate code for multiple classeså

• Code space is an issue

Page 70: Compilation   0368-3133 (Semester A, 2013/14)

71

Summary

• OO is a programming/design paradigm• OO features complicates compilation

– Semantic analysis– Code generation– Runtime – Memory management

• Understanding compilation of OO can be useful for programmers

Page 71: Compilation   0368-3133 (Semester A, 2013/14)

72

Compilation 0368-3133 (Semester A, 2013/14)

Noam Rinetzky

Page 72: Compilation   0368-3133 (Semester A, 2013/14)

73

What is a compiler?

“A compiler is a computer program that transforms source code written in a programming language (source language) into another language (target language).

The most common reason for wanting to transform source code is to create an executable program.”

--Wikipedia

Page 73: Compilation   0368-3133 (Semester A, 2013/14)

74

Conceptual Structure of a Compiler

Executable code

exe

Sourcetext

txtSemantic

RepresentationBackend

CompilerFrontend

LexicalAnalysi

s

Syntax Analysi

sParsing

Semantic

Analysis

IntermediateRepresentati

on(IR)

CodeGeneration

Page 74: Compilation   0368-3133 (Semester A, 2013/14)

75

Conceptual Structure of a Compiler

Executable code

exe

Sourcetext

txtSemantic

RepresentationBackend

CompilerFrontend

LexicalAnalysi

s

Syntax Analysi

sParsing

Semantic

Analysis

IntermediateRepresentati

on(IR)

CodeGeneration

Page 75: Compilation   0368-3133 (Semester A, 2013/14)

76

From scanning to parsing((23 + 7) * x)

) x * ) 7 + 23 ( (RP Id OP RP Num OP Num LP LP

Lexical Analyzer

program text

token stream

ParserGrammar: E ... | Id Id ‘a’ | ... | ‘z’

Op(*)

Id(b)

Num(23) Num(7)

Op(+)

Abstract Syntax Tree

validsyntaxerror

Page 76: Compilation   0368-3133 (Semester A, 2013/14)

77

From scanning to parsing((23 + 7) * x)

) x * ) 7 + 23 ( (RP Id OP RP Num OP Num LP LP

Lexical Analyzer

program text

token stream

ParserGrammar: E ... | Id Id ‘a’ | ... | ‘z’

Op(*)

Id(b)

Num(23) Num(7)

Op(+)

Abstract Syntax Tree

validsyntaxerror

Page 77: Compilation   0368-3133 (Semester A, 2013/14)

78

Conceptual Structure of a Compiler

Executable code

exe

Sourcetext

txtSemantic

RepresentationBackend

CompilerFrontend

LexicalAnalysi

s

Syntax Analysi

sParsing

Semantic

Analysis

IntermediateRepresentati

on(IR)

CodeGeneration

Page 78: Compilation   0368-3133 (Semester A, 2013/14)

79

Context Analysis

Op(*)

Id(b)

Num(23) Num(7)

Op(+)

Abstract Syntax Tree

E1 : int E2 : int

E1 + E2 : int

Type rules

Semantic Error Valid + Symbol Table

Page 79: Compilation   0368-3133 (Semester A, 2013/14)

80

Code Generation

Op(*)

Id(b)

Num(23) Num(7)

Op(+)

Valid Abstract Syntax TreeSymbol Tablecgen

Frame Manager

Verification (possible runtime)Errors/Warnings

Intermediate Representation (IR)

Executable Codeinput output

Page 80: Compilation   0368-3133 (Semester A, 2013/14)

81

Optimization

sourcecode

Frontend IR Code

generatortargetcode

Program AnalysisAbstract interpretation

Can appear in later stages too

Page 81: Compilation   0368-3133 (Semester A, 2013/14)

82

Conceptual Structure of a Compiler

Executable code

exe

Sourcetext

txtSemantic

RepresentationBackend

CompilerFrontend

LexicalAnalysi

s

Syntax Analysi

sParsing

Semantic

Analysis

IntermediateRepresentati

on(IR)

CodeGeneration

Page 82: Compilation   0368-3133 (Semester A, 2013/14)

83

Register Allocation

• The process of assigning variables to registers and managing data transfer in and out of registers

• Using registers intelligently is a critical step in any compiler– A good register allocator can generate code orders of

magnitude better than a bad register allocator

Source code

(program)

LexicalAnalysis

Syntax Analysis

Parsing

AST SymbolTableetc.

Inter.Rep.(IR)

CodeGeneration

Target code

(executable)

Page 83: Compilation   0368-3133 (Semester A, 2013/14)

84

Register Allocation: Goals

• Reduce number of temporaries (registers)– Machine has at most K registers– Some registers have special purpose

• E.g., pass parameters• Reduce the number of move instructions

– MOVE R1,R2 // R1 R2

Source code

(program)

LexicalAnalysis

Syntax Analysis

Parsing

AST SymbolTableetc.

Inter.Rep.(IR)

CodeGeneration

Target code

(executable)

Page 84: Compilation   0368-3133 (Semester A, 2013/14)

85

AST

+ Sy

m. T

ab.

Code generationSource code

(program)

LexicalAnalysis

Syntax Analysis

Parsing

Context Analysis

Portable/Retargetable

code generation

Target code

(executable)

Asse

mbl

y

IRText

Toke

n st

ream

AST

CodeGeneration

Page 85: Compilation   0368-3133 (Semester A, 2013/14)

86

Inst

ructi

on se

lecti

on

AST

+ Sy

m. T

ab.

Code generationSource code

(program)

LexicalAnalysis

Syntax Analysis

Parsing

Context Analysis

Portable/Retargetable

code generation

Target code

(executable)

Asse

mbl

y

IRText

Toke

n st

ream

AST

“Nai

ve”

IRIR

Opti

miza

tion

IR (T

AC) g

ener

ation

“Ass

embl

y”

(sym

bolic

regi

ster

s)

Peep

hole

opti

miza

tion

Asse

mbl

y

CodeGeneration

regi

ster

allo

catio

n

Page 86: Compilation   0368-3133 (Semester A, 2013/14)

87

Runtime System (GC)

AST

+ Sy

m. T

ab.

Source code

(program)

LexicalAnalysis

Syntax Analysis

Parsing

Context Analysis

Portable/Retargetable

code generation

Target code

(executable)

IRText

Toke

n st

ream

AST

CodeGeneration

Link

er

Asse

mbl

er

Load

er

Sym

bolic

Add

r

Obj

ect F

ile

Exec

utab

le F

ile

imag

e

Executing program

Runti

me

Syst

em

Page 87: Compilation   0368-3133 (Semester A, 2013/14)

88

Compilation Execution

AST

+ Sy

m. T

ab.

Source code

(program)

LexicalAnalysis

Syntax Analysis

Parsing

Context Analysis

Portable/Retargetable

code generation

Linking

IRText

Toke

n st

ream

AST

CodeGeneration

Obj

ect F

ile

Imag

e

Exec

utab

le F

ile

“Hel

lo W

orld

Executing program

Runti

me

Syst

em

Loading

Page 88: Compilation   0368-3133 (Semester A, 2013/14)

89

The End

• And advanced course next semester

• And workshop on detecting malicious JavaScripts

Page 89: Compilation   0368-3133 (Semester A, 2013/14)

90

The End

• And advanced course next semester

• And workshop on detecting malicious JavaScripts (using static analysis)

• And thanks you & good luck!