package rebelsky.pal;

/**
 * The basic integer add instruction in PAL.
 *
 * @author Samuel A. Rebelsky
 * @version 1.1 of December 2002.
 */
public class IAdd
  implements Instruction
{
  // +--------+------------------------------------------------------------
  // | Fields |
  // +--------+

  /** The left operand of the operation. */
  Variable left;

  /** The right operand of the operation. */
  Variable right;

  /** The destination of the opeartion. */
  Variable destination;


  // +--------------+------------------------------------------------------
  // | Constructors |
  // +--------------+

  /** 
   * Create a new instruction to add two values and store the
   * result in destination.
   */
  public IAdd(Variable left, Variable right, Variable destination)
  {
    this.left = left;
    this.right = right;
    this.destination = destination;
  } // IAdd(Variable,Variable,Variable)


  // +---------+-----------------------------------------------------------
  // | Methods |
  // +---------+

  /** 
   * Execute the instruction on a machine. 
   * 
   * @exception Exception
   *   If one of the memory locations is invalid.
   *   If the destination is a constant or label.
   */
  public void execute(Computer hal)
    throws Exception
  {
    destination.iset(hal, (left.iget(hal) + right.iget(hal)));
  } // execute(Computer)

  /** Convert the instruction to a string (usually for printing). */
  public String toString()
  {
    return "  IADD "
           + left.toString() + ", "  + right.toString()
           + " -> " + destination.toString();
  } // toString()

} // class IAdd

