diff --git a/task1/CodegenImpl.java b/task1/CodegenImpl.java index a78f48b..c3f1139 100644 --- a/task1/CodegenImpl.java +++ b/task1/CodegenImpl.java @@ -148,24 +148,24 @@ public class CodegenImpl implements Codegen { String methodLabel = String.format("%s_%d", call.name, argLen); StringBuilder builder = new StringBuilder(); - // Store return address & current stack pointer - builder.append("mv s7,sp\n"); + // Store return address builder.append("sw ra,-4(sp)\n"); - builder.append("addi sp,sp,-4\n"); + builder.append("mv t0,sp\n"); + // shuffle stack pointer to start of arguments + builder.append(String.format("addi sp,sp,-%d\n", (argLen + 1) * 4)); + builder.append("sw t0,-8(sp)\n"); // store old stack pointer (top of frame) for (int i = argLen - 1; i >= 0; i--) { Exp argExp = call.args.get(i); builder.append(compileExp(argExp)); builder.append( - "sw a0,-4(sp)\n" - + "addi sp,sp,-4\n" + String.format("sw a0,%d(sp)\n", i*4) ); } - // push old frame pointer & start-of-frame pointer + // push old frame pointer builder.append("sw fp,-4(sp)\n"); - builder.append("sw s7,-8(sp)\n"); builder.append("addi sp,sp,-8\n"); builder.append( @@ -175,6 +175,7 @@ public class CodegenImpl implements Codegen { // restore old frame pointer & stack pointer + "lw fp,4(sp)\n" + "lw sp,(sp)\n" + // restore return address of caller + "lw ra,-4(sp)\n" ); diff --git a/task2/CodegenImpl.java b/task2/CodegenImpl.java index a78f48b..c3f1139 100644 --- a/task2/CodegenImpl.java +++ b/task2/CodegenImpl.java @@ -148,24 +148,24 @@ public class CodegenImpl implements Codegen { String methodLabel = String.format("%s_%d", call.name, argLen); StringBuilder builder = new StringBuilder(); - // Store return address & current stack pointer - builder.append("mv s7,sp\n"); + // Store return address builder.append("sw ra,-4(sp)\n"); - builder.append("addi sp,sp,-4\n"); + builder.append("mv t0,sp\n"); + // shuffle stack pointer to start of arguments + builder.append(String.format("addi sp,sp,-%d\n", (argLen + 1) * 4)); + builder.append("sw t0,-8(sp)\n"); // store old stack pointer (top of frame) for (int i = argLen - 1; i >= 0; i--) { Exp argExp = call.args.get(i); builder.append(compileExp(argExp)); builder.append( - "sw a0,-4(sp)\n" - + "addi sp,sp,-4\n" + String.format("sw a0,%d(sp)\n", i*4) ); } - // push old frame pointer & start-of-frame pointer + // push old frame pointer builder.append("sw fp,-4(sp)\n"); - builder.append("sw s7,-8(sp)\n"); builder.append("addi sp,sp,-8\n"); builder.append( @@ -175,6 +175,7 @@ public class CodegenImpl implements Codegen { // restore old frame pointer & stack pointer + "lw fp,4(sp)\n" + "lw sp,(sp)\n" + // restore return address of caller + "lw ra,-4(sp)\n" ); diff --git a/task3/CodegenImpl.java b/task3/CodegenImpl.java index a78f48b..c3f1139 100644 --- a/task3/CodegenImpl.java +++ b/task3/CodegenImpl.java @@ -148,24 +148,24 @@ public class CodegenImpl implements Codegen { String methodLabel = String.format("%s_%d", call.name, argLen); StringBuilder builder = new StringBuilder(); - // Store return address & current stack pointer - builder.append("mv s7,sp\n"); + // Store return address builder.append("sw ra,-4(sp)\n"); - builder.append("addi sp,sp,-4\n"); + builder.append("mv t0,sp\n"); + // shuffle stack pointer to start of arguments + builder.append(String.format("addi sp,sp,-%d\n", (argLen + 1) * 4)); + builder.append("sw t0,-8(sp)\n"); // store old stack pointer (top of frame) for (int i = argLen - 1; i >= 0; i--) { Exp argExp = call.args.get(i); builder.append(compileExp(argExp)); builder.append( - "sw a0,-4(sp)\n" - + "addi sp,sp,-4\n" + String.format("sw a0,%d(sp)\n", i*4) ); } - // push old frame pointer & start-of-frame pointer + // push old frame pointer builder.append("sw fp,-4(sp)\n"); - builder.append("sw s7,-8(sp)\n"); builder.append("addi sp,sp,-8\n"); builder.append( @@ -175,6 +175,7 @@ public class CodegenImpl implements Codegen { // restore old frame pointer & stack pointer + "lw fp,4(sp)\n" + "lw sp,(sp)\n" + // restore return address of caller + "lw ra,-4(sp)\n" );