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

Slides:



Advertisements
Similar presentations
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Advertisements

1 CIS 461 Compiler Design and Construction Fall 2014 Instructor: Hugh McGuire slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module.
1 Lecture 10 Intermediate Representations. 2 front end »produces an intermediate representation (IR) for the program. optimizer »transforms the code in.
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Compilation /15a Lecture 13 Compiling Object-Oriented Programs Noam Rinetzky 1.
Compiler construction in4020 – lecture 10 Koen Langendoen Delft University of Technology The Netherlands.
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
The Functions and Purposes of Translators Code Generation (Intermediate Code, Optimisation, Final Code), Linkers & Loaders.
1 Starting a Program The 4 stages that take a C++ program (or any high-level programming language) and execute it in internal memory are: Compiler - C++
Compilation (Semester A, 2013/14) Lecture 13: Assembler, Linker & Loader Noam Rinetzky Slides credit: Eli Bendersky, Mooly Sagiv & Sanjeev Setia.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
Compiling Object Oriented Programs Mooly Sagiv Chapter
PZ05A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ05A - Abstract data types Programming Language Design.
By Neng-Fa Zhou Compiler Construction CIS 707 Prof. Neng-Fa Zhou
Assembler/Linker/Loader Mooly Sagiv html:// Chapter 4.3.
CS 536 Spring Run-time organization Lecture 19.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
Java for High Performance Computing Jordi Garcia Almiñana 14 de Octubre de 1998 de la era post-internet.
Compiler Summary Mooly Sagiv html://
Run-time Environment and Program Organization
CS 153: Concepts of Compiler Design August 25 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Compiling Object Oriented Programs Mooly Sagiv Chapter 6.2.9, 6.5.
Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#
ITEC 352 Lecture 11 ISA - CPU. ISA (2) Review Questions? HW 2 due on Friday ISA –Machine language –Buses –Memory.
High level & Low level language High level programming languages are more structured, are closer to spoken language and are more intuitive than low level.
Semantic Analysis (Generating An AST) CS 471 September 26, 2007.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
LANGUAGE TRANSLATORS: WEEK 24 TRANSLATION TO ‘INTERMEDIATE’ CODE (overview) Labs this week: Tutorial Exercises on Code Generation.
Runtime Environments Compiler Construction Chapter 7.
1 Comp 104: Operating Systems Concepts Java Development and Run-Time Store Organisation.
Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.
CST320 - Lec 11 Why study compilers? n n Ties lots of things you know together: –Theory (finite automata, grammars) –Data structures –Modularization –Utilization.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Activation Records (in Tiger) CS 471 October 24, 2007.
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
Compiler Construction Dr. Noam Rinetzky and Orr Tamir School of Computer Science Tel Aviv University
Compilation (Semester A, 2013/14) Lecture 13b: Memory Management Noam Rinetzky Slides credit: Eran Yahav 1.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 1, 08/28/03 Prof. Roy Levow.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
Programming Languages
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
Compilers I CNS History Wires Wires Machine Language Machine Language FFBA FFBA No Translation necessary No Translation necessary Assembly Language.
Compilation /16a Lecture 10 Compiling Object-Oriented Programs Noam Rinetzky 1.
Compilation Lecture 13: Course summary: Advanced Topics Noam Rinetzky 1.
Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.
ICS312 Introduction to Compilers Set 23. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
OCR A Level F453: The function and purpose of translators Translators a. describe the need for, and use of, translators to convert source code.
Programming 2 Intro to Java Machine code Assembly languages Fortran Basic Pascal Scheme CC++ Java LISP Smalltalk Smalltalk-80.
Abstract data types Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Chapter 1. Introduction.
Lecture 3 Translation.
CS 404 Introduction to Compiler Design
Assemblers, linkers, loaders
Computer Architecture & Operations I
Introduction to Compiler Construction
Chapter 5- Assembling , Linking, and Executing Programs
CSCI-235 Micro-Computer Applications
Compiler Construction (CS-636)
2.1. Compilers and Interpreters
and Executing Programs
Programming Languages
Intermediate Representations Hal Perkins Autumn 2011
Computer Organization & Compilation Process
Code Generation.
CSE401 Introduction to Compiler Construction
Compiler Construction
Abstract data types Programming Language Design and Implementation
Computer Organization & Compilation Process
Presentation transcript:

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

AST + Sym. Tab. Stages of compilation Source code (program) Lexical Analysis Syntax Analysis Parsing Context Analysis Portable/Retarg etable code generation Target code (executable) Assembly IR TextToken streamAST Code Generation 2

Compilation  Execution AST + Sym. Tab. Source code (program) Lexical Analysis Syntax Analysis Parsing Context Analysis Portable/Retarg etable code generation Target code (executable) IR TextToken streamAST Code Generation Linker Assembler Loader Symbolic Addr Object FileExecutable Fileimage Executing program Runtime System 3

Compilation  Execution AST + Sym. Tab. Source code (program) Lexical Analysis Syntax Analysis Parsing Context Analysis Portable/Retarg etable code generation Linking IR TextToken streamAST Code Generation Object File Image Executable File“Hello World” Executing program Runtime System Loading 4

