Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with LLVM from Java [closed]

Tags:

java

llvm

llvm-ir

I'm fully aware that a similar question was already asked here: Generating LLVM Code from Java

The thing is, that was in 2012...I looked at the solutions and found most of the projects mentioned abandoned or at least very inactive. So, as someone who is most used to working with Java, what would be my options for working with LLVM (to create a toy language, not using clang or anything)?

  • Try to use one of those projects, even if the might be outdated? Sounds like a bad idea.
  • Learn C/C++? Don't get me wrong, I already have a C++-book lying around and I don't say it's a bad language, but I highly doubt I would feel comfortable working with it.
  • Use bindings for other languages, like Haskell, Python, etc.? I might prefer that over C/C++, but that would mean to learn another complete language before getting started...
  • Write my own bindings? I never did anything like that,I would not even know the difference between JNI, JNA and whatnot...but might be interesting to learn.
  • Try to format LLVM IR in text form? Might work, but is probably not the best idea either.
like image 634
Silverclaw Avatar asked Sep 09 '26 14:09

Silverclaw


2 Answers

Just to finally answer this, Java C++ Presets are a useful and mostly-upto-date option for this: https://github.com/bytedeco/javacpp-presets/tree/master/llvm

like image 168
Silverclaw Avatar answered Sep 11 '26 05:09

Silverclaw


I came across the same problem recently since I'm using ANTLR in Java/Scala to define my lexer and parser and LLVM to generate the actual machine code through its IR. In trying to bind JVM-based front-end to LLVM IR and back-end I am actually trying to use GraalVM https://www.graalvm.org/ since it offers a seamless way to interact across languages, including the LLVM bitcode, using its Polyglot.

Here some references:

  • https://www.graalvm.org/docs/reference-manual/languages/llvm/
  • https://medium.com/graalvm/graalvm-llvm-toolchain-f606f995bf
  • https://blog.plan99.net/graal-truffle-134d8f28fb69

You should be able to access LLVM IR directly from your Java/Scala code or vice versa, you could access your AST in JVM-based language from LLVM/C++ code using Polyglot calls.

like image 33
gifa Avatar answered Sep 11 '26 05:09

gifa