1 /*
   2  * Copyright (c) 2014, 2016, 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 package org.graalvm.compiler.hotspot.replacements;
  24 
  25 import static org.graalvm.compiler.replacements.SnippetTemplate.DEFAULT_REPLACER;
  26 import static org.graalvm.compiler.replacements.nodes.CStringConstant.cstring;
  27 
  28 import org.graalvm.compiler.api.replacements.Snippet;
  29 import org.graalvm.compiler.api.replacements.Snippet.ConstantParameter;
  30 import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor;
  31 import org.graalvm.compiler.graph.Node.ConstantNodeParameter;
  32 import org.graalvm.compiler.graph.Node.NodeIntrinsic;
  33 import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
  34 import org.graalvm.compiler.hotspot.nodes.StubStartNode;
  35 import org.graalvm.compiler.nodes.StructuredGraph;
  36 import org.graalvm.compiler.nodes.extended.ForeignCallNode;
  37 import org.graalvm.compiler.nodes.spi.LoweringTool;
  38 import org.graalvm.compiler.replacements.SnippetTemplate.AbstractTemplates;
  39 import org.graalvm.compiler.replacements.SnippetTemplate.Arguments;
  40 import org.graalvm.compiler.replacements.SnippetTemplate.SnippetInfo;
  41 import org.graalvm.compiler.replacements.Snippets;
  42 import org.graalvm.compiler.replacements.nodes.AssertionNode;
  43 import org.graalvm.compiler.word.Word;
  44 
  45 import jdk.vm.ci.code.TargetDescription;
  46 
  47 public class AssertionSnippets implements Snippets {
  48 
  49     /**
  50      * This call can only be used with true for the "vmError" parameter, so that it can be
  51      * configured to be a leaf method.
  52      */
  53     public static final ForeignCallDescriptor ASSERTION_VM_MESSAGE_C = new ForeignCallDescriptor("assertionVmMessageC", void.class, boolean.class, Word.class, long.class, long.class, long.class);
  54 
  55     @Snippet
  56     public static void assertion(boolean value, @ConstantParameter String message) {
  57         if (!value) {
  58             vmMessageC(ASSERTION_VM_MESSAGE_C, true, cstring(message), 0L, 0L, 0L);
  59         }
  60     }
  61 
  62     @Snippet
  63     public static void stubAssertion(boolean value, @ConstantParameter String message) {
  64         if (!value) {
  65             vmMessageC(ASSERTION_VM_MESSAGE_C, true, cstring(message), 0L, 0L, 0L);
  66         }
  67     }
  68 
  69     @NodeIntrinsic(ForeignCallNode.class)
  70     static native void vmMessageC(@ConstantNodeParameter ForeignCallDescriptor stubPrintfC, boolean vmError, Word format, long v1, long v2, long v3);
  71 
  72     public static class Templates extends AbstractTemplates {
  73 
  74         private final SnippetInfo assertion = snippet(AssertionSnippets.class, "assertion");
  75         private final SnippetInfo stubAssertion = snippet(AssertionSnippets.class, "stubAssertion");
  76 
  77         public Templates(HotSpotProviders providers, TargetDescription target) {
  78             super(providers, providers.getSnippetReflection(), target);
  79         }
  80 
  81         public void lower(AssertionNode assertionNode, LoweringTool tool) {
  82             StructuredGraph graph = assertionNode.graph();
  83             Arguments args = new Arguments(graph.start() instanceof StubStartNode ? stubAssertion : assertion, graph.getGuardsStage(), tool.getLoweringStage());
  84             args.add("value", assertionNode.value());
  85             args.addConst("message", "failed runtime assertion in snippet/stub: " + assertionNode.message() + " (" + graph.method() + ")");
  86 
  87             template(args).instantiate(providers.getMetaAccess(), assertionNode, DEFAULT_REPLACER, args);
  88         }
  89     }
  90 }