1/*
2 * Copyright (c) 2011, 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 */
23package org.graalvm.compiler.nodes.extended;
24
25import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_8;
26import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_8;
27
28import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor;
29import org.graalvm.compiler.core.common.type.StampFactory;
30import org.graalvm.compiler.core.common.type.TypeReference;
31import org.graalvm.compiler.graph.NodeClass;
32import org.graalvm.compiler.graph.NodeInputList;
33import org.graalvm.compiler.nodeinfo.NodeInfo;
34import org.graalvm.compiler.nodeinfo.Verbosity;
35import org.graalvm.compiler.nodes.ValueNode;
36import org.graalvm.compiler.nodes.memory.AbstractMemoryCheckpoint;
37import org.graalvm.compiler.nodes.memory.MemoryCheckpoint;
38import org.graalvm.compiler.nodes.spi.Lowerable;
39import org.graalvm.compiler.nodes.spi.LoweringTool;
40import org.graalvm.word.LocationIdentity;
41
42import jdk.vm.ci.meta.MetaAccessProvider;
43
44/**
45 * A node that represents an exception thrown implicitly by a Java bytecode. It can be lowered to
46 * either a {@linkplain ForeignCallDescriptor foreign} call or a pre-allocated exception object.
47 */
48// @formatter:off
49@NodeInfo(cycles = CYCLES_8,
50          cyclesRationale = "Node will be lowered to a foreign call.",
51          size = SIZE_8)
52// @formatter:on
53public final class BytecodeExceptionNode extends AbstractMemoryCheckpoint implements Lowerable, MemoryCheckpoint.Single {
54
55    public static final NodeClass<BytecodeExceptionNode> TYPE = NodeClass.create(BytecodeExceptionNode.class);
56    protected final Class<? extends Throwable> exceptionClass;
57    @Input NodeInputList<ValueNode> arguments;
58
59    public BytecodeExceptionNode(MetaAccessProvider metaAccess, Class<? extends Throwable> exceptionClass, ValueNode... arguments) {
60        super(TYPE, StampFactory.objectNonNull(TypeReference.createExactTrusted(metaAccess.lookupJavaType(exceptionClass))));
61        this.exceptionClass = exceptionClass;
62        this.arguments = new NodeInputList<>(this, arguments);
63    }
64
65    public Class<? extends Throwable> getExceptionClass() {
66        return exceptionClass;
67    }
68
69    @Override
70    public String toString(Verbosity verbosity) {
71        if (verbosity == Verbosity.Name) {
72            return super.toString(verbosity) + "#" + exceptionClass.getSimpleName();
73        }
74        return super.toString(verbosity);
75    }
76
77    @Override
78    public LocationIdentity getLocationIdentity() {
79        return LocationIdentity.any();
80    }
81
82    @Override
83    public void lower(LoweringTool tool) {
84        tool.getLowerer().lower(this, tool);
85    }
86
87    public NodeInputList<ValueNode> getArguments() {
88        return arguments;
89    }
90}
91