Problem: Efficient dynamic type checking for Java ƒsingle class inheritance ƒmultiple interface inheritance ƒarray subtyping Solutions ƒLeverage (JIT)

Slides:



Advertisements
Similar presentations
Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
Advertisements

Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
COS 461 Fall 1997 Network Objects u first good implementation: DEC SRC Network Objects for Modula-3 u recent implementation: Java RMI (Remote Method Invocation)
Java Review Interface, Casting, Generics, Iterator.
Portability and Safety Mahdi Milani Fard Dec, 2006 Java.
Compilation 2007 Code Generation Michael I. Schwartzbach BRICS, University of Aarhus.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Elementary Data Types Prof. Alamdeep Singh. Scalar Data Types Scalar data types represent a single object, i.e. only one value can be derived. In general,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
1 Java Object Model Part 1. 2 Type Definition: set of values – a set of values and set of operations –a set of operations that can be applied to those.
Elementary Data Types Scalar Data Types Numerical Data Types Other
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
Cmp Sci 187: Midterm Review Based on Lecture Notes.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Java Data Types  Everything is an Object  Except Primitive Data Types  For efficiency  Platform independent  Portable  “slow”  Objects are often.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Exploiting Prolific Types for Memory Management and Optimizations By Yefim Shuf et al.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Java and C++, The Difference An introduction Unit - 00.
CIS 270—Application Development II Chapter 13—Exception Handling.
1 The Java Virtual Machine Yearly Programming Project.
Fast, Effective Code Generation in a Just-In-Time Java Compiler Rejin P. James & Roshan C. Subudhi CSE Department USC, Columbia.
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.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Lecture objectives  Differences between Java and C#  Understand and use abstract classes  Casting in Java: why is it important?  Master exceptions.
O VERVIEW OF THE IBM J AVA J UST - IN -T IME C OMPILER Presenters: Zhenhua Liu, Sanjeev Singh 1.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
CSC204 – Programming I Lecture 4 August 28, 2002.
Java Virtual Machine Case Study on the Design of JikesRVM.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Generics and Collections. Introduction Generics New feature of J2SE 5.0 Provide compile-time type safety Catch invalid types at compile time Generic methods.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
CS 598 Scripting Languages Design and Implementation 14. Self Compilers.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Jun 14, 2004RAM-SE'04 Workshop, Oslo, Norway 1 Negligent Class Loaders for Software Evolution Yoshiki Sato, Shigeru Chiba (Tokyo Institute of Technology.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
RealTimeSystems Lab Jong-Koo, Lim
Eliminating External Fragmentation in a Non-Moving Garbage Collector for Java Author: Fridtjof Siebert, CASES 2000 Michael Sallas Object-Oriented Languages.
JAVA MULTIPLE CHOICE QUESTION.
Object-oriented Programming in Java
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Java package classes Java package classes.
Java Programming Language
Chap 2. Identifiers, Keywords, and Types
Subtype Substitution Principle
ICOM 4029 Fall 2003 Lecture 2 (Adapted from Prof. Necula UCB CS 164)
Presentation transcript:

