Skip to content

Commit 6b4b812

Browse files
committed
Add BVCMP BitBlasting
1 parent 5202665 commit 6b4b812

File tree

4 files changed

+57
-9
lines changed

4 files changed

+57
-9
lines changed

core.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,10 @@ void c3_add_assert (C3 *c3, ASTNode *assert) {
367367

368368
void c3_solve_by_sat(C3 *c3, ASTNode *assertq) {
369369
/* Bitblasting: Translate SMT operations to combinational logic */
370-
//c3_bitblast (c3, assertq);
370+
c3_bitblast (c3, assertq);
371+
printf("Bitblast: \n");
372+
ast_print (assertq);
373+
printf("\n");
371374
}
372375

373376
int main(int argc, char **argv, char **envp) {

include/parser/ast.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ ASTVec ast_vec_new();
4242
ASTNode* ast_vec_add(ASTVec vec, ASTNode* node);
4343
size_t ast_vec_size(ASTVec vec);
4444
Type ast_get_type(ASTNode *node);
45+
ASTKind ast_get_kind(ASTNode *node);
4546
ASTNode* ast_create_bvc(unsigned int width, unsigned long long bvconst);
4647
ASTBVConst* ast_bvc_create(unsigned int bits);
4748
size_t ast_bvc_size(unsigned int bits);

parser/ast.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,17 @@ Type ast_get_type(ASTNode *node) {
136136
return UNKNOWN_TYPE;
137137
}
138138

139+
ASTKind ast_get_kind(ASTNode *node) {
140+
if (node->kind) {
141+
return node->kind;
142+
} else if (node->name) {
143+
return SYMBOL;
144+
} else if (node->bvconst) {
145+
return BVCONST;
146+
}
147+
return -1;
148+
}
149+
139150
ASTNode* ast_create_bvc(unsigned int width, unsigned long long bvconst) {
140151
if (width <= 0 || width > (sizeof (unsigned long long) * 8)) {
141152
c3_fatal_error ("CreateBVConst: trying to create bvconst of width %d", width);

tosat/bitblaster.c

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,36 @@ ASTVec c3_bitblast_neg(ASTVec vec) {
5555

5656
static ASTNode *c3_bitblast_bvle (ASTVec left, ASTVec right, bool sign, bool bvlt) {
5757
C3ListIter *lit, *rit;
58-
lit = left->tail;
59-
rit = right->tail;
60-
return NULL;
58+
ASTNode *prevbit = bb->ASTTrue;
59+
lit = left->head;
60+
rit = right->head;
61+
while (lit->n != left->tail) {
62+
ASTNode *lnode = (ASTNode *)lit->data;
63+
ASTNode *rnode = (ASTNode *)rit->data;
64+
ASTNode *thisbit = ast_create_node3 (ITE, ast_create_node2 (IFF, rnode, lnode), prevbit, rnode);
65+
prevbit = thisbit;
66+
lit = lit->n;
67+
rit = rit->n;
68+
}
69+
ASTNode *lmsb = (ASTNode *)lit->data;
70+
ASTNode *rmsb = (ASTNode *)lit->data;
71+
if (sign) {
72+
lmsb = ast_create_node1 (NOT, lmsb);
73+
rmsb = ast_create_node1 (NOT, rmsb);
74+
}
75+
ASTNode *msb = ast_create_node3 (ITE, ast_create_node2 (IFF, rmsb, lmsb), prevbit, rmsb);
76+
if (bvlt) {
77+
msb = ast_create_node1 (NOT, msb);
78+
}
79+
return msb;
6180
}
6281

6382
ASTNode *c3_bitblast_cmp(ASTNode *form) {
6483
ASTNode *result;
6584
ASTVec children = form->children;
6685
ASTVec left = c3_bitblast_term ((ASTNode *)children->head->data);
6786
ASTVec right = c3_bitblast_term ((ASTNode *)children->head->n->data);
68-
const ASTKind k = form->kind;
87+
const ASTKind k = ast_get_kind (form);
6988
switch (k) {
7089
case BVLE:
7190
{
@@ -99,12 +118,13 @@ ASTNode *c3_bitblast_cmp(ASTNode *form) {
99118
}
100119
case BVSGT:
101120
{
102-
//result = c3_bitblast_bvle (right, left, true, true);
121+
result = ast_create_node1 (NOT, c3_bitblast_bvle (left, right, true, false));
103122
break;
104123
}
105124
case BVSLT:
106125
{
107-
//result = c3_bitblast_bvle (left, right, true, true);
126+
//result = ast_create_node1 (NOT, c3_bitblast_bvle (right, left, true, false));
127+
result = form;
108128
break;
109129
}
110130
default:
@@ -114,7 +134,7 @@ ASTNode *c3_bitblast_cmp(ASTNode *form) {
114134
}
115135

116136
ASTVec c3_bitblast_term(ASTNode *term) {
117-
const ASTKind k = term->kind;
137+
const ASTKind k = ast_get_kind (term);
118138
ASTVec children = term->children, result;
119139
unsigned int numbits = term->value_width;
120140

@@ -125,8 +145,17 @@ ASTVec c3_bitblast_term(ASTNode *term) {
125145
result = c3_bitblast_neg (kids);
126146
break;
127147
}
148+
case BVSX:
149+
case BVZX:
150+
{
151+
printf ("BVSX¥n");
152+
/* TODO: */
153+
result = term;
154+
break;
155+
}
128156
case BVCONST:
129157
{
158+
printf ("BVCONST¥n");
130159
ASTVec cnts = ast_vec_new ();
131160
ASTBVConst *bvconst = term->bvconst;
132161
unsigned int i;
@@ -147,7 +176,7 @@ ASTNode *c3_bitblast_form(ASTNode *form) {
147176
ASTVec children = form->children;
148177
ASTVec echildren = ast_vec_new ();
149178
C3ListIter *iter;
150-
const ASTKind k = form->kind;
179+
const ASTKind k = ast_get_kind (form);
151180

152181
switch (k) {
153182
case TRUE:
@@ -172,11 +201,14 @@ ASTNode *c3_bitblast_form(ASTNode *form) {
172201
case IFF:
173202
case XOR:
174203
case IMPLIES:
204+
{
205+
printf ("TWO¥n");
175206
c3_list_foreach (children, iter, child) {
176207
c3_list_append (echildren, c3_bitblast_form (child));
177208
}
178209
result = ast_create_node (k, echildren);
179210
break;
211+
}
180212
case EQ:
181213
{
182214
ASTVec left = c3_bitblast_term ((ASTNode *)children->head->data);
@@ -193,6 +225,7 @@ ASTNode *c3_bitblast_form(ASTNode *form) {
193225
case BVSGT:
194226
case BVSLT:
195227
{
228+
printf ("BVSLT¥n");
196229
result = c3_bitblast_cmp (form);
197230
break;
198231
}

0 commit comments

Comments
 (0)