1 cananian 1.1.2.1  // OPER.java, created Wed Aug  5 06:47:58 1998 by cananian
  2 cananian 1.1.2.1  // Copyright (C) 1998 C. Scott Ananian <cananian@alumni.princeton.edu>
  3 cananian 1.1.2.1  // Licensed under the terms of the GNU GPL; see COPYING for details.
  4 cananian 1.1.2.1  package harpoon.IR.Quads;
  5 cananian 1.1.2.1  
  6 cananian 1.1.2.1  import java.lang.reflect.Method;
  7 cananian 1.1.2.1  import java.lang.reflect.InvocationTargetException;
  8 cananian 1.1.2.1  import java.util.Hashtable;
  9 cananian 1.1.2.1  
 10 cananian 1.1.2.1  import harpoon.Analysis.Maps.ConstMap;
 11 cananian 1.1.2.10 import harpoon.ClassFile.HClass;
 12 cananian 1.1.2.10 import harpoon.ClassFile.HCodeElement;
 13 cananian 1.1.2.1  import harpoon.Temp.Temp;
 14 cananian 1.1.2.1  import harpoon.Temp.TempMap;
 15 cananian 1.1.2.1  import harpoon.Util.Util;
 16 cananian 1.1.2.3  
 17 cananian 1.1.2.1  /**
 18 cananian 1.1.2.1   * <code>OPER</code> objects represent arithmetic/logical operations,
 19 cananian 1.1.2.1   * including mathematical operators such as add and subtract, 
 20 cananian 1.1.2.1   * conversion operators such as double-to-int, and comparison
 21 cananian 1.1.2.1   * operators such as greater than and equals.
 22 cananian 1.1.2.1   * <p>
 23 cananian 1.1.2.1   * <code>OPER</code> quads never throw exceptions.  Any exception thrown
 24 cananian 1.1.2.1   * implicitly by the java bytecode opcode corresponding to an OPER is
 25 cananian 1.1.2.1   * rewritten as an explicit test and throw in the Quad IR.
 26 cananian 1.1.2.1   * 
 27 cananian 1.1.2.1   * @author  C. Scott Ananian <cananian@alumni.princeton.edu>
 28 cananian 1.5       * @version $Id: OPER.java,v 1.5 2002/04/11 04:00:34 cananian Exp $
 29 cananian 1.1.2.1   */
 30 cananian 1.1.2.1  public class OPER extends Quad {
 31 cananian 1.1.2.3      /** The <code>Temp</code> in which to store the result of the operation. */
 32 cananian 1.1.2.3      protected Temp dst;
 33 cananian 1.1.2.1      /** The operation to be performed, from the <code>Qop</code> class. */
 34 cananian 1.1.2.3      final protected int opcode;
 35 cananian 1.1.2.1      /** Operands of the operation, in left-to-right order. */
 36 cananian 1.1.2.3      protected Temp[] operands;
 37 cananian 1.1.2.1      /** Creates a <code>OPER</code>. */
 38 cananian 1.1.2.5      public OPER(QuadFactory qf, HCodeElement source,
 39 cananian 1.1.2.1                  int opcode, Temp dst, Temp[] operands) {
 40 cananian 1.1.2.5          super(qf, source);
 41 cananian 1.1.2.1          this.opcode = opcode;
 42 cananian 1.1.2.1          this.dst = dst;
 43 cananian 1.1.2.1          this.operands = operands;
 44 cananian 1.1.2.3          // VERIFY legality of OPER.
 45 cananian 1.3.2.1          assert dst!=null && operands!=null;
 46 cananian 1.1.2.9          if (kind()==QuadKind.OPER) // allow subclassing.
 47 cananian 1.3.2.1              assert Qop.isValid(opcode);
 48 cananian 1.1.2.3          for (int i=0; i<operands.length; i++)
 49 cananian 1.3.2.1              assert operands[i]!=null;
 50 cananian 1.1.2.1      }
 51 cananian 1.1.2.3      // ACCESSOR METHODS:
 52 cananian 1.1.2.3      /** Returns the <code>Temp</code> in which to store the result of the
 53 cananian 1.1.2.3       *  operation. */
 54 cananian 1.1.2.3      public Temp dst() { return dst; }
 55 cananian 1.1.2.3      /** Returns the operation to be performed, as an integer enumeration
 56 cananian 1.1.2.3       *  from the <code>Qop</code> class. */
 57 cananian 1.1.2.3      public int  opcode() { return opcode; }
 58 cananian 1.1.2.3      /** Returns an array of <code>Temp</code>s which are the operands
 59 cananian 1.1.2.3       *  of the operation. */
 60 cananian 1.1.2.3      public Temp[] operands()
 61 cananian 1.1.2.3      { return (Temp[]) Util.safeCopy(Temp.arrayFactory, operands); }
 62 cananian 1.1.2.3      /** Returns a specified element of the <code>operands</code> array. */
 63 cananian 1.1.2.3      public Temp operands(int i) { return operands[i]; }
 64 cananian 1.1.2.3      /** Returns the length of the <code>operands</code> array. */
 65 cananian 1.1.2.3      public int operandsLength() { return operands.length; }
 66 cananian 1.1.2.1  
 67 cananian 1.1.2.1      /** Returns the Temps used by this OPER. */
 68 cananian 1.1.2.2      public Temp[] use() 
 69 cananian 1.1.2.2      { return (Temp[]) Util.safeCopy(Temp.arrayFactory, operands); }
 70 cananian 1.1.2.1      /** Returns the Temps defined by this OPER. */
 71 cananian 1.1.2.1      public Temp[] def() { return new Temp[] { dst }; }
 72 cananian 1.1.2.1  
 73 cananian 1.1.2.4      public int kind() { return QuadKind.OPER; }
 74 cananian 1.1.2.4  
 75 cananian 1.1.2.6      public Quad rename(QuadFactory qqf, TempMap defMap, TempMap useMap) {
 76 cananian 1.1.2.6          return new OPER(qqf, this,
 77 cananian 1.1.2.6                          opcode, map(defMap,dst), map(useMap,operands));
 78 cananian 1.1.2.4      }
 79 cananian 1.1.2.6      /** Rename all used variables in this Quad according to a mapping.
 80 cananian 1.1.2.6       * @deprecated does not preserve immutability. */
 81 cananian 1.1.2.4      void renameUses(TempMap tm) {
 82 cananian 1.1.2.1          for (int i=0; i<operands.length; i++)
 83 cananian 1.1.2.1              operands[i] = tm.tempMap(operands[i]);
 84 cananian 1.1.2.1      }
 85 cananian 1.1.2.6      /** Rename all defined variables in this Quad according to a mapping.
 86 cananian 1.1.2.6       * @deprecated does not preserve immutability. */
 87 cananian 1.1.2.4      void renameDefs(TempMap tm) {
 88 cananian 1.1.2.1          dst = tm.tempMap(dst);
 89 cananian 1.1.2.1      }
 90 cananian 1.1.2.1  
 91 cananian 1.1.2.11     public void accept(QuadVisitor v) { v.visit(this); }
 92 cananian 1.5          public <T> T accept(QuadValueVisitor<T> v) { return v.visit(this); }
 93 cananian 1.1.2.11     public void accept(OperVisitor v) { v.dispatch(this); }
 94 cananian 1.1.2.1  
 95 cananian 1.1.2.1      /** Returns a human-readable representation of this Quad. */
 96 cananian 1.1.2.1      public String toString() {
 97 cananian 1.1.2.1          StringBuffer sb = new StringBuffer(dst.toString());
 98 cananian 1.1.2.1          sb.append(" = OPER " + Qop.toString(opcode) + "(");
 99 cananian 1.1.2.1          for (int i=0; i<operands.length; i++) {
100 cananian 1.1.2.1              sb.append(operands[i].toString());
101 cananian 1.1.2.1              if (i<operands.length-1)
102 cananian 1.1.2.1                  sb.append(", ");
103 cananian 1.1.2.1          }
104 cananian 1.1.2.1          sb.append(')');
105 cananian 1.1.2.1          return sb.toString();
106 cananian 1.1.2.1      }
107 cananian 1.1.2.1  
108 cananian 1.1.2.1      // -------------------------------------------------------
109 cananian 1.1.2.1      //   Evaluation functions.
110 cananian 1.1.2.1  
111 cananian 1.1.2.1      /** Determines the result type of an <code>OPER</code>. */
112 cananian 1.1.2.1      public HClass evalType() {
113 cananian 1.1.2.8          return Qop.resultType(opcode);
114 cananian 1.1.2.1      }
115 cananian 1.1.2.8  
116 cananian 1.1.2.1      /** Evaluates a constant value for the result of an <code>OPER</code>, 
117 cananian 1.1.2.1       *  given constant values for the operands. */
118 cananian 1.1.2.8      public Object evalValue(Object[] opvalues) { 
119 cananian 1.1.2.8          return Qop.evaluate(opcode, opvalues);
120 cananian 1.1.2.1      }
121 cananian 1.2      }