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

Similar documents
Chapter 1 Fundamentals of Java Programming

Habanero Extreme Scale Software Research Project

Characteristics of Java (Optional) Y. Daniel Liang Supplement for Introduction to Java Programming

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

Fundamentals of Java Programming

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

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

Java (12 Weeks) Introduction to Java Programming Language

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

The C Programming Language course syllabus associate level

Glossary of Object Oriented Terms

1. What are Data Structures? Introduction to Data Structures. 2. What will we Study? CITS2200 Data Structures and Algorithms

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

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

Jonathan Worthington Scarborough Linux User Group

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

Objective C and iphone App

1 The Java Virtual Machine

Java Interview Questions and Answers

How To Design Software

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

CSE 307: Principles of Programming Languages

Implementation Aspects of OO-Languages

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

Checking Access to Protected Members in the Java Virtual Machine

picojava TM : A Hardware Implementation of the Java Virtual Machine

ALLIED PAPER : DISCRETE MATHEMATICS (for B.Sc. Computer Technology & B.Sc. Multimedia and Web Technology)

PART IV Performance oriented design, Performance testing, Performance tuning & Performance solutions. Outline. Performance oriented design

CSCI E 98: Managed Environments for the Execution of Programs

1. Overview of the Java Language

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

Java in Education. Choosing appropriate tool for creating multimedia is the first step in multimedia design

Chapter 6: Programming Languages

Java Programming. Binnur Kurt Istanbul Technical University Computer Engineering Department. Java Programming. Version 0.0.

Virtual Machine Learning: Thinking Like a Computer Architect

Design by Contract beyond class modelling

Cloud Computing. Up until now

Write Barrier Removal by Static Analysis

AP Computer Science A - Syllabus Overview of AP Computer Science A Computer Facilities

Agenda. What is and Why Polymorphism? Examples of Polymorphism in Java programs 3 forms of Polymorphism

Can You Trust Your JVM Diagnostic Tools?

El Dorado Union High School District Educational Services

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

CS 209 Programming in Java #1

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

MA-WA1920: Enterprise iphone and ipad Programming

Advanced Data Structures

History OOP languages Year Language 1967 Simula Smalltalk

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

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

AP Computer Science Java Subset

6.088 Intro to C/C++ Day 4: Object-oriented programming in C++ Eunsuk Kang and Jean Yang

Chapter 1. Dr. Chris Irwin Davis Phone: (972) Office: ECSS CS-4337 Organization of Programming Languages

OKLAHOMA SUBJECT AREA TESTS (OSAT )

JavaFX Session Agenda

Crash Course in Java

Object Oriented Database Management System for Decision Support System.

Tutorial on Writing Modular Programs in Scala

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

Object-Oriented Programming

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Object Oriented Databases. OOAD Fall 2012 Arjun Gopalakrishna Bhavya Udayashankar

What is a programming language?

Syllabus for CS 134 Java Programming

Reconfigurable Architecture Requirements for Co-Designed Virtual Machines

Semantic Analysis: Types and Type Checking

Built-in Concurrency Primitives in Java Programming Language. by Yourii Martiak and Mahir Atmis

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Chapter 7D The Java Virtual Machine

Semester Review. CSC 301, Fall 2015

Facebook Twitter YouTube Google Plus Website

TECHNOLOGY Computer Programming II Grade: 9-12 Standard 2: Technology and Society Interaction

Structural Typing on the Java Virtual. Machine with invokedynamic

Chapter 24 - Quality Management. Lecture 1. Chapter 24 Quality management

Programming Language Pragmatics

Replication on Virtual Machines

CEC225 COURSE COMPACT

CSE 452: Programming Languages. Acknowledgements. Contents. Java and its Evolution

Course MS10975A Introduction to Programming. Length: 5 Days

Hardware/Software Co-Design of a Java Virtual Machine

HVM TP : A Time Predictable and Portable Java Virtual Machine for Hard Real-Time Embedded Systems JTRES 2014

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

Lecture 1 Introduction to Android

C Compiler Targeting the Java Virtual Machine

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

Java Application Developer Certificate Program Competencies

Programming and Software Development CTAG Alignments

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

Chapter 5 Names, Bindings, Type Checking, and Scopes

Parrot in a Nutshell. Dan Sugalski dan@sidhe.org. Parrot in a nutshell 1

Introduction to Object-Oriented Programming

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Java CPD (I) Frans Coenen Department of Computer Science

Object Oriented Design

Transcription:

Compiling Object Oriented Languages What is an Object-Oriented Programming Language? Last time Dynamic compilation Today Introduction to compiling object oriented languages What are the issues? Objects Encapsulate code and data Inheritance Supports code reuse and software evolution Subtype polymorphism Can use a subclass wherever a parent class is expected Student Person Teacher PrintName(Person p); Person p = new Person; Student s = new Student; PrintName(p); PrintName(s); CS553 Lecture Compiling Object Oriented Languages 3 Dynamic binding (message sends) Binding of method name to code is done dynamically based on the dynamic type of the (receiver) object p.reprimand(); CS553 Lecture Compiling Object Oriented Languages 4 Implementation: Inheritance of Instance Variables Goal Lay out object for type-independent instance variable access Solution (single inheritance) Prefixing: super-class fields are at beginning of object Person Name Student Name ID Teacher Name Salary CS553 Lecture Compiling Object Oriented Languages 5 Implementation: Dynamic Binding Problem The appropriate method depends on the dynamic type of the object e.g., p.reprimand() Solution Create descriptor for each class (not object) encoding available methods Encode pointer to class descriptor in each object Lay out methods in class descriptor just like instance variables Person getname Student getname reprimand workhard Usage summary Load class descriptor pointer from object Load method address from descriptor Jump to method Teacher getname reprimand party CS553 Lecture Compiling Object Oriented Languages 6 1

