|
|
|
@ -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,37 +510,31 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
|
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());
|
|
|
|
|
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 != 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");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
@ -545,11 +544,6 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
|
@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;
|
|
|
|
@ -559,7 +553,8 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
|
{
|
|
|
|
|
Identifier targetID = (Identifier)target;
|
|
|
|
|
Object var = getVar(targetID);
|
|
|
|
|
if(var instanceof StackVarRuntimeInfo){
|
|
|
|
|
if(var instanceof StackVarRuntimeInfo)
|
|
|
|
|
{
|
|
|
|
|
StackVarRuntimeInfo rtinfo = (StackVarRuntimeInfo) var;
|
|
|
|
|
backend.emitSW(reg, rtinfo.sl, rtinfo.off ,"Store local variable: "+((Identifier)target).name);
|
|
|
|
|
rtinfo.free();
|
|
|
|
@ -589,7 +584,6 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
|
freeRegister(tmpHandle);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|