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