569485c7fc1cbc73b587c10b57e4cd5040109672
[repair.git] / Repair / RepairCompiler / MCC / IR / AbstractInterferes.java
1 package MCC.IR;
2
3 class AbstractInterferes {
4     static public boolean interferes(AbstractRepair ar, DNFPredicate dp) {
5         if ((ar.getDescriptor()!=dp.getPredicate().getDescriptor()) &&
6             ((ar.getDescriptor() instanceof SetDescriptor)||
7              !dp.getPredicate().usesDescriptor((RelationDescriptor)ar.getDescriptor())))
8             return false;
9
10         /* This if handles all the c comparisons in the paper */
11         if (ar.getDescriptor()==dp.getPredicate().getDescriptor()&&
12             (ar.getType()==AbstractRepair.ADDTOSET||ar.getType()==AbstractRepair.ADDTORELATION)&&
13             (ar.getPredicate().getPredicate() instanceof ExprPredicate)&&
14             (dp.getPredicate() instanceof ExprPredicate)&&
15             (dp.getPredicate().inverted()==ar.getPredicate().getPredicate().inverted())&&
16             (((ExprPredicate)dp.getPredicate()).getType()==ExprPredicate.SIZE)) {
17             boolean neg1=ar.getPredicate().isNegated();
18             Opcode op1=((ExprPredicate)ar.getPredicate().getPredicate()).getOp();
19             int size1=((ExprPredicate)ar.getPredicate().getPredicate()).leftsize();
20             boolean neg2=dp.isNegated();
21             Opcode op2=((ExprPredicate)dp.getPredicate()).getOp();
22             int size2=((ExprPredicate)dp.getPredicate()).leftsize();
23             if ((!neg1&&((op1==Opcode.EQ)||(op1==Opcode.NE)||(op1==Opcode.GT)||op1==Opcode.GE))||
24                 (neg1&&((op1==Opcode.EQ)||(op1==Opcode.NE)||(op1==Opcode.LT)||op1==Opcode.LE))) {
25                 int size1a=0;
26                 if (!neg1) {
27                     if((op1==Opcode.EQ)||(op1==Opcode.GE))
28                         size1a=size1;
29                     if((op1==Opcode.GT)||(op1==Opcode.NE))
30                         size1a=size1+1;
31                 }
32                 if (neg1) {
33                     if((op1==Opcode.EQ)||(op1==Opcode.LE))
34                         size1a=size1+1;
35                     if((op1==Opcode.LT)||(op1==Opcode.NE))
36                         size1a=size1;
37                 }
38                 if ((!neg2&&(op2==Opcode.EQ)&&(size1a==size2))||
39                     (neg2&&(op2==Opcode.EQ)&&(size1a!=size2))||
40                     (!neg2&&(op2==Opcode.NE)&&(size1a!=size2))||
41                     (neg2&&(op2==Opcode.NE)&&(size1a==size2))||
42                     (!neg2&&(op2==Opcode.GE))||
43                     (!neg2&&(op2==Opcode.GT))||
44                     (neg2&&(op2==Opcode.LE))||
45                     (neg2&&(op2==Opcode.LT))||
46                     (neg2&&(op2==Opcode.GE)&&(size1a<size2))||
47                     (neg2&&(op2==Opcode.GT)&&(size1a<=size2))||
48                     (!neg2&&(op2==Opcode.LE)&&(size1a<=size2))||
49                     (!neg2&&(op2==Opcode.LT)&&(size1a<size2)))
50                     return false;
51             } 
52         }
53         /* This if handles all the c comparisons in the paper */
54         if (ar.getDescriptor()==dp.getPredicate().getDescriptor()&&
55             (ar.getType()==AbstractRepair.REMOVEFROMSET||ar.getType()==AbstractRepair.REMOVEFROMRELATION)&&
56             (ar.getPredicate().getPredicate() instanceof ExprPredicate)&&
57             (dp.getPredicate() instanceof ExprPredicate)&&
58             (dp.getPredicate().inverted()==ar.getPredicate().getPredicate().inverted())&&
59             (((ExprPredicate)dp.getPredicate()).getType()==ExprPredicate.SIZE)) {
60             boolean neg1=ar.getPredicate().isNegated();
61             Opcode op1=((ExprPredicate)ar.getPredicate().getPredicate()).getOp();
62             int size1=((ExprPredicate)ar.getPredicate().getPredicate()).leftsize();
63             boolean neg2=dp.isNegated();
64             Opcode op2=((ExprPredicate)dp.getPredicate()).getOp();
65             int size2=((ExprPredicate)dp.getPredicate()).leftsize();
66             if ((neg1&&((op1==Opcode.EQ)||(op1==Opcode.LT)||op1==Opcode.LE)||(op1==Opcode.NE))||
67                 (!neg1&&((op1==Opcode.EQ)||(op1==Opcode.GT)||op1==Opcode.GE)||(op1==Opcode.NE))) {
68                 int size1a=0;
69                 if (neg1) {
70                     if((op1==Opcode.EQ)||(op1==Opcode.GE))
71                         size1a=size1-1;
72                     if((op1==Opcode.GT)||(op1==Opcode.NE))
73                         size1a=size1;
74                 }
75                 if (!neg1) {
76                     if((op1==Opcode.EQ)||(op1==Opcode.LE))
77                         size1a=size1;
78                     if((op1==Opcode.LT)||(op1==Opcode.NE))
79                         size1a=size1-1;
80                 }
81                 if ((!neg2&&(op2==Opcode.EQ)&&(size1a==size2))||
82                     (neg2&&(op2==Opcode.EQ)&&(size1a!=size2))||
83                     (!neg2&&(op2==Opcode.NE)&&(size1a!=size2))||
84                     (neg2&&(op2==Opcode.NE)&&(size1a==size2))||
85                     (neg2&&(op2==Opcode.GE))||
86                     (neg2&&(op2==Opcode.GT))||
87                     (!neg2&&(op2==Opcode.LE))||
88                     (!neg2&&(op2==Opcode.LT))||
89                     (!neg2&&(op2==Opcode.GE)&&(size1a>=size2))||
90                     (!neg2&&(op2==Opcode.GT)&&(size1a>size2))||
91                     (neg2&&(op2==Opcode.LE)&&(size1a>size2))||
92                     (neg2&&(op2==Opcode.LT)&&(size1a>=size2)))
93                     return false;
94             } 
95         }
96         return true;
97     }
98
99     static public boolean interferes(Descriptor des, boolean satisfy, DNFPredicate dp) {
100         if ((des!=dp.getPredicate().getDescriptor()) &&
101             ((des instanceof SetDescriptor)||
102              !dp.getPredicate().usesDescriptor((RelationDescriptor)des)))
103             return false;
104
105         /* This if handles all the c comparisons in the paper */
106         if (des==dp.getPredicate().getDescriptor()&&
107             (satisfy)&&
108             (dp.getPredicate() instanceof ExprPredicate)&&
109             (((ExprPredicate)dp.getPredicate()).getType()==ExprPredicate.SIZE)) {
110             boolean neg2=dp.isNegated();
111             Opcode op2=((ExprPredicate)dp.getPredicate()).getOp();
112             int size2=((ExprPredicate)dp.getPredicate()).leftsize();
113             {
114                 if ((!neg2&&(op2==Opcode.GE))||
115                     (!neg2&&(op2==Opcode.GT))||
116                     (neg2&&(op2==Opcode.LE))||
117                     (neg2&&(op2==Opcode.LT)))
118                     return false;
119             }
120         }
121         /* This if handles all the c comparisons in the paper */
122         if (des==dp.getPredicate().getDescriptor()&&
123             (!satisfy)&&
124             (dp.getPredicate() instanceof ExprPredicate)&&
125             (((ExprPredicate)dp.getPredicate()).getType()==ExprPredicate.SIZE)) {
126             boolean neg2=dp.isNegated();
127             Opcode op2=((ExprPredicate)dp.getPredicate()).getOp();
128             int size2=((ExprPredicate)dp.getPredicate()).leftsize();
129             {
130                 if ((neg2&&(op2==Opcode.GE))||
131                     (neg2&&(op2==Opcode.GT))||
132                     (!neg2&&(op2==Opcode.LE))||
133                     (!neg2&&(op2==Opcode.LT)))
134                     return false;
135             } 
136         }
137         return true;
138     }
139
140     static public boolean interferes(Descriptor des, boolean satisfy, Rule r, boolean satisfyrule) {
141         for(int i=0;i<r.numQuantifiers();i++) {
142             Quantifier q=r.getQuantifier(i);
143             if (q instanceof RelationQuantifier||q instanceof SetQuantifier) {
144                 if (q.getRequiredDescriptors().contains(des)&&(satisfy==satisfyrule))
145                     return true;
146             } else if (q instanceof ForQuantifier) {
147                 if (q.getRequiredDescriptors().contains(des))
148                     return true;
149             } else throw new Error("Unrecognized Quantifier");
150         }
151         /* Scan DNF form */
152         DNFRule drule=r.getDNFGuardExpr();
153         for(int i=0;i<drule.size();i++) {
154             RuleConjunction rconj=drule.get(i);
155             for(int j=0;j<rconj.size();j++) {
156                 DNFExpr dexpr=rconj.get(j);
157                 Expr expr=dexpr.getExpr();
158                 boolean negated=dexpr.getNegation();
159                 /*
160                   satisfy  negated
161                   Yes      No             Yes
162                   Yes      Yes            No
163                   No       No             No
164                   No       Yes            Yes
165                 */
166                 boolean satisfiesrule=(satisfy^negated);/*XOR of these */
167                 if (satisfiesrule==satisfyrule) {
168                     /* Effect is the one being tested for */
169                     /* Only expr's to be concerned with are TupleOfExpr and
170                        ElementOfExpr */
171                     if (expr.getRequiredDescriptors().contains(des)) {
172                         if (((expr instanceof ElementOfExpr)||
173                             (expr instanceof TupleOfExpr))&&
174                             (expr.getRequiredDescriptors().size()==1))
175                             return true;
176                         else
177                             throw new Error("Unrecognized EXPR");
178                     }
179                 }
180             }
181         }
182         return false;
183     }
184 }