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,91 +510,80 @@ 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(); {
System.out.println("var name: " + info.getVarName()); FuncInfo varFuncInfo = info.getFuncInfo();
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
} else { {
System.out.println("We are referencing local variable"); backend.emitMV(tmp, FP, "Referencing local variable"+info.getVarName());
backend.emitMV(tmp, FP, "We are referencing local variable"); }
} int idx = varFuncInfo.getVarIndex(info.getVarName());
int paramSize = varFuncInfo.getParams().size();
int idx = varFuncInfo.getVarIndex(info.getVarName()); int offset = getLocalVarLocationOffset(idx, paramSize);
int paramSize = varFuncInfo.getParams().size(); return new StackVarRuntimeInfo(tmp, offset, tmpHandle);
int offset = getLocalVarLocationOffset(idx, paramSize);
return new StackVarRuntimeInfo(tmp, offset, tmpHandle);
} }
@Override @Override
public Register analyze(AssignStmt node) public Register analyze(AssignStmt node)
{ {
System.out.println("Inside AssignStmt: "); Register reg = node.value.dispatch(this);
//backend.emitLW(T6, FP, 0, "Inside AssignStmt: "); if (reg == null)
//Type t = node.value.getInferredType(); reg = A0;
// if(t.isSpecialType() || t.isListType()) for(Expr target: node.targets)
//{ {
Register reg = node.value.dispatch(this); if(target instanceof Identifier)
if (reg == null)
reg = A0;
for(Expr target: node.targets)
{ {
if(target instanceof Identifier) Identifier targetID = (Identifier)target;
Object var = getVar(targetID);
if(var instanceof StackVarRuntimeInfo)
{ {
Identifier targetID = (Identifier)target; StackVarRuntimeInfo rtinfo = (StackVarRuntimeInfo) var;
Object var = getVar(targetID); backend.emitSW(reg, rtinfo.sl, rtinfo.off ,"Store local variable: "+((Identifier)target).name);
if(var instanceof StackVarRuntimeInfo){ rtinfo.free();
StackVarRuntimeInfo rtinfo = (StackVarRuntimeInfo) var;
backend.emitSW(reg, rtinfo.sl, rtinfo.off ,"Store local variable: "+((Identifier)target).name);
rtinfo.free();
}
else
backend.emitSW(reg, (Label)var, T0, "Store Global variable: " + ((Identifier)targetID).name);
} }
else else
backend.emitSW(reg, (Label)var, T0, "Store Global variable: " + ((Identifier)targetID).name);
}
else
{
backend.emitSW(reg, Register.FP, -sp_off*wordSize, "Store value to b stored");
incSp(1);
boolean old_lvalue = l_value;
l_value = true;
Register ret = target.dispatch(this);
l_value = old_lvalue;
sp_off--;
Register tmp = reg;
int tmpHandle = -1;
if(ret.equals(reg))
{ {
backend.emitSW(reg, Register.FP, -sp_off*wordSize, "Store value to b stored"); tmpHandle = getRegister();
incSp(1); tmp = registerPool[tmpHandle];
boolean old_lvalue = l_value;
l_value = true;
Register ret = target.dispatch(this);
l_value = old_lvalue;
sp_off--;
Register tmp = reg;
int tmpHandle = -1;
if(ret.equals(reg))
{
tmpHandle = getRegister();
tmp = registerPool[tmpHandle];
}
backend.emitLW(tmp, Register.FP, -sp_off*wordSize, "Load value to b stored");
backend.emitSW(tmp, ret, 0, "Set list element");
if(tmpHandle >= 0)
freeRegister(tmpHandle);
} }
backend.emitLW(tmp, Register.FP, -sp_off*wordSize, "Load value to b stored");
backend.emitSW(tmp, ret, 0, "Set list element");
if(tmpHandle >= 0)
freeRegister(tmpHandle);
} }
//} }
return null; return null;
} }

Loading…
Cancel
Save