Fix invoke stack pointer clobbering

This commit is contained in:
Henry 2019-12-20 15:21:19 +00:00
parent 1b48c14a83
commit 505166d1d6
3 changed files with 24 additions and 21 deletions

View file

@ -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"
);

View file

@ -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"
);

View file

@ -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"
);