Analyze Binary expression
This commit is contained in:
@@ -162,7 +162,7 @@ public class CodeGenImpl extends CodeGenBase {
|
|||||||
protected Label elseBlock;
|
protected Label elseBlock;
|
||||||
// Variable to store offset from frame pointer to identify next
|
// Variable to store offset from frame pointer to identify next
|
||||||
// empty space on stack frame to store variable
|
// empty space on stack frame to store variable
|
||||||
private final int offset = 12;
|
private int offset = 3;
|
||||||
/**
|
/**
|
||||||
* An analyzer for the function described by FUNCINFO0, which is null for the
|
* An analyzer for the function described by FUNCINFO0, which is null for the
|
||||||
* top level.
|
* top level.
|
||||||
@@ -272,7 +272,45 @@ public class CodeGenImpl extends CodeGenBase {
|
|||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public Void analyze(BinaryExpr node) {
|
public Void analyze(BinaryExpr node) {
|
||||||
|
node.left.dispatch(this);
|
||||||
|
backend.emitSW(Register.A0, Register.FP, offset*4, "Push on stack slot "+offset);
|
||||||
|
offset++;
|
||||||
|
node.right.dispatch(this);
|
||||||
|
offset--;
|
||||||
|
backend.emitLW(Register.T0, Register.FP, (offset)*4, "Pop stack slot "+offset);
|
||||||
|
if(node.operator.equals("+"))
|
||||||
|
backend.emitADD(Register.A0, Register.A0, Register.T0, "Add operation");
|
||||||
|
else if(node.operator.equals("-"))
|
||||||
|
backend.emitSUB(Register.A0, Register.A0, Register.T0, "Sub operation");
|
||||||
|
else if(node.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
|
||||||
|
{
|
||||||
elseBlock = generateLocalLabel();
|
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(">"))
|
||||||
|
{
|
||||||
|
backend.emitBLT(Register.A0, Register.T0,elseBlock, comment);
|
||||||
|
backend.emitBEQ(Register.A0, Register.T0,elseBlock, comment);
|
||||||
|
}
|
||||||
|
else if(node.operator.equals(">="))
|
||||||
|
backend.emitBLT(Register.A0, Register.T0,elseBlock, comment);
|
||||||
|
else if(node.operator.equals("<="))
|
||||||
|
{
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user