54601
|
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;
|
58299
|
28 |
import jdk.vm.ci.hotspot.HotSpotSpeculationLog;
|
54601
|
29 |
import jdk.vm.ci.services.Services;
|
|
30 |
|
|
31 |
/**
|
|
32 |
* JDK13+ version of {@code LibGraal}.
|
|
33 |
*/
|
|
34 |
public class LibGraal {
|
|
35 |
|
|
36 |
public static boolean isAvailable() {
|
55509
|
37 |
return inLibGraal() || isolate != 0L;
|
54601
|
38 |
}
|
|
39 |
|
58299
|
40 |
public static boolean isSupported() {
|
|
41 |
return true;
|
|
42 |
}
|
|
43 |
|
55509
|
44 |
public static boolean inLibGraal() {
|
54601
|
45 |
return Services.IS_IN_NATIVE_IMAGE;
|
|
46 |
}
|
|
47 |
|
55509
|
48 |
public static void registerNativeMethods(HotSpotJVMCIRuntime runtime, Class<?> clazz) {
|
54601
|
49 |
if (clazz.isPrimitive()) {
|
|
50 |
throw new IllegalArgumentException();
|
|
51 |
}
|
55509
|
52 |
if (inLibGraal() || !isAvailable()) {
|
54601
|
53 |
throw new IllegalStateException();
|
|
54 |
}
|
55509
|
55 |
runtime.registerNativeMethods(clazz);
|
54601
|
56 |
}
|
|
57 |
|
|
58 |
public static long translate(HotSpotJVMCIRuntime runtime, Object obj) {
|
|
59 |
if (!isAvailable()) {
|
|
60 |
throw new IllegalStateException();
|
|
61 |
}
|
55509
|
62 |
if (!inLibGraal() && LibGraalScope.currentScope.get() == null) {
|
|
63 |
throw new IllegalStateException("Not within a " + LibGraalScope.class.getName());
|
|
64 |
}
|
|
65 |
return runtime.translate(obj);
|
54601
|
66 |
}
|
|
67 |
|
|
68 |
public static <T> T unhand(HotSpotJVMCIRuntime runtime, Class<T> type, long handle) {
|
|
69 |
if (!isAvailable()) {
|
|
70 |
throw new IllegalStateException();
|
|
71 |
}
|
55509
|
72 |
if (!inLibGraal() && LibGraalScope.currentScope.get() == null) {
|
|
73 |
throw new IllegalStateException("Not within a " + LibGraalScope.class.getName());
|
|
74 |
}
|
|
75 |
return runtime.unhand(type, handle);
|
54601
|
76 |
}
|
|
77 |
|
|
78 |
private static long initializeLibgraal() {
|
|
79 |
try {
|
|
80 |
// Initialize JVMCI to ensure JVMCI opens its packages to
|
|
81 |
// Graal otherwise the call to HotSpotJVMCIRuntime.runtime()
|
|
82 |
// below will fail on JDK13+.
|
|
83 |
Services.initializeJVMCI();
|
|
84 |
|
55509
|
85 |
HotSpotJVMCIRuntime runtime = HotSpotJVMCIRuntime.runtime();
|
|
86 |
long[] nativeInterface = runtime.registerNativeMethods(LibGraal.class);
|
|
87 |
return nativeInterface[1];
|
54601
|
88 |
} catch (UnsupportedOperationException e) {
|
|
89 |
return 0L;
|
|
90 |
}
|
|
91 |
}
|
|
92 |
|
55509
|
93 |
static final long isolate = Services.IS_BUILDING_NATIVE_IMAGE ? 0L : initializeLibgraal();
|
|
94 |
|
|
95 |
static boolean isCurrentThreadAttached(HotSpotJVMCIRuntime runtime) {
|
|
96 |
return runtime.isCurrentThreadAttached();
|
|
97 |
}
|
|
98 |
|
58299
|
99 |
public static boolean attachCurrentThread(HotSpotJVMCIRuntime runtime, boolean isDaemon) {
|
|
100 |
return runtime.attachCurrentThread(isDaemon);
|
55509
|
101 |
}
|
|
102 |
|
58299
|
103 |
public static void detachCurrentThread(HotSpotJVMCIRuntime runtime) {
|
55509
|
104 |
runtime.detachCurrentThread();
|
|
105 |
}
|
|
106 |
|
|
107 |
static native long getCurrentIsolateThread(long iso);
|
58299
|
108 |
|
|
109 |
public static long getFailedSpeculationsAddress(HotSpotSpeculationLog log) {
|
|
110 |
return log.getFailedSpeculationsAddress();
|
|
111 |
}
|
54601
|
112 |
}
|