Compiling Object Oriented Programs Mooly Sagiv Chapter 6.2.9, 6.5.

Slides:



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

Compilation (Semester A, 2013/14) Lecture 14: Compiling Object Oriented Programs Noam Rinetzky Slides credit: Mooly Sagiv 1.
Programming Languages and Paradigms
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Compiler construction in4020 – lecture 10 Koen Langendoen Delft University of Technology The Netherlands.
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
Portability and Safety Mahdi Milani Fard Dec, 2006 Java.
Assembler/Linker/Loader Mooly Sagiv html:// Chapter 4.3 J. Levine: Linkers & Loaders
Compiling Object Oriented Programs Mooly Sagiv Chapter
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
Introduction to Advanced Topics Chapter 1 Mooly Sagiv Schrierber
Chapter 9 Imperative and object-oriented languages 1.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Control Flow Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
1 Control Flow Analysis Mooly Sagiv Tel Aviv University Textbook Chapter 3
1 Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 24: Dynamic Binding COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Assembler, Linker and OO Paradigm Professor Yihjia Tsai Tamkang University.
Inheritance and Polymorphism CS351 – Programming Paradigms.
C++ fundamentals.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Adapted from Prof. Necula UCB CS 1641 Overview of COOL ICOM 4029 Lecture 2 ICOM 4029 Fall 2008.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
The Procedure Abstraction, Part VI: Inheritance in OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
The Procedure Abstraction, Part V: Support for OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
Guided Notes Ch. 9 ADT and Modules Ch. 10 Object-Oriented Programming PHP support for OOP and Assignment 4 Term project proposal C++ and Java Designer.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
11/12/2015© Hal Perkins & UW CSEL-1 CSE P 501 – Compilers Code Shape II – Objects & Classes Hal Perkins Winter 2008.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Compiling Objects in Class-Based Languages CS 153: Compilers.
Object-Oriented Programming Chapter Chapter
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
ISBN Object-Oriented Programming Chapter Chapter
Introduction to Object-Oriented Programming Lesson 2.
Compilation /16a Lecture 10 Compiling Object-Oriented Programs Noam Rinetzky 1.
Compilation Lecture 13: Course summary: Advanced Topics Noam Rinetzky 1.
1 Lecture 23: Dynamic Binding (Section ) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos.
Lecture 12 Implementation Issues with Polymorphism.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 20 – C++ Subclasses and Inheritance.
CSCE 240 – Intro to Software Engineering Lecture 3.
Design issues for Object-Oriented Languages
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
Types of Programming Languages
Object Oriented Analysis and Design
Compiler Design 18. Object Oriented Semantic Analysis (Symbol Tables, Type Checking) Kanat Bolazar March 30, 2010.
The Procedure Abstraction Part V: Run-time Structures for OOLs
Interfaces.
More Object-Oriented Programming
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Inheritance and Polymorphism
Programming Languages and Paradigms
ICOM 4029 Fall 2003 Lecture 2 (Adapted from Prof. Necula UCB CS 164)
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Presentation transcript:

Compiling Object Oriented Programs Mooly Sagiv Chapter 6.2.9, 6.5

Subjects OO programs Objects Types Features of OO languages Optimizations for OO languages Handling modules Summary

Object Oriented Programs Objects (usually of type called class) –Code –Data Naturally supports Abstract Data Type implementations Information hiding Evolution & reusability Examples: C++, Eifel, Java, Modula 3, Smalltalk, Cool

A Simple Example class Vehicle extends object { int position = 10; void move(int x) { position = position + x ; } class Car extends Vehicle { int passengers = 0 ; void await(vehicle v) { if (v.position < position) v.move(position-v.position); else this.move(10); } class Truck extends Vehicle { void move(int x) { if (x < 55) position = position+x; } 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);} } position=10 Truck t

A Simple Example class Vehicle extends object { int position = 10; void move(int x) { position = position + x ; } class Car extends Vehicle { int passengers = 0 ; void await(vehicle v) { if (v.position < position) v.move(position-v.position); else this.move(10); } class Truck extends Vehicle { void move(int x) { if (x < 55) position = position+x; } 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); }} position=10 Truck t passengers=0 position=10 Car c

A Simple Example class Vehicle extends object { int position = 10; void move(int x) { position = position + x ; } class Car extends Vehicle { int passengers = 0 ; void await(vehicle v) { if (v.position < position) v.move(position-v.position); else this.move(10); } class Truck extends Vehicle { void move(int x) { if (x < 55) position = position+x; } 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);}} position=10 Truck t passengers=0 position=10 Car c v

