Jeannie: Language Support for the Java Native Interface

Download Report

Transcript Jeannie: Language Support for the Java Native Interface

Jeannie: Granting Java Native
Interface Developers their Wishes
Martin Hirzel
Robert Grimm
IBM Watson Research Center
New York University
October 23, 2007
1
FFI: Foreign Function Interface
Standard
Libraries
—
java.io
User
Program
—
nqueens
Custom
Libraries
—
JavaBDD
Virtual Execution Environment
—
JVM
Operating System
—
Linux
Brown:
Java
Motivation:
• OS Services
• Legacy Code
• Performance
• Convenience
Blue:
C
2
JNI = Java Native Interface
Java
Native method:
no body in Java
C function with mangled name
implements Java method
C
QuickTime™ and a
TIFF (Uncompressed) decompressor
are needed to see this picture.
Read Java
field from C
Throw Java
exception from C
3
FFI Design Goals
Productivity
– Writing & maintaining code
Safety
– Preventing & detecting bugs
Efficiency
– At transition & elsewhere
Portability
– Different OS, HW, virtual execution environment
4
Jeannie Example
Support full
C syntax
Can use
short names
Native methods have a body
QuickTime™ and a
TIFF (Uncompressed) decompressor
are needed to see this picture.
Nested
block
Nested
expression
Can nest to
any depth
Exception handling in C
5
Jeannie Build Process
Socket.jni
Preprocessor
Socket.jni.i
C sources
Jeannie Compiler
Socket.i
Java sources
Socket.java
C Compiler
Java Compiler
Network.dll
Network.jar
6
Translation Scheme
Jeannie source code
Generated code
1
2
4
QuickTime™ and a
TIFF (Uncompressed) decompressor
are needed to see this picture.
“1”
“2”
3
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)
7
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
8
Java Garbage Collection
JNI
Jeannie
Implicit
Local
(do not collect until
reference
control returns to Java)
Not supported
Do not collect
Global
(just store in
until user calls
reference
Java and use
DeleteGlobalRef
`x.f)
9
Compiler Stages
Jeannie code
Jeannie grammar
Jeannie Parser
Jeannie AST
Jeannie Analyzer
Jeannie AST+SymTab
Code generator
C code
Java code
10
Scalable Composition: Syntax
Jeannie code
C grammar
Jeannie Parser
Jeannie grammar
Jeannie AST
Java grammar
Rats!
parser
generator
Jeannie Analyzer
Jeannie AST+SymTab
Code generator
C code
Java code
11
Scalable Composition: Analyzers
Jeannie code
C grammar
Jeannie Parser
Jeannie grammar
Jeannie AST
Java grammar
C +Jeannie +Java Analyzer
Jeannie AST+SymTab
Rats!
parser
generator
xtc visitors,
common
type rep.
Code generator
C code
Java code
12
Scalable Composition: CodeGen
Jeannie code
C grammar
Jeannie Parser
Jeannie grammar
Jeannie AST
Java grammar
C +Jeannie +Java Analyzer
Jeannie AST+SymTab
C stencils
Code generator
Java stencils
C AST
Java AST
C pretty printer
Java pretty printer
C code
Java code
Rats!
parser
generator
xtc visitors,
common
type rep.
xtc AST
generator
13
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
14
Efficiency: JNI vs. Jeannie
J 9 / L inux
H otSpot / O S X
With array write
With array read
Exception in C in Java
Constant Java expr. in C
Constant C expr. in Java
Empty Java block in C
Empty C block in Java
JavaBDD: Rubik's Cube
JavaBDD: 11-Queens
0
0 .2
0 .4
0 .6
0 .8
1
1
1 .2
1 .4
1 .6
1 .8
2
15
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];
}
16
Efficiency:
Bulk vs. Simple Array Access
J 9 / L inux
H otSpot / O S X
Read
Write
0
10
20
30
40
50
60
70
80
90
1
17
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
18
Conclusions
• Have:
– Language: Jeannie = FFI(Java+C)
– Open source compiler contributed to xtc
http://cs.nyu.edu/rgrimm/xtc
• Next:
– Debugging
– Fault isolation
– Optimization
19