190 likes | 459 Views
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.
E N D
Jeannie: Granting Java Native Interface Developers their Wishes
FFI: Foreign Function Interface UserProgram — nqueens Brown:Java StandardLibraries—java.io CustomLibraries—JavaBDD • Motivation: • OS Services • Legacy Code • Performance • Convenience Virtual Execution Environment —JVM Operating System —Linux Blue:C
JNI = Java Native Interface Java Native method:no body in Java C function with mangled nameimplements Java method C Read Javafield from C Throw Javaexception from C
FFI Design Goals Productivity • Writing & maintaining code Safety • Preventing & detecting bugs Efficiency • At transition & elsewhere Portability • Different OS, HW, virtual execution environment
JeannieExample Support fullC syntax Can useshort names Native methods have a body Nestedblock Nestedexpression Can nest toany depth Exception handling in C
Jeannie Build Process Socket.jni Preprocessor Socket.jni.i Jeannie Compiler C sources Java sources Socket.i Socket.java C Compiler Java Compiler Network.dll Network.jar
Translation Scheme Jeannie source code Generated code 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) 1 2 3 4 “1” “2”
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
Compiler Stages Jeannie code Jeannie grammar Jeannie Parser Jeannie AST Jeannie Analyzer Jeannie AST+SymTab Code generator C code Java code
Scalable Composition: Syntax Jeannie code C grammar Rats!parsergenerator Jeannie Parser Jeannie grammar Java grammar Jeannie AST Jeannie Analyzer Jeannie AST+SymTab Code generator C code Java code
Scalable Composition: Analyzers Jeannie code C grammar Rats!parsergenerator Jeannie Parser Jeannie grammar Java grammar Jeannie AST xtc visitors,commontype rep. C +Jeannie +Java Analyzer Jeannie AST+SymTab Code generator C code Java code
Scalable Composition: CodeGen Jeannie code C grammar Rats!parsergenerator Jeannie Parser Jeannie grammar Java grammar Jeannie AST xtc visitors,commontype rep. C +Jeannie +Java Analyzer Jeannie AST+SymTab xtc ASTgenerator Code generator Java stencils C stencils C AST Java AST C pretty printer Java pretty printer C code Java code
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
Array Access • Nested expression is simple: for (i = 0, n = `ja.length; i < n; i++) s += `ja[`i]; • But bulk access is faster: with (jint* ca = `ja) { jint n = `ja.length; for (jint i=0; i<n; i++) s += ca[i]; }
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
Conclusions • Have: • Language: Jeannie = FFI(Java+C) • Open source compiler contributed to xtc http://cs.nyu.edu/rgrimm/xtc • Next: • Debugging • Fault isolation • Optimization