Implementation Aspects of OO-Languages

Similar documents
Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Stack Allocation. Run-Time Data Structures. Static Structures

1 The Java Virtual Machine

Habanero Extreme Scale Software Research Project

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Schema Classes. Polyhedra Ltd

Part I. Multiple Choice Questions (2 points each):

picojava TM : A Hardware Implementation of the Java Virtual Machine

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Comp 411 Principles of Programming Languages Lecture 34 Semantics of OO Languages. Corky Cartwright Swarat Chaudhuri November 30, 20111

University of Twente. A simulation of the Java Virtual Machine using graph grammars

Java Interfaces. Recall: A List Interface. Another Java Interface Example. Interface Notes. Why an interface construct? Interfaces & Java Types

Java Interview Questions and Answers

Infrastructure that supports (distributed) componentbased application development

Number Representation

Motorola 8- and 16-bit Embedded Application Binary Interface (M8/16EABI)

Crash Course in Java

Java (12 Weeks) Introduction to Java Programming Language

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Chapter 1 Fundamentals of Java Programming

Part 7: Object Oriented Databases

Caml Virtual Machine File & data formats Document version: 1.4

MCI-Java: A Modified Java Virtual Machine Approach to Multiple Code Inheritance

CS 111 Classes I 1. Software Organization View to this point:

AP Computer Science Java Subset

An Incomplete C++ Primer. University of Wyoming MA 5310

MPLAB TM C30 Managed PSV Pointers. Beta support included with MPLAB C30 V3.00

Java Universal Binding: Storing Java Objects in Relational and Object-Oriented Databases

A Reverse Inheritance Relationship for Improving Reusability and Evolution: The Point of View of Feature Factorization

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

CHAPTER THREE, Network Services Management Framework

CS506 Web Design and Development Solved Online Quiz No. 01

Sources: On the Web: Slides will be available on:

iphone SDK Enrolled students will be invited to developer program Login to Program Portal Request a Certificate Download and install the SDK

The programming language C. sws1 1

Checking Access to Protected Members in the Java Virtual Machine

java Features Version April 19, 2013 by Thorsten Kracht

Design by Contract beyond class modelling

1 bool operator==(complex a, Complex b) { 2 return a.real()==b.real() 3 && a.imag()==b.imag(); 4 } 1 bool Complex::operator==(Complex b) {

Java Virtual Machine Locks

1. Overview of the Java Language

Chapter 5 Names, Bindings, Type Checking, and Scopes

C++FA 5.1 PRACTICE MID-TERM EXAM

Chapter 13 Storage classes

CS-XXX: Graduate Programming Languages. Lecture 25 Multiple Inheritance and Interfaces. Dan Grossman 2012

JavaCard. Java Card - old vs new

Principles of Database Management Systems. Overview. Principles of Data Layout. Topic for today. "Executive Summary": here.

Overview. CISC Developments. RISC Designs. CISC Designs. VAX: Addressing Modes. Digital VAX

Programming Languages Featherweight Java David Walker

Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z)

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

A Java-based system support for distributed applications on the Internet

Visio Tutorial 1BB50 Data and Object Modeling (DOM) How to make a UML Class Diagram 2004/2005

Inheritance in Programming Languages

Inside the Java Virtual Machine

What is COM/DCOM. Distributed Object Systems 4 COM/DCOM. COM vs Corba 1. COM vs. Corba 2. Multiple inheritance vs multiple interfaces

Binary search tree with SIMD bandwidth optimization using SSE

Instruction Set Architecture (ISA) Design. Classification Categories

Interpreters and virtual machines. Interpreters. Interpreters. Why interpreters? Tree-based interpreters. Text-based interpreters

Organization of Records in Blocks

Programming languages C

Software Engineering Techniques

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

INPUT AND OUTPUT STREAMS

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Semantic Analysis: Types and Type Checking

Compile-time type versus run-time type. Consider the parameter to this function:

Object Oriented Design

Interfaces Design by Contract Syntactic Substitutability Inheritance Considered Harmful Fragile Base Class Problems Mixins and View-Based Composition

The Separation of Interface and Implementation in C++

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

The Java Virtual Machine and Mobile Devices. John Buford, Ph.D. Oct 2003 Presented to Gordon College CS 311

Chapter 13 - Inheritance

CS5460: Operating Systems

Lecture 7 Notes: Object-Oriented Programming (OOP) and Inheritance

Object Instance Profiling

Semester Review. CSC 301, Fall 2015

Unit title: Computer Games: Programming (SCQF level 6)

The Java Virtual Machine (JVM) Pat Morin COMP 3002

11 November

Formal Specification and Verification of Avionics Software

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Java Types and Enums. Nathaniel Osgood MIT April 25, 2012

Introduction to Data Structures

An Overview of Java. overview-1

