start of new file
[IRC.git] / Robust / src / Analysis / Prefetch / PrefetchPair.java
1 package Analysis.Prefetch;
2 import IR.Flat.*;
3 import java.util.*;
4 import IR.*;
5
6 public class PrefetchPair {
7     public TempDescriptor base;
8     public ArrayList<Descriptor> desc;
9     
10     public PrefetchPair(){
11         base = new TempDescriptor("");
12         desc = new ArrayList<Descriptor>();
13     }
14     
15     public PrefetchPair(TempDescriptor t, Descriptor f) {
16         base = t;
17         desc = new ArrayList<Descriptor>();
18         desc.add(f);
19     }
20
21     public PrefetchPair(TempDescriptor t, ArrayList<Descriptor> descriptor) {
22         base = t;
23         desc = new ArrayList<Descriptor>();
24         desc.addAll(descriptor);
25     }
26
27     public TempDescriptor getBase() {
28         return base;
29     }
30     
31     public Descriptor getDescAt(int index) {
32         return desc.get(index);
33     }
34     
35     public ArrayList<Descriptor> getDesc() {
36         return desc;
37     }
38     
39     public FieldDescriptor getFieldDesc(int index) {
40         return (FieldDescriptor) desc.get(index);
41     }
42     
43     public IndexDescriptor getIndexDesc(int index) {
44         return (IndexDescriptor) desc.get(index);
45     }
46     
47     public int hashCode() {
48         int hashcode = base.toString().hashCode();
49         if(desc != null) {
50             hashcode^=desc.hashCode();
51         }
52         return hashcode;
53     }
54     
55     public String toString() {
56         String label= getBase().toString();
57         if(getDesc() == null)
58             return label;
59         ListIterator it=getDesc().listIterator();
60         for(;it.hasNext();) {
61             Object o = it.next();
62             if(o instanceof FieldDescriptor) {
63                 FieldDescriptor fd = (FieldDescriptor) o;
64                 label+="."+ fd.toString();
65             } else { 
66                 IndexDescriptor id = (IndexDescriptor) o;
67                 label+= id.toString();
68             }
69         }
70         return label;
71     }
72     
73     public boolean equals(Object o) {
74         if(o instanceof PrefetchPair) {
75             PrefetchPair pp = (PrefetchPair) o;
76             return base == pp.base && desc.equals(pp.desc);
77         }
78         return false;
79     }
80     
81     public Object clone() {
82         PrefetchPair newpp = new PrefetchPair();
83         newpp.base = this.base;
84         for(int i = 0; i < this.desc.size(); i++) {
85             Object o = desc.get(i);
86             if(o instanceof FieldDescriptor) {
87                 newpp.desc.add((FieldDescriptor) o);
88             } else {
89                 ArrayList<TempDescriptor> td = new ArrayList<TempDescriptor>();
90                 for(int j = 0; j < ((IndexDescriptor)o).tddesc.size(); j++) {
91                     td.add(((IndexDescriptor)o).getTempDescAt(j));
92                 }
93                 IndexDescriptor idesc = new IndexDescriptor();
94                 idesc.tddesc = td;
95                 idesc.offset = ((IndexDescriptor)o).offset;
96                 newpp.desc.add(idesc);
97             }
98         }
99         return newpp;
100     }
101
102     /** This function returns true if a tempdescriptor object is found in the array of descriptors
103      *  for a given prefetch pair else returns false*/
104     public boolean containsTemp(TempDescriptor td) {
105         ArrayList<Descriptor> desc = (ArrayList<Descriptor>) getDesc();
106         for(ListIterator it = desc.listIterator();it.hasNext();) {
107             Object o = it.next();
108             if(o instanceof IndexDescriptor) {
109                 ArrayList<TempDescriptor> tdarray = (ArrayList<TempDescriptor>)((IndexDescriptor)o).tddesc;
110                 if(tdarray.contains(td)) {
111                     return true;
112                 }
113             }
114         }
115         return false;
116     }
117
118     /** This function creates a new Arraylist of Descriptors by replacing old tempdescriptors with new
119      * tempdescriptors when there is a match */
120     public PrefetchPair replaceTemp(TempDescriptor td, TempDescriptor[] newtd) {
121         PrefetchPair npp=(PrefetchPair)clone();
122         ArrayList<Descriptor> desc = (ArrayList<Descriptor>) npp.getDesc();
123         for(ListIterator it = desc.listIterator();it.hasNext();) {
124             Object currdesc = it.next();
125             if(currdesc instanceof IndexDescriptor) {
126                 ArrayList<TempDescriptor> tdarray = (ArrayList<TempDescriptor>)((IndexDescriptor)currdesc).tddesc;
127                 if (tdarray.contains(td)) {
128                     int index = tdarray.indexOf(td);
129                     tdarray.set(index, newtd[0]);
130                     for(int i=1;i<newtd.length;i++) {
131                         tdarray.add(newtd[i]);
132                     }
133                 }
134             }
135         }
136         return npp;
137     }
138
139 }