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 * Executes this plugin against a set of invocation arguments. 142 * 143 * The default implementation in {@link InvocationPlugin} dispatches to the {@code apply(...)} 144 * method that matches the number of arguments or to {@link #applyPolymorphic} if {@code plugin} 145 * is {@linkplain #isSignaturePolymorphic() signature polymorphic}. 146 * 147 * @param targetMethod the method for which this plugin is being applied 148 * @param receiver access to the receiver, {@code null} if {@code targetMethod} is static 149 * @param argsIncludingReceiver all arguments to the invocation include the receiver in position 150 * 0 if {@code targetMethod} is not static 151 * @return {@code true} if this plugin handled the invocation of {@code targetMethod} 152 * {@code false} if the graph builder should process the invoke further (e.g., by 153 * inlining it or creating an {@link Invoke} node). A plugin that does not handle an 154 * invocation must not modify the graph being constructed. 155 */ 156 default boolean execute(GraphBuilderContext b, ResolvedJavaMethod targetMethod, InvocationPlugin.Receiver receiver, ValueNode[] argsIncludingReceiver) { 157 if (isSignaturePolymorphic()) { 158 return applyPolymorphic(b, targetMethod, receiver, argsIncludingReceiver); 159 } else if (receiver != null) { 160 assert !targetMethod.isStatic(); 161 assert argsIncludingReceiver.length > 0; 162 if (argsIncludingReceiver.length == 1) { 163 return apply(b, targetMethod, receiver); 164 } else if (argsIncludingReceiver.length == 2) { 165 return apply(b, targetMethod, receiver, argsIncludingReceiver[1]); 166 } else if (argsIncludingReceiver.length == 3) { 167 return apply(b, targetMethod, receiver, argsIncludingReceiver[1], argsIncludingReceiver[2]); 168 } else if (argsIncludingReceiver.length == 4) { 169 return apply(b, targetMethod, receiver, argsIncludingReceiver[1], argsIncludingReceiver[2], argsIncludingReceiver[3]); 170 } else if (argsIncludingReceiver.length == 5) { 171 return apply(b, targetMethod, receiver, argsIncludingReceiver[1], argsIncludingReceiver[2], argsIncludingReceiver[3], argsIncludingReceiver[4]); 172 } else { 173 return defaultHandler(b, targetMethod, receiver, argsIncludingReceiver); 174 } 175 } else { 176 assert targetMethod.isStatic(); 177 if (argsIncludingReceiver.length == 0) { 178 return apply(b, targetMethod, null); 179 } else if (argsIncludingReceiver.length == 1) { 180 return apply(b, targetMethod, null, argsIncludingReceiver[0]); 181 } else if (argsIncludingReceiver.length == 2) { 182 return apply(b, targetMethod, null, argsIncludingReceiver[0], argsIncludingReceiver[1]); 183 } else if (argsIncludingReceiver.length == 3) { 184 return apply(b, targetMethod, null, argsIncludingReceiver[0], argsIncludingReceiver[1], argsIncludingReceiver[2]); 185 } else if (argsIncludingReceiver.length == 4) { 186 return apply(b, targetMethod, null, argsIncludingReceiver[0], argsIncludingReceiver[1], argsIncludingReceiver[2], argsIncludingReceiver[3]); 187 } else if (argsIncludingReceiver.length == 5) { 188 return apply(b, targetMethod, null, argsIncludingReceiver[0], argsIncludingReceiver[1], argsIncludingReceiver[2], argsIncludingReceiver[3], argsIncludingReceiver[4]); 189 } else { 190 return defaultHandler(b, targetMethod, receiver, argsIncludingReceiver); 191 } 192 193 } 194 } 195 196 /** 197 * Handles an invocation when a specific {@code apply} method is not available. 198 */ 199 default boolean defaultHandler(@SuppressWarnings("unused") GraphBuilderContext b, ResolvedJavaMethod targetMethod, @SuppressWarnings("unused") InvocationPlugin.Receiver receiver, 200 ValueNode... args) { 201 throw new GraalError("Invocation plugin for %s does not handle invocations with %d arguments", targetMethod.format("%H.%n(%p)"), args.length); 202 } 203 204 default StackTraceElement getApplySourceLocation(MetaAccessProvider metaAccess) { 205 Class<?> c = getClass(); 206 for (Method m : c.getDeclaredMethods()) { 207 if (m.getName().equals("apply")) { 208 return metaAccess.lookupJavaMethod(m).asStackTraceElement(0); 209 } else if (m.getName().equals("defaultHandler")) { 210 return metaAccess.lookupJavaMethod(m).asStackTraceElement(0); 211 } 212 } 213 throw new GraalError("could not find method named \"apply\" or \"defaultHandler\" in " + c.getName()); 214 } 215 }