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.annotation.Annotation;
31 import java.lang.reflect.Executable;
32 import java.lang.reflect.InvocationTargetException;
33 import java.lang.reflect.Method;
34 import java.lang.reflect.Modifier;
35 import java.lang.reflect.Type;
36 import java.util.HashMap;
37 import java.util.Map;
38
39 import jdk.vm.ci.common.JVMCIError;
40 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.Option;
41 import jdk.vm.ci.meta.Constant;
42 import jdk.vm.ci.meta.ConstantPool;
43 import jdk.vm.ci.meta.DefaultProfilingInfo;
44 import jdk.vm.ci.meta.ExceptionHandler;
45 import jdk.vm.ci.meta.JavaConstant;
46 import jdk.vm.ci.meta.JavaMethod;
47 import jdk.vm.ci.meta.JavaType;
48 import jdk.vm.ci.meta.LineNumberTable;
49 import jdk.vm.ci.meta.Local;
50 import jdk.vm.ci.meta.LocalVariableTable;
51 import jdk.vm.ci.meta.ModifiersProvider;
52 import jdk.vm.ci.meta.ProfilingInfo;
53 import jdk.vm.ci.meta.ResolvedJavaMethod;
54 import jdk.vm.ci.meta.ResolvedJavaType;
55 import jdk.vm.ci.meta.Signature;
56 import jdk.vm.ci.meta.SpeculationLog;
57 import jdk.vm.ci.meta.TriState;
58
59 /**
60 * Implementation of {@link JavaMethod} for resolved HotSpot methods.
61 */
62 final class HotSpotResolvedJavaMethodImpl extends HotSpotMethod implements HotSpotResolvedJavaMethod, MetaspaceWrapperObject {
63
64 /**
65 * Reference to metaspace Method object.
66 */
67 private final long metaspaceMethod;
68
69 private final HotSpotResolvedObjectTypeImpl holder;
70 private final HotSpotConstantPool constantPool;
71 private final HotSpotSignature signature;
193
194 public long getMetaspacePointer() {
195 return metaspaceMethod;
196 }
197
198 @Override
199 public Constant getEncoding() {
200 return getMetaspaceMethodConstant();
201 }
202
203 /**
204 * Gets the complete set of modifiers for this method which includes the JVM specification
205 * modifiers as well as the HotSpot internal modifiers.
206 */
207 public int getAllModifiers() {
208 return UNSAFE.getInt(metaspaceMethod + config().methodAccessFlagsOffset);
209 }
210
211 @Override
212 public int getModifiers() {
213 return getAllModifiers() & ModifiersProvider.jvmMethodModifiers();
214 }
215
216 @Override
217 public boolean canBeStaticallyBound() {
218 return (isFinal() || isPrivate() || isStatic() || holder.isLeaf()) && isConcrete();
219 }
220
221 @Override
222 public byte[] getCode() {
223 if (getCodeSize() == 0) {
224 return null;
225 }
226 if (code == null && holder.isLinked()) {
227 code = compilerToVM().getBytecode(this);
228 assert code.length == getCodeSize() : "expected: " + getCodeSize() + ", actual: " + code.length;
229 }
230 return code;
231 }
232
233 @Override
473 return javaMethod.getAnnotations();
474 }
475 return new Annotation[0];
476 }
477
478 @Override
479 public Annotation[] getDeclaredAnnotations() {
480 Executable javaMethod = toJava();
481 if (javaMethod != null) {
482 return javaMethod.getDeclaredAnnotations();
483 }
484 return new Annotation[0];
485 }
486
487 @Override
488 public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
489 Executable javaMethod = toJava();
490 return javaMethod == null ? null : javaMethod.getAnnotation(annotationClass);
491 }
492
493 public boolean isDefault() {
494 if (isConstructor()) {
495 return false;
496 }
497 // Copied from java.lang.Method.isDefault()
498 int mask = Modifier.ABSTRACT | Modifier.PUBLIC | Modifier.STATIC;
499 return ((getModifiers() & mask) == Modifier.PUBLIC) && getDeclaringClass().isInterface();
500 }
501
502 @Override
503 public Type[] getGenericParameterTypes() {
504 Executable 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) {
680 Map<Long, SpeculationLog> map = SpeculationLogs.get(holder.mirror());
681 synchronized (map) {
682 SpeculationLog log = map.get(this.metaspaceMethod);
683 if (log == null) {
684 log = new HotSpotSpeculationLog();
685 map.put(metaspaceMethod, log);
686 }
687 return log;
688 }
689 }
690
691 public int intrinsicId() {
692 HotSpotVMConfig config = config();
693 return UNSAFE.getChar(metaspaceMethod + config.methodIntrinsicIdOffset);
694 }
695
696 public boolean isIntrinsicCandidate() {
697 return (getFlags() & config().methodFlagsIntrinsicCandidate) != 0;
698 }
699
700 @Override
701 public JavaConstant invoke(JavaConstant receiver, JavaConstant[] arguments) {
702 assert !isConstructor();
703 Method javaMethod = (Method) toJava();
704 javaMethod.setAccessible(true);
705
706 Object[] objArguments = new Object[arguments.length];
707 for (int i = 0; i < arguments.length; i++) {
708 objArguments[i] = HotSpotObjectConstantImpl.asBoxedValue(arguments[i]);
709 }
710 Object objReceiver = receiver != null && !receiver.isNull() ? ((HotSpotObjectConstantImpl) receiver).object() : null;
711
712 try {
713 Object objResult = javaMethod.invoke(objReceiver, objArguments);
714 return javaMethod.getReturnType() == void.class ? null : HotSpotObjectConstantImpl.forBoxedValue(getSignature().getReturnKind(), objResult);
715
716 } catch (IllegalAccessException | InvocationTargetException ex) {
717 throw new IllegalArgumentException(ex);
718 }
719 }
720
721 /**
722 * Allocates a compile id for this method by asking the VM for one.
723 *
724 * @param entryBCI entry bci
725 * @return compile id
726 */
727 public int allocateCompileId(int entryBCI) {
728 return compilerToVM().allocateCompileId(this, entryBCI);
729 }
730
731 public boolean hasCodeAtLevel(int entryBCI, int level) {
732 if (entryBCI == config().invocationEntryBci) {
733 return hasCompiledCodeAtLevel(level);
734 }
735 return compilerToVM().hasCompiledCodeForOSR(this, entryBCI, level);
736 }
737 }
|
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.HotSpotModifiers.BRIDGE;
28 import static jdk.vm.ci.hotspot.HotSpotModifiers.SYNTHETIC;
29 import static jdk.vm.ci.hotspot.HotSpotModifiers.VARARGS;
30 import static jdk.vm.ci.hotspot.HotSpotModifiers.jvmMethodModifiers;
31 import static jdk.vm.ci.hotspot.HotSpotVMConfig.config;
32 import static jdk.vm.ci.hotspot.UnsafeAccess.UNSAFE;
33
34 import java.lang.annotation.Annotation;
35 import java.lang.reflect.Executable;
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.hotspot.HotSpotJVMCIRuntime.Option;
43 import jdk.vm.ci.meta.Constant;
44 import jdk.vm.ci.meta.ConstantPool;
45 import jdk.vm.ci.meta.DefaultProfilingInfo;
46 import jdk.vm.ci.meta.ExceptionHandler;
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.Local;
51 import jdk.vm.ci.meta.LocalVariableTable;
52 import jdk.vm.ci.meta.ProfilingInfo;
53 import jdk.vm.ci.meta.ResolvedJavaMethod;
54 import jdk.vm.ci.meta.ResolvedJavaType;
55 import jdk.vm.ci.meta.Signature;
56 import jdk.vm.ci.meta.SpeculationLog;
57 import jdk.vm.ci.meta.TriState;
58
59 /**
60 * Implementation of {@link JavaMethod} for resolved HotSpot methods.
61 */
62 final class HotSpotResolvedJavaMethodImpl extends HotSpotMethod implements HotSpotResolvedJavaMethod, MetaspaceWrapperObject {
63
64 /**
65 * Reference to metaspace Method object.
66 */
67 private final long metaspaceMethod;
68
69 private final HotSpotResolvedObjectTypeImpl holder;
70 private final HotSpotConstantPool constantPool;
71 private final HotSpotSignature signature;
193
194 public long getMetaspacePointer() {
195 return metaspaceMethod;
196 }
197
198 @Override
199 public Constant getEncoding() {
200 return getMetaspaceMethodConstant();
201 }
202
203 /**
204 * Gets the complete set of modifiers for this method which includes the JVM specification
205 * modifiers as well as the HotSpot internal modifiers.
206 */
207 public int getAllModifiers() {
208 return UNSAFE.getInt(metaspaceMethod + config().methodAccessFlagsOffset);
209 }
210
211 @Override
212 public int getModifiers() {
213 return getAllModifiers() & jvmMethodModifiers();
214 }
215
216 @Override
217 public boolean canBeStaticallyBound() {
218 return (isFinal() || isPrivate() || isStatic() || holder.isLeaf()) && isConcrete();
219 }
220
221 @Override
222 public byte[] getCode() {
223 if (getCodeSize() == 0) {
224 return null;
225 }
226 if (code == null && holder.isLinked()) {
227 code = compilerToVM().getBytecode(this);
228 assert code.length == getCodeSize() : "expected: " + getCodeSize() + ", actual: " + code.length;
229 }
230 return code;
231 }
232
233 @Override
473 return javaMethod.getAnnotations();
474 }
475 return new Annotation[0];
476 }
477
478 @Override
479 public Annotation[] getDeclaredAnnotations() {
480 Executable javaMethod = toJava();
481 if (javaMethod != null) {
482 return javaMethod.getDeclaredAnnotations();
483 }
484 return new Annotation[0];
485 }
486
487 @Override
488 public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
489 Executable javaMethod = toJava();
490 return javaMethod == null ? null : javaMethod.getAnnotation(annotationClass);
491 }
492
493 public boolean isBridge() {
494 return (BRIDGE & getModifiers()) != 0;
495 }
496
497 @Override
498 public boolean isSynthetic() {
499 return (SYNTHETIC & getModifiers()) != 0;
500 }
501
502 public boolean isVarArgs() {
503 return (VARARGS & getModifiers()) != 0;
504 }
505
506 public boolean isDefault() {
507 if (isConstructor()) {
508 return false;
509 }
510 // Copied from java.lang.Method.isDefault()
511 int mask = Modifier.ABSTRACT | Modifier.PUBLIC | Modifier.STATIC;
512 return ((getModifiers() & mask) == Modifier.PUBLIC) && getDeclaringClass().isInterface();
513 }
514
515 @Override
516 public Type[] getGenericParameterTypes() {
517 Executable javaMethod = toJava();
518 return javaMethod == null ? null : javaMethod.getGenericParameterTypes();
519 }
520
521 public Class<?>[] signatureToTypes() {
522 Signature sig = getSignature();
523 int count = sig.getParameterCount(false);
524 Class<?>[] result = new Class<?>[count];
525 for (int i = 0; i < result.length; ++i) {
693 Map<Long, SpeculationLog> map = SpeculationLogs.get(holder.mirror());
694 synchronized (map) {
695 SpeculationLog log = map.get(this.metaspaceMethod);
696 if (log == null) {
697 log = new HotSpotSpeculationLog();
698 map.put(metaspaceMethod, log);
699 }
700 return log;
701 }
702 }
703
704 public int intrinsicId() {
705 HotSpotVMConfig config = config();
706 return UNSAFE.getChar(metaspaceMethod + config.methodIntrinsicIdOffset);
707 }
708
709 public boolean isIntrinsicCandidate() {
710 return (getFlags() & config().methodFlagsIntrinsicCandidate) != 0;
711 }
712
713 /**
714 * Allocates a compile id for this method by asking the VM for one.
715 *
716 * @param entryBCI entry bci
717 * @return compile id
718 */
719 public int allocateCompileId(int entryBCI) {
720 return compilerToVM().allocateCompileId(this, entryBCI);
721 }
722
723 public boolean hasCodeAtLevel(int entryBCI, int level) {
724 if (entryBCI == config().invocationEntryBci) {
725 return hasCompiledCodeAtLevel(level);
726 }
727 return compilerToVM().hasCompiledCodeForOSR(this, entryBCI, level);
728 }
729 }
|