Object Model Baojian Hua What ’ s an Object Model? An object model dictates how to represent an object in memory A good object.

Slides:



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

A little bit on Class-Based OO Languages CS153: Compilers Greg Morrisett.
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.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
Subprograms The basic abstraction mechanism. Functions correspond to the mathematical notion of computation input output procedures affect the environment,
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Polymorphism, Virtual Methods and Abstract Classes.
Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #17, March 12, 2007 Procedure Abstraction, Name Spaces, Scoping Rules, Activation Records,
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C and Data Structures Baojian Hua
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
Encapsulation by Subprograms and Type Definitions
CS 536 Spring Code Generation II Lecture 21.
CS Winter 2011 Introduction to the C programming language.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Pointers and Arrays C and Data Structures Baojian Hua
Object-oriented Languages Compiler Baojian Hua
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
1 CSE 303 Lecture 24 Inheritance in C++, continued slides created by Marty Stepp
C++ fundamentals.
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#
OOP Languages: Java vs C++
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Java and C++, The Difference An introduction Unit - 00.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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,
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Compiler Construction
The Procedure Abstraction, Part VI: Inheritance in OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 11: Compiler II: Code Generation slide 1www.idc.ac.il/tecs.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 11: Compiler II: Code Generation slide 1www.nand2tetris.org Building.
Learners Support Publications Classes and Objects.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Peyman Dodangeh Sharif University of Technology Fall 2013.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
CS 261 – Data Structures Introduction to C Programming.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Copyright © 2005 Elsevier Object-Oriented Programming Control or PROCESS abstraction is a very old idea (subroutines!), though few languages provide it.
Compilation /16a Lecture 10 Compiling Object-Oriented Programs Noam Rinetzky 1.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Classes, Interfaces and Packages
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
OOP Basics Classes & Methods (c) IDMS/SQL News
FASTFAST All rights reserved © MEP Make programming fun again.
Section 11: Comparing Java and C
Computer Organization and Design Pointers, Arrays and Strings in C
Advanced Programming Behnam Hatami Fall 2017.
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
Classes and Objects.
Java Programming Language
Data Structures & Algorithms
Presentation transcript:

Object Model Baojian Hua

What ’ s an Object Model? An object model dictates how to represent an object in memory A good object model can: Make the representation simpler Maximize the efficiency of frequent language operations Minimize storage overhead

Object Model in Java Two sorts of value formats in Java: Primitive: int, float, etc. Reference: pointers to objects Arrays with elements Scalar with fields

Object Header An object header may contain: Virtual methods pointers Hash code Type information pointers Lock Garbage collection information Array length Misc fields: such as profiling information We sketch a few of them next

Next Sketch the Java object model in a step- by-step way. Road-Path: Static and dynamic data Static and dynamic method Inheritance Give some insights into the general principals I write Java syntax in Meta Language

Java#1: Static Data Fields First look at a Java subset with only static data fields. Abstract syntax: // example class C { static int i; static int j; } datatype class = T of {name : string, staticVar : var list}

Two Steps Name mangling Try to avoid name space conflict Consider: class C1 {static int i;} class C2 {static int i;} Lifting Lift the variables out of the class body Just like the C global variables

To C We write a translation function to show how to compile this Java#1 to C fun trans (Class {name, vars}) = case varList of [] => [] | x :: xs => (name^x) :: trans xs (* or the following, if you’re really lazy :-P *) fun trans (Class{name, vars} = List.map addPrefix vars // an example class C { static int i j; } // would be //translated into: int C_i; int C_j;

To Pentium To Pentium is also not hard, you would want to only change the definition of “ prefix ” // example code from above again trans (Class {static int i; static int j}) =.section.data.C_i:.int 0.C_j:.int 0

Member Data Access All the static data member access should be systematically turned into the newly created names

