BinaryExpr Integer operands Analyze

master
Apoorva Ranade 4 years ago
parent a822a4ab69
commit 4b1b313bbe

@ -53,6 +53,9 @@ public class CodeGenImpl extends CodeGenBase
/** Index out of bounds. */
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.
*
@ -312,6 +315,9 @@ public class CodeGenImpl extends CodeGenBase
}
@Override
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);
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);
sp_off--;
backend.emitLW(Register.T0, Register.FP, -sp_off*wordSize, "Pop stack slot "+sp_off);
// Arithmetic Operators
if(node.operator.equals("+"))
if(operator.equals("+"))
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");
else if(node.operator.equals("*"))
else if(operator.equals("*"))
backend.emitMUL(Register.A0, Register.A0, Register.T0, "Mul operation");
else if(node.operator.equals("/"))
backend.emitDIV(Register.A0, Register.A0, Register.T0, "Div 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.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
{ // Comparison operators
elseBlock = generateLocalLabel();
String comment="Branch on not "+node.operator;
if(node.operator.equals("=="))
backend.emitBNE(Register.A0, Register.T0,elseBlock, comment);
else if(node.operator.equals("!="))
backend.emitBEQ(Register.A0, Register.T0,elseBlock, comment);
else if(node.operator.equals("<"))
backend.emitBGE(Register.A0, Register.T0,elseBlock, comment);
else if(node.operator.equals(">"))
//elseBlock = generateLocalLabel();
String comment="Operator: "+operator;
if(operator.equals("=="))
{
backend.emitXOR(Register.A0, Register.A0, Register.T0, "Check for equality");
backend.emitSEQZ(Register.A0, Register.A0, "Result is True if XOR equals 0");
}
else if(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.emitBEQ(Register.A0, Register.T0,elseBlock, comment);
backend.emitADDI(Register.A0,Register.A0, 1, "Increment by 1");
backend.emitSLT(Register.A0, Register.A0, Register.T0, comment);
}
else if(node.operator.equals(">="))
backend.emitBLT(Register.A0, Register.T0,elseBlock, comment);
else if(node.operator.equals("<="))
else if(operator.equals("is"))
{
Label temp = generateLocalLabel();
backend.emitBEQ(Register.A0, Register.T0,temp, "Branch on "+node.operator);
backend.emitBGE(Register.A0, Register.T0,elseBlock, comment);
backend.emitLocalLabel(temp, "True part of if check");
backend.emitXOR(Register.A0, Register.A0, Register.T0, "is operation");
backend.emitSEQZ(Register.A0, Register.A0, "Result is True if XOR equals 0");
}
else
{}
}
}
else if(node.left.getInferredType().equals(Type.STR_TYPE) && node.right.getInferredType().equals(Type.STR_TYPE))
{
}
return null;
}
@ -534,6 +571,7 @@ public class CodeGenImpl extends CodeGenBase
emitErrorFunc(errorNone, "Operation on None");
emitErrorFunc(errorDiv, "Division by zero");
emitErrorFunc(errorOob, "Index out of bounds");
emitErrorFunc(errorNI, "Not Implemented");
}
/** Emit an error routine labeled ERRLABEL that aborts with message MSG. */

Loading…
Cancel
Save