1 /*
   2  * Copyright (c) 2012, 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.nodes;
  24 
  25 import static org.graalvm.compiler.hotspot.HotSpotBackend.VM_ERROR;
  26 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_UNKNOWN;
  27 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_UNKNOWN;
  28 
  29 import org.graalvm.compiler.bytecode.Bytecode;
  30 import org.graalvm.compiler.core.common.LIRKind;
  31 import org.graalvm.compiler.core.common.spi.ForeignCallLinkage;
  32 import org.graalvm.compiler.core.common.type.StampFactory;
  33 import org.graalvm.compiler.graph.NodeClass;
  34 import org.graalvm.compiler.nodeinfo.NodeInfo;
  35 import org.graalvm.compiler.nodes.FrameState;
  36 import org.graalvm.compiler.nodes.ValueNode;
  37 import org.graalvm.compiler.nodes.spi.LIRLowerable;
  38 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  39 import org.graalvm.compiler.replacements.Log;
  40 import org.graalvm.compiler.replacements.nodes.CStringConstant;
  41 
  42 import jdk.vm.ci.code.CodeUtil;
  43 import jdk.vm.ci.meta.ResolvedJavaMethod;
  44 import jdk.vm.ci.meta.Value;
  45 
  46 /**
  47  * Causes the VM to exit with a description of the current Java location and an optional
  48  * {@linkplain Log#printf(String, long) formatted} error message specified.
  49  */
  50 @NodeInfo(cycles = CYCLES_UNKNOWN, size = SIZE_UNKNOWN)
  51 public final class VMErrorNode extends DeoptimizingStubCall implements LIRLowerable {
  52 
  53     public static final NodeClass<VMErrorNode> TYPE = NodeClass.create(VMErrorNode.class);
  54     protected final String format;
  55     @Input ValueNode value;
  56 
  57     public VMErrorNode(String format, ValueNode value) {
  58         super(TYPE, StampFactory.forVoid());
  59         this.format = format;
  60         this.value = value;
  61     }
  62 
  63     @Override
  64     public void generate(NodeLIRBuilderTool gen) {
  65         String whereString;
  66         if (stateBefore() != null) {
  67             String nl = CodeUtil.NEW_LINE;
  68             StringBuilder sb = new StringBuilder("in compiled code associated with frame state:");
  69             FrameState fs = stateBefore();
  70             while (fs != null) {
  71                 Bytecode.appendLocation(sb.append(nl).append("\t"), fs.getCode(), fs.bci);
  72                 fs = fs.outerFrameState();
  73             }
  74             whereString = sb.toString();
  75         } else {
  76             ResolvedJavaMethod method = graph().method();
  77             whereString = "in compiled code for " + (method == null ? graph().toString() : method.format("%H.%n(%p)"));
  78         }
  79 
  80         LIRKind wordKind = gen.getLIRGeneratorTool().getLIRKind(StampFactory.pointer());
  81         Value whereArg = gen.getLIRGeneratorTool().emitConstant(wordKind, new CStringConstant(whereString));
  82         Value formatArg = gen.getLIRGeneratorTool().emitConstant(wordKind, new CStringConstant(format));
  83 
  84         ForeignCallLinkage linkage = gen.getLIRGeneratorTool().getForeignCalls().lookupForeignCall(VM_ERROR);
  85         gen.getLIRGeneratorTool().emitForeignCall(linkage, null, whereArg, formatArg, gen.operand(value));
  86     }
  87 
  88     @NodeIntrinsic
  89     public static native void vmError(@ConstantNodeParameter String format, long value);
  90 }