1 /* 2 * Copyright (c) 2015, 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 java.util.List; 26 import java.util.Set; 27 28 import jdk.vm.ci.code.CompilationRequest; 29 import jdk.vm.ci.common.JVMCIError; 30 import jdk.vm.ci.common.NativeImageReinitialize; 31 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.Option; 32 import jdk.vm.ci.runtime.JVMCICompiler; 33 import jdk.vm.ci.runtime.JVMCICompilerFactory; 34 import jdk.vm.ci.runtime.JVMCIRuntime; 35 import jdk.vm.ci.services.JVMCIPermission; 36 import jdk.vm.ci.services.JVMCIServiceLocator; 37 38 import static jdk.vm.ci.services.Services.IS_IN_NATIVE_IMAGE; 39 40 final class HotSpotJVMCICompilerConfig { 41 42 /** 43 * This factory allows JVMCI initialization to succeed but raises an error if the VM asks JVMCI 44 * to perform a compilation. This allows the reflective parts of the JVMCI API to be used 45 * without requiring a compiler implementation to be available. 46 */ 47 private static class DummyCompilerFactory implements JVMCICompilerFactory, JVMCICompiler { 48 49 @Override 50 public HotSpotCompilationRequestResult compileMethod(CompilationRequest request) { 51 throw new JVMCIError("no JVMCI compiler selected"); 52 } 53 54 @Override 55 public String getCompilerName() { 56 return "null"; 57 } 58 59 @Override 60 public JVMCICompiler createCompiler(JVMCIRuntime runtime) { 61 return this; 62 } 63 } 64 65 /** 66 * Factory of the selected system compiler. 67 */ 68 @NativeImageReinitialize private static JVMCICompilerFactory compilerFactory; 69 70 /** 71 * Gets the selected system compiler factory. 72 * 73 * @return the selected system compiler factory 74 * @throws SecurityException if a security manager is present and it denies 75 * {@link JVMCIPermission} for any {@link JVMCIServiceLocator} loaded by this method 76 */ 77 static JVMCICompilerFactory getCompilerFactory() { 78 if (compilerFactory == null) { 79 JVMCICompilerFactory factory = null; 80 String compilerName = Option.Compiler.getString(); 81 if (compilerName != null) { 82 if (compilerName.isEmpty() || compilerName.equals("null")) { 83 factory = new DummyCompilerFactory(); 84 } else { 85 for (JVMCICompilerFactory f : getJVMCICompilerFactories()) { 86 if (f.getCompilerName().equals(compilerName)) { 87 factory = f; 88 } 89 } 90 if (factory == null) { 91 throw new JVMCIError("JVMCI compiler '%s' not found", compilerName); 92 } 93 } 94 } else { 95 // Auto select a single available compiler 96 for (JVMCICompilerFactory f : getJVMCICompilerFactories()) { 97 if (factory == null) { 98 openJVMCITo(f.getClass().getModule()); 99 factory = f; 100 } else { 101 // Multiple factories seen - cancel auto selection 102 factory = null; 103 break; 104 } 105 } 106 if (factory == null) { 107 factory = new DummyCompilerFactory(); 108 } 109 } 110 factory.onSelection(); 111 compilerFactory = factory; 112 } 113 return compilerFactory; 114 } 115 116 /** 117 * Opens all JVMCI packages to {@code otherModule}. 118 */ 119 private static void openJVMCITo(Module otherModule) { 120 if (!IS_IN_NATIVE_IMAGE) { 121 Module jvmci = HotSpotJVMCICompilerConfig.class.getModule(); 122 if (jvmci != otherModule) { 123 Set<String> packages = jvmci.getPackages(); 124 for (String pkg : packages) { 125 boolean opened = jvmci.isOpen(pkg, otherModule); 126 if (!opened) { 127 jvmci.addOpens(pkg, otherModule); 128 } 129 } 130 } 131 } 132 } 133 134 private static List<JVMCICompilerFactory> getJVMCICompilerFactories() { 135 return JVMCIServiceLocator.getProviders(JVMCICompilerFactory.class); 136 } 137 }