start of new file
[IRC.git] / Robust / src / Analysis / Prefetch / PrefetchPair.java
index ee4c8c91282f99426173b1a1f7456750f113c2f3..fa0ecaab1ec560366c7a341faa5264bcd8a7f997 100644 (file)
@@ -4,114 +4,136 @@ import java.util.*;
 import IR.*;
 
 public class PrefetchPair {
-       TempDescriptor base;
-       ArrayList<Descriptor> desc;
-       ArrayList<Boolean> isTempDesc; //Keeps track if the desc is a FieldDescriptor or TempDescriptor. Has same size() as desc
-                               
+    public TempDescriptor base;
+    public ArrayList<Descriptor> desc;
+    
+    public PrefetchPair(){
+       base = new TempDescriptor("");
+       desc = new ArrayList<Descriptor>();
+    }
+    
+    public PrefetchPair(TempDescriptor t, Descriptor f) {
+       base = t;
+       desc = new ArrayList<Descriptor>();
+       desc.add(f);
+    }
 
-       public PrefetchPair() {
-       }
-
-       public PrefetchPair(TempDescriptor t) {
-               base = t;
-               desc = null;
-               isTempDesc = null;
-       }
-
-       public PrefetchPair(TempDescriptor t, Descriptor f, Boolean type) {
-               base = t;
-               if (desc == null) 
-                       desc = new ArrayList<Descriptor>();
-               if (isTempDesc == null)
-                       isTempDesc = new ArrayList<Boolean>();
-               desc.add(f);
-               isTempDesc.add(type);
-       }
-
-       public PrefetchPair(TempDescriptor t, ArrayList<Descriptor> descriptor, ArrayList<Boolean> bool) {
-               base = t;
-               if(desc == null){
-                       desc = new ArrayList<Descriptor>();
-               }
-               if(isTempDesc == null)
-                       isTempDesc = new ArrayList<Boolean>();
-               desc.addAll(descriptor);
-               isTempDesc.addAll(bool);
-       }
-
-       public TempDescriptor getBase() {
-               return base;
-       }
+    public PrefetchPair(TempDescriptor t, ArrayList<Descriptor> descriptor) {
+       base = t;
+       desc = new ArrayList<Descriptor>();
+       desc.addAll(descriptor);
+    }
 
-       public boolean isTempDescDesc(int index) {
-               return isTempDesc.get(index).booleanValue();
+    public TempDescriptor getBase() {
+       return base;
+    }
+    
+    public Descriptor getDescAt(int index) {
+       return desc.get(index);
+    }
+    
+    public ArrayList<Descriptor> getDesc() {
+       return desc;
+    }
+    
+    public FieldDescriptor getFieldDesc(int index) {
+       return (FieldDescriptor) desc.get(index);
+    }
+    
+    public IndexDescriptor getIndexDesc(int index) {
+       return (IndexDescriptor) desc.get(index);
+    }
+    
+    public int hashCode() {
+       int hashcode = base.toString().hashCode();
+       if(desc != null) {
+           hashcode^=desc.hashCode();
        }
-
-       public Descriptor getDescAt(int index) {
-               return desc.get(index);
-       }
-
-       public ArrayList<Descriptor> getDesc() {
-               return desc;
-       }
-
-       public ArrayList<Boolean> getisTempDesc() {
-               return isTempDesc;
+       return hashcode;
+    }
+    
+    public String toString() {
+       String label= getBase().toString();
+       if(getDesc() == null)
+           return label;
+       ListIterator it=getDesc().listIterator();
+       for(;it.hasNext();) {
+           Object o = it.next();
+           if(o instanceof FieldDescriptor) {
+               FieldDescriptor fd = (FieldDescriptor) o;
+               label+="."+ fd.toString();
+           } else { 
+               IndexDescriptor id = (IndexDescriptor) o;
+               label+= id.toString();
+           }
        }
-
-       public FieldDescriptor getFieldDesc(int index) {
-               return (FieldDescriptor) desc.get(index);
-       }
-
-       public TempDescriptor getTempDesc(int index) {
-               return (TempDescriptor) desc.get(index);
+       return label;
+    }
+    
+    public boolean equals(Object o) {
+       if(o instanceof PrefetchPair) {
+           PrefetchPair pp = (PrefetchPair) o;
+           return base == pp.base && desc.equals(pp.desc);
        }
-
-       public int hashCode() {
-               int hashcode = base.toString().hashCode();
-               if(desc != null) {
-                       ListIterator li = desc.listIterator();
-                       while(li.hasNext()) {
-                               hashcode = hashcode ^ li.next().toString().hashCode();
-                       }
+       return false;
+    }
+    
+    public Object clone() {
+       PrefetchPair newpp = new PrefetchPair();
+       newpp.base = this.base;
+       for(int i = 0; i < this.desc.size(); i++) {
+           Object o = desc.get(i);
+           if(o instanceof FieldDescriptor) {
+               newpp.desc.add((FieldDescriptor) o);
+           } else {
+               ArrayList<TempDescriptor> td = new ArrayList<TempDescriptor>();
+               for(int j = 0; j < ((IndexDescriptor)o).tddesc.size(); j++) {
+                   td.add(((IndexDescriptor)o).getTempDescAt(j));
                }
-               return hashcode;
+               IndexDescriptor idesc = new IndexDescriptor();
+               idesc.tddesc = td;
+               idesc.offset = ((IndexDescriptor)o).offset;
+               newpp.desc.add(idesc);
+           }
        }
+       return newpp;
+    }
 
-       public String toString() {
-               String label= getBase().toString();
-               if(getDesc() == null || getisTempDesc() == null)
-                       return label;
-               ListIterator it=getDesc().listIterator();
-               ListIterator istemp=getisTempDesc().listIterator();
-               for(;it.hasNext() && istemp.hasNext();) {
-                       Boolean isFd = (Boolean) istemp.next();
-                       if(isFd.booleanValue() == false) {
-                               FieldDescriptor fd = (FieldDescriptor) it.next();
-                               label+="."+ fd.toString();
-                       } else { 
-                               TempDescriptor td = (TempDescriptor) it.next();
-                               label+="."+ td.toString();
-                       }
+    /** This function returns true if a tempdescriptor object is found in the array of descriptors
+     *  for a given prefetch pair else returns false*/
+    public boolean containsTemp(TempDescriptor td) {
+       ArrayList<Descriptor> desc = (ArrayList<Descriptor>) getDesc();
+       for(ListIterator it = desc.listIterator();it.hasNext();) {
+           Object o = it.next();
+           if(o instanceof IndexDescriptor) {
+               ArrayList<TempDescriptor> tdarray = (ArrayList<TempDescriptor>)((IndexDescriptor)o).tddesc;
+               if(tdarray.contains(td)) {
+                   return true;
                }
-               return label;
+           }
        }
+       return false;
+    }
 
-       public boolean equals(Object o) {
-               if(o instanceof PrefetchPair) {
-                       PrefetchPair pp = (PrefetchPair) o;
-                       if(base != pp.base) {
-                               return false;
-                       }
-                       if (desc == null && pp.desc == null) {
-                               return true;
-                       } else if (desc != null && pp.desc != null) {
-                               if (desc.equals((List<Descriptor>)pp.desc) && 
-                                               isTempDesc.equals((List<Boolean>)pp.isTempDesc)) {
-                                       return true;
-                               } 
-                       }
+    /** This function creates a new Arraylist of Descriptors by replacing old tempdescriptors with new
+     * tempdescriptors when there is a match */
+    public PrefetchPair replaceTemp(TempDescriptor td, TempDescriptor[] newtd) {
+       PrefetchPair npp=(PrefetchPair)clone();
+       ArrayList<Descriptor> desc = (ArrayList<Descriptor>) npp.getDesc();
+       for(ListIterator it = desc.listIterator();it.hasNext();) {
+           Object currdesc = it.next();
+           if(currdesc instanceof IndexDescriptor) {
+               ArrayList<TempDescriptor> tdarray = (ArrayList<TempDescriptor>)((IndexDescriptor)currdesc).tddesc;
+               if (tdarray.contains(td)) {
+                   int index = tdarray.indexOf(td);
+                   tdarray.set(index, newtd[0]);
+                   for(int i=1;i<newtd.length;i++) {
+                       tdarray.add(newtd[i]);
+                   }
                }
-               return false;
+           }
        }
+       return npp;
+    }
+
 }