package org.graalvm.compiler.nodes;
import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_0;
import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_1;
import java.util.Map;
import org.graalvm.compiler.core.common.LIRKind;
import org.graalvm.compiler.core.common.type.AbstractObjectStamp;
import org.graalvm.compiler.core.common.type.FloatStamp;
import org.graalvm.compiler.core.common.type.IntegerStamp;
import org.graalvm.compiler.core.common.type.Stamp;
import org.graalvm.compiler.core.common.type.StampFactory;
import org.graalvm.compiler.debug.GraalError;
import org.graalvm.compiler.graph.Node;
import org.graalvm.compiler.graph.NodeClass;
import org.graalvm.compiler.graph.iterators.NodeIterable;
import org.graalvm.compiler.lir.ConstantValue;
import org.graalvm.compiler.nodeinfo.NodeInfo;
import org.graalvm.compiler.nodeinfo.Verbosity;
import org.graalvm.compiler.nodes.calc.FloatingNode;
import org.graalvm.compiler.nodes.spi.ArrayLengthProvider;
import org.graalvm.compiler.nodes.spi.LIRLowerable;
import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
import jdk.vm.ci.code.CodeUtil;
import jdk.vm.ci.meta.Constant;
import jdk.vm.ci.meta.ConstantReflectionProvider;
import jdk.vm.ci.meta.JavaConstant;
import jdk.vm.ci.meta.JavaKind;
import jdk.vm.ci.meta.MetaAccessProvider;
import jdk.vm.ci.meta.PrimitiveConstant;
@NodeInfo(nameTemplate = "C({p#rawvalue}) {p#stampKind}", cycles = CYCLES_0, size = SIZE_1)
public final class ConstantNode extends FloatingNode implements LIRLowerable, ArrayLengthProvider {
public static final NodeClass<ConstantNode> TYPE = NodeClass.create(ConstantNode.class);
protected final Constant value;
private final int stableDimension;
private final boolean isDefaultStable;
private static ConstantNode createPrimitive(JavaConstant value) {
assert value.getJavaKind() != JavaKind.Object;
return new ConstantNode(value, StampFactory.forConstant(value));
}
public ConstantNode(Constant value, Stamp stamp) {
this(value, stamp, 0, false);
}
private ConstantNode(Constant value, Stamp stamp, int stableDimension, boolean isDefaultStable) {
super(TYPE, stamp);
assert stamp != null && stamp.isCompatible(value) : stamp + " " + value;
this.value = value;
this.stableDimension = stableDimension;
if (stableDimension == 0) {
this.isDefaultStable = false;
} else {
this.isDefaultStable = isDefaultStable;
}
}
public Constant getValue() {
return value;
}
public int getStableDimension() {
return stableDimension;
}
public boolean isDefaultStable() {
return isDefaultStable;
}
public static NodeIterable<ConstantNode> getConstantNodes(StructuredGraph graph) {
return graph.getNodes().filter(ConstantNode.class);
}
public void replace(StructuredGraph graph, Node replacement) {
assert graph == graph();
replaceAtUsagesAndDelete(replacement);
}
@Override
public void generate(NodeLIRBuilderTool gen) {
LIRKind kind = gen.getLIRGeneratorTool().getLIRKind(stamp(NodeView.DEFAULT));
if (onlyUsedInVirtualState()) {
gen.setResult(this, new ConstantValue(kind, value));
} else {
gen.setResult(this, gen.getLIRGeneratorTool().emitConstant(kind, value));
}
}
private boolean onlyUsedInVirtualState() {
for (Node n : this.usages()) {
if (n instanceof VirtualState) {
} else {
return false;
}
}
return true;
}
public static ConstantNode forConstant(JavaConstant constant, MetaAccessProvider metaAccess, StructuredGraph graph) {
if (constant.getJavaKind().getStackKind() == JavaKind.Int && constant.getJavaKind() != JavaKind.Int) {
return forInt(constant.asInt(), graph);
}
if (constant.getJavaKind() == JavaKind.Object) {
return unique(graph, new ConstantNode(constant, StampFactory.forConstant(constant, metaAccess)));
} else {
return unique(graph, createPrimitive(constant));
}
}
public static ConstantNode forConstant(JavaConstant constant, int stableDimension, boolean isDefaultStable, MetaAccessProvider metaAccess) {
if (constant.getJavaKind().getStackKind() == JavaKind.Int && constant.getJavaKind() != JavaKind.Int) {
return forInt(constant.asInt());
}
if (constant.getJavaKind() == JavaKind.Object) {
return new ConstantNode(constant, StampFactory.forConstant(constant, metaAccess), stableDimension, isDefaultStable);
} else {
assert stableDimension == 0;
return createPrimitive(constant);
}
}
public static ConstantNode forConstant(JavaConstant array, MetaAccessProvider metaAccess) {
return forConstant(array, 0, false, metaAccess);
}
public static ConstantNode forConstant(Stamp stamp, Constant constant, MetaAccessProvider metaAccess, StructuredGraph graph) {
return graph.unique(new ConstantNode(constant, stamp.constant(constant, metaAccess)));
}
public static ConstantNode forConstant(Stamp stamp, Constant constant, int stableDimension, boolean isDefaultStable, MetaAccessProvider metaAccess) {
return new ConstantNode(constant, stamp.constant(constant, metaAccess), stableDimension, isDefaultStable);
}
public static ConstantNode forConstant(Stamp stamp, Constant constant, MetaAccessProvider metaAccess) {
return new ConstantNode(constant, stamp.constant(constant, metaAccess));
}
public static ConstantNode forPrimitive(JavaConstant constant, StructuredGraph graph) {
assert constant.getJavaKind() != JavaKind.Object;
return forConstant(constant, null, graph);
}
public static ConstantNode forPrimitive(JavaConstant constant) {
assert constant.getJavaKind() != JavaKind.Object;
return forConstant(constant, null);
}
public static ConstantNode forPrimitive(Stamp stamp, JavaConstant constant, StructuredGraph graph) {
if (stamp instanceof IntegerStamp) {
assert constant.getJavaKind().isNumericInteger() && stamp.getStackKind() == constant.getJavaKind().getStackKind();
IntegerStamp istamp = (IntegerStamp) stamp;
return forIntegerBits(istamp.getBits(), constant, graph);
} else {
assert constant.getJavaKind().isNumericFloat() && stamp.getStackKind() == constant.getJavaKind();
return forPrimitive(constant, graph);
}
}
public static ConstantNode forPrimitive(Stamp stamp, Constant constant) {
if (stamp instanceof IntegerStamp) {
PrimitiveConstant primitive = (PrimitiveConstant) constant;
assert primitive.getJavaKind().isNumericInteger() && stamp.getStackKind() == primitive.getJavaKind().getStackKind();
IntegerStamp istamp = (IntegerStamp) stamp;
return forIntegerBits(istamp.getBits(), primitive);
} else if (stamp instanceof FloatStamp) {
PrimitiveConstant primitive = (PrimitiveConstant) constant;
assert primitive.getJavaKind().isNumericFloat() && stamp.getStackKind() == primitive.getJavaKind();
return forConstant(primitive, null);
} else {
assert !(stamp instanceof AbstractObjectStamp);
return new ConstantNode(constant, stamp.constant(constant, null));
}
}
public static ConstantNode forDouble(double d, StructuredGraph graph) {
return unique(graph, createPrimitive(JavaConstant.forDouble(d)));
}
public static ConstantNode forDouble(double d) {
return createPrimitive(JavaConstant.forDouble(d));
}
public static ConstantNode forFloat(float f, StructuredGraph graph) {
return unique(graph, createPrimitive(JavaConstant.forFloat(f)));
}
public static ConstantNode forFloat(float f) {
return createPrimitive(JavaConstant.forFloat(f));
}
public static ConstantNode forLong(long i, StructuredGraph graph) {
return unique(graph, createPrimitive(JavaConstant.forLong(i)));
}
public static ConstantNode forLong(long i) {
return createPrimitive(JavaConstant.forLong(i));
}
public static ConstantNode forInt(int i, StructuredGraph graph) {
return unique(graph, createPrimitive(JavaConstant.forInt(i)));
}
public static ConstantNode forInt(int i) {
return createPrimitive(JavaConstant.forInt(i));
}
public static ConstantNode forBoolean(boolean i, StructuredGraph graph) {
return unique(graph, createPrimitive(JavaConstant.forInt(i ? 1 : 0)));
}
public static ConstantNode forBoolean(boolean i) {
return createPrimitive(JavaConstant.forInt(i ? 1 : 0));
}
public static ConstantNode forByte(byte i, StructuredGraph graph) {
return unique(graph, createPrimitive(JavaConstant.forInt(i)));
}
public static ConstantNode forChar(char i, StructuredGraph graph) {
return unique(graph, createPrimitive(JavaConstant.forInt(i)));
}
public static ConstantNode forShort(short i, StructuredGraph graph) {
return unique(graph, createPrimitive(JavaConstant.forInt(i)));
}
private static ConstantNode unique(StructuredGraph graph, ConstantNode node) {
return graph.unique(node);
}
private static ConstantNode forIntegerBits(int bits, JavaConstant constant, StructuredGraph graph) {
long value = constant.asLong();
long bounds = CodeUtil.signExtend(value, bits);
return unique(graph, new ConstantNode(constant, StampFactory.forInteger(bits, bounds, bounds)));
}
public static ConstantNode forIntegerBits(int bits, long value, StructuredGraph graph) {
return forIntegerBits(bits, JavaConstant.forPrimitiveInt(bits, value), graph);
}
private static ConstantNode forIntegerBits(int bits, JavaConstant constant) {
long value = constant.asLong();
long bounds = CodeUtil.signExtend(value, bits);
return new ConstantNode(constant, StampFactory.forInteger(bits, bounds, bounds));
}
public static ConstantNode forIntegerBits(int bits, long value) {
return forIntegerBits(bits, JavaConstant.forPrimitiveInt(bits, value));
}
public static ConstantNode forIntegerStamp(Stamp stamp, long value, StructuredGraph graph) {
if (stamp instanceof IntegerStamp) {
IntegerStamp intStamp = (IntegerStamp) stamp;
return forIntegerBits(intStamp.getBits(), value, graph);
} else {
return forIntegerKind(stamp.getStackKind(), value, graph);
}
}
public static ConstantNode forIntegerStamp(Stamp stamp, long value) {
if (stamp instanceof IntegerStamp) {
IntegerStamp intStamp = (IntegerStamp) stamp;
return forIntegerBits(intStamp.getBits(), value);
} else {
return forIntegerKind(stamp.getStackKind(), value);
}
}
public static ConstantNode forIntegerKind(JavaKind kind, long value, StructuredGraph graph) {
switch (kind) {
case Byte:
case Short:
case Int:
return ConstantNode.forInt((int) value, graph);
case Long:
return ConstantNode.forLong(value, graph);
default:
throw GraalError.shouldNotReachHere("unknown kind " + kind);
}
}
public static ConstantNode forIntegerKind(JavaKind kind, long value) {
switch (kind) {
case Byte:
case Short:
case Int:
return createPrimitive(JavaConstant.forInt((int) value));
case Long:
return createPrimitive(JavaConstant.forLong(value));
default:
throw GraalError.shouldNotReachHere("unknown kind " + kind);
}
}
public static ConstantNode forFloatingKind(JavaKind kind, double value, StructuredGraph graph) {
switch (kind) {
case Float:
return ConstantNode.forFloat((float) value, graph);
case Double:
return ConstantNode.forDouble(value, graph);
default:
throw GraalError.shouldNotReachHere("unknown kind " + kind);
}
}
public static ConstantNode forFloatingKind(JavaKind kind, double value) {
switch (kind) {
case Float:
return ConstantNode.forFloat((float) value);
case Double:
return ConstantNode.forDouble(value);
default:
throw GraalError.shouldNotReachHere("unknown kind " + kind);
}
}
public static ConstantNode forFloatingStamp(Stamp stamp, double value, StructuredGraph graph) {
return forFloatingKind(stamp.getStackKind(), value, graph);
}
public static ConstantNode forFloatingStamp(Stamp stamp, double value) {
return forFloatingKind(stamp.getStackKind(), value);
}
public static ConstantNode defaultForKind(JavaKind kind, StructuredGraph graph) {
return unique(graph, defaultForKind(kind));
}
public static ConstantNode defaultForKind(JavaKind kind) {
switch (kind) {
case Boolean:
case Byte:
case Char:
case Short:
case Int:
return ConstantNode.forInt(0);
case Double:
return ConstantNode.forDouble(0.0);
case Float:
return ConstantNode.forFloat(0.0f);
case Long:
return ConstantNode.forLong(0L);
case Object:
return ConstantNode.forConstant(JavaConstant.NULL_POINTER, null);
default:
return null;
}
}
@Override
public Map<Object, Object> getDebugProperties(Map<Object, Object> map) {
Map<Object, Object> properties = super.getDebugProperties(map);
properties.put("rawvalue", value.toValueString());
properties.put("stampKind", stamp.unrestricted().toString());
return properties;
}
@Override
public String toString(Verbosity verbosity) {
if (verbosity == Verbosity.Name) {
return super.toString(Verbosity.Name) + "(" + value.toValueString() + ", " + stamp(NodeView.DEFAULT).unrestricted().toString() + ")";
} else {
return super.toString(verbosity);
}
}
@Override
public ValueNode findLength(FindLengthMode mode, ConstantReflectionProvider constantReflection) {
if (constantReflection == null || !(value instanceof JavaConstant) || ((JavaConstant) value).isNull()) {
return null;
}
Integer length = constantReflection.readArrayLength((JavaConstant) value);
if (length == null) {
return null;
}
return ConstantNode.forInt(length);
}
}