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