3 #include "mutableset.h"
10 #include "satencoder.h"
11 #include "sattranslator.h"
13 #include "polarityassignment.h"
14 #include "orderdecompose.h"
16 CSolver::CSolver() : unsat(false) {
18 satEncoder = allocSATEncoder(this);
21 /** This function tears down the solver and the entire AST */
24 uint size = allBooleans.getSize();
25 for (uint i = 0; i < size; i++) {
26 delete allBooleans.get(i);
29 size = allSets.getSize();
30 for (uint i = 0; i < size; i++) {
31 delete allSets.get(i);
34 size = allElements.getSize();
35 for (uint i = 0; i < size; i++) {
36 delete allElements.get(i);
39 size = allTables.getSize();
40 for (uint i = 0; i < size; i++) {
41 delete allTables.get(i);
44 size = allPredicates.getSize();
45 for (uint i = 0; i < size; i++) {
46 delete allPredicates.get(i);
49 size = allOrders.getSize();
50 for (uint i = 0; i < size; i++) {
51 delete allOrders.get(i);
54 size = allFunctions.getSize();
55 for (uint i = 0; i < size; i++) {
56 delete allFunctions.get(i);
59 deleteSATEncoder(satEncoder);
63 Set *CSolver::createSet(VarType type, uint64_t *elements, uint numelements) {
64 Set *set = new Set(type, elements, numelements);
69 Set *CSolver::createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange) {
70 Set *set = new Set(type, lowrange, highrange);
75 MutableSet *CSolver::createMutableSet(VarType type) {
76 MutableSet *set = new MutableSet(type);
81 void CSolver::addItem(MutableSet *set, uint64_t element) {
82 set->addElementMSet(element);
85 uint64_t CSolver::createUniqueItem(MutableSet *set) {
86 uint64_t element = set->low++;
87 set->addElementMSet(element);
91 Element *CSolver::getElementVar(Set *set) {
92 Element *element = new ElementSet(set);
93 allElements.push(element);
97 Element *CSolver::getElementConst(VarType type, uint64_t value) {
98 Element *element = new ElementConst(value, type);
99 allElements.push(element);
103 Boolean *CSolver::getBooleanVar(VarType type) {
104 Boolean *boolean = new BooleanVar(type);
105 allBooleans.push(boolean);
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);
115 Predicate *CSolver::createPredicateOperator(CompOp op, Set **domain, uint numDomain) {
116 Predicate *predicate = new PredicateOperator(op, domain,numDomain);
117 allPredicates.push(predicate);
121 Predicate *CSolver::createPredicateTable(Table *table, UndefinedBehavior behavior) {
122 Predicate *predicate = new PredicateTable(table, behavior);
123 allPredicates.push(predicate);
127 Table *CSolver::createTable(Set **domains, uint numDomain, Set *range) {
128 Table *table = new Table(domains,numDomain,range);
129 allTables.push(table);
133 Table *CSolver::createTableForPredicate(Set **domains, uint numDomain) {
134 return createTable(domains, numDomain, NULL);
137 void CSolver::addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result) {
138 table->addNewTableEntry(inputs, inputSize, result);
141 Function *CSolver::completeTable(Table *table, UndefinedBehavior behavior) {
142 Function *function = new FunctionTable(table, behavior);
143 allFunctions.push(function);
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);
153 Boolean *CSolver::applyPredicate(Predicate *predicate, Element **inputs, uint numInputs) {
154 return applyPredicateTable(predicate, inputs, numInputs, NULL);
157 Boolean *CSolver::applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, Boolean *undefinedStatus) {
158 BooleanPredicate *boolean = new BooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
159 allBooleans.push(boolean);
163 Boolean *CSolver::applyLogicalOperation(LogicOp op, Boolean **array, uint asize) {
164 return new BooleanLogic(this, op, array, asize);
167 void CSolver::addConstraint(Boolean *constraint) {
168 constraints.add(constraint);
171 Order *CSolver::createOrder(OrderType type, Set *set) {
172 Order *order = new Order(type, set);
173 allOrders.push(order);
177 Boolean *CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) {
178 Boolean *constraint = new BooleanOrder(order, first, second);
179 allBooleans.push(constraint);
183 int CSolver::startEncoding() {
184 computePolarities(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]);
197 uint64_t CSolver::getElementValue(Element *element) {
198 switch (GETELEMENTTYPE(element)) {
202 return getElementValueSATTranslator(this, element);
209 bool CSolver::getBooleanValue(Boolean *boolean) {
210 switch (GETBOOLEANTYPE(boolean)) {
212 return getBooleanVariableValueSATTranslator(this, boolean);
219 HappenedBefore CSolver::getOrderConstraintValue(Order *order, uint64_t first, uint64_t second) {
220 return getOrderConstraintValueSATTranslator(this, order, first, second);