Example class C {static int i;} class Main { public static void main (String [] args) { C.i = 99; } public static void foo (){ C.i = 88; } C_i = 99;C_i = 88;

Problem These static fields could also be accessed through objects: C c = new C (); c.i = 99; Should this assignment be turned into? C_i = 99;

Java#2: Static Methods (*Abstract syntax: *) datatype class = T of {name : string, staticData : var list, staticMethod : func list } datatype func = T of {name : string, args : …, locals : …, stm : …} // example class C { static int i, j; static int f (int a, int b) {…} static int g (int x) {…} }

Java#2: Static Methods The translation of static methods are very much like that of the static member data fields. Let ’ s try the our previous steps: Name mangling Lifting

Translation to C fun trans (Class {name, funs, …}) = transFunc (name, funs) and transFunc (name, funs) = List.map addPrefix funcs // example code class C {static int foo (int i) {…}} // be translated into: int C_foo (int i) {…} To Pentium code is simple, leave to you But later we may find that this naïve translation scheme does NOT work for dynamic methods…

Static Method Reference class C {pubic static int foo (int i){…}} class Main { public static void main (String [] args) { C.foo (99); } C_foo (99) ;

Problem These static methods could also be accessed by objects: C a = new C (); a.foo (99); Should this assignment be turned into the following? C_foo (99);

Java#3: Instance Variables Abstract Syntax: datatype class = T of { name : string, staticData : var list, dynData : var list, staticMethods : func list } Created only after the real object is allocated, and destroyed after the object is reclaimed Every object holds its own version of data

Translation to C fun trans (Class {name, dynData, …}) = struct name {dynData} // example code class C {int i; int j;} // would be translated into struct C {int i; int j;} All dynamic data fields in a class grouped as a same-name C structure To Pentium is similar

Member Data Access fun trans (new C ()) = malloc (struct C); Every object is malloc”ed” in memory… Dereference an object is NOT different from accessing an ordinary pointer, all your C (x86) hacking skills apply…

Example class C {int i;} class Main { public static void main (String [] args) { C a; a = new C (); a.i = 99; } a = malloc (struct C); a -> i = 99; struct C {int i;}

Java#4: Instance Methods Abstract syntax: datatype class = T of {name : string, staticData : var list, dynData : var list, staticMethods : func list, dynMethods : func list } Could be invoked only after concrete object instance has been born …

Java#4: Instance Methods As a first try, we would like to experiment our “ old ” magic: mangling followed by lifting Then, what the method invocations would look like? C c = new C (); c.foo (args); Should it be turned into C_foo (args) ?

Access the Instance Data class C{ int i; int set (int i){ this.i = i; return 0; } int get (){ return i; } struct {int i;} int C_set (int i){ this.i = i; return 0; } int C_get (){ return i; } Where does the “this” or “i” come from? “i”?

“ This ” To invoke a dynamic method, we would have to also tell it which object instance we ’ re using, and pass this object to this method explicitly (recall that an object is a pointer to a malloc-ed structure … ) In Java, passing the object itself to a dynamic method is the job of “ this ” pointer

Access the Instance Data --- Revisit class C{ int i; int set (C this, int i){ this.i = i; return 0; } int get (C this){ return this.i; } struct C {int i;} int C_set (C * this, int i){ this -> i = i; return 0; } int C_get (C * this){ return this -> i; } “this” turns into a pointer…

Java#5: Class Inheritance Java allows you to write tree-like class hierarchy An object may have both: a static “ type ” ---the declared type, and a dynamic “ type ” ---the creation type Class inheritance make things messy

Inheritance Syntax Abstract syntax: datatype class = T of { name : string, staticData : var list, dynData : var list, staticMethods : func list, dynMethods : func list, extends : class option }

Example // class hierarchy class C1 { int i; void f () {…} } class C2 extends C1 { int j; void f () {…} void g () {…} } // and the “Main” class class Main{ static void main (…) { …; test (a); } void test (C1 a){ a.f (); }

Example Continued Notice that object a has a static “type” C1 in declaration: void test (C1 a) {…}; But the real argument arg may have a sub-class name as its real type---its dynamic type, consider: C2 arg = new C2 (); test(arg); It’s well-typed, so what the a.f() would be at run time? C1_f() or C2_f() ?

Dynamic Method Dispatch Generally, we can NOT always statically predicate what the dynamic type of the incoming argument object would be So we would have to record such kind of information in object itself, and do method dispatch at run time invocation points

Virtual Method Table (VMT) We can use a virtual method table An object is composed of virtual method table pointer and data fields (just the C- style structure we ’ ve seen) VMT pointer at zero offset, followed by others data fields VMT itself may be used by all same-type objects to reduce the space overhead

VMT in Graph C1 i C1_f C2 i C2_f C2_g j

Method Invocation fun trans (a.f (…)) = x = lookup (*a, f); x (a, …); First, we take the VMT pointer through the object pointer Lookup the callee method Note that this is static known And call this method pointer, with additional “this” pointer augmented as before

Java#6: Interface Consider an interface: I = {foo (); bar ();} Any object of a class C that implements methods named foo and bar can be treated as if it has interface type I Can we use C's vtable? No In general, C may have defined methods before, between, or after foo and bar or may have defined them in a different order So to support interfaces, we need a level of indirection …

Interface address of method 1 address of method 2 … address of method n Shared vtable for Interface vtable pointer instance variable 1 instance variable 2 … instance variable m Actual Object vtable pointer actual object Wrapper Object

Backup Slides A Toy Mini-Java Compiler ---Putting All Together

The Phases The toy Mini-Java compiler is organized into several separate passes: Front-end issues: lexing, parsing, type-checking etc. Name-mangling: make the class property and “this” pointer explicit De-SubClass: eliminate “sub-class” De-Class: eliminate “class”, and output C code These passes are presented by a running example

A Sample Program class A{ int i; int j; int k; int f (int x, int y){ …} int g (int a){ …} } class B extends A{ int i; int e; int k; int f (int x, int y){ …} }

After Front-end Processing class A{ int i; int j; int k; int f (int x, int y){ …} int g (int a){ …} } class B extends A{ int i; int e; int k; int f (int x, int y){ …} }

After Name Mangling class A{ int A_i; int A_j; int A_k; int A_f (int x, int y){ …} int A_g (int a){ …} } class B extends A{ int B_i; int B_e; int B_k; int B_f (int x, int y){ …} }

After Inserting “ this ” class A{ int A_i; int A_j; int A_k; int A_f (A this, int x, int y){ …} int A_g (A this, int a){ …} } class B extends A{ int B_i; int B_e; int B_k; int B_f (B this, int x, int y){ …} }

After De-SubClass class A{ int A_i; int A_j; int A_k; int A_f (A this, int x, int y){ …} int A_g (A this, int a){ …} } class B{ int B_i; int A_j; int B_k; int B_e; int B_f (B this, int x, int y){ …} int A_g (A this, int a){ …} } // Note the order

What Happened? Instance data fields unification Relative order is important Insert an extra “ this ” argument As the first pointer? Methods name mangling You may or may not want to copy the actual method body, if you don ’ t want to, you just have to construct the vtable here … After these, all classes are closed

After De-Class (class A) struct Data_A{ Vtable_A * vptr; int i; int j; int k; } struct Vtable_A{ int (*f) (); //=A_f int (*g) (); //=A_g } int A_f (struct Data_A * this, int x, int y){ … } int A_g (struct Data_A * this, int a){ … }

After De-Class (class B) struct Data_B{ Vtable_B * vptr; int i; int j; int k; int e; } struct Vtable_B{ int (*f) (); //=B_f int (*g) (); //=A_g } int B_f (struct Data_B * this, int x, int y){ … } int A_g (struct Data_B * this, int a){ … }

What Happened? Object layout selection The “ 0 ” offset is the vptr Vtable layout selection The vtable fields should be initialized by corresponding method pointers Methods lifting Methods go to top-level