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