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