CIS 190: C/C++ Programming. Polymorphism

Graphical Systems Modeling with UML / SysML Class diagrams

How To Write Portable Programs In C

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

Efficient Low-Level Software Development for the i.mx Platform

Dsc+Mock: A Test Case + Mock Class Generator in Support of Coding Against Interfaces

The V8 JavaScript Engine

UML Class Diagrams (1.8.7) 9/2/2009

core. Volume I - Fundamentals Seventh Edition Sun Microsystems Press A Prentice Hall Title ULB Darmstadt

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

How To Trace

Transcription:

1 Implementation Aspects of OO-Languages Allocation of space for data members: The space for data members is laid out the same way it is done for structures in C or other languages. Specifically: The data members are allocated next to each other. Some padding may be required in between fields, if the underlying machine architecture requires primitive types to be aligned at certain addresses. At runtime, there is no need to look up the name of a field and identify the corresponding offset into a structure; instead, we can statically translate field names into relative addresses, with respect to the beginning of the object. Data members for a derived class immediately follow the data members of the base class Multiple inheritance requires more complicated handling, we will not discuss it here

2 Implementation Aspects of OO-Languages class B { int i; double d; char c; float f; } 0 int i // Integer requires 4 bytes 4 // pad, XXXXXXXXXXX 8 double d // Double requires 8 bytes char c XXXXX 16 // char needs 1 byte, 3 are padded float f 20 // float to be aligned on 4-byte // require 4-bytes of space

Implementation Aspects of OO-Languages class C { 0 int k, l; B b; } 4 8 12 16 24 28 int k int l int i XXXXXXXXXXX double d char c XXXXX float f 3

4 Implementation Aspects of OO-Languages class D: public C { 0 double x; } 4 8 12 16 int k int l int i XXXXXXXXXXX double d 24 char c XXXXX float f 28 32 double x

5 Implementation of Virtual Functions Approach 1: Lookup type info at runtime, and then call the function defined by that type. Problem: very expensive, require type info to be maintained at runtime.

6 Implementation of Virtual Functions(Contd.) Approach 2: Treat function members like data members: Allocate storage for them within the object. Put a pointer to the function in this location, and translate calls to the function to make an indirection through this field. Benefit: No need to maintain type info at runtime. Implementation of virtual methods is fast. Problem: Potentially lot of space is wasted for each object. Even though all objects of the same class have identical values for the table.

7 Implementation of Virtual Functions(Contd.) Approach 3: Introduce additional indirection into approach 2. Store a pointer to a table in the object, and this table holds the actual pointers to virtual functions. Now we use only one word of storage in each object.

Implementation of Virtual Functions(Contd.) class B { int i ; char c ; virtual void g(); virtual void h(); } B b1, b2; i c VMT ptr i c VMT ptr Ptr to B s g Ptr to B s h 8

Impact of subtype principle on Implementation The subtype principle requires that any piece of code that operates on an object of type B can work "as is" when given an object belonging to a subclass of B. This implies that runtime representation used for objects of a subtype A must be compatible with those for objects of the base type B. Note that the way the fields of an object are accessed at runtime is using an offset from the start address for the object. For instance, b1.i will be accessed using an expression of the form *(&b1+0), where 0 is the offset corresponding to the field i. Similarly, the field b1.c will be accessed using the expression *(&b1+1) 9

10 Impact of subtype principle on Implementation (Contd.) an invocation of the virtual member function b1.h() will be implemented at runtime using an instruction of the form: call *(*(&b1+2)+1) &b1+2 gives the location where the VMT ptr is located *(&b1+2) gives the value of the VMT ptr, which corresponds to the location of the VMT table *(&b1+2) + 1 yields the location within the VMT table where the pointer to virtual function h is stored.

11 Impact of subtype principle on Implementation (Contd.) The subtype principle imposes the following constraint: Any field of an object of type B must be stored at the same offset from the base of any object that belongs to a subtype of B. The VMT ptr must be present at the same offset from the base of any object of type B or one of its subclasses. The location of virtual function pointers within the VMT should remain the same for all virtual functions of B across all subclasses of B.

Impact of subtype principle on Implementation (Contd.) We must use the following layout for an object of type A defined as follows: class A: public B { A a; float f; void h(); // reuses implementation of G from B; virtual void k();} a s layout i c VMT ptr Float f Virtual Method Table (VMT)for class A Ptr to B s g Ptr to A s h Ptr to A s k 12

Impact of subtype principle on Implementation (Contd.) In order to satisfy the constraint that VMT ptr appear at the same position in objects of type A and B, it is necessary for the data field f in A to appear after the VMT field. A couple of other points: a) non-virtual functions are statically dispatched, so they do not appear in the VMT table b) when a virtual function f is NOT redefined in a subclass, the VMT table for that class is initialized with an entry to the function f defined its superclass. 13