e8ea646fc66368a8461b935cec2a5bead4c242da
[satune.git] / src / csolver.cc
1 #include "csolver.h"
2 #include "set.h"
3 #include "mutableset.h"
4 #include "element.h"
5 #include "boolean.h"
6 #include "predicate.h"
7 #include "order.h"
8 #include "table.h"
9 #include "function.h"
10 #include "satencoder.h"
11 #include "sattranslator.h"
12 #include "tunable.h"
13 #include "orderencoder.h"
14 #include "polarityassignment.h"
15
16 CSolver::CSolver() : unsat(false) {
17         constraints = allocDefHashSetBoolean();
18         tuner = allocTuner();
19         satEncoder = allocSATEncoder(this);
20 }
21
22 /** This function tears down the solver and the entire AST */
23
24 CSolver::~CSolver() {
25         deleteHashSetBoolean(constraints);
26
27         uint size = allBooleans.getSize();
28         for (uint i = 0; i < size; i++) {
29                 delete allBooleans.get(i);
30         }
31
32         size = allSets.getSize();
33         for (uint i = 0; i < size; i++) {
34                 delete allSets.get(i);
35         }
36
37         size = allElements.getSize();
38         for (uint i = 0; i < size; i++) {
39                 delete allElements.get(i);
40         }
41
42         size = allTables.getSize();
43         for (uint i = 0; i < size; i++) {
44                 delete allTables.get(i);
45         }
46
47         size = allPredicates.getSize();
48         for (uint i = 0; i < size; i++) {
49                 delete allPredicates.get(i);
50         }
51
52         size = allOrders.getSize();
53         for (uint i = 0; i < size; i++) {
54                 delete allOrders.get(i);
55         }
56
57         size = allFunctions.getSize();
58         for (uint i = 0; i < size; i++) {
59                 delete allFunctions.get(i);
60         }
61
62         deleteSATEncoder(satEncoder);
63         deleteTuner(tuner);
64 }
65
66 Set *CSolver::createSet(VarType type, uint64_t *elements, uint numelements) {
67         Set *set = new Set(type, elements, numelements);
68         allSets.push(set);
69         return set;
70 }
71
72 Set *CSolver::createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange) {
73         Set *set = new Set(type, lowrange, highrange);
74         allSets.push(set);
75         return set;
76 }
77
78 MutableSet *CSolver::createMutableSet(VarType type) {
79         MutableSet *set = new MutableSet(type);
80         allSets.push(set);
81         return set;
82 }
83
84 void CSolver::addItem(MutableSet *set, uint64_t element) {
85         set->addElementMSet(element);
86 }
87
88 uint64_t CSolver::createUniqueItem(MutableSet *set) {
89         uint64_t element = set->low++;
90         set->addElementMSet(element);
91         return element;
92 }
93
94 Element *CSolver::getElementVar(Set *set) {
95         Element *element = new ElementSet(set);
96         allElements.push(element);
97         return element;
98 }
99
100 Element *CSolver::getElementConst(VarType type, uint64_t value) {
101         Element *element = new ElementConst(value, type);
102         allElements.push(element);
103         return element;
104 }
105
106 Boolean *CSolver::getBooleanVar(VarType type) {
107         Boolean *boolean = new BooleanVar(type);
108         allBooleans.push(boolean);
109         return boolean;
110 }
111
112 Function *CSolver::createFunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range,OverFlowBehavior overflowbehavior) {
113         Function *function = new FunctionOperator(op, domain, numDomain, range, overflowbehavior);
114         allFunctions.push(function);
115         return function;
116 }
117
118 Predicate *CSolver::createPredicateOperator(CompOp op, Set **domain, uint numDomain) {
119         Predicate *predicate = new PredicateOperator(op, domain,numDomain);
120         allPredicates.push(predicate);
121         return predicate;
122 }
123
124 Predicate *CSolver::createPredicateTable(Table *table, UndefinedBehavior behavior) {
125         Predicate *predicate = new PredicateTable(table, behavior);
126         allPredicates.push(predicate);
127         return predicate;
128 }
129
130 Table *CSolver::createTable(Set **domains, uint numDomain, Set *range) {
131         Table *table = new Table(domains,numDomain,range);
132         allTables.push(table);
133         return table;
134 }
135
136 Table *CSolver::createTableForPredicate(Set **domains, uint numDomain) {
137         return createTable(domains, numDomain, NULL);
138 }
139
140 void CSolver::addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result) {
141         table->addNewTableEntry(inputs, inputSize, result);
142 }
143
144 Function *CSolver::completeTable(Table *table, UndefinedBehavior behavior) {
145         Function *function = new FunctionTable(table, behavior);
146         allFunctions.push(function);
147         return function;
148 }
149
150 Element *CSolver::applyFunction(Function *function, Element **array, uint numArrays, Boolean *overflowstatus) {
151         Element *element = new ElementFunction(function,array,numArrays,overflowstatus);
152         allElements.push(element);
153         return element;
154 }
155
156 Boolean *CSolver::applyPredicate(Predicate *predicate, Element **inputs, uint numInputs) {
157         return applyPredicateTable(predicate, inputs, numInputs, NULL);
158 }
159
160 Boolean *CSolver::applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, Boolean *undefinedStatus) {
161         Boolean *boolean = new BooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
162         allBooleans.push(boolean);
163         return boolean;
164 }
165
166 Boolean *CSolver::applyLogicalOperation(LogicOp op, Boolean **array, uint asize) {
167         return new BooleanLogic(this, op, array, asize);
168 }
169
170 void CSolver::addConstraint(Boolean *constraint) {
171         addHashSetBoolean(constraints, constraint);
172 }
173
174 Order *CSolver::createOrder(OrderType type, Set *set) {
175         Order *order = new Order(type, set);
176         allOrders.push(order);
177         return order;
178 }
179
180 Boolean *CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) {
181         Boolean *constraint = new BooleanOrder(order, first, second);
182         allBooleans.push(constraint);
183         return constraint;
184 }
185
186 int CSolver::startEncoding() {
187         naiveEncodingDecision(this);
188         computePolarities(this);
189         orderAnalysis(this);
190         encodeAllSATEncoder(this, satEncoder);
191         int result = solveCNF(satEncoder->cnf);
192         model_print("sat_solver's result:%d\tsolutionSize=%d\n", result, satEncoder->cnf->solver->solutionsize);
193         for (int i = 1; i <= satEncoder->cnf->solver->solutionsize; i++) {
194                 model_print("%d, ", satEncoder->cnf->solver->solution[i]);
195         }
196         model_print("\n");
197         return result;
198 }
199
200 uint64_t CSolver::getElementValue(Element *element) {
201         switch (GETELEMENTTYPE(element)) {
202         case ELEMSET:
203         case ELEMCONST:
204         case ELEMFUNCRETURN:
205                 return getElementValueSATTranslator(this, element);
206         default:
207                 ASSERT(0);
208         }
209         exit(-1);
210 }
211
212 bool CSolver::getBooleanValue(Boolean *boolean) {
213         switch (GETBOOLEANTYPE(boolean)) {
214         case BOOLEANVAR:
215                 return getBooleanVariableValueSATTranslator(this, boolean);
216         default:
217                 ASSERT(0);
218         }
219         exit(-1);
220 }
221
222 HappenedBefore CSolver::getOrderConstraintValue(Order *order, uint64_t first, uint64_t second) {
223         return getOrderConstraintValueSATTranslator(this, order, first, second);
224 }
225