author | dlong |
Tue, 20 Mar 2018 10:23:14 -0700 | |
changeset 49467 | 74db2b7cec75 |
parent 48826 | c4d9d1b08e2e |
child 50858 | 2d3e99a72541 |
permissions | -rw-r--r-- |
43972 | 1 |
/* |
49467
74db2b7cec75
8146201: [AOT] Class static initializers that are not pure should not be executed during static compilation
dlong
parents:
48826
diff
changeset
|
2 |
* Copyright (c) 2011, 2018, 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 static jdk.vm.ci.hotspot.CompilerToVM.compilerToVM; |
|
26 |
import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.runtime; |
|
27 |
import static jdk.vm.ci.hotspot.HotSpotVMConfig.config; |
|
28 |
import static jdk.vm.ci.hotspot.UnsafeAccess.UNSAFE; |
|
29 |
||
30 |
import java.lang.invoke.MethodHandle; |
|
31 |
||
32 |
import jdk.vm.ci.common.JVMCIError; |
|
33 |
import jdk.vm.ci.meta.ConstantPool; |
|
34 |
import jdk.vm.ci.meta.JavaConstant; |
|
35 |
import jdk.vm.ci.meta.JavaField; |
|
36 |
import jdk.vm.ci.meta.JavaMethod; |
|
37 |
import jdk.vm.ci.meta.JavaType; |
|
38 |
import jdk.vm.ci.meta.ResolvedJavaMethod; |
|
39 |
import jdk.vm.ci.meta.ResolvedJavaType; |
|
40 |
import jdk.vm.ci.meta.Signature; |
|
41 |
||
42 |
/** |
|
43 |
* Implementation of {@link ConstantPool} for HotSpot. |
|
44 |
*/ |
|
47668 | 45 |
public final class HotSpotConstantPool implements ConstantPool, MetaspaceWrapperObject { |
43972 | 46 |
|
47 |
/** |
|
48 |
* Subset of JVM bytecode opcodes used by {@link HotSpotConstantPool}. |
|
49 |
*/ |
|
50 |
public static class Bytecodes { |
|
51 |
public static final int LDC = 18; // 0x12 |
|
52 |
public static final int LDC_W = 19; // 0x13 |
|
53 |
public static final int LDC2_W = 20; // 0x14 |
|
54 |
public static final int GETSTATIC = 178; // 0xB2 |
|
55 |
public static final int PUTSTATIC = 179; // 0xB3 |
|
56 |
public static final int GETFIELD = 180; // 0xB4 |
|
57 |
public static final int PUTFIELD = 181; // 0xB5 |
|
58 |
public static final int INVOKEVIRTUAL = 182; // 0xB6 |
|
59 |
public static final int INVOKESPECIAL = 183; // 0xB7 |
|
60 |
public static final int INVOKESTATIC = 184; // 0xB8 |
|
61 |
public static final int INVOKEINTERFACE = 185; // 0xB9 |
|
62 |
public static final int INVOKEDYNAMIC = 186; // 0xBA |
|
63 |
public static final int NEW = 187; // 0xBB |
|
64 |
public static final int NEWARRAY = 188; // 0xBC |
|
65 |
public static final int ANEWARRAY = 189; // 0xBD |
|
66 |
public static final int CHECKCAST = 192; // 0xC0 |
|
67 |
public static final int INSTANCEOF = 193; // 0xC1 |
|
68 |
public static final int MULTIANEWARRAY = 197; // 0xC5 |
|
69 |
||
70 |
static boolean isInvoke(int opcode) { |
|
71 |
switch (opcode) { |
|
72 |
case INVOKEVIRTUAL: |
|
73 |
case INVOKESPECIAL: |
|
74 |
case INVOKESTATIC: |
|
75 |
case INVOKEINTERFACE: |
|
76 |
case INVOKEDYNAMIC: |
|
77 |
return true; |
|
78 |
default: |
|
79 |
return false; |
|
80 |
} |
|
81 |
} |
|
82 |
||
83 |
/** |
|
84 |
* See: {@code Rewriter::maybe_rewrite_invokehandle}. |
|
85 |
*/ |
|
86 |
static boolean isInvokeHandleAlias(int opcode) { |
|
87 |
switch (opcode) { |
|
88 |
case INVOKEVIRTUAL: |
|
89 |
case INVOKESPECIAL: |
|
90 |
return true; |
|
91 |
default: |
|
92 |
return false; |
|
93 |
} |
|
94 |
} |
|
95 |
} |
|
96 |
||
97 |
/** |
|
98 |
* Enum of all {@code JVM_CONSTANT} constants used in the VM. This includes the public and |
|
99 |
* internal ones. |
|
100 |
*/ |
|
101 |
private enum JVM_CONSTANT { |
|
102 |
// @formatter:off |
|
103 |
Utf8(config().jvmConstantUtf8), |
|
104 |
Integer(config().jvmConstantInteger), |
|
105 |
Long(config().jvmConstantLong), |
|
106 |
Float(config().jvmConstantFloat), |
|
107 |
Double(config().jvmConstantDouble), |
|
108 |
Class(config().jvmConstantClass), |
|
109 |
UnresolvedClass(config().jvmConstantUnresolvedClass), |
|
110 |
UnresolvedClassInError(config().jvmConstantUnresolvedClassInError), |
|
111 |
String(config().jvmConstantString), |
|
112 |
Fieldref(config().jvmConstantFieldref), |
|
113 |
MethodRef(config().jvmConstantMethodref), |
|
114 |
InterfaceMethodref(config().jvmConstantInterfaceMethodref), |
|
115 |
NameAndType(config().jvmConstantNameAndType), |
|
116 |
MethodHandle(config().jvmConstantMethodHandle), |
|
117 |
MethodHandleInError(config().jvmConstantMethodHandleInError), |
|
118 |
MethodType(config().jvmConstantMethodType), |
|
119 |
MethodTypeInError(config().jvmConstantMethodTypeInError), |
|
120 |
InvokeDynamic(config().jvmConstantInvokeDynamic); |
|
121 |
// @formatter:on |
|
122 |
||
123 |
private final int tag; |
|
124 |
||
125 |
private static final int ExternalMax = config().jvmConstantExternalMax; |
|
126 |
private static final int InternalMin = config().jvmConstantInternalMin; |
|
127 |
private static final int InternalMax = config().jvmConstantInternalMax; |
|
128 |
||
129 |
JVM_CONSTANT(int tag) { |
|
130 |
this.tag = tag; |
|
131 |
} |
|
132 |
||
133 |
/** |
|
134 |
* Maps JVM_CONSTANT tags to {@link JVM_CONSTANT} values. Using a separate class for lazy |
|
135 |
* initialization. |
|
136 |
*/ |
|
137 |
static class TagValueMap { |
|
138 |
private static final JVM_CONSTANT[] table = new JVM_CONSTANT[ExternalMax + 1 + (InternalMax - InternalMin) + 1]; |
|
139 |
||
140 |
static { |
|
141 |
assert InternalMin > ExternalMax; |
|
142 |
for (JVM_CONSTANT e : values()) { |
|
143 |
table[indexOf(e.tag)] = e; |
|
144 |
} |
|
145 |
} |
|
146 |
||
147 |
private static int indexOf(int tag) { |
|
148 |
if (tag >= InternalMin) { |
|
149 |
return tag - InternalMin + ExternalMax + 1; |
|
150 |
} else { |
|
151 |
assert tag <= ExternalMax; |
|
152 |
} |
|
153 |
return tag; |
|
154 |
} |
|
155 |
||
156 |
static JVM_CONSTANT get(int tag) { |
|
157 |
JVM_CONSTANT res = table[indexOf(tag)]; |
|
158 |
if (res != null) { |
|
159 |
return res; |
|
160 |
} |
|
161 |
throw new JVMCIError("Unknown JVM_CONSTANT tag %s", tag); |
|
162 |
} |
|
163 |
} |
|
164 |
||
165 |
public static JVM_CONSTANT getEnum(int tag) { |
|
166 |
return TagValueMap.get(tag); |
|
167 |
} |
|
168 |
} |
|
169 |
||
170 |
private static class LookupTypeCacheElement { |
|
171 |
int lastCpi = Integer.MIN_VALUE; |
|
172 |
JavaType javaType; |
|
173 |
||
174 |
LookupTypeCacheElement(int lastCpi, JavaType javaType) { |
|
175 |
super(); |
|
176 |
this.lastCpi = lastCpi; |
|
177 |
this.javaType = javaType; |
|
178 |
} |
|
179 |
} |
|
180 |
||
181 |
/** |
|
182 |
* Reference to the C++ ConstantPool object. |
|
183 |
*/ |
|
184 |
private final long metaspaceConstantPool; |
|
185 |
private volatile LookupTypeCacheElement lastLookupType; |
|
186 |
||
187 |
/** |
|
188 |
* Gets the JVMCI mirror from a HotSpot constant pool.The VM is responsible for ensuring that |
|
189 |
* the ConstantPool is kept alive for the duration of this call and the |
|
190 |
* {@link HotSpotJVMCIMetaAccessContext} keeps it alive after that. |
|
191 |
* |
|
192 |
* Called from the VM. |
|
193 |
* |
|
194 |
* @param metaspaceConstantPool a metaspace ConstantPool object |
|
195 |
* @return the {@link HotSpotConstantPool} corresponding to {@code metaspaceConstantPool} |
|
196 |
*/ |
|
197 |
@SuppressWarnings("unused") |
|
198 |
private static HotSpotConstantPool fromMetaspace(long metaspaceConstantPool) { |
|
199 |
HotSpotConstantPool cp = new HotSpotConstantPool(metaspaceConstantPool); |
|
200 |
runtime().metaAccessContext.add(cp); |
|
201 |
return cp; |
|
202 |
} |
|
203 |
||
204 |
private HotSpotConstantPool(long metaspaceConstantPool) { |
|
205 |
this.metaspaceConstantPool = metaspaceConstantPool; |
|
206 |
} |
|
207 |
||
208 |
/** |
|
209 |
* Gets the holder for this constant pool as {@link HotSpotResolvedObjectTypeImpl}. |
|
210 |
* |
|
211 |
* @return holder for this constant pool |
|
212 |
*/ |
|
213 |
private HotSpotResolvedObjectType getHolder() { |
|
214 |
return compilerToVM().getResolvedJavaType(this, config().constantPoolHolderOffset, false); |
|
215 |
} |
|
216 |
||
217 |
/** |
|
47668 | 218 |
* Converts a raw index from the bytecodes to a constant pool cache index by adding a |
43972 | 219 |
* {@link HotSpotVMConfig#constantPoolCpCacheIndexTag constant}. |
220 |
* |
|
221 |
* @param rawIndex index from the bytecode |
|
222 |
* @param opcode bytecode to convert the index for |
|
47668 | 223 |
* @return constant pool cache index |
43972 | 224 |
*/ |
47668 | 225 |
private static int rawIndexToConstantPoolCacheIndex(int rawIndex, int opcode) { |
43972 | 226 |
int index; |
227 |
if (opcode == Bytecodes.INVOKEDYNAMIC) { |
|
228 |
index = rawIndex; |
|
229 |
// See: ConstantPool::is_invokedynamic_index |
|
230 |
assert index < 0 : "not an invokedynamic constant pool index " + index; |
|
231 |
} else { |
|
232 |
assert opcode == Bytecodes.GETFIELD || opcode == Bytecodes.PUTFIELD || opcode == Bytecodes.GETSTATIC || opcode == Bytecodes.PUTSTATIC || opcode == Bytecodes.INVOKEINTERFACE || |
|
233 |
opcode == Bytecodes.INVOKEVIRTUAL || opcode == Bytecodes.INVOKESPECIAL || opcode == Bytecodes.INVOKESTATIC : "unexpected invoke opcode " + opcode; |
|
234 |
index = rawIndex + config().constantPoolCpCacheIndexTag; |
|
235 |
} |
|
236 |
return index; |
|
237 |
} |
|
238 |
||
239 |
/** |
|
240 |
* Decode a constant pool cache index to a constant pool index. |
|
241 |
* |
|
242 |
* See {@code ConstantPool::decode_cpcache_index}. |
|
243 |
* |
|
244 |
* @param index constant pool cache index |
|
245 |
* @return decoded index |
|
246 |
*/ |
|
247 |
private static int decodeConstantPoolCacheIndex(int index) { |
|
248 |
if (isInvokedynamicIndex(index)) { |
|
249 |
return decodeInvokedynamicIndex(index); |
|
250 |
} else { |
|
251 |
return index - config().constantPoolCpCacheIndexTag; |
|
252 |
} |
|
253 |
} |
|
254 |
||
255 |
/** |
|
256 |
* See {@code ConstantPool::is_invokedynamic_index}. |
|
257 |
*/ |
|
258 |
private static boolean isInvokedynamicIndex(int index) { |
|
259 |
return index < 0; |
|
260 |
} |
|
261 |
||
262 |
/** |
|
263 |
* See {@code ConstantPool::decode_invokedynamic_index}. |
|
264 |
*/ |
|
265 |
private static int decodeInvokedynamicIndex(int i) { |
|
266 |
assert isInvokedynamicIndex(i) : i; |
|
267 |
return ~i; |
|
268 |
} |
|
269 |
||
270 |
long getMetaspaceConstantPool() { |
|
271 |
return metaspaceConstantPool; |
|
272 |
} |
|
273 |
||
47668 | 274 |
@Override |
43972 | 275 |
public long getMetaspacePointer() { |
276 |
return getMetaspaceConstantPool(); |
|
277 |
} |
|
278 |
||
279 |
/** |
|
280 |
* Gets the constant pool tag at index {@code index}. |
|
281 |
* |
|
282 |
* @param index constant pool index |
|
283 |
* @return constant pool tag |
|
284 |
*/ |
|
285 |
private JVM_CONSTANT getTagAt(int index) { |
|
286 |
assert checkBounds(index); |
|
287 |
HotSpotVMConfig config = config(); |
|
288 |
final long metaspaceConstantPoolTags = UNSAFE.getAddress(getMetaspaceConstantPool() + config.constantPoolTagsOffset); |
|
289 |
final int tag = UNSAFE.getByteVolatile(null, metaspaceConstantPoolTags + config.arrayU1DataOffset + index); |
|
290 |
if (tag == 0) { |
|
291 |
return null; |
|
292 |
} |
|
293 |
return JVM_CONSTANT.getEnum(tag); |
|
294 |
} |
|
295 |
||
296 |
/** |
|
297 |
* Gets the constant pool entry at index {@code index}. |
|
298 |
* |
|
299 |
* @param index constant pool index |
|
300 |
* @return constant pool entry |
|
301 |
*/ |
|
302 |
long getEntryAt(int index) { |
|
303 |
assert checkBounds(index); |
|
304 |
int offset = index * runtime().getHostJVMCIBackend().getTarget().wordSize; |
|
305 |
return UNSAFE.getAddress(getMetaspaceConstantPool() + config().constantPoolSize + offset); |
|
306 |
} |
|
307 |
||
308 |
/** |
|
309 |
* Gets the integer constant pool entry at index {@code index}. |
|
310 |
* |
|
311 |
* @param index constant pool index |
|
312 |
* @return integer constant pool entry at index |
|
313 |
*/ |
|
314 |
private int getIntAt(int index) { |
|
315 |
assert checkTag(index, JVM_CONSTANT.Integer); |
|
316 |
int offset = index * runtime().getHostJVMCIBackend().getTarget().wordSize; |
|
317 |
return UNSAFE.getInt(getMetaspaceConstantPool() + config().constantPoolSize + offset); |
|
318 |
} |
|
319 |
||
320 |
/** |
|
321 |
* Gets the long constant pool entry at index {@code index}. |
|
322 |
* |
|
323 |
* @param index constant pool index |
|
324 |
* @return long constant pool entry |
|
325 |
*/ |
|
326 |
private long getLongAt(int index) { |
|
327 |
assert checkTag(index, JVM_CONSTANT.Long); |
|
328 |
int offset = index * runtime().getHostJVMCIBackend().getTarget().wordSize; |
|
329 |
return UNSAFE.getLong(getMetaspaceConstantPool() + config().constantPoolSize + offset); |
|
330 |
} |
|
331 |
||
332 |
/** |
|
333 |
* Gets the float constant pool entry at index {@code index}. |
|
334 |
* |
|
335 |
* @param index constant pool index |
|
336 |
* @return float constant pool entry |
|
337 |
*/ |
|
338 |
private float getFloatAt(int index) { |
|
339 |
assert checkTag(index, JVM_CONSTANT.Float); |
|
340 |
int offset = index * runtime().getHostJVMCIBackend().getTarget().wordSize; |
|
341 |
return UNSAFE.getFloat(getMetaspaceConstantPool() + config().constantPoolSize + offset); |
|
342 |
} |
|
343 |
||
344 |
/** |
|
345 |
* Gets the double constant pool entry at index {@code index}. |
|
346 |
* |
|
347 |
* @param index constant pool index |
|
348 |
* @return float constant pool entry |
|
349 |
*/ |
|
350 |
private double getDoubleAt(int index) { |
|
351 |
assert checkTag(index, JVM_CONSTANT.Double); |
|
352 |
int offset = index * runtime().getHostJVMCIBackend().getTarget().wordSize; |
|
353 |
return UNSAFE.getDouble(getMetaspaceConstantPool() + config().constantPoolSize + offset); |
|
354 |
} |
|
355 |
||
356 |
/** |
|
357 |
* Gets the {@code JVM_CONSTANT_NameAndType} constant pool entry at index {@code index}. |
|
358 |
* |
|
359 |
* @param index constant pool index |
|
360 |
* @return {@code JVM_CONSTANT_NameAndType} constant pool entry |
|
361 |
*/ |
|
362 |
private int getNameAndTypeAt(int index) { |
|
363 |
assert checkTag(index, JVM_CONSTANT.NameAndType); |
|
364 |
int offset = index * runtime().getHostJVMCIBackend().getTarget().wordSize; |
|
365 |
return UNSAFE.getInt(getMetaspaceConstantPool() + config().constantPoolSize + offset); |
|
366 |
} |
|
367 |
||
368 |
/** |
|
369 |
* Gets the {@code JVM_CONSTANT_NameAndType} reference index constant pool entry at index |
|
370 |
* {@code index}. |
|
371 |
* |
|
372 |
* @param index constant pool index |
|
373 |
* @return {@code JVM_CONSTANT_NameAndType} reference constant pool entry |
|
374 |
*/ |
|
375 |
private int getNameAndTypeRefIndexAt(int index) { |
|
376 |
return compilerToVM().lookupNameAndTypeRefIndexInPool(this, index); |
|
377 |
} |
|
378 |
||
379 |
/** |
|
380 |
* Gets the name of a {@code JVM_CONSTANT_NameAndType} constant pool entry referenced by another |
|
381 |
* entry denoted by {@code which}. |
|
382 |
* |
|
383 |
* @param which constant pool index or constant pool cache index |
|
384 |
* @return name as {@link String} |
|
385 |
*/ |
|
386 |
private String getNameOf(int which) { |
|
387 |
return compilerToVM().lookupNameInPool(this, which); |
|
388 |
} |
|
389 |
||
390 |
/** |
|
391 |
* Gets the name reference index of a {@code JVM_CONSTANT_NameAndType} constant pool entry at |
|
392 |
* index {@code index}. |
|
393 |
* |
|
394 |
* @param index constant pool index |
|
395 |
* @return name reference index |
|
396 |
*/ |
|
397 |
private int getNameRefIndexAt(int index) { |
|
398 |
final int refIndex = getNameAndTypeAt(index); |
|
399 |
// name ref index is in the low 16-bits. |
|
400 |
return refIndex & 0xFFFF; |
|
401 |
} |
|
402 |
||
403 |
/** |
|
404 |
* Gets the signature of a {@code JVM_CONSTANT_NameAndType} constant pool entry referenced by |
|
405 |
* another entry denoted by {@code which}. |
|
406 |
* |
|
407 |
* @param which constant pool index or constant pool cache index |
|
408 |
* @return signature as {@link String} |
|
409 |
*/ |
|
410 |
private String getSignatureOf(int which) { |
|
411 |
return compilerToVM().lookupSignatureInPool(this, which); |
|
412 |
} |
|
413 |
||
414 |
/** |
|
415 |
* Gets the signature reference index of a {@code JVM_CONSTANT_NameAndType} constant pool entry |
|
416 |
* at index {@code index}. |
|
417 |
* |
|
418 |
* @param index constant pool index |
|
419 |
* @return signature reference index |
|
420 |
*/ |
|
421 |
private int getSignatureRefIndexAt(int index) { |
|
422 |
final int refIndex = getNameAndTypeAt(index); |
|
423 |
// signature ref index is in the high 16-bits. |
|
424 |
return refIndex >>> 16; |
|
425 |
} |
|
426 |
||
427 |
/** |
|
428 |
* Gets the klass reference index constant pool entry at index {@code index}. |
|
429 |
* |
|
430 |
* @param index constant pool index |
|
431 |
* @return klass reference index |
|
432 |
*/ |
|
433 |
private int getKlassRefIndexAt(int index) { |
|
434 |
return compilerToVM().lookupKlassRefIndexInPool(this, index); |
|
435 |
} |
|
436 |
||
437 |
/** |
|
438 |
* Gets the uncached klass reference index constant pool entry at index {@code index}. See: |
|
439 |
* {@code ConstantPool::uncached_klass_ref_index_at}. |
|
440 |
* |
|
441 |
* @param index constant pool index |
|
442 |
* @return klass reference index |
|
443 |
*/ |
|
444 |
private int getUncachedKlassRefIndexAt(int index) { |
|
445 |
assert checkTagIsFieldOrMethod(index); |
|
446 |
int offset = index * runtime().getHostJVMCIBackend().getTarget().wordSize; |
|
447 |
final int refIndex = UNSAFE.getInt(getMetaspaceConstantPool() + config().constantPoolSize + offset); |
|
448 |
// klass ref index is in the low 16-bits. |
|
449 |
return refIndex & 0xFFFF; |
|
450 |
} |
|
451 |
||
452 |
/** |
|
453 |
* Checks that the constant pool index {@code index} is in the bounds of the constant pool. |
|
454 |
* |
|
455 |
* @param index constant pool index |
|
456 |
* @throws AssertionError if the check fails |
|
457 |
*/ |
|
458 |
private boolean checkBounds(int index) { |
|
459 |
assert 0 <= index && index < length() : "index " + index + " not between 0 and " + length(); |
|
460 |
return true; |
|
461 |
} |
|
462 |
||
463 |
/** |
|
464 |
* Checks that the constant pool tag at index {@code index} is equal to {@code tag}. |
|
465 |
* |
|
466 |
* @param index constant pool index |
|
467 |
* @param tag expected tag |
|
468 |
* @throws AssertionError if the check fails |
|
469 |
*/ |
|
470 |
private boolean checkTag(int index, JVM_CONSTANT tag) { |
|
471 |
final JVM_CONSTANT tagAt = getTagAt(index); |
|
472 |
assert tagAt == tag : "constant pool tag at index " + index + " is " + tagAt + " but expected " + tag; |
|
473 |
return true; |
|
474 |
} |
|
475 |
||
476 |
/** |
|
477 |
* Asserts that the constant pool tag at index {@code index} is a {@link JVM_CONSTANT#Fieldref}, |
|
478 |
* or a {@link JVM_CONSTANT#MethodRef}, or a {@link JVM_CONSTANT#InterfaceMethodref}. |
|
479 |
* |
|
480 |
* @param index constant pool index |
|
481 |
* @throws AssertionError if the check fails |
|
482 |
*/ |
|
483 |
private boolean checkTagIsFieldOrMethod(int index) { |
|
484 |
final JVM_CONSTANT tagAt = getTagAt(index); |
|
485 |
assert tagAt == JVM_CONSTANT.Fieldref || tagAt == JVM_CONSTANT.MethodRef || tagAt == JVM_CONSTANT.InterfaceMethodref : tagAt; |
|
486 |
return true; |
|
487 |
} |
|
488 |
||
489 |
@Override |
|
490 |
public int length() { |
|
491 |
return UNSAFE.getInt(getMetaspaceConstantPool() + config().constantPoolLengthOffset); |
|
492 |
} |
|
493 |
||
48826 | 494 |
public boolean hasDynamicConstant() { |
495 |
return (flags() & config().constantPoolHasDynamicConstant) != 0; |
|
496 |
} |
|
497 |
||
498 |
private int flags() { |
|
499 |
return UNSAFE.getInt(getMetaspaceConstantPool() + config().constantPoolFlagsOffset); |
|
500 |
} |
|
501 |
||
43972 | 502 |
@Override |
503 |
public Object lookupConstant(int cpi) { |
|
504 |
assert cpi != 0; |
|
505 |
final JVM_CONSTANT tag = getTagAt(cpi); |
|
506 |
switch (tag) { |
|
507 |
case Integer: |
|
508 |
return JavaConstant.forInt(getIntAt(cpi)); |
|
509 |
case Long: |
|
510 |
return JavaConstant.forLong(getLongAt(cpi)); |
|
511 |
case Float: |
|
512 |
return JavaConstant.forFloat(getFloatAt(cpi)); |
|
513 |
case Double: |
|
514 |
return JavaConstant.forDouble(getDoubleAt(cpi)); |
|
515 |
case Class: |
|
516 |
case UnresolvedClass: |
|
517 |
case UnresolvedClassInError: |
|
518 |
final int opcode = -1; // opcode is not used |
|
519 |
return lookupType(cpi, opcode); |
|
520 |
case String: |
|
521 |
/* |
|
522 |
* Normally, we would expect a String here, but anonymous classes can have |
|
523 |
* "pseudo strings" (arbitrary live objects) patched into a String entry. Such |
|
524 |
* entries do not have a symbol in the constant pool slot. |
|
525 |
*/ |
|
526 |
Object string = compilerToVM().resolvePossiblyCachedConstantInPool(this, cpi); |
|
527 |
return HotSpotObjectConstantImpl.forObject(string); |
|
528 |
case MethodHandle: |
|
529 |
case MethodHandleInError: |
|
530 |
case MethodType: |
|
531 |
case MethodTypeInError: |
|
532 |
Object obj = compilerToVM().resolveConstantInPool(this, cpi); |
|
533 |
return HotSpotObjectConstantImpl.forObject(obj); |
|
534 |
default: |
|
535 |
throw new JVMCIError("Unknown constant pool tag %s", tag); |
|
536 |
} |
|
537 |
} |
|
538 |
||
539 |
@Override |
|
540 |
public String lookupUtf8(int cpi) { |
|
541 |
assert checkTag(cpi, JVM_CONSTANT.Utf8); |
|
542 |
return compilerToVM().getSymbol(getEntryAt(cpi)); |
|
543 |
} |
|
544 |
||
545 |
@Override |
|
546 |
public Signature lookupSignature(int cpi) { |
|
547 |
return new HotSpotSignature(runtime(), lookupUtf8(cpi)); |
|
548 |
} |
|
549 |
||
550 |
@Override |
|
551 |
public JavaConstant lookupAppendix(int cpi, int opcode) { |
|
552 |
assert Bytecodes.isInvoke(opcode); |
|
47668 | 553 |
final int index = rawIndexToConstantPoolCacheIndex(cpi, opcode); |
43972 | 554 |
Object appendix = compilerToVM().lookupAppendixInPool(this, index); |
555 |
if (appendix == null) { |
|
556 |
return null; |
|
557 |
} else { |
|
558 |
return HotSpotObjectConstantImpl.forObject(appendix); |
|
559 |
} |
|
560 |
} |
|
561 |
||
562 |
/** |
|
563 |
* Gets a {@link JavaType} corresponding a given resolved or unresolved type. |
|
564 |
* |
|
565 |
* @param type either a ResolvedJavaType or a String naming a unresolved type. |
|
566 |
*/ |
|
567 |
private static JavaType getJavaType(final Object type) { |
|
568 |
if (type instanceof String) { |
|
569 |
String name = (String) type; |
|
570 |
return HotSpotUnresolvedJavaType.create(runtime(), "L" + name + ";"); |
|
571 |
} else { |
|
572 |
return (JavaType) type; |
|
573 |
} |
|
574 |
} |
|
575 |
||
576 |
@Override |
|
577 |
public JavaMethod lookupMethod(int cpi, int opcode) { |
|
47668 | 578 |
final int index = rawIndexToConstantPoolCacheIndex(cpi, opcode); |
43972 | 579 |
final HotSpotResolvedJavaMethod method = compilerToVM().lookupMethodInPool(this, index, (byte) opcode); |
580 |
if (method != null) { |
|
581 |
return method; |
|
582 |
} else { |
|
583 |
// Get the method's name and signature. |
|
584 |
String name = getNameOf(index); |
|
585 |
HotSpotSignature signature = new HotSpotSignature(runtime(), getSignatureOf(index)); |
|
586 |
if (opcode == Bytecodes.INVOKEDYNAMIC) { |
|
587 |
HotSpotResolvedObjectType holder = HotSpotResolvedObjectTypeImpl.fromObjectClass(MethodHandle.class); |
|
588 |
return new HotSpotMethodUnresolved(name, signature, holder); |
|
589 |
} else { |
|
590 |
final int klassIndex = getKlassRefIndexAt(index); |
|
591 |
final Object type = compilerToVM().lookupKlassInPool(this, klassIndex); |
|
592 |
JavaType holder = getJavaType(type); |
|
593 |
return new HotSpotMethodUnresolved(name, signature, holder); |
|
594 |
} |
|
595 |
} |
|
596 |
} |
|
597 |
||
598 |
@Override |
|
599 |
public JavaType lookupType(int cpi, int opcode) { |
|
600 |
final LookupTypeCacheElement elem = this.lastLookupType; |
|
601 |
if (elem != null && elem.lastCpi == cpi) { |
|
602 |
return elem.javaType; |
|
603 |
} else { |
|
604 |
final Object type = compilerToVM().lookupKlassInPool(this, cpi); |
|
605 |
JavaType result = getJavaType(type); |
|
606 |
if (result instanceof ResolvedJavaType) { |
|
607 |
this.lastLookupType = new LookupTypeCacheElement(cpi, result); |
|
608 |
} |
|
609 |
return result; |
|
610 |
} |
|
611 |
} |
|
612 |
||
613 |
@Override |
|
614 |
public JavaField lookupField(int cpi, ResolvedJavaMethod method, int opcode) { |
|
47668 | 615 |
final int index = rawIndexToConstantPoolCacheIndex(cpi, opcode); |
43972 | 616 |
final int nameAndTypeIndex = getNameAndTypeRefIndexAt(index); |
617 |
final int typeIndex = getSignatureRefIndexAt(nameAndTypeIndex); |
|
618 |
String typeName = lookupUtf8(typeIndex); |
|
619 |
JavaType type = runtime().lookupType(typeName, getHolder(), false); |
|
620 |
||
621 |
final int holderIndex = getKlassRefIndexAt(index); |
|
622 |
JavaType holder = lookupType(holderIndex, opcode); |
|
623 |
||
624 |
if (holder instanceof HotSpotResolvedObjectTypeImpl) { |
|
625 |
int[] info = new int[3]; |
|
626 |
HotSpotResolvedObjectTypeImpl resolvedHolder; |
|
627 |
try { |
|
628 |
resolvedHolder = compilerToVM().resolveFieldInPool(this, index, (HotSpotResolvedJavaMethodImpl) method, (byte) opcode, info); |
|
629 |
} catch (Throwable t) { |
|
630 |
/* |
|
631 |
* If there was an exception resolving the field we give up and return an unresolved |
|
632 |
* field. |
|
633 |
*/ |
|
634 |
return new HotSpotUnresolvedField(holder, lookupUtf8(getNameRefIndexAt(nameAndTypeIndex)), type); |
|
635 |
} |
|
636 |
final int flags = info[0]; |
|
637 |
final int offset = info[1]; |
|
638 |
final int fieldIndex = info[2]; |
|
639 |
HotSpotResolvedJavaField result = resolvedHolder.createField(type, offset, flags, fieldIndex); |
|
640 |
return result; |
|
641 |
} else { |
|
642 |
return new HotSpotUnresolvedField(holder, lookupUtf8(getNameRefIndexAt(nameAndTypeIndex)), type); |
|
643 |
} |
|
644 |
} |
|
645 |
||
47668 | 646 |
/* |
647 |
* Converts a raw index from the bytecodes to a constant pool index |
|
648 |
* (not a cache index). |
|
649 |
* |
|
650 |
* @param rawIndex index from the bytecode |
|
651 |
* @param opcode bytecode to convert the index for |
|
652 |
* @return constant pool index |
|
653 |
*/ |
|
654 |
public int rawIndexToConstantPoolIndex(int index, int opcode) { |
|
655 |
if (isInvokedynamicIndex(index)) { |
|
656 |
assert opcode == Bytecodes.INVOKEDYNAMIC; |
|
657 |
index = decodeInvokedynamicIndex(index) + config().constantPoolCpCacheIndexTag; |
|
658 |
} else { |
|
659 |
assert opcode != Bytecodes.INVOKEDYNAMIC; |
|
660 |
index = rawIndexToConstantPoolCacheIndex(index, opcode); |
|
661 |
} |
|
662 |
return compilerToVM().constantPoolRemapInstructionOperandFromCache(this, index); |
|
663 |
} |
|
664 |
||
43972 | 665 |
@Override |
49467
74db2b7cec75
8146201: [AOT] Class static initializers that are not pure should not be executed during static compilation
dlong
parents:
48826
diff
changeset
|
666 |
public void loadReferencedType(int cpi, int opcode) { |
74db2b7cec75
8146201: [AOT] Class static initializers that are not pure should not be executed during static compilation
dlong
parents:
48826
diff
changeset
|
667 |
loadReferencedType(cpi, opcode, true /* initialize */); |
74db2b7cec75
8146201: [AOT] Class static initializers that are not pure should not be executed during static compilation
dlong
parents:
48826
diff
changeset
|
668 |
} |
74db2b7cec75
8146201: [AOT] Class static initializers that are not pure should not be executed during static compilation
dlong
parents:
48826
diff
changeset
|
669 |
|
43972 | 670 |
@SuppressWarnings("fallthrough") |
49467
74db2b7cec75
8146201: [AOT] Class static initializers that are not pure should not be executed during static compilation
dlong
parents:
48826
diff
changeset
|
671 |
public void loadReferencedType(int cpi, int opcode, boolean initialize) { |
43972 | 672 |
int index; |
673 |
switch (opcode) { |
|
674 |
case Bytecodes.CHECKCAST: |
|
675 |
case Bytecodes.INSTANCEOF: |
|
676 |
case Bytecodes.NEW: |
|
677 |
case Bytecodes.ANEWARRAY: |
|
678 |
case Bytecodes.MULTIANEWARRAY: |
|
679 |
case Bytecodes.LDC: |
|
680 |
case Bytecodes.LDC_W: |
|
681 |
case Bytecodes.LDC2_W: |
|
682 |
index = cpi; |
|
683 |
break; |
|
684 |
case Bytecodes.INVOKEDYNAMIC: { |
|
685 |
// invokedynamic instructions point to a constant pool cache entry. |
|
686 |
index = decodeConstantPoolCacheIndex(cpi) + config().constantPoolCpCacheIndexTag; |
|
687 |
index = compilerToVM().constantPoolRemapInstructionOperandFromCache(this, index); |
|
688 |
break; |
|
689 |
} |
|
690 |
case Bytecodes.GETSTATIC: |
|
691 |
case Bytecodes.PUTSTATIC: |
|
692 |
case Bytecodes.GETFIELD: |
|
693 |
case Bytecodes.PUTFIELD: |
|
694 |
case Bytecodes.INVOKEVIRTUAL: |
|
695 |
case Bytecodes.INVOKESPECIAL: |
|
696 |
case Bytecodes.INVOKESTATIC: |
|
697 |
case Bytecodes.INVOKEINTERFACE: { |
|
698 |
// invoke and field instructions point to a constant pool cache entry. |
|
47668 | 699 |
index = rawIndexToConstantPoolCacheIndex(cpi, opcode); |
43972 | 700 |
index = compilerToVM().constantPoolRemapInstructionOperandFromCache(this, index); |
701 |
break; |
|
702 |
} |
|
703 |
default: |
|
704 |
throw JVMCIError.shouldNotReachHere("Unexpected opcode " + opcode); |
|
705 |
} |
|
706 |
||
707 |
final JVM_CONSTANT tag = getTagAt(index); |
|
708 |
if (tag == null) { |
|
709 |
assert getTagAt(index - 1) == JVM_CONSTANT.Double || getTagAt(index - 1) == JVM_CONSTANT.Long; |
|
710 |
return; |
|
711 |
} |
|
712 |
switch (tag) { |
|
713 |
case MethodRef: |
|
714 |
case Fieldref: |
|
715 |
case InterfaceMethodref: |
|
716 |
index = getUncachedKlassRefIndexAt(index); |
|
717 |
// Read the tag only once because it could change between multiple reads. |
|
718 |
final JVM_CONSTANT klassTag = getTagAt(index); |
|
719 |
assert klassTag == JVM_CONSTANT.Class || klassTag == JVM_CONSTANT.UnresolvedClass || klassTag == JVM_CONSTANT.UnresolvedClassInError : klassTag; |
|
720 |
// fall through |
|
721 |
case Class: |
|
722 |
case UnresolvedClass: |
|
723 |
case UnresolvedClassInError: |
|
724 |
final HotSpotResolvedObjectTypeImpl type = compilerToVM().resolveTypeInPool(this, index); |
|
49467
74db2b7cec75
8146201: [AOT] Class static initializers that are not pure should not be executed during static compilation
dlong
parents:
48826
diff
changeset
|
725 |
if (initialize) { |
74db2b7cec75
8146201: [AOT] Class static initializers that are not pure should not be executed during static compilation
dlong
parents:
48826
diff
changeset
|
726 |
Class<?> klass = type.mirror(); |
74db2b7cec75
8146201: [AOT] Class static initializers that are not pure should not be executed during static compilation
dlong
parents:
48826
diff
changeset
|
727 |
if (!klass.isPrimitive() && !klass.isArray()) { |
74db2b7cec75
8146201: [AOT] Class static initializers that are not pure should not be executed during static compilation
dlong
parents:
48826
diff
changeset
|
728 |
UNSAFE.ensureClassInitialized(klass); |
74db2b7cec75
8146201: [AOT] Class static initializers that are not pure should not be executed during static compilation
dlong
parents:
48826
diff
changeset
|
729 |
} |
43972 | 730 |
} |
731 |
if (tag == JVM_CONSTANT.MethodRef) { |
|
732 |
if (Bytecodes.isInvokeHandleAlias(opcode) && isSignaturePolymorphicHolder(type)) { |
|
47668 | 733 |
final int methodRefCacheIndex = rawIndexToConstantPoolCacheIndex(cpi, opcode); |
43972 | 734 |
assert checkTag(compilerToVM().constantPoolRemapInstructionOperandFromCache(this, methodRefCacheIndex), JVM_CONSTANT.MethodRef); |
735 |
compilerToVM().resolveInvokeHandleInPool(this, methodRefCacheIndex); |
|
736 |
} |
|
737 |
} |
|
738 |
||
739 |
break; |
|
740 |
case InvokeDynamic: |
|
741 |
if (isInvokedynamicIndex(cpi)) { |
|
742 |
compilerToVM().resolveInvokeDynamicInPool(this, cpi); |
|
743 |
} |
|
744 |
break; |
|
745 |
default: |
|
746 |
// nothing |
|
747 |
break; |
|
748 |
} |
|
749 |
||
750 |
} |
|
751 |
||
752 |
// Lazily initialized. |
|
753 |
private static String[] signaturePolymorphicHolders; |
|
754 |
||
755 |
/** |
|
756 |
* Determines if {@code type} contains signature polymorphic methods. |
|
757 |
*/ |
|
758 |
static boolean isSignaturePolymorphicHolder(final ResolvedJavaType type) { |
|
759 |
String name = type.getName(); |
|
760 |
if (signaturePolymorphicHolders == null) { |
|
761 |
signaturePolymorphicHolders = compilerToVM().getSignaturePolymorphicHolders(); |
|
762 |
} |
|
763 |
for (String holder : signaturePolymorphicHolders) { |
|
764 |
if (name.equals(holder)) { |
|
765 |
return true; |
|
766 |
} |
|
767 |
} |
|
768 |
return false; |
|
769 |
} |
|
770 |
||
47668 | 771 |
/** |
772 |
* Check for a resolved dynamic adapter method at the specified index, |
|
773 |
* resulting from either a resolved invokedynamic or invokevirtual on a signature polymorphic |
|
774 |
* MethodHandle method (HotSpot invokehandle). |
|
775 |
* |
|
776 |
* @param cpi the constant pool index |
|
777 |
* @param opcode the opcode of the instruction for which the lookup is being performed |
|
778 |
* @return {@code true} if a signature polymorphic method reference was found, otherwise {@code false} |
|
779 |
*/ |
|
780 |
public boolean isResolvedDynamicInvoke(int cpi, int opcode) { |
|
781 |
if (Bytecodes.isInvokeHandleAlias(opcode)) { |
|
782 |
final int methodRefCacheIndex = rawIndexToConstantPoolCacheIndex(cpi, opcode); |
|
783 |
assert checkTag(compilerToVM().constantPoolRemapInstructionOperandFromCache(this, methodRefCacheIndex), JVM_CONSTANT.MethodRef); |
|
784 |
int op = compilerToVM().isResolvedInvokeHandleInPool(this, methodRefCacheIndex); |
|
785 |
return op == opcode; |
|
786 |
} |
|
787 |
return false; |
|
788 |
} |
|
789 |
||
43972 | 790 |
@Override |
791 |
public String toString() { |
|
792 |
HotSpotResolvedObjectType holder = getHolder(); |
|
793 |
return "HotSpotConstantPool<" + holder.toJavaName() + ">"; |
|
794 |
} |
|
795 |
} |