1 cananian 1.1.2.1  // SIGMA.java, created Mon Sep 14 03:03:50 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.8  import harpoon.ClassFile.HCodeElement;
  7 cananian 1.1.2.1  import harpoon.Temp.Temp;
  8 cananian 1.1.2.1  import harpoon.Temp.TempMap;
  9 cananian 1.1.2.1  import harpoon.Util.Util;
 10 cananian 1.1.2.3  
 11 cananian 1.1.2.1  /**
 12 cananian 1.1.2.1   * <code>SIGMA</code> functions are added where control flow splits. <p>
 13 cananian 1.1.2.1   * They have the form: <code>&lt;t1, t2, ..., tn&gt; = sigma(t0)</code>.
 14 cananian 1.1.2.1   * 
 15 cananian 1.1.2.1   * @author  C. Scott Ananian <cananian@alumni.princeton.edu>
 16 cananian 1.5       * @version $Id: SIGMA.java,v 1.5 2002/04/11 04:00:35 cananian Exp $
 17 cananian 1.1.2.1   */
 18 cananian 1.1.2.1  public abstract class SIGMA extends Quad {
 19 cananian 1.1.2.3      /** dst[i][j] is the j'th element of the tuple on the left-hand side
 20 cananian 1.1.2.3       *  of the i'th sigma function in this block. */
 21 cananian 1.1.2.3      protected Temp dst[][];
 22 cananian 1.1.2.3      /** src[i] is the argument to the i'th sigma function in this block. */
 23 cananian 1.1.2.3      protected Temp src[];
 24 cananian 1.1.2.1      
 25 cananian 1.1.2.3      /** Creates a <code>SIGMA</code> representing a block of sigma 
 26 cananian 1.1.2.3       *  functions.
 27 cananian 1.1.2.3       * @param dst
 28 cananian 1.1.2.3       *        the elements of the tuples on the left-hand side of a sigma
 29 cananian 1.1.2.3       *        function assignment block.
 30 cananian 1.1.2.3       * @param src
 31 cananian 1.1.2.3       *        the arguments to the sigma functions in this block.
 32 cananian 1.1.2.3       */
 33 cananian 1.1.2.5      public SIGMA(QuadFactory qf, HCodeElement source,
 34 cananian 1.1.2.5                   Temp dst[][], Temp src[], int arity) {
 35 cananian 1.1.2.5          super(qf, source, 1, arity);
 36 cananian 1.1.2.1          this.dst = dst;
 37 cananian 1.1.2.1          this.src = src;
 38 cananian 1.3.2.1          assert dst!=null && src!=null;
 39 cananian 1.3.2.1          assert arity>0;
 40 cananian 1.3.2.1          assert dst.length==src.length;
 41 cananian 1.1.2.3          for (int i=0; i<dst.length; i++)
 42 cananian 1.3.2.1              assert dst[i].length==arity;
 43 cananian 1.3.2.1          assert arity==arity();
 44 cananian 1.1.2.1      }
 45 cananian 1.1.2.3      /** Creates a <code>SIGMA</code> object with the specified arity.
 46 cananian 1.1.2.3       *  Each sigma function will return a tuple with <code>arity</code>
 47 cananian 1.1.2.3       *  elements.
 48 cananian 1.1.2.3       * @param src
 49 cananian 1.1.2.3       *        the arguments to the sigma functions.
 50 cananian 1.1.2.3       * @param arity
 51 cananian 1.1.2.3       *        the number of successors to this quad.
 52 cananian 1.1.2.3       */
 53 cananian 1.1.2.5      public SIGMA(QuadFactory qf, HCodeElement source, Temp src[], int arity) {
 54 cananian 1.1.2.5          this(qf, source, new Temp[src.length][arity], src, arity);
 55 cananian 1.1.2.3      }
 56 cananian 1.1.2.3      // ACCESSOR METHODS:
 57 cananian 1.1.2.3      /** Returns the argument to the <code>nSigma</code>'th sigma function in
 58 cananian 1.1.2.3       *  the block. */
 59 cananian 1.1.2.3      public Temp src(int nSigma) { return src[nSigma]; }
 60 cananian 1.1.2.3      /** Returns the <code>nTuple</code>'th element of the tuple returned
 61 cananian 1.1.2.3       *  by the <code>nSigma</code>'th sigma function in the block. */
 62 cananian 1.1.2.3      public Temp dst(int nSigma, int nTuple) { return dst[nSigma][nTuple]; }
 63 cananian 1.1.2.3      /** Returns an array holding the elements of the tuple returned by
 64 cananian 1.1.2.3       *  the <code>nSigma</code>'th sigma function in the block. */
 65 cananian 1.1.2.3      public Temp[] dst(int nSigma)
 66 cananian 1.1.2.3      { return (Temp[]) Util.safeCopy(Temp.arrayFactory, dst[nSigma]); }
 67 cananian 1.1.2.3      
 68 bdemsky  1.1.2.11     public Temp[] src() {
 69 bdemsky  1.1.2.11         return (Temp[]) Util.safeCopy(Temp.arrayFactory, src);
 70 bdemsky  1.1.2.11     }
 71 bdemsky  1.1.2.11     public Temp[][] dst() {
 72 bdemsky  1.1.2.11         return (Temp[][]) Util.safeCopy(Temp.doubleArrayFactory, dst);
 73 bdemsky  1.1.2.11     }
 74 bdemsky  1.1.2.11 
 75 cananian 1.1.2.3      /** Returns the number of sigma functions in the block. */
 76 cananian 1.1.2.3      public int numSigmas() { return src.length; }
 77 cananian 1.1.2.3      /** Returns the number of elements in the tuple returned by each 
 78 cananian 1.1.2.3       *  sigma function. */
 79 cananian 1.1.2.3      public int arity() { return next.length; }
 80 cananian 1.1.2.3  
 81 cananian 1.1.2.9      /** Removes a given sigma function from the block.
 82 cananian 1.1.2.9       *  @deprecated Does not preserve immutability. */
 83 cananian 1.1.2.3      public void removeSigma(int nSigma) {
 84 cananian 1.3.2.1          assert 0<=nSigma && nSigma<numSigmas();
 85 cananian 1.1.2.3          src = (Temp[])   Util.shrink(Temp.arrayFactory,       src, nSigma);
 86 cananian 1.1.2.3          dst = (Temp[][]) Util.shrink(Temp.doubleArrayFactory, dst, nSigma);
 87 cananian 1.1.2.1      }
 88 cananian 1.1.2.1  
 89 cananian 1.1.2.1      public void assign(Temp[] d, int which_succ) {
 90 cananian 1.3.2.1          assert d.length == src.length;
 91 cananian 1.1.2.1          for (int i=0; i < d.length; i++)
 92 cananian 1.1.2.1              dst[i][which_succ] = d[i];
 93 cananian 1.1.2.1      }
 94 cananian 1.1.2.1  
 95 cananian 1.1.2.2      public Temp[] use()
 96 cananian 1.1.2.2      { return (Temp[]) Util.safeCopy(Temp.arrayFactory, src); }
 97 cananian 1.1.2.2  
 98 cananian 1.1.2.1      public Temp[] def() {
 99 cananian 1.1.2.1          int n=0;
100 cananian 1.1.2.1          for (int i=0; i<dst.length; i++)
101 cananian 1.1.2.1              n+=dst[i].length;
102 cananian 1.1.2.1          Temp[] d = new Temp[n];
103 cananian 1.1.2.1          n=0;
104 cananian 1.1.2.1          for (int i=0; i<dst.length; i++) {
105 cananian 1.1.2.1              System.arraycopy(dst[i], 0, d, n, dst[i].length);
106 cananian 1.1.2.1              n+=dst[i].length;
107 cananian 1.1.2.1          }
108 cananian 1.1.2.1          return d;
109 cananian 1.1.2.1      }
110 cananian 1.1.2.4  
111 cananian 1.1.2.6      /* @deprecated does not preserve immutability. */
112 cananian 1.1.2.4      void renameUses(TempMap tm) {
113 cananian 1.1.2.1          for (int i=0; i<src.length; i++) {
114 cananian 1.1.2.1              src[i] = tm.tempMap(src[i]);
115 cananian 1.1.2.1          }
116 cananian 1.1.2.1      }
117 cananian 1.1.2.6      /* @deprecated does not preserve immutability. */
118 cananian 1.1.2.4      void renameDefs(TempMap tm) {
119 cananian 1.1.2.1          for (int i=0; i<dst.length; i++) {
120 cananian 1.1.2.1              for (int j=0; j<dst[i].length; j++)
121 cananian 1.1.2.1                  dst[i][j] = tm.tempMap(dst[i][j]);
122 cananian 1.1.2.1          }
123 cananian 1.1.2.1      }
124 cananian 1.1.2.1  
125 cananian 1.1.2.10     public void accept(QuadVisitor v) { v.visit(this); }
126 cananian 1.5          public <T> T accept(QuadValueVisitor<T> v) { return v.visit(this); }
127 cananian 1.1.2.1  
128 cananian 1.1.2.1      public String toString() {
129 cananian 1.1.2.1          StringBuffer sb = new StringBuffer("SIGMA("+next().length+"): ");
130 cananian 1.1.2.1          for (int i=0; i<src.length; i++) {
131 cananian 1.1.2.1              sb.append("<");
132 cananian 1.1.2.1              for (int j=0; j<dst[i].length; j++) {
133 cananian 1.1.2.1                  sb.append(dst[i][j].toString());
134 cananian 1.1.2.1                  if (j < dst[i].length - 1)
135 cananian 1.1.2.1                      sb.append(", ");
136 cananian 1.1.2.1              }
137 cananian 1.1.2.1              sb.append(">=");
138 cananian 1.1.2.1              sb.append(src[i].toString());
139 cananian 1.1.2.1              if (i < src.length-1)
140 cananian 1.1.2.1                  sb.append("; ");
141 cananian 1.1.2.1          }
142 cananian 1.1.2.1          return sb.toString();
143 cananian 1.1.2.1      }
144 cananian 1.2      }