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 control link = 1
// space for static link = 1 // space for static link = 1
// space for locals = num of locals // space for locals = num of locals
//int requiredStackSpace = (1 + 1 + 1 + funcInfo.getLocals().size())*wordSize;
String sizelabel = "@" + funcInfo.getFuncName()+".size"; String sizelabel = "@" + funcInfo.getFuncName()+".size";
backend.emitADDI(SP, SP, "-"+sizelabel, "Reserve space for stack frame."); backend.emitADDI(SP, SP, "-"+sizelabel, "Reserve space for stack frame.");
backend.emitSW(RA, SP, sizelabel+"-4", "return address"); backend.emitSW(RA, SP, sizelabel+"-4", "return address");
backend.emitSW(FP, SP, sizelabel + "-8", "control link"); backend.emitSW(FP, SP, sizelabel + "-8", "control link");
// if we want to add static 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); StmtAnalyzer stmtAnalyzer = new StmtAnalyzer(funcInfo);
int emptySlotNum = 3; int emptySlotNum = 3;
@ -130,7 +128,7 @@ public class CodeGenImpl extends CodeGenBase
Literal varLiteral = var.getInitialValue(); Literal varLiteral = var.getInitialValue();
Register reg = varLiteral.dispatch(stmtAnalyzer); Register reg = varLiteral.dispatch(stmtAnalyzer);
// All Literals should save locations for the values in A0 // 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++; emptySlotNum++;
} }
@ -274,7 +272,6 @@ public class CodeGenImpl extends CodeGenBase
backend.defineSym(size_label, max_sp*wordSize); backend.defineSym(size_label, max_sp*wordSize);
} }
// *********** functions start *********** // *********** functions start ***********
public Register analyze(CallExpr node) public Register analyze(CallExpr node)
@ -374,22 +371,22 @@ public class CodeGenImpl extends CodeGenBase
} }
else if (expr.getInferredType().equals(Type.BOOL_TYPE)) 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))) 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)))
{ {
if(!(args.size() == 1 && (args.get(0) instanceof CallExpr) && if(!(args.size() == 1 && (args.get(0) instanceof CallExpr) &&
(sym.get(((CallExpr) args.get(0)).function.name) instanceof ClassInfo))) (sym.get(((CallExpr) args.get(0)).function.name) instanceof ClassInfo)))
backend.emitJAL(makeboolLabel, "Box boolean"); backend.emitJAL(makeboolLabel, "Box boolean");
} }
} }
// All expressions should save their end result in A0 // All expressions should save their end result in A0
// So, once expr is evaluated add value inside A0 onto stack as an actual argument // 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"); 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.emitADDI(SP, FP, -(sp_off)*wordSize, "Set SP to last argument.");
backend.emitJAL(calleeFunctionInfo.getCodeLabel(), "Invoke function: " + functionId.name); backend.emitJAL(calleeFunctionInfo.getCodeLabel(), "Invoke function: " + functionId.name);
backend.emitADDI(SP, FP, "-"+size_label, "Set SP to stack frame top."); 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) public Register analyze(MethodCallExpr node)
{ {
System.out.println("Inside MethodCallExpr: " + node.method.member.name);
Register obj = node.method.object.dispatch(this); Register obj = node.method.object.dispatch(this);
int n_args = node.args.size(); int n_args = node.args.size();
@ -532,7 +528,7 @@ public class CodeGenImpl extends CodeGenBase
} }
else 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 idx = varFuncInfo.getVarIndex(info.getVarName());
int paramSize = varFuncInfo.getParams().size(); int paramSize = varFuncInfo.getParams().size();
@ -563,7 +559,7 @@ public class CodeGenImpl extends CodeGenBase
} }
else 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); incSp(1);
boolean old_lvalue = l_value; boolean old_lvalue = l_value;
l_value = true; l_value = true;
@ -577,7 +573,7 @@ public class CodeGenImpl extends CodeGenBase
tmpHandle = getRegister(); tmpHandle = getRegister();
tmp = registerPool[tmpHandle]; 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"); backend.emitSW(tmp, ret, 0, "Set list element");
if(tmpHandle >= 0) if(tmpHandle >= 0)
freeRegister(tmpHandle); freeRegister(tmpHandle);
@ -589,8 +585,6 @@ public class CodeGenImpl extends CodeGenBase
@Override @Override
public Register analyze(ExprStmt node) public Register analyze(ExprStmt node)
{ {
System.out.println("Inside ExprStmt: ");
// backend.emitLW(T6, FP, 0, "Inside ExprStmt: ");
node.expr.dispatch(this); node.expr.dispatch(this);
return null; return null;
} }
@ -598,20 +592,19 @@ public class CodeGenImpl extends CodeGenBase
@Override @Override
public Register analyze(IfExpr node) public Register analyze(IfExpr node)
{ {
System.out.println("Inside IfExpr: "); Register result = node.condition.dispatch(this);
Register result = node.condition.dispatch(this);
Label ln = generateLocalLabel(); Label ln = generateLocalLabel();
elseBlock.push(generateLocalLabel()); elseBlock.push(generateLocalLabel());
backend.emitBEQZ(result, elseBlock.peek(),"Jump to end of loop"); backend.emitBEQZ(result, elseBlock.peek(),"Jump to end of loop");
result = node.thenExpr.dispatch(this); result = node.thenExpr.dispatch(this);
if(result != A0) 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.emitJ(ln, "Jump to end of if expression");
backend.emitLocalLabel(elseBlock.peek(), "Else part of if expression"); backend.emitLocalLabel(elseBlock.peek(), "Else part of if expression");
result = node.elseExpr.dispatch(this); result = node.elseExpr.dispatch(this);
if(result != A0) if(result != A0)
backend.emitMV(A0, result, "move result"); backend.emitMV(A0, result, "Move result");
elseBlock.pop(); elseBlock.pop();
backend.emitLocalLabel(ln, "End of if expression"); backend.emitLocalLabel(ln, "End of if expression");
return A0; return A0;
@ -620,9 +613,7 @@ public class CodeGenImpl extends CodeGenBase
@Override @Override
public Register analyze(IfStmt node) public Register analyze(IfStmt node)
{ {
System.out.println("Inside IfStmt: "); Register result = node.condition.dispatch(this);
Register result = node.condition.dispatch(this);
Label ln = generateLocalLabel(); Label ln = generateLocalLabel();
elseBlock.push(generateLocalLabel()); elseBlock.push(generateLocalLabel());
backend.emitBEQZ(result, elseBlock.peek(),"Jump to end of loop"); backend.emitBEQZ(result, elseBlock.peek(),"Jump to end of loop");
@ -640,9 +631,7 @@ public class CodeGenImpl extends CodeGenBase
@Override @Override
public Register analyze(BinaryExpr node) public Register analyze(BinaryExpr node)
{ {
System.out.println("Inside BinaryExpr: "); String operator = node.operator;
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)) 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.emitADDI(Register.A0,Register.A0, 1, "Increment by 1");
backend.emitSLT(Register.A0, T0, A0, comment); 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 else
{ {
backend.emitJAL(errorNI, "Operator not implemented for integer operands"); 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)) else if(node.left.getInferredType().equals(Type.BOOL_TYPE) && node.right.getInferredType().equals(Type.BOOL_TYPE))
{ {
// Comparison operators // Comparison operators
if(operator.equals("==")) if(operator.equals("=="))
{ {
@ -796,33 +778,33 @@ public class CodeGenImpl extends CodeGenBase
if(operator.equals("==")) if(operator.equals("=="))
{ {
incSp(2); incSp(2);
backend.emitSW(node.left.dispatch(this), FP, (1 - sp_off) *wordSize, "Push argument 0 from last."); 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.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.emitADDI(SP, FP, -sp_off * wordSize, "Set SP to last argument");
backend.emitJAL(streqlLabel, "Invoke method:streql"); backend.emitJAL(streqlLabel, "Invoke method:streql");
sp_off -= 2; 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("!=")) else if(operator.equals("!="))
{ {
incSp(2); incSp(2);
backend.emitSW(node.left.dispatch(this), FP, (1 - sp_off) *wordSize, "Push argument 0 from last."); 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.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.emitADDI(SP, FP, -sp_off * wordSize, "Set SP to last argument");
backend.emitJAL(strneqlLabel, "Invoke method:strneql"); backend.emitJAL(strneqlLabel, "Invoke method:strneql");
sp_off -= 2; 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("+")) else if(operator.equals("+"))
{ {
incSp(1); 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); incSp(1);
backend.emitSW(node.right.dispatch(this), FP, (- sp_off) *wordSize, "Push argument 1 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.emitADDI(SP, FP, -sp_off * wordSize, "Set SP to last argument");
backend.emitJAL(strcatLabel, "Invoke method:strcat"); backend.emitJAL(strcatLabel, "Invoke method:strcat");
sp_off -= 2; sp_off -= 2;
backend.emitADDI(SP, FP, -sp_off*wordSize, "restore sp"); backend.emitADDI(SP, FP, -sp_off*wordSize, "Restore sp");
} }
else else
backend.emitJAL(errorNI, "Operator not implemented for String operands"); backend.emitJAL(errorNI, "Operator not implemented for String operands");

Loading…
Cancel
Save