Source

z3 / src / ast / bv_decl_plugin.cpp

Full commit
  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
/*++
Copyright (c) 2006 Microsoft Corporation

Module Name:

    bv_decl_plugin.cpp

Abstract:

    <abstract>

Author:

    Leonardo de Moura (leonardo) 2008-01-09.

Revision History:

--*/
#include<sstream>
#include"bv_decl_plugin.h"
#include"arith_decl_plugin.h"
#include"warning.h"
#include"ast_pp.h"
#include"ast_smt2_pp.h"

bv_decl_plugin::bv_decl_plugin():
    m_bv_sym("bv"),
    m_concat_sym("concat"),
    m_sign_extend_sym("sign_extend"),
    m_zero_extend_sym("zero_extend"),
    m_extract_sym("extract"),
    m_rotate_left_sym("rotate_left"),
    m_rotate_right_sym("rotate_right"),
    m_repeat_sym("repeat"),
    m_bit2bool_sym("bit2bool"),
    m_mkbv_sym("mkbv"),
    m_bit0(0),
    m_bit1(0),
    m_carry(0),
    m_xor3(0),
    m_int_sort(0) {
}

void bv_decl_plugin::mk_table_upto(unsigned n) {
    if (m_powers.empty()) {
        m_powers.push_back(rational(1));
    }
    unsigned sz = m_powers.size();
    rational curr = m_powers[sz - 1];
    rational two(2);
    for (unsigned i = sz; i <= n; i++) {
        curr *= two;
        m_powers.push_back(curr);
    }
}

rational bv_decl_plugin::power_of_two(unsigned n) const {
    if (n >= m_powers.size()) {
        const_cast<bv_decl_plugin*>(this)->mk_table_upto(n + 1);
    }
    return m_powers[n];
}

void bv_decl_plugin::set_manager(ast_manager * m, family_id id) {
    decl_plugin::set_manager(m, id);

    for (unsigned i = 1; i <= 64; i++) {
        mk_bv_sort(i);
    }
    m_bit0 = m->mk_const_decl(symbol("bit0"), get_bv_sort(1), func_decl_info(m_family_id, OP_BIT0));
    m_bit1 = m->mk_const_decl(symbol("bit1"), get_bv_sort(1), func_decl_info(m_family_id, OP_BIT1));
    m->inc_ref(m_bit0);
    m->inc_ref(m_bit1);
    
    sort * b = m->mk_bool_sort();
    sort * d[3] = {b, b, b};
    m_carry = m_manager->mk_func_decl(symbol("carry"), 3, d, b, func_decl_info(m_family_id, OP_CARRY));
    m_manager->inc_ref(m_carry);
    m_xor3 = m_manager->mk_func_decl(symbol("xor3"), 3, d, b, func_decl_info(m_family_id, OP_XOR3));
    m_manager->inc_ref(m_xor3);

    m_int_sort = m_manager->mk_sort(m_manager->get_family_id("arith"), INT_SORT);
    SASSERT(m_int_sort != 0); // arith_decl_plugin must be installed before bv_decl_plugin.
    m_manager->inc_ref(m_int_sort);
}

void bv_decl_plugin::finalize() {
#define DEC_REF(FIELD) dec_range_ref(FIELD.begin(), FIELD.end(), *m_manager)
    if (m_bit0) { m_manager->dec_ref(m_bit0); }
    if (m_bit1) { m_manager->dec_ref(m_bit1); }
    if (m_carry) { m_manager->dec_ref(m_carry); }
    if (m_xor3) { m_manager->dec_ref(m_xor3); }
    if (m_int_sort) { m_manager->dec_ref(m_int_sort); }

    DEC_REF(m_bv_sorts);

    DEC_REF(m_bv_neg);
    DEC_REF(m_bv_add);
    DEC_REF(m_bv_sub);
    DEC_REF(m_bv_mul);

    DEC_REF(m_bv_sdiv);
    DEC_REF(m_bv_udiv);
    DEC_REF(m_bv_srem);
    DEC_REF(m_bv_urem);
    DEC_REF(m_bv_smod);

    DEC_REF(m_bv_sdiv0);
    DEC_REF(m_bv_udiv0);
    DEC_REF(m_bv_srem0);
    DEC_REF(m_bv_urem0);
    DEC_REF(m_bv_smod0);

    DEC_REF(m_bv_sdiv_i);
    DEC_REF(m_bv_udiv_i);
    DEC_REF(m_bv_srem_i);
    DEC_REF(m_bv_urem_i);
    DEC_REF(m_bv_smod_i);

    DEC_REF(m_bv_uleq);
    DEC_REF(m_bv_sleq);
    DEC_REF(m_bv_ugeq);
    DEC_REF(m_bv_sgeq);
    DEC_REF(m_bv_ult);
    DEC_REF(m_bv_slt);
    DEC_REF(m_bv_ugt);
    DEC_REF(m_bv_sgt);
    
    DEC_REF(m_bv_and);
    DEC_REF(m_bv_or);
    DEC_REF(m_bv_not);
    DEC_REF(m_bv_xor);
    DEC_REF(m_bv_nand);
    DEC_REF(m_bv_nor);
    DEC_REF(m_bv_xnor);
    
    DEC_REF(m_bv_redor);
    DEC_REF(m_bv_redand);
    DEC_REF(m_bv_comp);

    DEC_REF(m_bv_mul_ovfl);
    DEC_REF(m_bv_smul_ovfl);
    DEC_REF(m_bv_smul_udfl);

    DEC_REF(m_bv_shl);
    DEC_REF(m_bv_lshr);
    DEC_REF(m_bv_ashr);

    DEC_REF(m_ext_rotate_left);
    DEC_REF(m_ext_rotate_right);

    DEC_REF(m_int2bv);
    DEC_REF(m_bv2int);
    vector<ptr_vector<func_decl> >::iterator it  = m_bit2bool.begin();
    vector<ptr_vector<func_decl> >::iterator end = m_bit2bool.end();
    for (; it != end; ++it) {
        ptr_vector<func_decl> & ds = *it;
        DEC_REF(ds);
	}
    DEC_REF(m_mkbv);
}

