|
|
@ -53,6 +53,9 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
/** Index out of bounds. */
|
|
|
|
/** Index out of bounds. */
|
|
|
|
private final Label errorOob = new Label("error.OOB");
|
|
|
|
private final Label errorOob = new Label("error.OOB");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Not implemented. */
|
|
|
|
|
|
|
|
private final Label errorNI = new Label("error.NI");
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Emits the top level of the program.
|
|
|
|
* Emits the top level of the program.
|
|
|
|
*
|
|
|
|
*
|
|
|
@ -312,6 +315,9 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Register analyze(BinaryExpr node)
|
|
|
|
public Register analyze(BinaryExpr node)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
String operator = node.operator;
|
|
|
|
|
|
|
|
if(node.left.getInferredType().equals(Type.INT_TYPE) && node.right.getInferredType().equals(Type.INT_TYPE))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
node.left.dispatch(this);
|
|
|
|
node.left.dispatch(this);
|
|
|
|
backend.emitSW(Register.A0, Register.FP, -sp_off*wordSize, "Push on stack slot "+sp_off);
|
|
|
|
backend.emitSW(Register.A0, Register.FP, -sp_off*wordSize, "Push on stack slot "+sp_off);
|
|
|
@ -319,39 +325,70 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
node.right.dispatch(this);
|
|
|
|
node.right.dispatch(this);
|
|
|
|
sp_off--;
|
|
|
|
sp_off--;
|
|
|
|
backend.emitLW(Register.T0, Register.FP, -sp_off*wordSize, "Pop stack slot "+sp_off);
|
|
|
|
backend.emitLW(Register.T0, Register.FP, -sp_off*wordSize, "Pop stack slot "+sp_off);
|
|
|
|
|
|
|
|
|
|
|
|
// Arithmetic Operators
|
|
|
|
// Arithmetic Operators
|
|
|
|
if(node.operator.equals("+"))
|
|
|
|
if(operator.equals("+"))
|
|
|
|
backend.emitADD(Register.A0, Register.A0, Register.T0, "Add operation");
|
|
|
|
backend.emitADD(Register.A0, Register.A0, Register.T0, "Add operation");
|
|
|
|
else if(node.operator.equals("-"))
|
|
|
|
else if(operator.equals("-"))
|
|
|
|
backend.emitSUB(Register.A0, Register.A0, Register.T0, "Sub operation");
|
|
|
|
backend.emitSUB(Register.A0, Register.A0, Register.T0, "Sub operation");
|
|
|
|
else if(node.operator.equals("*"))
|
|
|
|
else if(operator.equals("*"))
|
|
|
|
backend.emitMUL(Register.A0, Register.A0, Register.T0, "Mul operation");
|
|
|
|
backend.emitMUL(Register.A0, Register.A0, Register.T0, "Mul operation");
|
|
|
|
else if(node.operator.equals("/"))
|
|
|
|
else if(operator.equals("//"))
|
|
|
|
backend.emitDIV(Register.A0, Register.A0, Register.T0, "Div operation");
|
|
|
|
{
|
|
|
|
|
|
|
|
Label label = generateLocalLabel();
|
|
|
|
|
|
|
|
backend.emitBNEZ(Register.T0, label, "Check for Divide-by-zero");
|
|
|
|
|
|
|
|
backend.emitJAL(errorDiv, "Divide-by-zero error");
|
|
|
|
|
|
|
|
backend.emitLocalLabel(label, "Divide since divisor not zero");
|
|
|
|
|
|
|
|
backend.emitDIV(Register.A0, Register.A0, Register.T0, "Divide operation");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if(operator.equals("%"))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Label label = generateLocalLabel();
|
|
|
|
|
|
|
|
backend.emitBNEZ(Register.T0, label, "Check for Divide-by-zero");
|
|
|
|
|
|
|
|
backend.emitJAL(errorDiv, "Divide-by-zero error");
|
|
|
|
|
|
|
|
backend.emitLocalLabel(label, "Divide since divisor not zero");
|
|
|
|
|
|
|
|
backend.emitREM(Register.A0, Register.A0, Register.T0, "Modulus operation");
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{ // Comparison operators
|
|
|
|
{ // Comparison operators
|
|
|
|
elseBlock = generateLocalLabel();
|
|
|
|
//elseBlock = generateLocalLabel();
|
|
|
|
String comment="Branch on not "+node.operator;
|
|
|
|
String comment="Operator: "+operator;
|
|
|
|
if(node.operator.equals("=="))
|
|
|
|
if(operator.equals("=="))
|
|
|
|
backend.emitBNE(Register.A0, Register.T0,elseBlock, comment);
|
|
|
|
{
|
|
|
|
else if(node.operator.equals("!="))
|
|
|
|
backend.emitXOR(Register.A0, Register.A0, Register.T0, "Check for equality");
|
|
|
|
backend.emitBEQ(Register.A0, Register.T0,elseBlock, comment);
|
|
|
|
backend.emitSEQZ(Register.A0, Register.A0, "Result is True if XOR equals 0");
|
|
|
|
else if(node.operator.equals("<"))
|
|
|
|
}
|
|
|
|
backend.emitBGE(Register.A0, Register.T0,elseBlock, comment);
|
|
|
|
else if(operator.equals("!="))
|
|
|
|
else if(node.operator.equals(">"))
|
|
|
|
{
|
|
|
|
|
|
|
|
backend.emitXOR(Register.A0, Register.A0, Register.T0, "Check for inequality");
|
|
|
|
|
|
|
|
backend.emitSNEZ(Register.A0, Register.A0, "Result is True if XOR does not equal 0");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if(operator.equals("<"))
|
|
|
|
|
|
|
|
backend.emitSLT(Register.A0,Register.A0, Register.T0, comment);
|
|
|
|
|
|
|
|
else if(operator.equals(">"))
|
|
|
|
|
|
|
|
backend.emitSLT(Register.A0, Register.T0, Register.A0, comment);
|
|
|
|
|
|
|
|
else if(operator.equals("<="))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
backend.emitADDI(Register.T0,Register.T0, 1, "Increment by 1");
|
|
|
|
|
|
|
|
backend.emitSLT(Register.A0, Register.A0, Register.T0, comment);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if(operator.equals(">="))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
backend.emitBLT(Register.A0, Register.T0,elseBlock, comment);
|
|
|
|
backend.emitADDI(Register.A0,Register.A0, 1, "Increment by 1");
|
|
|
|
backend.emitBEQ(Register.A0, Register.T0,elseBlock, comment);
|
|
|
|
backend.emitSLT(Register.A0, Register.A0, Register.T0, comment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(node.operator.equals(">="))
|
|
|
|
else if(operator.equals("is"))
|
|
|
|
backend.emitBLT(Register.A0, Register.T0,elseBlock, comment);
|
|
|
|
|
|
|
|
else if(node.operator.equals("<="))
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Label temp = generateLocalLabel();
|
|
|
|
backend.emitXOR(Register.A0, Register.A0, Register.T0, "is operation");
|
|
|
|
backend.emitBEQ(Register.A0, Register.T0,temp, "Branch on "+node.operator);
|
|
|
|
backend.emitSEQZ(Register.A0, Register.A0, "Result is True if XOR equals 0");
|
|
|
|
backend.emitBGE(Register.A0, Register.T0,elseBlock, comment);
|
|
|
|
|
|
|
|
backend.emitLocalLabel(temp, "True part of if check");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if(node.left.getInferredType().equals(Type.STR_TYPE) && node.right.getInferredType().equals(Type.STR_TYPE))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -534,6 +571,7 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
emitErrorFunc(errorNone, "Operation on None");
|
|
|
|
emitErrorFunc(errorNone, "Operation on None");
|
|
|
|
emitErrorFunc(errorDiv, "Division by zero");
|
|
|
|
emitErrorFunc(errorDiv, "Division by zero");
|
|
|
|
emitErrorFunc(errorOob, "Index out of bounds");
|
|
|
|
emitErrorFunc(errorOob, "Index out of bounds");
|
|
|
|
|
|
|
|
emitErrorFunc(errorNI, "Not Implemented");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Emit an error routine labeled ERRLABEL that aborts with message MSG. */
|
|
|
|
/** Emit an error routine labeled ERRLABEL that aborts with message MSG. */
|
|
|
|