From 221b928d0e4551ffffd98536de9056221444dc39 Mon Sep 17 00:00:00 2001 From: nitrix Date: Tue, 10 Mar 2020 12:07:55 +0100 Subject: [PATCH] push all args onto stack before moving them into the local var to ensure that the function parameters can be used in the tail recursive function call --- .../java/de/hsrm/compiler/Klang/visitors/GenASM.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/hsrm/compiler/Klang/visitors/GenASM.java b/src/main/java/de/hsrm/compiler/Klang/visitors/GenASM.java index 126845d..89df0ed 100644 --- a/src/main/java/de/hsrm/compiler/Klang/visitors/GenASM.java +++ b/src/main/java/de/hsrm/compiler/Klang/visitors/GenASM.java @@ -695,16 +695,20 @@ public class GenASM implements Visitor { public Void visit(FunctionCall e) { if (e.isTailRecursive) { - // Visit the arguments and move them into the location of the corresponding local var + // Visit the arguments and move them into the stack for(int i = 0; i < e.arguments.length; i++) { e.arguments[i].welcome(this); - int offset = this.env.get(this.currentFunctionParams[i].name); if (e.arguments[i].type.equals(Type.getFloatType())) { this.ex.write(" movq %xmm0, %rax\n"); } - this.ex.write(" movq %rax, " + offset + "(%rbp)\n"); + this.ex.write(" pushq %rax\n"); + } + + // push args into local var locations, last arg is ontop of the stack + for (int i = e.arguments.length - 1; i >= 0; i--) { + this.ex.write(" popq " + this.env.get(this.currentFunctionParams[i].name) + "(%rbp)\n"); } this.ex.write(" jmp .L" + this.currentFunctionStartLabel + "\n");