...
[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     private SetAnalysis setanalysis;
11
12     public ExactSize(State state) {
13         this.state=state;
14         this.sizemap=new Hashtable();
15         this.constraintmap=new Hashtable();
16         this.setanalysis=state.setanalysis;
17         computesizes();
18     }
19     
20     public int getsize(SetDescriptor sd) {
21         SizeObject so=new SizeObject(sd);
22         if (sizemap.containsKey(so))
23             return ((Integer)sizemap.get(so)).intValue();
24         else
25             return -1;
26     }
27     public Constraint getConstraint(SetDescriptor sd) {
28         SizeObject so=new SizeObject(sd);
29         return (Constraint)constraintmap.get(so);
30     }
31
32     public int getsize(RelationDescriptor rd, SetDescriptor sd, boolean inverted) {
33         Iterator it=setanalysis.getSuperset(sd).iterator();
34         while(sd!=null) {
35             SizeObject so=new SizeObject(rd,sd,inverted);
36             if (sizemap.containsKey(so))
37                 return ((Integer)sizemap.get(so)).intValue();
38             sd=null;
39             if (it.hasNext())
40                 sd=(SetDescriptor)it.next();
41         }
42         return -1;
43     }
44
45     public Constraint getConstraint(RelationDescriptor rd, SetDescriptor sd, boolean inverted) {
46         Iterator it=setanalysis.getSuperset(sd).iterator();
47         while(sd!=null) {
48             SizeObject so=new SizeObject(rd,sd,inverted);
49             if (constraintmap.containsKey(so))
50                 return ((Constraint)constraintmap.get(so));
51             sd=null;
52             if (it.hasNext())
53                 sd=(SetDescriptor)it.next();
54         }
55         return null;
56     }
57
58     private void computesizes() {
59         for(Iterator it=state.stSets.descriptors();it.hasNext();) {
60             SetDescriptor sd=(SetDescriptor)it.next();
61             for(int i=0;i<state.vConstraints.size();i++) {
62                 Constraint c=(Constraint)state.vConstraints.get(i);
63                 if (c.numQuantifiers()!=0)
64                     continue;
65                 DNFConstraint dconst=c.dnfconstraint;
66                 int oldsize=-1;
67                 boolean matches=true;
68                 for(int j=0;j<dconst.size();j++) {
69                     Conjunction conj=dconst.get(j);
70                     boolean goodmatch=false;
71                     for(int k=0;k<conj.size();k++) {
72                         DNFPredicate dpred=conj.get(k);
73                         if (!dpred.isNegated()) {
74                             Predicate p=dpred.getPredicate();
75                             if (p instanceof ExprPredicate) {
76                                 ExprPredicate ep=(ExprPredicate)p;
77                                 if (ep.getType()==ExprPredicate.SIZE&&
78                                     ep.getOp()==Opcode.EQ&&
79                                     ep.getDescriptor()==sd&&
80                                     ep.isRightInt()) {
81                                     if (j==0) {
82                                         oldsize=ep.rightSize();
83                                         goodmatch=true;
84                                         break;
85                                     } else {
86                                         if (oldsize==ep.rightSize()) {
87                                             goodmatch=true;
88                                             break;
89                                         }
90                                     }
91                                 }                       
92                             }
93                         }
94                     }
95                     if (!goodmatch) {
96                         matches=false;
97                         break; //this constraint won't work
98                     }
99                 }
100                 if (matches) {
101                     System.out.println("Set "+sd.toString()+" has size "+oldsize);
102                     SizeObject so=new SizeObject(sd);
103                     sizemap.put(so,new Integer(oldsize));
104                     constraintmap.put(so,c);
105                 }
106             }
107         }
108
109         for(Iterator it=state.stRelations.descriptors();it.hasNext();) {
110             RelationDescriptor rd=(RelationDescriptor)it.next();
111             for(int i=0;i<state.vConstraints.size();i++) {
112                 Constraint c=(Constraint)state.vConstraints.get(i);
113                 if (c.numQuantifiers()!=1||!(c.getQuantifier(0) instanceof SetQuantifier))
114                     continue;
115                 SetQuantifier q=(SetQuantifier) c.getQuantifier(0);
116
117                 DNFConstraint dconst=c.dnfconstraint;
118                 int oldsize=-1;
119                 boolean matches=true;
120                 boolean inverted=false;
121                 for(int j=0;j<dconst.size();j++) {
122                     Conjunction conj=dconst.get(j);
123                     boolean goodmatch=false;
124                     for(int k=0;k<conj.size();k++) {
125                         DNFPredicate dpred=conj.get(k);
126                         if (!dpred.isNegated()) {
127                             Predicate p=dpred.getPredicate();
128                             if (p instanceof ExprPredicate) {
129                                 ExprPredicate ep=(ExprPredicate)p;
130                                 if (ep.getType()==ExprPredicate.SIZE&&
131                                     ep.getOp()==Opcode.EQ&&
132                                     ep.getDescriptor()==rd&&
133                                     ep.isRightInt()&&
134                                     ((ImageSetExpr)((SizeofExpr)((OpExpr)ep.expr).left).getSetExpr()).getVar()==q.getVar()) {
135                                     if (j==0) {
136                                         oldsize=ep.rightSize();
137                                         goodmatch=true;
138                                         inverted=ep.inverted();
139                                         break;
140                                     } else {
141                                         if (oldsize==ep.rightSize()&&inverted==ep.inverted()) {
142                                             goodmatch=true;
143                                             break;
144                                         }
145                                     }
146                                 }                       
147                             }
148                         }
149                     }
150                     if (!goodmatch) {
151                         matches=false;
152                         break; //this constraint won't work
153                     }
154                 }
155                 if (matches) {
156                     System.out.println("Set "+rd.toString()+" has size "+oldsize);
157                     SizeObject so=new SizeObject(rd,q.getSet(),inverted);
158                     sizemap.put(so,new Integer(oldsize));
159                     constraintmap.put(so,c);
160                 }
161             }
162         }
163     }
164 }