1 cananian 1.1.2.1  // ARRAYINIT.java, created Fri Dec 11 07:06:49 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.HClass;
  7 cananian 1.1.2.8  import harpoon.ClassFile.HCodeElement;
  8 cananian 1.1.2.1  import harpoon.Temp.Temp;
  9 cananian 1.1.2.1  import harpoon.Temp.TempMap;
 10 cananian 1.1.2.1  import harpoon.Util.Util;
 11 cananian 1.1.2.1  
 12 cananian 1.1.2.1  /**
 13 cananian 1.1.2.1   * <code>ARRAYINIT</code> represents an array initialization operation.
 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: ARRAYINIT.java,v 1.5 2002/04/11 04:00:29 cananian Exp $
 17 cananian 1.1.2.1   */
 18 cananian 1.1.2.1  public class ARRAYINIT extends Quad {
 19 cananian 1.1.2.1      /** The array reference to initialize. */
 20 cananian 1.1.2.1      protected Temp objectref;
 21 cananian 1.1.2.1      /** The component type. */
 22 cananian 1.1.2.1      protected HClass type;
 23 cananian 1.1.2.5      /** The starting index for the initializers. */
 24 cananian 1.1.2.5      protected int offset;
 25 cananian 1.1.2.9      /** The array initializers. Elements must be instances of the type
 26 cananian 1.1.2.9       *  wrapper. */
 27 cananian 1.1.2.1      protected Object[] value;
 28 cananian 1.1.2.1  
 29 cananian 1.1.2.1      /** Creates a <code>ARRAYINIT</code> representing an array initializer.
 30 cananian 1.1.2.1       *  The values in the <code>value</code> array are stored in sequential
 31 cananian 1.1.2.1       *  elements of the array referenced by <code>objectref</code> starting
 32 cananian 1.1.2.1       *  at element 0.
 33 cananian 1.1.2.1       * @param objectref
 34 cananian 1.1.2.1       *        the array to initialize.
 35 cananian 1.1.2.5       * @param offset
 36 cananian 1.1.2.5       *        the starting index for the initializers.
 37 cananian 1.1.2.1       * @param type
 38 cananian 1.1.2.1       *        the component type of the array.
 39 cananian 1.1.2.1       * @param value
 40 cananian 1.1.2.1       *        the values to store in the array.
 41 cananian 1.1.2.1       */
 42 cananian 1.1.2.3      public ARRAYINIT(QuadFactory qf, HCodeElement source,
 43 cananian 1.1.2.5                       Temp objectref, int offset, HClass type, Object[] value) {
 44 cananian 1.1.2.3          super(qf, source);
 45 cananian 1.1.2.1          this.objectref = objectref;
 46 cananian 1.1.2.1          this.type = type;
 47 cananian 1.1.2.5          this.offset = offset;
 48 cananian 1.1.2.1          this.value = value;
 49 cananian 1.1.2.1          // VERIFY legality of ARRAYINIT.
 50 cananian 1.3.2.1          assert objectref!=null && type!=null && value!=null;
 51 cananian 1.3.2.1          assert type.isPrimitive() && type!=HClass.Void;
 52 cananian 1.3.2.1          /*assert offset>=0; // legal, it just throws an exception. */
 53 cananian 1.1.2.1      }
 54 cananian 1.1.2.1      /** Returns the <code>Temp</code> referencing the array to be
 55 cananian 1.1.2.1       *  initialized. */
 56 cananian 1.1.2.1      public Temp objectref() { return objectref; }
 57 cananian 1.1.2.1      /** Returns the component type of the array to be initialized. */
 58 cananian 1.1.2.1      public HClass type() { return type; }
 59 cananian 1.1.2.5      /** Returns the starting offset of the initializers. */
 60 cananian 1.1.2.5      public int offset() { return offset; }
 61 cananian 1.1.2.1      /** Returns the array initializers. */
 62 cananian 1.1.2.1      public Object[] value() { return (Object[]) value.clone(); }
 63 cananian 1.1.2.1  
 64 cananian 1.1.2.1      /** Returns the <code>Temp</code> used by this <code>Quad</code>.
 65 cananian 1.1.2.1       * @return the <code>objectref</code> field.
 66 cananian 1.1.2.1       */
 67 cananian 1.1.2.1      public Temp[] use() { return new Temp[] { objectref }; }
 68 cananian 1.1.2.1  
 69 cananian 1.1.2.2      public int kind() { return QuadKind.ARRAYINIT; }
 70 cananian 1.1.2.2  
 71 cananian 1.1.2.6      public Quad rename(QuadFactory qqf, TempMap defMap, TempMap useMap) {
 72 cananian 1.1.2.6          return new ARRAYINIT(qqf, this, map(useMap,objectref), offset, type, 
 73 cananian 1.1.2.2                               (Object[]) value.clone());
 74 cananian 1.1.2.2      }
 75 cananian 1.1.2.6      /** Rename all defined variables in this Quad according to a mapping.
 76 cananian 1.1.2.6       * @deprecated does not preserve immutability. */
 77 cananian 1.1.2.2      void renameUses(TempMap tm) {
 78 cananian 1.1.2.1          objectref = tm.tempMap(objectref);
 79 cananian 1.1.2.1      }
 80 cananian 1.1.2.1  
 81 cananian 1.1.2.10     public void accept(QuadVisitor v) { v.visit(this); }
 82 cananian 1.5          public <T> T accept(QuadValueVisitor<T> v) { return v.visit(this); }
 83 cananian 1.1.2.1  
 84 cananian 1.1.2.1      /** Returns human-readable representation of this quad. */
 85 cananian 1.1.2.1      public String toString() {
 86 cananian 1.1.2.1          StringBuffer sb = new StringBuffer();
 87 cananian 1.1.2.1          sb.append(objectref.toString());
 88 cananian 1.1.2.5          sb.append("["+offset+"-"+(offset+value.length-1)+"]");
 89 cananian 1.1.2.5          sb.append(" = ARRAYINIT (");
 90 cananian 1.1.2.1          sb.append(type.getName());
 91 cananian 1.1.2.1          sb.append(") { ");
 92 cananian 1.1.2.1          for (int i=0; i<value.length; i++) {
 93 cananian 1.1.2.11             if (type.getName().equals("java.lang.String"))
 94 cananian 1.1.2.4                  sb.append("\""+Util.escape(value[i].toString())+"\"");
 95 cananian 1.1.2.9              else if (type.equals(HClass.Char))
 96 cananian 1.1.2.9                  sb.append("\'"+Util.escape(value[i].toString())+"\'");
 97 cananian 1.1.2.1              else
 98 cananian 1.1.2.4                  sb.append(value[i].toString());
 99 cananian 1.1.2.1              if (i < value.length-1)
100 cananian 1.1.2.1                  sb.append(", ");
101 cananian 1.1.2.1          }
102 cananian 1.1.2.1          sb.append(" }");
103 cananian 1.1.2.1          return sb.toString();
104 cananian 1.1.2.1      }
105 cananian 1.2      }