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
![Page 1: J e a n n i e : Granting Java Native Interface Developers their Wishes](https://reader036.vdocuments.mx/reader036/viewer/2022062723/5681400a550346895dab4444/html5/thumbnails/1.jpg)
1
Jeannie: Granting Java Native Interface Developers their Wishes
Martin Hirzel Robert GrimmIBM Watson Research Center New York University
October 23, 2007
![Page 2: J e a n n i e : Granting Java Native Interface Developers their Wishes](https://reader036.vdocuments.mx/reader036/viewer/2022062723/5681400a550346895dab4444/html5/thumbnails/2.jpg)
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
![Page 3: J e a n n i e : Granting Java Native Interface Developers their Wishes](https://reader036.vdocuments.mx/reader036/viewer/2022062723/5681400a550346895dab4444/html5/thumbnails/3.jpg)
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
![Page 4: J e a n n i e : Granting Java Native Interface Developers their Wishes](https://reader036.vdocuments.mx/reader036/viewer/2022062723/5681400a550346895dab4444/html5/thumbnails/4.jpg)
4
FFI Design Goals
Productivity– Writing & maintaining code
Safety– Preventing & detecting bugs
Efficiency– At transition & elsewhere
Portability– Different OS, HW, virtual execution environment
![Page 5: J e a n n i e : Granting Java Native Interface Developers their Wishes](https://reader036.vdocuments.mx/reader036/viewer/2022062723/5681400a550346895dab4444/html5/thumbnails/5.jpg)
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
![Page 6: J e a n n i e : Granting Java Native Interface Developers their Wishes](https://reader036.vdocuments.mx/reader036/viewer/2022062723/5681400a550346895dab4444/html5/thumbnails/6.jpg)
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
![Page 7: J e a n n i e : Granting Java Native Interface Developers their Wishes](https://reader036.vdocuments.mx/reader036/viewer/2022062723/5681400a550346895dab4444/html5/thumbnails/7.jpg)
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
![Page 8: J e a n n i e : Granting Java Native Interface Developers their Wishes](https://reader036.vdocuments.mx/reader036/viewer/2022062723/5681400a550346895dab4444/html5/thumbnails/8.jpg)
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
![Page 9: J e a n n i e : Granting Java Native Interface Developers their Wishes](https://reader036.vdocuments.mx/reader036/viewer/2022062723/5681400a550346895dab4444/html5/thumbnails/9.jpg)
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)
![Page 10: J e a n n i e : Granting Java Native Interface Developers their Wishes](https://reader036.vdocuments.mx/reader036/viewer/2022062723/5681400a550346895dab4444/html5/thumbnails/10.jpg)
10
Compiler StagesJeannie code
Jeannie AST
Jeannie AST+SymTab
C code Java code
Jeannie Parser
Jeannie Analyzer
Code generator
Jeannie grammar
![Page 11: J e a n n i e : Granting Java Native Interface Developers their Wishes](https://reader036.vdocuments.mx/reader036/viewer/2022062723/5681400a550346895dab4444/html5/thumbnails/11.jpg)
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
![Page 12: J e a n n i e : Granting Java Native Interface Developers their Wishes](https://reader036.vdocuments.mx/reader036/viewer/2022062723/5681400a550346895dab4444/html5/thumbnails/12.jpg)
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
![Page 13: J e a n n i e : Granting Java Native Interface Developers their Wishes](https://reader036.vdocuments.mx/reader036/viewer/2022062723/5681400a550346895dab4444/html5/thumbnails/13.jpg)
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.
![Page 14: J e a n n i e : Granting Java Native Interface Developers their Wishes](https://reader036.vdocuments.mx/reader036/viewer/2022062723/5681400a550346895dab4444/html5/thumbnails/14.jpg)
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
![Page 15: J e a n n i e : Granting Java Native Interface Developers their Wishes](https://reader036.vdocuments.mx/reader036/viewer/2022062723/5681400a550346895dab4444/html5/thumbnails/15.jpg)
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
![Page 16: J e a n n i e : Granting Java Native Interface Developers their Wishes](https://reader036.vdocuments.mx/reader036/viewer/2022062723/5681400a550346895dab4444/html5/thumbnails/16.jpg)
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];
}
![Page 17: J e a n n i e : Granting Java Native Interface Developers their Wishes](https://reader036.vdocuments.mx/reader036/viewer/2022062723/5681400a550346895dab4444/html5/thumbnails/17.jpg)
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
![Page 18: J e a n n i e : Granting Java Native Interface Developers their Wishes](https://reader036.vdocuments.mx/reader036/viewer/2022062723/5681400a550346895dab4444/html5/thumbnails/18.jpg)
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
![Page 19: J e a n n i e : Granting Java Native Interface Developers their Wishes](https://reader036.vdocuments.mx/reader036/viewer/2022062723/5681400a550346895dab4444/html5/thumbnails/19.jpg)
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