master
bill 4 years ago
commit efc8ab2024

@ -434,8 +434,9 @@ public class CodeGenImpl extends CodeGenBase
@Override @Override
public Register analyze(IfExpr node) public Register analyze(IfExpr node)
{ {
node.condition.dispatch(this); Register result = node.condition.dispatch(this);
Label ln = generateLocalLabel(); Label ln = generateLocalLabel();
backend.emitBEQZ(result, ln,"Jump to beginning of loop");
node.thenExpr.dispatch(this); node.thenExpr.dispatch(this);
backend.emitJ(ln, "Jump to end of if expression"); backend.emitJ(ln, "Jump to end of if expression");
backend.emitLocalLabel(elseBlock, "Else part of if expression"); backend.emitLocalLabel(elseBlock, "Else part of if expression");
@ -447,8 +448,9 @@ public class CodeGenImpl extends CodeGenBase
@Override @Override
public Register analyze(IfStmt node) public Register analyze(IfStmt node)
{ {
node.condition.dispatch(this); Register result = node.condition.dispatch(this);
Label ln = generateLocalLabel(); Label ln = generateLocalLabel();
backend.emitBEQZ(result, ln,"Jump to beginning of loop");
for(Stmt s:node.thenBody) for(Stmt s:node.thenBody)
s.dispatch(this); s.dispatch(this);
backend.emitJ(ln, "Jump to end of if statement"); backend.emitJ(ln, "Jump to end of if statement");
@ -737,13 +739,13 @@ public class CodeGenImpl extends CodeGenBase
{ {
Label startLoop = generateLocalLabel(); Label startLoop = generateLocalLabel();
backend.emitLocalLabel(startLoop, "Beginning of while loop"); backend.emitLocalLabel(startLoop, "Beginning of while loop");
node.condition.dispatch(this); Register result = node.condition.dispatch(this);
Label endLoop = elseBlock; Label endLoop = elseBlock;
backend.emitBEQZ(result, endLoop,"Jump to beginning of loop");
for(Stmt stmt:node.body) for(Stmt stmt:node.body)
stmt.dispatch(this); stmt.dispatch(this);
backend.emitJ(startLoop, "Jump to beginning of loop"); backend.emitJ(startLoop, "Jump to beginning of loop");
backend.emitLocalLabel(endLoop, "End of while loop"); backend.emitLocalLabel(endLoop, "End of while loop");
return null; return null;
} }

Loading…
Cancel
Save