Why are Object-Oriented Languages Slow? Dynamism Code Data Style Granularity (lots of small objects) Exploit dynamism Other reasons High-level (modern) features such as safety Garbage collection CS553 Lecture Compiling Object Oriented Languages 7 Dynamism: Code Dynamic binding What code gets executed at a particular static message send? It depends, and it may change class rectangle extends shape { int length() {... int width() {...?? int area() { return (length() * width()); class square extends rectangle { int size; int length() { return(size); int width() { return(size); rect.area(); sq.area(); CS553 Lecture Compiling Object Oriented Languages 8 Cost of Dynamic Binding Direct cost Overhead of performing dynamic method invocation Indirect cost Inhibits static analysis of the code Dynamism: Data Object instance types are not statically apparent Need to be able to manipulate uniformly Boxing: wrap up all data and reference it with a pointer class rectangle:shape { Want to inline and assign to registers, etc. int length() {... int width() {... int area() { return (length() * width()); Driessen and Holzle (OOPSLA 96), on C++ programs median of 5.2% total execution time spent on dynamic dispatch Integer n = new Integer(33); n pointer type descriptor data (int) CS553 Lecture Compiling Object Oriented Languages 9 CS553 Lecture Compiling Object Oriented Languages 10 2

Cost of Dynamism: Data Direct cost Overhead of actually extracting data e.g., 2 loads versus 0 (if data already in a register) Indirect cost More difficult to statically reason about data Style Sometimes programmers write C-style code in OO languages Easy: just optimize it away Sometimes programmers actually exploit dynamism Hard: it can t just be optimized away Programmers create many small objects Thwarts local analysis Exacerbates dynamism problem Huge problem for pure OO languages CS553 Lecture Compiling Object Oriented Languages 11 Programmers create many small methods Methods to encapsulate data e.g. Methods to get and set member fields CS553 Lecture Compiling Object Oriented Languages 12 A Concrete : Java High-level and modern Object-oriented Mobile/portable (standard bytecode IL) Multithreaded (great for structuring distributed and UI programs) Garbage collected Dynamic class loading Reasonable exception system Rich standard libraries Approaches to Implementing Java Interpretation Extremely portable Simple stack machine Performance suffers Interpretation overhead Stack machine (no registers) Direct compilation Compile the source or bytecodes to native code Sacrifices portability Can have very good performance CS553 Lecture Compiling Object Oriented Languages 13 CS553 Lecture Compiling Object Oriented Languages 14 3

Why is Java Slow? Bytecode interpretation? Not a good answer Why is Java Slow? Impediments to performance Flexible array semantics Run-time checks (null pointers, array bounds, types) Precise exception semantics thwart optimization Dynamic class loading thwarts optimization Even the class hierarchy is dynamic Multithreading introduces synchronization overhead Lots of memory references (poor cache performance)... and all the usual OO challenges CS553 Lecture Compiling Object Oriented Languages 15 CS553 Lecture Compiling Object Oriented Languages 16 Scientific Programming and Java Consider matrix multiplication Costs 6 null pointer checks (with just 2 floating point operations!) 6 index checks Can we optimize this code? Precise exception model Exception semantics inhibit removal or reordering No multidimensional arrays Rows may alias CS553 Lecture Compiling Object Oriented Languages 17 More on Matrix Multiplication Why can t we just do this...? if (m <= C.size(0) && p <= C.size(1) && m <= A.size(0) && n <= A.size(1) && n <= B.size(0) && p <= B.size(1)) { else { raise exception No out-of-bounds checks, right? CS553 Lecture Compiling Object Oriented Languages 18 4

Exceptions in Java Exceptions in Java are precise The effects of all statements and expressions before a thrown exception must appear to have taken place, and The effects of all statements or expressions after a thrown exception must appear not to have taken place Implications Must be very careful or clever when Eliminating checks or Reordering statements CS553 Lecture Compiling Object Oriented Languages 19 Safe Regions [Moreira et al. TOPLAS 2000] Idea Create two versions of a block of code One is guaranteed not to except and is optimized accordingly The other is used when the code might except if (m <= C.size(0) && p <= C.size(1) && m <= A.size(0) && n <= A.size(1) && n <= B.size(0) && p <= B.size(1)) { // safe region else { // unsafe region CS553 Lecture Compiling Object Oriented Languages 20 Java Arrays and Loop Transformations Java Arrays Java arrays No multidimensional arrays Instead use arrays of arrays (can be ragged) Requires one memory reference for each array dimension Rows may alias with one another Arrays are common in scientific applications Their use requires optimization for good performance Large body of work on loop transformations makes assumptions Arrays stored in contiguous memory No aliasing among array elements (Arrays are not ragged) CS553 Lecture Compiling Object Oriented Languages 21 IBM Systems Journal 39(1) 2000 IBM CS553 Lecture Compiling Object Oriented Languages 22 5

Summary Implementing OOP requires handling... member variables and inheritance dynamic binding due to polymorphism Some OOP features that lead to inefficient code dynamic code and data programming style (ie. use of dynamism and small function bodies) safety (ie. array bounds checks and precise exceptions) garbage collection Many sources of inefficiency in Java The cost of a cleaner object-oriented language Next Time Today! Seminar on giving talks from 4-5 in USC 110 Assignments Project 3 due today Project 4 is posted Lecture Garbage collection Progress in improving Java efficiency Greatest performance boost comes from eliminating interpretation overhead Scientific application performance (ie. imitating multi-dim arrays) CS553 Lecture Compiling Object Oriented Languages 23 CS553 Lecture Compiling Object Oriented Languages 24 6