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