From ca92cffcd3d43347e244487466a7292ac3a94c62 Mon Sep 17 00:00:00 2001 From: Sanjar Ahmadov Date: Tue, 4 May 2021 19:33:47 -0400 Subject: [PATCH 1/3] added static link and accessing local variables capability --- src/main/java/chocopy/pa3/CodeGenImpl.java | 76 ++++++++++++---------- 1 file changed, 40 insertions(+), 36 deletions(-) diff --git a/src/main/java/chocopy/pa3/CodeGenImpl.java b/src/main/java/chocopy/pa3/CodeGenImpl.java index 6dcfd5f..f0483d8 100644 --- a/src/main/java/chocopy/pa3/CodeGenImpl.java +++ b/src/main/java/chocopy/pa3/CodeGenImpl.java @@ -300,25 +300,32 @@ public class CodeGenImpl extends CodeGenBase // function Identifier functionId = node.function; FuncInfo calleeFunctionInfo = (FuncInfo) sym.get(node.function.name); - - List args = node.args; - - int depth = calleeFunctionInfo.getDepth(); + boolean isInMainFunction = funcInfo == null; + if (!isInMainFunction) { + System.out.println("--> caller: " + funcInfo.getFuncName() + " depth: " + funcInfo.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(); Register tmp = registerPool[tmpHandle]; - FuncInfo currFuncInfo = calleeFunctionInfo; + backend.emitMV(tmp, FP, "Load FP"); - while (depth > 0) { - final FuncInfo parentFuncInfo = currFuncInfo.getParentFuncInfo(); - backend.emitLW(tmp, tmp, parentFuncInfo.getParams().size()*wordSize, "Load static link to " + parentFuncInfo.getFuncName()); - currFuncInfo = parentFuncInfo; - --depth; + + FuncInfo curr = funcInfo; + for (int i = 0; i < jumps; i++) { + backend.emitLW(tmp, tmp, curr.getParams().size()*wordSize, "Load static link to " + curr.getFuncName()); + curr = curr.getParentFuncInfo(); } + + backend.emitSW(tmp, SP, -1 * wordSize, "Push static link onto active frame."); freeRegister(tmpHandle); - - 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."); + } + + List args = node.args; + int spaceRequiredForArgs = (args.size() + 1)*4; for (int i = 0; i < args.size(); i++) { int argNum = i + 1; 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 // 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.emitJAL(calleeFunctionInfo.getCodeLabel(), "Invoke function: " + functionId.name); backend.emitADDI(SP, SP, spaceRequiredForArgs, "Set SP to stack frame top."); } + return A0; } @@ -468,27 +476,23 @@ public class CodeGenImpl extends CodeGenBase return ((GlobalVarInfo)info).getLabel(); } private StackVarRuntimeInfo getStackVar(StackVarInfo info){ - 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); - } + FuncInfo varFuncInfo = info.getFuncInfo(); + 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"); + } else { + backend.emitLW(tmp, FP, funcInfo.getParams().size()*wordSize, "We are referencing non local variable"); + } + + int idx = varFuncInfo.getVarIndex(info.getVarName()); + int paramSize = varFuncInfo.getParams().size(); + int offset = getLocalVarLocationOffset(idx, paramSize); + return new StackVarRuntimeInfo(tmp, offset, tmpHandle); } @Override From f2485b1e975b94a421cfb9d6a0edb50867fe6d92 Mon Sep 17 00:00:00 2001 From: Sanjar Ahmadov Date: Tue, 4 May 2021 21:17:48 -0400 Subject: [PATCH 2/3] Nested functions work --- src/main/java/chocopy/pa3/CodeGenImpl.java | 58 ++++++++++++++++++++-- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/src/main/java/chocopy/pa3/CodeGenImpl.java b/src/main/java/chocopy/pa3/CodeGenImpl.java index f0483d8..0d474b7 100644 --- a/src/main/java/chocopy/pa3/CodeGenImpl.java +++ b/src/main/java/chocopy/pa3/CodeGenImpl.java @@ -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 args = node.args; @@ -429,7 +431,7 @@ public class CodeGenImpl extends CodeGenBase @Override public Register analyze(StringLiteral node) { - System.out.println("Inside StringLiteral: "); + System.out.println("Inside StringLiteral: "); backend.emitLW(T6, FP, 0, "Inside StringLiteral: "); Label l = constants.getStrConstant(node.value); backend.emitLA(Register.A0, l, "Load string literal"); @@ -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(); + + 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); } From ef4ac31e0446f8af449807bc793af9d3fceef686 Mon Sep 17 00:00:00 2001 From: Sanjar Ahmadov Date: Tue, 4 May 2021 21:21:54 -0400 Subject: [PATCH 3/3] cleanup --- src/main/java/chocopy/pa3/CodeGenImpl.java | 28 +--------------------- 1 file changed, 1 insertion(+), 27 deletions(-) diff --git a/src/main/java/chocopy/pa3/CodeGenImpl.java b/src/main/java/chocopy/pa3/CodeGenImpl.java index 0d474b7..cda13b1 100644 --- a/src/main/java/chocopy/pa3/CodeGenImpl.java +++ b/src/main/java/chocopy/pa3/CodeGenImpl.java @@ -441,7 +441,7 @@ public class CodeGenImpl extends CodeGenBase @Override public Register analyze(BooleanLiteral node) { - System.out.println("Inside BooleanLiteral: "); + System.out.println("Inside BooleanLiteral: "); backend.emitLW(T6, FP, 0, "Inside BooleanLiteral: "); if(node.value==true) backend.emitLI(Register.A0, 1, "Load boolean literal: true "); @@ -486,7 +486,6 @@ public class CodeGenImpl extends CodeGenBase System.out.println("varfuncInfo name: " + varFuncInfo.getFuncName()); System.out.println("var idx: " + varFuncInfo.getVarIndex(info.getVarName())); - int tmpHandle = getRegister(); Register tmp = registerPool[tmpHandle]; @@ -515,31 +514,6 @@ public class CodeGenImpl extends CodeGenBase 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); }