-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathToySMT.c
1279 lines (1104 loc) · 31.1 KB
/
ToySMT.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdarg.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include "ToySMT.h"
#include "utils.h"
#define VAR_ALWAYS_FALSE 1
#define VAR_ALWAYS_TRUE 2
struct variable* var_always_false=NULL;
struct variable* var_always_true=NULL;
// global switches
bool dump_internal_variables;
// fwd decl:
void print_expr(struct expr* e);
struct expr* create_unary_expr(enum OP t, struct expr* op)
{
struct expr* rt=xmalloc(sizeof(struct expr));
memset (rt, 0, sizeof(struct expr));
rt->type=EXPR_UNARY;
rt->op=t;
rt->op1=op;
return rt;
};
struct expr* create_bin_expr(enum OP t, struct expr* op1, struct expr* op2)
{
/*
printf ("%s()\n", __FUNCTION__);
printf ("op1=");
print_expr(op1);
printf ("\n");
printf ("op2=");
print_expr(op2);
printf ("\n");
*/
struct expr* rt=xmalloc(sizeof(struct expr));
memset (rt, 0, sizeof(struct expr));
rt->type=EXPR_BINARY;
rt->op=t;
rt->op1=op1;
rt->op2=op2;
return rt;
};
struct expr* create_ternary_expr(enum OP t, struct expr* op1, struct expr* op2, struct expr* op3)
{
/*
printf ("%s()\n", __FUNCTION__);
printf ("op1=");
print_expr(op1);
printf ("\n");
printf ("op2=");
print_expr(op2);
printf ("\n");
*/
struct expr* rt=xmalloc(sizeof(struct expr));
rt->type=EXPR_TERNARY;
rt->op=t;
rt->op1=op1;
rt->op2=op2;
rt->op3=op3;
return rt;
};
// from smt2.y:
int yylineno;
// fwd decl;
char* op_name(enum OP op);
struct expr* create_vararg_expr(enum OP t, struct expr* args)
{
/*
printf ("%s(). input=\n", __FUNCTION__);
for (struct expr* e=args; e; e=e->next)
{
print_expr(e);
printf ("\n");
};
*/
// this provides left associativity.
// be sure at least two expr in chain:
if (args->next==NULL)
die("line %d: '%s' requires 2 or more arguments!\n", yylineno, op_name(t));
if (args->next->next==NULL)
// only two expr in chain:
return create_bin_expr(t, args->next, args);
else
// >2 expr in chain:
return create_bin_expr(t, create_vararg_expr(t, args->next), args);
};
struct expr* create_distinct_expr(struct expr* args)
{
// for 3 args:
// and (a!=b, a!=c, b!=c)
// be sure at least two expr in chain:
if (args->next==NULL)
die("line %d: 'distinct' requires 2 or more arguments!\n", yylineno);
if (args->next->next==NULL)
// only two expr in chain:
return create_bin_expr (OP_NEQ, args->next, args);
else
{
// >2 expr in chain:
struct expr* e1=args;
struct expr* big_AND_expr=NULL;
for (struct expr* e2=args->next; e2; e2=e2->next)
{
struct expr* t=create_bin_expr (OP_NEQ, e1, e2);
t->next=big_AND_expr;
big_AND_expr=t;
};
// at this moment, big_AND_expr is chained expression of expr we will pass inside AND(...)
struct expr *t=create_distinct_expr(args->next);
t->next=big_AND_expr;
/*
printf ("%s(). what we passing inside AND(...):\n", __FUNCTION__);
print_expr(t);
printf ("\n");
*/
return create_vararg_expr(OP_AND, t);
};
}
struct expr* create_const_expr(uint32_t c, int w)
{
//printf ("%s(%d, %d)\n", __FUNCTION__, c, w);
struct expr* rt=xmalloc(sizeof(struct expr));
rt->type=EXPR_CONST;
rt->const_val=c;
rt->const_width=w;
return rt;
};
struct expr* create_zero_extend_expr(int bits, struct expr* e)
{
struct expr* rt=xmalloc(sizeof(struct expr));
rt->type=EXPR_ZERO_EXTEND;
rt->const_val=bits;
rt->op1=e;
return rt;
};
// get [start, end) bits
struct expr* create_extract_expr(unsigned end, unsigned start, struct expr* e)
{
if (start>end)
die ("line %d: start must be >=end, but you have start=%d, end=%d\n", yylineno, start, end);
unsigned w=end-start+1;
struct expr* rt=xmalloc(sizeof(struct expr));
rt->type=EXPR_EXTRACT;
rt->const_val=start;
rt->const_width=w;
rt->op1=e;
return rt;
};
char* op_name(enum OP op)
{
switch (op)
{
case OP_NOT: return "not";
case OP_EQ: return "=";
case OP_NEQ: return "!="; // supported in SMT-LIB 2.x? not sure.
case OP_OR: return "or";
case OP_XOR: return "xor";
case OP_AND: return "and";
case OP_BVNOT: return "bvnot";
case OP_BVNEG: return "bvneg";
case OP_BVXOR: return "bvxor";
case OP_BVADD: return "bvadd";
case OP_BVAND: return "bvand";
case OP_BVSUB: return "bvsub";
case OP_BVUGE: return "bvuge";
case OP_BVULE: return "bvule";
case OP_BVUGT: return "bvugt";
case OP_BVULT: return "bvult";
case OP_ITE: return "ite";
default:
assert(0);
};
};
void print_expr(struct expr* e)
{
if (e->type==EXPR_ID)
{
printf ("%s", e->id);
return;
};
if (e->type==EXPR_CONST)
{
printf ("%d (%d bits)", e->const_val, e->const_width);
return;
};
if (e->type==EXPR_ZERO_EXTEND)
{
printf ("(ZEXT by %d bits: ", e->const_val);
print_expr(e->op1);
printf (")");
return;
};
if (e->type==EXPR_EXTRACT)
{
printf ("(extract, start=%d width=%d bits: ", e->const_val, e->const_width);
print_expr(e->op1);
printf (")");
return;
};
if (e->type==EXPR_UNARY)
{
printf ("(%s ", op_name(e->op));
print_expr(e->op1);
printf (")");
return;
};
if (e->type==EXPR_BINARY)
{
printf ("(%s ", op_name(e->op));
print_expr(e->op1);
printf (" ");
print_expr(e->op2);
printf (")");
return;
};
if (e->type==EXPR_TERNARY)
{
printf ("(%s ", op_name(e->op));
print_expr(e->op1);
printf (" ");
print_expr(e->op2);
printf (" ");
print_expr(e->op3);
printf (")");
return;
};
assert (0);
};
// 3 instead of 1, becasue two variables (false/true) are allocated at start
int next_var_no=3;
struct variable
{
int type; // TY_BOOL, TY_BITVEC
bool internal; // 0/1, 1 for internal
char* id; // name
int var_no; // in SAT instance
int width; // in bits, 1 for bool
// TODO: uint64_t? bitmap?
uint32_t val; // what we've got from from SAT-solver's results. 0/1 for Bool
struct variable* next;
};
struct variable* vars=NULL;
char* false_true_s[2]={"false", "true"};
void dump_all_variables(bool dump_internal)
{
//for (struct variable* v=vars; v; v=v->next)
// printf ("type=%d id=%s width=%d val=0x%x\n", v->type, v->id, v->width, v->val);
printf ("(model\n");
for (struct variable* v=vars; v; v=v->next)
{
if (v->internal==1 && dump_internal==false)
continue; // skip internal variables
if (v->type==TY_BOOL)
{
assert (v->val<=1);
printf ("\t(define-fun %s () Bool %s)\n", v->id, false_true_s[v->val]);
}
else if (v->type==TY_BITVEC)
{
printf ("\t(define-fun %s () (_ BitVec %d) (_ bv%u %d)) ; 0x%x\n",
v->id, v->width, v->val, v->width, v->val);
//printf ("\t(define-fun %s () (_ BitVec %d) (_ bv%u %d)) ; 0x%x var_no=%d\n",
// v->id, v->width, v->val, v->width, v->val, v->var_no);
}
else
{
assert(0);
};
}
printf (")\n");
};
struct variable* find_variable(char *id)
{
if (vars==NULL)
return NULL;
for (struct variable* v=vars; v; v=v->next)
{
if (strcmp(id, v->id)==0)
return v;
};
return NULL;
};
struct variable* find_variable_by_no(int no)
{
if (vars==NULL)
return NULL;
for (struct variable* v=vars; v; v=v->next)
{
if (v->var_no == no)
return v;
if (no >= v->var_no && no < v->var_no+v->width)
return v;
};
return NULL;
};
struct variable* create_variable(char *name, int type, int width, int internal)
{
if (type==TY_BOOL)
assert(width==1);
//printf ("%s(%s, %d)\n", __FUNCTION__, name, type);
//printf ("%s() line %d variables=%p\n", __FUNCTION__, __LINE__, vars);
if (find_variable(name)!=NULL)
die ("Fatal error: variable %s is already defined\n", name);
struct variable* v;
if (vars==NULL)
{
v=vars=xmalloc(sizeof(struct variable));
}
else
{
for (v=vars; v->next; v=v->next);
v->next=xmalloc(sizeof(struct variable));
v=v->next;
};
v->type=type;
v->id=xstrdup(name); // TODO replace strdup with something
if (type==TY_BOOL)
{
v->var_no=next_var_no;
v->width=1;
next_var_no++;
}
else if (type==TY_BITVEC)
{
v->var_no=next_var_no;
v->width=width;
next_var_no+=width;
}
else
assert(0);
//printf ("%s() %s var_no=%d\n", __FUNCTION__, name, v->var_no);
v->internal=internal;
return v;
}
int next_internal_var=1;
struct variable* create_internal_variable(char* prefix, int type, int width)
{
char tmp[128];
snprintf (tmp, sizeof(tmp), "%s!%d", prefix, next_internal_var);
next_internal_var++;
return create_variable(tmp, type, width, 1);
};
int clauses_t=0;
struct clause
{
char *c;
struct clause* next;
};
struct clause *clauses=NULL;
struct clause *last_clause=NULL;
void add_line(char *s)
{
if (clauses==NULL)
last_clause=clauses=xmalloc(sizeof(struct clause));
else
{
struct clause *cl=xmalloc(sizeof(struct clause));
last_clause->next=cl;
last_clause=cl;
};
last_clause->c=s;
}
void add_clause(const char* fmt, ...)
{
va_list va;
va_start (va, fmt);
size_t buflen=vsnprintf (NULL, 0, fmt, va)+2+1;
char* buf=xmalloc(buflen);
int written=vsnprintf (buf, buflen, fmt, va);
assert (written<buflen);
strcpy (buf+strlen(buf), " 0");
add_line(buf);
clauses_t++;
};
void add_clause1(int v1)
{
add_clause ("%d", v1);
};
void add_clause2(int v1, int v2)
{
add_clause ("%d %d", v1, v2);
};
void add_clause3(int v1, int v2, int v3)
{
add_clause ("%d %d %d", v1, v2, v3);
};
void add_clause4(int v1, int v2, int v3, int v4)
{
add_clause ("%d %d %d %d", v1, v2, v3, v4);
};
void add_comment(const char* s)
{
//printf ("%s() %s\n", __FUNCTION__, s);
size_t len=strlen(s)+3;
char *tmp=xmalloc(len);
snprintf (tmp, len, "c %s", s);
add_line(tmp);
};
struct variable* generate_const(uint32_t val, int width)
{
//printf ("%s(%d, %d)\n", __FUNCTION__, val, width);
struct variable* rt=create_internal_variable("internal", TY_BITVEC, width);
add_comment("generate_const()");
for (int i=0; i<width; i++)
{
if ((val>>i)&1)
{
// add "always true" for this bit
add_clause1 (rt->var_no+i);
}
else
{
// add "always false" for this bit
add_clause1 (-(rt->var_no+i));
}
};
return rt;
}
void add_Tseitin_NOT(int v1, int v2)
{
add_clause2 (-v1, -v2);
add_clause2 (v1, v2);
}
struct variable* generate_NOT(struct variable* v)
{
if (v->type!=TY_BOOL)
die ("Error: sort mismatch: 'not' takes bool expression, which is not in %s\n", v->id);
struct variable* rt=create_internal_variable("internal", TY_BOOL, 1);
add_comment ("generate_NOT");
add_Tseitin_NOT (rt->var_no, v->var_no);
return rt;
};
struct variable* generate_BVNOT(struct variable* v)
{
if (v->type!=TY_BITVEC)
die ("Error: sort mismatch: 'bvnot' takes bitvec expression, which is not in %s\n", v->id);
struct variable* rt=create_internal_variable("internal", TY_BITVEC, v->width);
add_comment ("generate_BVNOT");
for (int i=0; i<v->width; i++)
add_Tseitin_NOT (rt->var_no+i, v->var_no+i);
return rt;
};
// fwd decl:
struct variable* generate_BVADD(struct variable* v1, struct variable* v2);
struct variable* generate_BVNEG(struct variable* v)
{
if (v->type!=TY_BITVEC)
die ("Error: sort mismatch: 'bvneg' takes bitvec expression, which is not in %s\n", v->id);
add_comment ("generate_BVNEG");
return generate_BVADD(generate_BVNOT(v), generate_const(1, v->width));
};
void add_Tseitin_XOR(int v1, int v2, int v3)
{
add_clause3 (-v1, -v2, -v3);
add_clause3 (v1, v2, -v3);
add_clause3 (v1, -v2, v3);
add_clause3 (-v1, v2, v3);
};
// TODO use Tseitin + gates?
// full-adder, as found by Mathematica using truth table:
void add_FA(int a, int b, int cin, int s, int cout)
{
add_comment("add_FA");
add_clause4(-a, -b, -cin, s);
add_clause3(-a, -b, cout);
add_clause3(-a, -cin, cout);
add_clause3(-a, cout, s);
add_clause4(a, b, cin, -s);
add_clause3(a, b, -cout);
add_clause3(a, cin, -cout);
add_clause3(a, -cout, -s);
add_clause3(-b, -cin, cout);
add_clause3(-b, cout, s);
add_clause3(b, cin, -cout);
add_clause3(b, -cout, -s);
add_clause3(-cin, cout, s);
add_clause3(cin, -cout, -s);
};
void generate_adder(struct variable* a, struct variable* b, struct variable *carry_in, // inputs
struct variable** sum, struct variable** carry_out) // outputs
{
assert(a->type==TY_BITVEC);
assert(b->type==TY_BITVEC);
assert(a->width==b->width);
assert(carry_in->type==TY_BOOL);
*sum=create_internal_variable("adder_sum", TY_BITVEC, a->width);
add_comment (__FUNCTION__);
int carry=carry_in->var_no;
// the first full-adder could be half-adder, but we make things simple here
for (int i=0; i<a->width; i++)
{
*carry_out=create_internal_variable("adder_carry", TY_BOOL, 1);
add_FA(a->var_no+i, b->var_no+i, carry, (*sum)->var_no+i, (*carry_out)->var_no);
// newly created carry_out is a carry_in for the next full-adder:
carry=(*carry_out)->var_no;
};
};
struct variable* generate_BVADD(struct variable* v1, struct variable* v2)
{
assert(v1->type==TY_BITVEC);
assert(v2->type==TY_BITVEC);
assert(v1->width==v2->width);
struct variable *sum;
struct variable *carry_out;
generate_adder(v1, v2, var_always_false, &sum, &carry_out);
return sum;
};
// TODO use Tseitin + gates?
// full-subtractor, as found by Mathematica using truth table:
void add_FS(int x, int y, int bin, int d, int bout)
{
add_comment("add_FS");
add_clause3(-bin, bout, -d);
add_clause3(-bin, bout, -y);
add_clause4(-bin, -d, -x, y);
add_clause4(-bin, -d, x, -y);
add_clause4(-bin, d, -x, -y);
add_clause4(-bin, d, x, y);
add_clause3(bin, -bout, d);
add_clause3(bin, -bout, y);
add_clause4(bin, -d, -x, -y);
add_clause4(bin, -d, x, y);
add_clause4(bin, d, -x, y);
add_clause4(bin, d, x, -y);
add_clause3(-bout, d, y);
add_clause3(bout, -d, -y);
};
struct variable* generate_BVSUB(struct variable* v1, struct variable* v2)
{
assert(v1->type==TY_BITVEC);
assert(v2->type==TY_BITVEC);
assert(v1->width==v2->width);
struct variable* rt=create_internal_variable("SUB_result", TY_BITVEC, v1->width);
add_comment ("generate_BVSUB");
int borrow=VAR_ALWAYS_FALSE;
// the first full-subtractor could be half-subtractor, but we make things simple here
for (int i=0; i<v1->width; i++)
{
struct variable* borrow_out=create_internal_variable("internal", TY_BOOL, 1);
add_FS(v1->var_no+i, v2->var_no+i, borrow, rt->var_no+i, borrow_out->var_no);
// newly created borrow_out is a borrow_in for the next full-subtractor:
borrow=borrow_out->var_no;
};
return rt;
};
// only borrow-out is returned!
// TODO join two functions into one?!
struct variable* generate_BVSUB_borrow(struct variable* v1, struct variable* v2)
{
assert(v1->type==TY_BITVEC);
assert(v2->type==TY_BITVEC);
assert(v1->width==v2->width);
struct variable* rt=create_internal_variable("internal", TY_BITVEC, v1->width);
add_comment ("generate_BVSUB_borrow");
int borrow=VAR_ALWAYS_FALSE;
struct variable* borrow_out=NULL; // make compiler happy
// the first full-subtractor could be half-subtractor, but we make things simple here
for (int i=0; i<v1->width; i++)
{
borrow_out=create_internal_variable("internal", TY_BOOL, 1);
add_FS(v1->var_no+i, v2->var_no+i, borrow, rt->var_no+i, borrow_out->var_no);
// newly created borrow_out is a borrow_in for the next full-subtractor:
borrow=borrow_out->var_no;
};
//printf ("%s() returns %s\n", __FUNCTION__, borrow_out->id);
return borrow_out;
};
// fwd decl:
struct variable* generate_EQ(struct variable* v1, struct variable* v2);
struct variable* generate_OR(struct variable* v1, struct variable* v2);
struct variable* generate_BVULT(struct variable* v1, struct variable* v2)
{
assert(v1->type==TY_BITVEC);
assert(v2->type==TY_BITVEC);
assert(v1->width==v2->width);
add_comment (__FUNCTION__);
return generate_BVSUB_borrow(v1, v2);
};
struct variable* generate_BVULE(struct variable* v1, struct variable* v2)
{
assert(v1->type==TY_BITVEC);
assert(v2->type==TY_BITVEC);
assert(v1->width==v2->width);
add_comment (__FUNCTION__);
return generate_OR(generate_BVULT(v1, v2), generate_EQ(v1, v2));
};
struct variable* generate_BVUGT(struct variable* v1, struct variable* v2)
{
assert(v1->type==TY_BITVEC);
assert(v2->type==TY_BITVEC);
assert(v1->width==v2->width);
add_comment (__FUNCTION__);
return generate_BVSUB_borrow(v2, v1);
};
struct variable* generate_BVUGE(struct variable* v1, struct variable* v2)
{
assert(v1->type==TY_BITVEC);
assert(v2->type==TY_BITVEC);
assert(v1->width==v2->width);
add_comment (__FUNCTION__);
return generate_OR(generate_BVUGT(v1, v2), generate_EQ(v1, v2));
};
// fwd decl:
struct variable* generate_ITE(struct variable* sel, struct variable* t, struct variable* f);
// it's like SUBGE in ARM CPU in ARM mode
// rationale: used in divisor!
void generate_BVSUBGE(struct variable* enable, struct variable* v1, struct variable* v2,
struct variable** output, struct variable** cond)
{
assert(v1->type==TY_BITVEC);
assert(v2->type==TY_BITVEC);
assert(v1->width==v2->width);
*cond=generate_BVUGE(v1, v2);
struct variable *diff=generate_BVSUB(v1, v2);
*output=generate_ITE(enable, generate_ITE(*cond, diff, v1), v1);
};
// fwd decl:
void add_Tseitin_OR_list(int var, int width, int var_out);
struct variable* generate_zero_extend(struct variable *in, int zeroes_to_add);
void add_Tseitin_BV_is_zero (int var_no, int width, int var_no_out)
{
// all bits in BV are zero?
//int tmp=next_var_no++; // allocate var
struct variable *tmp=create_internal_variable("tmp", TY_BOOL, 1);
add_Tseitin_OR_list(var_no, width, tmp->var_no);
add_Tseitin_NOT(tmp->var_no, var_no_out);
};
// fwd decl
void add_Tseitin_EQ(int v1, int v2);
struct variable* generate_shift_left(struct variable* X, unsigned int cnt);
struct variable* generate_extract(struct variable *v, unsigned begin, unsigned width);
struct variable* generate_shift_right(struct variable* X, unsigned int cnt);
void generate_divisor (struct variable* divident, struct variable* divisor, struct variable** q, struct variable** r)
{
assert (divident->type==TY_BITVEC);
assert (divisor->type==TY_BITVEC);
assert (divident->width==divisor->width);
int w=divident->width;
struct variable* wide1=generate_zero_extend(divisor, w);
struct variable* wide2=generate_shift_left(wide1, w-1);
*q=create_internal_variable("quotient", TY_BITVEC, w);
for (int i=0; i<w; i++)
{
struct variable* enable=create_internal_variable("enable", TY_BOOL, 1);
// enable is 1 if high part of wide2 is cleared
add_Tseitin_BV_is_zero (wide2->var_no+w, w, enable->var_no);
struct variable* cond;
generate_BVSUBGE(enable, divident, generate_extract(wide2, 0, w), ÷nt, &cond);
add_Tseitin_EQ(cond->var_no, (*q)->var_no+w-1-i);
wide2=generate_shift_right(wide2, 1);
};
*r=divident;
};
struct variable* generate_BVUDIV(struct variable* v1, struct variable* v2)
{
struct variable *q;
struct variable *r;
generate_divisor (v1, v2, &q, &r);
return q;
};
struct variable* generate_BVUREM(struct variable* v1, struct variable* v2)
{
struct variable *q;
struct variable *r;
generate_divisor (v1, v2, &q, &r);
return r;
};
struct variable* generate_XOR(struct variable* v1, struct variable* v2)
{
assert(v1->type==TY_BOOL);
assert(v2->type==TY_BOOL);
struct variable* rt=create_internal_variable("internal", TY_BOOL, 1);
add_comment ("generate_XOR");
add_Tseitin_XOR (v1->var_no, v2->var_no, rt->var_no);
return rt;
};
// fwd decl:
void add_Tseitin_AND(int a, int b, int out);
struct variable* generate_BVAND(struct variable* v1, struct variable* v2)
{
assert(v1->type==TY_BITVEC);
assert(v2->type==TY_BITVEC);
assert(v1->width==v2->width);
struct variable* rt=create_internal_variable("AND_result", TY_BITVEC, v1->width);
add_comment (__FUNCTION__);
for (int i=0; i<v1->width; i++)
add_Tseitin_AND (v1->var_no+i, v2->var_no+i, rt->var_no+i);
return rt;
};
struct variable* generate_BVXOR(struct variable* v1, struct variable* v2)
{
assert(v1->type==TY_BITVEC);
assert(v2->type==TY_BITVEC);
assert(v1->width==v2->width);
struct variable* rt=create_internal_variable("internal", TY_BITVEC, v1->width);
add_comment ("generate_BVXOR");
for (int i=0; i<v1->width; i++)
add_Tseitin_XOR (v1->var_no+i, v2->var_no+i, rt->var_no+i);
return rt;
};
// as in Tseitin transformations.
// return=var OR var+1 OR ... OR var+width-1
void add_Tseitin_OR_list(int var, int width, int var_out)
{
//printf ("%s(%d, %d)\n", __FUNCTION__, var, width);
add_comment (__FUNCTION__);
char* tmp=create_string_of_numbers_in_range(var, width);
add_clause("%s -%d", tmp, var_out);
for (int i=0; i<width; i++)
add_clause2(-(var+i), var_out);
};
struct variable* generate_OR_list(int var, int width)
{
//printf ("%s(%d, %d)\n", __FUNCTION__, var, width);
struct variable* rt=create_internal_variable("internal", TY_BOOL, 1);
add_comment (__FUNCTION__);
add_Tseitin_OR_list(var, width, rt->var_no);
return rt;
};
struct variable* generate_EQ(struct variable* v1, struct variable* v2)
{
//printf ("%s() v1=%d v2=%d\n", __FUNCTION__, v1->var_no, v2->var_no);
if (v1->type==TY_BOOL)
{
if(v2->type!=TY_BOOL)
{
printf ("%s() sort mismatch\n", __FUNCTION__);
printf ("v1=%s type=%d width=%d\n", v1->id, v1->type, v1->width);
printf ("v2=%s type=%d width=%d\n", v2->id, v2->type, v2->width);
die("");
};
add_comment ("generate_EQ");
struct variable *v=generate_NOT(generate_XOR(v1, v2));
//printf ("%s() returns %s (Bool)\n", __FUNCTION__, v->id);
return v;
}
else
{
assert (v2->type==TY_BITVEC);
if(v1->width!=v2->width)
{
printf ("line %d. = can't work on bitvectors of different widths. you supplied %d and %d\n",
yylineno, v1->width, v2->width);
printf ("v1=%s, v2=%s\n", v1->id, v2->id);
exit(0);
};
add_comment ("generate_EQ for two bitvectors");
struct variable* t=generate_BVXOR(v1,v2);
struct variable* v=generate_NOT(generate_OR_list(t->var_no, t->width));
//printf ("%s() returns %s (bitvec %d)\n", __FUNCTION__, v->id, v->width);
return v;
};
};
struct variable* generate_NEQ(struct variable* v1, struct variable* v2)
{
return generate_NOT(generate_EQ(v1,v2));
};
void add_Tseitin_AND(int a, int b, int out)
{
add_clause3 (-a, -b, out);
add_clause2 (a, -out);
add_clause2 (b, -out);
};
struct variable* generate_AND(struct variable* v1, struct variable* v2)
{
struct variable* rt=create_internal_variable("internal", TY_BOOL, 1);
add_comment ("generate_AND");
add_Tseitin_AND(v1->var_no, v2->var_no, rt->var_no);
return rt;
};
void add_Tseitin_mult_by_bit(int width, int var_no_in, int var_no_out, int var_no_bit)
{
for (int i=0; i<width; i++)
add_Tseitin_AND(var_no_in+i, var_no_bit, var_no_out+i);
};
/*
struct variable* generate_mult_by_bit(struct variable *in, struct variable* bit)
{
assert (in->type==TY_BITVEC);
assert (bit->type==TY_BOOL);
struct variable* rt=create_internal_variable("internal", TY_BITVEC, in->width);
add_Tseitin_mult_by_bit(in->width, in->var_no, rt->var_no, bit->var_no);
return rt;
};
*/
// v1=v2 always!
void add_Tseitin_EQ(int v1, int v2)
{
add_clause2 (-v1, v2);
add_clause2 (v1, -v2);
}
void add_Tseitin_EQ_bitvecs(int width, int v1, int v2)
{
for (int i=0; i<width; i++)
add_Tseitin_EQ(v1+i, v2+i);
}
struct variable* generate_zero_extend(struct variable *in, int zeroes_to_add)
{
int final_width=in->width+zeroes_to_add;
struct variable* rt=create_internal_variable("zero_extended", TY_BITVEC, final_width);
add_Tseitin_EQ_bitvecs(in->width, in->var_no, rt->var_no);
for (int i=0; i<zeroes_to_add; i++)
add_clause1(-(rt->var_no + in->width + i));
return rt;
};
void add_Tseitin_always_false(int v, int width)
{
for (int i=0; i<width; i++)
add_Tseitin_EQ(v+i, VAR_ALWAYS_FALSE);
};
// cnt is not a SMT variable!
struct variable* generate_shift_left(struct variable* X, unsigned int cnt)
{
int w=X->width;
struct variable* rt=create_internal_variable("shifted_left", TY_BITVEC, w);
add_Tseitin_always_false(rt->var_no, cnt);
add_Tseitin_EQ_bitvecs(w-cnt, rt->var_no+cnt, X->var_no);
return rt;
};
// cnt is not a SMT variable!
struct variable* generate_shift_right(struct variable* X, unsigned int cnt)
{
int w=X->width;
struct variable* rt=create_internal_variable("shifted_right", TY_BITVEC, w);
add_Tseitin_always_false(rt->var_no+w-1-cnt, cnt);
add_Tseitin_EQ_bitvecs(w-cnt, rt->var_no, X->var_no+cnt);
return rt;
};
struct variable* generate_extract(struct variable *v, unsigned begin, unsigned width)
{
struct variable* rt=create_internal_variable("extracted", TY_BITVEC, width);
for (int i=0; i<width; i++)
add_Tseitin_EQ(rt->var_no+i, v->var_no+begin+i);
return rt;
};
struct variable* generate_BVMUL(struct variable* X, struct variable* Y)
{
assert (X->type==TY_BITVEC);
assert (Y->type==TY_BITVEC);
assert (X->width==Y->width);
int w=X->width;
int final_w=w*2;
struct variable* X_extended=generate_zero_extend(X, w);
struct variable* partial_products1[w]; // warning: GCC (?) extension
struct variable* partial_products2[w]; // warning: GCC (?) extension
for (int i=0; i<w; i++)
{
partial_products1[i]=create_internal_variable("partial_product1", TY_BITVEC, final_w);
add_Tseitin_mult_by_bit(final_w, X_extended->var_no, partial_products1[i]->var_no, Y->var_no+i);
partial_products2[i]=generate_shift_left(partial_products1[i], i);
};
struct variable *product=partial_products2[0];