package com.oracle.svm.truffle.api;
import static com.oracle.svm.core.util.VMError.shouldNotReachHere;
import org.graalvm.collections.EconomicMap;
import org.graalvm.compiler.bytecode.BytecodeProvider;
import org.graalvm.compiler.graph.SourceLanguagePositionProvider;
import org.graalvm.compiler.nodes.EncodedGraph;
import org.graalvm.compiler.nodes.StructuredGraph;
import org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin;
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
import org.graalvm.compiler.nodes.graphbuilderconf.LoopExplosionPlugin;
import org.graalvm.compiler.nodes.graphbuilderconf.MethodSubstitutionPlugin;
import org.graalvm.compiler.nodes.graphbuilderconf.NodePlugin;
import org.graalvm.compiler.nodes.graphbuilderconf.ParameterPlugin;
import org.graalvm.compiler.nodes.spi.CoreProviders;
import org.graalvm.compiler.replacements.PEGraphDecoder;
import com.oracle.svm.core.graal.meta.SharedRuntimeMethod;
import com.oracle.svm.graal.GraalSupport;
import jdk.vm.ci.code.Architecture;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import java.util.concurrent.ConcurrentHashMap;
public class SubstratePEGraphDecoder extends PEGraphDecoder {
private final EconomicMap<ResolvedJavaMethod, EncodedGraph> graphCache = EconomicMap.create();
public SubstratePEGraphDecoder(Architecture architecture, StructuredGraph graph, CoreProviders providers, LoopExplosionPlugin loopExplosionPlugin, InvocationPlugins invocationPlugins,
InlineInvokePlugin[] inlineInvokePlugins, ParameterPlugin parameterPlugin, NodePlugin[] nodePlugins, ResolvedJavaMethod peRootForInlining,
SourceLanguagePositionProvider sourceLanguagePosition, ConcurrentHashMap<SpecialCallTargetCacheKey, Object> specialCallTargetCache,
ConcurrentHashMap<ResolvedJavaMethod, Object> invocationPluginsCache) {
super(architecture, graph, providers, loopExplosionPlugin, invocationPlugins, inlineInvokePlugins, parameterPlugin, nodePlugins,
peRootForInlining, sourceLanguagePosition, specialCallTargetCache, invocationPluginsCache);
}
@Override
protected EncodedGraph lookupEncodedGraph(ResolvedJavaMethod method, MethodSubstitutionPlugin plugin, BytecodeProvider intrinsicBytecodeProvider, boolean isSubstitution,
boolean trackNodeSourcePosition) {
EncodedGraph result = graphCache.get(method);
if (result == null) {
result = createGraph(method, trackNodeSourcePosition);
}
return result;
}
private EncodedGraph createGraph(ResolvedJavaMethod method, boolean trackNodeSourcePosition) {
EncodedGraph result = GraalSupport.encodedGraph((SharedRuntimeMethod) method, trackNodeSourcePosition);
if (result == null) {
throw shouldNotReachHere("Graph not available for runtime compilation: " + method.format("%H.%n(%p)"));
}
graphCache.put(method, result);
return result;
}
}