void bv_decl_plugin::mk_bv_sort(unsigned bv_size) {
    force_ptr_array_size(m_bv_sorts, bv_size + 1);
    if (m_bv_sorts[bv_size] == 0) {
        parameter p(bv_size);
        sort_size sz;
        if (sort_size::is_very_big_base2(bv_size)) {
            sz = sort_size::mk_very_big();
        }
        else {
            sz = sort_size(power_of_two(bv_size));
        }
        m_bv_sorts[bv_size] = m_manager->mk_sort(symbol("bv"), sort_info(m_family_id, BV_SORT, sz, 1, &p));
        m_manager->inc_ref(m_bv_sorts[bv_size]);
    }
}

inline sort * bv_decl_plugin::get_bv_sort(unsigned bv_size) {
	if (bv_size < (1 << 12)) {
	    mk_bv_sort(bv_size);
            return m_bv_sorts[bv_size];
	}
	parameter p(bv_size);
	sort_size sz(sort_size::mk_very_big());
	return m_manager->mk_sort(symbol("bv"), sort_info(m_family_id, BV_SORT, sz, 1, &p));
}

sort * bv_decl_plugin::mk_sort(decl_kind k, unsigned num_parameters, parameter const * parameters) {
    if (!(num_parameters == 1 && parameters[0].is_int())) {
        m_manager->raise_exception("expecting one integer parameter to bit-vector sort");
    }
    unsigned bv_size = parameters[0].get_int();
    mk_bv_sort(bv_size);
    return m_bv_sorts[bv_size];
}

func_decl * bv_decl_plugin::mk_binary(ptr_vector<func_decl> & decls, decl_kind k,
                                    char const * name, unsigned bv_size, bool ac, bool idempotent) {
    force_ptr_array_size(decls, bv_size + 1);
    
    if (decls[bv_size] == 0) {
        sort * s = get_bv_sort(bv_size);
        func_decl_info info(m_family_id, k);
        info.set_associative(ac);
        info.set_flat_associative(ac);
        info.set_commutative(ac);
        info.set_idempotent(idempotent);
        decls[bv_size] = m_manager->mk_func_decl(symbol(name), s, s, s, info);
        m_manager->inc_ref(decls[bv_size]);
    }

    return decls[bv_size];
}

func_decl * bv_decl_plugin::mk_unary(ptr_vector<func_decl> & decls, decl_kind k, char const * name, unsigned bv_size) {
    force_ptr_array_size(decls, bv_size + 1);
    
    if (decls[bv_size] == 0) {
        sort * s = get_bv_sort(bv_size);
        decls[bv_size] = m_manager->mk_func_decl(symbol(name), s, s, func_decl_info(m_family_id, k));
        m_manager->inc_ref(decls[bv_size]);
    }

    return decls[bv_size];
}

func_decl * bv_decl_plugin::mk_int2bv(unsigned bv_size, unsigned num_parameters, parameter const * parameters,
                                    unsigned arity, sort * const * domain) {
    force_ptr_array_size(m_int2bv, bv_size + 1);

    if (arity != 1) {
        m_manager->raise_exception("expecting one argument to int2bv");
        return 0;
    }

    if (m_int2bv[bv_size] == 0) {
        sort * s = get_bv_sort(bv_size);
        m_int2bv[bv_size] = m_manager->mk_func_decl(symbol("int2bv"), domain[0], s, 
                                                    func_decl_info(m_family_id, OP_INT2BV, num_parameters, parameters));
        m_manager->inc_ref(m_int2bv[bv_size]);
    }

    return m_int2bv[bv_size];
}

