39 import org.graalvm.compiler.nodes.ValueNode;
40 import org.graalvm.compiler.nodes.spi.LIRLowerable;
41 import org.graalvm.compiler.nodes.spi.Lowerable;
42 import org.graalvm.compiler.nodes.spi.LoweringTool;
43 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
44
45 /**
46 * Assertion nodes will go away as soon as the value evaluates to true. Compile-time assertions will
47 * fail if this has not happened by the time the node is lowered to LIR, while runtime assertions
48 * may need to insert a check.
49 */
50 @NodeInfo(cycles = CYCLES_UNKNOWN, size = SIZE_UNKNOWN)
51 public final class AssertionNode extends FixedWithNextNode implements Lowerable, Canonicalizable, LIRLowerable {
52
53 public static final NodeClass<AssertionNode> TYPE = NodeClass.create(AssertionNode.class);
54 @Input ValueNode condition;
55
56 protected final boolean compileTimeAssertion;
57 protected final String message;
58
59 public AssertionNode(boolean compileTimeAssertion, ValueNode condition, String message) {
60 super(TYPE, StampFactory.forVoid());
61 this.condition = condition;
62 this.compileTimeAssertion = compileTimeAssertion;
63 this.message = message;
64 }
65
66 public ValueNode condition() {
67 return condition;
68 }
69
70 public String message() {
71 return message;
72 }
73
74 @Override
75 public Node canonical(CanonicalizerTool tool) {
76 if (condition.isConstant() && condition.asJavaConstant().asInt() != 0) {
77 return null;
78 }
79 /*
80 * Assertions with a constant "false" value do not immediately cause an error, since they
81 * may be unreachable and could thus be removed by later optimizations.
82 */
83 return this;
95 }
96 }
97
98 @Override
99 public void generate(NodeLIRBuilderTool generator) {
100 assert compileTimeAssertion;
101 if (GraalOptions.ImmutableCode.getValue(getOptions())) {
102 // Snippet assertions are disabled for AOT
103 return;
104 }
105 if (condition.isConstant()) {
106 if (condition.asJavaConstant().asInt() == 0) {
107 throw new GraalError("%s: failed compile-time assertion: %s", this, message);
108 }
109 } else {
110 throw new GraalError("%s: failed compile-time assertion (value %s): %s", this, condition, message);
111 }
112 }
113
114 @NodeIntrinsic
115 public static native void assertion(@ConstantNodeParameter boolean compileTimeAssertion, boolean condition, @ConstantNodeParameter String message);
116 }
|
39 import org.graalvm.compiler.nodes.ValueNode;
40 import org.graalvm.compiler.nodes.spi.LIRLowerable;
41 import org.graalvm.compiler.nodes.spi.Lowerable;
42 import org.graalvm.compiler.nodes.spi.LoweringTool;
43 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
44
45 /**
46 * Assertion nodes will go away as soon as the value evaluates to true. Compile-time assertions will
47 * fail if this has not happened by the time the node is lowered to LIR, while runtime assertions
48 * may need to insert a check.
49 */
50 @NodeInfo(cycles = CYCLES_UNKNOWN, size = SIZE_UNKNOWN)
51 public final class AssertionNode extends FixedWithNextNode implements Lowerable, Canonicalizable, LIRLowerable {
52
53 public static final NodeClass<AssertionNode> TYPE = NodeClass.create(AssertionNode.class);
54 @Input ValueNode condition;
55
56 protected final boolean compileTimeAssertion;
57 protected final String message;
58
59 public AssertionNode(boolean compileTimeAssertion, ValueNode condition, String message, Object arg1, Object arg2) {
60 super(TYPE, StampFactory.forVoid());
61 this.condition = condition;
62 this.compileTimeAssertion = compileTimeAssertion;
63 this.message = message + arg1 + arg2;
64 }
65
66 public ValueNode condition() {
67 return condition;
68 }
69
70 public String message() {
71 return message;
72 }
73
74 @Override
75 public Node canonical(CanonicalizerTool tool) {
76 if (condition.isConstant() && condition.asJavaConstant().asInt() != 0) {
77 return null;
78 }
79 /*
80 * Assertions with a constant "false" value do not immediately cause an error, since they
81 * may be unreachable and could thus be removed by later optimizations.
82 */
83 return this;
95 }
96 }
97
98 @Override
99 public void generate(NodeLIRBuilderTool generator) {
100 assert compileTimeAssertion;
101 if (GraalOptions.ImmutableCode.getValue(getOptions())) {
102 // Snippet assertions are disabled for AOT
103 return;
104 }
105 if (condition.isConstant()) {
106 if (condition.asJavaConstant().asInt() == 0) {
107 throw new GraalError("%s: failed compile-time assertion: %s", this, message);
108 }
109 } else {
110 throw new GraalError("%s: failed compile-time assertion (value %s): %s", this, condition, message);
111 }
112 }
113
114 @NodeIntrinsic
115 public static native void assertion(@ConstantNodeParameter boolean compileTimeAssertion, boolean condition, @ConstantNodeParameter String message, @ConstantNodeParameter Object arg1,
116 @ConstantNodeParameter Object arg2);
117
118 public static void assertion(@ConstantNodeParameter boolean compileTimeAssertion, boolean condition, @ConstantNodeParameter String message) {
119 assertion(compileTimeAssertion, condition, message, "", "");
120 }
121
122 public static void assertion(@ConstantNodeParameter boolean compileTimeAssertion, boolean condition, @ConstantNodeParameter String message, @ConstantNodeParameter Object arg1) {
123 assertion(compileTimeAssertion, condition, message, arg1, "");
124 }
125 }
|