Mercurial > hg > truffle
comparison graal/com.oracle.jvmci.hotspot/src/com/oracle/jvmci/hotspot/HotSpotJVMCIRuntime.java @ 21551:5324104ac4f3
moved com.oracle.graal.hotspot.jvmci classes to com.oracle.jvmci.hotspot module (JBS:GRAAL-53)
author | Doug Simon <doug.simon@oracle.com> |
---|---|
date | Tue, 26 May 2015 17:13:37 +0200 |
parents | |
children | b1530a6cce8c |
comparison
equal
deleted
inserted
replaced
21550:f48a6cea31eb | 21551:5324104ac4f3 |
---|---|
1 /* | |
2 * Copyright (c) 2015, 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 com.oracle.jvmci.hotspot; | |
24 | |
25 import static com.oracle.jvmci.common.UnsafeAccess.*; | |
26 import static com.oracle.jvmci.hotspot.InitTimer.*; | |
27 | |
28 import java.lang.reflect.*; | |
29 import java.util.*; | |
30 | |
31 import com.oracle.graal.api.code.*; | |
32 import com.oracle.graal.api.meta.*; | |
33 import com.oracle.graal.debug.*; | |
34 import com.oracle.graal.options.*; | |
35 import com.oracle.jvmci.common.*; | |
36 import com.oracle.jvmci.hotspot.logging.*; | |
37 import com.oracle.jvmci.runtime.*; | |
38 | |
39 //JaCoCo Exclude | |
40 | |
41 public final class HotSpotJVMCIRuntime implements HotSpotJVMCIRuntimeProvider, HotSpotProxified { | |
42 | |
43 private static final HotSpotJVMCIRuntime instance; | |
44 | |
45 static { | |
46 try (InitTimer t0 = timer("HotSpotJVMCIRuntime.<clinit>")) { | |
47 try (InitTimer t = timer("initialize HotSpotOptions")) { | |
48 HotSpotOptions.initialize(); | |
49 } | |
50 | |
51 try (InitTimer t = timer("HotSpotJVMCIRuntime.<init>")) { | |
52 instance = new HotSpotJVMCIRuntime(); | |
53 } | |
54 | |
55 try (InitTimer t = timer("HotSpotJVMCIRuntime.completeInitialization")) { | |
56 // Why deferred initialization? See comment in completeInitialization(). | |
57 instance.completeInitialization(); | |
58 } | |
59 } | |
60 } | |
61 | |
62 /** | |
63 * Gets the singleton {@link HotSpotJVMCIRuntime} object. | |
64 */ | |
65 public static HotSpotJVMCIRuntime runtime() { | |
66 assert instance != null; | |
67 return instance; | |
68 } | |
69 | |
70 /** | |
71 * Do deferred initialization. | |
72 */ | |
73 public void completeInitialization() { | |
74 TTY.initialize(Options.LogFile.getStream(compilerToVm)); | |
75 | |
76 // Proxies for the VM/Compiler interfaces cannot be initialized | |
77 // in the constructor as proxy creation causes static | |
78 // initializers to be executed for all the types involved in the | |
79 // proxied methods. Some of these static initializers (e.g. in | |
80 // HotSpotMethodData) rely on the static 'instance' field being set | |
81 // to retrieve configuration details. | |
82 CompilerToVM toVM = this.compilerToVm; | |
83 | |
84 if (CountingProxy.ENABLED) { | |
85 toVM = CountingProxy.getProxy(CompilerToVM.class, toVM); | |
86 } | |
87 if (Logger.ENABLED) { | |
88 toVM = LoggingProxy.getProxy(CompilerToVM.class, toVM); | |
89 } | |
90 | |
91 this.compilerToVm = toVM; | |
92 } | |
93 | |
94 public static class Options { | |
95 | |
96 // @formatter:off | |
97 @Option(help = "The JVMCI runtime configuration to use", type = OptionType.Expert) | |
98 public static final OptionValue<String> JVMCIRuntime = new OptionValue<>(""); | |
99 | |
100 @Option(help = "File to which logging is sent. A %p in the name will be replaced with a string identifying the process, usually the process id.", type = OptionType.Expert) | |
101 public static final PrintStreamOption LogFile = new PrintStreamOption(); | |
102 // @formatter:on | |
103 } | |
104 | |
105 public static HotSpotJVMCIBackendFactory findFactory(String architecture) { | |
106 HotSpotJVMCIBackendFactory basic = null; | |
107 HotSpotJVMCIBackendFactory selected = null; | |
108 HotSpotJVMCIBackendFactory nonBasic = null; | |
109 int nonBasicCount = 0; | |
110 | |
111 for (HotSpotJVMCIBackendFactory factory : Services.load(HotSpotJVMCIBackendFactory.class)) { | |
112 if (factory.getArchitecture().equalsIgnoreCase(architecture)) { | |
113 if (factory.getJVMCIRuntimeName().equals(Options.JVMCIRuntime.getValue())) { | |
114 assert selected == null || checkFactoryOverriding(selected, factory); | |
115 selected = factory; | |
116 } | |
117 if (factory.getJVMCIRuntimeName().equals("basic")) { | |
118 assert basic == null || checkFactoryOverriding(basic, factory); | |
119 basic = factory; | |
120 } else { | |
121 nonBasic = factory; | |
122 nonBasicCount++; | |
123 } | |
124 } | |
125 } | |
126 | |
127 if (selected != null) { | |
128 return selected; | |
129 } else { | |
130 if (!Options.JVMCIRuntime.getValue().equals("")) { | |
131 // Fail fast if a non-default value for JVMCIRuntime was specified | |
132 // and the corresponding factory is not available | |
133 throw new JVMCIError("Specified runtime \"%s\" not available for the %s architecture", Options.JVMCIRuntime.getValue(), architecture); | |
134 } else if (nonBasicCount == 1) { | |
135 // If there is exactly one non-basic runtime, select this one. | |
136 return nonBasic; | |
137 } else { | |
138 return basic; | |
139 } | |
140 } | |
141 } | |
142 | |
143 /** | |
144 * Checks that a factory overriding is valid. A factory B can only override/replace a factory A | |
145 * if the B.getClass() is a subclass of A.getClass(). This models the assumption that B is | |
146 * extends the behavior of A and has therefore understood the behavior expected of A. | |
147 * | |
148 * @param baseFactory | |
149 * @param overridingFactory | |
150 */ | |
151 private static boolean checkFactoryOverriding(HotSpotJVMCIBackendFactory baseFactory, HotSpotJVMCIBackendFactory overridingFactory) { | |
152 return baseFactory.getClass().isAssignableFrom(overridingFactory.getClass()); | |
153 } | |
154 | |
155 /** | |
156 * Gets the kind of a word value on the {@linkplain #getHostJVMCIBackend() host} backend. | |
157 */ | |
158 public static Kind getHostWordKind() { | |
159 return instance.getHostJVMCIBackend().getCodeCache().getTarget().wordKind; | |
160 } | |
161 | |
162 /** | |
163 * Reads a klass pointer from a constant object. | |
164 */ | |
165 public static long unsafeReadKlassPointer(Object object) { | |
166 return instance.getCompilerToVM().readUnsafeKlassPointer(object); | |
167 } | |
168 | |
169 /** | |
170 * Reads a word value from a given object. | |
171 */ | |
172 public static long unsafeReadWord(Object object, long offset) { | |
173 if (getHostWordKind() == Kind.Long) { | |
174 return unsafe.getLong(object, offset); | |
175 } | |
176 return unsafe.getInt(object, offset) & 0xFFFFFFFFL; | |
177 } | |
178 | |
179 protected/* final */CompilerToVM compilerToVm; | |
180 | |
181 protected final HotSpotVMConfig config; | |
182 private final JVMCIBackend hostBackend; | |
183 | |
184 /** | |
185 * Graal mirrors are stored as a {@link ClassValue} associated with the {@link Class} of the | |
186 * type. This data structure stores both {@link HotSpotResolvedObjectType} and | |
187 * {@link HotSpotResolvedPrimitiveType} types. | |
188 */ | |
189 private final ClassValue<ResolvedJavaType> graalMirrors = new ClassValue<ResolvedJavaType>() { | |
190 @Override | |
191 protected ResolvedJavaType computeValue(Class<?> javaClass) { | |
192 if (javaClass.isPrimitive()) { | |
193 Kind kind = Kind.fromJavaClass(javaClass); | |
194 return new HotSpotResolvedPrimitiveType(kind); | |
195 } else { | |
196 return new HotSpotResolvedObjectTypeImpl(javaClass); | |
197 } | |
198 } | |
199 }; | |
200 | |
201 private final Map<Class<? extends Architecture>, JVMCIBackend> backends = new HashMap<>(); | |
202 | |
203 private HotSpotJVMCIRuntime() { | |
204 CompilerToVM toVM = new CompilerToVMImpl(); | |
205 compilerToVm = toVM; | |
206 try (InitTimer t = timer("HotSpotVMConfig<init>")) { | |
207 config = new HotSpotVMConfig(compilerToVm); | |
208 } | |
209 | |
210 if (Boolean.valueOf(System.getProperty("graal.printconfig"))) { | |
211 printConfig(config); | |
212 } | |
213 | |
214 String hostArchitecture = config.getHostArchitectureName(); | |
215 | |
216 HotSpotJVMCIBackendFactory factory; | |
217 try (InitTimer t = timer("find factory:", hostArchitecture)) { | |
218 factory = findFactory(hostArchitecture); | |
219 } | |
220 try (InitTimer t = timer("create JVMCI backend:", hostArchitecture)) { | |
221 hostBackend = registerBackend(factory.createJVMCIBackend(this, null)); | |
222 } | |
223 } | |
224 | |
225 private JVMCIBackend registerBackend(JVMCIBackend backend) { | |
226 Class<? extends Architecture> arch = backend.getCodeCache().getTarget().arch.getClass(); | |
227 JVMCIBackend oldValue = backends.put(arch, backend); | |
228 assert oldValue == null : "cannot overwrite existing backend for architecture " + arch.getSimpleName(); | |
229 return backend; | |
230 } | |
231 | |
232 public ResolvedJavaType fromClass(Class<?> javaClass) { | |
233 return graalMirrors.get(javaClass); | |
234 } | |
235 | |
236 private static void printConfig(HotSpotVMConfig config) { | |
237 Field[] fields = config.getClass().getDeclaredFields(); | |
238 Map<String, Field> sortedFields = new TreeMap<>(); | |
239 for (Field f : fields) { | |
240 f.setAccessible(true); | |
241 sortedFields.put(f.getName(), f); | |
242 } | |
243 for (Field f : sortedFields.values()) { | |
244 try { | |
245 Logger.info(String.format("%9s %-40s = %s", f.getType().getSimpleName(), f.getName(), Logger.pretty(f.get(config)))); | |
246 } catch (Exception e) { | |
247 } | |
248 } | |
249 } | |
250 | |
251 public HotSpotVMConfig getConfig() { | |
252 return config; | |
253 } | |
254 | |
255 public CompilerToVM getCompilerToVM() { | |
256 return compilerToVm; | |
257 } | |
258 | |
259 public JavaType lookupType(String name, HotSpotResolvedObjectType accessingType, boolean resolve) { | |
260 Objects.requireNonNull(accessingType, "cannot resolve type without an accessing class"); | |
261 // If the name represents a primitive type we can short-circuit the lookup. | |
262 if (name.length() == 1) { | |
263 Kind kind = Kind.fromPrimitiveOrVoidTypeChar(name.charAt(0)); | |
264 return fromClass(kind.toJavaClass()); | |
265 } | |
266 | |
267 // Resolve non-primitive types in the VM. | |
268 HotSpotResolvedObjectTypeImpl hsAccessingType = (HotSpotResolvedObjectTypeImpl) accessingType; | |
269 final long metaspaceKlass = compilerToVm.lookupType(name, hsAccessingType.mirror(), resolve); | |
270 | |
271 if (metaspaceKlass == 0L) { | |
272 assert resolve == false; | |
273 return HotSpotUnresolvedJavaType.create(this, name); | |
274 } | |
275 return HotSpotResolvedObjectTypeImpl.fromMetaspaceKlass(metaspaceKlass); | |
276 } | |
277 | |
278 public JVMCIBackend getHostJVMCIBackend() { | |
279 return hostBackend; | |
280 } | |
281 | |
282 public <T extends Architecture> JVMCIBackend getJVMCIBackend(Class<T> arch) { | |
283 assert arch != Architecture.class; | |
284 return backends.get(arch); | |
285 } | |
286 | |
287 public Map<Class<? extends Architecture>, JVMCIBackend> getBackends() { | |
288 return Collections.unmodifiableMap(backends); | |
289 } | |
290 | |
291 /** | |
292 * Called from the VM. | |
293 */ | |
294 @SuppressWarnings({"unused", "static-method"}) | |
295 private void compileTheWorld() throws Throwable { | |
296 for (HotSpotVMEventListener l : Services.load(HotSpotVMEventListener.class)) { | |
297 l.notifyCompileTheWorld(); | |
298 } | |
299 } | |
300 | |
301 /** | |
302 * Shuts down the runtime. | |
303 * | |
304 * Called from the VM. | |
305 */ | |
306 @SuppressWarnings({"unused", "static-method"}) | |
307 private void shutdown() throws Exception { | |
308 for (HotSpotVMEventListener l : Services.load(HotSpotVMEventListener.class)) { | |
309 l.notifyShutdown(); | |
310 } | |
311 } | |
312 } |