func_decl * bv_decl_plugin::mk_bv2int(unsigned bv_size, unsigned num_parameters, parameter const * parameters,
                                    unsigned arity, sort * const * domain) {
    force_ptr_array_size(m_bv2int, bv_size + 1);

    if (arity != 1) {
        m_manager->raise_exception("expecting one argument to bv2int");
        return 0;
    }
    
    if (m_bv2int[bv_size] == 0) {
        m_bv2int[bv_size] = m_manager->mk_func_decl(symbol("bv2int"), domain[0], m_int_sort, 
                                                    func_decl_info(m_family_id, OP_BV2INT));
        m_manager->inc_ref(m_bv2int[bv_size]);
    }

    return m_bv2int[bv_size];
}

func_decl * bv_decl_plugin::mk_pred(ptr_vector<func_decl> & decls, decl_kind k, char const * name, unsigned bv_size) {
    force_ptr_array_size(decls, bv_size + 1);
    
    if (decls[bv_size] == 0) {
        sort * s = get_bv_sort(bv_size);
        decls[bv_size] = m_manager->mk_func_decl(symbol(name), s, s, m_manager->mk_bool_sort(), func_decl_info(m_family_id, k));
        m_manager->inc_ref(decls[bv_size]);
    }

    return decls[bv_size];
}

func_decl * bv_decl_plugin::mk_reduction(ptr_vector<func_decl> & decls, decl_kind k, char const * name, unsigned bv_size) {
    force_ptr_array_size(decls, bv_size + 1);
    
    if (decls[bv_size] == 0) {
        sort * d = get_bv_sort(bv_size);
        sort * r = get_bv_sort(1);
        decls[bv_size] = m_manager->mk_func_decl(symbol(name), d, r, func_decl_info(m_family_id, k));
        m_manager->inc_ref(decls[bv_size]);
    }

    return decls[bv_size];
}

func_decl * bv_decl_plugin::mk_comp(unsigned bv_size) {
    force_ptr_array_size(m_bv_comp, bv_size + 1);
    
    if (m_bv_comp[bv_size] == 0) {
        sort * d = get_bv_sort(bv_size);
        sort * r = get_bv_sort(1);
        func_decl_info info(m_family_id, OP_BCOMP);
        info.set_commutative();
        m_bv_comp[bv_size] = m_manager->mk_func_decl(symbol("bvcomp"), d, d, r, info);
        m_manager->inc_ref(m_bv_comp[bv_size]);
    }

    return m_bv_comp[bv_size];
}


