Skip to content

Commit 5202665

Browse files
committed
Add BVCONST BitBlasting
1 parent 459548c commit 5202665

File tree

5 files changed

+48
-14
lines changed

5 files changed

+48
-14
lines changed

core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ static void _c3_dpll_remove2 (C3 *c3, C3List *cnf, int32_t l) {
7676
if (fnd = c3_list_find (disj, (void*)&l, c3_compare_symbol)) {
7777
int32_t* found = c3_list_get_data (fnd);
7878
debug_log (2, "found: %d\n",*found);
79-
if (l < 0 && *found < 0 || l > 0 && *found > 0) {
79+
if ((l < 0 && *found < 0) || (l > 0 && *found > 0)) {
8080
/* same literal (same sign) */
8181
/* Delete all elements of list */
8282
c3_list_clear (disj);

include/parser/ast.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@ ASTNode* ast_create_bvc(unsigned int width, unsigned long long bvconst);
4646
ASTBVConst* ast_bvc_create(unsigned int bits);
4747
size_t ast_bvc_size(unsigned int bits);
4848
void ast_bvc_resize(ASTBVConst *bvconst, unsigned int width);
49+
bool ast_bvc_bit_test (ASTBVConst *bvconst, unsigned int index);
4950
void ast_print(ASTNode *root);
5051
#endif

ordered_map.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* C3 Theorem Prover - Apache v2.0 - Copyright 2017 - rkx1209 */
22
#include <stdio.h>
33
#include <stdlib.h>
4+
#include <string.h>
45
#include <c3_ordered_map.h>
56

67
static int c3_compare_pair (const C3Pair *a, const C3Pair *b) {

parser/ast.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,11 @@ void ast_bvc_resize (ASTBVConst *bvconst, unsigned int width) {
198198
}
199199
}
200200

201+
bool ast_bvc_bit_test (ASTBVConst *bvconst, unsigned int index) {
202+
unsigned char *bits = bvconst->bits;
203+
return bits [index / 8] & (1 >> (index % 8));
204+
}
205+
201206
void ast_print(ASTNode *root) {
202207
C3ListIter *iter;
203208
ASTNode *node;

tosat/bitblaster.c

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <stdlib.h>
55

66
C3BitBlast *bb;
7+
78
C3BitBlast *c3_bitblast_new() {
89
C3BitBlast *bitblast;
910
bitblast = (C3BitBlast *) malloc (sizeof (C3BitBlast));
@@ -52,8 +53,10 @@ ASTVec c3_bitblast_neg(ASTVec vec) {
5253
return result;
5354
}
5455

55-
static ASTNode *c3_bitblast_cmp_internal (ASTVec left, ASTVec right, bool sign, bool bvlt) {
56-
/* TODO: */
56+
static ASTNode *c3_bitblast_bvle (ASTVec left, ASTVec right, bool sign, bool bvlt) {
57+
C3ListIter *lit, *rit;
58+
lit = left->tail;
59+
rit = right->tail;
5760
return NULL;
5861
}
5962

@@ -66,40 +69,42 @@ ASTNode *c3_bitblast_cmp(ASTNode *form) {
6669
switch (k) {
6770
case BVLE:
6871
{
69-
result = c3_bitblast_cmp_internal (left, right, false, false);
72+
result = c3_bitblast_bvle (left, right, false, false);
7073
break;
7174
}
7275
case BVGE:
7376
{
74-
result = c3_bitblast_cmp_internal (left, right, false, false);
77+
result = c3_bitblast_bvle (right, left, false, false);
7578
break;
7679
}
7780
case BVGT:
7881
{
79-
result = c3_bitblast_cmp_internal (right, left, false, true);
82+
result = c3_bitblast_bvle (right, left, false, true);
8083
break;
8184
}
8285
case BVLT:
8386
{
84-
result = c3_bitblast_cmp_internal (left, right, true, true);
87+
result = c3_bitblast_bvle (left, right, false, true);
8588
break;
8689
}
8790
case BVSLE:
8891
{
89-
result = c3_bitblast_cmp_internal (right, left, true, true);
92+
result = c3_bitblast_bvle (left, right, true, false);
9093
break;
9194
}
9295
case BVSGE:
9396
{
94-
result = c3_bitblast_cmp_internal (left, right, false, false);
97+
result = c3_bitblast_bvle (right, left, true, false);
9598
break;
9699
}
97100
case BVSGT:
98101
{
102+
//result = c3_bitblast_bvle (right, left, true, true);
99103
break;
100104
}
101105
case BVSLT:
102106
{
107+
//result = c3_bitblast_bvle (left, right, true, true);
103108
break;
104109
}
105110
default:
@@ -111,23 +116,39 @@ ASTNode *c3_bitblast_cmp(ASTNode *form) {
111116
ASTVec c3_bitblast_term(ASTNode *term) {
112117
const ASTKind k = term->kind;
113118
ASTVec children = term->children, result;
119+
unsigned int numbits = term->value_width;
120+
114121
switch (k) {
115122
case BVNOT:
116123
{
117124
const ASTVec kids = c3_bitblast_term ((ASTNode *)children->head->data);
118125
result = c3_bitblast_neg (kids);
119126
break;
120127
}
128+
case BVCONST:
129+
{
130+
ASTVec cnts = ast_vec_new ();
131+
ASTBVConst *bvconst = term->bvconst;
132+
unsigned int i;
133+
for (i = 0; i < numbits; i++) {
134+
ast_vec_add (cnts, ast_bvc_bit_test (bvconst, i) ? bb->ASTTrue : bb->ASTFalse);
135+
}
136+
result = cnts;
137+
break;
138+
}
121139
default:
122140
break;
123141
}
124142
return result;
125143
}
126144

127145
ASTNode *c3_bitblast_form(ASTNode *form) {
128-
ASTNode *result;
146+
ASTNode *result, *child;
129147
ASTVec children = form->children;
148+
ASTVec echildren = ast_vec_new ();
149+
C3ListIter *iter;
130150
const ASTKind k = form->kind;
151+
131152
switch (k) {
132153
case TRUE:
133154
result = bb->ASTTrue;
@@ -136,10 +157,13 @@ ASTNode *c3_bitblast_form(ASTNode *form) {
136157
result = bb->ASTFalse;
137158
break;
138159
case NOT:
139-
result = ast_create_node1 (NOT, (ASTNode *)children->head->data);
160+
result = ast_create_node1 (NOT, c3_bitblast_form ((ASTNode *)children->head->data));
140161
break;
141162
case ITE:
142-
result = ast_create_node3 (ITE, (ASTNode *)children->head->data, (ASTNode *)children->head->n->data, (ASTNode *)children->head->n->n->data);
163+
result = ast_create_node3 (ITE,
164+
c3_bitblast_form ((ASTNode *)children->head->data),
165+
c3_bitblast_form ((ASTNode *)children->head->n->data),
166+
c3_bitblast_form ((ASTNode *)children->head->n->n->data));
143167
break;
144168
case AND:
145169
case OR:
@@ -148,7 +172,10 @@ ASTNode *c3_bitblast_form(ASTNode *form) {
148172
case IFF:
149173
case XOR:
150174
case IMPLIES:
151-
result = ast_create_node (k, children);
175+
c3_list_foreach (children, iter, child) {
176+
c3_list_append (echildren, c3_bitblast_form (child));
177+
}
178+
result = ast_create_node (k, echildren);
152179
break;
153180
case EQ:
154181
{
@@ -177,6 +204,6 @@ ASTNode *c3_bitblast_form(ASTNode *form) {
177204

178205
void c3_bitblast(C3 *c3, ASTNode *assertq) {
179206
bb = c3_bitblast_new ();
180-
c3_bitblast_form (assertq);
207+
assertq = c3_bitblast_form (assertq);
181208
c3_bitblast_free (bb);
182209
}

0 commit comments

Comments
 (0)