3 #include "mutableset.h"
10 #include "satencoder.h"
11 #include "sattranslator.h"
13 #include "orderencoder.h"
14 #include "polarityassignment.h"
15 #include "asttransform.h"
17 CSolver::CSolver() : unsat(false) {
19 satEncoder = allocSATEncoder(this);
22 /** This function tears down the solver and the entire AST */
25 uint size = allBooleans.getSize();
26 for (uint i = 0; i < size; i++) {
27 delete allBooleans.get(i);
30 size = allSets.getSize();
31 for (uint i = 0; i < size; i++) {
32 delete allSets.get(i);
35 size = allElements.getSize();
36 for (uint i = 0; i < size; i++) {
37 delete allElements.get(i);
40 size = allTables.getSize();
41 for (uint i = 0; i < size; i++) {
42 delete allTables.get(i);
45 size = allPredicates.getSize();
46 for (uint i = 0; i < size; i++) {
47 delete allPredicates.get(i);
50 size = allOrders.getSize();
51 for (uint i = 0; i < size; i++) {
52 delete allOrders.get(i);
55 size = allFunctions.getSize();
56 for (uint i = 0; i < size; i++) {
57 delete allFunctions.get(i);
60 deleteSATEncoder(satEncoder);
64 Set *CSolver::createSet(VarType type, uint64_t *elements, uint numelements) {
65 Set *set = new Set(type, elements, numelements);
70 Set *CSolver::createRangeSet(VarType type, uint64_t lowrange, uint64_t highrange) {
71 Set *set = new Set(type, lowrange, highrange);
76 MutableSet *CSolver::createMutableSet(VarType type) {
77 MutableSet *set = new MutableSet(type);
82 void CSolver::addItem(MutableSet *set, uint64_t element) {
83 set->addElementMSet(element);
86 uint64_t CSolver::createUniqueItem(MutableSet *set) {
87 uint64_t element = set->low++;
88 set->addElementMSet(element);
92 Element *CSolver::getElementVar(Set *set) {
93 Element *element = new ElementSet(set);
94 allElements.push(element);
98 Element *CSolver::getElementConst(VarType type, uint64_t value) {
99 Element *element = new ElementConst(value, type);
100 allElements.push(element);
104 Boolean *CSolver::getBooleanVar(VarType type) {
105 Boolean *boolean = new BooleanVar(type);
106 allBooleans.push(boolean);
110 Function *CSolver::createFunctionOperator(ArithOp op, Set **domain, uint numDomain, Set *range,OverFlowBehavior overflowbehavior) {
111 Function *function = new FunctionOperator(op, domain, numDomain, range, overflowbehavior);
112 allFunctions.push(function);
116 Predicate *CSolver::createPredicateOperator(CompOp op, Set **domain, uint numDomain) {
117 Predicate *predicate = new PredicateOperator(op, domain,numDomain);
118 allPredicates.push(predicate);
122 Predicate *CSolver::createPredicateTable(Table *table, UndefinedBehavior behavior) {
123 Predicate *predicate = new PredicateTable(table, behavior);
124 allPredicates.push(predicate);
128 Table *CSolver::createTable(Set **domains, uint numDomain, Set *range) {
129 Table *table = new Table(domains,numDomain,range);
130 allTables.push(table);
134 Table *CSolver::createTableForPredicate(Set **domains, uint numDomain) {
135 return createTable(domains, numDomain, NULL);
138 void CSolver::addTableEntry(Table *table, uint64_t *inputs, uint inputSize, uint64_t result) {
139 table->addNewTableEntry(inputs, inputSize, result);
142 Function *CSolver::completeTable(Table *table, UndefinedBehavior behavior) {
143 Function *function = new FunctionTable(table, behavior);
144 allFunctions.push(function);
148 Element *CSolver::applyFunction(Function *function, Element **array, uint numArrays, Boolean *overflowstatus) {
149 Element *element = new ElementFunction(function,array,numArrays,overflowstatus);
150 allElements.push(element);
154 Boolean *CSolver::applyPredicate(Predicate *predicate, Element **inputs, uint numInputs) {
155 return applyPredicateTable(predicate, inputs, numInputs, NULL);
158 Boolean *CSolver::applyPredicateTable(Predicate *predicate, Element **inputs, uint numInputs, Boolean *undefinedStatus) {
159 Boolean *boolean = new BooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
160 allBooleans.push(boolean);
164 Boolean *CSolver::applyLogicalOperation(LogicOp op, Boolean **array, uint asize) {
165 return new BooleanLogic(this, op, array, asize);
168 void CSolver::addConstraint(Boolean *constraint) {
169 constraints.add(constraint);
172 Order *CSolver::createOrder(OrderType type, Set *set) {
173 Order *order = new Order(type, set);
174 allOrders.push(order);
178 Boolean *CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) {
179 Boolean *constraint = new BooleanOrder(order, first, second);
180 allBooleans.push(constraint);
184 int CSolver::startEncoding() {
185 computePolarities(this);
187 naiveEncodingDecision(this);
189 encodeAllSATEncoder(this, satEncoder);
190 int result = solveCNF(satEncoder->cnf);
191 model_print("sat_solver's result:%d\tsolutionSize=%d\n", result, satEncoder->cnf->solver->solutionsize);
192 for (int i = 1; i <= satEncoder->cnf->solver->solutionsize; i++) {
193 model_print("%d, ", satEncoder->cnf->solver->solution[i]);
199 uint64_t CSolver::getElementValue(Element *element) {
200 switch (GETELEMENTTYPE(element)) {
204 return getElementValueSATTranslator(this, element);
211 bool CSolver::getBooleanValue(Boolean *boolean) {
212 switch (GETBOOLEANTYPE(boolean)) {
214 return getBooleanVariableValueSATTranslator(this, boolean);
221 HappenedBefore CSolver::getOrderConstraintValue(Order *order, uint64_t first, uint64_t second) {
222 return getOrderConstraintValueSATTranslator(this, order, first, second);