diff --git a/src/main/java/chocopy/pa3/CodeGenImpl.java b/src/main/java/chocopy/pa3/CodeGenImpl.java index 340a1f8..c1f3651 100644 --- a/src/main/java/chocopy/pa3/CodeGenImpl.java +++ b/src/main/java/chocopy/pa3/CodeGenImpl.java @@ -130,8 +130,8 @@ public class CodeGenImpl extends CodeGenBase StmtAnalyzer stmtAnalyzer = new StmtAnalyzer(funcInfo); int emptySlotNum = 3; for (StackVarInfo var : funcInfo.getLocals()) { - Literal varLitral = var.getInitialValue(); - varLitral.dispatch(stmtAnalyzer); + Literal varLiteral = var.getInitialValue(); + varLiteral.dispatch(stmtAnalyzer); // All Literals should save locations for the values in A0 backend.emitSW(A0, SP, requiredStackSpace-emptySlotNum*wordSize, "Push local variable " + var.getVarName() + " onto stack"); emptySlotNum++; @@ -292,7 +292,7 @@ public class CodeGenImpl extends CodeGenBase incSp(1); backend.emitSW(A0, FP, -sp_off*wordSize, "Push argument 0 from last."); - backend.emitADDI(SP, FP, sp_off, "Set SP to last argument."); + backend.emitADDI(SP, FP, sp_off*wordSize, "Set SP to last argument."); backend.emitLW(A1, A0, getDispatchTableOffset(), "Load address of object's dispatch table"); backend.emitLW(A1, A1, getMethodOffset(cls, "__init__"), String.format("Load address of method: %s.__init__", cls.getClassName())); backend.emitJALR(A1, String.format("Invoke method: %s.__init", cls.getClassName())); @@ -305,10 +305,10 @@ public class CodeGenImpl extends CodeGenBase FuncInfo calleeFunctionInfo = (FuncInfo) sym.get(node.function.name); boolean isInMainFunction = funcInfo == null; if (!isInMainFunction) { - System.out.println("--> caller: " + funcInfo.getFuncName() + " depth: " + funcInfo.getDepth()); - System.out.println("--> callee: " + calleeFunctionInfo.getFuncName() + " depth: " + calleeFunctionInfo.getDepth()); + System.out.println("--> caller: " + funcInfo.getFuncName() + " depth: " + funcInfo.getDepth()); + System.out.println("--> callee: " + calleeFunctionInfo.getFuncName() + " depth: " + calleeFunctionInfo.getDepth()); - int calleeDepth = calleeFunctionInfo.getDepth(); + int calleeDepth = calleeFunctionInfo.getDepth(); int callerDepth = funcInfo.getDepth(); int jumps = callerDepth - calleeDepth + 1; @@ -325,7 +325,8 @@ public class CodeGenImpl extends CodeGenBase backend.emitSW(tmp, SP, -1 * wordSize, "Push static link onto active frame."); freeRegister(tmpHandle); - } else { + } + else { backend.emitSW(FP, SP, -1 * wordSize, "Push static link onto active frame."); } @@ -1000,9 +1001,6 @@ public class CodeGenImpl extends CodeGenBase r=getRegister(); regs.add(r); Register ln = registerPool[r]; - //backend.emitBNEZ(Register.A0, temp, "Ensure not none"); - //backend.emitJ(errorNone, "Empty String"); - //backend.emitLocalLabel(temp, "Continue execution for for-loop"); backend.emitMV(l,Register.A0,"Location of String"); r=getRegister(); regs.add(r); @@ -1012,15 +1010,14 @@ public class CodeGenImpl extends CodeGenBase backend.emitMV(iter, Register.ZERO, "Initialize index variable"); backend.emitSW(iter, Register.FP, -sp_off*wordSize, "Store index on stack"); incSp(1); + backend.emitADDI(Register.SP, Register.SP, -2*wordSize, "Set SP to last argument"); backend.emitLocalLabel(startLoop, "Start for loop"); - sp_off--; - backend.emitLW(iter, Register.FP, -(sp_off)*wordSize, "Load index from stack"); - backend.emitLW(l, Register.FP, -(sp_off-1)*wordSize, "Store string location from stack"); + backend.emitLW(iter, Register.FP, -(sp_off-1)*wordSize, "Load index from stack"); + backend.emitLW(l, Register.FP, -(sp_off-2)*wordSize, "Load string location from stack"); backend.emitLW(ln, l, getAttrOffset(strClass, "__len__"), "Get attribute __len__"); backend.emitBGEU(iter, ln, endLoop, "Jump to end loop if counter exceeds length"); backend.emitADDI(iden, iter, 1, "Increment counter"); - backend.emitSW(iden, Register.FP, -sp_off*wordSize, "Store index on stack"); - incSp(1); + backend.emitSW(iden, Register.FP, -(sp_off-1)*wordSize, "Store index on stack"); backend.emitADDI(iden, iter, 4 * wordSize, "Convert index to offset to char in bytes"); backend.emitADD(iden, l, iden, "Get pointer to char"); backend.emitLBU(iden, iden, 0, "Load character"); @@ -1032,12 +1029,13 @@ public class CodeGenImpl extends CodeGenBase if(info instanceof GlobalVarInfo) { GlobalVarInfo gvi=(GlobalVarInfo) info; - backend.emitSW(iden, gvi.getLabel(), Register.T0, "Assign global: "+node.identifier.name+"(using tmp register)"); + backend.emitMV(Register.A0,l,"Copy value"); + backend.emitSW(Register.A0, gvi.getLabel(), Register.T0, "Assign global: "+node.identifier.name+"(using tmp register)"); } else { StackVarRuntimeInfo rtinfo = (StackVarRuntimeInfo) getVar(node.identifier); - backend.emitSW(iden, rtinfo.sl, rtinfo.off,"Store local variable: "+node.identifier.name); + backend.emitSW(l, rtinfo.sl, rtinfo.off,"Store local variable: "+node.identifier.name); rtinfo.free(); } for(Stmt stmt:node.body)