diff --git a/src/main/java/chocopy/pa3/CodeGenImpl.java b/src/main/java/chocopy/pa3/CodeGenImpl.java index e2a3558..e2637af 100644 --- a/src/main/java/chocopy/pa3/CodeGenImpl.java +++ b/src/main/java/chocopy/pa3/CodeGenImpl.java @@ -92,7 +92,6 @@ public class CodeGenImpl extends CodeGenBase stmt.dispatch(stmtAnalyzer); } stmtAnalyzer.emitSizeLabel(); - backend.emitLI(A0, EXIT_ECALL, "Code for ecall: exit"); backend.emitEcall(null); } @@ -110,8 +109,6 @@ public class CodeGenImpl extends CodeGenBase { backend.emitGlobalLabel(funcInfo.getCodeLabel()); - - // --- Prologue --- // space for return address = 1 // space for control link = 1 @@ -126,14 +123,14 @@ public class CodeGenImpl extends CodeGenBase //backend.emitSW(FP, SP, requiredStackSpace-12, "static link"); backend.emitADDI(FP, SP, sizelabel, "New fp is at old SP."); - StmtAnalyzer stmtAnalyzer = new StmtAnalyzer(funcInfo); int emptySlotNum = 3; - for (StackVarInfo var : funcInfo.getLocals()) { + for (StackVarInfo var : funcInfo.getLocals()) + { Literal varLiteral = var.getInitialValue(); - varLiteral.dispatch(stmtAnalyzer); + Register reg = varLiteral.dispatch(stmtAnalyzer); // All Literals should save locations for the values in A0 - backend.emitSW(A0, SP, sizelabel+String.format("-%d",emptySlotNum*wordSize), "Push local variable " + var.getVarName() + " onto stack"); + backend.emitSW(reg, SP, sizelabel+String.format("-%d",emptySlotNum*wordSize), "Push local variable " + var.getVarName() + " onto stack"); emptySlotNum++; } @@ -143,13 +140,13 @@ public class CodeGenImpl extends CodeGenBase // statements use fp to load the variables // example: 0(fp) is the last variable (z) while 8(fp) is the first variable (x) // for function with 3 params f(x, y, z) - for (Stmt stmt : funcInfo.getStatements()) { + for (Stmt stmt : funcInfo.getStatements()) + { stmt.dispatch(stmtAnalyzer); } stmtAnalyzer.emitSizeLabel(); backend.emitJ(stmtAnalyzer.epilogue, "Jump to function epilogue"); - // --- Epilogue --- backend.emitLocalLabel(stmtAnalyzer.epilogue, "Epilogue"); backend.emitLW(RA, FP, -4, "Get return address"); @@ -159,7 +156,8 @@ public class CodeGenImpl extends CodeGenBase } /** An analyzer that encapsulates code generation for statements. */ - private class StmtAnalyzer extends AbstractNodeAnalyzer { + private class StmtAnalyzer extends AbstractNodeAnalyzer + { /* * The symbol table has all the info you need to determine what a given * identifier 'x' in the current scope is. You can use it as follows: SymbolInfo @@ -199,8 +197,7 @@ public class CodeGenImpl extends CodeGenBase /** Label of code that exits from block. */ protected Stack