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