// $Id: CbrInstruction.java,v 1.1.1.1 1999/12/05 22:19:50 mpp Exp $

package IR2;

import java.util.*;

public class CbrInstruction extends LowInstruction {
  protected RValue condition;
  public LowInstruction next_if_true, next_if_false; 

  public CbrInstruction(RValue c, LowInstruction a, LowInstruction b) {
    condition = c; next_if_true = a; next_if_false = b;
    next = next_if_false;
    next_if_true.add_label();
    next_if_false.add_label();
  }

  public String desc() {
    return "cbr to " + next_if_true.get_label().get_name();
  }

  public String pretty_print_lowIR(int indent, boolean recursive) {
    boolean deja_vu = this.printed;
    String output = super.pretty_print_lowIR(indent, false);
    if (deja_vu) return output;

    for (int i = 0; i < indent; i++) output += " ";
    output += "   condition: " + condition.desc() + "\n";
    output += next_if_false.pretty_print_lowIR(indent, true);
    output += next_if_true.pretty_print_lowIR(indent, true);
    
    return output;
  }

  // Concrete implementation of method declared abstract in LowInstruction
  public DelocalizedInstruction do_asm(Codegen c, MethodDescriptor d, 
                                       CFG output, 
                                       DelocalizedInstruction prev) { 
    DelocalizedRValue cond = condition.generate_rvalue_asm(d, c, output);
    return output.addElement(
      new DelocalizedCondBranchInstruction(cond, 
                                        next_if_true.get_label().get_name()),
      output.last());
  } 
  
  /* Algebraic simplification optimization. */
  public void algebraic_simplify() {
    condition = condition.algebraic_simplify();
  }


  /* Traversable methods. */
  public void traverse(Traversal t) {
    t.save();
    t.traverse(next_if_false);
    t.restore();
    t.traverse(next_if_true);
  }
}
