author | dlong |
Thu, 31 Oct 2019 16:54:16 -0700 | |
changeset 58877 | aec7bf35d6f5 |
parent 58299 | 6df94ce3ab2f |
permissions | -rw-r--r-- |
43972 | 1 |
/* |
58299 | 2 |
* Copyright (c) 2012, 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.hotspot.stubs; |
26 |
||
52956 | 27 |
import static jdk.vm.ci.code.BytecodeFrame.UNKNOWN_BCI; |
43972 | 28 |
import static jdk.vm.ci.hotspot.HotSpotCallingConventionType.JavaCall; |
29 |
import static jdk.vm.ci.hotspot.HotSpotCallingConventionType.JavaCallee; |
|
30 |
import static jdk.vm.ci.hotspot.HotSpotCallingConventionType.NativeCall; |
|
58299 | 31 |
import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.RegisterEffect.COMPUTES_REGISTERS_KILLED; |
32 |
import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.RegisterEffect.DESTROYS_ALL_CALLER_SAVE_REGISTERS; |
|
52956 | 33 |
import static org.graalvm.compiler.nodes.CallTargetNode.InvokeKind.Static; |
34 |
import static org.graalvm.compiler.nodes.ConstantNode.forBoolean; |
|
43972 | 35 |
|
36 |
import org.graalvm.compiler.core.common.CompilationIdentifier; |
|
37 |
import org.graalvm.compiler.core.common.LIRKind; |
|
38 |
import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor; |
|
46371 | 39 |
import org.graalvm.compiler.core.common.type.Stamp; |
43972 | 40 |
import org.graalvm.compiler.core.common.type.StampFactory; |
41 |
import org.graalvm.compiler.core.common.type.StampPair; |
|
46640 | 42 |
import org.graalvm.compiler.debug.DebugContext; |
49451 | 43 |
import org.graalvm.compiler.debug.GraalError; |
43972 | 44 |
import org.graalvm.compiler.debug.JavaMethodContext; |
45 |
import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage; |
|
51436 | 46 |
import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.Reexecutability; |
43972 | 47 |
import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.Transition; |
48 |
import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkageImpl; |
|
52956 | 49 |
import org.graalvm.compiler.hotspot.meta.HotSpotLoweringProvider; |
43972 | 50 |
import org.graalvm.compiler.hotspot.meta.HotSpotProviders; |
51 |
import org.graalvm.compiler.hotspot.nodes.StubForeignCallNode; |
|
52956 | 52 |
import org.graalvm.compiler.hotspot.stubs.ForeignCallSnippets.Templates; |
43972 | 53 |
import org.graalvm.compiler.nodes.InvokeNode; |
54 |
import org.graalvm.compiler.nodes.ParameterNode; |
|
55 |
import org.graalvm.compiler.nodes.ReturnNode; |
|
56 |
import org.graalvm.compiler.nodes.StructuredGraph; |
|
57 |
import org.graalvm.compiler.nodes.ValueNode; |
|
46344 | 58 |
import org.graalvm.compiler.options.OptionValues; |
43972 | 59 |
import org.graalvm.compiler.replacements.GraphKit; |
60 |
import org.graalvm.compiler.replacements.nodes.ReadRegisterNode; |
|
61 |
import org.graalvm.compiler.word.Word; |
|
62 |
import org.graalvm.compiler.word.WordTypes; |
|
49873 | 63 |
import jdk.internal.vm.compiler.word.LocationIdentity; |
43972 | 64 |
|
50858 | 65 |
import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime; |
43972 | 66 |
import jdk.vm.ci.hotspot.HotSpotSignature; |
46371 | 67 |
import jdk.vm.ci.meta.JavaKind; |
43972 | 68 |
import jdk.vm.ci.meta.JavaMethod; |
69 |
import jdk.vm.ci.meta.JavaType; |
|
70 |
import jdk.vm.ci.meta.MetaAccessProvider; |
|
71 |
import jdk.vm.ci.meta.ResolvedJavaMethod; |
|
72 |
import jdk.vm.ci.meta.ResolvedJavaType; |
|
73 |
import jdk.vm.ci.meta.Signature; |
|
74 |
||
75 |
/** |
|
76 |
* A {@linkplain #getGraph generated} stub for a {@link Transition non-leaf} foreign call from |
|
77 |
* compiled code. A stub is required for such calls as the caller may be scheduled for |
|
78 |
* deoptimization while the call is in progress. And since these are foreign/runtime calls on slow |
|
79 |
* paths, we don't want to force the register allocator to spill around the call. As such, this stub |
|
80 |
* saves and restores all allocatable registers. It also |
|
52956 | 81 |
* {@linkplain ForeignCallSnippets#handlePendingException handles} any exceptions raised during the |
82 |
* foreign call. |
|
43972 | 83 |
*/ |
84 |
public class ForeignCallStub extends Stub { |
|
85 |
||
50858 | 86 |
private final HotSpotJVMCIRuntime jvmciRuntime; |
43972 | 87 |
|
88 |
/** |
|
89 |
* The target of the call. |
|
90 |
*/ |
|
91 |
private final HotSpotForeignCallLinkage target; |
|
92 |
||
93 |
/** |
|
94 |
* Specifies if the JavaThread value for the current thread is to be prepended to the arguments |
|
95 |
* for the call to {@link #target}. |
|
96 |
*/ |
|
97 |
protected final boolean prependThread; |
|
98 |
||
99 |
/** |
|
100 |
* Creates a stub for a call to code at a given address. |
|
101 |
* |
|
102 |
* @param address the address of the code to call |
|
103 |
* @param descriptor the signature of the call to this stub |
|
104 |
* @param prependThread true if the JavaThread value for the current thread is to be prepended |
|
105 |
* to the arguments for the call to {@code address} |
|
51436 | 106 |
* @param reexecutability specifies if the stub call can be re-executed without (meaningful) |
107 |
* side effects. Deoptimization will not return to a point before a stub call that |
|
108 |
* cannot be re-executed. |
|
43972 | 109 |
* @param killedLocations the memory locations killed by the stub call |
110 |
*/ |
|
50858 | 111 |
public ForeignCallStub(OptionValues options, HotSpotJVMCIRuntime runtime, HotSpotProviders providers, long address, ForeignCallDescriptor descriptor, boolean prependThread, |
51436 | 112 |
Transition transition, Reexecutability reexecutability, LocationIdentity... killedLocations) { |
46344 | 113 |
super(options, providers, HotSpotForeignCallLinkageImpl.create(providers.getMetaAccess(), providers.getCodeCache(), providers.getWordTypes(), providers.getForeignCalls(), descriptor, 0L, |
58299 | 114 |
COMPUTES_REGISTERS_KILLED, JavaCall, JavaCallee, transition, reexecutability, killedLocations)); |
43972 | 115 |
this.jvmciRuntime = runtime; |
116 |
this.prependThread = prependThread; |
|
52956 | 117 |
MetaAccessProvider metaAccess = providers.getMetaAccess(); |
43972 | 118 |
Class<?>[] targetParameterTypes = createTargetParameters(descriptor); |
119 |
ForeignCallDescriptor targetSig = new ForeignCallDescriptor(descriptor.getName() + ":C", descriptor.getResultType(), targetParameterTypes); |
|
52956 | 120 |
target = HotSpotForeignCallLinkageImpl.create(metaAccess, providers.getCodeCache(), providers.getWordTypes(), providers.getForeignCalls(), targetSig, address, |
58299 | 121 |
DESTROYS_ALL_CALLER_SAVE_REGISTERS, NativeCall, NativeCall, transition, reexecutability, killedLocations); |
43972 | 122 |
} |
123 |
||
124 |
/** |
|
125 |
* Gets the linkage information for the call from this stub. |
|
126 |
*/ |
|
127 |
public HotSpotForeignCallLinkage getTargetLinkage() { |
|
128 |
return target; |
|
129 |
} |
|
130 |
||
131 |
private Class<?>[] createTargetParameters(ForeignCallDescriptor descriptor) { |
|
132 |
Class<?>[] parameters = descriptor.getArgumentTypes(); |
|
133 |
if (prependThread) { |
|
134 |
Class<?>[] newParameters = new Class<?>[parameters.length + 1]; |
|
135 |
System.arraycopy(parameters, 0, newParameters, 1, parameters.length); |
|
136 |
newParameters[0] = Word.class; |
|
137 |
return newParameters; |
|
138 |
} |
|
139 |
return parameters; |
|
140 |
} |
|
141 |
||
142 |
@Override |
|
143 |
protected ResolvedJavaMethod getInstalledCodeOwner() { |
|
144 |
return null; |
|
145 |
} |
|
146 |
||
147 |
private class DebugScopeContext implements JavaMethod, JavaMethodContext { |
|
148 |
@Override |
|
149 |
public JavaMethod asJavaMethod() { |
|
150 |
return this; |
|
151 |
} |
|
152 |
||
153 |
@Override |
|
154 |
public Signature getSignature() { |
|
155 |
ForeignCallDescriptor d = linkage.getDescriptor(); |
|
156 |
MetaAccessProvider metaAccess = providers.getMetaAccess(); |
|
157 |
Class<?>[] arguments = d.getArgumentTypes(); |
|
158 |
ResolvedJavaType[] parameters = new ResolvedJavaType[arguments.length]; |
|
159 |
for (int i = 0; i < arguments.length; i++) { |
|
160 |
parameters[i] = metaAccess.lookupJavaType(arguments[i]); |
|
161 |
} |
|
162 |
return new HotSpotSignature(jvmciRuntime, metaAccess.lookupJavaType(d.getResultType()), parameters); |
|
163 |
} |
|
164 |
||
165 |
@Override |
|
166 |
public String getName() { |
|
167 |
return linkage.getDescriptor().getName(); |
|
168 |
} |
|
169 |
||
170 |
@Override |
|
171 |
public JavaType getDeclaringClass() { |
|
172 |
return providers.getMetaAccess().lookupJavaType(ForeignCallStub.class); |
|
173 |
} |
|
174 |
||
175 |
@Override |
|
176 |
public String toString() { |
|
177 |
return format("ForeignCallStub<%n(%p)>"); |
|
178 |
} |
|
179 |
} |
|
180 |
||
181 |
@Override |
|
182 |
protected Object debugScopeContext() { |
|
183 |
return new DebugScopeContext() { |
|
184 |
||
185 |
}; |
|
186 |
} |
|
187 |
||
188 |
/** |
|
189 |
* Creates a graph for this stub. |
|
190 |
* <p> |
|
191 |
* If the stub returns an object, the graph created corresponds to this pseudo code: |
|
192 |
* |
|
193 |
* <pre> |
|
194 |
* Object foreignFunctionStub(args...) { |
|
195 |
* foreignFunction(currentThread, args); |
|
51436 | 196 |
* if ((shouldClearException && clearPendingException(thread())) || (!shouldClearException && hasPendingException(thread)) { |
43972 | 197 |
* getAndClearObjectResult(thread()); |
51436 | 198 |
* DeoptimizeCallerNode.deopt(None, RuntimeConstraint); |
43972 | 199 |
* } |
200 |
* return verifyObject(getAndClearObjectResult(thread())); |
|
201 |
* } |
|
202 |
* </pre> |
|
203 |
* |
|
204 |
* If the stub returns a primitive or word, the graph created corresponds to this pseudo code |
|
205 |
* (using {@code int} as the primitive return type): |
|
206 |
* |
|
207 |
* <pre> |
|
208 |
* int foreignFunctionStub(args...) { |
|
209 |
* int result = foreignFunction(currentThread, args); |
|
51436 | 210 |
* if ((shouldClearException && clearPendingException(thread())) || (!shouldClearException && hasPendingException(thread)) { |
211 |
* DeoptimizeCallerNode.deopt(None, RuntimeConstraint); |
|
43972 | 212 |
* } |
213 |
* return result; |
|
214 |
* } |
|
215 |
* </pre> |
|
216 |
* |
|
217 |
* If the stub is void, the graph created corresponds to this pseudo code: |
|
218 |
* |
|
219 |
* <pre> |
|
220 |
* void foreignFunctionStub(args...) { |
|
221 |
* foreignFunction(currentThread, args); |
|
51436 | 222 |
* if ((shouldClearException && clearPendingException(thread())) || (!shouldClearException && hasPendingException(thread)) { |
223 |
* DeoptimizeCallerNode.deopt(None, RuntimeConstraint); |
|
43972 | 224 |
* } |
225 |
* } |
|
226 |
* </pre> |
|
227 |
* |
|
228 |
* In each example above, the {@code currentThread} argument is the C++ JavaThread value (i.e., |
|
229 |
* %r15 on AMD64) and is only prepended if {@link #prependThread} is true. |
|
230 |
*/ |
|
231 |
@Override |
|
49451 | 232 |
@SuppressWarnings("try") |
46640 | 233 |
protected StructuredGraph getGraph(DebugContext debug, CompilationIdentifier compilationId) { |
43972 | 234 |
WordTypes wordTypes = providers.getWordTypes(); |
235 |
Class<?>[] args = linkage.getDescriptor().getArgumentTypes(); |
|
236 |
boolean isObjectResult = !LIRKind.isValue(linkage.getOutgoingCallingConvention().getReturn()); |
|
51436 | 237 |
// Do we want to clear the pending exception? |
58040
d99af76d7689
8226953: AOT: assert(oopDesc::is_oop(obj)) failed: not an oop
iveresov
parents:
52956
diff
changeset
|
238 |
boolean shouldClearException = linkage.isReexecutable(); |
49451 | 239 |
try { |
52956 | 240 |
HotSpotLoweringProvider lowerer = (HotSpotLoweringProvider) providers.getLowerer(); |
241 |
Templates foreignCallSnippets = lowerer.getForeignCallSnippets(); |
|
242 |
ResolvedJavaMethod handlePendingException = foreignCallSnippets.handlePendingException.getMethod(); |
|
243 |
ResolvedJavaMethod getAndClearObjectResult = foreignCallSnippets.getAndClearObjectResult.getMethod(); |
|
244 |
ResolvedJavaMethod verifyObject = foreignCallSnippets.verifyObject.getMethod(); |
|
245 |
ResolvedJavaMethod thisMethod = getGraphMethod(); |
|
49873 | 246 |
GraphKit kit = new GraphKit(debug, thisMethod, providers, wordTypes, providers.getGraphBuilderPlugins(), compilationId, toString()); |
247 |
StructuredGraph graph = kit.getGraph(); |
|
248 |
ParameterNode[] params = createParameters(kit, args); |
|
249 |
ReadRegisterNode thread = kit.append(new ReadRegisterNode(providers.getRegisters().getThreadRegister(), wordTypes.getWordKind(), true, false)); |
|
250 |
ValueNode result = createTargetCall(kit, params, thread); |
|
52956 | 251 |
createStaticInvoke(kit, handlePendingException, thread, forBoolean(shouldClearException, graph), forBoolean(isObjectResult, graph)); |
49873 | 252 |
if (isObjectResult) { |
52956 | 253 |
InvokeNode object = createStaticInvoke(kit, getAndClearObjectResult, thread); |
254 |
result = createStaticInvoke(kit, verifyObject, object); |
|
49873 | 255 |
} |
256 |
kit.append(new ReturnNode(linkage.getDescriptor().getResultType() == void.class ? null : result)); |
|
257 |
debug.dump(DebugContext.VERBOSE_LEVEL, graph, "Initial stub graph"); |
|
43972 | 258 |
|
49873 | 259 |
kit.inlineInvokes("Foreign call stub.", "Backend"); |
49451 | 260 |
|
49873 | 261 |
debug.dump(DebugContext.VERBOSE_LEVEL, graph, "Stub graph before compilation"); |
262 |
return graph; |
|
49451 | 263 |
} catch (Exception e) { |
264 |
throw GraalError.shouldNotReachHere(e); |
|
43972 | 265 |
} |
266 |
} |
|
267 |
||
52956 | 268 |
private static InvokeNode createStaticInvoke(GraphKit kit, ResolvedJavaMethod method, ValueNode... args) { |
269 |
return kit.createInvoke(method, Static, null, UNKNOWN_BCI, args); |
|
270 |
} |
|
271 |
||
272 |
private ResolvedJavaMethod getGraphMethod() { |
|
273 |
ResolvedJavaMethod thisMethod = null; |
|
274 |
for (ResolvedJavaMethod method : providers.getMetaAccess().lookupJavaType(ForeignCallStub.class).getDeclaredMethods()) { |
|
275 |
if (method.getName().equals("getGraph")) { |
|
276 |
if (thisMethod == null) { |
|
277 |
thisMethod = method; |
|
278 |
} else { |
|
279 |
throw new InternalError("getGraph is ambiguous"); |
|
280 |
} |
|
281 |
} |
|
282 |
} |
|
283 |
if (thisMethod == null) { |
|
284 |
throw new InternalError("Can't find getGraph"); |
|
285 |
} |
|
286 |
return thisMethod; |
|
287 |
} |
|
288 |
||
43972 | 289 |
private ParameterNode[] createParameters(GraphKit kit, Class<?>[] args) { |
290 |
ParameterNode[] params = new ParameterNode[args.length]; |
|
291 |
ResolvedJavaType accessingClass = providers.getMetaAccess().lookupJavaType(getClass()); |
|
292 |
for (int i = 0; i < args.length; i++) { |
|
293 |
ResolvedJavaType type = providers.getMetaAccess().lookupJavaType(args[i]).resolve(accessingClass); |
|
294 |
StampPair stamp = StampFactory.forDeclaredType(kit.getGraph().getAssumptions(), type, false); |
|
295 |
ParameterNode param = kit.unique(new ParameterNode(i, stamp)); |
|
296 |
params[i] = param; |
|
297 |
} |
|
298 |
return params; |
|
299 |
} |
|
300 |
||
301 |
private StubForeignCallNode createTargetCall(GraphKit kit, ParameterNode[] params, ReadRegisterNode thread) { |
|
46371 | 302 |
Stamp stamp = StampFactory.forKind(JavaKind.fromJavaClass(target.getDescriptor().getResultType())); |
43972 | 303 |
if (prependThread) { |
304 |
ValueNode[] targetArguments = new ValueNode[1 + params.length]; |
|
305 |
targetArguments[0] = thread; |
|
306 |
System.arraycopy(params, 0, targetArguments, 1, params.length); |
|
46371 | 307 |
return kit.append(new StubForeignCallNode(providers.getForeignCalls(), stamp, target.getDescriptor(), targetArguments)); |
43972 | 308 |
} else { |
46371 | 309 |
return kit.append(new StubForeignCallNode(providers.getForeignCalls(), stamp, target.getDescriptor(), params)); |
43972 | 310 |
} |
311 |
} |
|
312 |
} |