dc69a575568a101132171a872b54673ccffa02c6
[satune.git] / src / ASTAnalyses / Polarity / polarityassignment.cc
1 #include "polarityassignment.h"
2 #include "csolver.h"
3
4 void computePolarities(CSolver *This) {
5         SetIteratorBooleanEdge *iterator = This->getConstraints();
6         while (iterator->hasNext()) {
7                 BooleanEdge boolean = iterator->next();
8                 Boolean *b = boolean.getBoolean();
9                 bool isNegated = boolean.isNegated();
10                 computePolarity(b, isNegated ? P_FALSE : P_TRUE);
11         }
12         delete iterator;
13 }
14
15 void updateEdgePolarity(BooleanEdge dst, BooleanEdge src) {
16         Boolean *bdst = dst.getBoolean();
17         Boolean *bsrc = src.getBoolean();
18         bool isNegated = dst.isNegated() ^ src.isNegated();
19         Polarity p = isNegated ? negatePolarity(bsrc->polarity) : bsrc->polarity;
20         updatePolarity(bdst, p);
21 }
22
23 void updateEdgePolarity(BooleanEdge dst, Polarity p) {
24         Boolean *bdst = dst.getBoolean();
25         bool isNegated = dst.isNegated();
26         if (isNegated)
27                 p=negatePolarity(p);
28         updatePolarity(bdst, p);
29 }
30
31 bool updatePolarity(Boolean *This, Polarity polarity) {
32         Polarity oldpolarity = This->polarity;
33         Polarity newpolarity = (Polarity) (This->polarity | polarity);
34         This->polarity = newpolarity;
35         return newpolarity != oldpolarity;
36 }
37
38 void updateMustValue(Boolean *This, BooleanValue value) {
39         This->boolVal = (BooleanValue) (This->boolVal | value);
40 }
41
42 void computePolarity(Boolean *This, Polarity polarity) {
43         if (updatePolarity(This, polarity)) {
44                 switch (This->type) {
45                 case BOOLEANVAR:
46                 case ORDERCONST:
47                         return;
48                 case PREDICATEOP:
49                         return computePredicatePolarity((BooleanPredicate *)This);
50                 case LOGICOP:
51                         return computeLogicOpPolarity((BooleanLogic *)This);
52                 default:
53                         ASSERT(0);
54                 }
55         }
56 }
57
58 void computePredicatePolarity(BooleanPredicate *This) {
59         if (This->undefStatus) {
60                 computePolarity(This->undefStatus.getBoolean(), P_BOTHTRUEFALSE);
61         }
62         for(uint i=0; i < This->inputs.getSize(); i++) {
63                 Element * e = This->inputs.get(i);
64                 computeElement(e);
65         }
66 }
67
68 void computeElement(Element *e) {
69         if (e->type == ELEMFUNCRETURN) {
70                 ElementFunction *ef = (ElementFunction *) e;
71
72                 if (ef->overflowstatus) {
73                         computePolarity(ef->overflowstatus.getBoolean(), P_BOTHTRUEFALSE);
74                 }
75
76                 for(uint i=0; i < ef->inputs.getSize(); i++) {
77                         Element * echild = ef->inputs.get(i);
78                         computeElement(echild);
79                 }       
80         }
81 }
82
83 void computeLogicOpPolarity(BooleanLogic *This) {
84         Polarity child = computeLogicOpPolarityChildren(This);
85         uint size = This->inputs.getSize();
86         for (uint i = 0; i < size; i++) {
87                 BooleanEdge b = This->inputs.get(i);
88                 computePolarity(b.getBoolean(), b.isNegated() ? negatePolarity(child) : child);
89         }
90 }
91
92 BooleanValue negateBooleanValue(BooleanValue This) {
93         switch (This) {
94         case BV_UNDEFINED:
95         case BV_UNSAT:
96                 return This;
97         case BV_MUSTBETRUE:
98                 return BV_MUSTBEFALSE;
99         case BV_MUSTBEFALSE:
100                 return BV_MUSTBETRUE;
101         default:
102                 ASSERT(0);
103         }
104 }
105
106 Polarity computeLogicOpPolarityChildren(BooleanLogic *This) {
107         switch (This->op) {
108         case SATC_AND: {
109                 return This->polarity;
110         }
111         case SATC_IFF: {
112                 return P_BOTHTRUEFALSE;
113         }
114         default:
115                 ASSERT(0);
116         }
117 }
118
119 Polarity negatePolarity(Polarity This) {
120         switch (This) {
121         case P_UNDEFINED:
122         case P_BOTHTRUEFALSE:
123                 return This;
124         case P_TRUE:
125                 return P_FALSE;
126         case P_FALSE:
127                 return P_TRUE;
128         default:
129                 ASSERT(0);
130         }
131 }