Enable interface for mgc version.
[IRC.git] / Robust / src / IR / Virtual.java
1 package IR;
2 import java.util.*;
3 import Analysis.Locality.LocalityBinding;
4 import Analysis.Locality.LocalityAnalysis;
5
6
7 public class Virtual {
8   State state;
9   LocalityAnalysis locality;
10   Hashtable<MethodDescriptor, Integer> methodnumber;
11   Hashtable<ClassDescriptor, Integer> classmethodcount;
12   Hashtable<LocalityBinding, Integer> localitynumber;
13
14   public int getMethodNumber(MethodDescriptor md) {
15     return methodnumber.get(md).intValue();
16   }
17
18   public int getMethodCount(ClassDescriptor md) {
19     return classmethodcount.get(md).intValue();
20   }
21
22   public int getLocalityNumber(LocalityBinding lb) {
23     return localitynumber.get(lb).intValue();
24   }
25
26   public Virtual(State state, LocalityAnalysis locality) {
27     this.state=state;
28     this.locality=locality;
29     classmethodcount=new Hashtable<ClassDescriptor, Integer>();
30     if (state.DSM||state.SINGLETM)
31       localitynumber=new Hashtable<LocalityBinding, Integer>();
32     else
33       methodnumber=new Hashtable<MethodDescriptor, Integer>();
34     doAnalysis();
35   }
36
37   private void doAnalysis() {
38     Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();
39     while(classit.hasNext()) {
40       ClassDescriptor cd=(ClassDescriptor)classit.next();
41       if (state.DSM||state.SINGLETM)
42         numberLocality(cd);
43       else
44         numberMethods(cd);
45     }
46   }
47
48   private int numberLocality(ClassDescriptor cd) {
49     if (classmethodcount.containsKey(cd))
50       return classmethodcount.get(cd).intValue();
51     ClassDescriptor superdesc=cd.getSuperDesc();
52     int start=0;
53     if (superdesc!=null)
54       start=numberLocality(superdesc);
55
56     if (locality.getClassBindings(cd)!=null)
57       for(Iterator<LocalityBinding> lbit=locality.getClassBindings(cd).iterator(); lbit.hasNext();) {
58         LocalityBinding lb=lbit.next();
59         MethodDescriptor md=lb.getMethod();
60         //Is it a static method or constructor
61         if (md.isStatic()||md.getReturnType()==null)
62           continue;
63
64         if (superdesc!=null) {
65           Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
66           boolean foundmatch=false;
67           for(Iterator matchit=possiblematches.iterator(); matchit.hasNext();) {
68             MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
69             if (md.matches(matchmd)) {
70               Set<LocalityBinding> lbset=locality.getMethodBindings(matchmd);
71               if (lbset!=null)
72                 for(Iterator<LocalityBinding> suplbit=lbset.iterator(); suplbit.hasNext();) {
73                   LocalityBinding suplb=suplbit.next();
74                   if (lb.contextMatches(suplb)) {
75                     foundmatch=true;
76                     localitynumber.put(lb, localitynumber.get(suplb));
77                     break;
78                   }
79                 }
80               break;
81             }
82           }
83           if (!foundmatch)
84             localitynumber.put(lb, new Integer(start++));
85         } else {
86           localitynumber.put(lb, new Integer(start++));
87         }
88       }
89     classmethodcount.put(cd, new Integer(start));
90     return start;
91   }
92
93   private int numberMethods(ClassDescriptor cd) {
94     if (classmethodcount.containsKey(cd))
95       return classmethodcount.get(cd).intValue();
96     ClassDescriptor superdesc=cd.getSuperDesc();
97     int start=0;
98     if (superdesc!=null)
99       start=numberMethods(superdesc);
100     if(state.MGC) {
101       // TODO add version for normal Java later
102       // check the inherited interfaces
103       Iterator it_sifs = cd.getSuperInterfaces();
104       while(it_sifs.hasNext()) {
105         ClassDescriptor superif = (ClassDescriptor)it_sifs.next();
106         start += numberMethods(superif); // TODO Can there be duplicated methods from multiple ancestors?
107       }
108     }
109     for(Iterator it=cd.getMethods(); it.hasNext();) {
110       MethodDescriptor md=(MethodDescriptor)it.next();
111       if (md.isStatic()||md.getReturnType()==null)
112         continue;
113       if (superdesc!=null) {
114         Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
115         boolean foundmatch=false;
116         for(Iterator matchit=possiblematches.iterator(); matchit.hasNext();) {
117           MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
118           if (md.matches(matchmd)) {
119             int num=((Integer)methodnumber.get(matchmd)).intValue();
120             methodnumber.put(md, new Integer(num));
121             foundmatch=true;
122             break;
123           }
124         }
125     if(state.MGC) {
126       // TODO add version for normal Java later
127       if(!foundmatch) {
128         // check if there is a matched method in inherited interfaces
129         Iterator it_sifs = cd.getSuperInterfaces();
130         while(it_sifs.hasNext() && !foundmatch) {
131           ClassDescriptor superif = (ClassDescriptor)it_sifs.next();
132           Set possiblematches_if=superif.getMethodTable().getSet(md.getSymbol());
133           for(Iterator matchit=possiblematches_if.iterator(); matchit.hasNext();) {
134             MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
135             if (md.matches(matchmd)) {
136               int num=((Integer)methodnumber.get(matchmd)).intValue();
137               methodnumber.put(md, new Integer(num));
138               foundmatch=true;
139               break;
140             }
141           }
142         }
143       }
144     }
145         if (!foundmatch)
146           methodnumber.put(md, new Integer(start++));
147       } else {
148         methodnumber.put(md, new Integer(start++));
149       }
150     }
151     classmethodcount.put(cd, new Integer(start));
152     return start;
153   }
154 }
155