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 24 25 package org.graalvm.compiler.hotspot.stubs; 26 27 import static jdk.vm.ci.hotspot.HotSpotCallingConventionType.JavaCall; 28 import static jdk.vm.ci.hotspot.HotSpotCallingConventionType.JavaCallee; 29 import static jdk.vm.ci.hotspot.HotSpotCallingConventionType.NativeCall; 30 import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.RegisterEffect.DESTROYS_REGISTERS; 31 import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.RegisterEffect.PRESERVES_REGISTERS; 32 33 import org.graalvm.compiler.core.common.CompilationIdentifier; 34 import org.graalvm.compiler.core.common.LIRKind; 35 import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor; 36 import org.graalvm.compiler.core.common.type.Stamp; 37 import org.graalvm.compiler.core.common.type.StampFactory; 38 import org.graalvm.compiler.core.common.type.StampPair; 39 import org.graalvm.compiler.debug.DebugContext; 40 import org.graalvm.compiler.debug.GraalError; 41 import org.graalvm.compiler.debug.JavaMethodContext; 42 import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage; 43 import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.Reexecutability; 44 import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.Transition; 45 import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkageImpl; 46 import org.graalvm.compiler.hotspot.meta.HotSpotProviders; 47 import org.graalvm.compiler.hotspot.nodes.StubForeignCallNode; 48 import org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil; 49 import org.graalvm.compiler.nodes.ConstantNode; 50 import org.graalvm.compiler.nodes.InvokeNode; 51 import org.graalvm.compiler.nodes.ParameterNode; 52 import org.graalvm.compiler.nodes.ReturnNode; 53 import org.graalvm.compiler.nodes.StructuredGraph; 54 import org.graalvm.compiler.nodes.ValueNode; 55 import org.graalvm.compiler.options.OptionValues; 56 import org.graalvm.compiler.phases.common.RemoveValueProxyPhase; 57 import org.graalvm.compiler.replacements.GraphKit; 58 import org.graalvm.compiler.replacements.nodes.ReadRegisterNode; 59 import org.graalvm.compiler.word.Word; 60 import org.graalvm.compiler.word.WordTypes; 61 import jdk.internal.vm.compiler.word.LocationIdentity; 62 63 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime; 64 import jdk.vm.ci.hotspot.HotSpotSignature; 65 import jdk.vm.ci.meta.JavaKind; 66 import jdk.vm.ci.meta.JavaMethod; 67 import jdk.vm.ci.meta.JavaType; 68 import jdk.vm.ci.meta.MetaAccessProvider; 69 import jdk.vm.ci.meta.ResolvedJavaMethod; 70 import jdk.vm.ci.meta.ResolvedJavaType; 71 import jdk.vm.ci.meta.Signature; 72 73 /** 74 * A {@linkplain #getGraph generated} stub for a {@link Transition non-leaf} foreign call from 75 * compiled code. A stub is required for such calls as the caller may be scheduled for 76 * deoptimization while the call is in progress. And since these are foreign/runtime calls on slow 77 * paths, we don't want to force the register allocator to spill around the call. As such, this stub 78 * saves and restores all allocatable registers. It also 79 * {@linkplain StubUtil#handlePendingException(Word, boolean, boolean) handles} any exceptions 80 * raised during the foreign call. 81 */ 82 public class ForeignCallStub extends Stub { 83 84 private final HotSpotJVMCIRuntime jvmciRuntime; 85 86 /** 87 * The target of the call. 88 */ 89 private final HotSpotForeignCallLinkage target; 90 91 /** 92 * Specifies if the JavaThread value for the current thread is to be prepended to the arguments 93 * for the call to {@link #target}. 94 */ 95 protected final boolean prependThread; 96 97 /** 98 * Creates a stub for a call to code at a given address. 99 * 100 * @param address the address of the code to call 101 * @param descriptor the signature of the call to this stub 102 * @param prependThread true if the JavaThread value for the current thread is to be prepended 103 * to the arguments for the call to {@code address} 104 * @param reexecutability specifies if the stub call can be re-executed without (meaningful) 105 * side effects. Deoptimization will not return to a point before a stub call that 106 * cannot be re-executed. 107 * @param killedLocations the memory locations killed by the stub call 108 */ 109 public ForeignCallStub(OptionValues options, HotSpotJVMCIRuntime runtime, HotSpotProviders providers, long address, ForeignCallDescriptor descriptor, boolean prependThread, 110 Transition transition, Reexecutability reexecutability, LocationIdentity... killedLocations) { 111 super(options, providers, HotSpotForeignCallLinkageImpl.create(providers.getMetaAccess(), providers.getCodeCache(), providers.getWordTypes(), providers.getForeignCalls(), descriptor, 0L, 112 PRESERVES_REGISTERS, JavaCall, JavaCallee, transition, reexecutability, killedLocations)); 113 this.jvmciRuntime = runtime; 114 this.prependThread = prependThread; 115 Class<?>[] targetParameterTypes = createTargetParameters(descriptor); 116 ForeignCallDescriptor targetSig = new ForeignCallDescriptor(descriptor.getName() + ":C", descriptor.getResultType(), targetParameterTypes); 117 target = HotSpotForeignCallLinkageImpl.create(providers.getMetaAccess(), providers.getCodeCache(), providers.getWordTypes(), providers.getForeignCalls(), targetSig, address, 118 DESTROYS_REGISTERS, NativeCall, NativeCall, transition, reexecutability, killedLocations); 119 } 120 121 /** 122 * Gets the linkage information for the call from this stub. 123 */ 124 public HotSpotForeignCallLinkage getTargetLinkage() { 125 return target; 126 } 127 128 private Class<?>[] createTargetParameters(ForeignCallDescriptor descriptor) { 129 Class<?>[] parameters = descriptor.getArgumentTypes(); 130 if (prependThread) { 131 Class<?>[] newParameters = new Class<?>[parameters.length + 1]; 132 System.arraycopy(parameters, 0, newParameters, 1, parameters.length); 133 newParameters[0] = Word.class; 134 return newParameters; 135 } 136 return parameters; 137 } 217 * void foreignFunctionStub(args...) { 218 * foreignFunction(currentThread, args); 219 * if ((shouldClearException && clearPendingException(thread())) || (!shouldClearException && hasPendingException(thread)) { 220 * DeoptimizeCallerNode.deopt(None, RuntimeConstraint); 221 * } 222 * } 223 * </pre> 224 * 225 * In each example above, the {@code currentThread} argument is the C++ JavaThread value (i.e., 226 * %r15 on AMD64) and is only prepended if {@link #prependThread} is true. 227 */ 228 @Override 229 @SuppressWarnings("try") 230 protected StructuredGraph getGraph(DebugContext debug, CompilationIdentifier compilationId) { 231 WordTypes wordTypes = providers.getWordTypes(); 232 Class<?>[] args = linkage.getDescriptor().getArgumentTypes(); 233 boolean isObjectResult = !LIRKind.isValue(linkage.getOutgoingCallingConvention().getReturn()); 234 // Do we want to clear the pending exception? 235 boolean shouldClearException = linkage.isReexecutable() || linkage.isReexecutableOnlyAfterException(); 236 try { 237 ResolvedJavaMethod thisMethod = providers.getMetaAccess().lookupJavaMethod(ForeignCallStub.class.getDeclaredMethod("getGraph", DebugContext.class, CompilationIdentifier.class)); 238 GraphKit kit = new GraphKit(debug, thisMethod, providers, wordTypes, providers.getGraphBuilderPlugins(), compilationId, toString()); 239 StructuredGraph graph = kit.getGraph(); 240 ParameterNode[] params = createParameters(kit, args); 241 ReadRegisterNode thread = kit.append(new ReadRegisterNode(providers.getRegisters().getThreadRegister(), wordTypes.getWordKind(), true, false)); 242 ValueNode result = createTargetCall(kit, params, thread); 243 kit.createInvoke(StubUtil.class, "handlePendingException", thread, ConstantNode.forBoolean(shouldClearException, graph), ConstantNode.forBoolean(isObjectResult, graph)); 244 if (isObjectResult) { 245 InvokeNode object = kit.createInvoke(HotSpotReplacementsUtil.class, "getAndClearObjectResult", thread); 246 result = kit.createInvoke(StubUtil.class, "verifyObject", object); 247 } 248 kit.append(new ReturnNode(linkage.getDescriptor().getResultType() == void.class ? null : result)); 249 debug.dump(DebugContext.VERBOSE_LEVEL, graph, "Initial stub graph"); 250 251 kit.inlineInvokes("Foreign call stub.", "Backend"); 252 new RemoveValueProxyPhase().apply(graph); 253 254 debug.dump(DebugContext.VERBOSE_LEVEL, graph, "Stub graph before compilation"); 255 return graph; 256 } catch (Exception e) { 257 throw GraalError.shouldNotReachHere(e); 258 } 259 } 260 261 private ParameterNode[] createParameters(GraphKit kit, Class<?>[] args) { 262 ParameterNode[] params = new ParameterNode[args.length]; 263 ResolvedJavaType accessingClass = providers.getMetaAccess().lookupJavaType(getClass()); 264 for (int i = 0; i < args.length; i++) { 265 ResolvedJavaType type = providers.getMetaAccess().lookupJavaType(args[i]).resolve(accessingClass); 266 StampPair stamp = StampFactory.forDeclaredType(kit.getGraph().getAssumptions(), type, false); 267 ParameterNode param = kit.unique(new ParameterNode(i, stamp)); 268 params[i] = param; 269 } 270 return params; 271 } 272 273 private StubForeignCallNode createTargetCall(GraphKit kit, ParameterNode[] params, ReadRegisterNode thread) { 274 Stamp stamp = StampFactory.forKind(JavaKind.fromJavaClass(target.getDescriptor().getResultType())); 275 if (prependThread) { 276 ValueNode[] targetArguments = new ValueNode[1 + params.length]; 277 targetArguments[0] = thread; 278 System.arraycopy(params, 0, targetArguments, 1, params.length); 279 return kit.append(new StubForeignCallNode(providers.getForeignCalls(), stamp, target.getDescriptor(), targetArguments)); 280 } else { | 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 24 25 package org.graalvm.compiler.hotspot.stubs; 26 27 import static jdk.vm.ci.code.BytecodeFrame.UNKNOWN_BCI; 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; 31 import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.RegisterEffect.DESTROYS_REGISTERS; 32 import static org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.RegisterEffect.PRESERVES_REGISTERS; 33 import static org.graalvm.compiler.nodes.CallTargetNode.InvokeKind.Static; 34 import static org.graalvm.compiler.nodes.ConstantNode.forBoolean; 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; 39 import org.graalvm.compiler.core.common.type.Stamp; 40 import org.graalvm.compiler.core.common.type.StampFactory; 41 import org.graalvm.compiler.core.common.type.StampPair; 42 import org.graalvm.compiler.debug.DebugContext; 43 import org.graalvm.compiler.debug.GraalError; 44 import org.graalvm.compiler.debug.JavaMethodContext; 45 import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage; 46 import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.Reexecutability; 47 import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage.Transition; 48 import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkageImpl; 49 import org.graalvm.compiler.hotspot.meta.HotSpotLoweringProvider; 50 import org.graalvm.compiler.hotspot.meta.HotSpotProviders; 51 import org.graalvm.compiler.hotspot.nodes.StubForeignCallNode; 52 import org.graalvm.compiler.hotspot.stubs.ForeignCallSnippets.Templates; 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; 58 import org.graalvm.compiler.options.OptionValues; 59 import org.graalvm.compiler.phases.common.RemoveValueProxyPhase; 60 import org.graalvm.compiler.replacements.GraphKit; 61 import org.graalvm.compiler.replacements.nodes.ReadRegisterNode; 62 import org.graalvm.compiler.word.Word; 63 import org.graalvm.compiler.word.WordTypes; 64 import jdk.internal.vm.compiler.word.LocationIdentity; 65 66 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime; 67 import jdk.vm.ci.hotspot.HotSpotSignature; 68 import jdk.vm.ci.meta.JavaKind; 69 import jdk.vm.ci.meta.JavaMethod; 70 import jdk.vm.ci.meta.JavaType; 71 import jdk.vm.ci.meta.MetaAccessProvider; 72 import jdk.vm.ci.meta.ResolvedJavaMethod; 73 import jdk.vm.ci.meta.ResolvedJavaType; 74 import jdk.vm.ci.meta.Signature; 75 76 /** 77 * A {@linkplain #getGraph generated} stub for a {@link Transition non-leaf} foreign call from 78 * compiled code. A stub is required for such calls as the caller may be scheduled for 79 * deoptimization while the call is in progress. And since these are foreign/runtime calls on slow 80 * paths, we don't want to force the register allocator to spill around the call. As such, this stub 81 * saves and restores all allocatable registers. It also 82 * {@linkplain ForeignCallSnippets#handlePendingException handles} any exceptions raised during the 83 * foreign call. 84 */ 85 public class ForeignCallStub extends Stub { 86 87 private final HotSpotJVMCIRuntime jvmciRuntime; 88 89 /** 90 * The target of the call. 91 */ 92 private final HotSpotForeignCallLinkage target; 93 94 /** 95 * Specifies if the JavaThread value for the current thread is to be prepended to the arguments 96 * for the call to {@link #target}. 97 */ 98 protected final boolean prependThread; 99 100 /** 101 * Creates a stub for a call to code at a given address. 102 * 103 * @param address the address of the code to call 104 * @param descriptor the signature of the call to this stub 105 * @param prependThread true if the JavaThread value for the current thread is to be prepended 106 * to the arguments for the call to {@code address} 107 * @param reexecutability specifies if the stub call can be re-executed without (meaningful) 108 * side effects. Deoptimization will not return to a point before a stub call that 109 * cannot be re-executed. 110 * @param killedLocations the memory locations killed by the stub call 111 */ 112 public ForeignCallStub(OptionValues options, HotSpotJVMCIRuntime runtime, HotSpotProviders providers, long address, ForeignCallDescriptor descriptor, boolean prependThread, 113 Transition transition, Reexecutability reexecutability, LocationIdentity... killedLocations) { 114 super(options, providers, HotSpotForeignCallLinkageImpl.create(providers.getMetaAccess(), providers.getCodeCache(), providers.getWordTypes(), providers.getForeignCalls(), descriptor, 0L, 115 PRESERVES_REGISTERS, JavaCall, JavaCallee, transition, reexecutability, killedLocations)); 116 this.jvmciRuntime = runtime; 117 this.prependThread = prependThread; 118 MetaAccessProvider metaAccess = providers.getMetaAccess(); 119 Class<?>[] targetParameterTypes = createTargetParameters(descriptor); 120 ForeignCallDescriptor targetSig = new ForeignCallDescriptor(descriptor.getName() + ":C", descriptor.getResultType(), targetParameterTypes); 121 target = HotSpotForeignCallLinkageImpl.create(metaAccess, providers.getCodeCache(), providers.getWordTypes(), providers.getForeignCalls(), targetSig, address, 122 DESTROYS_REGISTERS, NativeCall, NativeCall, transition, reexecutability, killedLocations); 123 } 124 125 /** 126 * Gets the linkage information for the call from this stub. 127 */ 128 public HotSpotForeignCallLinkage getTargetLinkage() { 129 return target; 130 } 131 132 private Class<?>[] createTargetParameters(ForeignCallDescriptor descriptor) { 133 Class<?>[] parameters = descriptor.getArgumentTypes(); 134 if (prependThread) { 135 Class<?>[] newParameters = new Class<?>[parameters.length + 1]; 136 System.arraycopy(parameters, 0, newParameters, 1, parameters.length); 137 newParameters[0] = Word.class; 138 return newParameters; 139 } 140 return parameters; 141 } 221 * void foreignFunctionStub(args...) { 222 * foreignFunction(currentThread, args); 223 * if ((shouldClearException && clearPendingException(thread())) || (!shouldClearException && hasPendingException(thread)) { 224 * DeoptimizeCallerNode.deopt(None, RuntimeConstraint); 225 * } 226 * } 227 * </pre> 228 * 229 * In each example above, the {@code currentThread} argument is the C++ JavaThread value (i.e., 230 * %r15 on AMD64) and is only prepended if {@link #prependThread} is true. 231 */ 232 @Override 233 @SuppressWarnings("try") 234 protected StructuredGraph getGraph(DebugContext debug, CompilationIdentifier compilationId) { 235 WordTypes wordTypes = providers.getWordTypes(); 236 Class<?>[] args = linkage.getDescriptor().getArgumentTypes(); 237 boolean isObjectResult = !LIRKind.isValue(linkage.getOutgoingCallingConvention().getReturn()); 238 // Do we want to clear the pending exception? 239 boolean shouldClearException = linkage.isReexecutable() || linkage.isReexecutableOnlyAfterException(); 240 try { 241 HotSpotLoweringProvider lowerer = (HotSpotLoweringProvider) providers.getLowerer(); 242 Templates foreignCallSnippets = lowerer.getForeignCallSnippets(); 243 ResolvedJavaMethod handlePendingException = foreignCallSnippets.handlePendingException.getMethod(); 244 ResolvedJavaMethod getAndClearObjectResult = foreignCallSnippets.getAndClearObjectResult.getMethod(); 245 ResolvedJavaMethod verifyObject = foreignCallSnippets.verifyObject.getMethod(); 246 ResolvedJavaMethod thisMethod = getGraphMethod(); 247 GraphKit kit = new GraphKit(debug, thisMethod, providers, wordTypes, providers.getGraphBuilderPlugins(), compilationId, toString()); 248 StructuredGraph graph = kit.getGraph(); 249 ParameterNode[] params = createParameters(kit, args); 250 ReadRegisterNode thread = kit.append(new ReadRegisterNode(providers.getRegisters().getThreadRegister(), wordTypes.getWordKind(), true, false)); 251 ValueNode result = createTargetCall(kit, params, thread); 252 createStaticInvoke(kit, handlePendingException, thread, forBoolean(shouldClearException, graph), forBoolean(isObjectResult, graph)); 253 if (isObjectResult) { 254 InvokeNode object = createStaticInvoke(kit, getAndClearObjectResult, thread); 255 result = createStaticInvoke(kit, verifyObject, object); 256 } 257 kit.append(new ReturnNode(linkage.getDescriptor().getResultType() == void.class ? null : result)); 258 debug.dump(DebugContext.VERBOSE_LEVEL, graph, "Initial stub graph"); 259 260 kit.inlineInvokes("Foreign call stub.", "Backend"); 261 new RemoveValueProxyPhase().apply(graph); 262 263 debug.dump(DebugContext.VERBOSE_LEVEL, graph, "Stub graph before compilation"); 264 return graph; 265 } catch (Exception e) { 266 throw GraalError.shouldNotReachHere(e); 267 } 268 } 269 270 private static InvokeNode createStaticInvoke(GraphKit kit, ResolvedJavaMethod method, ValueNode... args) { 271 return kit.createInvoke(method, Static, null, UNKNOWN_BCI, args); 272 } 273 274 private ResolvedJavaMethod getGraphMethod() { 275 ResolvedJavaMethod thisMethod = null; 276 for (ResolvedJavaMethod method : providers.getMetaAccess().lookupJavaType(ForeignCallStub.class).getDeclaredMethods()) { 277 if (method.getName().equals("getGraph")) { 278 if (thisMethod == null) { 279 thisMethod = method; 280 } else { 281 throw new InternalError("getGraph is ambiguous"); 282 } 283 } 284 } 285 if (thisMethod == null) { 286 throw new InternalError("Can't find getGraph"); 287 } 288 return thisMethod; 289 } 290 291 private ParameterNode[] createParameters(GraphKit kit, Class<?>[] args) { 292 ParameterNode[] params = new ParameterNode[args.length]; 293 ResolvedJavaType accessingClass = providers.getMetaAccess().lookupJavaType(getClass()); 294 for (int i = 0; i < args.length; i++) { 295 ResolvedJavaType type = providers.getMetaAccess().lookupJavaType(args[i]).resolve(accessingClass); 296 StampPair stamp = StampFactory.forDeclaredType(kit.getGraph().getAssumptions(), type, false); 297 ParameterNode param = kit.unique(new ParameterNode(i, stamp)); 298 params[i] = param; 299 } 300 return params; 301 } 302 303 private StubForeignCallNode createTargetCall(GraphKit kit, ParameterNode[] params, ReadRegisterNode thread) { 304 Stamp stamp = StampFactory.forKind(JavaKind.fromJavaClass(target.getDescriptor().getResultType())); 305 if (prependThread) { 306 ValueNode[] targetArguments = new ValueNode[1 + params.length]; 307 targetArguments[0] = thread; 308 System.arraycopy(params, 0, targetArguments, 1, params.length); 309 return kit.append(new StubForeignCallNode(providers.getForeignCalls(), stamp, target.getDescriptor(), targetArguments)); 310 } else { |