43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2014, 2015, 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 |
package org.graalvm.compiler.hotspot.meta;
|
|
24 |
|
48861
|
25 |
import jdk.vm.ci.hotspot.HotSpotResolvedJavaType;
|
43972
|
26 |
import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
|
|
27 |
import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider;
|
|
28 |
import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
|
|
29 |
import org.graalvm.compiler.word.WordTypes;
|
|
30 |
|
|
31 |
import jdk.vm.ci.hotspot.HotSpotConstantReflectionProvider;
|
|
32 |
import jdk.vm.ci.hotspot.HotSpotObjectConstant;
|
|
33 |
import jdk.vm.ci.meta.JavaConstant;
|
|
34 |
import jdk.vm.ci.meta.JavaKind;
|
|
35 |
import jdk.vm.ci.meta.ResolvedJavaType;
|
|
36 |
|
|
37 |
public class HotSpotSnippetReflectionProvider implements SnippetReflectionProvider {
|
|
38 |
|
|
39 |
private final HotSpotGraalRuntimeProvider runtime;
|
|
40 |
private final HotSpotConstantReflectionProvider constantReflection;
|
|
41 |
private final WordTypes wordTypes;
|
|
42 |
|
|
43 |
public HotSpotSnippetReflectionProvider(HotSpotGraalRuntimeProvider runtime, HotSpotConstantReflectionProvider constantReflection, WordTypes wordTypes) {
|
|
44 |
this.runtime = runtime;
|
|
45 |
this.constantReflection = constantReflection;
|
|
46 |
this.wordTypes = wordTypes;
|
|
47 |
}
|
|
48 |
|
|
49 |
@Override
|
|
50 |
public JavaConstant forObject(Object object) {
|
|
51 |
return constantReflection.forObject(object);
|
|
52 |
}
|
|
53 |
|
|
54 |
@Override
|
|
55 |
public Object asObject(ResolvedJavaType type, JavaConstant constant) {
|
|
56 |
if (constant.isNull()) {
|
|
57 |
return null;
|
|
58 |
}
|
|
59 |
HotSpotObjectConstant hsConstant = (HotSpotObjectConstant) constant;
|
|
60 |
return hsConstant.asObject(type);
|
|
61 |
}
|
|
62 |
|
|
63 |
@Override
|
|
64 |
public <T> T asObject(Class<T> type, JavaConstant constant) {
|
|
65 |
if (constant.isNull()) {
|
|
66 |
return null;
|
|
67 |
}
|
|
68 |
HotSpotObjectConstant hsConstant = (HotSpotObjectConstant) constant;
|
|
69 |
return hsConstant.asObject(type);
|
|
70 |
}
|
|
71 |
|
|
72 |
@Override
|
|
73 |
public JavaConstant forBoxed(JavaKind kind, Object value) {
|
|
74 |
if (kind == JavaKind.Object) {
|
|
75 |
return forObject(value);
|
|
76 |
} else {
|
|
77 |
return JavaConstant.forBoxedPrimitive(value);
|
|
78 |
}
|
|
79 |
}
|
|
80 |
|
|
81 |
// Lazily initialized
|
|
82 |
private Class<?> wordTypesType;
|
|
83 |
private Class<?> runtimeType;
|
|
84 |
private Class<?> configType;
|
|
85 |
|
|
86 |
@Override
|
|
87 |
public <T> T getInjectedNodeIntrinsicParameter(Class<T> type) {
|
|
88 |
// Need to test all fields since there no guarantee under the JMM
|
|
89 |
// about the order in which these fields are written.
|
|
90 |
GraalHotSpotVMConfig config = runtime.getVMConfig();
|
48190
|
91 |
if (configType == null || wordTypesType == null || runtimeType == null) {
|
43972
|
92 |
wordTypesType = wordTypes.getClass();
|
|
93 |
runtimeType = runtime.getClass();
|
|
94 |
configType = config.getClass();
|
|
95 |
}
|
|
96 |
|
|
97 |
if (type.isAssignableFrom(wordTypesType)) {
|
|
98 |
return type.cast(wordTypes);
|
|
99 |
}
|
|
100 |
if (type.isAssignableFrom(runtimeType)) {
|
|
101 |
return type.cast(runtime);
|
|
102 |
}
|
|
103 |
if (type.isAssignableFrom(configType)) {
|
|
104 |
return type.cast(config);
|
|
105 |
}
|
|
106 |
return null;
|
|
107 |
}
|
48861
|
108 |
|
|
109 |
@Override
|
|
110 |
public Class<?> originalClass(ResolvedJavaType type) {
|
|
111 |
return ((HotSpotResolvedJavaType) type).mirror();
|
|
112 |
}
|
43972
|
113 |
}
|