Download this.readFloat()

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Multiple Code Inheritance in
Java
Maria Cutumisu
Supervisors: Duane Szafron, Paul Lu
Outline
•
•
•
•
•
•
Overview
Motivation
Implementation
Validation
Syntax Support for Compilation
Conclusions
2
Type Separation Mechanisms in
Java
Type
Interface
Class
We want separation of types.
Interface-Type
Code-Type
Data-Type
C++
MI (class)
MI (class)
MI (class)
Java
MI (interface)
SI (class)
SI (class)
Our Java
MI (interface)
MI (code-type)
*We do not support multiple data inheritance.
SI* (class)
3
Code Promotion Measurements Methods
DataInput
……
readFloat()
DataInputStream
DataOutput
……
readFloat()
……
writeFloat()
RandomAccessFile
……
writeFloat()
DataOutputStream
Class
Identical
Abstract
Abstract
and
Super
DataInputStream
4/19
8/19
0/19
12/19
63%
DataOutputStream
2/17
0/17
6*/17
2+6*/17
12%
RandomAccessFile
6/45
8/45
6/45
20/45
44%
Total
Method
Decrease
4
Basic Implementation - I
DataOutput
DataInput
readFloat()
readInt()
RandomAccessFile
VMT
MT
DataInput
readInt
RandomAccessFile
…
12 readFloat()
13 readInt()
Interface
MT
RandomAccessFile
readInt
Class
Code
Class Loader Changed.
Code is now
in the interface!
Code
Code was
in the class!
5
Dispatch Scenarios - Ambiguities
We detect ambiguities
at load-time.
InterfaceA x;
x = new ClassB();
x.alpha();
Scenario 3
Scenario 4
InterfaceA alpha()
InterfaceA alpha()
InterfaceB alpha()
InterfaceC alpha()
ClassB
InterfaceB alpha()
ClassA
ClassB
6
Basic Implementation - II
writeFloat()
hypothetical () in DataInput
DataOutput output;
DataInput input;
output = new RAF();
input = new RAF();
output.writeFloat();
this.readFloat();
input.readFloat();
DataOutput
DataInput
readFloat()
readInt()
RandomAccessFile
invokeinterface
invokevirtual
invokevirtual
invokeinterface
invokeinterface
7
Super Call Implementation
New syntax proposed for
super calls to interfaces
Bytecode
proposed
super(InterfaceA).alpha();
Bytecode previously
generated for super calls
invokespecial
#superclass/alpha()
invokemulti-super
#InterfaceA/alpha()
Execution-time change.
Bytecode
used
compiler script
invokeinterface
#InterfaceA/alpha()
8
Summary of Changes to the JVM
• Small and localized changes:
 class loader algorithm (load-time) - 11 lines of
pseudo-code.
 execution of invokeinterface_quick
(execution-time) - 5 lines of code.
9
Validation
• Semantics and performance are preserved for
single inheritance programs. The following single
inheritance Java programs ran correctly with our
JVM: javac, jasper, and javap.
• Multiple inheritance programs show correct
answers with the re-factored java.io library.
• Traditional super calls generate the same results
under both JVMs.
10
Validation - MI Programs
Multiple Inheritance
Test Program
java.io
library
Sun JVM
output
11
Validation - MI Programs
Multiple Inheritance
Test Program
Tests the super
call mechanism
java.io
library
Uses the re-factored
java.io library
Our JVM
output

12
Adding Code in Interfaces
interface DataInput{
public float readFloat()
throws IOException;
/*MI_CODE
{
return
abstract class DataInput{
public float readFloat()
throws IOException
Script {
return
Float.intBitsToFloat(readInt());
Float.intBitsToFloat(readInt());
}
MI_CODE*/
}
}
;
}
javac
javac
DataInput.class
jasper
Script
DataInput.class
jasper
DataInput_MI.j
DataInput.j
DataInput.j
jasmin
DataInput.class
13
Conclusions - I
• The first implementation of multiple code inheritance
in Java is provided by modifying Sun’s JVM. It is
based on the novel concept of adding code to
interfaces.
• Facilitates code re-use, supports separation of
inheritance concepts, and improves clarity and
expressiveness of implementation.
• Only small and localized changes are made to the
JVM.
• Java syntax and compiler are not changed. A set of
scripts allows a programmer to add code in interfaces.
14
Conclusions - II
• We defined a super call mechanism for super
calls to interfaces, resembling the one in C++.
• Single inheritance programs achieve the same
performance as with the original JVM.
• Single and multiple inheritance programs run
correctly with our JVM, for both our basic and
super call mechanism implementations.
• Multiple code inheritance measurements show
significant code decrease.
15
Load-time Change
if (imb.code <> null) //code in interface
currentmb = class.vmt[vmtIndex];
if (currentmb.code == null) //no code in MT
class.vmt[vmtIndex] = imb; //point VMT to imb
else //potential code ambiguity
if (!currentmb.class.imt.contains(imb) &&
!imb.class.imt.contains(currentmb))
throw ambiguous method exception
end if
end if
end if
16
Execution-time Change
case opc_invokeinterface_quick:
imb = constant_pool[GET_INDEX(pc+1)].mb;
interface = imb.class;
offset = imb.offset;
…
//args_size = pc[3];
//REMOVED
args_size = imb.args_size;
//ADDED
optop -= args_size;
…
if (pc[3] == 255)
//ADDED
mb = interface.MT[offset];
//ADDED
goto callmethod;
//ADDED
end if
//ADDED
…
end case
17