src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.serviceprovider/src/org/graalvm/compiler/serviceprovider/GraalServices.java
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
parent 54914 9feb4852536f
child 58679 9c3209ff7550
equal deleted inserted replaced
58677:13588c901957 58678:9cf78a70fa4f
    40 import java.util.function.Supplier;
    40 import java.util.function.Supplier;
    41 
    41 
    42 import org.graalvm.compiler.serviceprovider.SpeculationReasonGroup.SpeculationContextObject;
    42 import org.graalvm.compiler.serviceprovider.SpeculationReasonGroup.SpeculationContextObject;
    43 
    43 
    44 import jdk.vm.ci.code.BytecodePosition;
    44 import jdk.vm.ci.code.BytecodePosition;
       
    45 import jdk.vm.ci.code.VirtualObject;
    45 import jdk.vm.ci.meta.ResolvedJavaField;
    46 import jdk.vm.ci.meta.ResolvedJavaField;
    46 import jdk.vm.ci.meta.ResolvedJavaMethod;
    47 import jdk.vm.ci.meta.ResolvedJavaMethod;
    47 import jdk.vm.ci.meta.ResolvedJavaType;
    48 import jdk.vm.ci.meta.ResolvedJavaType;
    48 import jdk.vm.ci.meta.SpeculationLog.SpeculationReason;
    49 import jdk.vm.ci.meta.SpeculationLog.SpeculationReason;
    49 import jdk.vm.ci.meta.SpeculationLog.SpeculationReasonEncoding;
    50 import jdk.vm.ci.meta.SpeculationLog.SpeculationReasonEncoding;
    86 
    87 
    87         if (IS_BUILDING_NATIVE_IMAGE) {
    88         if (IS_BUILDING_NATIVE_IMAGE) {
    88             synchronized (servicesCache) {
    89             synchronized (servicesCache) {
    89                 ArrayList<S> providersList = new ArrayList<>();
    90                 ArrayList<S> providersList = new ArrayList<>();
    90                 for (S provider : providers) {
    91                 for (S provider : providers) {
    91                     providersList.add(provider);
    92                     Module module = provider.getClass().getModule();
       
    93                     if (isHotSpotGraalModule(module.getName())) {
       
    94                         providersList.add(provider);
       
    95                     }
    92                 }
    96                 }
    93                 providers = providersList;
    97                 providers = providersList;
    94                 servicesCache.put(service, providersList);
    98                 servicesCache.put(service, providersList);
    95                 return providers;
    99                 return providers;
    96             }
   100             }
    97         }
   101         }
    98 
   102 
    99         return providers;
   103         return providers;
   100     }
   104     }
   101 
   105 
       
   106     /**
       
   107      * Determines if the module named by {@code name} is part of Graal when it is configured as a
       
   108      * HotSpot JIT compiler.
       
   109      */
       
   110     private static boolean isHotSpotGraalModule(String name) {
       
   111         if (name != null) {
       
   112             return name.equals("jdk.internal.vm.compiler") ||
       
   113                             name.equals("jdk.internal.vm.compiler.management") ||
       
   114                             name.equals("com.oracle.graal.graal_enterprise");
       
   115         }
       
   116         return false;
       
   117     }
       
   118 
   102     protected static <S> Iterable<S> load0(Class<S> service) {
   119     protected static <S> Iterable<S> load0(Class<S> service) {
   103         Iterable<S> iterable = ServiceLoader.load(service, GraalServices.class.getClassLoader());
   120         Module module = GraalServices.class.getModule();
       
   121         // Graal cannot know all the services used by another module
       
   122         // (e.g. enterprise) so dynamically register the service use now.
       
   123         if (!module.canUse(service)) {
       
   124             module.addUses(service);
       
   125         }
       
   126 
       
   127         ModuleLayer layer = module.getLayer();
       
   128         Iterable<S> iterable = ServiceLoader.load(layer, service);
   104         return new Iterable<>() {
   129         return new Iterable<>() {
   105             @Override
   130             @Override
   106             public Iterator<S> iterator() {
   131             public Iterator<S> iterator() {
   107                 Iterator<S> iterator = iterable.iterator();
   132                 Iterator<S> iterator = iterable.iterator();
   108                 return new Iterator<>() {
   133                 return new Iterator<>() {
   133      * opened all its packages to the module defining {@link GraalServices}.
   158      * opened all its packages to the module defining {@link GraalServices}.
   134      *
   159      *
   135      * @param other all JVMCI packages will be opened to the module defining this class
   160      * @param other all JVMCI packages will be opened to the module defining this class
   136      */
   161      */
   137     static void openJVMCITo(Class<?> other) {
   162     static void openJVMCITo(Class<?> other) {
   138         if (IS_IN_NATIVE_IMAGE) return;
   163         if (IS_IN_NATIVE_IMAGE) {
       
   164             return;
       
   165         }
   139 
   166 
   140         Module jvmciModule = JVMCI_MODULE;
   167         Module jvmciModule = JVMCI_MODULE;
   141         Module otherModule = other.getModule();
   168         Module otherModule = other.getModule();
   142         if (jvmciModule != otherModule) {
   169         if (jvmciModule != otherModule) {
   143             for (String pkg : jvmciModule.getPackages()) {
   170             for (String pkg : jvmciModule.getPackages()) {
   207     /**
   234     /**
   208      * Determines if invoking {@link Object#toString()} on an instance of {@code c} will only run
   235      * Determines if invoking {@link Object#toString()} on an instance of {@code c} will only run
   209      * trusted code.
   236      * trusted code.
   210      */
   237      */
   211     public static boolean isToStringTrusted(Class<?> c) {
   238     public static boolean isToStringTrusted(Class<?> c) {
       
   239         if (IS_IN_NATIVE_IMAGE) {
       
   240             return true;
       
   241         }
       
   242 
   212         Module module = c.getModule();
   243         Module module = c.getModule();
   213         Module jvmciModule = JVMCI_MODULE;
   244         Module jvmciModule = JVMCI_MODULE;
   214         assert jvmciModule.getPackages().contains("jdk.vm.ci.runtime");
   245         assert jvmciModule.getPackages().contains("jdk.vm.ci.runtime");
   215         if (module == jvmciModule || jvmciModule.isOpen(JVMCI_RUNTIME_PACKAGE, module)) {
   246         if (module == jvmciModule || jvmciModule.isOpen(JVMCI_RUNTIME_PACKAGE, module)) {
   216             // Can access non-statically-exported package in JVMCI
   247             // Can access non-statically-exported package in JVMCI
   517      * {@code double}.
   548      * {@code double}.
   518      */
   549      */
   519     public static double fma(double a, double b, double c) {
   550     public static double fma(double a, double b, double c) {
   520         return Math.fma(a, b, c);
   551         return Math.fma(a, b, c);
   521     }
   552     }
       
   553 
       
   554     public static VirtualObject createVirtualObject(ResolvedJavaType type, int id, boolean isAutoBox) {
       
   555         return VirtualObject.get(type, id, isAutoBox);
       
   556     }
   522 }
   557 }