反汇编 Java JIT 编译的原生字节码

2022-01-16 00:00:00 jvm jit compiler-construction java

有没有办法对 Java 即时编译器生成的本机代码进行汇编转储?

Is there any way to do an assembly dump of the native code generated by the Java just-in-time compiler?

还有一个相关的问题:有没有什么方法可以在不运行 JVM 的情况下使用 JIT 编译器将我的代码编译为本机代码?

And a related question: Is there any way to use the JIT compiler without running the JVM to compile my code into native machine code?

推荐答案

是的,有一种打印生成的本机代码的方法(需要 OpenJDK 7).

Yes, there is a way to print the generated native code (requires OpenJDK 7).

不,没有办法使用 JDK 的 JIT 将 Java 字节码编译为本机代码并将其保存为本机可执行文件.

No, there is no way to compile your Java bytecode to native code using the JDK's JIT and save it as a native executable.

即使这是可能的,它也可能没有你想象的那么有用.JVM 做了一些非常复杂的优化,如果需要,它甚至可以动态地去优化代码.换句话说,它并不像 JIT 将您的代码编译为本地机器语言那么简单,然后在程序运行时该本地机器语言将保持不变.此外,这不会让您制作独立于 JVM 和运行时库的本机可执行文件.

Even if this were possible, it would probably not as useful as you think. The JVM does some very sophisticated optimizations, and it can even de-optimize code on the fly if necessary. In other words, it's not as simple as the JIT compiles your code to native machine language, and then that native machine language will remain unchanged while the program is running. Also, this would not let you make a native executable that is independent of the JVM and runtime library.

相关文章