1 /* 2 * Copyright (c) 2014, 2014, 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.internal.jvmci.hotspot; 24 25 import static jdk.internal.jvmci.hotspot.HotSpotJVMCIRuntime.*; 26 import static jdk.internal.jvmci.hotspot.HotSpotResolvedObjectTypeImpl.*; 27 import jdk.internal.jvmci.common.*; 28 import jdk.internal.jvmci.meta.*; 29 30 public class HotSpotMethodHandleAccessProvider implements MethodHandleAccessProvider, HotSpotProxified { 31 32 private final ConstantReflectionProvider constantReflection; 33 34 public HotSpotMethodHandleAccessProvider(ConstantReflectionProvider constantReflection) { 35 this.constantReflection = constantReflection; 36 } 37 38 /** 39 * Lazy initialization to break class initialization cycle. Field and method lookup is only 40 * possible after the {@link HotSpotJVMCIRuntime} is fully initialized. 41 */ 42 static class LazyInitialization { 43 static final ResolvedJavaField methodHandleFormField; 44 static final ResolvedJavaField lambdaFormVmentryField; 45 static final ResolvedJavaMethod lambdaFormCompileToBytecodeMethod; 46 static final HotSpotResolvedJavaField memberNameVmtargetField; 47 48 /** 49 * Search for an instance field with the given name in a class. 50 * 51 * @param className name of the class to search in 52 * @param fieldName name of the field to be searched 53 * @return resolved java field 54 * @throws ClassNotFoundException 55 */ 56 private static ResolvedJavaField findFieldInClass(String className, String fieldName) throws ClassNotFoundException { 57 Class<?> clazz = Class.forName(className); 58 ResolvedJavaType type = runtime().fromClass(clazz); 59 ResolvedJavaField[] fields = type.getInstanceFields(false); 60 for (ResolvedJavaField field : fields) { 61 if (field.getName().equals(fieldName)) { 62 return field; 63 } 64 } 65 return null; 66 } 67 68 private static ResolvedJavaMethod findMethodInClass(String className, String methodName) throws ClassNotFoundException { 69 Class<?> clazz = Class.forName(className); 70 HotSpotResolvedObjectTypeImpl type = fromObjectClass(clazz); 71 ResolvedJavaMethod result = null; 72 for (ResolvedJavaMethod method : type.getDeclaredMethods()) { 73 if (method.getName().equals(methodName)) { 74 assert result == null : "more than one method found: " + className + "." + methodName; 75 result = method; 76 } 77 } 78 assert result != null : "method not found: " + className + "." + methodName; 79 return result; 80 } 81 82 static { 83 try { 84 methodHandleFormField = findFieldInClass("java.lang.invoke.MethodHandle", "form"); 85 lambdaFormVmentryField = findFieldInClass("java.lang.invoke.LambdaForm", "vmentry"); 86 lambdaFormCompileToBytecodeMethod = findMethodInClass("java.lang.invoke.LambdaForm", "compileToBytecode"); 87 memberNameVmtargetField = (HotSpotResolvedJavaField) findFieldInClass("java.lang.invoke.MemberName", "vmtarget"); 88 } catch (Throwable ex) { 89 throw new JVMCIError(ex); 90 } 91 } 92 } 93 94 @Override 95 public IntrinsicMethod lookupMethodHandleIntrinsic(ResolvedJavaMethod method) { 96 int intrinsicId = ((HotSpotResolvedJavaMethodImpl) method).intrinsicId(); 97 if (intrinsicId != 0) { 98 return getMethodHandleIntrinsic(intrinsicId); 99 } 100 return null; 101 } 102 103 public static IntrinsicMethod getMethodHandleIntrinsic(int intrinsicId) { 104 HotSpotVMConfig config = runtime().getConfig(); 105 if (intrinsicId == config.vmIntrinsicInvokeBasic) { 106 return IntrinsicMethod.INVOKE_BASIC; 107 } else if (intrinsicId == config.vmIntrinsicLinkToInterface) { 108 return IntrinsicMethod.LINK_TO_INTERFACE; 109 } else if (intrinsicId == config.vmIntrinsicLinkToSpecial) { 110 return IntrinsicMethod.LINK_TO_SPECIAL; 111 } else if (intrinsicId == config.vmIntrinsicLinkToStatic) { 112 return IntrinsicMethod.LINK_TO_STATIC; 113 } else if (intrinsicId == config.vmIntrinsicLinkToVirtual) { 114 return IntrinsicMethod.LINK_TO_VIRTUAL; 115 } 116 return null; 117 } 118 119 @Override 120 public ResolvedJavaMethod resolveInvokeBasicTarget(JavaConstant methodHandle, boolean forceBytecodeGeneration) { 121 if (methodHandle.isNull()) { 122 return null; 123 } 124 125 /* Load non-public field: LambdaForm MethodHandle.form */ 126 JavaConstant lambdaForm = constantReflection.readFieldValue(LazyInitialization.methodHandleFormField, methodHandle); 127 if (lambdaForm.isNull()) { 128 return null; 129 } 130 131 JavaConstant memberName; 132 if (forceBytecodeGeneration) { 133 /* Invoke non-public method: MemberName LambdaForm.compileToBytecode() */ 134 memberName = LazyInitialization.lambdaFormCompileToBytecodeMethod.invoke(lambdaForm, new JavaConstant[0]); 135 } else { 136 /* Load non-public field: MemberName LambdaForm.vmentry */ 137 memberName = constantReflection.readFieldValue(LazyInitialization.lambdaFormVmentryField, lambdaForm); 138 } 139 return getTargetMethod(memberName); 140 } 141 142 @Override 143 public ResolvedJavaMethod resolveLinkToTarget(JavaConstant memberName) { 144 return getTargetMethod(memberName); 145 } 146 147 /** 148 * Returns the {@link ResolvedJavaMethod} for the vmtarget of a java.lang.invoke.MemberName. 149 */ 150 private static ResolvedJavaMethod getTargetMethod(JavaConstant memberName) { 151 if (memberName.isNull()) { 152 return null; 153 } 154 155 Object object = ((HotSpotObjectConstantImpl) memberName).object(); 156 /* Read the ResolvedJavaMethod from the injected field MemberName.vmtarget */ 157 return runtime().compilerToVm.getResolvedJavaMethod(object, LazyInitialization.memberNameVmtargetField.offset()); 158 } 159 }