OO: Compilation  Execution AST + Sym. Tab. Source code (program) Lexical Analysis Syntax Analysis Parsing Context Analysis Portable/Retarg etable code generation Linking IR TextToken streamAST Code Generation Object File Image Executable File“Hello World” Executing program Runtime System Loading 5

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 6

Memory Layout stack grows down (towards lower addresses) heap grows up (towards higher addresses) Heap stack code static data 7

Memory Layout stack grows down (towards lower addresses) heap grows up (towards higher addresses) Heap stack code static data Runtime type information 8

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 9

A Simple Example class 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); } 10

A Simple Example class 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 Truck t 11

A Simple Example class 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 Truck t position=10 Car c passengers=0 12

A Simple Example class 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 13

A Simple Example class 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 14

A Simple Example class 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 15

A Simple Example class 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 16

A Simple Example class 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 17

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

Translation into C (Vehicle) class Vehicle extends object { int pos = 10; void move(int x) { pos = pos + x ; } typedef struct Vehicle { int pos; } Ve; 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; void NewVe(Ve *this){ this  pos = 10; } void moveVe(Ve *this, int x){ this  pos = this  pos + x; } 20

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){ this  pos = 10; } void moveTr(Ve *this, int x){ if (x<55) this  pos = this  pos + x; } 21

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){ this  pos = 10; this  passengers = 0; } void awaitCa(Ca *this, Ve *v){ if (v  pos < this  pos) moveVe(this  pos - v  pos) else MoveCa(this, 10) } 22

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); } 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); } void moveCa() ? 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() ? void moveVe(Ve *this, int x){ this  pos = this  pos + x; } 25

Compiling Simple Classes Fields are handled as records Methods have unique names class A { field a1; field a2; method m1() {…} method m2(int i) {…} } a1 a2 Runtime object m1A m2A Compile-Time Table void m2A(classA *this, int i) { // Body of m2 with any object // field f as this  f … } 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) {…} } a1 a2 Runtime object m1A m2A Compile-Time Table void m2_A(classA *this, int i) { // Body of m2 with any object // field f as this  f … } a.m2(5) m2A(a,5) //m2A(&a,5) 27

Features of OO languages Inheritance Method overriding Polymorphism Dynamic binding 28

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() {…} } 29

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() {…} } 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 a3; method m2() { … a3 … } method m3() {…} } m2 is declared and defined m2 is redefined 31

Method Overriding Redefines functionality Affects semantic analysis a1 a2 Runtime object m1A_A m2A_A Compile-Time Table a1 a2 Runtime object b1 m1A_A m2A_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() {…} } 32

Method Overriding Redefines functionality Affects semantic analysis a1 a2 Runtime object m1A_A m2A_A Compile-Time Table a1 a2 Runtime object b1 m1A_A m2A_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 33

Method Overriding a1 a2 Runtime object m1A_A m2A_A Compile-Time Table a1 a2 Runtime object b1 m1A_A m2A_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) 34

Method Overriding a1 a2 Runtime object m1A_A m2A_A Compile-Time Table a1 a2 Runtime object b1 m1A_A m2A_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) {…} 35

Method Overriding a1 a2 Runtime object m1A_A m2A_A Compile-Time Table a1 a2 Runtime object b1 m1A_A m2A_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) {…} 36

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 37

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 ; a1 a2 b1 Pointer to B Pointer to A inside B (also) classA *a = convert_ptr_to_B_to_ptr_A(b) ; A B 38

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 39

Conceptual Impl. of Dynamic Binding a1 a2 Runtime object m1A_A m2A_A Compile-Time Table a1 a2 Runtime object b1 m1A_A m2A_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) {…} 40

Conceptual Impl. of Dynamic Binding a1 a2 Runtime object m1A_A m2A_A Compile-Time Table a1 a2 Runtime object b1 m1A_A m2A_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) {…} 41

Conceptual Impl. of Dynamic Binding a1 a2 Runtime object m1A_A m2A_A Compile-Time Table a1 a2 Runtime object b1 m1A_A m2A_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) {…} 42

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

More efficient implementation typedef 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){…} 44

More efficient implementation typedef 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 m1A_A m2A_A (Runtime) Dispatch Table vtable p classA *p; 45

More efficient implementation typedef 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 m1A m2A (Runtime) Dispatch Table vtable p classA *p; m1A_A m2A_A Code 46

More efficient implementation typedef 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 m1A_A m2A_A (Runtime) Dispatch Table vtable p classA *p; 47

More efficient implementation typedef 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 vtable p classB *p; 48

More efficient implementation typedef 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 vtable p classB *p; m1A_A m2A_B (Runtime) Dispatch Table m3B_B 49

More efficient implementation typedef 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 vtable p m1A_A m2A_B (Runtime) Dispatch Table m3B_B 50

More efficient implementation typedef 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 vtable p convert_ptr_to_B_to_ptr_to_A(p) m1A_A m2A_B (Runtime) Dispatch Table m3B_B 51

