IRLoop loop = new IRLoop(s);
s.startLoop(loop);
s.addInstr(new LABEL_Instr(loop.loopStartLabel));
if (isLoopHeadCondition) {
Operand cv = build(conditionNode, s);
s.addInstr(new BEQInstr(cv, isWhile ? BooleanLiteral.FALSE : BooleanLiteral.TRUE, loop.loopEndLabel));
}
s.addInstr(new LABEL_Instr(loop.iterStartLabel));
// Looks like while can be treated as an expression!
// So, capture the result of the body so that it can be returned.
Variable whileResult = s.getNewTemporaryVariable();
if (bodyNode != null) {
Operand v = build(bodyNode, s);
if (v != U_NIL) {
s.addInstr(new CopyInstr((Variable)whileResult, v));
}
else {
// If the body of the while had an explicit return, the value in 'whileResult' will never be used.
// So, we dont need to set it to anything. We can leave it undefined!
}
}
// SSS FIXME: Is this correctly placed ... at the end of the loop iteration?
s.addInstr(new ThreadPollInstr());
s.addInstr(new LABEL_Instr(loop.iterEndLabel));
if (isLoopHeadCondition) {
// Issue a jump back to the head of the while loop
s.addInstr(new JumpInstr(loop.loopStartLabel));
}
else {
Operand cv = build(conditionNode, s);
s.addInstr(new BEQInstr(cv, isWhile ? BooleanLiteral.TRUE : BooleanLiteral.FALSE, loop.iterStartLabel));
}
s.addInstr(new LABEL_Instr(loop.loopEndLabel));
s.endLoop(loop);