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); | 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); |