Multiple Inheritance 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(){…} } 52

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 53

A simple implementation Merge dispatch tables of superclases Generate code for upcasts and downcasts 54

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 55

Downcasting ( E  C,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 56

Upcasting ( C,D  E ) 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 57

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(){…} } 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(){…} } 59

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(){…} } 60

Dependent Inheritance The simple solution does not work The positions of nested fields do not agree 61

Independent Inheritance Multiple copies of shared superclasses 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(){…} } 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 c1 a1 vtable c2 a1 m4D_E m5E_E vtable 62

Implementation Use an index table to access fields Access offsets indirectly 63

Implementation 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(){…} } 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 c m4D_E m5E_E Index tables 64

Class Descriptors Runtime information associated with instances Dispatch tables –Invoked methods Index tables Shared between instances of the same class Can have more (reflection) 65

Interface Types Java supports limited form of multiple inheritance Interface consists of several methods but no fields A class can implement multiple interfaces Simpler 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); } 66

Interface Types 67

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) 68

Other OO Features Information hiding –private/public/protected fields –Semantic analysis (context handling) Testing class membership 69

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 70

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 71

Compilation (Semester A, 2013/14) Noam Rinetzky 72

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 73

Conceptual Structure of a Compiler Executable code exe Source text txt Semantic Representation Backend Compiler Frontend Lexical Analysis Syntax Analysis Parsing Semantic Analysis Intermediate Representation (IR) Code Generation 74

Conceptual Structure of a Compiler Executable code exe Source text txt Semantic Representation Backend Compiler Frontend Lexical Analysis Syntax Analysis Parsing Semantic Analysis Intermediate Representation (IR) Code Generation 75

From scanning to parsing ((23 + 7) * x) )x*)7+23(( RPIdOPRPNumOPNumLP Lexical Analyzer program text token stream Parser Grammar: E ... | Id Id  ‘a’ |... | ‘z’ Op(*) Id(b) Num(23)Num(7) Op(+) Abstract Syntax Tree valid syntax error 76

From scanning to parsing ((23 + 7) * x) )x*)7+23(( RPIdOPRPNumOPNumLP Lexical Analyzer program text token stream Parser Grammar: E ... | Id Id  ‘a’ |... | ‘z’ Op(*) Id(b) Num(23)Num(7) Op(+) Abstract Syntax Tree valid syntax error 77

Conceptual Structure of a Compiler Executable code exe Source text txt Semantic Representation Backend Compiler Frontend Lexical Analysis Syntax Analysis Parsing Semantic Analysis Intermediate Representation (IR) Code Generation 78

Context Analysis Op(*) Id(b) Num(23)Num(7) Op(+) Abstract Syntax Tree E1 : int E2 : int E1 + E2 : int Type rules Semantic ErrorValid + Symbol Table 79

Code Generation Op(*) Id(b) Num(23)Num(7) Op(+) Valid Abstract Syntax Tree Symbol Table cgen Frame Manager Verification (possible runtime) Errors/Warnings Intermediate Representation (IR) Executable Codeinputoutput 80

Optimization source code Front end IR Code generator target code Program Analysis Abstract interpretation Can appear in later stages too 81

Conceptual Structure of a Compiler Executable code exe Source text txt Semantic Representation Backend Compiler Frontend Lexical Analysis Syntax Analysis Parsing Semantic Analysis Intermediate Representation (IR) Code Generation 82

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) Lexical Analysis Syntax Analysis Parsing ASTSymbol Table etc. Inter. Rep. (IR) Code Generation Target code (executable) 83

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) Lexical Analysis Syntax Analysis Parsing ASTSymbol Table etc. Inter. Rep. (IR) Code Generation Target code (executable) 84

AST + Sym. Tab. Code generation Source code (program) Lexical Analysis Syntax Analysis Parsing Context Analysis Portable/Retarg etable code generation Target code (executable) Assembly IR TextToken streamAST Code Generation 85

Instruction selection AST + Sym. Tab. Code generation Source code (program) Lexical Analysis Syntax Analysis Parsing Context Analysis Portable/Retarg etable code generation Target code (executable) Assembly IR TextToken streamAST “Naive” IR IR OptimizationIR (TAC) generation “Assembly” (symbolic registers) Peephole optimization Assembly Code Generation register allocation 86

Runtime System (GC) AST + Sym. Tab. Source code (program) Lexical Analysis Syntax Analysis Parsing Context Analysis Portable/Retarg etable code generation Target code (executable) IR TextToken streamAST Code Generation Linker Assembler Loader Symbolic Addr Object FileExecutable Fileimage Executing program Runtime System 87

Compilation  Execution AST + Sym. Tab. Source code (program) Lexical Analysis Syntax Analysis Parsing Context Analysis Portable/Retarg etable code generation Linking IR TextToken streamAST Code Generation Object File ImageExecutable File“Hello World” Executing program Runtime System Loading 88

The End And advanced course next semester And workshop on detecting malicious JavaScripts 89

The End And advanced course next semester And workshop on detecting malicious JavaScripts (using static analysis) And thanks you & good luck! 90