diff --git a/substratevm/src/com.oracle.svm.core.foreign/src/com/oracle/svm/core/foreign/AbiUtils.java b/substratevm/src/com.oracle.svm.core.foreign/src/com/oracle/svm/core/foreign/AbiUtils.java index 41f141598e89..5f7508fce6d5 100644 --- a/substratevm/src/com.oracle.svm.core.foreign/src/com/oracle/svm/core/foreign/AbiUtils.java +++ b/substratevm/src/com.oracle.svm.core.foreign/src/com/oracle/svm/core/foreign/AbiUtils.java @@ -511,9 +511,22 @@ protected CallingSequence makeCallingSequence(MethodType type, FunctionDescripto @Override protected List generateAdaptations(NativeEntryPointInfo nep) { var adaptations = super.generateAdaptations(nep); - /* Drop the rax parametersAssignment */ - assert adaptations.get(adaptations.size() - 1) == null; - adaptations.set(adaptations.size() - 1, Adapter.drop()); + var assignments = nep.parametersAssignment(); + + if (assignments.length > 0) { + final int last = assignments.length - 1; + if (assignments[last].equals(X86_64Architecture.Regs.rax)) { + /* + * This branch is only taken when the function is variadic, that is when rax is + * passed as an additional pseudo-parameter, where it will contain the number of + * XMM registers passed as arguments. However, we need to remove the rax + * assignment since rax will already be assigned separately in + * SubstrateAMD64RegisterConfig.getCallingConvention and later used in + * SubstrateAMD64NodeLIRBuilder.visitInvokeArguments. + */ + adaptations.set(last, Adapter.drop()); + } + } return adaptations; } diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/graal/snippets/CFunctionSnippets.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/graal/snippets/CFunctionSnippets.java index e94411963ce5..0b8463b956ab 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/graal/snippets/CFunctionSnippets.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/graal/snippets/CFunctionSnippets.java @@ -30,15 +30,12 @@ import org.graalvm.compiler.api.replacements.Snippet; import org.graalvm.compiler.api.replacements.Snippet.ConstantParameter; -import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor; import org.graalvm.compiler.graph.Node; import org.graalvm.compiler.nodes.FixedNode; import org.graalvm.compiler.nodes.FixedWithNextNode; import org.graalvm.compiler.nodes.Invoke; import org.graalvm.compiler.nodes.InvokeNode; import org.graalvm.compiler.nodes.InvokeWithExceptionNode; -import org.graalvm.compiler.nodes.ValueNode; -import org.graalvm.compiler.nodes.extended.ForeignCallNode; import org.graalvm.compiler.nodes.extended.MembarNode; import org.graalvm.compiler.nodes.spi.LoweringTool; import org.graalvm.compiler.options.OptionValues; @@ -50,7 +47,6 @@ import org.graalvm.compiler.replacements.Snippets; import org.graalvm.nativeimage.Platforms; import org.graalvm.nativeimage.c.struct.SizeOf; -import org.graalvm.nativeimage.c.type.CIntPointer; import org.graalvm.nativeimage.impl.InternalPlatform; import org.graalvm.word.LocationIdentity; @@ -62,7 +58,6 @@ import com.oracle.svm.core.graal.nodes.VerificationMarkerNode; import com.oracle.svm.core.graal.stackvalue.LoweredStackValueNode; import com.oracle.svm.core.graal.stackvalue.StackValueNode.StackSlotIdentity; -import com.oracle.svm.core.nodes.CFunctionCaptureNode; import com.oracle.svm.core.nodes.CFunctionEpilogueNode; import com.oracle.svm.core.nodes.CFunctionPrologueDataNode; import com.oracle.svm.core.nodes.CFunctionPrologueNode; @@ -98,7 +93,6 @@ public final class CFunctionSnippets extends SubstrateTemplates implements Snippets { private final SnippetInfo prologue; - private final SnippetInfo capture; private final SnippetInfo epilogue; /** @@ -123,14 +117,6 @@ private static CPrologueData prologueSnippet(@ConstantParameter int newThreadSta return CFunctionPrologueDataNode.cFunctionPrologueData(anchor, newThreadStatus); } - @Node.NodeIntrinsic(value = ForeignCallNode.class) - public static native void callCaptureFunction(@Node.ConstantNodeParameter ForeignCallDescriptor descriptor, int states, CIntPointer captureBuffer); - - @Snippet - private static void captureSnippet(@ConstantParameter ForeignCallDescriptor captureFunction, int statesToCapture, CIntPointer captureBuffer) { - callCaptureFunction(captureFunction, statesToCapture, captureBuffer); - } - @Snippet private static void epilogueSnippet(@ConstantParameter int oldThreadStatus) { if (SubstrateOptions.MultiThreaded.getValue()) { @@ -157,11 +143,9 @@ private static void epilogueSnippet(@ConstantParameter int oldThreadStatus) { super(options, providers); this.prologue = snippet(providers, CFunctionSnippets.class, "prologueSnippet"); - this.capture = snippet(providers, CFunctionSnippets.class, "captureSnippet"); this.epilogue = snippet(providers, CFunctionSnippets.class, "epilogueSnippet"); lowerings.put(CFunctionPrologueNode.class, new CFunctionPrologueLowering()); - lowerings.put(CFunctionCaptureNode.class, new CFunctionCaptureLowering()); lowerings.put(CFunctionEpilogueNode.class, new CFunctionEpilogueLowering()); } @@ -193,27 +177,6 @@ public void lower(CFunctionPrologueNode node, LoweringTool tool) { } } - class CFunctionCaptureLowering implements NodeLoweringProvider { - @Override - public void lower(CFunctionCaptureNode node, LoweringTool tool) { - if (tool.getLoweringStage() != LoweringTool.StandardLoweringStage.LOW_TIER) { - return; - } - - ValueNode statesToCapture = node.getStatesToCapture(); - ForeignCallDescriptor captureFunction = node.getCaptureFunction(); - ValueNode buffer = node.getCaptureBuffer(); - Arguments args = new Arguments(capture, node.graph().getGuardsStage(), tool.getLoweringStage()); - args.addConst("captureFunction", captureFunction); - args.add("statesToCapture", statesToCapture); - args.add("captureBuffer", buffer); - - SnippetTemplate template = template(tool, node, args); - template.setMayRemoveLocation(true); - template.instantiate(tool.getMetaAccess(), node, SnippetTemplate.DEFAULT_REPLACER, args); - } - } - class CFunctionEpilogueLowering implements NodeLoweringProvider { @Override public void lower(CFunctionEpilogueNode node, LoweringTool tool) { diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/nodes/CFunctionCaptureNode.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/nodes/CFunctionCaptureNode.java index 43319828d00c..fc7b0b255cf3 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/nodes/CFunctionCaptureNode.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/nodes/CFunctionCaptureNode.java @@ -37,8 +37,10 @@ import org.graalvm.compiler.nodeinfo.NodeInfo; import org.graalvm.compiler.nodes.FixedWithNextNode; import org.graalvm.compiler.nodes.ValueNode; +import org.graalvm.compiler.nodes.extended.ForeignCallNode; import org.graalvm.compiler.nodes.memory.SingleMemoryKill; import org.graalvm.compiler.nodes.spi.Lowerable; +import org.graalvm.compiler.nodes.spi.LoweringTool; import org.graalvm.word.LocationIdentity; /** @@ -76,15 +78,13 @@ public LocationIdentity getKilledLocationIdentity() { return LocationIdentity.any(); } - public ForeignCallDescriptor getCaptureFunction() { - return captureFunction; - } - - public ValueNode getStatesToCapture() { - return statesToCapture; - } + @Override + public void lower(LoweringTool tool) { + if (tool.getLoweringStage() != LoweringTool.StandardLoweringStage.LOW_TIER) { + return; + } - public ValueNode getCaptureBuffer() { - return captureBuffer; + final ForeignCallNode call = graph().add(new ForeignCallNode(captureFunction, statesToCapture, captureBuffer)); + graph().replaceFixedWithFixed(this, call); } }