added static link and accessing local variables capability

master
Sanjar Ahmadov 3 years ago
parent 14a5f54ceb
commit ca92cffcd3

@ -300,25 +300,32 @@ public class CodeGenImpl extends CodeGenBase
// function // function
Identifier functionId = node.function; Identifier functionId = node.function;
FuncInfo calleeFunctionInfo = (FuncInfo) sym.get(node.function.name); FuncInfo calleeFunctionInfo = (FuncInfo) sym.get(node.function.name);
boolean isInMainFunction = funcInfo == null;
List<Expr> args = node.args; if (!isInMainFunction) {
System.out.println("--> caller: " + funcInfo.getFuncName() + " depth: " + funcInfo.getDepth());
int depth = calleeFunctionInfo.getDepth(); System.out.println("--> callee: " + calleeFunctionInfo.getFuncName() + " depth: " + calleeFunctionInfo.getDepth());
int calleeDepth = calleeFunctionInfo.getDepth();
int callerDepth = funcInfo.getDepth();
int jumps = callerDepth - calleeDepth + 1;
int tmpHandle = getRegister(); int tmpHandle = getRegister();
Register tmp = registerPool[tmpHandle]; Register tmp = registerPool[tmpHandle];
FuncInfo currFuncInfo = calleeFunctionInfo;
backend.emitMV(tmp, FP, "Load FP"); backend.emitMV(tmp, FP, "Load FP");
while (depth > 0) {
final FuncInfo parentFuncInfo = currFuncInfo.getParentFuncInfo(); FuncInfo curr = funcInfo;
backend.emitLW(tmp, tmp, parentFuncInfo.getParams().size()*wordSize, "Load static link to " + parentFuncInfo.getFuncName()); for (int i = 0; i < jumps; i++) {
currFuncInfo = parentFuncInfo; backend.emitLW(tmp, tmp, curr.getParams().size()*wordSize, "Load static link to " + curr.getFuncName());
--depth; curr = curr.getParentFuncInfo();
} }
backend.emitSW(tmp, SP, -1 * wordSize, "Push static link onto active frame.");
freeRegister(tmpHandle); freeRegister(tmpHandle);
}
int spaceRequiredForArgs = (args.size() + 1)*4;
backend.emitMV(T0, FP, "Get static link to " + (funcInfo != null ? funcInfo.getFuncName() : "main")); List<Expr> args = node.args;
backend.emitSW(T0, SP, -wordSize, "Push static link onto active frame."); int spaceRequiredForArgs = (args.size() + 1)*4;
for (int i = 0; i < args.size(); i++) { for (int i = 0; i < args.size(); i++) {
int argNum = i + 1; int argNum = i + 1;
int slotNum = argNum + 1; // We have extra slot for static link int slotNum = argNum + 1; // We have extra slot for static link
@ -344,13 +351,14 @@ public class CodeGenImpl extends CodeGenBase
// All expressions should save their end result in A0 // All expressions should save their end result in A0
// So, once expr is evaluated add value inside A0 onto stack as an actual argument // So, once expr is evaluated add value inside A0 onto stack as an actual argument
backend.emitSW(A0, SP, -wordSize*slotNum, "Push actual argument for " + formalParamName + " onto stack"); backend.emitSW(A0, SP, -slotNum * wordSize, "Push actual argument for " + formalParamName + " onto stack");
} }
backend.emitADDI(SP, SP, -spaceRequiredForArgs, "Set SP to last argument."); backend.emitADDI(SP, SP, -spaceRequiredForArgs, "Set SP to last argument.");
backend.emitJAL(calleeFunctionInfo.getCodeLabel(), "Invoke function: " + functionId.name); backend.emitJAL(calleeFunctionInfo.getCodeLabel(), "Invoke function: " + functionId.name);
backend.emitADDI(SP, SP, spaceRequiredForArgs, "Set SP to stack frame top."); backend.emitADDI(SP, SP, spaceRequiredForArgs, "Set SP to stack frame top.");
} }
return A0; return A0;
} }
@ -468,27 +476,23 @@ public class CodeGenImpl extends CodeGenBase
return ((GlobalVarInfo)info).getLabel(); return ((GlobalVarInfo)info).getLabel();
} }
private StackVarRuntimeInfo getStackVar(StackVarInfo info){ private StackVarRuntimeInfo getStackVar(StackVarInfo info){
int curr_depth = funcInfo.getDepth(); FuncInfo varFuncInfo = info.getFuncInfo();
int d_depth = curr_depth - info.getFuncInfo().getDepth(); System.out.println("varfuncInfo name: " + varFuncInfo.getFuncName());
if(d_depth == 0) { System.out.println("var idx: " + varFuncInfo.getVarIndex(info.getVarName()));
int idx = funcInfo.getVarIndex(info.getVarName());
int paramSize = funcInfo.getParams().size(); int tmpHandle = getRegister();
int offset = getLocalVarLocationOffset(idx, paramSize); Register tmp = registerPool[tmpHandle];
return new StackVarRuntimeInfo(FP, offset);
} else if (funcInfo == info.getFuncInfo() || funcInfo == null) {
{ backend.emitMV(tmp, FP, "We are referencing local variable");
FuncInfo curr = funcInfo; } else {
int tmpHandle = getRegister(); backend.emitLW(tmp, FP, funcInfo.getParams().size()*wordSize, "We are referencing non local variable");
Register tmp = registerPool[tmpHandle]; }
backend.emitMV(tmp, FP, "tmp = FP");
while(d_depth > 0){ int idx = varFuncInfo.getVarIndex(info.getVarName());
curr = curr.getParentFuncInfo(); int paramSize = varFuncInfo.getParams().size();
backend.emitLW(tmp, tmp, curr.getParams().size()*wordSize, "Get static link"); int offset = getLocalVarLocationOffset(idx, paramSize);
-- d_depth; return new StackVarRuntimeInfo(tmp, offset, tmpHandle);
}
// UNSAFE!!!!! PLEASE FREE THE TMP_HANDLE MANUALLY
return new StackVarRuntimeInfo(tmp, -wordSize*curr.getVarIndex(info.getVarName()), tmpHandle);
}
} }
@Override @Override

Loading…
Cancel
Save