A Simple Example class Vehicle extends object { int position = 10; void move(int x) { position = position + x ; } class Car extends Vehicle { int passengers = 0 ; void await(vehicle v) { if (v.position < position) v.move(position-v.position); else this.move(10); } class Truck extends Vehicle { void move(int x) { if (x < 55) position = position+x; } 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);}} position=10 Truck t passengers=0 position=10 Car c v

A Simple Example class Vehicle extends object { int position = 10; void move(int x) { position = position + x ; } class Car extends Vehicle { int passengers = 0 ; void await(vehicle v) { if (v.position < position) v.move(position-v.position); else this.move(10); } class Truck extends Vehicle { void move(int x) { if (x < 55) position = position+x; } 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);}} position=10 Truck t passengers=0 position=70 Car c v

A Simple Example class Vehicle extends object { int position = 10; void move(int x) { position = position + x ; } class Car extends Vehicle { int passengers = 0 ; void await(vehicle v) { if (v.position < position) v.move(position-v.position); else this.move(10); } class Truck extends Vehicle { void move(int x) { if (x < 55) position = position+x; } 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); }} position=10 Truck t passengers=0 position=140 Car c v

Translation into C (Vehicle) class Vehicle extends object { int position = 10; void move(int x) { position = position + x ; } struct Vehicle { int position ; } void New_V(struct Vehicle *this) { this  position = 10; } void move_V(struct Vehicle *this, int x) { this  position=this  position + x; }

Translation into C(Truck) class Truck extends Vehicle { void move(int x) { if (x < 55) position = position+x; } struct Truck { int position ; } void New_T(struct Truck *this) { this  position = 10; } void move_T(struct Truck *this, int x) { if (x <55) this  position=this  position + x; }

Naïve Translation into C(Car) struct Car { int position ; int passengers; } void New_C(struct Car *this) { this  position = 10; this  passengers = 0; } void await_C(struct Car *this, struct Vehicle *v) { if (v  position < this  position ) move_V(this  position - v  position ) else Move_C(this, 10) ;} class Car extends Vehicle { int passengers = 0 ; void await(vehicle v) { if (v.position < position) v.move(position-v.position); else this.move(10); }

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 main_M() { struct Truck *t = malloc(1, sizeof(struct Truck)); struct Car *c= malloc(1, sizeof(struct Car)); struct Vehicle *v = (struct Vehicle*) c; move_V((struct Vehicle*) c, 60); move_V(v, 70); await_C(c,(struct Vehicle *) t); }

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 m1_A m2_A Compile-Time Table void m2_A(class_A *this, int i) { Body of m2 with any object field x as this  x } a.m2(5) m2_A(&a, 5)

Features of OO languages Inheritance Method overriding Polymorphism Dynamic binding

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() { … } } a1 a2 Runtime object m1_A m2_A Compile-Time Table class B extends A { field a3; method m3() { … } } a1 a2 Runtime object a3 m1_A m2_A Compile-Time Table m3_B

Method Overriding Redefines functionality class A { field a1; field a2; method m1() { … } method m2() { … } } class B extends A { field a3; method m2() { … } method m3() { … } } m2 is declared and defined m2 is redefined

Method Overriding Redefines functionality Affects semantic analysis class A { field a1; field a2; method m1() { … } method m2() { … } } class B extends A { field a3; method m2() { … } method m3() { … } } a1 a2 Runtime object m1_A_A m2_A_A Compile-Time Table a1 a2 Runtime object a3 m1_A_A m2_A_B Compile-Time Table m3_B_B

Method Overriding class A { field a1; field a2; method m1() { … } method m2() { … } } class B extends A { field a3; method m2() { … } method m3() { … } } a1 a2 Runtime object m1_A_A m2_A_A Compile-Time Table a1 a2 Runtime object a3 m1_A_A m2_A_B Compile-Time Table m3_B_B a.m2 () // class(a)=A m2_A_A (&a) a.m2 () // class(a)=B m2_A_B (&a)  

Method Overriding (C) struct class_A { field a1; field a2; } void m1_A_A(class_A *this) { … } void m2_A_A(class_A *this, int x) ) { … } struct class_B { field a1; field a2; field a3; } void m2_A_B(class_B *this, int x) { … } void m3_B_B(class B *this) { … } } a1 a2 Runtime object m1_A_A m2_A_A Compile-Time Table a1 a2 Runtime object a3 m1_A_A m2_A_B Compile-Time Table m3_B_B a.m2 (5) // class(a)=A m2_A_A (&a, 5) a.m2 (5) // class(a)=B m2_A_B (&a, 5)  

Abstract Methods Declared separately –Defined in child classes Java abstract classes Handled similarly Textbook uses “Virtual” for abstract

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 class A *a=convert_ptr_to_B_to_ptr_A(b) ; 

