+bool CSolver::isFalse(BooleanEdge b) {
+ return b.isNegated()?b->isTrue():b->isFalse();
+}
+
+BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge arg1, BooleanEdge arg2) {
+ BooleanEdge array[] = {arg1, arg2};
+ return applyLogicalOperation(op, array, 2);
+}
+
+BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge arg) {
+ BooleanEdge array[] = {arg};
+ return applyLogicalOperation(op, array, 1);
+}
+
+static int ptrcompares(const void *p1, const void *p2) {
+ uintptr_t b1 = *(uintptr_t const *) p1;
+ uintptr_t b2 = *(uintptr_t const *) p2;
+ if (b1 < b2)
+ return -1;
+ else if (b1 == b2)
+ return 0;
+ else
+ return 1;
+}
+
+BooleanEdge CSolver::rewriteLogicalOperation(LogicOp op, BooleanEdge * array, uint asize) {
+ BooleanEdge newarray[asize];
+ memcpy(newarray, array, asize * sizeof(BooleanEdge));
+ for(uint i=0; i < asize; i++) {
+ BooleanEdge b=newarray[i];
+ if (b->type == LOGICOP) {
+ if (((BooleanLogic *) b.getBoolean())->replaced) {
+ newarray[i] = doRewrite(newarray[i]);
+ i--;//Check again
+ }
+ }
+ }
+ return applyLogicalOperation(op, newarray, asize);
+}
+
+BooleanEdge CSolver::applyLogicalOperation(LogicOp op, BooleanEdge *array, uint asize) {
+ BooleanEdge newarray[asize];
+ switch (op) {
+ case SATC_NOT: {
+ return array[0].negate();
+ }
+ case SATC_IFF: {
+ for (uint i = 0; i < 2; i++) {
+ if (array[i]->type == BOOLCONST) {
+ if (array[i]->isTrue()) {
+ return array[1 - i];
+ } else {
+ newarray[0] = array[1 - i];
+ return applyLogicalOperation(SATC_NOT, newarray, 1);
+ }
+ } else if (array[i]->type == LOGICOP) {
+ BooleanLogic *b =(BooleanLogic *)array[i].getBoolean();
+ if (b->replaced) {
+ return rewriteLogicalOperation(op, array, asize);
+ }
+ }
+ }
+ break;
+ }
+ case SATC_OR: {
+ for (uint i =0; i <asize; i++) {
+ newarray[i] = applyLogicalOperation(SATC_NOT, array[i]);
+ }
+ return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_AND, newarray, asize));
+ }
+ case SATC_AND: {
+ uint newindex = 0;
+ for (uint i = 0; i < asize; i++) {
+ BooleanEdge b = array[i];
+ if (b->type == LOGICOP) {
+ if (((BooleanLogic *)b.getBoolean())->replaced)
+ return rewriteLogicalOperation(op, array, asize);
+ }
+ if (b->type == BOOLCONST) {
+ if (b->isTrue())
+ continue;
+ else
+ return boolFalse;
+ } else
+ newarray[newindex++] = b;
+ }
+ if (newindex == 0) {
+ return boolTrue;
+ } else if (newindex == 1) {
+ return newarray[0];
+ } else {
+ bsdqsort(newarray, newindex, sizeof(BooleanEdge), ptrcompares);
+ array = newarray;
+ asize = newindex;
+ }
+ break;
+ }
+ case SATC_XOR: {
+ //handle by translation
+ return applyLogicalOperation(SATC_NOT, applyLogicalOperation(SATC_IFF, array, asize));
+ }
+ case SATC_IMPLIES: {
+ //handle by translation
+ return applyLogicalOperation(SATC_OR, applyLogicalOperation(SATC_NOT, array[0]), array[1]);
+ }
+ }
+
+ ASSERT(asize != 0);
+ Boolean *boolean = new BooleanLogic(this, op, array, asize);
+ Boolean *b = boolMap.get(boolean);
+ if (b == NULL) {
+ boolean->updateParents();
+ boolMap.put(boolean, boolean);
+ allBooleans.push(boolean);
+ return BooleanEdge(boolean);
+ } else {
+ delete boolean;
+ return BooleanEdge(b);
+ }
+}
+
+BooleanEdge CSolver::orderConstraint(Order *order, uint64_t first, uint64_t second) {
+ Boolean *constraint = new BooleanOrder(order, first, second);
+ allBooleans.push(constraint);
+ return BooleanEdge(constraint);
+}
+
+void CSolver::addConstraint(BooleanEdge constraint) {
+ if (isTrue(constraint))
+ return;
+ else if (isFalse(constraint))
+ setUnSAT();
+ else {
+ if (constraint->type == LOGICOP) {
+ BooleanLogic *b=(BooleanLogic *) constraint.getBoolean();
+ if (!constraint.isNegated()) {
+ if (b->op==SATC_AND) {
+ for(uint i=0;i<b->inputs.getSize();i++) {
+ addConstraint(b->inputs.get(i));
+ }
+ return;
+ }
+ }
+ if (b->replaced) {
+ addConstraint(doRewrite(constraint));
+ return;
+ }
+ }
+ constraints.add(constraint);
+ Boolean *ptr=constraint.getBoolean();
+
+ if (ptr->boolVal == BV_UNSAT)
+ setUnSAT();
+
+ replaceBooleanWithTrueNoRemove(constraint);
+ constraint->parents.clear();
+ }