Rename everything
[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 *allocCSolver() {
17         CSolver *This = (CSolver *) ourmalloc(sizeof(CSolver));
18         This->unsat = false;
19         This->constraints = allocDefHashSetBoolean();
20         This->allBooleans = allocDefVectorBoolean();
21         This->allSets = allocDefVectorSet();
22         This->allElements = allocDefVectorElement();
23         This->allPredicates = allocDefVectorPredicate();
24         This->allTables = allocDefVectorTable();
25         This->allOrders = allocDefVectorOrder();
26         This->allFunctions = allocDefVectorFunction();
27         This->tuner = allocTuner();
28         This->satEncoder = allocSATEncoder(This);
29         return This;
30 }
31
32 /** This function tears down the solver and the entire AST */
33
34 void deleteSolver(CSolver *This) {
35         deleteHashSetBoolean(This->constraints);
36
37         uint size = getSizeVectorBoolean(This->allBooleans);
38         for (uint i = 0; i < size; i++) {
39                 deleteBoolean(getVectorBoolean(This->allBooleans, i));
40         }
41         deleteVectorBoolean(This->allBooleans);
42
43         size = getSizeVectorSet(This->allSets);
44         for (uint i = 0; i < size; i++) {
45                 deleteSet(getVectorSet(This->allSets, i));
46         }
47         deleteVectorSet(This->allSets);
48
49         size = getSizeVectorElement(This->allElements);
50         for (uint i = 0; i < size; i++) {
51                 deleteElement(getVectorElement(This->allElements, i));
52         }
53         deleteVectorElement(This->allElements);
54
55         size = getSizeVectorTable(This->allTables);
56         for (uint i = 0; i < size; i++) {
57                 deleteTable(getVectorTable(This->allTables, i));
58         }
59         deleteVectorTable(This->allTables);
60
61         size = getSizeVectorPredicate(This->allPredicates);
62         for (uint i = 0; i < size; i++) {
63                 deletePredicate(getVectorPredicate(This->allPredicates, i));
64         }
65         deleteVectorPredicate(This->allPredicates);
66
67         size = getSizeVectorOrder(This->allOrders);
68         for (uint i = 0; i < size; i++) {
69                 deleteOrder(getVectorOrder(This->allOrders, i));
70         }
71         deleteVectorOrder(This->allOrders);
72
73         size = getSizeVectorFunction(This->allFunctions);
74         for (uint i = 0; i < size; i++) {
75                 deleteFunction(getVectorFunction(This->allFunctions, i));
76         }
77         deleteVectorFunction(This->allFunctions);
78         deleteSATEncoder(This->satEncoder);
79         deleteTuner(This->tuner);
80         ourfree(This);
81 }
82
83 Set *createSet(CSolver *This, VarType type, uint64_t *elements, uint numelements) {
84         Set *set = allocSet(type, elements, numelements);
85         pushVectorSet(This->allSets, set);
86         return set;
87 }
88
89 Set *createRangeSet(CSolver *This, VarType type, uint64_t lowrange, uint64_t highrange) {
90         Set *set = allocSetRange(type, lowrange, highrange);
91         pushVectorSet(This->allSets, set);
92         return set;
93 }
94
95 MutableSet *createMutableSet(CSolver *This, VarType type) {
96         MutableSet *set = allocMutableSet(type);
97         pushVectorSet(This->allSets, set);
98         return set;
99 }
100
101 void addItem(CSolver *This, MutableSet *set, uint64_t element) {
102         addElementMSet(set, element);
103 }
104
105 uint64_t createUniqueItem(CSolver *This, MutableSet *set) {
106         uint64_t element = set->low++;
107         addElementMSet(set, element);
108         return element;
109 }
110
111 Element *getElementVar(CSolver *This, Set *set) {
112         Element *element = allocElementSet(set);
113         pushVectorElement(This->allElements, element);
114         return element;
115 }
116
117 Element *getElementConst(CSolver *This, VarType type, uint64_t value) {
118         Element *element = allocElementConst(value, type);
119         pushVectorElement(This->allElements, element);
120         return element;
121 }
122
123 Boolean *getBooleanVar(CSolver *This, VarType type) {
124         Boolean *boolean = allocBooleanVar(type);
125         pushVectorBoolean(This->allBooleans, boolean);
126         return boolean;
127 }
128
129 Function *createFunctionOperator(CSolver *This, ArithOp op, Set **domain, uint numDomain, Set *range,OverFlowBehavior overflowbehavior) {
130         Function *function = allocFunctionOperator(op, domain, numDomain, range, overflowbehavior);
131         pushVectorFunction(This->allFunctions, function);
132         return function;
133 }
134
135 Predicate *createPredicateOperator(CSolver *This, CompOp op, Set **domain, uint numDomain) {
136         Predicate *predicate = allocPredicateOperator(op, domain,numDomain);
137         pushVectorPredicate(This->allPredicates, predicate);
138         return predicate;
139 }
140
141 Predicate *createPredicateTable(CSolver *This, Table *table, UndefinedBehavior behavior) {
142         Predicate *predicate = allocPredicateTable(table, behavior);
143         pushVectorPredicate(This->allPredicates, predicate);
144         return predicate;
145 }
146
147 Table *createTable(CSolver *This, Set **domains, uint numDomain, Set *range) {
148         Table *table = allocTable(domains,numDomain,range);
149         pushVectorTable(This->allTables, table);
150         return table;
151 }
152
153 Table *createTableForPredicate(CSolver *solver, Set **domains, uint numDomain) {
154         return createTable(solver, domains, numDomain, NULL);
155 }
156
157 void addTableEntry(CSolver *This, Table *table, uint64_t *inputs, uint inputSize, uint64_t result) {
158         addNewTableEntry(table,inputs, inputSize,result);
159 }
160
161 Function *completeTable(CSolver *This, Table *table, UndefinedBehavior behavior) {
162         Function *function = allocFunctionTable(table, behavior);
163         pushVectorFunction(This->allFunctions,function);
164         return function;
165 }
166
167 Element *applyFunction(CSolver *This, Function *function, Element **array, uint numArrays, Boolean *overflowstatus) {
168         Element *element = allocElementFunction(function,array,numArrays,overflowstatus);
169         pushVectorElement(This->allElements, element);
170         return element;
171 }
172
173 Boolean *applyPredicate(CSolver *This, Predicate *predicate, Element **inputs, uint numInputs) {
174         return applyPredicateTable(This, predicate, inputs, numInputs, NULL);
175 }
176 Boolean *applyPredicateTable(CSolver *This, Predicate *predicate, Element **inputs, uint numInputs, Boolean *undefinedStatus) {
177         Boolean *boolean = allocBooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
178         pushVectorBoolean(This->allBooleans, boolean);
179         return boolean;
180 }
181
182 Boolean *applyLogicalOperation(CSolver *This, LogicOp op, Boolean **array, uint asize) {
183         return allocBooleanLogicArray(This, op, array, asize);
184 }
185
186 void addConstraint(CSolver *This, Boolean *constraint) {
187         addHashSetBoolean(This->constraints, constraint);
188 }
189
190 Order *createOrder(CSolver *This, OrderType type, Set *set) {
191         Order *order = allocOrder(type, set);
192         pushVectorOrder(This->allOrders, order);
193         return order;
194 }
195
196 Boolean *orderConstraint(CSolver *This, Order *order, uint64_t first, uint64_t second) {
197         Boolean *constraint = allocBooleanOrder(order, first, second);
198         pushVectorBoolean(This->allBooleans,constraint);
199         return constraint;
200 }
201
202 int startEncoding(CSolver *This) {
203         naiveEncodingDecision(This);
204         SATEncoder *satEncoder = This->satEncoder;
205         computePolarities(This);
206         orderAnalysis(This);
207         encodeAllSATEncoder(This, satEncoder);
208         int result = solveCNF(satEncoder->cnf);
209         model_print("sat_solver's result:%d\tsolutionSize=%d\n", result, satEncoder->cnf->solver->solutionsize);
210         for (uint i = 1; i <= satEncoder->cnf->solver->solutionsize; i++) {
211                 model_print("%d, ", satEncoder->cnf->solver->solution[i]);
212         }
213         model_print("\n");
214         return result;
215 }
216
217 uint64_t getElementValue(CSolver *This, Element *element) {
218         switch (GETELEMENTTYPE(element)) {
219         case ELEMSET:
220         case ELEMCONST:
221         case ELEMFUNCRETURN:
222                 return getElementValueSATTranslator(This, element);
223         default:
224                 ASSERT(0);
225         }
226         exit(-1);
227 }
228
229 bool getBooleanValue( CSolver *This, Boolean *boolean) {
230         switch (GETBOOLEANTYPE(boolean)) {
231         case BOOLEANVAR:
232                 return getBooleanVariableValueSATTranslator(This, boolean);
233         default:
234                 ASSERT(0);
235         }
236         exit(-1);
237 }
238
239 HappenedBefore getOrderConstraintValue(CSolver *This, Order *order, uint64_t first, uint64_t second) {
240         return getOrderConstraintValueSATTranslator(This, order, first, second);
241 }
242