66

From what I've read, there is a llvm program that converts java bytecode to llvm's intermediate form called class2llvm. My question is, how do I access this. What front end do I have to install in order to access this.

VMkit is their implementation of a JVM, but I am looking for how to compile the java source code with llvm, not how to run it.

FeelTheBurns
  • 1,065
  • 2
  • 8
  • 14
  • 1
    you understand that you can use `javac` (the standard java compiler) to do this? you don't need anything from llvm - you can just install the oracle jdk (and i imagine that is how you are expected to prepare java files for class2llvm, but that is only a guess) – andrew cooke May 29 '12 at 18:09
  • 4
    @andrewcooke- Does `javac` really emit LLVM bytecode? Can you provide a link on how to do this? – templatetypedef May 29 '12 at 18:11
  • @andrewcooke How can you do this with javac? Are you sure about that? –  May 29 '12 at 18:12
  • 1
    Based on what I just read about VMkit, it supports ahead-of-time compilation. Isn't that essentially what you need? –  May 29 '12 at 18:14
  • 4
    maybe i am not understanding. javac will go from java source to bytecode. then class2llvm will go from bytecode to llvm. that gets you from java source to llvm. is that not what you want? – andrew cooke May 29 '12 at 18:22
  • @andrew cooke, atm I can't get vmkit to install correctly. So just as an alternative using javac will work. However I am unsure where to find the class2llvm function. It doesn't seem to be part of the core llvm package. – FeelTheBurns May 29 '12 at 18:46
  • 1
    ah, ok, i think i understand what confused everyone. when i said "you don't need anything from llvm" i meant to go from source to bytecode (not from source to llvm). sorry. – andrew cooke May 29 '12 at 18:56
  • 1
    You can find class2llvm here: https://llvm.org/viewvc/llvm-project/java/?sortby=file&pathrev=14484 However, I don't think it compiles anymore.. – cib Apr 19 '14 at 16:42
  • Please also check https://github.com/polyglot-compiler/JLang – user2284570 Jul 27 '20 at 09:00

4 Answers4

35

The Java frontend translates Java bytecode (.class files) into LLVM bytecode. Take a look at this link:

https://llvm.org/svn/llvm-project/java/trunk/docs/java-frontend.txt

Ashkan
  • 1,865
  • 16
  • 13
8

You may take a look at dragonegg, which enables llvm to use gcc's frontends. As gcc already has a frontend for java, called gcj, perhaps llvm can use it to compile java code. But I'm not sure how well llvm interfaces with the gcc frontend, so this may not work.

Martin Monperrus
  • 1,845
  • 2
  • 19
  • 28
lei_z
  • 1,049
  • 2
  • 13
  • 27
  • 1
    "As of GCC 7, the GCC Java frontend and associated libjava runtime library have been removed from GCC." Source: https://gcc.gnu.org/wiki/GCJ – user Jan 24 '21 at 13:59
3

I have executed a java class using vmkit ( http://vmkit.llvm.org/ ) based on LLVM. It uses LLVM for compiling and optimizing high-level languages to machine code. J3 is an implementation of a JVM with VMKit.

osgx
  • 90,338
  • 53
  • 357
  • 513
Ashkan
  • 1,865
  • 16
  • 13
2

[NOTE: From November 2015 it is no longer open source, so this hack is mostly useless.]

RoboVM might become the solution you're looking for. It's open source and compiles JVM bytecode (.class files) to machine code.

I assume they do it using something like class2llvm.

Unfortunately, it's still in alpha. I just tested it on HelloWorld.java. It gave 5x speed up of load time running on a single core. (Most of the run time is load time.)

echo Hello World! : <1 ms : 31K (/usr/bin/echo binary)

java HelloWorld : ~70 ms : 0.4K (HelloWorld.class JVM bytecode)

./HelloWorld : ~13 ms : 9.4MB (9.3MB binary + 57K robovm-rt.jar)

Note that java calls a 32MB $JAVA_HOME/lib/rt.jar file (and maybe more). Searching in such a large file must be part of the reason java is so slow to load. If RoboVM gets smarter, perhaps it can throw out most of the 9.3MB binary for an even faster load?

The website mentions iOS, but I think that's because they're selling their add-on UI libraries. RoboVM compiled fine for me on a flavor of Ubuntu. Just make sure to do

$ sudo apt-get install g++-multilib

first (and maybe install libpthread-stubs0-dev and libpthread-workqueue0...don't know if they mattered).

Community
  • 1
  • 1
expz
  • 1,578
  • 1
  • 13
  • 19
  • 1
    The question asks for a conversion of _sourcecode_, not _bytecode_. – Martin Schröder Jan 07 '15 at 15:35
  • 4
    @Martin, clearly the java compiler converts sourcecode to jvm bytecode which can then be further converted. That's probably the reason the answer with the most votes also explains conversion of bytecode. – expz Feb 24 '15 at 21:02
  • 1
    As of November 2015 RoboVM is no longer open source, i.e, code developed by the company is no longer released as open source code. – Nishant George Agrwal Nov 19 '15 at 19:25