func_decl * bv_decl_plugin::mk_func_decl(decl_kind k, unsigned bv_size) {
    switch (k) {
    case OP_BNEG:     return mk_unary(m_bv_neg, k, "bvneg", bv_size);
    case OP_BADD:     return mk_binary(m_bv_add, k, "bvadd", bv_size, true);
    case OP_BSUB:     return mk_binary(m_bv_sub, k, "bvsub", bv_size, false);
    case OP_BMUL:     return mk_binary(m_bv_mul, k, "bvmul", bv_size, true);
    case OP_BSDIV:    return mk_binary(m_bv_sdiv, k, "bvsdiv", bv_size, false);
    case OP_BUDIV:    return mk_binary(m_bv_udiv, k, "bvudiv", bv_size, false);
    case OP_BSREM:    return mk_binary(m_bv_srem, k, "bvsrem", bv_size, false);
    case OP_BUREM:    return mk_binary(m_bv_urem, k, "bvurem", bv_size, false);
    case OP_BSMOD:    return mk_binary(m_bv_smod, k, "bvsmod", bv_size, false);
    case OP_BSDIV0:   return mk_unary(m_bv_sdiv0, k, "bvsdiv0", bv_size);
    case OP_BUDIV0:   return mk_unary(m_bv_udiv0, k, "bvudiv0", bv_size);
    case OP_BSREM0:   return mk_unary(m_bv_srem0, k, "bvsrem0", bv_size);
    case OP_BUREM0:   return mk_unary(m_bv_urem0, k, "bvurem0", bv_size);
    case OP_BSMOD0:   return mk_unary(m_bv_smod0, k, "bvsmod0", bv_size);
    case OP_BSDIV_I:  return mk_binary(m_bv_sdiv_i, k, "bvsdiv_i", bv_size, false);
    case OP_BUDIV_I:  return mk_binary(m_bv_udiv_i, k, "bvudiv_i", bv_size, false);
    case OP_BSREM_I:  return mk_binary(m_bv_srem_i, k, "bvsrem_i", bv_size, false);
    case OP_BUREM_I:  return mk_binary(m_bv_urem_i, k, "bvurem_i", bv_size, false);
    case OP_BSMOD_I:  return mk_binary(m_bv_smod_i, k, "bvsmod_i", bv_size, false);
    case OP_ULEQ:     return mk_pred(m_bv_uleq, k, "bvule", bv_size);
    case OP_SLEQ:     return mk_pred(m_bv_sleq, k, "bvsle", bv_size);
    case OP_UGEQ:     return mk_pred(m_bv_ugeq, k, "bvuge", bv_size);
    case OP_SGEQ:     return mk_pred(m_bv_sgeq, k, "bvsge", bv_size);
    case OP_ULT:      return mk_pred(m_bv_ult, k, "bvult", bv_size);
    case OP_SLT:      return mk_pred(m_bv_slt, k, "bvslt", bv_size);
    case OP_UGT:      return mk_pred(m_bv_ugt, k, "bvugt", bv_size);
    case OP_SGT:      return mk_pred(m_bv_sgt, k, "bvsgt", bv_size);

    case OP_BAND:     return mk_binary(m_bv_and, k, "bvand", bv_size, true, true);
    case OP_BOR:      return mk_binary(m_bv_or, k, "bvor", bv_size, true, true);
    case OP_BNOT:     return mk_unary(m_bv_not, k, "bvnot", bv_size);
    case OP_BXOR:     return mk_binary(m_bv_xor, k, "bvxor", bv_size, true);
    case OP_BNAND:    return mk_binary(m_bv_nand, k, "bvnand", bv_size, false);
    case OP_BNOR:     return mk_binary(m_bv_nor, k, "bvnor", bv_size, false);
    case OP_BXNOR:    return mk_binary(m_bv_xnor, k, "bvxnor", bv_size, false); 

    case OP_BREDOR:   return mk_reduction(m_bv_redor, k, "bvredor", bv_size);
    case OP_BREDAND:  return mk_reduction(m_bv_redand, k, "bvredand", bv_size);
    case OP_BCOMP:    return mk_comp(bv_size);
    case OP_BUMUL_NO_OVFL: return mk_pred(m_bv_mul_ovfl, k, "bvumul_noovfl", bv_size);
    case OP_BSMUL_NO_OVFL: return mk_pred(m_bv_smul_ovfl, k, "bvsmul_noovfl", bv_size);
    case OP_BSMUL_NO_UDFL: return mk_pred(m_bv_smul_udfl, k, "bvsmul_noudfl", bv_size);
        
    case OP_BSHL:     return mk_binary(m_bv_shl, k, "bvshl", bv_size, false);
    case OP_BLSHR:    return mk_binary(m_bv_lshr, k, "bvlshr", bv_size, false);
    case OP_BASHR:    return mk_binary(m_bv_ashr, k, "bvashr", bv_size, false);

    case OP_EXT_ROTATE_LEFT: return mk_binary(m_ext_rotate_left, k, "ext_rotate_left", bv_size, false);
    case OP_EXT_ROTATE_RIGHT: return mk_binary(m_ext_rotate_right, k, "ext_rotate_right", bv_size, false);
    default:          return 0;
    }
}

inline bool bv_decl_plugin::get_bv_size(sort * s, int & result) {
    if (s->get_family_id() == m_family_id && s->get_decl_kind() == BV_SORT) {
        result = s->get_parameter(0).get_int();
        return true;
    }
    return false;
}

inline bool bv_decl_plugin::get_bv_size(expr * t, int & result) {
    return get_bv_size(m_manager->get_sort(t), result);
}

bool bv_decl_plugin::get_concat_size(unsigned arity, sort * const * domain, int & result) {
    result = 0;
    for (unsigned i = 0; i < arity; i++) {
        int sz;
        if (!get_bv_size(domain[i], sz)) {
            return false;
        }
        result += sz;
    }
    return true;
}

bool bv_decl_plugin::get_extend_size(unsigned num_parameters, parameter const * parameters, 
                                   unsigned arity, sort * const * domain, int & result) {
    int arg_sz;
    if (arity != 1 || num_parameters != 1 || !parameters[0].is_int() || !get_bv_size(domain[0], arg_sz)) {
        return false;
    }
    result = arg_sz + parameters[0].get_int();
    return true;
}

bool bv_decl_plugin::get_extract_size(unsigned num_parameters, parameter const * parameters, 
                                    unsigned arity, sort * const * domain, int & result) {
    int arg_sz;
    if (arity != 1 ||
        !get_bv_size(domain[0], arg_sz) || 
        num_parameters != 2 || 
        !parameters[0].is_int() || 
        !parameters[1].is_int() ||
        parameters[1].get_int() > parameters[0].get_int() ||
        parameters[0].get_int() >= arg_sz) {
        return false;
    }
    result = parameters[0].get_int() - parameters[1].get_int() + 1;
    return true;
}

bool bv_decl_plugin::get_int2bv_size(unsigned num_parameters, parameter const * parameters, int & result) {
    if (num_parameters != 1) {
        m_manager->raise_exception("int2bv expects one parameter");
        return false;
    }
    parameter p(parameters[0]);
    if (p.is_int()) {
        result = p.get_int();
        return true;
    }
    if (!p.is_ast() || !is_expr(p.get_ast())) {
        m_manager->raise_exception("int2bv expects one integer parameter");
        return false;
    }
    return get_bv_size(to_expr(p.get_ast()), result);
}


