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 Register sl;
public int regHandle;
StackVarRuntimeInfo(Register sl, int off){
StackVarRuntimeInfo(Register sl, int off)
{
this.off = off;
this.sl = sl;
regHandle = -1;
}
StackVarRuntimeInfo(Register sl, int off, int regHandle){
StackVarRuntimeInfo(Register sl, int off, int regHandle)
{
this.off = off;
this.sl = sl;
this.regHandle = regHandle;
}
public void free(){
if(regHandle >= 0){
public void free()
{
if(regHandle >= 0)
{
freeRegister(regHandle);
regHandle = -1;
}
}
}
private Object getVar(Identifier id){
private Object getVar(Identifier id)
{
SymbolInfo info = sym.get(id.name);
if(info instanceof StackVarInfo)
return getStackVar((StackVarInfo)info);
@ -505,91 +510,80 @@ public class CodeGenImpl extends CodeGenBase
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()));
private StackVarRuntimeInfo getStackVar(StackVarInfo info)
{
FuncInfo varFuncInfo = info.getFuncInfo();
int tmpHandle = getRegister();
Register tmp = registerPool[tmpHandle];
if (funcInfo != null && funcInfo != info.getFuncInfo()) {
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++) {
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 {
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);
}
else
{
backend.emitMV(tmp, FP, "Referencing local variable"+info.getVarName());
}
int idx = varFuncInfo.getVarIndex(info.getVarName());
int paramSize = varFuncInfo.getParams().size();
int offset = getLocalVarLocationOffset(idx, paramSize);
return new StackVarRuntimeInfo(tmp, offset, tmpHandle);
}
@Override
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);
if (reg == null)
reg = A0;
for(Expr target: node.targets)
Register reg = node.value.dispatch(this);
if (reg == null)
reg = A0;
for(Expr target: node.targets)
{
if(target instanceof Identifier)
{
if(target instanceof Identifier)
Identifier targetID = (Identifier)target;
Object var = getVar(targetID);
if(var instanceof StackVarRuntimeInfo)
{
Identifier targetID = (Identifier)target;
Object var = getVar(targetID);
if(var instanceof StackVarRuntimeInfo){
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);
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
{
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");
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))
{
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);
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);
}
//}
}
return null;
}

Loading…
Cancel
Save