From 0c0396650957f86c5c7a6fc9181f3384ef33e0d5 Mon Sep 17 00:00:00 2001 From: Henry Date: Fri, 20 Dec 2019 17:19:31 +0000 Subject: [PATCH] Always return stack pointer to bottom of frame when method returns --- task1/CodegenImpl.java | 3 +++ task2/CodegenImpl.java | 3 +++ task3/CodegenImpl.java | 3 +++ 3 files changed, 9 insertions(+) diff --git a/task1/CodegenImpl.java b/task1/CodegenImpl.java index c0b1c37..c2c87d4 100644 --- a/task1/CodegenImpl.java +++ b/task1/CodegenImpl.java @@ -176,6 +176,9 @@ public class CodegenImpl implements Codegen { // set frame pointer to start of arguments "addi s0,sp,8\n" + "call " + methodLabel + "\n" + // restore stack to frame pointer - 8 (bottom of frame) + // this ensures any stack growth inside the method is undone + + "addi sp,s0,-8\n" // restore old frame pointer & stack pointer + "lw s0,4(sp)\n" + "lw sp,(sp)\n" diff --git a/task2/CodegenImpl.java b/task2/CodegenImpl.java index c0b1c37..c2c87d4 100644 --- a/task2/CodegenImpl.java +++ b/task2/CodegenImpl.java @@ -176,6 +176,9 @@ public class CodegenImpl implements Codegen { // set frame pointer to start of arguments "addi s0,sp,8\n" + "call " + methodLabel + "\n" + // restore stack to frame pointer - 8 (bottom of frame) + // this ensures any stack growth inside the method is undone + + "addi sp,s0,-8\n" // restore old frame pointer & stack pointer + "lw s0,4(sp)\n" + "lw sp,(sp)\n" diff --git a/task3/CodegenImpl.java b/task3/CodegenImpl.java index c0b1c37..c2c87d4 100644 --- a/task3/CodegenImpl.java +++ b/task3/CodegenImpl.java @@ -176,6 +176,9 @@ public class CodegenImpl implements Codegen { // set frame pointer to start of arguments "addi s0,sp,8\n" + "call " + methodLabel + "\n" + // restore stack to frame pointer - 8 (bottom of frame) + // this ensures any stack growth inside the method is undone + + "addi sp,s0,-8\n" // restore old frame pointer & stack pointer + "lw s0,4(sp)\n" + "lw sp,(sp)\n"