5

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.
Community
  • 1
  • 1
Silverclaw
  • 1,316
  • 2
  • 15
  • 28
  • 1
    I suppose that one of the answers in original thread, to use SWIG to generate bindings from C, might be best solution. It is future proof (just regenerate bindings for new version of LLVM) and you are in full control. I have found working with SWIG a real pleasure - it has really great possibilities (it allows you for example to extend C++ class with hierarchy java class and have proper java methods called from C++ virtual dispatch - at no coding cost). – Artur Biesiadowski Sep 14 '16 at 09:44
  • I am at a similar situation. I want to implement a toy language in Java targeting LLVM IR and I am looking for a maintained Java bindings of the LLVM libraries. – Romildo Sep 14 '16 at 21:26
  • Just for the record, I think I ended up learning C...it's not even as bad as I thought but it's still a shame that Java lacks good LLVM bindings. I'll try if I can do that too, in case I ever have too much time... – Silverclaw Sep 15 '16 at 09:57

2 Answers2

9

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

Silverclaw
  • 1,316
  • 2
  • 15
  • 28
1

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:

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.

gifa
  • 86
  • 5