author | kvn |
Wed, 01 May 2019 12:31:29 -0700 | |
changeset 54669 | ad45b3802d4e |
parent 52381 | 7f90bc64b0fc |
permissions | -rw-r--r-- |
43972 | 1 |
/* |
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52381
diff
changeset
|
2 |
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved. |
43972 | 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 jdk.vm.ci.hotspot; |
|
24 |
||
25 |
import java.util.Objects; |
|
26 |
||
27 |
import jdk.vm.ci.common.JVMCIError; |
|
28 |
import jdk.vm.ci.meta.Constant; |
|
29 |
import jdk.vm.ci.meta.ConstantReflectionProvider; |
|
30 |
import jdk.vm.ci.meta.JavaConstant; |
|
31 |
import jdk.vm.ci.meta.JavaKind; |
|
32 |
import jdk.vm.ci.meta.MemoryAccessProvider; |
|
33 |
import jdk.vm.ci.meta.MethodHandleAccessProvider; |
|
34 |
import jdk.vm.ci.meta.ResolvedJavaField; |
|
35 |
import jdk.vm.ci.meta.ResolvedJavaType; |
|
36 |
||
37 |
/** |
|
38 |
* HotSpot implementation of {@link ConstantReflectionProvider}. |
|
39 |
*/ |
|
40 |
public class HotSpotConstantReflectionProvider implements ConstantReflectionProvider { |
|
41 |
||
50858 | 42 |
protected final HotSpotJVMCIRuntime runtime; |
43972 | 43 |
protected final HotSpotMethodHandleAccessProvider methodHandleAccess; |
52381
7f90bc64b0fc
8213203: [JVMCI] adopt formatting changes from jvmci 8
never
parents:
52017
diff
changeset
|
44 |
private final HotSpotMemoryAccessProviderImpl memoryAccess; |
43972 | 45 |
|
50858 | 46 |
public HotSpotConstantReflectionProvider(HotSpotJVMCIRuntime runtime) { |
43972 | 47 |
this.runtime = runtime; |
48 |
this.methodHandleAccess = new HotSpotMethodHandleAccessProvider(this); |
|
49 |
this.memoryAccess = new HotSpotMemoryAccessProviderImpl(runtime); |
|
50 |
} |
|
51 |
||
50858 | 52 |
@Override |
43972 | 53 |
public MethodHandleAccessProvider getMethodHandleAccess() { |
54 |
return methodHandleAccess; |
|
55 |
} |
|
56 |
||
57 |
@Override |
|
58 |
public MemoryAccessProvider getMemoryAccessProvider() { |
|
59 |
return memoryAccess; |
|
60 |
} |
|
61 |
||
62 |
@Override |
|
63 |
public Boolean constantEquals(Constant x, Constant y) { |
|
64 |
if (x == y) { |
|
65 |
return true; |
|
66 |
} else if (x instanceof HotSpotObjectConstantImpl) { |
|
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52381
diff
changeset
|
67 |
return y instanceof HotSpotObjectConstantImpl && x.equals(y); |
43972 | 68 |
} else { |
69 |
return Objects.equals(x, y); |
|
70 |
} |
|
71 |
} |
|
72 |
||
73 |
@Override |
|
74 |
public Integer readArrayLength(JavaConstant array) { |
|
75 |
if (array == null || array.getJavaKind() != JavaKind.Object || array.isNull()) { |
|
76 |
return null; |
|
77 |
} |
|
78 |
||
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52381
diff
changeset
|
79 |
HotSpotObjectConstantImpl arrayObject = ((HotSpotObjectConstantImpl) array); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52381
diff
changeset
|
80 |
return runtime.getReflection().getLength(arrayObject); |
43972 | 81 |
} |
82 |
||
83 |
@Override |
|
84 |
public JavaConstant readArrayElement(JavaConstant array, int index) { |
|
85 |
if (array == null || array.getJavaKind() != JavaKind.Object || array.isNull()) { |
|
86 |
return null; |
|
87 |
} |
|
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52381
diff
changeset
|
88 |
HotSpotObjectConstantImpl arrayObject = ((HotSpotObjectConstantImpl) array); |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52381
diff
changeset
|
89 |
return runtime.getReflection().readArrayElement(arrayObject, index); |
43972 | 90 |
} |
91 |
||
92 |
/** |
|
93 |
* Check if the constant is a boxed value that is guaranteed to be cached by the platform. |
|
94 |
* Otherwise the generated code might be the only reference to the boxed value and since object |
|
95 |
* references from nmethods are weak this can cause GC problems. |
|
96 |
* |
|
97 |
* @return true if the box is cached |
|
98 |
*/ |
|
99 |
private static boolean isBoxCached(JavaConstant source) { |
|
100 |
switch (source.getJavaKind()) { |
|
101 |
case Boolean: |
|
102 |
return true; |
|
103 |
case Char: |
|
104 |
return source.asInt() <= 127; |
|
105 |
case Byte: |
|
106 |
case Short: |
|
107 |
case Int: |
|
108 |
return source.asInt() >= -128 && source.asInt() <= 127; |
|
109 |
case Long: |
|
110 |
return source.asLong() >= -128 && source.asLong() <= 127; |
|
111 |
case Float: |
|
112 |
case Double: |
|
113 |
return false; |
|
114 |
default: |
|
115 |
throw new IllegalArgumentException("unexpected kind " + source.getJavaKind()); |
|
116 |
} |
|
117 |
} |
|
118 |
||
119 |
@Override |
|
120 |
public JavaConstant boxPrimitive(JavaConstant source) { |
|
121 |
if (source == null || !source.getJavaKind().isPrimitive() || !isBoxCached(source)) { |
|
122 |
return null; |
|
123 |
} |
|
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52381
diff
changeset
|
124 |
return runtime.getReflection().boxPrimitive(source); |
43972 | 125 |
} |
126 |
||
127 |
@Override |
|
128 |
public JavaConstant unboxPrimitive(JavaConstant source) { |
|
129 |
if (source == null || !source.getJavaKind().isObject()) { |
|
130 |
return null; |
|
131 |
} |
|
132 |
if (source.isNull()) { |
|
133 |
return null; |
|
134 |
} |
|
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52381
diff
changeset
|
135 |
return runtime.getReflection().unboxPrimitive((HotSpotObjectConstantImpl) source); |
43972 | 136 |
} |
137 |
||
50858 | 138 |
@Override |
43972 | 139 |
public JavaConstant forString(String value) { |
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52381
diff
changeset
|
140 |
return runtime.getReflection().forObject(value); |
43972 | 141 |
} |
142 |
||
143 |
public JavaConstant forObject(Object value) { |
|
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52381
diff
changeset
|
144 |
return runtime.getReflection().forObject(value); |
43972 | 145 |
} |
146 |
||
147 |
@Override |
|
148 |
public ResolvedJavaType asJavaType(Constant constant) { |
|
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52381
diff
changeset
|
149 |
if (constant instanceof HotSpotObjectConstantImpl) { |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52381
diff
changeset
|
150 |
return ((HotSpotObjectConstantImpl) constant).asJavaType(); |
43972 | 151 |
} |
152 |
if (constant instanceof HotSpotMetaspaceConstant) { |
|
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52381
diff
changeset
|
153 |
MetaspaceObject obj = HotSpotMetaspaceConstantImpl.getMetaspaceObject(constant); |
43972 | 154 |
if (obj instanceof HotSpotResolvedObjectTypeImpl) { |
155 |
return (ResolvedJavaType) obj; |
|
156 |
} |
|
157 |
} |
|
158 |
return null; |
|
159 |
} |
|
160 |
||
50858 | 161 |
@Override |
43972 | 162 |
public JavaConstant readFieldValue(ResolvedJavaField field, JavaConstant receiver) { |
163 |
HotSpotResolvedJavaField hotspotField = (HotSpotResolvedJavaField) field; |
|
164 |
if (hotspotField.isStatic()) { |
|
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52381
diff
changeset
|
165 |
HotSpotResolvedObjectTypeImpl holder = (HotSpotResolvedObjectTypeImpl) hotspotField.getDeclaringClass(); |
43972 | 166 |
if (holder.isInitialized()) { |
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52381
diff
changeset
|
167 |
return holder.readFieldValue(hotspotField, field.isVolatile()); |
43972 | 168 |
} |
169 |
} else { |
|
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52381
diff
changeset
|
170 |
if (receiver.isNonNull() && receiver instanceof HotSpotObjectConstantImpl) { |
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52381
diff
changeset
|
171 |
HotSpotObjectConstantImpl object = ((HotSpotObjectConstantImpl) receiver); |
50858 | 172 |
if (hotspotField.isInObject(receiver)) { |
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52381
diff
changeset
|
173 |
return object.readFieldValue(hotspotField, field.isVolatile()); |
44517
63b463587198
8177673: [JVMCI] missing checks in HotSpotMemoryAccessProviderImpl can cause VM assertions to fail
dnsimon
parents:
43972
diff
changeset
|
174 |
} |
43972 | 175 |
} |
176 |
} |
|
177 |
return null; |
|
178 |
} |
|
179 |
||
180 |
@Override |
|
181 |
public JavaConstant asJavaClass(ResolvedJavaType type) { |
|
54669
ad45b3802d4e
8220623: [JVMCI] Update JVMCI to support JVMCI based Compiler compiled into shared library
kvn
parents:
52381
diff
changeset
|
182 |
return ((HotSpotResolvedJavaType) type).getJavaMirror(); |
43972 | 183 |
} |
184 |
||
185 |
@Override |
|
186 |
public Constant asObjectHub(ResolvedJavaType type) { |
|
187 |
if (type instanceof HotSpotResolvedObjectType) { |
|
188 |
return ((HotSpotResolvedObjectType) type).klass(); |
|
189 |
} else { |
|
190 |
throw JVMCIError.unimplemented(); |
|
191 |
} |
|
192 |
} |
|
193 |
} |