1 /* 2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 25 package jdk.internal.vm.compiler.libgraal; 26 27 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime; 28 import jdk.vm.ci.services.Services; 29 30 /** 31 * JDK13+ version of {@code LibGraal}. 32 */ 33 public class LibGraal { 34 35 public static boolean isAvailable() { 36 return inLibGraal() || isolate != 0L; 37 } 38 39 public static boolean inLibGraal() { 40 return Services.IS_IN_NATIVE_IMAGE; 41 } 42 43 public static void registerNativeMethods(HotSpotJVMCIRuntime runtime, Class<?> clazz) { 44 if (clazz.isPrimitive()) { 45 throw new IllegalArgumentException(); 46 } 47 if (inLibGraal() || !isAvailable()) { 48 throw new IllegalStateException(); 49 } 50 runtime.registerNativeMethods(clazz); 51 } 52 53 public static long translate(HotSpotJVMCIRuntime runtime, Object obj) { 54 if (!isAvailable()) { 55 throw new IllegalStateException(); 56 } 57 if (!inLibGraal() && LibGraalScope.currentScope.get() == null) { 58 throw new IllegalStateException("Not within a " + LibGraalScope.class.getName()); 59 } 60 return runtime.translate(obj); 61 } 62 63 public static <T> T unhand(HotSpotJVMCIRuntime runtime, Class<T> type, long handle) { 64 if (!isAvailable()) { 65 throw new IllegalStateException(); 66 } 67 if (!inLibGraal() && LibGraalScope.currentScope.get() == null) { 68 throw new IllegalStateException("Not within a " + LibGraalScope.class.getName()); 69 } 70 return runtime.unhand(type, handle); 71 } 72 73 private static long initializeLibgraal() { 74 try { 75 // Initialize JVMCI to ensure JVMCI opens its packages to 76 // Graal otherwise the call to HotSpotJVMCIRuntime.runtime() 77 // below will fail on JDK13+. 78 Services.initializeJVMCI(); 79 80 HotSpotJVMCIRuntime runtime = HotSpotJVMCIRuntime.runtime(); 81 long[] nativeInterface = runtime.registerNativeMethods(LibGraal.class); 82 return nativeInterface[1]; 83 } catch (UnsupportedOperationException e) { 84 return 0L; 85 } 86 } 87 88 static final long isolate = Services.IS_BUILDING_NATIVE_IMAGE ? 0L : initializeLibgraal(); 89 90 static boolean isCurrentThreadAttached(HotSpotJVMCIRuntime runtime) { 91 return runtime.isCurrentThreadAttached(); 92 } 93 94 static boolean attachCurrentThread(HotSpotJVMCIRuntime runtime) { 95 return runtime.attachCurrentThread(false); 96 } 97 98 static void detachCurrentThread(HotSpotJVMCIRuntime runtime) { 99 runtime.detachCurrentThread(); 100 } 101 102 static native long getCurrentIsolateThread(long iso); 103 }