Edits
[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 }
63
64 void computeLogicOpPolarity(BooleanLogic *This) {
65         Polarity child = computeLogicOpPolarityChildren(This);
66         uint size = This->inputs.getSize();
67         for (uint i = 0; i < size; i++) {
68                 BooleanEdge b = This->inputs.get(i);
69                 computePolarity(b.getBoolean(), b.isNegated() ? negatePolarity(child) : child);
70         }
71 }
72
73 BooleanValue negateBooleanValue(BooleanValue This) {
74         switch (This) {
75         case BV_UNDEFINED:
76         case BV_UNSAT:
77                 return This;
78         case BV_MUSTBETRUE:
79                 return BV_MUSTBEFALSE;
80         case BV_MUSTBEFALSE:
81                 return BV_MUSTBETRUE;
82         default:
83                 ASSERT(0);
84         }
85 }
86
87 Polarity computeLogicOpPolarityChildren(BooleanLogic *This) {
88         switch (This->op) {
89         case SATC_AND: {
90                 return This->polarity;
91         }
92         case SATC_IFF: {
93                 return P_BOTHTRUEFALSE;
94         }
95         default:
96                 ASSERT(0);
97         }
98 }
99
100 Polarity negatePolarity(Polarity This) {
101         switch (This) {
102         case P_UNDEFINED:
103         case P_BOTHTRUEFALSE:
104                 return This;
105         case P_TRUE:
106                 return P_FALSE;
107         case P_FALSE:
108                 return P_TRUE;
109         default:
110                 ASSERT(0);
111         }
112 }