master
Apoorva Ranade 4 years ago
parent d529a8f17b
commit 5f54c2df4b

@ -479,25 +479,30 @@ public class CodeGenImpl extends CodeGenBase
public final int off; public final int off;
public final Register sl; public final Register sl;
public int regHandle; public int regHandle;
StackVarRuntimeInfo(Register sl, int off){ StackVarRuntimeInfo(Register sl, int off)
{
this.off = off; this.off = off;
this.sl = sl; this.sl = sl;
regHandle = -1; regHandle = -1;
} }
StackVarRuntimeInfo(Register sl, int off, int regHandle){ StackVarRuntimeInfo(Register sl, int off, int regHandle)
{
this.off = off; this.off = off;
this.sl = sl; this.sl = sl;
this.regHandle = regHandle; this.regHandle = regHandle;
} }
public void free(){ public void free()
if(regHandle >= 0){ {
if(regHandle >= 0)
{
freeRegister(regHandle); freeRegister(regHandle);
regHandle = -1; regHandle = -1;
} }
} }
} }
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)
return getStackVar((StackVarInfo)info); return getStackVar((StackVarInfo)info);
@ -505,37 +510,31 @@ public class CodeGenImpl extends CodeGenBase
return ((GlobalVarInfo)info).getLabel(); return ((GlobalVarInfo)info).getLabel();
} }
private StackVarRuntimeInfo getStackVar(StackVarInfo info){ private StackVarRuntimeInfo getStackVar(StackVarInfo info)
{
FuncInfo varFuncInfo = info.getFuncInfo(); 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]; Register tmp = registerPool[tmpHandle];
if (funcInfo != null && funcInfo != info.getFuncInfo()) { if (funcInfo != null && funcInfo != info.getFuncInfo())
{
int varFunDepth = varFuncInfo.getDepth(); int varFunDepth = varFuncInfo.getDepth();
int callerDepth = funcInfo.getDepth(); int callerDepth = funcInfo.getDepth();
int jumps = callerDepth - varFunDepth; int jumps = callerDepth - varFunDepth;
System.out.println("varFunDepth: " + varFunDepth);
System.out.println("callerDepth: " + callerDepth);
backend.emitMV(tmp, FP, "Load FP"); backend.emitMV(tmp, FP, "Load FP");
FuncInfo curr = funcInfo; FuncInfo curr = funcInfo;
for (int i = 0; i < jumps; i++) { 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()); backend.emitLW(tmp, tmp, curr.getParams().size()*wordSize, "Load static link from " + curr.getFuncName() + " to " + curr.getParentFuncInfo().getBaseName());
curr = curr.getParentFuncInfo(); curr = curr.getParentFuncInfo();
} }
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");
} }
else
{
backend.emitMV(tmp, FP, "Referencing local variable"+info.getVarName());
}
int idx = varFuncInfo.getVarIndex(info.getVarName()); int idx = varFuncInfo.getVarIndex(info.getVarName());
int paramSize = varFuncInfo.getParams().size(); int paramSize = varFuncInfo.getParams().size();
int offset = getLocalVarLocationOffset(idx, paramSize); int offset = getLocalVarLocationOffset(idx, paramSize);
@ -545,11 +544,6 @@ public class CodeGenImpl extends CodeGenBase
@Override @Override
public Register analyze(AssignStmt node) public Register analyze(AssignStmt node)
{ {
System.out.println("Inside AssignStmt: ");
//backend.emitLW(T6, FP, 0, "Inside AssignStmt: ");
//Type t = node.value.getInferredType();
// if(t.isSpecialType() || t.isListType())
//{
Register reg = node.value.dispatch(this); Register reg = node.value.dispatch(this);
if (reg == null) if (reg == null)
reg = A0; reg = A0;
@ -559,7 +553,8 @@ public class CodeGenImpl extends CodeGenBase
{ {
Identifier targetID = (Identifier)target; Identifier targetID = (Identifier)target;
Object var = getVar(targetID); Object var = getVar(targetID);
if(var instanceof StackVarRuntimeInfo){ if(var instanceof StackVarRuntimeInfo)
{
StackVarRuntimeInfo rtinfo = (StackVarRuntimeInfo) var; StackVarRuntimeInfo rtinfo = (StackVarRuntimeInfo) var;
backend.emitSW(reg, rtinfo.sl, rtinfo.off ,"Store local variable: "+((Identifier)target).name); backend.emitSW(reg, rtinfo.sl, rtinfo.off ,"Store local variable: "+((Identifier)target).name);
rtinfo.free(); rtinfo.free();
@ -589,7 +584,6 @@ public class CodeGenImpl extends CodeGenBase
freeRegister(tmpHandle); freeRegister(tmpHandle);
} }
} }
//}
return null; return null;
} }

Loading…
Cancel
Save