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 private final String reason; 50 51 DummyCompilerFactory(String reason) { 52 this.reason = reason; 53 } 54 55 @Override 56 public HotSpotCompilationRequestResult compileMethod(CompilationRequest request) { 57 throw new JVMCIError("no JVMCI compiler selected: " + reason); 58 } 59 60 @Override 61 public String getCompilerName() { 62 return "null"; 63 } 64 65 @Override 66 public JVMCICompiler createCompiler(JVMCIRuntime runtime) { 67 return this; 68 } 69 } 70 71 /** 72 * Factory of the selected system compiler. 73 */ 74 @NativeImageReinitialize private static JVMCICompilerFactory compilerFactory; 75 76 /** 77 * Gets the selected system compiler factory. 78 * 79 * @return the selected system compiler factory 80 * @throws SecurityException if a security manager is present and it denies 81 * {@link JVMCIPermission} for any {@link JVMCIServiceLocator} loaded by this method 82 */ 83 static JVMCICompilerFactory getCompilerFactory() { 84 if (compilerFactory == null) { 85 JVMCICompilerFactory factory = null; 86 String compilerName = Option.Compiler.getString(); 87 if (compilerName != null) { 88 if (compilerName.isEmpty()) { 89 factory = new DummyCompilerFactory(" empty \"\" is specified"); 90 } else if (compilerName.equals("null")) { 91 factory = new DummyCompilerFactory("\"null\" is specified"); 92 } else { 93 for (JVMCICompilerFactory f : getJVMCICompilerFactories()) { 94 if (f.getCompilerName().equals(compilerName)) { 95 factory = f; 96 } 97 } 98 if (factory == null) { 99 throw new JVMCIError("JVMCI compiler '%s' not found", compilerName); 100 } 101 } 102 } else { 103 // Auto select a single available compiler 104 String reason = "default compiler is not found"; 105 for (JVMCICompilerFactory f : getJVMCICompilerFactories()) { 106 if (factory == null) { 107 openJVMCITo(f.getClass().getModule()); 108 factory = f; 109 } else { 110 // Multiple factories seen - cancel auto selection 111 reason = "multiple factories seen: \"" + factory.getCompilerName() + "\" and \"" + f.getCompilerName() + "\""; 112 factory = null; 113 break; 114 } 115 } 116 if (factory == null) { 117 factory = new DummyCompilerFactory(reason); 118 } 119 } 120 factory.onSelection(); 121 compilerFactory = factory; 122 } 123 return compilerFactory; 124 } 125 126 /** 127 * Opens all JVMCI packages to {@code otherModule}. 128 */ 129 private static void openJVMCITo(Module otherModule) { 130 if (!IS_IN_NATIVE_IMAGE) { 131 Module jvmci = HotSpotJVMCICompilerConfig.class.getModule(); 132 if (jvmci != otherModule) { 133 Set<String> packages = jvmci.getPackages(); 134 for (String pkg : packages) { 135 boolean opened = jvmci.isOpen(pkg, otherModule); 136 if (!opened) { 137 jvmci.addOpens(pkg, otherModule); 138 } 139 } 140 } 141 } 142 } 143 144 private static List<JVMCICompilerFactory> getJVMCICompilerFactories() { 145 return JVMCIServiceLocator.getProviders(JVMCICompilerFactory.class); 146 } 147 }