Fix compilation problems for multicore code
[IRC.git] / Robust / src / IR / Virtual.java
1 package IR;
2 import java.util.*;
3
4 import Analysis.Locality.LocalityBinding;
5 import Analysis.Locality.LocalityAnalysis;
6
7
8 public class Virtual {
9   State state;
10   LocalityAnalysis locality;
11   Hashtable<MethodDescriptor, Integer> methodnumber;
12   Hashtable<ClassDescriptor, Integer> classmethodcount;
13   Hashtable<LocalityBinding, Integer> localitynumber;
14   
15   // for interfaces
16   int if_starts;
17   SymbolTable if_methods;
18
19   public Integer getMethodNumber(MethodDescriptor md) {
20     return methodnumber.get(md);
21   }
22
23   public int getMethodCount(ClassDescriptor md) {
24     return classmethodcount.get(md).intValue();
25   }
26
27   public int getLocalityNumber(LocalityBinding lb) {
28     return localitynumber.get(lb).intValue();
29   }
30
31   public Virtual(State state, LocalityAnalysis locality) {
32     this.state=state;
33     this.locality=locality;
34     this.if_starts = 0;
35     this.if_methods = new SymbolTable();
36     classmethodcount=new Hashtable<ClassDescriptor, Integer>();
37     if (state.DSM||state.SINGLETM)
38       localitynumber=new Hashtable<LocalityBinding, Integer>();
39     else
40       methodnumber=new Hashtable<MethodDescriptor, Integer>();
41     doAnalysis();
42   }
43
44   private void doAnalysis() {
45     Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();
46     while(classit.hasNext()) {
47       ClassDescriptor cd=(ClassDescriptor)classit.next();
48       numberMethodsIF(cd);
49     }
50     classit=state.getClassSymbolTable().getDescriptorsIterator();
51     while(classit.hasNext()) {
52       ClassDescriptor cd=(ClassDescriptor)classit.next();
53       if (state.DSM||state.SINGLETM)
54         numberLocality(cd);
55       else
56         numberMethods(cd);
57     }
58     classit=state.getClassSymbolTable().getDescriptorsIterator();
59     while(classit.hasNext()) {
60       ClassDescriptor cd=(ClassDescriptor)classit.next();
61       if(!cd.isInterface()) {
62         int count = classmethodcount.get(cd).intValue();
63         classmethodcount.put(cd, new Integer(count+this.if_starts));
64       }
65     }
66   }
67
68   private int numberLocality(ClassDescriptor cd) {
69     if (classmethodcount.containsKey(cd))
70       return classmethodcount.get(cd).intValue();
71     ClassDescriptor superdesc=cd.getSuperDesc();
72     int start=0;
73     if (superdesc!=null)
74       start=numberLocality(superdesc);
75
76     if (locality.getClassBindings(cd)!=null)
77       for(Iterator<LocalityBinding> lbit=locality.getClassBindings(cd).iterator(); lbit.hasNext();) {
78         LocalityBinding lb=lbit.next();
79         MethodDescriptor md=lb.getMethod();
80         //Is it a static method or constructor
81         if (md.isStatic()||md.getReturnType()==null)
82           continue;
83
84         if (superdesc!=null) {
85           Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
86           boolean foundmatch=false;
87           for(Iterator matchit=possiblematches.iterator(); matchit.hasNext();) {
88             MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
89             if (md.matches(matchmd)) {
90               Set<LocalityBinding> lbset=locality.getMethodBindings(matchmd);
91               if (lbset!=null)
92                 for(Iterator<LocalityBinding> suplbit=lbset.iterator(); suplbit.hasNext();) {
93                   LocalityBinding suplb=suplbit.next();
94                   if (lb.contextMatches(suplb)) {
95                     foundmatch=true;
96                     localitynumber.put(lb, localitynumber.get(suplb));
97                     break;
98                   }
99                 }
100               break;
101             }
102           }
103           if (!foundmatch)
104             localitynumber.put(lb, new Integer(start++));
105         } else {
106           localitynumber.put(lb, new Integer(start++));
107         }
108       }
109     classmethodcount.put(cd, new Integer(start));
110     return start;
111   }
112   
113   private int numberMethodsIF(ClassDescriptor cd) {
114     if(!cd.isInterface()) {
115       return 0;
116     }
117     int mnum = 0;
118     if (classmethodcount.containsKey(cd))
119       return classmethodcount.get(cd).intValue();
120     // check the inherited interfaces
121     Iterator it_sifs = cd.getSuperInterfaces();
122     while(it_sifs.hasNext()) {
123       ClassDescriptor superif = (ClassDescriptor)it_sifs.next();
124       mnum += numberMethodsIF(superif);
125     }
126     for(Iterator it=cd.getMethods(); it.hasNext();) {
127       MethodDescriptor md=(MethodDescriptor)it.next();
128       if (md.isStatic()||md.getReturnType()==null)
129         continue;
130       boolean foundmatch = false;
131       if(this.state.genAllMethods) {
132         foundmatch = true;
133       } else {
134         Set vec_md = this.state.getMethod2gen().getSet(md.getSymbol());
135         for(Iterator matchit=vec_md.iterator(); matchit.hasNext();) {
136           MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
137           if (md.matches(matchmd)) {
138             foundmatch=true;
139             break;
140           }
141         }
142       }
143       if(!foundmatch) {
144         continue;
145       }
146       foundmatch=false;
147       // check if there is a matched method that has been assigned method num
148       Set possiblematches_if = if_methods.getSet(md.getSymbol());
149       for(Iterator matchit=possiblematches_if.iterator(); matchit.hasNext();) {
150         MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
151         if (md.matches(matchmd)) {
152           int num=methodnumber.get(matchmd);
153           methodnumber.put(md, new Integer(num));
154           foundmatch=true;
155           break;
156         }
157       }
158       if(!foundmatch) {
159         methodnumber.put(md, new Integer(if_starts++));
160         if_methods.add(md);
161         mnum++;
162       }
163     }
164     classmethodcount.put(cd, new Integer(mnum));
165     return mnum;
166   }
167
168   private int numberMethods(ClassDescriptor cd) {
169     if (classmethodcount.containsKey(cd))
170       return classmethodcount.get(cd).intValue();
171     ClassDescriptor superdesc=cd.getSuperDesc();
172     int start=if_starts;
173     int mnum = 0;
174     if (superdesc!=null) {
175       mnum = numberMethods(superdesc);
176       start += mnum;
177     }
178     for(Iterator it=cd.getMethods(); it.hasNext();) {
179       MethodDescriptor md=(MethodDescriptor)it.next();
180       if (md.isStatic()||md.getReturnType()==null)
181         continue;
182       boolean foundmatch = false;
183       if(this.state.genAllMethods) {
184         foundmatch = true;
185       } else {
186         Set vec_md = this.state.getMethod2gen().getSet(md.getSymbol());
187         for(Iterator matchit=vec_md.iterator(); matchit.hasNext();) {
188           MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
189           if (md.matches(matchmd)) {
190             foundmatch=true;
191             break;
192           }
193         }
194       }
195       if(!foundmatch) {
196         continue;
197       }
198       foundmatch=false;
199       // check if there is a matched method in methods defined in interfaces
200       Set possiblematches_if=if_methods.getSet(md.getSymbol());
201       for(Iterator matchit=possiblematches_if.iterator(); matchit.hasNext();) {
202         MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
203         if (md.matches(matchmd)) {
204           int num = methodnumber.get(matchmd);
205           methodnumber.put(md, new Integer(num));
206           foundmatch=true;
207           break;
208         }
209       }
210       if (!foundmatch && superdesc!=null) {
211         Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
212         for(Iterator matchit=possiblematches.iterator(); matchit.hasNext();) {
213           MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
214           if (md.matches(matchmd)) {
215             int num = methodnumber.get(matchmd);
216             methodnumber.put(md, new Integer(num));
217             foundmatch=true;
218             break;
219           }
220         }
221       }
222       if (!foundmatch) {
223         methodnumber.put(md, new Integer(start++));
224         mnum++;
225       }
226     }
227     classmethodcount.put(cd, new Integer(mnum));
228     return mnum;
229   }
230 }
231