1 /*
2  * Copyright (c) 2011, 2020, 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.FrameState;
42 import org.graalvm.compiler.nodes.ValueNode;
43 import org.graalvm.compiler.nodes.memory.AbstractMemoryCheckpoint;
44 import org.graalvm.compiler.nodes.memory.SingleMemoryKill;
45 import org.graalvm.compiler.nodes.spi.Lowerable;
46 import jdk.internal.vm.compiler.word.LocationIdentity;
47 
48 import jdk.vm.ci.meta.JavaKind;
49 import jdk.vm.ci.meta.MetaAccessProvider;
50 
51 /**
52  * A node that represents an exception thrown implicitly by a Java bytecode. It can be lowered to
53  * either a {@linkplain ForeignCallDescriptor foreign} call or a pre-allocated exception object.
54  */
55 // @formatter:off
56 @NodeInfo(cycles = CYCLES_8,
57           cyclesRationale = "Node will be lowered to a foreign call.",
58           size = SIZE_8)
59 // @formatter:on
60 public final class BytecodeExceptionNode extends AbstractMemoryCheckpoint implements Lowerable, SingleMemoryKill, Canonicalizable {
61 
62     public enum BytecodeExceptionKind {
63         NULL_POINTER(0, NullPointerException.class),
64         OUT_OF_BOUNDS(2, ArrayIndexOutOfBoundsException.class),
65         CLASS_CAST(2, ClassCastException.class),
66         ARRAY_STORE(1, ArrayStoreException.class),
67         ILLEGAL_ARGUMENT_EXCEPTION(1, IllegalArgumentException.class),
68         DIVISION_BY_ZERO(0, ArithmeticException.class),
69         INTEGER_EXACT_OVERFLOW(0, ArithmeticException.class),
70         LONG_EXACT_OVERFLOW(0, ArithmeticException.class);
71 
72         final int numArguments;
73         final Class<? extends Throwable> exceptionClass;
74 
BytecodeExceptionKind(int numArguments, Class<? extends Throwable> exceptionClass)75         BytecodeExceptionKind(int numArguments, Class<? extends Throwable> exceptionClass) {
76             this.numArguments = numArguments;
77             this.exceptionClass = exceptionClass;
78         }
79     }
80 
81     public static final NodeClass<BytecodeExceptionNode> TYPE = NodeClass.create(BytecodeExceptionNode.class);
82     protected final BytecodeExceptionKind exceptionKind;
83     @Input NodeInputList<ValueNode> arguments;
84 
BytecodeExceptionNode(MetaAccessProvider metaAccess, BytecodeExceptionKind exceptionKind, ValueNode... arguments)85     public BytecodeExceptionNode(MetaAccessProvider metaAccess, BytecodeExceptionKind exceptionKind, ValueNode... arguments) {
86         super(TYPE, StampFactory.objectNonNull(TypeReference.createExactTrusted(metaAccess.lookupJavaType(exceptionKind.exceptionClass))));
87         this.exceptionKind = exceptionKind;
88         this.arguments = new NodeInputList<>(this, arguments);
89         GraalError.guarantee(arguments.length == exceptionKind.numArguments, "Mismatch in argument count for BytecodeExceptionNode");
90     }
91 
getExceptionKind()92     public BytecodeExceptionKind getExceptionKind() {
93         return exceptionKind;
94     }
95 
96     @Override
toString(Verbosity verbosity)97     public String toString(Verbosity verbosity) {
98         if (verbosity == Verbosity.Name) {
99             return super.toString(verbosity) + "#" + exceptionKind;
100         }
101         return super.toString(verbosity);
102     }
103 
104     @Override
getKilledLocationIdentity()105     public LocationIdentity getKilledLocationIdentity() {
106         return LocationIdentity.any();
107     }
108 
109     @Override
canonical(CanonicalizerTool tool)110     public Node canonical(CanonicalizerTool tool) {
111         if (tool.allUsagesAvailable() && (hasNoUsages() || (hasExactlyOneUsage() && usages().first() == stateAfter))) {
112             return null;
113         }
114         return this;
115     }
116 
getArguments()117     public NodeInputList<ValueNode> getArguments() {
118         return arguments;
119     }
120 
121     /**
122      * Create a new stateDuring for use by a foreign call.
123      */
createStateDuring()124     public FrameState createStateDuring() {
125         boolean rethrowException = false;
126         boolean duringCall = true;
127         return stateAfter.duplicateModified(graph(), stateAfter.bci, rethrowException, duringCall,
128                         JavaKind.Object, null, null);
129     }
130 
131 }
132