package jdk.internal.vm.compiler.libgraal;
import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.runtime;
import jdk.vm.ci.hotspot.HotSpotSpeculationLog;
import jdk.vm.ci.services.Services;
public class LibGraal {
static {
Services.initializeJVMCI();
}
public static boolean isAvailable() {
return inLibGraal() || theIsolate != 0L;
}
public static boolean isSupported() {
return true;
}
public static boolean inLibGraal() {
return Services.IS_IN_NATIVE_IMAGE;
}
public static void registerNativeMethods(Class<?> clazz) {
if (clazz.isPrimitive()) {
throw new IllegalArgumentException();
}
if (inLibGraal() || !isAvailable()) {
throw new IllegalStateException();
}
runtime().registerNativeMethods(clazz);
}
public static long translate(Object obj) {
if (!isAvailable()) {
throw new IllegalStateException();
}
if (!inLibGraal() && LibGraalScope.currentScope.get() == null) {
throw new IllegalStateException("Not within a " + LibGraalScope.class.getName());
}
return runtime().translate(obj);
}
public static <T> T unhand(Class<T> type, long handle) {
if (!isAvailable()) {
throw new IllegalStateException();
}
if (!inLibGraal() && LibGraalScope.currentScope.get() == null) {
throw new IllegalStateException("Not within a " + LibGraalScope.class.getName());
}
return runtime().unhand(type, handle);
}
private static long initializeLibgraal() {
try {
long[] javaVMInfo = runtime().registerNativeMethods(LibGraalScope.class);
return javaVMInfo[1];
} catch (UnsupportedOperationException e) {
return 0L;
}
}
static final long theIsolate = Services.IS_BUILDING_NATIVE_IMAGE ? 0L : initializeLibgraal();
static boolean isCurrentThreadAttached() {
return runtime().isCurrentThreadAttached();
}
public static boolean attachCurrentThread(boolean isDaemon, long[] isolate) {
if (isolate != null) {
isolate[0] = LibGraal.theIsolate;
}
return runtime().attachCurrentThread(isDaemon);
}
public static boolean detachCurrentThread(boolean release) {
runtime().detachCurrentThread();
return false;
}
public static long getFailedSpeculationsAddress(HotSpotSpeculationLog log) {
return log.getFailedSpeculationsAddress();
}
}