Lib/Source/Translate.cpp
/**
 * Translate if-then-else statement to target code
 */
Translate if-then-else statement to target code
Translate if - then - else statement to target code
void translateIf(Instr::List &seq, Stmt &s) {
  using namespace Target::instr;
  Label endifLabel = freshLabel();
  BranchCond cond  = condExp(seq, *s.if_cond());  
  if (s.else_block().empty()) {
    seq << branch(endifLabel).branch_cond(cond.negate());  
    stmts(&seq, s.then_block());                  
  } else {
    Label elseLabel = freshLabel();
    seq << branch(elseLabel).branch_cond(cond.negate());   
    stmts(&seq, s.then_block());                  
    seq << branch(endifLabel)                  
        << label(elseLabel);                   
    stmts(&seq, s.else_block());                  
  }
  seq << label(endifLabel);                    
}
void translateIf ( Instr :: List & seq , Stmt & s ) { using namespace Target :: instr ; Label endifLabel = freshLabel ( ) ; BranchCond cond = condExp ( seq , * s . if_cond ( ) ) ; if ( s . else_block ( ) . empty ( ) ) { seq << branch ( endifLabel ) . branch_cond ( cond . negate ( ) ) ; stmts ( & seq , s . then_block ( ) ) ; } else { Label elseLabel = freshLabel ( ) ; seq << branch ( elseLabel ) . branch_cond ( cond . negate ( ) ) ; stmts ( & seq , s . then_block ( ) ) ; seq << branch ( endifLabel ) << label ( elseLabel ) ; stmts ( & seq , s . else_block ( ) ) ; } seq << label ( endifLabel ) ; }
Translate if-then-else statement to target code
Translate if - then - else statement to target code
// Compile condition // Branch over 'then' statement // Compile 'then' statement // Branch to 'else' statement // Compile 'then' statement // Branch to endif // Label for 'else' statement // Compile 'else' statement // Label for endif