1/*
2 * Copyright (c) 2014, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package org.graalvm.compiler.replacements.nodes;
24
25import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_UNKNOWN;
26import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_UNKNOWN;
27
28import org.graalvm.compiler.core.common.GraalOptions;
29import org.graalvm.compiler.core.common.type.StampFactory;
30import org.graalvm.compiler.debug.GraalError;
31import org.graalvm.compiler.graph.Node;
32import org.graalvm.compiler.graph.NodeClass;
33import org.graalvm.compiler.graph.spi.Canonicalizable;
34import org.graalvm.compiler.graph.spi.CanonicalizerTool;
35import org.graalvm.compiler.nodeinfo.NodeInfo;
36import org.graalvm.compiler.nodes.FixedWithNextNode;
37import org.graalvm.compiler.nodes.ValueNode;
38import org.graalvm.compiler.nodes.spi.LIRLowerable;
39import org.graalvm.compiler.nodes.spi.Lowerable;
40import org.graalvm.compiler.nodes.spi.LoweringTool;
41import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
42
43/**
44 * Assertion nodes will go away as soon as the value evaluates to true. Compile-time assertions will
45 * fail if this has not happened by the time the node is lowered to LIR, while runtime assertions
46 * may need to insert a check.
47 */
48@NodeInfo(cycles = CYCLES_UNKNOWN, size = SIZE_UNKNOWN)
49public final class AssertionNode extends FixedWithNextNode implements Lowerable, Canonicalizable, LIRLowerable {
50
51    public static final NodeClass<AssertionNode> TYPE = NodeClass.create(AssertionNode.class);
52    @Input ValueNode condition;
53
54    protected final boolean compileTimeAssertion;
55    protected final String message;
56
57    public AssertionNode(boolean compileTimeAssertion, ValueNode condition, String message) {
58        super(TYPE, StampFactory.forVoid());
59        this.condition = condition;
60        this.compileTimeAssertion = compileTimeAssertion;
61        this.message = message;
62    }
63
64    public ValueNode condition() {
65        return condition;
66    }
67
68    public String message() {
69        return message;
70    }
71
72    @Override
73    public Node canonical(CanonicalizerTool tool) {
74        if (condition.isConstant() && condition.asJavaConstant().asInt() != 0) {
75            return null;
76        }
77        /*
78         * Assertions with a constant "false" value do not immediately cause an error, since they
79         * may be unreachable and could thus be removed by later optimizations.
80         */
81        return this;
82    }
83
84    @Override
85    public void lower(LoweringTool tool) {
86        if (!compileTimeAssertion) {
87            if (GraalOptions.ImmutableCode.getValue(getOptions())) {
88                // Snippet assertions are disabled for AOT
89                graph().removeFixed(this);
90            } else {
91                tool.getLowerer().lower(this, tool);
92            }
93        }
94    }
95
96    @Override
97    public void generate(NodeLIRBuilderTool generator) {
98        assert compileTimeAssertion;
99        if (GraalOptions.ImmutableCode.getValue(getOptions())) {
100            // Snippet assertions are disabled for AOT
101            return;
102        }
103        if (condition.isConstant()) {
104            if (condition.asJavaConstant().asInt() == 0) {
105                throw new GraalError("%s: failed compile-time assertion: %s", this, message);
106            }
107        } else {
108            throw new GraalError("%s: failed compile-time assertion (value %s): %s", this, condition, message);
109        }
110    }
111
112    @NodeIntrinsic
113    public static native void assertion(@ConstantNodeParameter boolean compileTimeAssertion, boolean condition, @ConstantNodeParameter String message);
114}
115