I want to use the LLVM code generation Framework from Java.
I.e., I do not want to compile Java code into LLVM. I simply want an LLVM library for code generation that I can call from Java. The usual LLVM library is C, so I cannot use it.
Are there any Java ports? If no, what would be the easiest way to do it anyway? Wrap the API into JNI?
A quick search for llvm java api bindings turned out several projects that seem like a good fit:
All of those libraries use JNI to access the C-API, so you have a DLL or SO file of the LLVM itself in any case.
To answer the question with more up-to-date information based on the current LLVM v3.6. Your options are:
- RoboVM LLVM Java bindings part of RoboVM project supporting the current LLVM v3.6 (GPL License).
- LLVM-J which moved to Github and supports LLVM v3.0 (MIT License).
- JLLVM which supports LLVM v3.2 (LGBL License).
If GPL licensing is OK with you, then you should definitely go for RoboVM. It's an active project with robust support for the latest LLVM version. JLLVM and LLVM-J are no longer active so you will need to do some extra work using them.
The easiest way to access C libraries from Java is JNA. You create a Java interface that is isomorphic with the parts of the C API you wish to use, and then JNA does the rest. This means that you only have to update methods directly pertinent to you when the API changes.
https://github.com/twall/jna/blob/master/README.md
SWIG is harder to use, but JNA can be a dead end if you find yourself needing to improve performance. JLLVM is a SWIG-based tool, so you might consider referencing it or forking it for your own purposes.
Don't use basic JNI – choose between SWIG or JNA.
Blindly relying on third-party wrappers with minimal history is a dicey proposition, but if you deliberately treat such a project as a starting point, you can't go wrong.
With both technologies, you'll occasionally have to go digging around for enum constants. If they're not easy to read from header files, you'll want to write a simple C program that prints out the constants you're interested in, so you can manually copy them to your Java interfaces.
来源:https://stackoverflow.com/questions/12358684/generating-llvm-code-from-java