func_decl * bv_decl_plugin::mk_num_decl(unsigned num_parameters, parameter const * parameters, unsigned arity) {
    if (!(num_parameters == 2 && arity == 0 && parameters[0].is_rational() && parameters[1].is_int())) {
        m_manager->raise_exception("invalid bit-vector numeral declaration");
        return 0;
    }
    unsigned bv_size = parameters[1].get_int();
    // TODO: sign an error if the parameters[0] is out of range, that is, it is a value not in [0, 2^{bv_size})
    // This cannot be enforced now, since some Z3 modules try to generate these invalid numerals.
    // After SMT-COMP, I should find all offending modules.
    // For now, I will just simplify the numeral here.
    parameter p0(mod(parameters[0].get_rational(), power_of_two(bv_size)));
    parameter ps[2] = { p0, parameters[1] };
    sort * bv = get_bv_sort(bv_size);
    return m_manager->mk_const_decl(m_bv_sym, bv, func_decl_info(m_family_id, OP_BV_NUM, num_parameters, ps));
}

func_decl * bv_decl_plugin::mk_bit2bool(unsigned bv_size, unsigned num_parameters, parameter const * parameters, 
                                        unsigned arity, sort * const * domain) {
    if (!(num_parameters == 1 && parameters[0].is_int() && arity == 1 && parameters[0].get_int() < static_cast<int>(bv_size))) {
        m_manager->raise_exception("invalid bit2bool declaration");
        return 0;
    }
    unsigned idx = parameters[0].get_int();
    m_bit2bool.reserve(bv_size+1);
    ptr_vector<func_decl> & v = m_bit2bool[bv_size];
    v.reserve(bv_size, 0);
    if (v[idx] == 0) {
        v[idx] = m_manager->mk_func_decl(m_bit2bool_sym, domain[0], m_manager->mk_bool_sort(), 
                                         func_decl_info(m_family_id, OP_BIT2BOOL, num_parameters, parameters));
        m_manager->inc_ref(v[idx]);
    }
    return v[idx];
}

func_decl * bv_decl_plugin::mk_mkbv(unsigned arity, sort * const * domain) {
    for (unsigned i = 0; i < arity; i++) {
        if (!m_manager->is_bool(domain[i])) {
            m_manager->raise_exception("invalid mkbv operator");
            return 0;
        }
    }
    unsigned bv_size = arity;
    m_mkbv.reserve(bv_size+1);
    if (m_mkbv[bv_size] == 0) {
        m_mkbv[bv_size] = m_manager->mk_func_decl(m_mkbv_sym, arity, domain, get_bv_sort(bv_size), func_decl_info(m_family_id, OP_MKBV));
        m_manager->inc_ref(m_mkbv[bv_size]);
    }
    return m_mkbv[bv_size];
}

