Nested functions work

master
Sanjar Ahmadov 3 years ago
parent ca92cffcd3
commit f2485b1e97

@ -322,6 +322,8 @@ public class CodeGenImpl extends CodeGenBase
backend.emitSW(tmp, SP, -1 * wordSize, "Push static link onto active frame.");
freeRegister(tmpHandle);
} else {
backend.emitSW(FP, SP, -1 * wordSize, "Push static link onto active frame.");
}
List<Expr> args = node.args;
@ -447,6 +449,7 @@ public class CodeGenImpl extends CodeGenBase
backend.emitLI(Register.A0, 0, "Load boolean literal: false ");
return Register.A0;
}
class StackVarRuntimeInfo {
public final int off;
public final Register sl;
@ -468,6 +471,7 @@ public class CodeGenImpl extends CodeGenBase
}
}
}
private Object getVar(Identifier id){
SymbolInfo info = sym.get(id.name);
if(info instanceof StackVarInfo)
@ -475,23 +479,67 @@ public class CodeGenImpl extends CodeGenBase
else
return ((GlobalVarInfo)info).getLabel();
}
private StackVarRuntimeInfo getStackVar(StackVarInfo info){
FuncInfo varFuncInfo = info.getFuncInfo();
System.out.println("var name: " + info.getVarName());
System.out.println("varfuncInfo name: " + varFuncInfo.getFuncName());
System.out.println("var idx: " + varFuncInfo.getVarIndex(info.getVarName()));
int tmpHandle = getRegister();
Register tmp = registerPool[tmpHandle];
if (funcInfo == info.getFuncInfo() || funcInfo == null) {
backend.emitMV(tmp, FP, "We are referencing local variable");
if (funcInfo != null && funcInfo != info.getFuncInfo()) {
int varFunDepth = varFuncInfo.getDepth();
int callerDepth = funcInfo.getDepth();
int jumps = callerDepth - varFunDepth;
System.out.println("varFunDepth: " + varFunDepth);
System.out.println("callerDepth: " + callerDepth);
backend.emitMV(tmp, FP, "Load FP");
FuncInfo curr = funcInfo;
for (int i = 0; i < jumps; i++) {
backend.emitLW(tmp, tmp, curr.getParams().size()*wordSize, "Load static link from " + curr.getFuncName() + " to " + curr.getParentFuncInfo().getBaseName());
curr = curr.getParentFuncInfo();
}
System.out.println("We are referencing non local variable");
} else {
backend.emitLW(tmp, FP, funcInfo.getParams().size()*wordSize, "We are referencing non local variable");
System.out.println("We are referencing local variable");
backend.emitMV(tmp, FP, "We are referencing local variable");
}
int idx = varFuncInfo.getVarIndex(info.getVarName());
int paramSize = varFuncInfo.getParams().size();
int offset = getLocalVarLocationOffset(idx, paramSize);
System.out.println("idx " +idx + " paramSize " + paramSize + " offset " + offset);
// - int curr_depth = funcInfo.getDepth();
// - int d_depth = curr_depth - info.getFuncInfo().getDepth();
// - 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();
// - Register tmp = registerPool[tmpHandle];
// - backend.emitMV(tmp, FP, "tmp = FP");
// - while(d_depth > 0){
// - curr = curr.getParentFuncInfo();
// - backend.emitLW(tmp, tmp, curr.getParams().size()*wordSize, "Get static link");
// - -- d_depth;
// - }
// - // UNSAFE!!!!! PLEASE FREE THE TMP_HANDLE MANUALLY
// - return new StackVarRuntimeInfo(tmp, -wordSize*curr.getVarIndex(info.getVarName()), tmpHandle);
// - }
return new StackVarRuntimeInfo(tmp, offset, tmpHandle);
}

Loading…
Cancel
Save