package com.oracle.svm.hosted.phases;
import org.graalvm.compiler.core.common.type.StampPair;
import org.graalvm.compiler.debug.DebugContext;
import org.graalvm.compiler.java.GraphBuilderPhase.Instance;
import org.graalvm.compiler.nodes.AbstractBeginNode;
import org.graalvm.compiler.nodes.CallTargetNode.InvokeKind;
import org.graalvm.compiler.nodes.ConstantNode;
import org.graalvm.compiler.nodes.UnwindNode;
import org.graalvm.compiler.nodes.ValueNode;
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
import org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext;
import org.graalvm.compiler.nodes.java.ExceptionObjectNode;
import org.graalvm.compiler.nodes.java.LoadFieldNode;
import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
import org.graalvm.compiler.phases.OptimisticOptimizations;
import org.graalvm.compiler.phases.util.Providers;
import com.oracle.graal.pointsto.meta.AnalysisMethod;
import com.oracle.graal.pointsto.meta.AnalysisType;
import com.oracle.graal.pointsto.meta.HostedProviders;
import com.oracle.svm.core.c.BoxedRelocatedPointer;
import com.oracle.svm.core.classinitialization.EnsureClassInitializedNode;
import com.oracle.svm.core.graal.code.SubstrateCompilationIdentifier;
import com.oracle.svm.core.graal.nodes.DeadEndNode;
import com.oracle.svm.core.graal.replacements.SubstrateGraphKit;
import com.oracle.svm.core.util.ExceptionHelpers;
import com.oracle.svm.core.util.VMError;
import com.oracle.svm.hosted.meta.HostedMethod;
import com.oracle.svm.hosted.nodes.SubstrateMethodCallTargetNode;
import jdk.vm.ci.meta.JavaKind;
import jdk.vm.ci.meta.ResolvedJavaField;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.meta.ResolvedJavaType;
public class HostedGraphKit extends SubstrateGraphKit {
public HostedGraphKit(DebugContext debug, HostedProviders providers, ResolvedJavaMethod method) {
super(debug, method, providers, providers.getWordTypes(), providers.getGraphBuilderPlugins(), new SubstrateCompilationIdentifier());
}
@Override
protected MethodCallTargetNode createMethodCallTarget(InvokeKind invokeKind, ResolvedJavaMethod targetMethod, ValueNode[] args, StampPair returnStamp, int bci) {
ResolvedJavaMethod method = graph.method();
if (method instanceof HostedMethod) {
return new SubstrateMethodCallTargetNode(invokeKind, targetMethod, args, returnStamp, ((HostedMethod) method).getProfilingInfo(), bci);
} else {
return super.createMethodCallTarget(invokeKind, targetMethod, args, returnStamp, bci);
}
}
@Override
protected Instance createGraphBuilderInstance(Providers theProviders, GraphBuilderConfiguration graphBuilderConfig, OptimisticOptimizations optimisticOpts,
IntrinsicContext initialIntrinsicContext) {
ResolvedJavaMethod method = graph.method();
if (method instanceof AnalysisMethod) {
return new AnalysisGraphBuilderPhase(theProviders, graphBuilderConfig, optimisticOpts, initialIntrinsicContext, wordTypes);
} else if (method instanceof HostedMethod) {
return new HostedGraphBuilderPhase(theProviders, graphBuilderConfig, optimisticOpts, initialIntrinsicContext, wordTypes);
} else {
throw VMError.shouldNotReachHere();
}
}
public void emitEnsureInitializedCall(ResolvedJavaType type) {
if (SubstrateClassInitializationPlugin.needsRuntimeInitialization(graph.method().getDeclaringClass(), type)) {
ValueNode hub = createConstant(getConstantReflection().asJavaClass(type), JavaKind.Object);
int bci = bci();
EnsureClassInitializedNode ensureInitializedNode = append(new EnsureClassInitializedNode(hub));
ensureInitializedNode.setStateAfter(getFrameState().create(bci, ensureInitializedNode));
AbstractBeginNode noExceptionEdge = add(ensureInitializedNode.createNextBegin());
ensureInitializedNode.setNext(noExceptionEdge);
ExceptionObjectNode exceptionEdge = createExceptionObjectNode(getFrameState(), bci);
ensureInitializedNode.setExceptionEdge(exceptionEdge);
lastFixedNode = exceptionEdge;
append(new UnwindNode(exceptionEdge));
assert lastFixedNode == null;
lastFixedNode = noExceptionEdge;
}
}
public void throwInvocationTargetException(ValueNode exception) {
ResolvedJavaMethod throwInvocationTargetException = findMethod(ExceptionHelpers.class, "throwInvocationTargetException", true);
createJavaCallWithExceptionAndUnwind(InvokeKind.Static, throwInvocationTargetException, exception);
append(new DeadEndNode());
}
public LoadFieldNode createLoadFieldNode(ConstantNode receiver, Class<BoxedRelocatedPointer> clazz, String fieldName) {
try {
ResolvedJavaType type = providers.getMetaAccess().lookupJavaType(clazz);
if (type instanceof AnalysisType) {
((AnalysisType) type).registerAsReachable();
}
ResolvedJavaField field = providers.getMetaAccess().lookupJavaField(clazz.getDeclaredField(fieldName));
return LoadFieldNode.createOverrideStamp(StampPair.createSingle(wordStamp((ResolvedJavaType) field.getType())), receiver, field);
} catch (NoSuchFieldException e) {
throw VMError.shouldNotReachHere(e);
}
}
}