1 /*
2 * Copyright (c) 2011, 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 org.graalvm.compiler.nodes.graphbuilderconf;
24
25 import java.util.Arrays;
26
27 import org.graalvm.compiler.core.common.type.StampPair;
28
29 import jdk.vm.ci.meta.JavaType;
30 import jdk.vm.ci.meta.ResolvedJavaType;
31
32 public class GraphBuilderConfiguration {
33
34 public static class Plugins {
35 private final InvocationPlugins invocationPlugins;
36 private NodePlugin[] nodePlugins;
37 private ParameterPlugin[] parameterPlugins;
38 private TypePlugin[] typePlugins;
39 private InlineInvokePlugin[] inlineInvokePlugins;
40 private LoopExplosionPlugin loopExplosionPlugin;
41 private ClassInitializationPlugin classInitializationPlugin;
42 private ProfilingPlugin profilingPlugin;
43
44 /**
45 * Creates a copy of a given set of plugins. The {@link InvocationPlugins} in
46 * {@code copyFrom} become the {@linkplain InvocationPlugins#getParent() default}
47 * {@linkplain #getInvocationPlugins() invocation plugins} in this object.
48 */
49 public Plugins(Plugins copyFrom) {
50 this.invocationPlugins = new InvocationPlugins(copyFrom.invocationPlugins);
51 this.nodePlugins = copyFrom.nodePlugins;
52 this.parameterPlugins = copyFrom.parameterPlugins;
53 this.typePlugins = copyFrom.typePlugins;
54 this.inlineInvokePlugins = copyFrom.inlineInvokePlugins;
55 this.loopExplosionPlugin = copyFrom.loopExplosionPlugin;
56 this.classInitializationPlugin = copyFrom.classInitializationPlugin;
57 this.profilingPlugin = copyFrom.profilingPlugin;
58 }
59
60 /**
61 * Creates a new set of plugins.
62 *
63 * @param invocationPlugins the {@linkplain #getInvocationPlugins() invocation plugins} in
64 * this object
65 */
66 public Plugins(InvocationPlugins invocationPlugins) {
67 this.invocationPlugins = invocationPlugins;
68 this.nodePlugins = new NodePlugin[0];
69 this.parameterPlugins = new ParameterPlugin[0];
70 this.typePlugins = new TypePlugin[0];
71 this.inlineInvokePlugins = new InlineInvokePlugin[0];
72 }
73
74 public InvocationPlugins getInvocationPlugins() {
75 return invocationPlugins;
76 }
150 public void clearInlineInvokePlugins() {
151 inlineInvokePlugins = new InlineInvokePlugin[0];
152 }
153
154 public LoopExplosionPlugin getLoopExplosionPlugin() {
155 return loopExplosionPlugin;
156 }
157
158 public void setLoopExplosionPlugin(LoopExplosionPlugin plugin) {
159 this.loopExplosionPlugin = plugin;
160 }
161
162 public ClassInitializationPlugin getClassInitializationPlugin() {
163 return classInitializationPlugin;
164 }
165
166 public void setClassInitializationPlugin(ClassInitializationPlugin plugin) {
167 this.classInitializationPlugin = plugin;
168 }
169
170 public ProfilingPlugin getProfilingPlugin() {
171 return profilingPlugin;
172 }
173
174 public void setProfilingPlugin(ProfilingPlugin plugin) {
175 this.profilingPlugin = plugin;
176 }
177
178 public StampPair getOverridingStamp(GraphBuilderTool b, JavaType type, boolean nonNull) {
179 for (TypePlugin plugin : getTypePlugins()) {
180 StampPair stamp = plugin.interceptType(b, type, nonNull);
181 if (stamp != null) {
182 return stamp;
183 }
184 }
185 return null;
186 }
187 }
188
189 private static final ResolvedJavaType[] EMPTY = new ResolvedJavaType[]{};
190
191 private final boolean eagerResolving;
192 private final BytecodeExceptionMode bytecodeExceptionMode;
193 private final boolean omitAssertions;
194 private final ResolvedJavaType[] skippedExceptionTypes;
195 private final boolean insertFullInfopoints;
196 private final boolean trackNodeSourcePosition;
197 private final Plugins plugins;
198
199 public enum BytecodeExceptionMode {
200 /**
201 * This mode always explicitly checks for exceptions.
202 */
203 CheckAll,
204 /**
205 * This mode omits all explicit exception edges.
206 */
207 OmitAll,
208 /**
209 * This mode omits exception edges at invokes, but not for implicit null checks or bounds
210 * checks.
211 */
212 ExplicitOnly,
213 /**
214 * This mode uses profiling information to decide whether to use explicit exception edges.
215 */
216 Profile
217 }
218
219 protected GraphBuilderConfiguration(boolean eagerResolving, BytecodeExceptionMode bytecodeExceptionMode, boolean omitAssertions, boolean insertFullInfopoints,
220 boolean trackNodeSourcePosition, ResolvedJavaType[] skippedExceptionTypes,
221 Plugins plugins) {
222 this.eagerResolving = eagerResolving;
223 this.bytecodeExceptionMode = bytecodeExceptionMode;
224 this.omitAssertions = omitAssertions;
225 this.insertFullInfopoints = insertFullInfopoints;
226 this.trackNodeSourcePosition = trackNodeSourcePosition;
227 this.skippedExceptionTypes = skippedExceptionTypes;
228 this.plugins = plugins;
229 }
230
231 /**
232 * Creates a copy of this configuration with all its plugins. The {@link InvocationPlugins} in
233 * this configuration become the {@linkplain InvocationPlugins#getParent() parent} of the
234 * {@link InvocationPlugins} in the copy.
235 */
236 public GraphBuilderConfiguration copy() {
237 Plugins newPlugins = new Plugins(plugins);
238 GraphBuilderConfiguration result = new GraphBuilderConfiguration(eagerResolving, bytecodeExceptionMode, omitAssertions, insertFullInfopoints, trackNodeSourcePosition, skippedExceptionTypes,
239 newPlugins);
240 return result;
241 }
242
243 public GraphBuilderConfiguration withEagerResolving(boolean newEagerResolving) {
244 return new GraphBuilderConfiguration(newEagerResolving, bytecodeExceptionMode, omitAssertions, insertFullInfopoints, trackNodeSourcePosition, skippedExceptionTypes, plugins);
245 }
246
247 public GraphBuilderConfiguration withSkippedExceptionTypes(ResolvedJavaType[] newSkippedExceptionTypes) {
248 return new GraphBuilderConfiguration(eagerResolving, bytecodeExceptionMode, omitAssertions, insertFullInfopoints, trackNodeSourcePosition, newSkippedExceptionTypes, plugins);
249 }
250
251 public GraphBuilderConfiguration withBytecodeExceptionMode(BytecodeExceptionMode newBytecodeExceptionMode) {
252 return new GraphBuilderConfiguration(eagerResolving, newBytecodeExceptionMode, omitAssertions, insertFullInfopoints, trackNodeSourcePosition, skippedExceptionTypes, plugins);
253 }
254
255 public GraphBuilderConfiguration withOmitAssertions(boolean newOmitAssertions) {
256 return new GraphBuilderConfiguration(eagerResolving, bytecodeExceptionMode, newOmitAssertions, insertFullInfopoints, trackNodeSourcePosition, skippedExceptionTypes, plugins);
257 }
258
259 public GraphBuilderConfiguration withFullInfopoints(boolean newInsertFullInfopoints) {
260 ResolvedJavaType[] newSkippedExceptionTypes = skippedExceptionTypes == EMPTY ? EMPTY : Arrays.copyOf(skippedExceptionTypes, skippedExceptionTypes.length);
261 return new GraphBuilderConfiguration(eagerResolving, bytecodeExceptionMode, omitAssertions, newInsertFullInfopoints, trackNodeSourcePosition, newSkippedExceptionTypes, plugins);
262 }
263
264 public GraphBuilderConfiguration withNodeSourcePosition(boolean newTrackNodeSourcePosition) {
265 ResolvedJavaType[] newSkippedExceptionTypes = skippedExceptionTypes == EMPTY ? EMPTY : Arrays.copyOf(skippedExceptionTypes, skippedExceptionTypes.length);
266 return new GraphBuilderConfiguration(eagerResolving, bytecodeExceptionMode, omitAssertions, insertFullInfopoints, newTrackNodeSourcePosition, newSkippedExceptionTypes, plugins);
267 }
268
269 public ResolvedJavaType[] getSkippedExceptionTypes() {
270 return skippedExceptionTypes;
271 }
272
273 public boolean eagerResolving() {
274 return eagerResolving;
275 }
276
277 public BytecodeExceptionMode getBytecodeExceptionMode() {
278 return bytecodeExceptionMode;
279 }
280
281 public boolean omitAssertions() {
282 return omitAssertions;
283 }
284
285 public boolean trackNodeSourcePosition() {
286 return trackNodeSourcePosition;
287 }
288
289 public boolean insertFullInfopoints() {
290 return insertFullInfopoints;
291 }
292
293 public static GraphBuilderConfiguration getDefault(Plugins plugins) {
294 return new GraphBuilderConfiguration(false, BytecodeExceptionMode.Profile, false, false, false, EMPTY, plugins);
295 }
296
297 public static GraphBuilderConfiguration getSnippetDefault(Plugins plugins) {
298 return new GraphBuilderConfiguration(true, BytecodeExceptionMode.OmitAll, false, false, false, EMPTY, plugins);
299 }
300
301 /**
302 * Returns {@code true} if it is an error for a class/field/method resolution to fail. The
303 * default is the same result as returned by {@link #eagerResolving()}. However, it may be
304 * overridden to allow failure even when {@link #eagerResolving} is {@code true}.
305 */
306 public boolean unresolvedIsError() {
307 return eagerResolving;
308 }
309
310 public Plugins getPlugins() {
311 return plugins;
312 }
313 }
|
1 /*
2 * Copyright (c) 2011, 2017, 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 org.graalvm.compiler.nodes.graphbuilderconf;
24
25 import java.util.Arrays;
26
27 import org.graalvm.compiler.core.common.type.StampPair;
28
29 import jdk.vm.ci.meta.JavaType;
30 import jdk.vm.ci.meta.ResolvedJavaType;
31
32 public class GraphBuilderConfiguration {
33
34 public static class Plugins {
35 private final InvocationPlugins invocationPlugins;
36 private NodePlugin[] nodePlugins;
37 private ParameterPlugin[] parameterPlugins;
38 private TypePlugin[] typePlugins;
39 private InlineInvokePlugin[] inlineInvokePlugins;
40 private LoopExplosionPlugin loopExplosionPlugin;
41 private ClassInitializationPlugin classInitializationPlugin;
42 private InvokeDynamicPlugin invokeDynamicPlugin;
43 private ProfilingPlugin profilingPlugin;
44
45 /**
46 * Creates a copy of a given set of plugins. The {@link InvocationPlugins} in
47 * {@code copyFrom} become the {@linkplain InvocationPlugins#getParent() default}
48 * {@linkplain #getInvocationPlugins() invocation plugins} in this object.
49 */
50 public Plugins(Plugins copyFrom) {
51 this.invocationPlugins = new InvocationPlugins(copyFrom.invocationPlugins);
52 this.nodePlugins = copyFrom.nodePlugins;
53 this.parameterPlugins = copyFrom.parameterPlugins;
54 this.typePlugins = copyFrom.typePlugins;
55 this.inlineInvokePlugins = copyFrom.inlineInvokePlugins;
56 this.loopExplosionPlugin = copyFrom.loopExplosionPlugin;
57 this.classInitializationPlugin = copyFrom.classInitializationPlugin;
58 this.invokeDynamicPlugin = copyFrom.invokeDynamicPlugin;
59 this.profilingPlugin = copyFrom.profilingPlugin;
60 }
61
62 /**
63 * Creates a new set of plugins.
64 *
65 * @param invocationPlugins the {@linkplain #getInvocationPlugins() invocation plugins} in
66 * this object
67 */
68 public Plugins(InvocationPlugins invocationPlugins) {
69 this.invocationPlugins = invocationPlugins;
70 this.nodePlugins = new NodePlugin[0];
71 this.parameterPlugins = new ParameterPlugin[0];
72 this.typePlugins = new TypePlugin[0];
73 this.inlineInvokePlugins = new InlineInvokePlugin[0];
74 }
75
76 public InvocationPlugins getInvocationPlugins() {
77 return invocationPlugins;
78 }
152 public void clearInlineInvokePlugins() {
153 inlineInvokePlugins = new InlineInvokePlugin[0];
154 }
155
156 public LoopExplosionPlugin getLoopExplosionPlugin() {
157 return loopExplosionPlugin;
158 }
159
160 public void setLoopExplosionPlugin(LoopExplosionPlugin plugin) {
161 this.loopExplosionPlugin = plugin;
162 }
163
164 public ClassInitializationPlugin getClassInitializationPlugin() {
165 return classInitializationPlugin;
166 }
167
168 public void setClassInitializationPlugin(ClassInitializationPlugin plugin) {
169 this.classInitializationPlugin = plugin;
170 }
171
172 public InvokeDynamicPlugin getInvokeDynamicPlugin() {
173 return invokeDynamicPlugin;
174 }
175
176 public void setInvokeDynamicPlugin(InvokeDynamicPlugin plugin) {
177 this.invokeDynamicPlugin = plugin;
178 }
179
180 public ProfilingPlugin getProfilingPlugin() {
181 return profilingPlugin;
182 }
183
184 public void setProfilingPlugin(ProfilingPlugin plugin) {
185 this.profilingPlugin = plugin;
186 }
187
188 public StampPair getOverridingStamp(GraphBuilderTool b, JavaType type, boolean nonNull) {
189 for (TypePlugin plugin : getTypePlugins()) {
190 StampPair stamp = plugin.interceptType(b, type, nonNull);
191 if (stamp != null) {
192 return stamp;
193 }
194 }
195 return null;
196 }
197 }
198
199 private static final ResolvedJavaType[] EMPTY = new ResolvedJavaType[]{};
200
201 private final boolean eagerResolving;
202 private final boolean unresolvedIsError;
203 private final BytecodeExceptionMode bytecodeExceptionMode;
204 private final boolean omitAssertions;
205 private final ResolvedJavaType[] skippedExceptionTypes;
206 private final boolean insertFullInfopoints;
207 private final boolean trackNodeSourcePosition;
208 private final Plugins plugins;
209
210 public enum BytecodeExceptionMode {
211 /**
212 * This mode always explicitly checks for exceptions.
213 */
214 CheckAll,
215 /**
216 * This mode omits all explicit exception edges.
217 */
218 OmitAll,
219 /**
220 * This mode omits exception edges at invokes, but not for implicit null checks or bounds
221 * checks.
222 */
223 ExplicitOnly,
224 /**
225 * This mode uses profiling information to decide whether to use explicit exception edges.
226 */
227 Profile
228 }
229
230 protected GraphBuilderConfiguration(boolean eagerResolving, boolean unresolvedIsError, BytecodeExceptionMode bytecodeExceptionMode, boolean omitAssertions, boolean insertFullInfopoints,
231 boolean trackNodeSourcePosition, ResolvedJavaType[] skippedExceptionTypes,
232 Plugins plugins) {
233 this.eagerResolving = eagerResolving;
234 this.unresolvedIsError = unresolvedIsError;
235 this.bytecodeExceptionMode = bytecodeExceptionMode;
236 this.omitAssertions = omitAssertions;
237 this.insertFullInfopoints = insertFullInfopoints;
238 this.trackNodeSourcePosition = trackNodeSourcePosition;
239 this.skippedExceptionTypes = skippedExceptionTypes;
240 this.plugins = plugins;
241 }
242
243 /**
244 * Creates a copy of this configuration with all its plugins. The {@link InvocationPlugins} in
245 * this configuration become the {@linkplain InvocationPlugins#getParent() parent} of the
246 * {@link InvocationPlugins} in the copy.
247 */
248 public GraphBuilderConfiguration copy() {
249 Plugins newPlugins = new Plugins(plugins);
250 GraphBuilderConfiguration result = new GraphBuilderConfiguration(eagerResolving, unresolvedIsError, bytecodeExceptionMode, omitAssertions, insertFullInfopoints, trackNodeSourcePosition,
251 skippedExceptionTypes, newPlugins);
252 return result;
253 }
254
255 /**
256 * Set the {@link #unresolvedIsError} flag. This flag can be set independently from
257 * {@link #eagerResolving}, i.e., even if eager resolving fails execution is assumed to be
258 * valid. This allows us for example to process unresolved types/methods/fields even when
259 * eagerly resolving elements.
260 */
261 public GraphBuilderConfiguration withUnresolvedIsError(boolean newUnresolvedIsError) {
262 return new GraphBuilderConfiguration(eagerResolving, newUnresolvedIsError, bytecodeExceptionMode, omitAssertions, insertFullInfopoints, trackNodeSourcePosition, skippedExceptionTypes,
263 plugins);
264 }
265
266 public GraphBuilderConfiguration withEagerResolving(boolean newEagerResolving) {
267 return new GraphBuilderConfiguration(newEagerResolving, unresolvedIsError, bytecodeExceptionMode, omitAssertions, insertFullInfopoints, trackNodeSourcePosition, skippedExceptionTypes,
268 plugins);
269 }
270
271 public GraphBuilderConfiguration withSkippedExceptionTypes(ResolvedJavaType[] newSkippedExceptionTypes) {
272 return new GraphBuilderConfiguration(eagerResolving, unresolvedIsError, bytecodeExceptionMode, omitAssertions, insertFullInfopoints, trackNodeSourcePosition, newSkippedExceptionTypes,
273 plugins);
274 }
275
276 public GraphBuilderConfiguration withBytecodeExceptionMode(BytecodeExceptionMode newBytecodeExceptionMode) {
277 return new GraphBuilderConfiguration(eagerResolving, unresolvedIsError, newBytecodeExceptionMode, omitAssertions, insertFullInfopoints, trackNodeSourcePosition, skippedExceptionTypes,
278 plugins);
279 }
280
281 public GraphBuilderConfiguration withOmitAssertions(boolean newOmitAssertions) {
282 return new GraphBuilderConfiguration(eagerResolving, unresolvedIsError, bytecodeExceptionMode, newOmitAssertions, insertFullInfopoints, trackNodeSourcePosition, skippedExceptionTypes,
283 plugins);
284 }
285
286 public GraphBuilderConfiguration withFullInfopoints(boolean newInsertFullInfopoints) {
287 ResolvedJavaType[] newSkippedExceptionTypes = skippedExceptionTypes == EMPTY ? EMPTY : Arrays.copyOf(skippedExceptionTypes, skippedExceptionTypes.length);
288 return new GraphBuilderConfiguration(eagerResolving, unresolvedIsError, bytecodeExceptionMode, omitAssertions, newInsertFullInfopoints, trackNodeSourcePosition, newSkippedExceptionTypes,
289 plugins);
290 }
291
292 public GraphBuilderConfiguration withNodeSourcePosition(boolean newTrackNodeSourcePosition) {
293 ResolvedJavaType[] newSkippedExceptionTypes = skippedExceptionTypes == EMPTY ? EMPTY : Arrays.copyOf(skippedExceptionTypes, skippedExceptionTypes.length);
294 return new GraphBuilderConfiguration(eagerResolving, unresolvedIsError, bytecodeExceptionMode, omitAssertions, insertFullInfopoints, newTrackNodeSourcePosition, newSkippedExceptionTypes,
295 plugins);
296 }
297
298 public ResolvedJavaType[] getSkippedExceptionTypes() {
299 return skippedExceptionTypes;
300 }
301
302 public boolean eagerResolving() {
303 return eagerResolving;
304 }
305
306 public BytecodeExceptionMode getBytecodeExceptionMode() {
307 return bytecodeExceptionMode;
308 }
309
310 public boolean omitAssertions() {
311 return omitAssertions;
312 }
313
314 public boolean trackNodeSourcePosition() {
315 return trackNodeSourcePosition;
316 }
317
318 public boolean insertFullInfopoints() {
319 return insertFullInfopoints;
320 }
321
322 public static GraphBuilderConfiguration getDefault(Plugins plugins) {
323 return new GraphBuilderConfiguration(false, false, BytecodeExceptionMode.Profile, false, false, false, EMPTY, plugins);
324 }
325
326 public static GraphBuilderConfiguration getSnippetDefault(Plugins plugins) {
327 return new GraphBuilderConfiguration(true, true, BytecodeExceptionMode.OmitAll, false, false, false, EMPTY, plugins);
328 }
329
330 /** Returns {@code true} if it is an error for a class/field/method resolution to fail. */
331 public boolean unresolvedIsError() {
332 return unresolvedIsError;
333 }
334
335 public Plugins getPlugins() {
336 return plugins;
337 }
338 }
|