j e a n n i e : granting java native interface developers their wishes
DESCRIPTION
J e a n n i e : Granting Java Native Interface Developers their Wishes. FFI: Foreign Function Interface. User Program — nqueens. Brown: Java. Stan dard Libra ries — ja va. io. Cus tom Libra ries — Java BDD. Motivation: OS Services Legacy Code Performance Convenience. - PowerPoint PPT PresentationTRANSCRIPT
1
Jeannie: Granting Java Native Interface Developers their Wishes
Martin Hirzel Robert GrimmIBM Watson Research Center New York University
October 23, 2007
2
FFI: Foreign Function Interface
UserProgram
—nqueens
Virtual Execution Environment —JVM
Operating System —Linux
StandardLibraries
— java.io
CustomLibraries
—JavaBDD
Motivation:• OS Services• Legacy Code• Performance• Convenience
Blue:C
Brown:Java
3
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.
JNI = Java Native InterfaceJava
C
Native method:no body in Java
C function with mangled nameimplements Java method
Read Javafield from C
Throw Javaexception from C
4
FFI Design Goals
Productivity– Writing & maintaining code
Safety– Preventing & detecting bugs
Efficiency– At transition & elsewhere
Portability– Different OS, HW, virtual execution environment
5
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.
Jeannie Example
Native methods have a body
Nestedblock
Can nest toany depth
Can useshort names
Nestedexpression
Support fullC syntax
Exception handling in C
6
Jeannie Build ProcessSocket.jni
Socket.jni.i
Preprocessor
Jeannie Compiler
Socket.i Socket.java
Java Compiler
Java sources
Network.jar
C Compiler
C sources
Network.dll
7
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.
Translation Scheme
“1”
“2”
class JavaEnv { int x int z native m1() m2(CEnv) native m3(CEnv) m4(CEnv)}struct CEnv { jint y};Java_C_m1(JEnv)Java_C_m3(JEnv,CEnv)
Jeannie source code Generated code
1
2
43
8
Typing
• Nested expressions have equivalent types– if (`((jboolean)feof(stdin))) ..;
• Java references are opaque in C– Use in nested Java expression– Assign to variable– Pass as parameter– Return as result
• C can widen Java references– `List lst = `new ArrayList(10);
• C pointers/structs/unions are illegal in Java
9
Java Garbage Collection
JNI Jeannie
Localreference
Implicit(do not collect until
control returns to Java)
Global reference
Do not collect until user calls
DeleteGlobalRef
Not supported(just store in Java and use
`x.f)
10
Compiler StagesJeannie code
Jeannie AST
Jeannie AST+SymTab
C code Java code
Jeannie Parser
Jeannie Analyzer
Code generator
Jeannie grammar
11
Scalable Composition: SyntaxJeannie code
Jeannie AST+SymTab
Jeannie Parser
Code generator
C grammar
Jeannie grammar
Java grammar
Rats!parsergenerator
Jeannie Analyzer
C code Java code
Jeannie AST
12
C grammar
Jeannie grammar
Java grammar
Scalable Composition: AnalyzersJeannie code
Jeannie ParserRats!parsergenerator
xtc visitors,commontype rep.Jeannie AST+SymTab
Code generator
C code Java code
C +Jeannie +Java Analyzer
Jeannie AST
13
C +Jeannie +Java Analyzer
C grammar
Jeannie grammar
Java grammar
Scalable Composition: CodeGenJeannie code
Jeannie AST
Jeannie AST+SymTab
C AST Java AST
C code Java code
Jeannie Parser
Code generator
C pretty printer Java pretty printer
C stencils Java stencils xtc ASTgenerator
Rats!parsergenerator
xtc visitors,commontype rep.
14
FFI Design Goals: Revisited
Productivity– Concise syntax
Safety– Static error checking
Efficiency– See next slide …
Portability– Java virtual machines: HotSpot, J9, Jikes RVM– Operating systems: Mac OS X, Linux, Cygwin
15
Efficiency: JNI vs. Jeannie
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
JavaBDD: 11-Queens
JavaBDD: Rubik's Cube
Empty C block in Java
Empty Java block in C
Constant C expr. in Java
Constant Java expr. in C
Exception in C in Java
With array read
With array writeJ9 / Linux
HotSpot / OS X
1
16
• Nested expression is simple:for (i = 0, n = `ja.length; i < n; i++)
s += `ja[`i];
Array Access
• But bulk access is faster:with (jint* ca = `ja) {
jint n = `ja.length;
for (jint i=0; i<n; i++)
s += ca[i];
}
17
0 10 20 30 40 50 60 70 80 90
Write
Read
J9 / Linux
HotSpot / OS X
Efficiency:Bulk vs. Simple Array Access
1
18
Related Work
• Improving JNI– Productivity: Bubak+Kurzyniec (Janet)– Safety: Tan et al. (Ccured, ILEA),
Furr+Foster (type inference)– Efficiency: Stepanian et al. (JIT native code)– Portability: Chen et al. (dynamic binary translator)
• Deep language interoperability– XJ, XTATIC, C, Linq, DALI
• Scalable composition– Polyglot, JastAdd, Silver, Stratego/XT, Safari
19
Conclusions
• Have:– Language: Jeannie = FFI(Java+C)– Open source compiler contributed to xtc http://cs.nyu.edu/rgrimm/xtc
• Next:– Debugging– Fault isolation– Optimization