1 /*
   2  * Copyright (c) 2014, 2018, 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  */
  23 
  24 
  25 package org.graalvm.compiler.replacements.nodes;
  26 
  27 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_UNKNOWN;
  28 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_UNKNOWN;
  29 
  30 import org.graalvm.compiler.core.common.GraalOptions;
  31 import org.graalvm.compiler.core.common.type.StampFactory;
  32 import org.graalvm.compiler.debug.GraalError;
  33 import org.graalvm.compiler.graph.Node;
  34 import org.graalvm.compiler.graph.NodeClass;
  35 import org.graalvm.compiler.graph.spi.Canonicalizable;
  36 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  37 import org.graalvm.compiler.nodeinfo.NodeInfo;
  38 import org.graalvm.compiler.nodes.FixedWithNextNode;
  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;
  84     }
  85 
  86     @Override
  87     public void lower(LoweringTool tool) {
  88         if (!compileTimeAssertion) {
  89             if (GraalOptions.ImmutableCode.getValue(getOptions())) {
  90                 // Snippet assertions are disabled for AOT
  91                 graph().removeFixed(this);
  92             } else {
  93                 tool.getLowerer().lower(this, tool);
  94             }
  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 }