master
Apoorva Ranade 4 years ago
parent 832b4f8d98
commit ee03f7b1c0

@ -114,14 +114,12 @@ public class CodeGenImpl extends CodeGenBase
// space for control link = 1
// space for static link = 1
// space for locals = num of locals
//int requiredStackSpace = (1 + 1 + 1 + funcInfo.getLocals().size())*wordSize;
String sizelabel = "@" + funcInfo.getFuncName()+".size";
backend.emitADDI(SP, SP, "-"+sizelabel, "Reserve space for stack frame.");
backend.emitSW(RA, SP, sizelabel+"-4", "return address");
backend.emitSW(FP, SP, sizelabel + "-8", "control link");
// if we want to add static link
//backend.emitSW(FP, SP, requiredStackSpace-12, "static link");
backend.emitADDI(FP, SP, sizelabel, "New fp is at old SP.");
backend.emitADDI(FP, SP, sizelabel, "New FP is at old SP.");
StmtAnalyzer stmtAnalyzer = new StmtAnalyzer(funcInfo);
int emptySlotNum = 3;
@ -130,7 +128,7 @@ public class CodeGenImpl extends CodeGenBase
Literal varLiteral = var.getInitialValue();
Register reg = varLiteral.dispatch(stmtAnalyzer);
// All Literals should save locations for the values in A0
backend.emitSW(reg, SP, sizelabel+String.format("-%d",emptySlotNum*wordSize), "Push local variable " + var.getVarName() + " onto stack");
backend.emitSW(reg, SP, sizelabel+String.format("-%d",emptySlotNum*wordSize), "Push local variable: " + var.getVarName() + " onto stack");
emptySlotNum++;
}
@ -274,7 +272,6 @@ public class CodeGenImpl extends CodeGenBase
backend.defineSym(size_label, max_sp*wordSize);
}
// *********** functions start ***********
public Register analyze(CallExpr node)
@ -374,9 +371,11 @@ public class CodeGenImpl extends CodeGenBase
}
else if (expr.getInferredType().equals(Type.BOOL_TYPE))
{
if (!formalParamInfo.getVarType().equals(Type.OBJECT_TYPE) && !formalParamInfo.getVarType().equals(Type.BOOL_TYPE)) {
if(!formalParamInfo.getVarType().equals(Type.OBJECT_TYPE) && !formalParamInfo.getVarType().equals(Type.BOOL_TYPE))
{
backend.emitJAL(errorNI, "Passed argument does not match formal parameter");
} else if ((functionId.name.equals("print"))&&(formalParamInfo.getVarType().equals(Type.OBJECT_TYPE) || formalParamInfo.getVarType().equals(Type.BOOL_TYPE)))
}
else if ((functionId.name.equals("print"))&&(formalParamInfo.getVarType().equals(Type.OBJECT_TYPE) || formalParamInfo.getVarType().equals(Type.BOOL_TYPE)))
{
if(!(args.size() == 1 && (args.get(0) instanceof CallExpr) &&
(sym.get(((CallExpr) args.get(0)).function.name) instanceof ClassInfo)))
@ -388,8 +387,6 @@ public class CodeGenImpl extends CodeGenBase
// So, once expr is evaluated add value inside A0 onto stack as an actual argument
backend.emitSW(A0, FP, -(sp_off) * wordSize, "Push actual argument for " + formalParamName + " onto stack");
}
//System.out.printf("2: %d",sp_off - spaceRequiredForArgs/wordSize - origsp);
backend.emitADDI(SP, FP, -(sp_off)*wordSize, "Set SP to last argument.");
backend.emitJAL(calleeFunctionInfo.getCodeLabel(), "Invoke function: " + functionId.name);
backend.emitADDI(SP, FP, "-"+size_label, "Set SP to stack frame top.");
@ -400,7 +397,6 @@ public class CodeGenImpl extends CodeGenBase
public Register analyze(MethodCallExpr node)
{
System.out.println("Inside MethodCallExpr: " + node.method.member.name);
Register obj = node.method.object.dispatch(this);
int n_args = node.args.size();
@ -532,7 +528,7 @@ public class CodeGenImpl extends CodeGenBase
}
else
{
backend.emitMV(tmp, FP, "Referencing local variable"+info.getVarName());
backend.emitMV(tmp, FP, "Referencing local variable: "+info.getVarName());
}
int idx = varFuncInfo.getVarIndex(info.getVarName());
int paramSize = varFuncInfo.getParams().size();
@ -563,7 +559,7 @@ public class CodeGenImpl extends CodeGenBase
}
else
{
backend.emitSW(reg, Register.FP, -sp_off*wordSize, "Store value to b stored");
backend.emitSW(reg, Register.FP, -sp_off*wordSize, "Store value to be stored");
incSp(1);
boolean old_lvalue = l_value;
l_value = true;
@ -577,7 +573,7 @@ public class CodeGenImpl extends CodeGenBase
tmpHandle = getRegister();
tmp = registerPool[tmpHandle];
}
backend.emitLW(tmp, Register.FP, -sp_off*wordSize, "Load value to b stored");
backend.emitLW(tmp, Register.FP, -sp_off*wordSize, "Load value to be stored");
backend.emitSW(tmp, ret, 0, "Set list element");
if(tmpHandle >= 0)
freeRegister(tmpHandle);
@ -589,8 +585,6 @@ public class CodeGenImpl extends CodeGenBase
@Override
public Register analyze(ExprStmt node)
{
System.out.println("Inside ExprStmt: ");
// backend.emitLW(T6, FP, 0, "Inside ExprStmt: ");
node.expr.dispatch(this);
return null;
}
@ -598,7 +592,6 @@ public class CodeGenImpl extends CodeGenBase
@Override
public Register analyze(IfExpr node)
{
System.out.println("Inside IfExpr: ");
Register result = node.condition.dispatch(this);
Label ln = generateLocalLabel();
elseBlock.push(generateLocalLabel());
@ -606,12 +599,12 @@ public class CodeGenImpl extends CodeGenBase
backend.emitBEQZ(result, elseBlock.peek(),"Jump to end of loop");
result = node.thenExpr.dispatch(this);
if(result != A0)
backend.emitMV(A0, result, "move result");
backend.emitMV(A0, result, "Move result");
backend.emitJ(ln, "Jump to end of if expression");
backend.emitLocalLabel(elseBlock.peek(), "Else part of if expression");
result = node.elseExpr.dispatch(this);
if(result != A0)
backend.emitMV(A0, result, "move result");
backend.emitMV(A0, result, "Move result");
elseBlock.pop();
backend.emitLocalLabel(ln, "End of if expression");
return A0;
@ -620,9 +613,7 @@ public class CodeGenImpl extends CodeGenBase
@Override
public Register analyze(IfStmt node)
{
System.out.println("Inside IfStmt: ");
Register result = node.condition.dispatch(this);
Label ln = generateLocalLabel();
elseBlock.push(generateLocalLabel());
backend.emitBEQZ(result, elseBlock.peek(),"Jump to end of loop");
@ -640,9 +631,7 @@ public class CodeGenImpl extends CodeGenBase
@Override
public Register analyze(BinaryExpr node)
{
System.out.println("Inside BinaryExpr: ");
String operator = node.operator;
//backend.emitLW(T6, FP, 0, "Inside BinaryExpr: ");
if(node.left.getInferredType().equals(Type.INT_TYPE) && node.right.getInferredType().equals(Type.INT_TYPE))
{
@ -705,12 +694,6 @@ public class CodeGenImpl extends CodeGenBase
backend.emitADDI(Register.A0,Register.A0, 1, "Increment by 1");
backend.emitSLT(Register.A0, T0, A0, comment);
}
/* Maybe NA
else if(operator.equals("is"))
{
backend.emitXOR(Register.A0, Register.A0, Register.T0, comment);
backend.emitSEQZ(Register.A0, Register.A0, "Result is True if XOR equals 0");
}*/
else
{
backend.emitJAL(errorNI, "Operator not implemented for integer operands");
@ -719,7 +702,6 @@ public class CodeGenImpl extends CodeGenBase
}
else if(node.left.getInferredType().equals(Type.BOOL_TYPE) && node.right.getInferredType().equals(Type.BOOL_TYPE))
{
// Comparison operators
if(operator.equals("=="))
{
@ -796,9 +778,9 @@ public class CodeGenImpl extends CodeGenBase
if(operator.equals("=="))
{
incSp(2);
backend.emitSW(node.left.dispatch(this), FP, (1 - sp_off) *wordSize, "Push argument 0 from last.");
backend.emitSW(node.right.dispatch(this), FP, ( - sp_off) *wordSize, "Push argument 1 from last.");
backend.emitADDI(SP, FP, -sp_off * wordSize, "Set SP to last argument.");
backend.emitSW(node.left.dispatch(this), FP, (1 - sp_off) *wordSize, "Push argument 0 from last");
backend.emitSW(node.right.dispatch(this), FP, ( - sp_off) *wordSize, "Push argument 1 from last");
backend.emitADDI(SP, FP, -sp_off * wordSize, "Set SP to last argument");
backend.emitJAL(streqlLabel, "Invoke method:streql");
sp_off -= 2;
backend.emitADDI(SP, FP, -sp_off*wordSize, "Restore sp");
@ -806,23 +788,23 @@ public class CodeGenImpl extends CodeGenBase
else if(operator.equals("!="))
{
incSp(2);
backend.emitSW(node.left.dispatch(this), FP, (1 - sp_off) *wordSize, "Push argument 0 from last.");
backend.emitSW(node.right.dispatch(this), FP, ( - sp_off) *wordSize, "Push argument 1 from last.");
backend.emitADDI(SP, FP, -sp_off * wordSize, "Set SP to last argument.");
backend.emitSW(node.left.dispatch(this), FP, (1 - sp_off) *wordSize, "Push argument 0 from last");
backend.emitSW(node.right.dispatch(this), FP, ( - sp_off) *wordSize, "Push argument 1 from last");
backend.emitADDI(SP, FP, -sp_off * wordSize, "Set SP to last argument");
backend.emitJAL(strneqlLabel, "Invoke method:strneql");
sp_off -= 2;
backend.emitADDI(SP, FP, -sp_off*wordSize, "restore sp");
backend.emitADDI(SP, FP, -sp_off*wordSize, "Restore SP");
}
else if(operator.equals("+"))
{
incSp(1);
backend.emitSW(node.left.dispatch(this), FP, (- sp_off) *wordSize, "Push argument 0 from last.");
backend.emitSW(node.left.dispatch(this), FP, (- sp_off) *wordSize, "Push argument 0 from last");
incSp(1);
backend.emitSW(node.right.dispatch(this), FP, (- sp_off) *wordSize, "Push argument 1 from last.");
backend.emitADDI(SP, FP, -sp_off * wordSize, "Set SP to last argument.");
backend.emitSW(node.right.dispatch(this), FP, (- sp_off) *wordSize, "Push argument 1 from last");
backend.emitADDI(SP, FP, -sp_off * wordSize, "Set SP to last argument");
backend.emitJAL(strcatLabel, "Invoke method:strcat");
sp_off -= 2;
backend.emitADDI(SP, FP, -sp_off*wordSize, "restore sp");
backend.emitADDI(SP, FP, -sp_off*wordSize, "Restore sp");
}
else
backend.emitJAL(errorNI, "Operator not implemented for String operands");

Loading…
Cancel
Save