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