package Temp;

/**
 * Temporaries for intermediate representation trees used in
 * compiling programs.
 * <p>
 * History
 * <dl>
 * <dt>Version 1.0 (unknown date)
 * <dd>Created by Andrew Appel as part of ``Modern Compiler
 *     implementation in Java''
 * <dt>Version 1.1 (10 December 1998)
 * <dd>Updated by Sam Rebelsky.
 * <dd>Changed the toString method.
 * <dd>Added a constructor that permits you to specify the
 *     number of the temporary.
 * <dd>Added some comments.
 * </dl>
 * 
 * @author Andrew Appel
 * @author Samuel A. Rebelsky
 * @version 1.0 of 10 December 1998
 */
public class Temp  {
   // +-----------+----------------------------------------------- 
   // | Constants |
   // +-----------+

   /**
    * The names of the predefined registers.
    */
   private String[] predefined = {
      "ZERO",
      "PC",
      "SP",
      "FP",
      "HP",
      "ACC",
      "RV",
      "RA",
      "EIGHT",
      "NINE",
      "A0",
      "A1",
      "A2",
      "A3",
      "A4",
      "A5",
      "A6",
      "A7",
      "A8",
      "A9",
   };


   // +--------+-------------------------------------------------- 
   // | Fields |
   // +--------+

   /**
    * The number of the temporary last allocated.
    */
   private static int count = 20;

   /**
    * The number of the current temporary.
    */
   private int num;


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

   /**
    * Build a new temporary.
    */
   public Temp() { 
     num=count++;
   } // Temp()
 
   /**
    * Build a new temporary with designated number.
    */
   public Temp(int num) {
     this.num = num;
     if (count <= num) {
       count = num+1;
     }
   } // Temp(int)

   /**
    * Build a temporary for one of the standard registers.
    */
   public Temp(String name) {
     // Look through the list until we find it.
     for (int i = 0; i < predefined.length; ++i) {
       if (predefined[i].equals(name)) {
         // Found!  Use the appropriate number.
         num = i;
         return;
       }
     }
     // Whoops, we've fallen through.  Just create a new
     // temporary.
     num = count++;
     
   } // Temp(String)


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

   /**
    * Convert to a string in preparation for printing.
    */
   public String toString() {
     if ((num < predefined.length) && (num >= 0)) 
       return predefined[num];
     else
       return "R(" + num + ")";
    } // toString()
} // class Temp

