HotSpotJVMCICompilerConfig.java revision 12772:fbb9c8026495
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 */
23package jdk.vm.ci.hotspot;
24
25import jdk.vm.ci.code.CompilationRequest;
26import jdk.vm.ci.common.JVMCIError;
27import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.Option;
28import jdk.vm.ci.runtime.JVMCICompiler;
29import jdk.vm.ci.runtime.JVMCICompilerFactory;
30import jdk.vm.ci.runtime.JVMCIRuntime;
31import jdk.vm.ci.services.JVMCIPermission;
32import jdk.vm.ci.services.JVMCIServiceLocator;
33import jdk.vm.ci.services.internal.ReflectionAccessJDK;
34
35final 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 "null";
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                if (compilerName.isEmpty() || compilerName.equals("null")) {
77                    factory = new DummyCompilerFactory();
78                } else {
79                    for (JVMCICompilerFactory f : JVMCIServiceLocator.getProviders(JVMCICompilerFactory.class)) {
80                        if (f.getCompilerName().equals(compilerName)) {
81                            factory = f;
82                        }
83                    }
84                    if (factory == null) {
85                        throw new JVMCIError("JVMCI compiler '%s' not found", compilerName);
86                    }
87                }
88            } else {
89                // Auto select a single available compiler
90                for (JVMCICompilerFactory f : JVMCIServiceLocator.getProviders(JVMCICompilerFactory.class)) {
91                    if (factory == null) {
92                        ReflectionAccessJDK.openJVMCITo(f.getClass());
93                        factory = f;
94                    } else {
95                        // Multiple factories seen - cancel auto selection
96                        factory = null;
97                        break;
98                    }
99                }
100                if (factory == null) {
101                    factory = new DummyCompilerFactory();
102                }
103            }
104            factory.onSelection();
105            compilerFactory = factory;
106        }
107        return compilerFactory;
108    }
109}
110