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  */
23 
24 
25 package org.graalvm.compiler.nodes.extended;
26 
27 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_8;
28 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_8;
29 
30 import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor;
31 import org.graalvm.compiler.core.common.type.StampFactory;
32 import org.graalvm.compiler.core.common.type.TypeReference;
33 import org.graalvm.compiler.debug.GraalError;
34 import org.graalvm.compiler.graph.Node;
35 import org.graalvm.compiler.graph.NodeClass;
36 import org.graalvm.compiler.graph.NodeInputList;
37 import org.graalvm.compiler.graph.spi.Canonicalizable;
38 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
39 import org.graalvm.compiler.nodeinfo.NodeInfo;
40 import org.graalvm.compiler.nodeinfo.Verbosity;
41 import org.graalvm.compiler.nodes.ValueNode;
42 import org.graalvm.compiler.nodes.memory.AbstractMemoryCheckpoint;
43 import org.graalvm.compiler.nodes.memory.MemoryCheckpoint;
44 import org.graalvm.compiler.nodes.spi.Lowerable;
45 import org.graalvm.compiler.nodes.spi.LoweringTool;
46 import jdk.internal.vm.compiler.word.LocationIdentity;
47 
48 import jdk.vm.ci.meta.MetaAccessProvider;
49 
50 /**
51  * A node that represents an exception thrown implicitly by a Java bytecode. It can be lowered to
52  * either a {@linkplain ForeignCallDescriptor foreign} call or a pre-allocated exception object.
53  */
54 // @formatter:off
55 @NodeInfo(cycles = CYCLES_8,
56           cyclesRationale = "Node will be lowered to a foreign call.",
57           size = SIZE_8)
58 // @formatter:on
59 public final class BytecodeExceptionNode extends AbstractMemoryCheckpoint implements Lowerable, MemoryCheckpoint.Single, Canonicalizable {
60 
61     public enum BytecodeExceptionKind {
62         NULL_POINTER(0, NullPointerException.class),
63         OUT_OF_BOUNDS(2, ArrayIndexOutOfBoundsException.class),
64         CLASS_CAST(2, ClassCastException.class),
65         ARRAY_STORE(1, ArrayStoreException.class),
66         DIVISION_BY_ZERO(0, ArithmeticException.class);
67 
68         final int numArguments;
69         final Class<? extends Throwable> exceptionClass;
70 
BytecodeExceptionKind(int numArguments, Class<? extends Throwable> exceptionClass)71         BytecodeExceptionKind(int numArguments, Class<? extends Throwable> exceptionClass) {
72             this.numArguments = numArguments;
73             this.exceptionClass = exceptionClass;
74         }
75     }
76 
77     public static final NodeClass<BytecodeExceptionNode> TYPE = NodeClass.create(BytecodeExceptionNode.class);
78     protected final BytecodeExceptionKind exceptionKind;
79     @Input NodeInputList<ValueNode> arguments;
80 
BytecodeExceptionNode(MetaAccessProvider metaAccess, BytecodeExceptionKind exceptionKind, ValueNode... arguments)81     public BytecodeExceptionNode(MetaAccessProvider metaAccess, BytecodeExceptionKind exceptionKind, ValueNode... arguments) {
82         super(TYPE, StampFactory.objectNonNull(TypeReference.createExactTrusted(metaAccess.lookupJavaType(exceptionKind.exceptionClass))));
83         this.exceptionKind = exceptionKind;
84         this.arguments = new NodeInputList<>(this, arguments);
85         GraalError.guarantee(arguments.length == exceptionKind.numArguments, "Mismatch in argument count for BytecodeExceptionNode");
86     }
87 
getExceptionKind()88     public BytecodeExceptionKind getExceptionKind() {
89         return exceptionKind;
90     }
91 
92     @Override
toString(Verbosity verbosity)93     public String toString(Verbosity verbosity) {
94         if (verbosity == Verbosity.Name) {
95             return super.toString(verbosity) + "#" + exceptionKind;
96         }
97         return super.toString(verbosity);
98     }
99 
100     @Override
getLocationIdentity()101     public LocationIdentity getLocationIdentity() {
102         return LocationIdentity.any();
103     }
104 
105     @Override
canonical(CanonicalizerTool tool)106     public Node canonical(CanonicalizerTool tool) {
107         if (tool.allUsagesAvailable() && getUsageCount() == 0) {
108             return null;
109         }
110         return this;
111     }
112 
113     @Override
lower(LoweringTool tool)114     public void lower(LoweringTool tool) {
115         tool.getLowerer().lower(this, tool);
116     }
117 
getArguments()118     public NodeInputList<ValueNode> getArguments() {
119         return arguments;
120     }
121 }
122