001/*
002 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.hotspot.replacements;
024
025import static com.oracle.graal.hotspot.nodes.CStringNode.*;
026import static com.oracle.graal.replacements.SnippetTemplate.*;
027import jdk.internal.jvmci.code.*;
028
029import com.oracle.graal.compiler.common.spi.*;
030import com.oracle.graal.graph.Node.ConstantNodeParameter;
031import com.oracle.graal.graph.Node.NodeIntrinsic;
032import com.oracle.graal.hotspot.meta.*;
033import com.oracle.graal.hotspot.nodes.*;
034import com.oracle.graal.nodes.*;
035import com.oracle.graal.nodes.extended.*;
036import com.oracle.graal.nodes.spi.*;
037import com.oracle.graal.replacements.*;
038import com.oracle.graal.replacements.Snippet.ConstantParameter;
039import com.oracle.graal.replacements.SnippetTemplate.AbstractTemplates;
040import com.oracle.graal.replacements.SnippetTemplate.Arguments;
041import com.oracle.graal.replacements.SnippetTemplate.SnippetInfo;
042import com.oracle.graal.replacements.nodes.*;
043import com.oracle.graal.word.*;
044
045public class AssertionSnippets implements Snippets {
046
047    /**
048     * This call can only be used with true for the "vmError" parameter, so that it can be
049     * configured to be a leaf method.
050     */
051    public static final ForeignCallDescriptor ASSERTION_VM_MESSAGE_C = new ForeignCallDescriptor("assertionVmMessageC", void.class, boolean.class, Word.class, long.class, long.class, long.class);
052
053    @Snippet
054    public static void assertion(boolean value, @ConstantParameter String message) {
055        if (!value) {
056            vmMessageC(ASSERTION_VM_MESSAGE_C, true, cstring(message), 0L, 0L, 0L);
057        }
058    }
059
060    @Snippet
061    public static void stubAssertion(boolean value, @ConstantParameter String message) {
062        if (!value) {
063            vmMessageC(ASSERTION_VM_MESSAGE_C, true, cstring(message), 0L, 0L, 0L);
064        }
065    }
066
067    @NodeIntrinsic(ForeignCallNode.class)
068    static native void vmMessageC(@ConstantNodeParameter ForeignCallDescriptor stubPrintfC, boolean vmError, Word format, long v1, long v2, long v3);
069
070    public static class Templates extends AbstractTemplates {
071
072        private final SnippetInfo assertion = snippet(AssertionSnippets.class, "assertion");
073        private final SnippetInfo stubAssertion = snippet(AssertionSnippets.class, "stubAssertion");
074
075        public Templates(HotSpotProviders providers, TargetDescription target) {
076            super(providers, providers.getSnippetReflection(), target);
077        }
078
079        public void lower(AssertionNode assertionNode, LoweringTool tool) {
080            StructuredGraph graph = assertionNode.graph();
081            Arguments args = new Arguments(graph.start() instanceof StubStartNode ? stubAssertion : assertion, graph.getGuardsStage(), tool.getLoweringStage());
082            args.add("value", assertionNode.value());
083            args.addConst("message", "failed runtime assertion in snippet/stub: " + assertionNode.message() + " (" + graph.method() + ")");
084
085            template(args).instantiate(providers.getMetaAccess(), assertionNode, DEFAULT_REPLACER, args);
086        }
087    }
088}