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

package IR2;

import java.util.*;

public abstract class BinaryOpExpr extends RValue {
  protected RValue arg1, arg2;

  public BinaryOpExpr(RValue a1, RValue a2) {
    arg1 = a1;
    arg2 = a2;
  }

  public Enumeration neighbors() {
	return new ShortEnumeration(arg1, arg2);
  }

  public String desc(String op) { 
	return "(" + arg1.desc() + ") " + op + " (" + arg2.desc() + ")";
  }

  public DelocalizedRValue generate_rvalue_asm
    (MethodDescriptor d, Codegen c, CFG output) {
    DelocalizedRValue da1 = arg1.generate_rvalue_asm(d, c, output);
    DelocalizedRValue da2 = arg2.generate_rvalue_asm(d, c, output);
    DelocalizedLValue result = new TempVarDescriptor(d);
    output.addElement
      (new DelocalizedBinaryOpInstruction(op_num(), result, da1, da2));
    return result;
  }
  // Child classes should override this to return the appropriate operator #.
  protected int op_num() { return -1; }


  /* Algebraic simplification optimization. */
  public RValue algebraic_simplify() {
    arg1 = arg1.algebraic_simplify();
    arg2 = arg2.algebraic_simplify();
    return this;
  }


  public String pretty_print(int indent, boolean recursive) {
	String output = new String();
	
	for (int i = 0; i < indent; i++) output += " ";
	if (recursive) {
      indent += 2;
      output += "(" + this.node_name() + "\n";
      /* stuff - calls to prettyprints of internals of this node*/
      for (Enumeration e = this.neighbors(); e.hasMoreElements();) {
		Walkable foo = (Walkable)e.nextElement();
		if (foo == null) 
          output += "-error-";
		else
          output += foo.pretty_print(indent, true);
      }
      for (int i = 0; i < indent; i++) output += " ";
      output += ") /* "+ this.node_name() + " */\n";
	} else {
      output += "(" + this.node_name() + ")\n";
	}
	return output;
  }
}
