Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Theoretically speaking , can I get the openJDK JIT , and compile my java code to native?

I just wonder how can I get rid of the java jre dependency and produce native code and deliver the compiled code as the application?

So does it possible?

P.S. I know about gcj compiler is it what its doing ?

like image 298
user63898 Avatar asked Feb 15 '11 08:02

user63898


People also ask

Does OpenJDK have a JIT?

The JIT compiler in OpenJDK. A Java-based JIT compiler takes . class files as input rather than Java code, which is consumed by javac . In this way, a JIT compiler differs from a compiler like GCC, which directly consumes the code that you produce.

Is Java JIT compiled?

The JIT compiler is enabled by default, and is activated when a Java method is called. The JIT compiler compiles the bytecodes of that method into native machine code, compiling it "just in time" to run. When a method has been compiled, the JVM calls the compiled code of that method directly instead of interpreting it.

Is JIT a compiler or interpreter?

A Just-In-Time (JIT) compiler is a feature of the run-time interpreter, that instead of interpreting bytecode every time a method is invoked, will compile the bytecode into the machine code instructions of the running machine, and then invoke this object code instead.

What is JIT compilation Java?

The Just-In-Time (JIT) compiler is a component of the runtime environment that improves the performance of Java™ applications by compiling bytecodes to native machine code at run time.


3 Answers

The compiled byte code will still depend on the java virtual machine. A JIT can't create code that "makes any sense" outside the JVM container. Yes, the result is a bunch of valid instructions for the target platform. But you still need the actual stack, heap and garbage collector (just to name a few required building blocks).

like image 57
Andreas Dolk Avatar answered Nov 07 '22 23:11

Andreas Dolk


Excelsior has a very good Java2Native compiler. I would love to use it, but sadly our project takes 8 hours to compile with this compiler. The resulting speed of the app is impressive thought.

like image 40
Daniel Avatar answered Nov 08 '22 01:11

Daniel


In theory, it's possible to take any interpreter for a language and turn it into a compiler that produces native code in that language. This is related to a series of equations called the Futamura projections. The high-level idea is essentially to "cheat" at how you define a compiler. Suppose that for some language L I have an interpreter I(p) that, given a program p written in language L, interprets that program. Now, I assume that interpreter I is represented directly in machine code. Further suppose that I have a program called mix that, given a machine code program and a sequence of input to that program, produces a new machine code program that is the initial program with its input fixed to be the specified input. For example, if I compiled this C++ program:

#include <iostream>
using namespace std;

int main() {
    string message;
    cin >> message;
    cout << message << endl;
}

And then used mix to mix the program with the input "Hello," I'd get a program that always prints out the message "Hello". In other words, it would be as if I had written this program:

#include <iostream>
using namespace std;

int main() {
    cout << "Hello" << endl;
}

It turns out that it's possible to build this program. I could do this, for example, by looking at the machine code, looking at every place that you try to read input from the console, and then replacing that with code that calls a function to instead read from a hardcoded string.

Now, consider what would happen if you were to run this mix program taking as input an interpreter I and some program p. Then the result of this would be a machine code program that is equivalent to the program I running on input p. In other words, you've just constructed a machine-code program that simulates what would happen if you were to run the interpreter on the program - which is a machine-code program that executes the program p!

Of course, this construction is completely impractical. To the best of my knowledge no one has written mix, and if they did, any program you made by turning an interpreter into a compiler would be woefully inefficient because it wouldn't be at all optimized.

As to your original question about whether you could take the JVM's JIT and use it to produce raw machine code for a Java program, I'm not sure since I haven't looked at the source code, but I strongly doubt it. The machine code almost certainly contains hooks that would call back into the JVM for specific tasks (for example, garbage collection, class loading, etc.), which would make the generated code not work in a standalone environment. However, it is a really cool idea to try to do this, and I hope that this answer shines some light on the theory behind it!

like image 39
templatetypedef Avatar answered Nov 08 '22 01:11

templatetypedef