func_decl * bv_decl_plugin::mk_func_decl(decl_kind k, unsigned num_parameters, parameter const * parameters,
                                       unsigned arity, sort * const * domain, sort * range) {
    int bv_size;
    if (k == OP_INT2BV && get_int2bv_size(num_parameters, parameters, bv_size)) {
        // bv_size is filled in.
    }
    else if (k == OP_BV_NUM) {
        return mk_num_decl(num_parameters, parameters, arity);
    }
    else if (k == OP_BIT0) {
        return m_bit0;
    }
    else if (k == OP_BIT1) {
        return m_bit1;
    }
    else if (k == OP_CARRY) {
        return m_carry;
    }
    else if (k == OP_XOR3) {
        return m_xor3;
    }
    else if (k == OP_MKBV) {
        return mk_mkbv(arity, domain);
    }
    else if (arity == 0) {
        m_manager->raise_exception("no arguments supplied to bit-vector operator");
        return 0;
    }
    else if (!get_bv_size(domain[0], bv_size)) {
        m_manager->raise_exception("could not extract bit-vector size");
        return 0;
    }
    func_decl * r = mk_func_decl(k, bv_size);
    if (r != 0) {
        if (arity != r->get_arity()) {
            m_manager->raise_exception("declared arity mismatches supplied arity");
            return 0;            
        }
        for (unsigned i = 0; i < arity; ++i) {
            if (domain[i] != r->get_domain(i)) {
                m_manager->raise_exception("declared sorts do not match supplied sorts");
                return 0;            
            }
        }
        return r;
    }
    int r_size;
    switch (k) {
    case OP_BIT2BOOL:
        return mk_bit2bool(bv_size, num_parameters, parameters, arity, domain);
    case OP_INT2BV:   
        return mk_int2bv(bv_size, num_parameters, parameters, arity, domain);
    case OP_BV2INT:   
        return mk_bv2int(bv_size, num_parameters, parameters, arity, domain);
    case OP_CONCAT: 
        if (!get_concat_size(arity, domain, r_size))
            m_manager->raise_exception("invalid concat application");
        return m_manager->mk_func_decl(m_concat_sym, arity, domain, get_bv_sort(r_size), 
                                       func_decl_info(m_family_id, k));
    case OP_SIGN_EXT:
        if (!get_extend_size(num_parameters, parameters, arity, domain, r_size))
            m_manager->raise_exception("invalid sign_extend application");
        return m_manager->mk_func_decl(m_sign_extend_sym, arity, domain, get_bv_sort(r_size), 
                                       func_decl_info(m_family_id, k, num_parameters, parameters));
    case OP_ZERO_EXT:
        if (!get_extend_size(num_parameters, parameters, arity, domain, r_size))
            m_manager->raise_exception("invalid zero_extend application");
        return m_manager->mk_func_decl(m_zero_extend_sym, arity, domain, get_bv_sort(r_size), 
                                       func_decl_info(m_family_id, k, num_parameters, parameters));
    case OP_EXTRACT:
        if (!get_extract_size(num_parameters, parameters, arity, domain, r_size))
            m_manager->raise_exception("invalid extract application");
        return m_manager->mk_func_decl(m_extract_sym, arity, domain, get_bv_sort(r_size), 
                                       func_decl_info(m_family_id, k, num_parameters, parameters));
    case OP_ROTATE_LEFT:
        if (arity != 1)
            m_manager->raise_exception("rotate left expects one argument");
        return m_manager->mk_func_decl(m_rotate_left_sym, arity, domain, domain[0], 
                                       func_decl_info(m_family_id, k, num_parameters, parameters));
    case OP_ROTATE_RIGHT:
        if (arity != 1)
            m_manager->raise_exception("rotate right expects one argument");
        return m_manager->mk_func_decl(m_rotate_right_sym, arity, domain, domain[0],
                                       func_decl_info(m_family_id, k, num_parameters, parameters));
    case OP_REPEAT:
        if (arity != 1)
            m_manager->raise_exception("repeat expects one argument");
        if (num_parameters != 1 || !parameters[0].is_int() || parameters[0].get_int() == 0)
            m_manager->raise_exception("repeat expects one nonzero integer parameter");
        if (!get_bv_size(domain[0], bv_size))
            m_manager->raise_exception("repeat expects an argument with bit-vector sort");
        return m_manager->mk_func_decl(m_repeat_sym, arity, domain, get_bv_sort(bv_size * parameters[0].get_int()), 
                                       func_decl_info(m_family_id, k, num_parameters, parameters));
    default:
        return 0;
    }
}

func_decl * bv_decl_plugin::mk_func_decl(decl_kind k, unsigned num_parameters, parameter const * parameters, 
                                       unsigned num_args, expr * const * args, sort * range) {
    int bv_size;
    if (k == OP_INT2BV && get_int2bv_size(num_parameters, parameters, bv_size)) {
        // bv_size is filled in.
    }
    else if (k == OP_BV_NUM) {
        return mk_num_decl(num_parameters, parameters, num_args);
    }
    else if (k == OP_BIT0) {
        return m_bit0;
    }
    else if (k == OP_BIT1) {
        return m_bit1;
    }
    else if (k == OP_CARRY) {
        return m_carry;
    }
    else if (k == OP_XOR3) {
        return m_xor3;
    }
    else if (k == OP_MKBV) {
        return decl_plugin::mk_func_decl(k, num_parameters, parameters, num_args, args, range);
    }
    else if (num_args == 0 || !get_bv_size(args[0], bv_size)) {
        m_manager->raise_exception("operator is applied to arguments of the wrong sort");
        return 0;
    }
    func_decl * r = mk_func_decl(k, bv_size);
    if (r != 0) {
        return r;
    }
    return decl_plugin::mk_func_decl(k, num_parameters, parameters, num_args, args, range);
}

bool bv_decl_plugin::is_value(app* e) const {
    return is_app_of(e, m_family_id, OP_BV_NUM);
}

void bv_decl_plugin::get_offset_term(app * a, expr * & t, rational & offset) const {
    family_id fid = get_family_id();
    if (a->get_num_args() == 2 && is_app_of(a, fid, OP_BADD) && is_app_of(a->get_arg(0), fid, OP_BV_NUM)) {
        unsigned sz;
        func_decl * decl = to_app(a->get_arg(0))->get_decl();
        offset = decl->get_parameter(0).get_rational();
        sz     = decl->get_parameter(1).get_int();
        t      = a->get_arg(1);
        offset = mod(offset, power_of_two(sz));
    }
    else {
        t      = a;
        offset = rational(0);
    }
}

