comparison jvmci/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotResolvedJavaMethodImpl.java @ 22672:1bbd4a7c274b

Rename jdk.internal.jvmci to jdk.vm.ci
author Tom Rodriguez <tom.rodriguez@oracle.com>
date Thu, 08 Oct 2015 17:28:41 -0700
parents jvmci/jdk.internal.jvmci.hotspot/src/jdk/internal/jvmci/hotspot/HotSpotResolvedJavaMethodImpl.java@9f24bf92ee94
children ce4bec6db0b2
comparison
equal deleted inserted replaced
22671:97f30e4d0e95 22672:1bbd4a7c274b
1 /*
2 * Copyright (c) 2011, 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 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.HotSpotResolvedJavaMethod.Options.UseProfilingInformation;
28 import static jdk.vm.ci.hotspot.HotSpotVMConfig.config;
29 import static jdk.vm.ci.hotspot.UnsafeAccess.UNSAFE;
30
31 import java.lang.annotation.Annotation;
32 import java.lang.reflect.Constructor;
33 import java.lang.reflect.InvocationTargetException;
34 import java.lang.reflect.Member;
35 import java.lang.reflect.Method;
36 import java.lang.reflect.Modifier;
37 import java.lang.reflect.Type;
38 import java.util.HashMap;
39 import java.util.Map;
40
41 import jdk.vm.ci.common.JVMCIError;
42 import jdk.vm.ci.meta.Constant;
43 import jdk.vm.ci.meta.ConstantPool;
44 import jdk.vm.ci.meta.DefaultProfilingInfo;
45 import jdk.vm.ci.meta.ExceptionHandler;
46 import jdk.vm.ci.meta.JavaConstant;
47 import jdk.vm.ci.meta.JavaMethod;
48 import jdk.vm.ci.meta.JavaType;
49 import jdk.vm.ci.meta.LineNumberTable;
50 import jdk.vm.ci.meta.LineNumberTableImpl;
51 import jdk.vm.ci.meta.Local;
52 import jdk.vm.ci.meta.LocalImpl;
53 import jdk.vm.ci.meta.LocalVariableTable;
54 import jdk.vm.ci.meta.LocalVariableTableImpl;
55 import jdk.vm.ci.meta.ModifiersProvider;
56 import jdk.vm.ci.meta.ProfilingInfo;
57 import jdk.vm.ci.meta.ResolvedJavaMethod;
58 import jdk.vm.ci.meta.ResolvedJavaType;
59 import jdk.vm.ci.meta.Signature;
60 import jdk.vm.ci.meta.SpeculationLog;
61 import jdk.vm.ci.meta.TriState;
62
63 /**
64 * Implementation of {@link JavaMethod} for resolved HotSpot methods.
65 */
66 final class HotSpotResolvedJavaMethodImpl extends HotSpotMethod implements HotSpotResolvedJavaMethod, HotSpotProxified, MetaspaceWrapperObject {
67
68 /**
69 * Reference to metaspace Method object.
70 */
71 private final long metaspaceMethod;
72
73 private final HotSpotResolvedObjectTypeImpl holder;
74 private final HotSpotConstantPool constantPool;
75 private final HotSpotSignature signature;
76 private HotSpotMethodData methodData;
77 private byte[] code;
78 private Member toJavaCache;
79
80 /**
81 * Gets the holder of a HotSpot metaspace method native object.
82 *
83 * @param metaspaceMethod a metaspace Method object
84 * @return the {@link ResolvedJavaType} corresponding to the holder of the
85 * {@code metaspaceMethod}
86 */
87 private static HotSpotResolvedObjectTypeImpl getHolder(long metaspaceMethod) {
88 HotSpotVMConfig config = config();
89 final long metaspaceConstMethod = UNSAFE.getAddress(metaspaceMethod + config.methodConstMethodOffset);
90 final long metaspaceConstantPool = UNSAFE.getAddress(metaspaceConstMethod + config.constMethodConstantsOffset);
91 return compilerToVM().getResolvedJavaType(null, metaspaceConstantPool + config.constantPoolHolderOffset, false);
92 }
93
94 /**
95 * Gets the JVMCI mirror from a HotSpot method. The VM is responsible for ensuring that the
96 * Method* is kept alive for the duration of this call and the
97 * {@link HotSpotJVMCIMetaAccessContext} keeps it alive after that.
98 *
99 * Called from the VM.
100 *
101 * @param metaspaceMethod a metaspace Method object
102 * @return the {@link ResolvedJavaMethod} corresponding to {@code metaspaceMethod}
103 */
104 @SuppressWarnings("unused")
105 private static HotSpotResolvedJavaMethod fromMetaspace(long metaspaceMethod) {
106 HotSpotResolvedObjectTypeImpl holder = getHolder(metaspaceMethod);
107 return holder.createMethod(metaspaceMethod);
108 }
109
110 HotSpotResolvedJavaMethodImpl(HotSpotResolvedObjectTypeImpl holder, long metaspaceMethod) {
111 // It would be too much work to get the method name here so we fill it in later.
112 super(null);
113 this.metaspaceMethod = metaspaceMethod;
114 this.holder = holder;
115
116 HotSpotVMConfig config = config();
117 final long constMethod = getConstMethod();
118
119 /*
120 * Get the constant pool from the metaspace method. Some methods (e.g. intrinsics for
121 * signature-polymorphic method handle methods) have their own constant pool instead of the
122 * one from their holder.
123 */
124 final long metaspaceConstantPool = UNSAFE.getAddress(constMethod + config.constMethodConstantsOffset);
125 if (metaspaceConstantPool == holder.getConstantPool().getMetaspaceConstantPool()) {
126 this.constantPool = holder.getConstantPool();
127 } else {
128 this.constantPool = compilerToVM().getConstantPool(null, constMethod + config.constMethodConstantsOffset);
129 }
130
131 final int nameIndex = UNSAFE.getChar(constMethod + config.constMethodNameIndexOffset);
132 this.name = constantPool.lookupUtf8(nameIndex);
133
134 final int signatureIndex = UNSAFE.getChar(constMethod + config.constMethodSignatureIndexOffset);
135 this.signature = (HotSpotSignature) constantPool.lookupSignature(signatureIndex);
136 }
137
138 /**
139 * Returns a pointer to this method's constant method data structure (
140 * {@code Method::_constMethod}). This pointer isn't wrapped since it should be safe to use it
141 * within the context of this HotSpotResolvedJavaMethodImpl since the Method* and ConstMethod*
142 * are kept alive as a pair.
143 *
144 * @return pointer to this method's ConstMethod
145 */
146 private long getConstMethod() {
147 assert metaspaceMethod != 0;
148 return UNSAFE.getAddress(metaspaceMethod + config().methodConstMethodOffset);
149 }
150
151 @Override
152 public boolean equals(Object obj) {
153 if (this == obj) {
154 return true;
155 }
156 if (obj instanceof HotSpotResolvedJavaMethodImpl) {
157 HotSpotResolvedJavaMethodImpl that = (HotSpotResolvedJavaMethodImpl) obj;
158 return that.metaspaceMethod == metaspaceMethod;
159 }
160 return false;
161 }
162
163 @Override
164 public int hashCode() {
165 return (int) metaspaceMethod;
166 }
167
168 /**
169 * Returns this method's flags ({@code Method::_flags}).
170 *
171 * @return flags of this method
172 */
173 private int getFlags() {
174 return UNSAFE.getByte(metaspaceMethod + config().methodFlagsOffset);
175 }
176
177 /**
178 * Returns this method's constant method flags ({@code ConstMethod::_flags}).
179 *
180 * @return flags of this method's ConstMethod
181 */
182 private int getConstMethodFlags() {
183 return UNSAFE.getChar(getConstMethod() + config().constMethodFlagsOffset);
184 }
185
186 @Override
187 public HotSpotResolvedObjectTypeImpl getDeclaringClass() {
188 return holder;
189 }
190
191 /**
192 * Gets the address of the C++ Method object for this method.
193 */
194 public Constant getMetaspaceMethodConstant() {
195 return HotSpotMetaspaceConstantImpl.forMetaspaceObject(this, false);
196 }
197
198 public long getMetaspacePointer() {
199 return metaspaceMethod;
200 }
201
202 @Override
203 public Constant getEncoding() {
204 return getMetaspaceMethodConstant();
205 }
206
207 /**
208 * Gets the complete set of modifiers for this method which includes the JVM specification
209 * modifiers as well as the HotSpot internal modifiers.
210 */
211 public int getAllModifiers() {
212 return UNSAFE.getInt(metaspaceMethod + config().methodAccessFlagsOffset);
213 }
214
215 @Override
216 public int getModifiers() {
217 return getAllModifiers() & ModifiersProvider.jvmMethodModifiers();
218 }
219
220 @Override
221 public boolean canBeStaticallyBound() {
222 return (isFinal() || isPrivate() || isStatic() || holder.isLeaf()) && isConcrete();
223 }
224
225 @Override
226 public byte[] getCode() {
227 if (getCodeSize() == 0) {
228 return null;
229 }
230 if (code == null && holder.isLinked()) {
231 code = compilerToVM().getBytecode(this);
232 assert code.length == getCodeSize() : "expected: " + getCodeSize() + ", actual: " + code.length;
233 }
234 return code;
235 }
236
237 @Override
238 public int getCodeSize() {
239 return UNSAFE.getChar(getConstMethod() + config().constMethodCodeSizeOffset);
240 }
241
242 @Override
243 public ExceptionHandler[] getExceptionHandlers() {
244 final boolean hasExceptionTable = (getConstMethodFlags() & config().constMethodHasExceptionTable) != 0;
245 if (!hasExceptionTable) {
246 return new ExceptionHandler[0];
247 }
248
249 HotSpotVMConfig config = config();
250 final int exceptionTableLength = compilerToVM().getExceptionTableLength(this);
251 ExceptionHandler[] handlers = new ExceptionHandler[exceptionTableLength];
252 long exceptionTableElement = compilerToVM().getExceptionTableStart(this);
253
254 for (int i = 0; i < exceptionTableLength; i++) {
255 final int startPc = UNSAFE.getChar(exceptionTableElement + config.exceptionTableElementStartPcOffset);
256 final int endPc = UNSAFE.getChar(exceptionTableElement + config.exceptionTableElementEndPcOffset);
257 final int handlerPc = UNSAFE.getChar(exceptionTableElement + config.exceptionTableElementHandlerPcOffset);
258 int catchTypeIndex = UNSAFE.getChar(exceptionTableElement + config.exceptionTableElementCatchTypeIndexOffset);
259
260 JavaType catchType;
261 if (catchTypeIndex == 0) {
262 catchType = null;
263 } else {
264 final int opcode = -1; // opcode is not used
265 catchType = constantPool.lookupType(catchTypeIndex, opcode);
266
267 // Check for Throwable which catches everything.
268 if (catchType instanceof HotSpotResolvedObjectTypeImpl) {
269 HotSpotResolvedObjectTypeImpl resolvedType = (HotSpotResolvedObjectTypeImpl) catchType;
270 if (resolvedType.mirror() == Throwable.class) {
271 catchTypeIndex = 0;
272 catchType = null;
273 }
274 }
275 }
276 handlers[i] = new ExceptionHandler(startPc, endPc, handlerPc, catchTypeIndex, catchType);
277
278 // Go to the next ExceptionTableElement
279 exceptionTableElement += config.exceptionTableElementSize;
280 }
281
282 return handlers;
283 }
284
285 /**
286 * Returns true if this method has a {@code CallerSensitive} annotation.
287 *
288 * @return true if CallerSensitive annotation present, false otherwise
289 */
290 public boolean isCallerSensitive() {
291 return (getFlags() & config().methodFlagsCallerSensitive) != 0;
292 }
293
294 /**
295 * Returns true if this method has a {@code ForceInline} annotation.
296 *
297 * @return true if ForceInline annotation present, false otherwise
298 */
299 public boolean isForceInline() {
300 return (getFlags() & config().methodFlagsForceInline) != 0;
301 }
302
303 /**
304 * Returns true if this method has a {@code DontInline} annotation.
305 *
306 * @return true if DontInline annotation present, false otherwise
307 */
308 public boolean isDontInline() {
309 return (getFlags() & config().methodFlagsDontInline) != 0;
310 }
311
312 /**
313 * Manually adds a DontInline annotation to this method.
314 */
315 public void setNotInlineable() {
316 compilerToVM().doNotInlineOrCompile(this);
317 }
318
319 /**
320 * Returns true if this method is one of the special methods that is ignored by security stack
321 * walks.
322 *
323 * @return true if special method ignored by security stack walks, false otherwise
324 */
325 public boolean ignoredBySecurityStackWalk() {
326 return compilerToVM().methodIsIgnoredBySecurityStackWalk(this);
327 }
328
329 @Override
330 public boolean isClassInitializer() {
331 return "<clinit>".equals(name) && isStatic();
332 }
333
334 @Override
335 public boolean isConstructor() {
336 return "<init>".equals(name) && !isStatic();
337 }
338
339 @Override
340 public int getMaxLocals() {
341 if (isAbstract() || isNative()) {
342 return 0;
343 }
344 HotSpotVMConfig config = config();
345 return UNSAFE.getChar(getConstMethod() + config.methodMaxLocalsOffset);
346 }
347
348 @Override
349 public int getMaxStackSize() {
350 if (isAbstract() || isNative()) {
351 return 0;
352 }
353 HotSpotVMConfig config = config();
354 return config.extraStackEntries + UNSAFE.getChar(getConstMethod() + config.constMethodMaxStackOffset);
355 }
356
357 @Override
358 public StackTraceElement asStackTraceElement(int bci) {
359 if (bci < 0 || bci >= getCodeSize()) {
360 // HotSpot code can only construct stack trace elements for valid bcis
361 StackTraceElement ste = compilerToVM().getStackTraceElement(this, 0);
362 return new StackTraceElement(ste.getClassName(), ste.getMethodName(), ste.getFileName(), -1);
363 }
364 return compilerToVM().getStackTraceElement(this, bci);
365 }
366
367 public ResolvedJavaMethod uniqueConcreteMethod(HotSpotResolvedObjectType receiver) {
368 if (receiver.isInterface()) {
369 // Cannot trust interfaces. Because of:
370 // interface I { void foo(); }
371 // class A { public void foo() {} }
372 // class B extends A implements I { }
373 // class C extends B { public void foo() { } }
374 // class D extends B { }
375 // Would lead to identify C.foo() as the unique concrete method for I.foo() without
376 // seeing A.foo().
377 return null;
378 }
379 if (this.isDefault()) {
380 // CHA for default methods doesn't work and may crash the VM
381 return null;
382 }
383 return compilerToVM().findUniqueConcreteMethod(((HotSpotResolvedObjectTypeImpl) receiver), this);
384 }
385
386 @Override
387 public HotSpotSignature getSignature() {
388 return signature;
389 }
390
391 /**
392 * Gets the value of {@code Method::_code}.
393 *
394 * @return the value of {@code Method::_code}
395 */
396 private long getCompiledCode() {
397 HotSpotVMConfig config = config();
398 return UNSAFE.getAddress(metaspaceMethod + config.methodCodeOffset);
399 }
400
401 /**
402 * Returns whether this method has compiled code.
403 *
404 * @return true if this method has compiled code, false otherwise
405 */
406 public boolean hasCompiledCode() {
407 return getCompiledCode() != 0L;
408 }
409
410 /**
411 * @param level
412 * @return true if the currently installed code was generated at {@code level}.
413 */
414 public boolean hasCompiledCodeAtLevel(int level) {
415 long compiledCode = getCompiledCode();
416 if (compiledCode != 0) {
417 return UNSAFE.getInt(compiledCode + config().nmethodCompLevelOffset) == level;
418 }
419 return false;
420 }
421
422 private static final String TraceMethodDataFilter = System.getProperty("jvmci.traceMethodDataFilter");
423
424 @Override
425 public ProfilingInfo getProfilingInfo(boolean includeNormal, boolean includeOSR) {
426 ProfilingInfo info;
427
428 if (UseProfilingInformation.getValue() && methodData == null) {
429 long metaspaceMethodData = UNSAFE.getAddress(metaspaceMethod + config().methodDataOffset);
430 if (metaspaceMethodData != 0) {
431 methodData = new HotSpotMethodData(metaspaceMethodData, this);
432 if (TraceMethodDataFilter != null && this.format("%H.%n").contains(TraceMethodDataFilter)) {
433 System.out.println("Raw method data for " + this.format("%H.%n(%p)") + ":");
434 System.out.println(methodData.toString());
435 }
436 }
437 }
438
439 if (methodData == null || (!methodData.hasNormalData() && !methodData.hasExtraData())) {
440 // Be optimistic and return false for exceptionSeen. A methodDataOop is allocated in
441 // case of a deoptimization.
442 info = DefaultProfilingInfo.get(TriState.FALSE);
443 } else {
444 info = new HotSpotProfilingInfo(methodData, this, includeNormal, includeOSR);
445 }
446 return info;
447 }
448
449 @Override
450 public void reprofile() {
451 compilerToVM().reprofile(this);
452 }
453
454 @Override
455 public ConstantPool getConstantPool() {
456 return constantPool;
457 }
458
459 @Override
460 public Annotation[][] getParameterAnnotations() {
461 if (isConstructor()) {
462 Constructor<?> javaConstructor = toJavaConstructor();
463 return javaConstructor == null ? null : javaConstructor.getParameterAnnotations();
464 }
465 Method javaMethod = toJava();
466 return javaMethod == null ? null : javaMethod.getParameterAnnotations();
467 }
468
469 @Override
470 public Annotation[] getAnnotations() {
471 if (isConstructor()) {
472 Constructor<?> javaConstructor = toJavaConstructor();
473 return javaConstructor == null ? new Annotation[0] : javaConstructor.getAnnotations();
474 }
475 Method javaMethod = toJava();
476 return javaMethod == null ? new Annotation[0] : javaMethod.getAnnotations();
477 }
478
479 @Override
480 public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
481 if (isConstructor()) {
482 Constructor<?> javaConstructor = toJavaConstructor();
483 return javaConstructor == null ? null : javaConstructor.getAnnotation(annotationClass);
484 }
485 Method javaMethod = toJava();
486 return javaMethod == null ? null : javaMethod.getAnnotation(annotationClass);
487 }
488
489 public boolean isDefault() {
490 if (isConstructor()) {
491 return false;
492 }
493 // Copied from java.lang.Method.isDefault()
494 int mask = Modifier.ABSTRACT | Modifier.PUBLIC | Modifier.STATIC;
495 return ((getModifiers() & mask) == Modifier.PUBLIC) && getDeclaringClass().isInterface();
496 }
497
498 @Override
499 public Type[] getGenericParameterTypes() {
500 if (isConstructor()) {
501 Constructor<?> javaConstructor = toJavaConstructor();
502 return javaConstructor == null ? null : javaConstructor.getGenericParameterTypes();
503 }
504 Method javaMethod = toJava();
505 return javaMethod == null ? null : javaMethod.getGenericParameterTypes();
506 }
507
508 public Class<?>[] signatureToTypes() {
509 Signature sig = getSignature();
510 int count = sig.getParameterCount(false);
511 Class<?>[] result = new Class<?>[count];
512 for (int i = 0; i < result.length; ++i) {
513 JavaType parameterType = sig.getParameterType(i, holder);
514 HotSpotResolvedJavaType resolvedParameterType = (HotSpotResolvedJavaType) parameterType.resolve(holder);
515 result[i] = resolvedParameterType.mirror();
516 }
517 return result;
518 }
519
520 private Method toJava() {
521 if (toJavaCache != null) {
522 return (Method) toJavaCache;
523 }
524 try {
525 Method result = holder.mirror().getDeclaredMethod(name, signatureToTypes());
526 toJavaCache = result;
527 return result;
528 } catch (NoSuchMethodException | NoClassDefFoundError e) {
529 return null;
530 }
531 }
532
533 private Constructor<?> toJavaConstructor() {
534 if (toJavaCache != null) {
535 return (Constructor<?>) toJavaCache;
536 }
537 try {
538 Constructor<?> result = holder.mirror().getDeclaredConstructor(signatureToTypes());
539 toJavaCache = result;
540 return result;
541 } catch (NoSuchMethodException | NoClassDefFoundError e) {
542 return null;
543 }
544 }
545
546 @Override
547 public boolean canBeInlined() {
548 if (isDontInline()) {
549 return false;
550 }
551 return compilerToVM().canInlineMethod(this);
552 }
553
554 @Override
555 public boolean shouldBeInlined() {
556 if (isForceInline()) {
557 return true;
558 }
559 return compilerToVM().shouldInlineMethod(this);
560 }
561
562 @Override
563 public LineNumberTable getLineNumberTable() {
564 final boolean hasLineNumberTable = (getConstMethodFlags() & config().constMethodHasLineNumberTable) != 0;
565 if (!hasLineNumberTable) {
566 return null;
567 }
568
569 long[] values = compilerToVM().getLineNumberTable(this);
570 if (values == null || values.length == 0) {
571 // Empty table so treat is as non-existent
572 return null;
573 }
574 assert values.length % 2 == 0;
575 int[] bci = new int[values.length / 2];
576 int[] line = new int[values.length / 2];
577
578 for (int i = 0; i < values.length / 2; i++) {
579 bci[i] = (int) values[i * 2];
580 line[i] = (int) values[i * 2 + 1];
581 }
582
583 return new LineNumberTableImpl(line, bci);
584 }
585
586 @Override
587 public LocalVariableTable getLocalVariableTable() {
588 final boolean hasLocalVariableTable = (getConstMethodFlags() & config().constMethodHasLocalVariableTable) != 0;
589 if (!hasLocalVariableTable) {
590 return null;
591 }
592
593 HotSpotVMConfig config = config();
594 long localVariableTableElement = compilerToVM().getLocalVariableTableStart(this);
595 final int localVariableTableLength = compilerToVM().getLocalVariableTableLength(this);
596 Local[] locals = new Local[localVariableTableLength];
597
598 for (int i = 0; i < localVariableTableLength; i++) {
599 final int startBci = UNSAFE.getChar(localVariableTableElement + config.localVariableTableElementStartBciOffset);
600 final int endBci = startBci + UNSAFE.getChar(localVariableTableElement + config.localVariableTableElementLengthOffset);
601 final int nameCpIndex = UNSAFE.getChar(localVariableTableElement + config.localVariableTableElementNameCpIndexOffset);
602 final int typeCpIndex = UNSAFE.getChar(localVariableTableElement + config.localVariableTableElementDescriptorCpIndexOffset);
603 final int slot = UNSAFE.getChar(localVariableTableElement + config.localVariableTableElementSlotOffset);
604
605 String localName = getConstantPool().lookupUtf8(nameCpIndex);
606 String localType = getConstantPool().lookupUtf8(typeCpIndex);
607
608 locals[i] = new LocalImpl(localName, runtime().lookupType(localType, holder, false), startBci, endBci, slot);
609
610 // Go to the next LocalVariableTableElement
611 localVariableTableElement += config.localVariableTableElementSize;
612 }
613
614 return new LocalVariableTableImpl(locals);
615 }
616
617 /**
618 * Returns the offset of this method into the v-table. The method must have a v-table entry as
619 * indicated by {@link #isInVirtualMethodTable(ResolvedJavaType)}, otherwise an exception is
620 * thrown.
621 *
622 * @return the offset of this method into the v-table
623 */
624 public int vtableEntryOffset(ResolvedJavaType resolved) {
625 if (!isInVirtualMethodTable(resolved)) {
626 throw new JVMCIError("%s does not have a vtable entry in type %s", this, resolved);
627 }
628 HotSpotVMConfig config = config();
629 final int vtableIndex = getVtableIndex((HotSpotResolvedObjectTypeImpl) resolved);
630 return config.instanceKlassVtableStartOffset + vtableIndex * config.vtableEntrySize + config.vtableEntryMethodOffset;
631 }
632
633 @Override
634 public boolean isInVirtualMethodTable(ResolvedJavaType resolved) {
635 if (resolved instanceof HotSpotResolvedObjectTypeImpl) {
636 HotSpotResolvedObjectTypeImpl hotspotResolved = (HotSpotResolvedObjectTypeImpl) resolved;
637 int vtableIndex = getVtableIndex(hotspotResolved);
638 return vtableIndex >= 0 && vtableIndex < hotspotResolved.getVtableLength();
639 }
640 return false;
641 }
642
643 private int getVtableIndex(HotSpotResolvedObjectTypeImpl resolved) {
644 if (!holder.isLinked()) {
645 return config().invalidVtableIndex;
646 }
647 if (holder.isInterface()) {
648 if (resolved.isInterface()) {
649 return config().invalidVtableIndex;
650 }
651 return getVtableIndexForInterfaceMethod(resolved);
652 }
653 return getVtableIndex();
654 }
655
656 /**
657 * Returns this method's virtual table index.
658 *
659 * @return virtual table index
660 */
661 private int getVtableIndex() {
662 assert !holder.isInterface();
663 HotSpotVMConfig config = config();
664 int result = UNSAFE.getInt(metaspaceMethod + config.methodVtableIndexOffset);
665 assert result >= config.nonvirtualVtableIndex : "must be linked";
666 return result;
667 }
668
669 private int getVtableIndexForInterfaceMethod(ResolvedJavaType resolved) {
670 HotSpotResolvedObjectTypeImpl hotspotType = (HotSpotResolvedObjectTypeImpl) resolved;
671 return compilerToVM().getVtableIndexForInterfaceMethod(hotspotType, this);
672 }
673
674 /**
675 * The {@link SpeculationLog} for methods compiled by JVMCI hang off this per-declaring-type
676 * {@link ClassValue}. The raw Method* value is safe to use as a key in the map as a) it is
677 * never moves and b) we never read from it.
678 * <p>
679 * One implication is that we will preserve {@link SpeculationLog}s for methods that have been
680 * redefined via class redefinition. It's tempting to periodically flush such logs but we cannot
681 * read the JVM_ACC_IS_OBSOLETE bit (or anything else) via the raw pointer as obsoleted methods
682 * are subject to clean up and deletion (see InstanceKlass::purge_previous_versions_internal).
683 */
684 private static final ClassValue<Map<Long, SpeculationLog>> SpeculationLogs = new ClassValue<Map<Long, SpeculationLog>>() {
685 @Override
686 protected Map<Long, SpeculationLog> computeValue(java.lang.Class<?> type) {
687 return new HashMap<>(4);
688 }
689 };
690
691 public SpeculationLog getSpeculationLog() {
692 Map<Long, SpeculationLog> map = SpeculationLogs.get(holder.mirror());
693 synchronized (map) {
694 SpeculationLog log = map.get(this.metaspaceMethod);
695 if (log == null) {
696 log = new HotSpotSpeculationLog();
697 map.put(metaspaceMethod, log);
698 }
699 return log;
700 }
701 }
702
703 public int intrinsicId() {
704 HotSpotVMConfig config = config();
705 return UNSAFE.getByte(metaspaceMethod + config.methodIntrinsicIdOffset) & 0xff;
706 }
707
708 @Override
709 public JavaConstant invoke(JavaConstant receiver, JavaConstant[] arguments) {
710 assert !isConstructor();
711 Method javaMethod = toJava();
712 javaMethod.setAccessible(true);
713
714 Object[] objArguments = new Object[arguments.length];
715 for (int i = 0; i < arguments.length; i++) {
716 objArguments[i] = HotSpotObjectConstantImpl.asBoxedValue(arguments[i]);
717 }
718 Object objReceiver = receiver != null && !receiver.isNull() ? ((HotSpotObjectConstantImpl) receiver).object() : null;
719
720 try {
721 Object objResult = javaMethod.invoke(objReceiver, objArguments);
722 return javaMethod.getReturnType() == void.class ? null : HotSpotObjectConstantImpl.forBoxedValue(getSignature().getReturnKind(), objResult);
723
724 } catch (IllegalAccessException | InvocationTargetException ex) {
725 throw new IllegalArgumentException(ex);
726 }
727 }
728
729 /**
730 * Allocates a compile id for this method by asking the VM for one.
731 *
732 * @param entryBCI entry bci
733 * @return compile id
734 */
735 public int allocateCompileId(int entryBCI) {
736 return compilerToVM().allocateCompileId(this, entryBCI);
737 }
738
739 public boolean hasCodeAtLevel(int entryBCI, int level) {
740 if (entryBCI == config().invocationEntryBci) {
741 return hasCompiledCodeAtLevel(level);
742 }
743 return compilerToVM().hasCompiledCodeForOSR(this, entryBCI, level);
744 }
745 }