1 /* 2 * Copyright (c) 2011, 2019, 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.MemoryCheckpoint; 45 import org.graalvm.compiler.nodes.spi.Lowerable; 46 import org.graalvm.compiler.nodes.spi.LoweringTool; 47 import jdk.internal.vm.compiler.word.LocationIdentity; 48 49 import jdk.vm.ci.meta.JavaKind; 50 import jdk.vm.ci.meta.MetaAccessProvider; 51 52 /** 53 * A node that represents an exception thrown implicitly by a Java bytecode. It can be lowered to 54 * either a {@linkplain ForeignCallDescriptor foreign} call or a pre-allocated exception object. 55 */ 56 // @formatter:off 57 @NodeInfo(cycles = CYCLES_8, 58 cyclesRationale = "Node will be lowered to a foreign call.", 59 size = SIZE_8) 60 // @formatter:on 61 public final class BytecodeExceptionNode extends AbstractMemoryCheckpoint implements Lowerable, MemoryCheckpoint.Single, Canonicalizable { 62 63 public enum BytecodeExceptionKind { 64 NULL_POINTER(0, NullPointerException.class), 65 OUT_OF_BOUNDS(2, ArrayIndexOutOfBoundsException.class), 66 CLASS_CAST(2, ClassCastException.class), 67 ARRAY_STORE(1, ArrayStoreException.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 117 @Override lower(LoweringTool tool)118 public void lower(LoweringTool tool) { 119 tool.getLowerer().lower(this, tool); 120 } 121 getArguments()122 public NodeInputList<ValueNode> getArguments() { 123 return arguments; 124 } 125 126 /** 127 * Create a new stateDuring for use by a foreign call. 128 */ createStateDuring()129 public FrameState createStateDuring() { 130 return stateAfter.duplicateModified(graph(), stateAfter.bci, /* rethrowException */ false, /* duringCall */ true, 131 JavaKind.Object, null, null); 132 } 133 134 } 135