Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to use java.lang.Compiler to implement custom JIT?

user644004Jun 9 2015 — edited Jun 17 2015

Simply as an exercise in curiosity, I'm trying to use java.lang.Compiler as documented in the current version of the JDK API. Essentially, I want to build my own JIT. But my experience with this endeavor is leading me to believe that the API documentation is in error.

(on my Mac dev box) I've created a shared library, libJit.dylib, that exports a function named "java_lang_Compiler_start":

#include "jni.h"

#include <iostream>

extern "C" {

JNIEXPORT void JNICALL java_lang_Compiler_start() {

     std::cout << "ohhai, jit!" << std::endl;

}

}

(compiled using `cc --shared -I ${JAVA_HOME}/include -I ${JAVA_HOME}/include/darwin -lstdc++ target/libJit.dylib src/main/c++/java_lang_Compiler.cxx`)

Then, with a very simple class having a psvm:

package us.w7tek;

class Jit {

     // static {

     //     System.loadLibrary("Jit");

     // }

     public static void main(String[] args) {

          System.out.println("Hai, psvm!");

     }

}

I start Java with the command `java -cp target/classes -Djava.library.path=${PWD}/target -Djava.compiler=Jit us.w7tek.Jit`

Using dtrace, and with the static initializer block shown above un-commented, I am able to verify that libJit.dylib is indeed found and loaded by the JVM. But, with the static initializer code block commented out as shown above, and contrary to the current API documentation for java.lang.Compiler, the native library is not loaded, and of course the java_lang_Compiler_start() function is not executed.

Stack Overflow and various other Google-able locations on the web seem to indicate that this feature has not worked since the introduction of HotSpot many years ago. But I have seen nothing official from Oracle (nor Sun); having said that, this subject is highly-resistant to Google and none of the search terms that I have imagined (including various attempts at quoting variations on "java.compiler", "java.lang.Compiler", JIT) have been very useful.

I've tried using a static initializer block to call java.lang.Compiler.enable() (for the purpose of ensuring that that class gets initialized; but dtrace has also told me that the java.lang.Compiler class file found in the rt.jar is not being loaded, instead the symbols for that class are being linked via JNI from libServer.dylib).

I've also tried to disable HotSpot, to see if that would magically re-enable the documented java.lang.Compiler feature. -Xint didn't help.

As well, I've demonstrated that -Djava.compiler=Xyz is somehow disregarded, and that system property is replaced with <null>.

Does anyone in this forum know how to make java.lang.Compiler work as documented?

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 15 2015
Added on Jun 9 2015
3 comments
878 views