Dynamic Binding An object o of class A can 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

Conceptual Implementation of Dynamic Binding struct class_A { field a1; field a2; } void m1_A_A(class_A *this) { … } void m2_A_A(class_A *this, int x) ) { … } struct class_B { field a1; field a2; field a3; } void m2_A_B(class_B *this, int x) { … } void m3_B_B(class B *this) { … } } a1 a2 Runtime object m1_A_A m2_A_A Compile-Time Table a1 a2 Runtime object a3 m1_A_A m2_A_B Compile-Time Table m3_B_B p.m2(3); 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); }

More efficient implementation Apply pointer conversion in sublasses void m2_A_B(class A *this_A, int x) { Class_B *this = convert_ptr_to_A_ptr_to_A_B(this_A); … } Use dispatch table to invoke functions Similar to table implementation of case

struct class_A { field a1; field a2; } void m1_A_A(class_A *this) { … } void m2_A_A(class_A *this, int x) ) { … } struct class_B { field a1; field a2; field a3; } void m2_A_B(class_A *this_A, int x) { Class_B *this = convert_ptr_to_A_to_ptr_to_B(this_A); … } void m3_B_B(class A *this_A) { … } } p.m2(3); p  dispatch_table  m2_A(p, 3);

struct class_A { field a1; field a2; } void m1_A_A(class_A *this) { … } void m2_A_A(class_A *this, int x) ) { … } struct class_B { field a1; field a2; field a3; } void m2_A_B(class_A *this_A, int x) { Class_B *this = convert_ptr_to_A_to_ptr_to_B(this_A); … } void m3_B_B(class A *this_A) { … } } p.m2(3); // p is a pointer to Bm2_A_B(convert_ptr_to_B_to_ptr_to_A(p), 3);

Multiple Inheritance

Allows unifying behaviors But raise 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

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

A simple implementation

A simple implementation (downcasting) convert_ptr_to_E_to_ptr_to_C(e) = e; convert_ptr_to_E_to_ptr_to_D(e) = e + sizeof(C);

A simple implementation (upcasting) convert_ptr_to_C_to_ptr_to_E(c) = c; convert_ptr_to_D_to_ptr_to_E(d) = d - sizeof(C);

Dependent Multiple Inheritance

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

Implementation Use an index table to access fields Access offsets indirectly Some compilers avoid index table and uses register allocation techniques to globally assign offsets

Interface Types Java supports limited form of multiple inheritance Interface consists of several methods but no fields A class can implement multiple interfaces public interface Comparable { public int compare(Comparable o); } Simpler to implement/understand/use A separate dispatch table per interface specification which refers to the implemented method

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 dynamic: –Fetch the class descriptor d at offset 0 from c –Fetch p the address of the method-instance f from (constant) f offset at d –Jump to the routine at address p (saving return address)

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

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

Code Generation for Modules Chapter 6.5

Modules Units of modularity Several related items grouped together in a syntactic structure Similar to objects but more restricted Supported in Java, Modula 3, ML, Ada

Issues The target language usually has one spaces –Generate unique names Generate code for initialization –Modules may use items from other modules –Init before used –Init only once –Circular dependencies Generics

Name Generation Generate unique names for modules Some assemblers support local names per file Use special characters which are invalid in the programming language to guarantee uniqueness

Module Initialization Initialize global variables Initialize before use Initialize once Circular dependencies

Avoiding Multiple Initializations If module A uses module B and C and B uses C –How to initialize C once Similar problem occurs when using C include files Two solutions –Compute a total order and init before use –Use special compile-time flag if NOT This module has been initialized: SET This module has been initialized to true // call initialization of the used modules // code for this module ’ s own initializations

Detecting Circular Dependencies Check the graph of used specifications is acyclic But what about implementation A’s specification can use B’s implementation B’s specification can use A’s implementation Detect at runtime (link time)

Detecting Circularity IF This module is being initialized: // deal with circular dependency IF NOT This module is initialized: SET This module is initialized TO true; SET This module is being initialized TO true // call initialization of the used modules SET This module is being initialized TO false; // code for this module ’ s own initializations

Generics in C syntax struct list { data ; struct list *next; } struct List *il;

Generics Increase reuse Increase security –Consistency can be checked Supported in C++ Will be supported in Java

Implementing Schemes for Generics Produce code for generics (expansion) –Similar to macros Design a runtime representation –dope vectors

Instantiation through expansion Create a copy of the AST and replace the generic unit Replace occurrences of generic types Process the resulting AST

Instantiation trough dope vectors

Summary OO features complicates compilation –Semantic analysis –Code generation –Runtime –Memory management (next class) Understanding compilation of OO can be useful for programers