bool bv_decl_plugin::are_distinct(app * a, app * b) const {
#if 1
    // Check for a + k1 != a + k2   when k1 != k2
    rational a_offset;
    expr *   a_term;
    rational b_offset;
    expr *   b_term;
    get_offset_term(a, a_term, a_offset);
    get_offset_term(b, b_term, b_offset);
    TRACE("bv_are_distinct", 
          tout << mk_ismt2_pp(a, *m_manager) << "\n" << mk_ismt2_pp(b, *m_manager) << "\n";
          tout << "---->\n";
          tout << "a: " << a_offset << " + " << mk_ismt2_pp(a_term, *m_manager) << "\n";
          tout << "b: " << b_offset << " + " << mk_ismt2_pp(b_term, *m_manager) << "\n";);
    if (a_term == b_term && a_offset != b_offset)
        return true;
#endif
    return decl_plugin::are_distinct(a, b);
}

void bv_decl_plugin::get_sort_names(svector<builtin_name> & sort_names, symbol const & logic) {
    if (logic == symbol::null)
        sort_names.push_back(builtin_name("bv", BV_SORT));
    sort_names.push_back(builtin_name("BitVec", BV_SORT));
}

void bv_decl_plugin::get_op_names(svector<builtin_name> & op_names, symbol const & logic) {
    op_names.push_back(builtin_name("bit1",OP_BIT1));
    op_names.push_back(builtin_name("bit0",OP_BIT0));
    op_names.push_back(builtin_name("bvneg",OP_BNEG));
    op_names.push_back(builtin_name("bvadd",OP_BADD));
    op_names.push_back(builtin_name("bvsub",OP_BSUB));
    op_names.push_back(builtin_name("bvmul",OP_BMUL));
    op_names.push_back(builtin_name("bvsdiv",OP_BSDIV));
    op_names.push_back(builtin_name("bvudiv",OP_BUDIV));
    op_names.push_back(builtin_name("bvsrem",OP_BSREM));
    op_names.push_back(builtin_name("bvurem",OP_BUREM));
    op_names.push_back(builtin_name("bvsmod",OP_BSMOD));

    op_names.push_back(builtin_name("bvule",OP_ULEQ));
    op_names.push_back(builtin_name("bvsle",OP_SLEQ));
    op_names.push_back(builtin_name("bvuge",OP_UGEQ));
    op_names.push_back(builtin_name("bvsge",OP_SGEQ));
    op_names.push_back(builtin_name("bvult",OP_ULT));
    op_names.push_back(builtin_name("bvslt",OP_SLT));
    op_names.push_back(builtin_name("bvugt",OP_UGT));
    op_names.push_back(builtin_name("bvsgt",OP_SGT));
    op_names.push_back(builtin_name("bvand",OP_BAND));
    op_names.push_back(builtin_name("bvor",OP_BOR));
    op_names.push_back(builtin_name("bvnot",OP_BNOT));
    op_names.push_back(builtin_name("bvxor",OP_BXOR));
    op_names.push_back(builtin_name("bvnand",OP_BNAND));
    op_names.push_back(builtin_name("bvnor",OP_BNOR));
    op_names.push_back(builtin_name("bvxnor",OP_BXNOR));
    op_names.push_back(builtin_name("concat",OP_CONCAT));
    op_names.push_back(builtin_name("sign_extend",OP_SIGN_EXT));
    op_names.push_back(builtin_name("zero_extend",OP_ZERO_EXT));
    op_names.push_back(builtin_name("extract",OP_EXTRACT));
    op_names.push_back(builtin_name("repeat",OP_REPEAT));
    op_names.push_back(builtin_name("bvredor",OP_BREDOR));
    op_names.push_back(builtin_name("bvredand",OP_BREDAND));
    op_names.push_back(builtin_name("bvcomp",OP_BCOMP));
    op_names.push_back(builtin_name("bvshl",OP_BSHL));
    op_names.push_back(builtin_name("bvlshr",OP_BLSHR));
    op_names.push_back(builtin_name("bvashr",OP_BASHR));
    op_names.push_back(builtin_name("rotate_left",OP_ROTATE_LEFT));
    op_names.push_back(builtin_name("rotate_right",OP_ROTATE_RIGHT));
    
    if (logic == symbol::null) {
        op_names.push_back(builtin_name("bvumul_noovfl",OP_BUMUL_NO_OVFL));
        op_names.push_back(builtin_name("bvsmul_noovfl",OP_BSMUL_NO_OVFL));
        op_names.push_back(builtin_name("bvsmul_noudfl",OP_BSMUL_NO_UDFL));
        
        op_names.push_back(builtin_name("bvsdiv0", OP_BSDIV0));
        op_names.push_back(builtin_name("bvudiv0", OP_BUDIV0));
        op_names.push_back(builtin_name("bvsrem0", OP_BSREM0));
        op_names.push_back(builtin_name("bvurem0", OP_BUREM0));
        op_names.push_back(builtin_name("bvsmod0", OP_BSMOD0));
        
        op_names.push_back(builtin_name("bvsdiv_i", OP_BSDIV_I));
        op_names.push_back(builtin_name("bvudiv_i", OP_BUDIV_I));
        op_names.push_back(builtin_name("bvsrem_i", OP_BSREM_I));
        op_names.push_back(builtin_name("bvurem_i", OP_BUREM_I));
        op_names.push_back(builtin_name("bvumod_i", OP_BSMOD_I));
        
        op_names.push_back(builtin_name("ext_rotate_left",OP_EXT_ROTATE_LEFT));
        op_names.push_back(builtin_name("ext_rotate_right",OP_EXT_ROTATE_RIGHT));
        op_names.push_back(builtin_name("int2bv",OP_INT2BV));
        op_names.push_back(builtin_name("bv2int",OP_BV2INT));
        op_names.push_back(builtin_name("mkbv",OP_MKBV));
    }
}

