0588c0f8c24189ac52ab7ca16693b41bf1074b96
[repair.git] / Repair / RepairCompiler / MCC / IR / ExactSize.java
1 package MCC.IR;
2 import java.util.*;
3 import MCC.State;
4
5
6 class ExactSize {
7     State state;
8     private Hashtable sizemap;
9     private Hashtable constraintmap;
10
11     public ExactSize(State state) {
12         this.state=state;
13         this.sizemap=new Hashtable();
14         this.constraintmap=new Hashtable();
15         computesizes();
16     }
17     
18     public int getsize(SetDescriptor sd) {
19         SizeObject so=new SizeObject(sd);
20         if (sizemap.containsKey(so))
21             return ((Integer)sizemap.get(so)).intValue();
22         else
23             return -1;
24     }
25     public Constraint getConstraint(SetDescriptor sd) {
26         SizeObject so=new SizeObject(sd);
27         return (Constraint)constraintmap.get(so);
28     }
29
30     private void computesizes() {
31         for(Iterator it=state.stSets.descriptors();it.hasNext();) {
32             SetDescriptor sd=(SetDescriptor)it.next();
33             for(int i=0;i<state.vConstraints.size();i++) {
34                 Constraint c=(Constraint)state.vConstraints.get(i);
35                 if (c.numQuantifiers()!=0)
36                     continue;
37                 DNFConstraint dconst=c.dnfconstraint;
38                 int oldsize=-1;
39                 boolean matches=true;
40                 for(int j=0;j<dconst.size();j++) {
41                     Conjunction conj=dconst.get(j);
42                     boolean goodmatch=false;
43                     for(int k=0;k<conj.size();k++) {
44                         DNFPredicate dpred=conj.get(k);
45                         if (!dpred.isNegated()) {
46                             Predicate p=dpred.getPredicate();
47                             if (p instanceof ExprPredicate) {
48                                 ExprPredicate ep=(ExprPredicate)p;
49                                 if (ep.getType()==ExprPredicate.SIZE&&
50                                     ep.getOp()==Opcode.EQ&&
51                                     ep.getDescriptor()==sd&&
52                                     ep.isRightInt()) {
53                                     if (j==0) {
54                                         oldsize=ep.rightSize();
55                                         goodmatch=true;
56                                         break;
57                                     } else {
58                                         if (oldsize==ep.rightSize()) {
59                                             goodmatch=true;
60                                             break;
61                                         }
62                                     }
63                                 }
64                             }
65                         }
66                     }
67                     if (!goodmatch) {
68                         matches=false;
69                         break; //this constraint won't work
70                     }
71                 }
72                 if (matches) {
73                     System.out.println("Set "+sd.toString()+" has size "+oldsize);
74                     SizeObject so=new SizeObject(sd);
75                     sizemap.put(so,new Integer(oldsize));
76                     constraintmap.put(so,c);
77                 }
78             }
79         }
80     }
81 }