Problem: Efficient dynamic type checking for Java ƒsingle class inheritance ƒmultiple interface inheritance ƒarray subtyping Solutions ƒLeverage (JIT) compile-time information ƒ3 data structures to handle common cases ƒinline code sequences for common cases Dynamic Type Checking [JVM'01]

Taxonomy of Java Types Proper classes always extend one superclass Proper classes may implement many interfaces There are no interface values null is a typeless value (for reference variables) variable type primitive reference boolean byte short char int long float double proper class java.lang.Object interface array of primitive proper class java.lang.Object interface array of primitive proper class java.lang.Object interface array of primitive proper class java.lang.Object interface array of primitive proper object java.lang.Object interface array of

Type Checking in Java Most operations are statically typed Some operations entail dynamic typechecking ƒ Explicit type tests ( instanceof ) b instanceof A; ƒ Down casts ( checkcast ) (A) b; ƒ Interface method invocation ( invokeinterface ) I i = b; // I an interface type i.foo(); ƒ Exception delivery ( athrow ) try {...} catch (A a) {... } ƒ Object array stores ( aastore ) DeclaredType [] X = X[3] = b;

(Simplified) Jalapeño Object Model All objects have a two word header 1Type Information Block (TIB) pointer 2Status word (locking, hashcode, gc, etc.) All types (primitives, arrays, classes) have unique ƒVM_Type objects ƒinteger ids ƒTIBs (arrays, classes only) TIBs are reachable from JTOC (global TOC for statics)

Type Information Block Truck VMTVMT Implements trits Element's TIB (null) TIB for Truck Superclass Id display VM_Type for Truck TIB for a type is an array of Objects The Virtual Method Table for the type Three dynamic type checking data structures The VM_Type object for the type

Example Hierarchy interface Cargo class java.lang.Object class DurableGood extends java.lang.Object class Vehicle extends DurableGood class Car extends Vehicle class Truck extends Vehicle implements Cargo The depth of a class is the number of its superclasses Object has depth 0 DurableGood has depth 1 Vehicle has depth 2 Truck has depth 3

Testing for a Proper Class A is known to be a proper class checkcast and instanceof bytecodes Most significant case (along with aastore) Superclass Identifier Display (SID) [Cohen '91] A class's display contains its type id and the type ids of its ancestors The display is ordered (indexed) by their depth Dynamic type check: Compare depth SID entry of object to type id of A if A_depth >= minimum (6) then array bounds check required

Object Durable Good Superclass Identifier Display Each type has a depth and a type id SID for a type is an array of shorts Maps superclass depth to superclass type id Padded to a minimum depth with invalid ids Truck TIB for Truck VMTVMT SID for Truck 3 90 VM_Type for Truck depth type id Dynamic type check: l r1, TIBoffset(b) l r1, SIDoffset(r1) l r1, A_depth<<1(r1) cmpi r1, A_id bne NoMatch Dynamic type check: l r1, TIBoffset(b) l r1, SIDoffset(r1) l r2, lengthOffset(r1) cmpi r2, A_depth bge NoMatch l r1, A_depth<<1(r1) cmpi r1, A_id bne NoMatch Truck Vehicle

Testing for an Interface  Does an object implement interface I? invokeinterface, checkcast, instanceof Three possible answers: 0 - No, 1 - Yes, 2 - Maybe (don't know yet) Monotonic: Maybe becomes Yes or No  Implements Trits Vector (Trit == 3-values) Per class cache indexed by interface id  Shared ITV's 1Initial ITV - all Maybe (most classes never ask) 2If a class doesn't explicitly implement an interface then it could share its superclass's ITV

Implements Trits Vector ITV for a type is a array of bytes (could use two bits with rotate/mask) No the type doesn't implement the interface Yes the type implements the interface Maybe no determination has yet been made Truck VMTVMT TIB for Truck VM_Type for Truck ITV for Truck Dynamic type check: l r1, TIBoffset(b) l r1, ITVoffset(r1) array-bounds-check l r1, I_interfaceId(r1) cmpi r1, 1 bne ResolveOrNoMatch Cargo

Testing for an Array Is b a subtype of array type A? Dimension of a type (or value) Number of (possible) subscripts of arrays 0 for classes and interfaces -1 for primitives 1Short-circuit type equality test: A == B ? Definitive, if A's base type is primitive or final 2If A's dimension greater than b's, no match! 3If A's dimension equal to b's Match, if, and only if, b's base type is subtype of A's base type (Special case: if A's base type is Object then match, unless b's base type is primitive) 4If A's dimension less than b's Match, if, and only if, A's base type is Object

Catching Throwables Explicit throws try {... throw b;... } catch (A a) {... } Implicit throws: RuntimeExceptions null pointer, out-of-bounds, divide-by-zero, etc. JVM walks the stack looking for catch ( A ) to match throw ( b ) Both A and B extend Throwable Both are proper classes Proper class test b 's SID[A_depth] == A_typeId Works even if A is not (yet) loaded A_depth is 0, but A_typeId is not the type id of Object The test fails (as it should since B has been loaded)

Object Array Stores Array subtyping is problematic Vehicle [ ] X = makeVehicleArray(); Truck b = makeTruck(); X[0] = b; At compile time, Truck must be a Vehicle But, what if X is an array of Cars? At runtime, aastore throws an ArrayStoreException Short-circuit: array type = declared array type ? If X is Vehicle[], the compile-time test was sufficient! Declared type may be recoverable from bytecodes

Object Array Stores II Vehicle [ ] X = makeVehicleArray(); Truck b = makeTruck(); X[0] = b; A is runtime element type of X Not known at compile time Acquire from Element Type TIB (ETT) slot 1Type equality short-circuit Compare TIB of b and ETT from X 2Proper-class short-circuit (1-D array of proper classes) Get the VM_Type for A from X 's ETT Get A 's type id and depth from its VM_Type A_typeId == b 's SID[A_depth] ?

Element Type TIB ETT is the TIB for the elements of an array null for non arrays Number of subscripts is dimension of type 0 for proper objects, -1 for primitives Truck[] VMTVMT TIB for Truck[] 1 VM_Type for Truck[] TIB for Truck VMTVMT dimension

Basic Optimizations of Dynamic Type Checking Constant ( null ) propagation (JIT compile time) (A) null always succeeds (null instanceof A) == false Type propagation idioms (JIT compile time) Aggressive inlining uncovers opportunities Reaching definition B b = new A();... (A) b similarly for instanceof and invokeinterface Redundant checkcast elimination if (b instanceof A) {... (A) b... }

Basic Optimizations of Dynamic Type Checking Short-circuit tests (runtime) b is null A == B (type equality test) Definitive, if A is A final proper class An array* of primitive An array* of a final proper class

Experimental Evaluation Implemented three dynamic type checking schemes in Jalapeño 1Prior Jalapeño: out-of-line type equality, cache last success 2Inline TE/Cache: inlined type equality and type cache check to approximate technique described in [Ishizaki et al.'00] inline TE/Cache check for instanceof and checkcast 3New Jalapeño All three include same basic optimizations 4final classes & null handled inline 5null and type propagation 6compile time folding of instanceof & checkcast Experimental setup 7AIX/PowerPC, 1 processor 604e with 768MB 8Jalapeño adaptive system 9copying, nongenerational garbage collector

Performance Impact of Dynamic Type Checking

Space Considerations Data Structures (per Type costs) 1Superclass ID Display: expected 7 (or 4) words (when depth < 6) 2Implements Trits Vector: ƒClass does not implement any interfaces, 1 word ƒOtherwise 4 words + Max(min ITV, largest interface ID implemented trits) 3Element Type TIB: 1 word No data structures for aastore declared type short-circuit Code costs highly variable 4Which sequences are inlined? 5Where the sequences are inlined (be selective based on profiling)? 6Some sequences are both definitive and smaller than calls to runtime cType equality test in restricted cases dProper subclass using SID eA few array cases

Conclusions Superclass Id Display (SID) Resolves proper class tests (normal case) Usually, in four instructions Padded to avoid array bounds checks Implements Trits Vector (ITV) Resolves interface implementation tests 3-valued to accommodate dynamic class loading Element Type TIB (ETT) Resolves some common object-array-store tests Short-circuit for object-array-store test ( aastore ) Does runtime array type = declared array type? Declared type may be recoverable from bytecodes Resolves large fraction of object-array-store tests Exploit compile-time knowledge to customize dynamic type checking

Related Work Single inheritance dynamic type checking Superclass display's in Oberon [Cohen '91] Padded to 8 elements [Pfister, et. al. '91] Imposed a maximum inheritance depth on Oberon Multiple inheritance dynamic type checking boolean TypeCheck [*, *] array TypeCheck[B_id, A_id] iff B is a subtype of A Space inefficient Subset type tests A set of small integers is associated with each type B is a subtype of A iff B_set  A_set Set coloring algorithm [Krall, et. al. '97] Produces space-efficient TypeCheck array Dynamic class loading may require recomputation Jalapeño implementation by Litvinov, no longer supported Caching schemes Cache most recent success [Ishizaki et. al.'00]