1 /* 2 * Copyright (c) 2015, 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.nodes.graphbuilderconf; 24 25 import java.lang.invoke.MethodHandle; 26 import java.lang.reflect.Method; 27 28 import org.graalvm.compiler.debug.GraalError; 29 import org.graalvm.compiler.nodes.Invoke; 30 import org.graalvm.compiler.nodes.ValueNode; 31 import org.graalvm.compiler.nodes.type.StampTool; 32 33 import jdk.vm.ci.meta.MetaAccessProvider; 34 import jdk.vm.ci.meta.ResolvedJavaMethod; 35 36 /** 37 * Plugin for handling a specific method invocation. 38 */ 39 public interface InvocationPlugin extends GraphBuilderPlugin { 40 41 /** 42 * The receiver in a non-static method. The class literal for this interface must be used with 43 * {@link InvocationPlugins#put} to denote the receiver argument for such a non-static method. 44 */ 45 public interface Receiver { 46 /** 47 * Gets the receiver value, null checking it first if necessary. 48 * 49 * @return the receiver value with a {@linkplain StampTool#isPointerNonNull(ValueNode) 50 * non-null} stamp 51 */ 52 default ValueNode get() { 53 return get(true); 54 } 55 56 /** 57 * Gets the receiver value, optionally null checking it first if necessary. 58 */ 59 ValueNode get(boolean performNullCheck); 60 61 /** 62 * Determines if the receiver is constant. 63 */ 64 default boolean isConstant() { 65 return false; 66 } 67 } 68 69 /** 70 * Determines if this plugin is for a method with a polymorphic signature (e.g. 71 * {@link MethodHandle#invokeExact(Object...)}). 72 */ 73 default boolean isSignaturePolymorphic() { 74 return false; 75 } 76 77 /** 78 * Determines if this plugin can only be used when inlining the method is it associated with. 79 * That is, this plugin cannot be used when the associated method is the compilation root. 80 */ 81 default boolean inlineOnly() { 82 return isSignaturePolymorphic(); 83 } 84 85 /** 86 * Handles invocation of a signature polymorphic method. 87 * 88 * @param receiver access to the receiver, {@code null} if {@code targetMethod} is static 89 * @param argsIncludingReceiver all arguments to the invocation include the raw receiver in 90 * position 0 if {@code targetMethod} is not static 91 * @see #execute 92 */ 93 default boolean applyPolymorphic(GraphBuilderContext b, ResolvedJavaMethod targetMethod, InvocationPlugin.Receiver receiver, ValueNode... argsIncludingReceiver) { 94 return defaultHandler(b, targetMethod, receiver, argsIncludingReceiver); 95 } 96 97 /** 98 * @see #execute 99 */ 100 default boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, InvocationPlugin.Receiver receiver) { 101 return defaultHandler(b, targetMethod, receiver); 102 } 103 104 /** 105 * @see #execute 106 */ 107 default boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, InvocationPlugin.Receiver receiver, ValueNode arg) { 108 return defaultHandler(b, targetMethod, receiver, arg); 109 } 110 111 /** 112 * @see #execute 113 */ 114 default boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, InvocationPlugin.Receiver receiver, ValueNode arg1, ValueNode arg2) { 115 return defaultHandler(b, targetMethod, receiver, arg1, arg2); 116 } 117 118 /** 119 * @see #execute 120 */ 121 default boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, InvocationPlugin.Receiver receiver, ValueNode arg1, ValueNode arg2, ValueNode arg3) { 122 return defaultHandler(b, targetMethod, receiver, arg1, arg2, arg3); 123 } 124 125 /** 126 * @see #execute 127 */ 128 default boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, InvocationPlugin.Receiver receiver, ValueNode arg1, ValueNode arg2, ValueNode arg3, ValueNode arg4) { 129 return defaultHandler(b, targetMethod, receiver, arg1, arg2, arg3, arg4); 130 } 131 132 /** 133 * @see #execute 134 */ 135 default boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, InvocationPlugin.Receiver receiver, ValueNode arg1, ValueNode arg2, ValueNode arg3, ValueNode arg4, ValueNode arg5) { 136 return defaultHandler(b, targetMethod, receiver, arg1, arg2, arg3, arg4, arg5); 137 } 138 139 /** 140 * Executes this plugin against a set of invocation arguments. 141 * 142 * The default implementation in {@link InvocationPlugin} dispatches to the {@code apply(...)} 143 * method that matches the number of arguments or to {@link #applyPolymorphic} if {@code plugin} 144 * is {@linkplain #isSignaturePolymorphic() signature polymorphic}. 145 * 146 * @param targetMethod the method for which this plugin is being applied 147 * @param receiver access to the receiver, {@code null} if {@code targetMethod} is static 148 * @param argsIncludingReceiver all arguments to the invocation include the receiver in position 149 * 0 if {@code targetMethod} is not static 150 * @return {@code true} if this plugin handled the invocation of {@code targetMethod} 151 * {@code false} if the graph builder should process the invoke further (e.g., by 152 * inlining it or creating an {@link Invoke} node). A plugin that does not handle an 153 * invocation must not modify the graph being constructed. 154 */ 155 default boolean execute(GraphBuilderContext b, ResolvedJavaMethod targetMethod, InvocationPlugin.Receiver receiver, ValueNode[] argsIncludingReceiver) { 156 if (isSignaturePolymorphic()) { 157 return applyPolymorphic(b, targetMethod, receiver, argsIncludingReceiver); 158 } else if (receiver != null) { 159 assert !targetMethod.isStatic(); 160 assert argsIncludingReceiver.length > 0; 161 if (argsIncludingReceiver.length == 1) { 162 return apply(b, targetMethod, receiver); 163 } else if (argsIncludingReceiver.length == 2) { 164 return apply(b, targetMethod, receiver, argsIncludingReceiver[1]); 165 } else if (argsIncludingReceiver.length == 3) { 166 return apply(b, targetMethod, receiver, argsIncludingReceiver[1], argsIncludingReceiver[2]); 167 } else if (argsIncludingReceiver.length == 4) { 168 return apply(b, targetMethod, receiver, argsIncludingReceiver[1], argsIncludingReceiver[2], argsIncludingReceiver[3]); 169 } else if (argsIncludingReceiver.length == 5) { 170 return apply(b, targetMethod, receiver, argsIncludingReceiver[1], argsIncludingReceiver[2], argsIncludingReceiver[3], argsIncludingReceiver[4]); 171 } else { 172 return defaultHandler(b, targetMethod, receiver, argsIncludingReceiver); 173 } 174 } else { 175 assert targetMethod.isStatic(); 176 if (argsIncludingReceiver.length == 0) { 177 return apply(b, targetMethod, null); 178 } else if (argsIncludingReceiver.length == 1) { 179 return apply(b, targetMethod, null, argsIncludingReceiver[0]); 180 } else if (argsIncludingReceiver.length == 2) { 181 return apply(b, targetMethod, null, argsIncludingReceiver[0], argsIncludingReceiver[1]); 182 } else if (argsIncludingReceiver.length == 3) { 183 return apply(b, targetMethod, null, argsIncludingReceiver[0], argsIncludingReceiver[1], argsIncludingReceiver[2]); 184 } else if (argsIncludingReceiver.length == 4) { 185 return apply(b, targetMethod, null, argsIncludingReceiver[0], argsIncludingReceiver[1], argsIncludingReceiver[2], argsIncludingReceiver[3]); 186 } else if (argsIncludingReceiver.length == 5) { 187 return apply(b, targetMethod, null, argsIncludingReceiver[0], argsIncludingReceiver[1], argsIncludingReceiver[2], argsIncludingReceiver[3], argsIncludingReceiver[4]); 188 } else { 189 return defaultHandler(b, targetMethod, receiver, argsIncludingReceiver); 190 } 191 192 } 193 } 194 195 /** 196 * Handles an invocation when a specific {@code apply} method is not available. 197 */ 198 default boolean defaultHandler(@SuppressWarnings("unused") GraphBuilderContext b, ResolvedJavaMethod targetMethod, @SuppressWarnings("unused") InvocationPlugin.Receiver receiver, 199 ValueNode... args) { 200 throw new GraalError("Invocation plugin for %s does not handle invocations with %d arguments", targetMethod.format("%H.%n(%p)"), args.length); 201 } 202 203 default StackTraceElement getApplySourceLocation(MetaAccessProvider metaAccess) { 204 Class<?> c = getClass(); 205 for (Method m : c.getDeclaredMethods()) { 206 if (m.getName().equals("apply")) { 207 return metaAccess.lookupJavaMethod(m).asStackTraceElement(0); 208 } else if (m.getName().equals("defaultHandler")) { 209 return metaAccess.lookupJavaMethod(m).asStackTraceElement(0); 210 } 211 } 212 throw new GraalError("could not find method named \"apply\" or \"defaultHandler\" in " + c.getName()); 213 } 214 }