68

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.

9
  • 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) Commented May 29, 2012 at 18:09
  • 5
    @andrewcooke- Does javac really emit LLVM bytecode? Can you provide a link on how to do this? Commented May 29, 2012 at 18:11
  • 1
    Based on what I just read about VMkit, it supports ahead-of-time compilation. Isn't that essentially what you need? Commented May 29, 2012 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? Commented May 29, 2012 at 18:22
  • 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. Commented May 29, 2012 at 18:56

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

2 Comments

This java-frontend is not finished, see stackoverflow.com/questions/60580188/…
The link is dead @Ashkan
7

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.

1 Comment

"As of GCC 7, the GCC Java frontend and associated libjava runtime library have been removed from GCC." Source: gcc.gnu.org/wiki/GCJ
2

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.

1 Comment

The question asks for a conversion of sourcecode, not bytecode.
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).

3 Comments

The question asks for a conversion of sourcecode, not bytecode.
@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.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.