1 cananian 1.1.2.1 // ANEW.java, created Wed Aug 26 18:42:57 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.7 import harpoon.ClassFile.HClass; 7 cananian 1.1.2.7 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>ANEW</code> represents an array creation 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: ANEW.java,v 1.5 2002/04/11 04:00:28 cananian Exp $ 17 cananian 1.1.2.1 * @see NEW 18 cananian 1.1.2.1 * @see AGET 19 cananian 1.1.2.1 * @see ASET 20 cananian 1.1.2.1 * @see ALENGTH 21 cananian 1.1.2.1 */ 22 cananian 1.1.2.1 public class ANEW extends Quad { 23 cananian 1.1.2.2 /** The <code>Temp</code> in which to store the new array reference. */ 24 cananian 1.1.2.2 protected Temp dst; 25 cananian 1.1.2.1 /** Description of array class to create. */ 26 cananian 1.1.2.2 final protected HClass hclass; 27 cananian 1.1.2.1 /** Lengths of each dimension to create. */ 28 cananian 1.1.2.2 protected Temp dims[]; 29 cananian 1.1.2.1 30 cananian 1.1.2.1 /** Creates an <code>ANEW</code> object. <code>ANEW</code> creates 31 cananian 1.1.2.1 * an array of the type and number of dimensions indicated by 32 cananian 1.1.2.1 * the <code>hclass</code> parameter. Each entry in <code>dims</code> 33 cananian 1.1.2.1 * denotes the number of components in a particular dimension of the 34 cananian 1.1.2.1 * array. <code>dims[0]</code> corresponds to the left-most dimension. 35 cananian 1.1.2.1 * The array class referenced by <code>hclass</code> may have more 36 cananian 1.1.2.1 * dimensions than the length of the <code>dims</code> parameter. In 37 cananian 1.1.2.1 * that case, only the first <code>dims.length</code> dimensions of the 38 cananian 1.1.2.2 * array are created. 39 cananian 1.1.2.2 * @param dst 40 cananian 1.1.2.2 * the <code>Temp</code> in which to store the new array 41 cananian 1.1.2.2 * reference. 42 cananian 1.1.2.2 * @param hclass 43 cananian 1.1.2.2 * the array class to create. 44 cananian 1.1.2.2 * @param dims 45 cananian 1.1.2.2 * <code>Temp</code>s holding the length of each array 46 cananian 1.1.2.2 * dimension. 47 cananian 1.1.2.2 */ 48 cananian 1.1.2.4 public ANEW(QuadFactory qf, HCodeElement source, 49 cananian 1.1.2.1 Temp dst, HClass hclass, Temp dims[]) { 50 cananian 1.1.2.4 super(qf, source); 51 cananian 1.1.2.1 this.dst = dst; 52 cananian 1.1.2.1 this.hclass = hclass; 53 cananian 1.1.2.1 this.dims = dims; 54 cananian 1.1.2.2 // VERIFY legality of this ANEW. 55 cananian 1.3.2.1 assert dst!=null && hclass!=null && dims!=null; 56 cananian 1.3.2.1 assert hclass.isArray(); 57 cananian 1.3.2.1 assert dims.length>0; 58 cananian 1.1.2.2 for (int i=0; i<dims.length; i++) 59 cananian 1.3.2.1 assert dims[i]!=null; 60 cananian 1.1.2.1 } 61 cananian 1.1.2.2 // ACCESSOR FUNCTIONS: 62 cananian 1.1.2.2 /** Returns the destination <code>Temp</code>. */ 63 cananian 1.1.2.2 public Temp dst() { return dst; } 64 cananian 1.1.2.2 /** Returns the array class this <code>ANEW</code> will create. */ 65 cananian 1.1.2.2 public HClass hclass() { return hclass; } 66 cananian 1.1.2.2 /** Returns an array of <code>Temp</code>s holding the length of 67 cananian 1.1.2.2 * each array dimension. */ 68 cananian 1.1.2.2 public Temp[] dims() 69 cananian 1.1.2.2 { return (Temp[]) Util.safeCopy(Temp.arrayFactory, dims); } 70 cananian 1.1.2.2 /** Returns a particular element of the <code>dims</code> array. */ 71 cananian 1.1.2.2 public Temp dims(int i) { return dims[i]; } 72 cananian 1.1.2.2 /** Returns the length of the <code>dims</code> array. */ 73 cananian 1.1.2.2 public int dimsLength() { return dims.length; } 74 cananian 1.1.2.1 75 cananian 1.1.2.1 /** Returns the Temp defined by this Quad. 76 cananian 1.1.2.1 * @return the <code>dst</code> field. */ 77 cananian 1.1.2.1 public Temp[] def() { return new Temp[] { dst }; } 78 cananian 1.1.2.1 /** Returns the Temps used by this Quad. 79 cananian 1.1.2.1 * @return the <code>dims</code> field. */ 80 cananian 1.1.2.1 public Temp[] use() 81 cananian 1.1.2.1 { return (Temp[]) Util.safeCopy(Temp.arrayFactory, dims); } 82 cananian 1.1.2.1 83 cananian 1.1.2.3 public int kind() { return QuadKind.ANEW; } 84 cananian 1.1.2.3 85 cananian 1.1.2.5 public Quad rename(QuadFactory qqf, TempMap defMap, TempMap useMap) { 86 cananian 1.1.2.4 return new ANEW(qqf, this, 87 cananian 1.1.2.5 map(defMap,dst), hclass, map(useMap, dims)); 88 cananian 1.1.2.3 } 89 cananian 1.1.2.5 /** Rename all used variables in this Quad according to a mapping. 90 cananian 1.1.2.5 * @deprecated does not preserve immutability. */ 91 cananian 1.1.2.3 void renameUses(TempMap tm) { 92 cananian 1.1.2.1 for (int i=0; i<dims.length; i++) 93 cananian 1.1.2.1 dims[i] = tm.tempMap(dims[i]); 94 cananian 1.1.2.1 } 95 cananian 1.1.2.5 /** Rename all defined variables in this Quad according to a mapping. 96 cananian 1.1.2.5 * @deprecated does not preserve immutability. */ 97 cananian 1.1.2.3 void renameDefs(TempMap tm) { 98 cananian 1.1.2.1 dst = tm.tempMap(dst); 99 cananian 1.1.2.1 } 100 cananian 1.1.2.1 101 cananian 1.1.2.8 public void accept(QuadVisitor v) { v.visit(this); } 102 cananian 1.5 public <T> T accept(QuadValueVisitor<T> v) { return v.visit(this); } 103 cananian 1.1.2.1 104 cananian 1.1.2.1 /** Returns a human-readable representation of this quad. */ 105 cananian 1.1.2.1 public String toString() { 106 cananian 1.1.2.1 HClass hc = hclass; 107 cananian 1.1.2.1 int d,i; 108 cananian 1.1.2.1 for (d = 0; hc.isArray(); d++) 109 cananian 1.1.2.1 hc = hc.getComponentType(); 110 cananian 1.1.2.1 StringBuffer sb = new StringBuffer(dst.toString() + " = ANEW "); 111 cananian 1.1.2.1 sb.append(hc.getName()); 112 cananian 1.1.2.1 for (i = 0; i<dims.length; i++) 113 cananian 1.1.2.1 sb.append("["+dims[i]+"]"); 114 cananian 1.1.2.1 for (; i<d; i++) 115 cananian 1.1.2.1 sb.append("[]"); 116 cananian 1.1.2.1 return sb.toString(); 117 cananian 1.1.2.1 } 118 cananian 1.2 }