package org.graalvm.compiler.nodes.graphbuilderconf;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.graalvm.compiler.core.common.type.StampPair;
import jdk.vm.ci.meta.JavaType;
import jdk.vm.ci.meta.ResolvedJavaType;
public final class GraphBuilderConfiguration {
public static class Plugins {
private final InvocationPlugins invocationPlugins;
private NodePlugin[] nodePlugins;
private ParameterPlugin[] parameterPlugins;
private TypePlugin[] typePlugins;
private InlineInvokePlugin[] inlineInvokePlugins;
private ClassInitializationPlugin classInitializationPlugin;
private InvokeDynamicPlugin invokeDynamicPlugin;
private ProfilingPlugin profilingPlugin;
public Plugins(Plugins copyFrom, InvocationPlugins invocationPlugins) {
this.invocationPlugins = invocationPlugins != null ? invocationPlugins : new InvocationPlugins(copyFrom.invocationPlugins);
this.nodePlugins = copyFrom.nodePlugins;
this.parameterPlugins = copyFrom.parameterPlugins;
this.typePlugins = copyFrom.typePlugins;
this.inlineInvokePlugins = copyFrom.inlineInvokePlugins;
this.classInitializationPlugin = copyFrom.classInitializationPlugin;
this.invokeDynamicPlugin = copyFrom.invokeDynamicPlugin;
this.profilingPlugin = copyFrom.profilingPlugin;
}
public Plugins(Plugins copyFrom) {
this(copyFrom, null);
}
public Plugins(InvocationPlugins invocationPlugins) {
this.invocationPlugins = invocationPlugins;
this.nodePlugins = new NodePlugin[0];
this.parameterPlugins = new ParameterPlugin[0];
this.typePlugins = new TypePlugin[0];
this.inlineInvokePlugins = new InlineInvokePlugin[0];
}
public InvocationPlugins getInvocationPlugins() {
return invocationPlugins;
}
public NodePlugin[] getNodePlugins() {
return nodePlugins;
}
public void appendNodePlugin(NodePlugin plugin) {
nodePlugins = Arrays.copyOf(nodePlugins, nodePlugins.length + 1);
nodePlugins[nodePlugins.length - 1] = plugin;
}
public void prependNodePlugin(NodePlugin plugin) {
NodePlugin[] newPlugins = new NodePlugin[nodePlugins.length + 1];
System.arraycopy(nodePlugins, 0, newPlugins, 1, nodePlugins.length);
newPlugins[0] = plugin;
nodePlugins = newPlugins;
}
public void clearNodePlugin() {
nodePlugins = new NodePlugin[0];
}
public ParameterPlugin[] getParameterPlugins() {
return parameterPlugins;
}
public void appendParameterPlugin(ParameterPlugin plugin) {
parameterPlugins = Arrays.copyOf(parameterPlugins, parameterPlugins.length + 1);
parameterPlugins[parameterPlugins.length - 1] = plugin;
}
public void prependParameterPlugin(ParameterPlugin plugin) {
ParameterPlugin[] newPlugins = new ParameterPlugin[parameterPlugins.length + 1];
System.arraycopy(parameterPlugins, 0, newPlugins, 1, parameterPlugins.length);
newPlugins[0] = plugin;
parameterPlugins = newPlugins;
}
public TypePlugin[] getTypePlugins() {
return typePlugins;
}
public void appendTypePlugin(TypePlugin plugin) {
typePlugins = Arrays.copyOf(typePlugins, typePlugins.length + 1);
typePlugins[typePlugins.length - 1] = plugin;
}
public void prependTypePlugin(TypePlugin plugin) {
TypePlugin[] newPlugins = new TypePlugin[typePlugins.length + 1];
System.arraycopy(typePlugins, 0, newPlugins, 1, typePlugins.length);
newPlugins[0] = plugin;
typePlugins = newPlugins;
}
public void clearParameterPlugin() {
parameterPlugins = new ParameterPlugin[0];
}
public InlineInvokePlugin[] getInlineInvokePlugins() {
return inlineInvokePlugins;
}
public void appendInlineInvokePlugin(InlineInvokePlugin plugin) {
inlineInvokePlugins = Arrays.copyOf(inlineInvokePlugins, inlineInvokePlugins.length + 1);
inlineInvokePlugins[inlineInvokePlugins.length - 1] = plugin;
}
public void prependInlineInvokePlugin(InlineInvokePlugin plugin) {
InlineInvokePlugin[] newPlugins = new InlineInvokePlugin[inlineInvokePlugins.length + 1];
System.arraycopy(inlineInvokePlugins, 0, newPlugins, 1, inlineInvokePlugins.length);
newPlugins[0] = plugin;
inlineInvokePlugins = newPlugins;
}
public void clearInlineInvokePlugins() {
inlineInvokePlugins = new InlineInvokePlugin[0];
}
public ClassInitializationPlugin getClassInitializationPlugin() {
return classInitializationPlugin;
}
public void setClassInitializationPlugin(ClassInitializationPlugin plugin) {
this.classInitializationPlugin = plugin;
}
public InvokeDynamicPlugin getInvokeDynamicPlugin() {
return invokeDynamicPlugin;
}
public void setInvokeDynamicPlugin(InvokeDynamicPlugin plugin) {
this.invokeDynamicPlugin = plugin;
}
public ProfilingPlugin getProfilingPlugin() {
return profilingPlugin;
}
public void setProfilingPlugin(ProfilingPlugin plugin) {
this.profilingPlugin = plugin;
}
public StampPair getOverridingStamp(GraphBuilderTool b, JavaType type, boolean nonNull) {
for (TypePlugin plugin : getTypePlugins()) {
StampPair stamp = plugin.interceptType(b, type, nonNull);
if (stamp != null) {
return stamp;
}
}
return null;
}
}
private final boolean eagerResolving;
private final boolean unresolvedIsError;
private final BytecodeExceptionMode bytecodeExceptionMode;
private final boolean omitAssertions;
private final List<ResolvedJavaType> skippedExceptionTypes;
private final boolean insertFullInfopoints;
private final boolean trackNodeSourcePosition;
private final boolean retainLocalVariables;
private final Plugins plugins;
private final boolean replaceLocalsWithConstants;
public enum BytecodeExceptionMode {
CheckAll,
OmitAll,
ExplicitOnly,
Profile
}
private GraphBuilderConfiguration(boolean eagerResolving,
boolean unresolvedIsError,
BytecodeExceptionMode bytecodeExceptionMode,
boolean omitAssertions,
boolean insertFullInfopoints,
boolean trackNodeSourcePosition,
boolean retainLocalVariables,
boolean replaceLocalsWithConstants,
List<ResolvedJavaType> skippedExceptionTypes,
Plugins plugins) {
this.eagerResolving = eagerResolving;
this.unresolvedIsError = unresolvedIsError;
this.bytecodeExceptionMode = bytecodeExceptionMode;
this.omitAssertions = omitAssertions;
this.insertFullInfopoints = insertFullInfopoints;
this.trackNodeSourcePosition = trackNodeSourcePosition;
this.retainLocalVariables = retainLocalVariables;
this.replaceLocalsWithConstants = replaceLocalsWithConstants;
this.skippedExceptionTypes = skippedExceptionTypes;
this.plugins = plugins;
}
public GraphBuilderConfiguration copy() {
Plugins newPlugins = new Plugins(plugins);
GraphBuilderConfiguration result = new GraphBuilderConfiguration(
eagerResolving,
unresolvedIsError,
bytecodeExceptionMode,
omitAssertions,
insertFullInfopoints,
trackNodeSourcePosition,
retainLocalVariables,
replaceLocalsWithConstants,
skippedExceptionTypes,
newPlugins);
return result;
}
public GraphBuilderConfiguration withUnresolvedIsError(boolean newUnresolvedIsError) {
return new GraphBuilderConfiguration(
eagerResolving,
newUnresolvedIsError,
bytecodeExceptionMode,
omitAssertions,
insertFullInfopoints,
trackNodeSourcePosition,
retainLocalVariables,
replaceLocalsWithConstants,
skippedExceptionTypes,
plugins);
}
public GraphBuilderConfiguration withEagerResolving(boolean newEagerResolving) {
return new GraphBuilderConfiguration(
newEagerResolving,
unresolvedIsError,
bytecodeExceptionMode,
omitAssertions,
insertFullInfopoints,
trackNodeSourcePosition,
retainLocalVariables,
replaceLocalsWithConstants,
skippedExceptionTypes,
plugins);
}
public GraphBuilderConfiguration withSkippedExceptionTypes(ResolvedJavaType[] newSkippedExceptionTypes) {
return new GraphBuilderConfiguration(
eagerResolving,
unresolvedIsError,
bytecodeExceptionMode,
omitAssertions,
insertFullInfopoints,
trackNodeSourcePosition,
retainLocalVariables,
replaceLocalsWithConstants,
Collections.unmodifiableList(Arrays.asList(newSkippedExceptionTypes)),
plugins);
}
public GraphBuilderConfiguration withBytecodeExceptionMode(BytecodeExceptionMode newBytecodeExceptionMode) {
return new GraphBuilderConfiguration(eagerResolving,
unresolvedIsError,
newBytecodeExceptionMode,
omitAssertions,
insertFullInfopoints,
trackNodeSourcePosition,
retainLocalVariables,
replaceLocalsWithConstants,
skippedExceptionTypes,
plugins);
}
public GraphBuilderConfiguration withOmitAssertions(boolean newOmitAssertions) {
return new GraphBuilderConfiguration(
eagerResolving,
unresolvedIsError,
bytecodeExceptionMode,
newOmitAssertions,
insertFullInfopoints,
trackNodeSourcePosition,
retainLocalVariables,
replaceLocalsWithConstants,
skippedExceptionTypes,
plugins);
}
public GraphBuilderConfiguration withFullInfopoints(boolean newInsertFullInfopoints) {
return new GraphBuilderConfiguration(
eagerResolving,
unresolvedIsError,
bytecodeExceptionMode,
omitAssertions,
newInsertFullInfopoints,
trackNodeSourcePosition,
retainLocalVariables,
replaceLocalsWithConstants,
skippedExceptionTypes,
plugins);
}
public GraphBuilderConfiguration withNodeSourcePosition(boolean newTrackNodeSourcePosition) {
return new GraphBuilderConfiguration(
eagerResolving,
unresolvedIsError,
bytecodeExceptionMode,
omitAssertions,
insertFullInfopoints,
newTrackNodeSourcePosition,
retainLocalVariables,
replaceLocalsWithConstants,
skippedExceptionTypes,
plugins);
}
public GraphBuilderConfiguration withRetainLocalVariables(boolean newRetainLocalVariables) {
return new GraphBuilderConfiguration(
eagerResolving,
unresolvedIsError,
bytecodeExceptionMode,
omitAssertions,
insertFullInfopoints,
trackNodeSourcePosition,
newRetainLocalVariables,
replaceLocalsWithConstants,
skippedExceptionTypes,
plugins);
}
public GraphBuilderConfiguration withReplaceLocalsWithConstants(boolean newReplaceLocalsWithConstants) {
return new GraphBuilderConfiguration(
eagerResolving,
unresolvedIsError,
bytecodeExceptionMode,
omitAssertions,
insertFullInfopoints,
trackNodeSourcePosition,
retainLocalVariables,
newReplaceLocalsWithConstants,
skippedExceptionTypes,
plugins);
}
public List<ResolvedJavaType> getSkippedExceptionTypes() {
return skippedExceptionTypes;
}
public boolean eagerResolving() {
return eagerResolving;
}
public BytecodeExceptionMode getBytecodeExceptionMode() {
return bytecodeExceptionMode;
}
public boolean omitAssertions() {
return omitAssertions;
}
public boolean trackNodeSourcePosition() {
return trackNodeSourcePosition;
}
public boolean retainLocalVariables() {
return retainLocalVariables;
}
public boolean insertFullInfopoints() {
return insertFullInfopoints;
}
public boolean replaceLocalsWithConstants() {
return this.replaceLocalsWithConstants;
}
public static GraphBuilderConfiguration getDefault(Plugins plugins) {
return new GraphBuilderConfiguration(
false,
false,
BytecodeExceptionMode.Profile,
false,
false,
false,
false,
false,
Collections.emptyList(),
plugins);
}
public static GraphBuilderConfiguration getSnippetDefault(Plugins plugins) {
return new GraphBuilderConfiguration(
true,
true,
BytecodeExceptionMode.OmitAll,
false,
false,
false,
false,
false,
Collections.emptyList(),
plugins);
}
public boolean unresolvedIsError() {
return unresolvedIsError;
}
public Plugins getPlugins() {
return plugins;
}
}