Generating LLVM Code from Java

雨燕双飞 提交于 2019-12-03 16:27:20

问题


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?


回答1:


A quick search for llvm java api bindings turned out several projects that seem like a good fit:

  • LLVM-J
  • JLLVM
  • LAJ

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.




回答2:


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.




回答3:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!