#include "order.h"
#include "table.h"
#include "function.h"
-
-CSolver * allocCSolver() {
- CSolver * tmp=(CSolver *) ourmalloc(sizeof(CSolver));
- tmp->constraints=allocDefVectorBoolean();
- tmp->allBooleans=allocDefVectorBoolean();
- tmp->allSets=allocDefVectorSet();
- tmp->allElements=allocDefVectorElement();
- tmp->allPredicates = allocDefVectorPredicate();
- tmp->allTables = allocDefVectorTable();
- tmp->allOrders = allocDefVectorOrder();
- tmp->allFunctions = allocDefVectorFunction();
- return tmp;
+#include "satencoder.h"
+#include "sattranslator.h"
+#include "tunable.h"
+#include "orderencoder.h"
+#include "polarityassignment.h"
+
+CSolver *allocCSolver() {
+ CSolver *This = (CSolver *) ourmalloc(sizeof(CSolver));
+ This->unsat = false;
+ This->constraints = allocDefHashSetBoolean();
+ This->allBooleans = allocDefVectorBoolean();
+ This->allSets = allocDefVectorSet();
+ This->allElements = allocDefVectorElement();
+ This->allPredicates = allocDefVectorPredicate();
+ This->allTables = allocDefVectorTable();
+ This->allOrders = allocDefVectorOrder();
+ This->allFunctions = allocDefVectorFunction();
+ This->tuner = allocTuner();
+ This->satEncoder = allocSATEncoder(This);
+ return This;
}
/** This function tears down the solver and the entire AST */
void deleteSolver(CSolver *This) {
- deleteVectorBoolean(This->constraints);
+ deleteHashSetBoolean(This->constraints);
- uint size=getSizeVectorBoolean(This->allBooleans);
- for(uint i=0;i<size;i++) {
+ uint size = getSizeVectorBoolean(This->allBooleans);
+ for (uint i = 0; i < size; i++) {
deleteBoolean(getVectorBoolean(This->allBooleans, i));
}
-
deleteVectorBoolean(This->allBooleans);
- size=getSizeVectorSet(This->allSets);
- for(uint i=0;i<size;i++) {
+ size = getSizeVectorSet(This->allSets);
+ for (uint i = 0; i < size; i++) {
deleteSet(getVectorSet(This->allSets, i));
}
-
deleteVectorSet(This->allSets);
- size=getSizeVectorElement(This->allElements);
- for(uint i=0;i<size;i++) {
+ size = getSizeVectorElement(This->allElements);
+ for (uint i = 0; i < size; i++) {
deleteElement(getVectorElement(This->allElements, i));
}
- size=getSizeVectorTable(This->allTables);
- for(uint i=0;i<size;i++) {
+ deleteVectorElement(This->allElements);
+
+ size = getSizeVectorTable(This->allTables);
+ for (uint i = 0; i < size; i++) {
deleteTable(getVectorTable(This->allTables, i));
}
- size=getSizeVectorPredicate(This->allPredicates);
- for(uint i=0;i<size;i++) {
+ deleteVectorTable(This->allTables);
+
+ size = getSizeVectorPredicate(This->allPredicates);
+ for (uint i = 0; i < size; i++) {
deletePredicate(getVectorPredicate(This->allPredicates, i));
}
- size=getSizeVectorOrder(This->allOrders);
- for(uint i=0;i<size;i++) {
+ deleteVectorPredicate(This->allPredicates);
+
+ size = getSizeVectorOrder(This->allOrders);
+ for (uint i = 0; i < size; i++) {
deleteOrder(getVectorOrder(This->allOrders, i));
}
deleteVectorOrder(This->allOrders);
- size=getSizeVectorFunction(This->allFunctions);
- for(uint i=0;i<size;i++) {
+
+ size = getSizeVectorFunction(This->allFunctions);
+ for (uint i = 0; i < size; i++) {
deleteFunction(getVectorFunction(This->allFunctions, i));
}
- deleteVectorOrder(This->allFunctions);
+ deleteVectorFunction(This->allFunctions);
+ deleteSATEncoder(This->satEncoder);
+ deleteTuner(This->tuner);
ourfree(This);
}
-Set * createSet(CSolver * This, VarType type, uint64_t * elements, uint numelements) {
- Set * set=allocSet(type, elements, numelements);
+Set *createSet(CSolver *This, VarType type, uint64_t *elements, uint numelements) {
+ Set *set = allocSet(type, elements, numelements);
pushVectorSet(This->allSets, set);
return set;
}
-Set * createRangeSet(CSolver * This, VarType type, uint64_t lowrange, uint64_t highrange) {
- Set * set=allocSetRange(type, lowrange, highrange);
+Set *createRangeSet(CSolver *This, VarType type, uint64_t lowrange, uint64_t highrange) {
+ Set *set = allocSetRange(type, lowrange, highrange);
pushVectorSet(This->allSets, set);
return set;
}
-MutableSet * createMutableSet(CSolver * This, VarType type) {
- MutableSet * set=allocMutableSet(type);
+MutableSet *createMutableSet(CSolver *This, VarType type) {
+ MutableSet *set = allocMutableSet(type);
pushVectorSet(This->allSets, set);
return set;
}
-void addItem(CSolver *solver, MutableSet * set, uint64_t element) {
+void addItem(CSolver *This, MutableSet *set, uint64_t element) {
addElementMSet(set, element);
}
-uint64_t createUniqueItem(CSolver *solver, MutableSet * set) {
- uint64_t element=set->low++;
+uint64_t createUniqueItem(CSolver *This, MutableSet *set) {
+ uint64_t element = set->low++;
addElementMSet(set, element);
return element;
}
-Element * getElementVar(CSolver *This, Set * set) {
- Element * element=allocElement(set);
+Element *getElementVar(CSolver *This, Set *set) {
+ Element *element = allocElementSet(set);
pushVectorElement(This->allElements, element);
return element;
}
-Boolean * getBooleanVar(CSolver *solver, VarType type) {
- Boolean* boolean= allocBoolean(type);
- pushVectorBoolean(solver->allBooleans, boolean);
+Element *getElementConst(CSolver *This, VarType type, uint64_t value) {
+ Element *element = allocElementConst(value, type);
+ pushVectorElement(This->allElements, element);
+ return element;
+}
+
+Boolean *getBooleanVar(CSolver *This, VarType type) {
+ Boolean *boolean = allocBooleanVar(type);
+ pushVectorBoolean(This->allBooleans, boolean);
return boolean;
}
-Function * createFunctionOperator(CSolver *solver, ArithOp op, Set ** domain, uint numDomain, Set * range,OverFlowBehavior overflowbehavior) {
- Function* function = allocFunctionOperator(op, domain, numDomain, range, overflowbehavior);
- pushVectorFunction(solver->allFunctions, function);
- return function;
+Function *createFunctionOperator(CSolver *This, ArithOp op, Set **domain, uint numDomain, Set *range,OverFlowBehavior overflowbehavior) {
+ Function *function = allocFunctionOperator(op, domain, numDomain, range, overflowbehavior);
+ pushVectorFunction(This->allFunctions, function);
+ return function;
}
-Predicate * createPredicateOperator(CSolver *solver, CompOp op, Set ** domain, uint numDomain) {
- Predicate* predicate= allocPredicate(op, domain,numDomain);
- pushVectorPredicate(solver->allPredicates, predicate);
+Predicate *createPredicateOperator(CSolver *This, CompOp op, Set **domain, uint numDomain) {
+ Predicate *predicate = allocPredicateOperator(op, domain,numDomain);
+ pushVectorPredicate(This->allPredicates, predicate);
return predicate;
}
-Table * createTable(CSolver *solver, Set **domains, uint numDomain, Set * range) {
- Table* table= allocTable(domains,numDomain,range);
- pushVectorTable(solver->allTables, table);
+Predicate *createPredicateTable(CSolver *This, Table *table, UndefinedBehavior behavior) {
+ Predicate *predicate = allocPredicateTable(table, behavior);
+ pushVectorPredicate(This->allPredicates, predicate);
+ return predicate;
+}
+
+Table *createTable(CSolver *This, Set **domains, uint numDomain, Set *range) {
+ Table *table = allocTable(domains,numDomain,range);
+ pushVectorTable(This->allTables, table);
return table;
}
-void addTableEntry(CSolver *solver, Table* table, uint64_t* inputs, uint inputSize, uint64_t result) {
- addNewTableEntry(table,inputs, inputSize,result);
+Table *createTableForPredicate(CSolver *solver, Set **domains, uint numDomain) {
+ return createTable(solver, domains, numDomain, NULL);
+}
+
+void addTableEntry(CSolver *This, Table *table, uint64_t *inputs, uint inputSize, uint64_t result) {
+ addNewTableEntry(table,inputs, inputSize,result);
}
-Function * completeTable(CSolver *solver, Table * table) {
- Function* function = allocFunctionTable(table);
- pushVectorFunction(solver->allFunctions,function);
- return function;
+Function *completeTable(CSolver *This, Table *table, UndefinedBehavior behavior) {
+ Function *function = allocFunctionTable(table, behavior);
+ pushVectorFunction(This->allFunctions,function);
+ return function;
}
-Element * applyFunction(CSolver *solver, Function * function, Element ** array, uint numArrays, Boolean * overflowstatus) {
- return NULL;
+Element *applyFunction(CSolver *This, Function *function, Element **array, uint numArrays, Boolean *overflowstatus) {
+ Element *element = allocElementFunction(function,array,numArrays,overflowstatus);
+ pushVectorElement(This->allElements, element);
+ return element;
}
-Boolean * applyPredicate(CSolver *solver, Predicate * predicate, Element ** inputs, uint numInputs) {
- return NULL;
+Boolean *applyPredicate(CSolver *This, Predicate *predicate, Element **inputs, uint numInputs) {
+ return applyPredicateTable(This, predicate, inputs, numInputs, NULL);
+}
+Boolean *applyPredicateTable(CSolver *This, Predicate *predicate, Element **inputs, uint numInputs, Boolean *undefinedStatus) {
+ Boolean *boolean = allocBooleanPredicate(predicate, inputs, numInputs, undefinedStatus);
+ pushVectorBoolean(This->allBooleans, boolean);
+ return boolean;
}
-Boolean * applyLogicalOperation(CSolver *solver, LogicOp op, Boolean ** array) {
- return NULL;
+Boolean *applyLogicalOperation(CSolver *This, LogicOp op, Boolean **array, uint asize) {
+ return allocBooleanLogicArray(This, op, array, asize);
}
-void addBoolean(CSolver *This, Boolean * constraint) {
- pushVectorBoolean(This->constraints, constraint);
+void addConstraint(CSolver *This, Boolean *constraint) {
+ addHashSetBoolean(This->constraints, constraint);
}
-Order * createOrder(CSolver *solver, OrderType type, Set * set) {
- Order* order = allocOrder(type, set);
- pushVectorOrder(solver->allOrders, order);
+Order *createOrder(CSolver *This, OrderType type, Set *set) {
+ Order *order = allocOrder(type, set);
+ pushVectorOrder(This->allOrders, order);
return order;
}
-Boolean * orderConstraint(CSolver *solver, Order * order, uint64_t first, uint64_t second) {
- Boolean* constraint = allocBooleanOrder(order, first, second);
- pushVectorBoolean(solver->allBooleans,constraint);
+Boolean *orderConstraint(CSolver *This, Order *order, uint64_t first, uint64_t second) {
+ Boolean *constraint = allocBooleanOrder(order, first, second);
+ pushVectorBoolean(This->allBooleans,constraint);
return constraint;
}
+
+int startEncoding(CSolver *This) {
+ naiveEncodingDecision(This);
+ SATEncoder *satEncoder = This->satEncoder;
+ computePolarities(This);
+ orderAnalysis(This);
+ encodeAllSATEncoder(This, satEncoder);
+ int result = solveCNF(satEncoder->cnf);
+ model_print("sat_solver's result:%d\tsolutionSize=%d\n", result, satEncoder->cnf->solver->solutionsize);
+ for (uint i = 1; i <= satEncoder->cnf->solver->solutionsize; i++) {
+ model_print("%d, ", satEncoder->cnf->solver->solution[i]);
+ }
+ model_print("\n");
+ return result;
+}
+
+uint64_t getElementValue(CSolver *This, Element *element) {
+ switch (GETELEMENTTYPE(element)) {
+ case ELEMSET:
+ case ELEMCONST:
+ case ELEMFUNCRETURN:
+ return getElementValueSATTranslator(This, element);
+ default:
+ ASSERT(0);
+ }
+ exit(-1);
+}
+
+bool getBooleanValue( CSolver *This, Boolean *boolean) {
+ switch (GETBOOLEANTYPE(boolean)) {
+ case BOOLEANVAR:
+ return getBooleanVariableValueSATTranslator(This, boolean);
+ default:
+ ASSERT(0);
+ }
+ exit(-1);
+}
+
+HappenedBefore getOrderConstraintValue(CSolver *This, Order *order, uint64_t first, uint64_t second) {
+ return getOrderConstraintValueSATTranslator(This, order, first, second);
+}
+