Changed active frame logic - matched it with the implementation guide

master
Sanjar Ahmadov 4 years ago
parent 0a5c6f1aa6
commit 14a5f54ceb

@ -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<String, Integer> 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<Label> elseBlock = new Stack<Label>();
/** Variable to keep track of slots of stored variables on stack */
// example usage: backend.emitLW(A0, SP, varSlotMap.get(varName)) to load the variable using its name
private Map<String, Integer> varSlotMap = new HashMap<>();
private final String size_label;
/** Variable to store offset from frame pointer to identify next
@ -233,6 +221,18 @@ public class CodeGenImpl extends CodeGenBase
registerAvilMap[handle] = false;
}
private int getParamLocationOffset(int index, int paramSize) {
return (paramSize - index - 1) * wordSize;
}
private int getLocalVarLocationOffset(int index, int paramSize) {
return - (index - paramSize + 1) * wordSize;
}
private int getStaticLinkOffset(int paramSize) {
return paramSize * wordSize;
}
/**
* An analyzer for the function described by FUNCINFO0, which is null for the
* top level.
@ -302,7 +302,7 @@ public class CodeGenImpl extends CodeGenBase
FuncInfo calleeFunctionInfo = (FuncInfo) sym.get(node.function.name);
List<Expr> args = node.args;
int spaceRequiredForArgs = (args.size() + 1)*4;
int depth = calleeFunctionInfo.getDepth();
int tmpHandle = getRegister();
Register tmp = registerPool[tmpHandle];
@ -315,7 +315,10 @@ public class CodeGenImpl extends CodeGenBase
--depth;
}
freeRegister(tmpHandle);
//backend.emitSW(A0, SP, -4, "Put static link");
int spaceRequiredForArgs = (args.size() + 1)*4;
backend.emitMV(T0, FP, "Get static link to " + (funcInfo != null ? funcInfo.getFuncName() : "main"));
backend.emitSW(T0, SP, -wordSize, "Push static link onto active frame.");
for (int i = 0; i < args.size(); i++) {
int argNum = i + 1;
int slotNum = argNum + 1; // We have extra slot for static link
@ -467,9 +470,12 @@ public class CodeGenImpl extends CodeGenBase
private StackVarRuntimeInfo getStackVar(StackVarInfo info){
int curr_depth = funcInfo.getDepth();
int d_depth = curr_depth - info.getFuncInfo().getDepth();
if(d_depth == 0)
return new StackVarRuntimeInfo(SP, varSlotMap.get(info.getVarName()));
else
if(d_depth == 0) {
int idx = funcInfo.getVarIndex(info.getVarName());
int paramSize = funcInfo.getParams().size();
int offset = getLocalVarLocationOffset(idx, paramSize);
return new StackVarRuntimeInfo(FP, offset);
} else
{
FuncInfo curr = funcInfo;
int tmpHandle = getRegister();

Loading…
Cancel
Save