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