expr * bv_decl_plugin::get_some_value(sort * s) {
    SASSERT(s->is_sort_of(m_family_id, BV_SORT));
    unsigned bv_size = s->get_parameter(0).get_int();
    parameter p[2] = { parameter(rational(0)), parameter(static_cast<int>(bv_size)) };
    return m_manager->mk_app(m_family_id, OP_BV_NUM, 2, p, 0, 0);
}

bv_util::bv_util(ast_manager & m):
    m_manager(m) {
    SASSERT(m.has_plugin(symbol("bv")));
    m_plugin = static_cast<bv_decl_plugin*>(m.get_plugin(m.get_family_id("bv")));
}

rational bv_util::norm(rational const & val, unsigned bv_size, bool is_signed) const {
    rational r = mod(val, power_of_two(bv_size));
    SASSERT(!r.is_neg());
    if (is_signed) {
        if (r >= power_of_two(bv_size - 1)) {
            r -= power_of_two(bv_size);
        }
        if (r < -power_of_two(bv_size - 1)) { 
            r += power_of_two(bv_size);
        }
    }
    return r;
}

bool bv_util::has_sign_bit(rational const & n, unsigned bv_size) const {
    SASSERT(bv_size > 0);
    rational m = norm(n, bv_size, false);
    rational p = power_of_two(bv_size - 1);
    return m >= p;
}

bool bv_util::is_bv_sort(sort const * s) const {
    return (s->get_family_id() == get_fid() && s->get_decl_kind() == BV_SORT && s->get_num_parameters() == 1); 
}

app * bv_util::mk_numeral(rational const & val, sort* s) {
    if (!is_bv_sort(s)) {
        return 0;
    }
    unsigned bv_size = get_bv_size(s);
    return mk_numeral(val, bv_size);
}

app * bv_util::mk_numeral(rational const & val, unsigned bv_size) {
    parameter p1(val);
    parameter p[2] = { p1, parameter(static_cast<int>(bv_size)) };
    return m_manager.mk_app(get_fid(), OP_BV_NUM, 2, p, 0, 0);
}

bool bv_util::is_numeral(expr const * n, rational & val, unsigned & bv_size) const {
    if (!is_app_of(n, get_fid(), OP_BV_NUM)) {
        return false;
    }
    func_decl * decl = to_app(n)->get_decl();
    val     = decl->get_parameter(0).get_rational();
    bv_size = decl->get_parameter(1).get_int();
    return true;
}


bool bv_util::is_allone(expr const * e) const {
    rational r;
    unsigned bv_size;
    if (!is_numeral(e, r, bv_size)) {
        return false;
    }
    bool result = (r == power_of_two(bv_size) - rational(1));
    TRACE("is_allone", tout << r << " " << result << "\n";);
    return result;
}

bool bv_util::is_zero(expr const * n) const {
    if (!is_app_of(n, get_fid(), OP_BV_NUM)) {
        return false;
    }
    func_decl * decl = to_app(n)->get_decl();
    return decl->get_parameter(0).get_rational().is_zero();
}

sort * bv_util::mk_sort(unsigned bv_size) {
    parameter p[1] = { parameter(bv_size) };
    return m_manager.mk_sort(get_fid(), BV_SORT, 1, p);
}


bool bv_util::mult_inverse(rational const & n, unsigned bv_size, rational & result) {
    if (n.is_one()) {
        result = n;
        return true;
    }
    
    if (!mod(n, rational(2)).is_one()) {
        return false;
    }
    
    rational g;
    rational x;
    rational y;
    g = gcd(n, power_of_two(bv_size), x, y);
    if (x.is_neg()) {
        x = mod(x, power_of_two(bv_size));
    }
    SASSERT(x.is_pos());
    SASSERT(mod(x * n, power_of_two(bv_size)).is_one());
    result = x;
    return true;
}

app * bv_util::mk_bv2int(expr* e) {
    sort* s = m_manager.mk_sort(m_manager.get_family_id("arith"), INT_SORT);
    parameter p(s);
    return m_manager.mk_app(get_fid(), OP_BV2INT, 1, &p, 1, &e); 
}