Object-oriented Languages Compiler Baojian Hua

Slides:



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

Dynamic memory allocation
C++ Language Fundamentals. 2 Contents 1. Introduction to C++ 2. Basic syntax rules 3. Declaring and using variables.
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
C++ Programming Languages
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Dynamic memory allocation. The process of allocating memory at run time is known as dynamic memory allocation. C have four library functions for allocating.
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
OOP in C. Cfront Original C++ compiler was just a pre- processor –generated C code which could be compiled with an existing C Compiler.
K. Stirewalt CSE 335: Software Design Foundations: Language Mechanisms and Primitive OO Concepts Lecture 2: Polymorphism Topics: – Polymorphism and virtual.
C and Data Structures Baojian Hua
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Functional List C and Data Structures Baojian Hua
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Closure and Environment Compiler Baojian Hua
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Memory Layout C and Data Structures Baojian Hua
Polymorphism C and Data Structures Baojian Hua
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
C++ fundamentals.
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 C ++ -overview Department of Computer Engineering 1.
From C++ to C#. Web programming The course is on web programming using ASP.Net and C# The course is on web programming using ASP.Net and C# ASP.Net is.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Programming Languages and Paradigms Object-Oriented Programming.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
C++ Programming. Table of Contents History What is C++? Development of C++ Standardized C++ What are the features of C++? What is Object Orientation?
Static and Dynamic Behavior CMPS Power of OOP Derives from the ability of objects to change their behavior dynamically at run time. Static – refers.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
C++
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Dynamic Memory Allocation Conventional array and other data declarations An incorrect attempt to size memory dynamically Requirement for dynamic allocation.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
SB Symbol table handling in ScriptBasic The Module sym.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
University of Washington Today Finished up virtual memory On to memory allocation Lab 3 grades up HW 4 up later today. Lab 5 out (this afternoon): time.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
CS212: Object Oriented Analysis and Design Lecture 16: Runtime Polymorphism.
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
Compilation /16a Lecture 10 Compiling Object-Oriented Programs Noam Rinetzky 1.
Dynamic Binding Implementation Object-Oriented Programming Spring
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 20 – C++ Subclasses and Inheritance.
Object Model Baojian Hua What ’ s an Object Model? An object model dictates how to represent an object in memory A good object.
The Slicing Problem CS240 Computer Science II. Some relationship between base and derived classes: data members Derived class inherits data members of.
CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]
JAVA TRAINING IN NOIDA. JAVA Java is a general-purpose computer programming language that is concurrent, class-based, object-oriented and specifically.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
Fundamental of Java Programming (630002) Unit – 1 Introduction to Java.
CS212: Object Oriented Analysis and Design
Inheritance, Polymorphism and the Object Memory Model
جامعة البحر الاحمر كلية العلوم التطبيقية قسم الفيزياء التطبيقية الفصل الداسي الثاني IIالمقرر: حاسوب د. خالد عثمان العالم.
Introduction to the C Language
جامعة البحر الاحمر كلية العلوم التطبيقية قسمي الحاسوب وتقنية المعلومات الفصل الداسي الثاني المقرر: اساليب برمجة 1 محاضرة رقم 1 د. خالد عثمان العالم.
C Programming Lecture-8 Pointers and Memory Management
Presentation transcript:

Object-oriented Languages Compiler Baojian Hua

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 …

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

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

Case study: Visual studio C++ (Microsoft)

Multiple Inheritance

Template