package org.graalvm.compiler.nodes.extended;
import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_2;
import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_1;
import org.graalvm.compiler.core.common.type.Stamp;
import org.graalvm.compiler.core.common.type.TypeReference;
import org.graalvm.compiler.debug.GraalError;
import org.graalvm.compiler.graph.Node;
import org.graalvm.compiler.graph.NodeClass;
import org.graalvm.compiler.graph.spi.Canonicalizable;
import org.graalvm.compiler.graph.spi.CanonicalizerTool;
import org.graalvm.compiler.nodeinfo.NodeInfo;
import org.graalvm.compiler.nodes.ConstantNode;
import org.graalvm.compiler.nodes.FixedWithNextNode;
import org.graalvm.compiler.nodes.NodeView;
import org.graalvm.compiler.nodes.ValueNode;
import org.graalvm.compiler.nodes.spi.Lowerable;
import org.graalvm.compiler.nodes.spi.LoweringTool;
import org.graalvm.compiler.nodes.type.StampTool;
import jdk.vm.ci.meta.Assumptions;
import jdk.vm.ci.meta.Assumptions.AssumptionResult;
import jdk.vm.ci.meta.JavaConstant;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.meta.ResolvedJavaType;
@NodeInfo(cycles = CYCLES_2, size = SIZE_1)
public final class LoadMethodNode extends FixedWithNextNode implements Lowerable, Canonicalizable {
public static final NodeClass<LoadMethodNode> TYPE = NodeClass.create(LoadMethodNode.class);
@Input ValueNode hub;
protected final ResolvedJavaMethod method;
protected final ResolvedJavaType receiverType;
protected final ResolvedJavaType callerType;
public ValueNode getHub() {
return hub;
}
public LoadMethodNode(@InjectedNodeParameter Stamp stamp, ResolvedJavaMethod method, ResolvedJavaType receiverType, ResolvedJavaType callerType, ValueNode hub) {
super(TYPE, stamp);
this.receiverType = receiverType;
this.callerType = callerType;
this.hub = hub;
this.method = method;
assert method.isConcrete() : "Cannot load abstract method from a hub";
assert method.hasReceiver() : "Cannot load a static method from a hub";
if (!method.isInVirtualMethodTable(receiverType)) {
throw new GraalError("%s does not have a vtable entry in type %s", method, receiverType);
}
}
@Override
public void lower(LoweringTool tool) {
tool.getLowerer().lower(this, tool);
}
@Override
public Node canonical(CanonicalizerTool tool) {
if (hub instanceof LoadHubNode) {
ValueNode object = ((LoadHubNode) hub).getValue();
TypeReference type = StampTool.typeReferenceOrNull(object);
if (type != null) {
if (type.isExact()) {
return resolveExactMethod(tool, type.getType());
}
Assumptions assumptions = graph().getAssumptions();
AssumptionResult<ResolvedJavaMethod> resolvedMethod = type.getType().findUniqueConcreteMethod(method);
if (resolvedMethod != null && resolvedMethod.canRecordTo(assumptions) && !type.getType().isInterface() && method.getDeclaringClass().isAssignableFrom(type.getType())) {
NodeView view = NodeView.from(tool);
resolvedMethod.recordTo(assumptions);
return ConstantNode.forConstant(stamp(view), resolvedMethod.getResult().getEncoding(), tool.getMetaAccess());
}
}
}
if (hub.isConstant()) {
return resolveExactMethod(tool, tool.getConstantReflection().asJavaType(hub.asConstant()));
}
return this;
}
private Node resolveExactMethod(CanonicalizerTool tool, ResolvedJavaType type) {
ResolvedJavaMethod newMethod = type.resolveConcreteMethod(method, callerType);
if (newMethod == null) {
return ConstantNode.forConstant(stamp(NodeView.DEFAULT), JavaConstant.NULL_POINTER, null);
} else {
return ConstantNode.forConstant(stamp(NodeView.DEFAULT), newMethod.getEncoding(), tool.getMetaAccess());
}
}
public ResolvedJavaMethod getMethod() {
return method;
}
public ResolvedJavaType getReceiverType() {
return receiverType;
}
public ResolvedJavaType getCallerType() {
return callerType;
}
}