43972
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
|
43972
|
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 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.nodes.graphbuilderconf;
|
|
26 |
|
54084
|
27 |
import static jdk.vm.ci.services.Services.IS_IN_NATIVE_IMAGE;
|
54601
|
28 |
import static org.graalvm.compiler.core.common.GraalOptions.UseEncodedGraphs;
|
|
29 |
import static org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext.CompilationContext.INLINE_AFTER_PARSING;
|
43972
|
30 |
import static org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.resolveType;
|
|
31 |
|
|
32 |
import java.lang.reflect.Method;
|
|
33 |
import java.lang.reflect.Modifier;
|
|
34 |
import java.lang.reflect.Type;
|
|
35 |
import java.util.Arrays;
|
|
36 |
import java.util.stream.Collectors;
|
|
37 |
|
|
38 |
import org.graalvm.compiler.bytecode.BytecodeProvider;
|
|
39 |
import org.graalvm.compiler.debug.GraalError;
|
54601
|
40 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
43972
|
41 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
42 |
|
|
43 |
import jdk.vm.ci.meta.MetaAccessProvider;
|
|
44 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
58877
|
45 |
import jdk.vm.ci.meta.ResolvedJavaType;
|
43972
|
46 |
|
|
47 |
/**
|
|
48 |
* An {@link InvocationPlugin} for a method where the implementation of the method is provided by a
|
|
49 |
* {@linkplain #getSubstitute(MetaAccessProvider) substitute} method. A substitute method must be
|
|
50 |
* static even if the substituted method is not.
|
|
51 |
*
|
|
52 |
* While performing intrinsification with method substitutions is simpler than writing an
|
|
53 |
* {@link InvocationPlugin} that does manual graph weaving, it has a higher compile time cost than
|
|
54 |
* the latter; parsing bytecodes to create nodes is slower than simply creating nodes. As such, the
|
|
55 |
* recommended practice is to use {@link MethodSubstitutionPlugin} only for complex
|
|
56 |
* intrinsifications which is typically those using non-straight-line control flow.
|
|
57 |
*/
|
|
58 |
public final class MethodSubstitutionPlugin implements InvocationPlugin {
|
|
59 |
|
58877
|
60 |
private InvocationPlugins.Registration registration;
|
|
61 |
|
43972
|
62 |
private ResolvedJavaMethod cachedSubstitute;
|
|
63 |
|
|
64 |
/**
|
|
65 |
* The class in which the substitute method is declared.
|
|
66 |
*/
|
|
67 |
private final Class<?> declaringClass;
|
|
68 |
|
|
69 |
/**
|
58877
|
70 |
* The name of the substitute method.
|
43972
|
71 |
*/
|
58877
|
72 |
private final String substituteName;
|
|
73 |
|
|
74 |
/**
|
|
75 |
* The name of the original method.
|
|
76 |
*/
|
|
77 |
private final String originalName;
|
43972
|
78 |
|
|
79 |
/**
|
|
80 |
* The parameter types of the substitute method.
|
|
81 |
*/
|
|
82 |
private final Type[] parameters;
|
|
83 |
|
|
84 |
private final boolean originalIsStatic;
|
|
85 |
|
|
86 |
private final BytecodeProvider bytecodeProvider;
|
|
87 |
|
|
88 |
/**
|
|
89 |
* Creates a method substitution plugin.
|
|
90 |
*
|
|
91 |
* @param bytecodeProvider used to get the bytecodes to parse for the substitute method
|
58877
|
92 |
* @param originalName the name of the original method
|
43972
|
93 |
* @param declaringClass the class in which the substitute method is declared
|
58877
|
94 |
* @param substituteName the name of the substitute method
|
43972
|
95 |
* @param parameters the parameter types of the substitute method. If the original method is not
|
|
96 |
* static, then {@code parameters[0]} must be the {@link Class} value denoting
|
|
97 |
* {@link InvocationPlugin.Receiver}
|
|
98 |
*/
|
58877
|
99 |
public MethodSubstitutionPlugin(InvocationPlugins.Registration registration, BytecodeProvider bytecodeProvider, String originalName, Class<?> declaringClass, String substituteName,
|
|
100 |
Type... parameters) {
|
|
101 |
assert bytecodeProvider != null : "Requires a non-null methodSubstitutionBytecodeProvider";
|
|
102 |
this.registration = registration;
|
43972
|
103 |
this.bytecodeProvider = bytecodeProvider;
|
58877
|
104 |
this.originalName = originalName;
|
43972
|
105 |
this.declaringClass = declaringClass;
|
58877
|
106 |
this.substituteName = substituteName;
|
43972
|
107 |
this.parameters = parameters;
|
|
108 |
this.originalIsStatic = parameters.length == 0 || parameters[0] != InvocationPlugin.Receiver.class;
|
|
109 |
}
|
|
110 |
|
|
111 |
@Override
|
|
112 |
public boolean inlineOnly() {
|
|
113 |
// Conservatively assume MacroNodes may be used in a substitution
|
|
114 |
return true;
|
|
115 |
}
|
|
116 |
|
|
117 |
/**
|
|
118 |
* Gets the substitute method, resolving it first if necessary.
|
|
119 |
*/
|
|
120 |
public ResolvedJavaMethod getSubstitute(MetaAccessProvider metaAccess) {
|
|
121 |
if (cachedSubstitute == null) {
|
|
122 |
cachedSubstitute = metaAccess.lookupJavaMethod(getJavaSubstitute());
|
|
123 |
}
|
|
124 |
return cachedSubstitute;
|
|
125 |
}
|
|
126 |
|
|
127 |
/**
|
46459
|
128 |
* Gets the object used to access the bytecodes of the substitute method.
|
|
129 |
*/
|
|
130 |
public BytecodeProvider getBytecodeProvider() {
|
|
131 |
return bytecodeProvider;
|
|
132 |
}
|
|
133 |
|
|
134 |
/**
|
54601
|
135 |
* Gets the class in which the substitute method is declared.
|
|
136 |
*/
|
|
137 |
public Class<?> getDeclaringClass() {
|
|
138 |
return declaringClass;
|
|
139 |
}
|
|
140 |
|
|
141 |
/**
|
43972
|
142 |
* Gets the reflection API version of the substitution method.
|
|
143 |
*/
|
54601
|
144 |
public Method getJavaSubstitute() throws GraalError {
|
43972
|
145 |
Method substituteMethod = lookupSubstitute();
|
|
146 |
int modifiers = substituteMethod.getModifiers();
|
|
147 |
if (Modifier.isAbstract(modifiers) || Modifier.isNative(modifiers)) {
|
|
148 |
throw new GraalError("Substitution method must not be abstract or native: " + substituteMethod);
|
|
149 |
}
|
|
150 |
if (!Modifier.isStatic(modifiers)) {
|
|
151 |
throw new GraalError("Substitution method must be static: " + substituteMethod);
|
|
152 |
}
|
|
153 |
return substituteMethod;
|
|
154 |
}
|
|
155 |
|
|
156 |
/**
|
|
157 |
* Determines if a given method is the substitute method of this plugin.
|
|
158 |
*/
|
|
159 |
private boolean isSubstitute(Method m) {
|
58877
|
160 |
if (Modifier.isStatic(m.getModifiers()) && m.getName().equals(substituteName)) {
|
43972
|
161 |
if (parameters.length == m.getParameterCount()) {
|
|
162 |
Class<?>[] mparams = m.getParameterTypes();
|
|
163 |
int start = 0;
|
|
164 |
if (!originalIsStatic) {
|
|
165 |
start = 1;
|
|
166 |
if (!mparams[0].isAssignableFrom(resolveType(parameters[0], false))) {
|
|
167 |
return false;
|
|
168 |
}
|
|
169 |
}
|
|
170 |
for (int i = start; i < mparams.length; i++) {
|
|
171 |
if (mparams[i] != resolveType(parameters[i], false)) {
|
|
172 |
return false;
|
|
173 |
}
|
|
174 |
}
|
46459
|
175 |
return true;
|
43972
|
176 |
}
|
|
177 |
}
|
|
178 |
return false;
|
|
179 |
}
|
|
180 |
|
46459
|
181 |
private Method lookupSubstitute(Method excluding) {
|
|
182 |
for (Method m : declaringClass.getDeclaredMethods()) {
|
|
183 |
if (!m.equals(excluding) && isSubstitute(m)) {
|
|
184 |
return m;
|
|
185 |
}
|
|
186 |
}
|
|
187 |
return null;
|
|
188 |
}
|
|
189 |
|
43972
|
190 |
/**
|
|
191 |
* Gets the substitute method of this plugin.
|
|
192 |
*/
|
|
193 |
private Method lookupSubstitute() {
|
46459
|
194 |
Method m = lookupSubstitute(null);
|
|
195 |
if (m != null) {
|
|
196 |
assert lookupSubstitute(m) == null : String.format("multiple matches found for %s:%n%s%n%s", this, m, lookupSubstitute(m));
|
|
197 |
return m;
|
43972
|
198 |
}
|
|
199 |
throw new GraalError("No method found specified by %s", this);
|
|
200 |
}
|
|
201 |
|
|
202 |
@Override
|
|
203 |
public boolean execute(GraphBuilderContext b, ResolvedJavaMethod targetMethod, InvocationPlugin.Receiver receiver, ValueNode[] argsIncludingReceiver) {
|
54601
|
204 |
if (IS_IN_NATIVE_IMAGE || (UseEncodedGraphs.getValue(b.getOptions()) && !b.parsingIntrinsic())) {
|
55509
|
205 |
StructuredGraph subst = b.getReplacements().getMethodSubstitution(this,
|
|
206 |
targetMethod,
|
|
207 |
INLINE_AFTER_PARSING,
|
|
208 |
StructuredGraph.AllowAssumptions.ifNonNull(b.getAssumptions()),
|
|
209 |
null /* cancellable */,
|
|
210 |
b.getOptions());
|
54601
|
211 |
if (subst == null) {
|
|
212 |
throw new GraalError("No graphs found for substitution %s", this);
|
|
213 |
}
|
|
214 |
return b.intrinsify(targetMethod, subst, receiver, argsIncludingReceiver);
|
54084
|
215 |
}
|
54601
|
216 |
ResolvedJavaMethod substitute = getSubstitute(b.getMetaAccess());
|
|
217 |
return b.intrinsify(bytecodeProvider, targetMethod, substitute, receiver, argsIncludingReceiver);
|
43972
|
218 |
}
|
|
219 |
|
|
220 |
@Override
|
|
221 |
public StackTraceElement getApplySourceLocation(MetaAccessProvider metaAccess) {
|
|
222 |
Class<?> c = getClass();
|
|
223 |
for (Method m : c.getDeclaredMethods()) {
|
|
224 |
if (m.getName().equals("execute")) {
|
|
225 |
return metaAccess.lookupJavaMethod(m).asStackTraceElement(0);
|
|
226 |
}
|
|
227 |
}
|
|
228 |
throw new GraalError("could not find method named \"execute\" in " + c.getName());
|
|
229 |
}
|
|
230 |
|
|
231 |
@Override
|
|
232 |
public String toString() {
|
58877
|
233 |
return String.format("%s[%s.%s(%s)]", getClass().getSimpleName(), declaringClass.getName(), substituteName,
|
43972
|
234 |
Arrays.asList(parameters).stream().map(c -> c.getTypeName()).collect(Collectors.joining(", ")));
|
|
235 |
}
|
58877
|
236 |
|
|
237 |
public String originalMethodAsString() {
|
|
238 |
return String.format("%s.%s(%s)", declaringClass.getName(), substituteName, Arrays.asList(parameters).stream().map(c -> c.getTypeName()).collect(Collectors.joining(", ")));
|
|
239 |
}
|
|
240 |
|
|
241 |
public ResolvedJavaMethod getOriginalMethod(MetaAccessProvider metaAccess) {
|
|
242 |
Class<?> clazz = resolveType(registration.getDeclaringType(), false);
|
|
243 |
if (clazz == null) {
|
|
244 |
throw new GraalError("Can't find original class for " + this + " with class " + registration.getDeclaringType());
|
|
245 |
}
|
|
246 |
ResolvedJavaType type = metaAccess.lookupJavaType(clazz);
|
|
247 |
String argumentsDescriptor = InvocationPlugins.toArgumentDescriptor(originalIsStatic, this.parameters);
|
|
248 |
for (ResolvedJavaMethod declared : type.getDeclaredMethods()) {
|
|
249 |
if (declared.getName().equals(originalName) && declared.isStatic() == originalIsStatic &&
|
|
250 |
declared.getSignature().toMethodDescriptor().startsWith(argumentsDescriptor)) {
|
|
251 |
return declared;
|
|
252 |
}
|
|
253 |
}
|
|
254 |
throw new GraalError("Can't find original method for " + this + " with class " + registration.getDeclaringType());
|
|
255 |
}
|
43972
|
256 |
}
|