src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotMethodHandleAccessProvider.java
Index Unified diffs Context diffs Sdiffs Frames Patch New Old Previous File Next File open Sdiff src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot

src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotMethodHandleAccessProvider.java

Print this page


   1 /*
   2  * Copyright (c) 2014, 2017, 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 
  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 ResolvedJavaField methodField;
  56         static final HotSpotResolvedJavaField vmtargetField;

  57 
  58         /**
  59          * Search for an instance field with the given name in a class.
  60          *
  61          * @param declaringType the type declaring the field
  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 NoSuchFieldError
  66          */
  67         private static ResolvedJavaField findFieldInClass(ResolvedJavaType declaringType, String fieldName, ResolvedJavaType fieldType) {
  68             ResolvedJavaField[] fields = declaringType.getInstanceFields(false);
  69             for (ResolvedJavaField field : fields) {
  70                 if (field.getName().equals(fieldName) && field.getType().equals(fieldType)) {
  71                     return field;
  72                 }
  73             }
  74             throw new NoSuchFieldError(fieldType.getName() + " " + declaringType + "." + fieldName);
  75         }
  76 
  77         private static ResolvedJavaType resolveType(Class<?> c) {
  78             return runtime().fromClass(c);
  79         }
  80 
  81         private static ResolvedJavaType resolveType(String className) throws ClassNotFoundException {
  82             return resolveType(Class.forName(className));
  83         }
  84 
  85         static {
  86             try {
  87                 ResolvedJavaType methodHandleType = resolveType(MethodHandle.class);
  88                 ResolvedJavaType memberNameType = resolveType("java.lang.invoke.MemberName");
  89                 lambdaFormType = resolveType("java.lang.invoke.LambdaForm");
  90                 methodHandleFormField = findFieldInClass(methodHandleType, "form", lambdaFormType);
  91                 lambdaFormVmentryField = findFieldInClass(lambdaFormType, "vmentry", memberNameType);
  92                 ResolvedJavaType methodType = resolveType("java.lang.invoke.ResolvedMethodName");

  93                 methodField = findFieldInClass(memberNameType, "method", methodType);
  94                 vmtargetField = (HotSpotResolvedJavaField) findFieldInClass(methodType, "vmtarget", resolveType(HotSpotJVMCIRuntime.getHostWordKind().toJavaClass()));
  95 


  96             } catch (Throwable ex) {
  97                 throw new JVMCIError(ex);
  98             }
  99         }













 100     }







 101 
 102     @Override
 103     public IntrinsicMethod lookupMethodHandleIntrinsic(ResolvedJavaMethod method) {
 104         int intrinsicId = ((HotSpotResolvedJavaMethodImpl) method).intrinsicId();
 105         if (intrinsicId != 0) {
 106             return getMethodHandleIntrinsic(intrinsicId);
 107         }
 108         return null;
 109     }
 110 
 111     public static IntrinsicMethod getMethodHandleIntrinsic(int intrinsicId) {
 112         HotSpotVMConfig config = runtime().getConfig();
 113         if (intrinsicId == config.vmIntrinsicInvokeBasic) {
 114             return IntrinsicMethod.INVOKE_BASIC;
 115         } else if (intrinsicId == config.vmIntrinsicLinkToInterface) {
 116             return IntrinsicMethod.LINK_TO_INTERFACE;
 117         } else if (intrinsicId == config.vmIntrinsicLinkToSpecial) {
 118             return IntrinsicMethod.LINK_TO_SPECIAL;
 119         } else if (intrinsicId == config.vmIntrinsicLinkToStatic) {
 120             return IntrinsicMethod.LINK_TO_STATIC;
 121         } else if (intrinsicId == config.vmIntrinsicLinkToVirtual) {
 122             return IntrinsicMethod.LINK_TO_VIRTUAL;
 123         }
 124         return null;
 125     }
 126 
 127     @Override
 128     public ResolvedJavaMethod resolveInvokeBasicTarget(JavaConstant methodHandle, boolean forceBytecodeGeneration) {
 129         if (methodHandle.isNull()) {
 130             return null;
 131         }
 132 
 133         /* Load non-public field: LambdaForm MethodHandle.form */
 134         JavaConstant lambdaForm = constantReflection.readFieldValue(LazyInitialization.methodHandleFormField, methodHandle);

 135         if (lambdaForm == null || lambdaForm.isNull()) {
 136             return null;
 137         }
 138 
 139         JavaConstant memberName = constantReflection.readFieldValue(LazyInitialization.lambdaFormVmentryField, lambdaForm);
 140         if (memberName.isNull() && forceBytecodeGeneration) {
 141             Object lf = ((HotSpotObjectConstant) lambdaForm).asObject(LazyInitialization.lambdaFormType);
 142             compilerToVM().compileToBytecode(Objects.requireNonNull(lf));
 143             memberName = constantReflection.readFieldValue(LazyInitialization.lambdaFormVmentryField, lambdaForm);
 144             assert memberName.isNonNull();
 145         }
 146         JavaConstant method = constantReflection.readFieldValue(LazyInitialization.methodField, memberName);
 147         return getTargetMethod(method);
 148     }
 149 
 150     @Override
 151     public ResolvedJavaMethod resolveLinkToTarget(JavaConstant memberName) {
 152         if (memberName.isNull()) {
 153             return null;
 154         }
 155         JavaConstant method = constantReflection.readFieldValue(LazyInitialization.methodField, memberName);
 156         return getTargetMethod(method);
 157     }
 158 
 159     /**
 160      * Returns the {@link ResolvedJavaMethod} for the method of a java.lang.invoke.MemberName.
 161      */
 162     private static ResolvedJavaMethod getTargetMethod(JavaConstant method) {
 163         if (method == null) {
 164             // If readFieldValue returns NULL the type was wrong
 165             throw new IllegalArgumentException("unexpected type for memberName");
 166         }
 167 
 168         Object object = ((HotSpotObjectConstantImpl) method).object();
 169         /* Read the ResolvedJavaMethod from the injected field MemberName.method.vmtarget */
 170         return compilerToVM().getResolvedJavaMethod(object, LazyInitialization.vmtargetField.getOffset());
 171     }
 172 }
   1 /*
   2  * Copyright (c) 2014, 2019, 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 
  28 import java.lang.invoke.MethodHandle;

  29 
  30 import jdk.vm.ci.common.JVMCIError;
  31 import jdk.vm.ci.common.NativeImageReinitialize;
  32 import jdk.vm.ci.hotspot.HotSpotMethodData.VMState;
  33 import jdk.vm.ci.meta.ConstantReflectionProvider;
  34 import jdk.vm.ci.meta.JavaConstant;
  35 import jdk.vm.ci.meta.MethodHandleAccessProvider;
  36 import jdk.vm.ci.meta.ResolvedJavaField;
  37 import jdk.vm.ci.meta.ResolvedJavaMethod;
  38 import jdk.vm.ci.meta.ResolvedJavaType;
  39 
  40 public class HotSpotMethodHandleAccessProvider implements MethodHandleAccessProvider {
  41 
  42     private final ConstantReflectionProvider constantReflection;
  43 
  44     public HotSpotMethodHandleAccessProvider(ConstantReflectionProvider constantReflection) {
  45         this.constantReflection = constantReflection;
  46     }
  47 
  48     /**
  49      * Lazy initialized reflection on {@link MethodHandle} internals. Field and method lookup is
  50      * only possible after the {@link HotSpotJVMCIRuntime} is fully initialized.
  51      */
  52     static final class Internals {
  53         final ResolvedJavaType lambdaFormType;
  54         final ResolvedJavaField methodHandleFormField;
  55         final ResolvedJavaField lambdaFormVmentryField;
  56         final HotSpotResolvedJavaField callSiteTargetField;
  57         final ResolvedJavaField methodField;
  58         final HotSpotResolvedJavaField vmtargetField;
  59 
  60         /**
  61          * Search for an instance field with the given name in a class.
  62          *
  63          * @param declaringType the type declaring the field
  64          * @param fieldName name of the field to be searched
  65          * @param fieldType resolved Java type of the field
  66          * @return resolved Java field
  67          * @throws NoSuchFieldError
  68          */
  69         private static ResolvedJavaField findFieldInClass(ResolvedJavaType declaringType, String fieldName, ResolvedJavaType fieldType) {
  70             ResolvedJavaField[] fields = declaringType.getInstanceFields(false);
  71             for (ResolvedJavaField field : fields) {
  72                 if (field.getName().equals(fieldName) && field.getType().equals(fieldType)) {
  73                     return field;
  74                 }
  75             }
  76             throw new NoSuchFieldError(declaringType + "." + fieldName);
  77         }
  78 
  79         private static ResolvedJavaType resolveType(String className) {
  80             return (ResolvedJavaType) runtime().lookupTypeInternal(className, null, true);
  81         }
  82 
  83         private Internals() {




  84             try {
  85                 ResolvedJavaType methodHandleType = resolveType("Ljava/lang/invoke/MethodHandle;");
  86                 ResolvedJavaType memberNameType = resolveType("Ljava/lang/invoke/MemberName;");
  87                 lambdaFormType = resolveType("Ljava/lang/invoke/LambdaForm;");
  88                 methodHandleFormField = findFieldInClass(methodHandleType, "form", lambdaFormType);
  89                 lambdaFormVmentryField = findFieldInClass(lambdaFormType, "vmentry", memberNameType);
  90 
  91                 ResolvedJavaType methodType = resolveType("Ljava/lang/invoke/ResolvedMethodName;");
  92                 methodField = findFieldInClass(memberNameType, "method", methodType);
  93                 vmtargetField = (HotSpotResolvedJavaField) findFieldInClass(methodType, "vmtarget", resolveType(Character.toString(HotSpotJVMCIRuntime.getHostWordKind().getTypeChar())));
  94 
  95                 ResolvedJavaType callSiteType = resolveType("Ljava/lang/invoke/CallSite;");
  96                 callSiteTargetField = (HotSpotResolvedJavaField) findFieldInClass(callSiteType, "target", methodHandleType);
  97             } catch (Throwable ex) {
  98                 throw new JVMCIError(ex);
  99             }
 100         }
 101 
 102         /**
 103          * Singleton instance lazily initialized via double-checked locking.
 104          */
 105         @NativeImageReinitialize private static volatile Internals instance;
 106 
 107         static Internals instance() {
 108             Internals result = instance;
 109             if (result == null) {
 110                 synchronized (VMState.class) {
 111                     result = instance;
 112                     if (result == null) {
 113                         instance = result = new Internals();
 114                     }
 115                 }
 116             }
 117             return result;
 118         }
 119 
 120     }
 121 
 122 
 123     @Override
 124     public IntrinsicMethod lookupMethodHandleIntrinsic(ResolvedJavaMethod method) {
 125         int intrinsicId = ((HotSpotResolvedJavaMethodImpl) method).intrinsicId();
 126         if (intrinsicId != 0) {
 127             return getMethodHandleIntrinsic(intrinsicId);
 128         }
 129         return null;
 130     }
 131 
 132     public static IntrinsicMethod getMethodHandleIntrinsic(int intrinsicId) {
 133         HotSpotVMConfig config = runtime().getConfig();
 134         if (intrinsicId == config.vmIntrinsicInvokeBasic) {
 135             return IntrinsicMethod.INVOKE_BASIC;
 136         } else if (intrinsicId == config.vmIntrinsicLinkToInterface) {
 137             return IntrinsicMethod.LINK_TO_INTERFACE;
 138         } else if (intrinsicId == config.vmIntrinsicLinkToSpecial) {
 139             return IntrinsicMethod.LINK_TO_SPECIAL;
 140         } else if (intrinsicId == config.vmIntrinsicLinkToStatic) {
 141             return IntrinsicMethod.LINK_TO_STATIC;
 142         } else if (intrinsicId == config.vmIntrinsicLinkToVirtual) {
 143             return IntrinsicMethod.LINK_TO_VIRTUAL;
 144         }
 145         return null;
 146     }
 147 
 148     @Override
 149     public ResolvedJavaMethod resolveInvokeBasicTarget(JavaConstant methodHandle, boolean forceBytecodeGeneration) {
 150         if (methodHandle.isNull()) {
 151             return null;
 152         }
 153 
 154         /* Load non-public field: LambdaForm MethodHandle.form */
 155         Internals internals = Internals.instance();
 156         JavaConstant lambdaForm = constantReflection.readFieldValue(internals.methodHandleFormField, methodHandle);
 157         if (lambdaForm == null || lambdaForm.isNull()) {
 158             return null;
 159         }
 160 
 161         JavaConstant memberName = constantReflection.readFieldValue(internals.lambdaFormVmentryField, lambdaForm);
 162         if (memberName.isNull() && forceBytecodeGeneration) {
 163             compilerToVM().compileToBytecode((HotSpotObjectConstantImpl) lambdaForm);
 164             memberName = constantReflection.readFieldValue(internals.lambdaFormVmentryField, lambdaForm);

 165             assert memberName.isNonNull();
 166         }
 167         JavaConstant method = constantReflection.readFieldValue(internals.methodField, memberName);
 168         return getTargetMethod(method);
 169     }
 170 
 171     @Override
 172     public ResolvedJavaMethod resolveLinkToTarget(JavaConstant memberName) {
 173         if (memberName.isNull()) {
 174             return null;
 175         }
 176         JavaConstant method = constantReflection.readFieldValue(Internals.instance().methodField, memberName);
 177         return getTargetMethod(method);
 178     }
 179 
 180     /**
 181      * Returns the {@link ResolvedJavaMethod} for the method of a java.lang.invoke.MemberName.
 182      */
 183     private static ResolvedJavaMethod getTargetMethod(JavaConstant method) {
 184         if (method == null) {
 185             // If readFieldValue returns NULL the type was wrong
 186             throw new IllegalArgumentException("unexpected type for memberName");
 187         }
 188 

 189         /* Read the ResolvedJavaMethod from the injected field MemberName.method.vmtarget */
 190         return compilerToVM().getResolvedJavaMethod((HotSpotObjectConstantImpl) method, Internals.instance().vmtargetField.getOffset());
 191     }
 192 }
src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotMethodHandleAccessProvider.java
Index Unified diffs Context diffs Sdiffs Frames Patch New Old Previous File Next File