Add codegen for entrypoint method & fixes

This commit is contained in:
Henry 2019-12-20 14:54:29 +00:00
parent fcf65eb627
commit 1b48c14a83
5 changed files with 474 additions and 7 deletions

View file

@ -4,7 +4,19 @@ public class CodegenImpl implements Codegen {
public CodegenImpl() {}
public String codegen(Program p) throws CodegenException {
return "";
StringBuilder builder = new StringBuilder();
Declaration main = null;
for (Declaration d : p.decls) {
if (main == null) {
main = d;
}
builder.append(compileDeclaration(d));
}
return builder.toString()
+ "call " + String.format("%s_%d:\n", main.id, main.numOfArgs);
}
private String generateLabel() {
@ -88,15 +100,16 @@ public class CodegenImpl implements Codegen {
String compLabel = generateLabel();
String bodyLabel = generateLabel();
String exitLabel = generateLabel();
String contLabel = generateLabel();
String breakLabel = generateLabel();
return "sw s2,-8(sp)\n"
+ "sw s3,-4(sp)\n"
+ "addi sp,sp,-8\n"
+ "la s2," + bodyLabel + "\n" // TODO: does this jump to the check?
+ "la s2," + contLabel + "\n"
+ "la s3," + breakLabel + "\n"
+ bodyLabel + ": " + compileExp(eUntil.body)
+ "sw s4,-4(sp)\n"
+ contLabel + ": sw s4,-4(sp)\n"
+ "addi sp,sp,-4\n"
+ "mv s4,a0\n"
+ compileFullComp(eUntil.l, eUntil.r, eUntil.comp, exitLabel, compLabel)
@ -136,7 +149,7 @@ public class CodegenImpl implements Codegen {
StringBuilder builder = new StringBuilder();
// Store return address & current stack pointer
builder.append("mv s3,sp\n");
builder.append("mv s7,sp\n");
builder.append("sw ra,-4(sp)\n");
builder.append("addi sp,sp,-4\n");
@ -152,7 +165,7 @@ public class CodegenImpl implements Codegen {
// push old frame pointer & start-of-frame pointer
builder.append("sw fp,-4(sp)\n");
builder.append("sw s3,-8(sp)\n");
builder.append("sw s7,-8(sp)\n");
builder.append("addi sp,sp,-8\n");
builder.append(