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.HotSpotResolvedObjectTypeImpl.fromObjectClass; 28 import jdk.vm.ci.common.JVMCIError; 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.MethodHandleAccessProvider; 33 import jdk.vm.ci.meta.ResolvedJavaField; 34 import jdk.vm.ci.meta.ResolvedJavaMethod; 35 import jdk.vm.ci.meta.ResolvedJavaType; 36 import jdk.vm.ci.meta.Signature; 37 38 public class HotSpotMethodHandleAccessProvider implements MethodHandleAccessProvider { 39 40 private final ConstantReflectionProvider constantReflection; 41 42 public HotSpotMethodHandleAccessProvider(ConstantReflectionProvider constantReflection) { 43 this.constantReflection = constantReflection; 44 } 45 46 /** 47 * Lazy initialization to break class initialization cycle. Field and method lookup is only 48 * possible after the {@link HotSpotJVMCIRuntime} is fully initialized. 49 */ 50 static class LazyInitialization { 51 static final ResolvedJavaField methodHandleFormField; 52 static final ResolvedJavaField lambdaFormVmentryField; 53 static final ResolvedJavaMethod lambdaFormCompileToBytecodeMethod; 54 static final HotSpotResolvedJavaField memberNameVmtargetField; 55 56 static final ResolvedJavaType CLASS = fromObjectClass(LazyInitialization.class); 57 58 /** 59 * Search for an instance field with the given name in a class. 60 * 61 * @param className name of the class to search in 62 * @param fieldName name of the field to be searched 63 * @param fieldType resolved Java type of the field 64 * @return resolved Java field 65 * @throws ClassNotFoundException 66 * @throws NoSuchFieldError 67 */ 68 private static ResolvedJavaField findFieldInClass(String className, String fieldName, ResolvedJavaType fieldType) 69 throws ClassNotFoundException { 70 Class<?> clazz = Class.forName(className); 71 ResolvedJavaType type = runtime().fromClass(clazz); 72 ResolvedJavaField[] fields = type.getInstanceFields(false); 73 for (ResolvedJavaField field : fields) { 74 if (field.getName().equals(fieldName) && field.getType().equals(fieldType)) { 75 return field; 76 } 77 } 78 throw new NoSuchFieldError(fieldType.getName() + " " + className + "." + fieldName); 79 } 80 81 private static ResolvedJavaMethod findMethodInClass(String className, String methodName, 82 ResolvedJavaType resultType, ResolvedJavaType[] parameterTypes) throws ClassNotFoundException { 83 Class<?> clazz = Class.forName(className); 84 HotSpotResolvedObjectTypeImpl type = fromObjectClass(clazz); 85 ResolvedJavaMethod result = null; 86 for (ResolvedJavaMethod method : type.getDeclaredMethods()) { 87 if (method.getName().equals(methodName) && signatureMatches(method, resultType, parameterTypes)) { 88 result = method; 89 } 90 } 91 if (result == null) { 92 StringBuilder sig = new StringBuilder("("); 93 for (ResolvedJavaType t : parameterTypes) { 94 sig.append(t.getName()).append(","); 95 } 96 if (sig.length() > 1) { 97 sig.replace(sig.length() - 1, sig.length(), ")"); 98 } else { 99 sig.append(')'); 100 } 101 throw new NoSuchMethodError(resultType.getName() + " " + className + "." + methodName + sig.toString()); 102 } 103 return result; 104 } 105 106 private static boolean signatureMatches(ResolvedJavaMethod m, ResolvedJavaType resultType, 107 ResolvedJavaType[] parameterTypes) { 108 Signature s = m.getSignature(); 109 if (!s.getReturnType(CLASS).equals(resultType)) { 110 return false; 111 } 112 if (s.getParameterCount(false) != parameterTypes.length) { 113 return false; 114 } 115 for (int i = 0; i < s.getParameterCount(false); ++i) { 116 if (!s.getParameterType(i, CLASS).equals(parameterTypes[i])) { 117 return false; 118 } 119 } 120 return true; 121 } 122 123 static { 124 try { 125 methodHandleFormField = findFieldInClass("java.lang.invoke.MethodHandle", "form", 126 fromObjectClass(Class.forName("java.lang.invoke.LambdaForm"))); 127 lambdaFormVmentryField = findFieldInClass("java.lang.invoke.LambdaForm", "vmentry", 128 fromObjectClass(Class.forName("java.lang.invoke.MemberName"))); 129 lambdaFormCompileToBytecodeMethod = findMethodInClass("java.lang.invoke.LambdaForm", "compileToBytecode", 130 new HotSpotResolvedPrimitiveType(JavaKind.Void), new ResolvedJavaType[]{}); 131 memberNameVmtargetField = (HotSpotResolvedJavaField) findFieldInClass("java.lang.invoke.MemberName", "vmtarget", 132 new HotSpotResolvedPrimitiveType(JavaKind.Long)); 133 } catch (Throwable ex) { 134 throw new JVMCIError(ex); 135 } 136 } 137 } 138 139 @Override 140 public IntrinsicMethod lookupMethodHandleIntrinsic(ResolvedJavaMethod method) { 141 int intrinsicId = ((HotSpotResolvedJavaMethodImpl) method).intrinsicId(); 142 if (intrinsicId != 0) { 143 return getMethodHandleIntrinsic(intrinsicId); 144 } 145 return null; 146 } 147 148 public static IntrinsicMethod getMethodHandleIntrinsic(int intrinsicId) { 149 HotSpotVMConfig config = runtime().getConfig(); 150 if (intrinsicId == config.vmIntrinsicInvokeBasic) { 151 return IntrinsicMethod.INVOKE_BASIC; 152 } else if (intrinsicId == config.vmIntrinsicLinkToInterface) { 156 } else if (intrinsicId == config.vmIntrinsicLinkToStatic) { 157 return IntrinsicMethod.LINK_TO_STATIC; 158 } else if (intrinsicId == config.vmIntrinsicLinkToVirtual) { 159 return IntrinsicMethod.LINK_TO_VIRTUAL; 160 } 161 return null; 162 } 163 164 @Override 165 public ResolvedJavaMethod resolveInvokeBasicTarget(JavaConstant methodHandle, boolean forceBytecodeGeneration) { 166 if (methodHandle.isNull()) { 167 return null; 168 } 169 170 /* Load non-public field: LambdaForm MethodHandle.form */ 171 JavaConstant lambdaForm = constantReflection.readFieldValue(LazyInitialization.methodHandleFormField, methodHandle); 172 if (lambdaForm == null || lambdaForm.isNull()) { 173 return null; 174 } 175 176 if (forceBytecodeGeneration) { 177 /* Invoke non-public method: MemberName LambdaForm.compileToBytecode() */ 178 LazyInitialization.lambdaFormCompileToBytecodeMethod.invoke(lambdaForm, new JavaConstant[0]); 179 } 180 /* Load non-public field: MemberName LambdaForm.vmentry */ 181 JavaConstant memberName = constantReflection.readFieldValue(LazyInitialization.lambdaFormVmentryField, lambdaForm); 182 return getTargetMethod(memberName); 183 } 184 185 @Override 186 public ResolvedJavaMethod resolveLinkToTarget(JavaConstant memberName) { 187 return getTargetMethod(memberName); 188 } 189 190 /** 191 * Returns the {@link ResolvedJavaMethod} for the vmtarget of a java.lang.invoke.MemberName. 192 */ 193 private static ResolvedJavaMethod getTargetMethod(JavaConstant memberName) { 194 if (memberName.isNull()) { 195 return null; 196 } 197 198 Object object = ((HotSpotObjectConstantImpl) memberName).object(); 199 /* Read the ResolvedJavaMethod from the injected field MemberName.vmtarget */ 200 return compilerToVM().getResolvedJavaMethod(object, LazyInitialization.memberNameVmtargetField.offset()); 201 } 202 } 203 | 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 28 import java.lang.invoke.MethodHandle; 29 import java.util.Objects; 30 31 import jdk.vm.ci.common.JVMCIError; 32 import jdk.vm.ci.meta.ConstantReflectionProvider; 33 import jdk.vm.ci.meta.JavaConstant; 34 import jdk.vm.ci.meta.MethodHandleAccessProvider; 35 import jdk.vm.ci.meta.ResolvedJavaField; 36 import jdk.vm.ci.meta.ResolvedJavaMethod; 37 import jdk.vm.ci.meta.ResolvedJavaType; 38 39 public class HotSpotMethodHandleAccessProvider implements MethodHandleAccessProvider { 40 41 private final ConstantReflectionProvider constantReflection; 42 43 public HotSpotMethodHandleAccessProvider(ConstantReflectionProvider constantReflection) { 44 this.constantReflection = constantReflection; 45 } 46 47 /** 48 * Lazy initialization to break class initialization cycle. Field and method lookup is only 49 * possible after the {@link HotSpotJVMCIRuntime} is fully initialized. 50 */ 51 static class LazyInitialization { 52 static final ResolvedJavaType lambdaFormType; 53 static final ResolvedJavaField methodHandleFormField; 54 static final ResolvedJavaField lambdaFormVmentryField; 55 static final HotSpotResolvedJavaField memberNameVmtargetField; 56 57 /** 58 * Search for an instance field with the given name in a class. 59 * 60 * @param declaringType the type declaring the field 61 * @param fieldName name of the field to be searched 62 * @param fieldType resolved Java type of the field 63 * @return resolved Java field 64 * @throws NoSuchFieldError 65 */ 66 private static ResolvedJavaField findFieldInClass(ResolvedJavaType declaringType, String fieldName, ResolvedJavaType fieldType) { 67 ResolvedJavaField[] fields = declaringType.getInstanceFields(false); 68 for (ResolvedJavaField field : fields) { 69 if (field.getName().equals(fieldName) && field.getType().equals(fieldType)) { 70 return field; 71 } 72 } 73 throw new NoSuchFieldError(fieldType.getName() + " " + declaringType + "." + fieldName); 74 } 75 76 private static ResolvedJavaType resolveType(Class<?> c) { 77 return runtime().fromClass(c); 78 } 79 80 private static ResolvedJavaType resolveType(String className) throws ClassNotFoundException { 81 return resolveType(Class.forName(className)); 82 } 83 84 static { 85 try { 86 ResolvedJavaType methodHandleType = resolveType(MethodHandle.class); 87 ResolvedJavaType memberNameType = resolveType("java.lang.invoke.MemberName"); 88 lambdaFormType = resolveType("java.lang.invoke.LambdaForm"); 89 methodHandleFormField = findFieldInClass(methodHandleType, "form", lambdaFormType); 90 lambdaFormVmentryField = findFieldInClass(lambdaFormType, "vmentry", memberNameType); 91 memberNameVmtargetField = (HotSpotResolvedJavaField) findFieldInClass(memberNameType, "vmtarget", resolveType(long.class)); 92 } catch (Throwable ex) { 93 throw new JVMCIError(ex); 94 } 95 } 96 } 97 98 @Override 99 public IntrinsicMethod lookupMethodHandleIntrinsic(ResolvedJavaMethod method) { 100 int intrinsicId = ((HotSpotResolvedJavaMethodImpl) method).intrinsicId(); 101 if (intrinsicId != 0) { 102 return getMethodHandleIntrinsic(intrinsicId); 103 } 104 return null; 105 } 106 107 public static IntrinsicMethod getMethodHandleIntrinsic(int intrinsicId) { 108 HotSpotVMConfig config = runtime().getConfig(); 109 if (intrinsicId == config.vmIntrinsicInvokeBasic) { 110 return IntrinsicMethod.INVOKE_BASIC; 111 } else if (intrinsicId == config.vmIntrinsicLinkToInterface) { 115 } else if (intrinsicId == config.vmIntrinsicLinkToStatic) { 116 return IntrinsicMethod.LINK_TO_STATIC; 117 } else if (intrinsicId == config.vmIntrinsicLinkToVirtual) { 118 return IntrinsicMethod.LINK_TO_VIRTUAL; 119 } 120 return null; 121 } 122 123 @Override 124 public ResolvedJavaMethod resolveInvokeBasicTarget(JavaConstant methodHandle, boolean forceBytecodeGeneration) { 125 if (methodHandle.isNull()) { 126 return null; 127 } 128 129 /* Load non-public field: LambdaForm MethodHandle.form */ 130 JavaConstant lambdaForm = constantReflection.readFieldValue(LazyInitialization.methodHandleFormField, methodHandle); 131 if (lambdaForm == null || lambdaForm.isNull()) { 132 return null; 133 } 134 135 JavaConstant memberName = constantReflection.readFieldValue(LazyInitialization.lambdaFormVmentryField, lambdaForm); 136 if (memberName.isNull() && forceBytecodeGeneration) { 137 Object lf = ((HotSpotObjectConstant) lambdaForm).asObject(LazyInitialization.lambdaFormType); 138 compilerToVM().compileToBytecode(Objects.requireNonNull(lf)); 139 memberName = constantReflection.readFieldValue(LazyInitialization.lambdaFormVmentryField, lambdaForm); 140 assert memberName.isNonNull(); 141 } 142 /* Load non-public field: MemberName LambdaForm.vmentry */ 143 return getTargetMethod(memberName); 144 } 145 146 @Override 147 public ResolvedJavaMethod resolveLinkToTarget(JavaConstant memberName) { 148 return getTargetMethod(memberName); 149 } 150 151 /** 152 * Returns the {@link ResolvedJavaMethod} for the vmtarget of a java.lang.invoke.MemberName. 153 */ 154 private static ResolvedJavaMethod getTargetMethod(JavaConstant memberName) { 155 if (memberName.isNull()) { 156 return null; 157 } 158 159 Object object = ((HotSpotObjectConstantImpl) memberName).object(); 160 /* Read the ResolvedJavaMethod from the injected field MemberName.vmtarget */ 161 return compilerToVM().getResolvedJavaMethod(object, LazyInitialization.memberNameVmtargetField.offset()); 162 } 163 } |