From 14a5f54cebea29995a3d90eb72c35aeae78f56a8 Mon Sep 17 00:00:00 2001 From: Sanjar Ahmadov Date: Tue, 4 May 2021 13:20:13 -0400 Subject: [PATCH] Changed active frame logic - matched it with the implementation guide --- src/main/java/chocopy/pa3/CodeGenImpl.java | 54 ++++++++++++---------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/src/main/java/chocopy/pa3/CodeGenImpl.java b/src/main/java/chocopy/pa3/CodeGenImpl.java index 5844f46..6dcfd5f 100644 --- a/src/main/java/chocopy/pa3/CodeGenImpl.java +++ b/src/main/java/chocopy/pa3/CodeGenImpl.java @@ -116,9 +116,8 @@ public class CodeGenImpl extends CodeGenBase // space for return address = 1 // space for control link = 1 // space for static link = 1 - // space for params = num of params // space for locals = num of locals - int requiredStackSpace = (1 + 1 + 1 + funcInfo.getParams().size() + funcInfo.getLocals().size())*wordSize; + int requiredStackSpace = (1 + 1 + 1 + funcInfo.getLocals().size())*wordSize; backend.emitADDI(SP, SP, -requiredStackSpace, "Reserve space for stack frame."); backend.emitSW(RA, SP, requiredStackSpace-4, "return address"); @@ -129,27 +128,16 @@ public class CodeGenImpl extends CodeGenBase StmtAnalyzer stmtAnalyzer = new StmtAnalyzer(funcInfo); - int emptySlotNum = 4; - - // example usage: backend.emitLW(A0, SP, varSlotMap.get(varName)) to load the variable using its name - Map varSlotMap = stmtAnalyzer.varSlotMap; - - for (int i = 0; i < funcInfo.getParams().size(); i++) { - String param = funcInfo.getParams().get(i); - backend.emitLW(A0, FP, -wordSize*i, "Load argument " + param + " from callers active frame"); - backend.emitSW(A0, SP, requiredStackSpace-emptySlotNum*wordSize, "Push argument " + param + " onto stack"); - varSlotMap.put(param, requiredStackSpace-emptySlotNum*wordSize); - emptySlotNum++; - } - + int emptySlotNum = 3; for (StackVarInfo var : funcInfo.getLocals()) { Literal varLitral = var.getInitialValue(); varLitral.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"); - varSlotMap.put(var.getVarName(), requiredStackSpace-emptySlotNum*wordSize); emptySlotNum++; } + + // --- Function Body --- // statements load all the variables that caller put on stack // statements use fp to load the variables @@ -160,6 +148,8 @@ public class CodeGenImpl extends CodeGenBase } stmtAnalyzer.emitSizeLabel(); backend.emitJ(stmtAnalyzer.epilogue, "Jump to function epilogue"); + + // --- Epilogue --- backend.emitLocalLabel(stmtAnalyzer.epilogue, "Epilogue"); backend.emitLW(RA, FP, -4, "Get return address"); @@ -209,9 +199,7 @@ public class CodeGenImpl extends CodeGenBase /** Label of code that exits from block. */ protected Stack