bill 4 years ago
commit 4d6e6fdede

@ -300,25 +300,34 @@ 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);
} else {
int spaceRequiredForArgs = (args.size() + 1)*4; backend.emitSW(FP, SP, -1 * wordSize, "Push static link onto active frame.");
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<Expr> args = node.args;
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 +353,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;
} }
@ -421,7 +431,7 @@ public class CodeGenImpl extends CodeGenBase
@Override @Override
public Register analyze(StringLiteral node) public Register analyze(StringLiteral node)
{ {
System.out.println("Inside StringLiteral: "); System.out.println("Inside StringLiteral: ");
backend.emitLW(T6, FP, 0, "Inside StringLiteral: "); backend.emitLW(T6, FP, 0, "Inside StringLiteral: ");
Label l = constants.getStrConstant(node.value); Label l = constants.getStrConstant(node.value);
backend.emitLA(Register.A0, l, "Load string literal"); backend.emitLA(Register.A0, l, "Load string literal");
@ -431,7 +441,7 @@ public class CodeGenImpl extends CodeGenBase
@Override @Override
public Register analyze(BooleanLiteral node) public Register analyze(BooleanLiteral node)
{ {
System.out.println("Inside BooleanLiteral: "); System.out.println("Inside BooleanLiteral: ");
backend.emitLW(T6, FP, 0, "Inside BooleanLiteral: "); backend.emitLW(T6, FP, 0, "Inside BooleanLiteral: ");
if(node.value==true) if(node.value==true)
backend.emitLI(Register.A0, 1, "Load boolean literal: true "); backend.emitLI(Register.A0, 1, "Load boolean literal: true ");
@ -439,6 +449,7 @@ public class CodeGenImpl extends CodeGenBase
backend.emitLI(Register.A0, 0, "Load boolean literal: false "); backend.emitLI(Register.A0, 0, "Load boolean literal: false ");
return Register.A0; return Register.A0;
} }
class StackVarRuntimeInfo { class StackVarRuntimeInfo {
public final int off; public final int off;
public final Register sl; public final Register sl;
@ -460,6 +471,7 @@ public class CodeGenImpl extends CodeGenBase
} }
} }
} }
private Object getVar(Identifier id){ private Object getVar(Identifier id){
SymbolInfo info = sym.get(id.name); SymbolInfo info = sym.get(id.name);
if(info instanceof StackVarInfo) if(info instanceof StackVarInfo)
@ -467,28 +479,42 @@ public class CodeGenImpl extends CodeGenBase
else else
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("var name: " + info.getVarName());
if(d_depth == 0) { System.out.println("varfuncInfo name: " + varFuncInfo.getFuncName());
int idx = funcInfo.getVarIndex(info.getVarName()); System.out.println("var idx: " + varFuncInfo.getVarIndex(info.getVarName()));
int paramSize = funcInfo.getParams().size();
int offset = getLocalVarLocationOffset(idx, paramSize); int tmpHandle = getRegister();
return new StackVarRuntimeInfo(FP, offset); Register tmp = registerPool[tmpHandle];
} else
{ 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; FuncInfo curr = funcInfo;
int tmpHandle = getRegister(); for (int i = 0; i < jumps; i++) {
Register tmp = registerPool[tmpHandle]; backend.emitLW(tmp, tmp, curr.getParams().size()*wordSize, "Load static link from " + curr.getFuncName() + " to " + curr.getParentFuncInfo().getBaseName());
backend.emitMV(tmp, FP, "tmp = FP"); curr = curr.getParentFuncInfo();
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); System.out.println("We are referencing non local variable");
} } else {
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);
return new StackVarRuntimeInfo(tmp, offset, tmpHandle);
} }
@Override @Override

Loading…
Cancel
Save