-
Notifications
You must be signed in to change notification settings - Fork 10
/
BitVector.fs
1673 lines (1424 loc) · 59.9 KB
/
BitVector.fs
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
module BitVector
open System
open System.Numerics
open System.Collections
open System.Collections.Generic
open Microsoft.Z3
open GlobalOptions
open Bit
open Util
type BString = (int*Bit) list
//BitVector Indexing
//|size - 1| size - 2 | ...| 2 | 1 | 0 |
//
type BitVector (size:int) =
member val Length = size:int with get, set
member val Bits = [(size, U)] with get, set
// CMW: reactivate member val for isConcrete instead of member r.?
member r.isConcreteValue = if not r.isValid then
false
else
not (List.exists (fun (n, b) -> n <= 0 || not (isConcrete b)) r.Bits)
member r.isPartiallyConcreteValue = if not r.isValid then
false
else
List.exists (fun (n, b) -> n <= 0 || (isConcrete b)) r.Bits
member r.isFullyUndefined = (r.Bits.Length = 1 && (snd r.Bits.Head) = U)
member r.isValid = r.Length <> 0
member r.isInvalid = not r.isValid
member r.markInvalid = r.Bits <- [(0, U)] ; r.Length <- 0
static member Invalid =
let res = (BitVector 0)
assert not res.isValid
res
static member Copy (bv:BitVector) =
let cpy = BitVector bv.Length
cpy.setBits bv.Bits
cpy
member r.CheckInvariant () =
if r.Length = 0 then
assert (r.Bits.Length = 1)
let cnt = (fst r.Bits.[0])
let bit = (snd r.Bits.[0])
assert (cnt = 0)
else
r.CheckNonbits()
r.CheckZeros()
r.CheckCompactness()
member r.CheckCompactness () =
// CMW: (snd x) <> N in the function definition below means bit-vectors should
// never have any N bits in them. Unclear whether this is what we want.
// AZ: Compactness should be that no two consecutive elements have the same Bit value
// I.e., (1, Zero) :: (1:Zero)::[] shouldn't occur, since (2,Zero)::[] is the
// cannon representation
let r = (fst (List.fold (fun a x -> let q = (snd x) <> N &&
((snd a) = N || (snd x) <> (snd a))
((q && (fst a)), (snd x)))
(true, N)
r.Bits))
assert(r)
member r.CheckZeros () =
let r = (List.fold (fun a x -> a || (fst x) = 0) false r.Bits)
assert(not r)
member r.CheckNonbits () =
let r = List.fold (fun a (i, x) -> (a || x = N) ) false r.Bits
assert(not r)
member r.SetFromBitTuples (l:(int * Bit) list) =
let f = (fun (i, b) (acc : (int * ((int*Bit) list))) ->
let curl = fst acc
let curlist = snd acc
match curlist with
| h::t -> let h = curlist.Head
if b = (snd h) then
(curl + i, ((fst h) + i, b) :: curlist.Tail)
else
(curl + i, (List.append [(i, b)] curlist))
| [] -> (i, [i, b]))
let (length, bits) = List.foldBack f l (0, [])
r.Length <- length
r.Bits <- bits
if (DBG) then r.CheckInvariant()
r
override r.ToString () =
let mutable res = ""
for i in 0 .. r.Bits.Length - 1 do
if (fst r.Bits.[i]) = 0 then
res <- res + "*" + (bitToString (snd r.Bits.[i])) + "*"
else
let (cnt, bit) = r.Bits.[i]
if cnt > BV_SEGMENT_STRING_LIMIT then
let b = (bitToString bit)
res <- res + b + ".." + b
else
for j in 1 .. cnt do
res <- res + (bitToString bit)
res
member r.setBits (bString: BString, ?isConcrete:bool) =
r.Bits <- bString
r.CheckInvariant()
member r.isMoreConcreteThan (other:BitVector) =
let i = (BitVector.Intersect r other) :> BitVector
i.isValid && (BitVector.bvEQ r i) && not (BitVector.bvEQ i other)
member r.Contains (value:BitVector) =
(BitVector.Intersect r value).isValid
member r.StartsWith (b:Bit) =
r.Bits.Length > 0 &&
(fst r.Bits.[0]) > 0 &&
(snd r.Bits.[0]) = b
member r.LeadingZeros =
if r.isInvalid then
0
else
match (snd r.Bits.Head) with
| Bit.Zero -> (fst r.Bits.Head)
| Bit.One -> 0
| Bit.U -> 0 // CMW: Do we want to count this as "possibly 0"?
| Bit.N -> assert(false) ; 0
override this.GetHashCode () =
let mutable res = this.Bits.Length
for i in 0 .. this.Bits.Length - 1 do
let bit = (snd this.Bits.[i])
res <- (res <<< 1) ^^^ (match bit with U -> 3 | Bit.One -> 2 | _ -> 1)
res
override this.Equals obj =
if obj = null then
false
else match obj with
| :? BitVector as other ->
if this.Length <> other.Length || this.Bits.Length <> other.Bits.Length then
false
else
assert(this.Length = other.Length && this.Bits.Length = other.Bits.Length)
let mutable res = 0
let f = fun a (xi, xb) (yi, yb) -> a && xi = yi && xb = yb
List.fold2 f true this.Bits other.Bits
| _ -> false
interface IComparable<BitVector> with
member this.CompareTo(other:BitVector) =
if this.Length < other.Length then
-1
elif this.Length > other.Length then
+1
elif this.Bits.Length < other.Bits.Length then
-1
elif this.Bits.Length > other.Bits.Length then
+1
else
assert(this.Length = other.Length && this.Bits.Length = other.Bits.Length)
let f = fun a (xi, xb) (yi, yb) -> if a <> 0 then a
elif xi < yi || xb < yb then -1
elif xi > yi || xb > yb then +1
else 0
List.fold2 f 0 this.Bits other.Bits
interface IComparable with
member this.CompareTo obj =
if obj = null then
1
else
match obj with
| :? BitVector as other -> (this :> IComparable<_>).CompareTo other
| _ -> invalidArg "obj" "not a BitVector"
interface IEquatable<BitVector> with
member this.Equals (other:BitVector) =
this.Equals other
//TODO: Make binary search steps?
static member int2BV (num:int64) (bvSize:int) =
let mutable result = []
let mutable counter = 1
let mutable current = num &&& 1L
for i in 1 .. bvSize - 1 do
if num &&& (1L <<< i) = (current <<< i) then
counter <- counter + 1
else
result <- (counter, bitFromInt current) :: result
counter <- 1
current <- 1L - current //Equivalent to Not, Numbers do not contain Undefined bits
result <- (counter , bitFromInt current) :: result
let bv = BitVector bvSize
bv.Bits <- result
bv.CheckInvariant()
bv
static member private bigint2BV (num:BigInteger) (bvSize:int) =
let mutable result = []
let mutable counter = 1
let mutable current = num &&& BigInteger.One
for i in 1 .. bvSize - 1 do
if num &&& (BigInteger.One <<< i) = (current <<< i) then
counter <- counter + 1
else
result <- (counter, bitFromBigInt current) :: result
counter <- 1
current <- BigInteger.One - current //Equivalent to Not, Numbers do not contain Undefined bits
result <- (counter , bitFromBigInt current) :: result
let bv = BitVector bvSize
bv.Bits <- result
bv.CheckInvariant()
bv
static member powerOf2 (m:int) (size:int) =
assert (m < size)
let res = BitVector size
let pre = if size - m - 1 > 0 then [(size - m - 1,Zero)] else []
let post = if m > 0 then [(m,Zero)] else []
res.setBits (pre @ ((1,One)::post),true)
res.CheckInvariant()
res
static member powerOf2Minus1 (m:int) (size:int) =
assert ( 0 < m && m < size )
let res = BitVector size
let pre = if m - size > 1 then [(m-size,Zero)] else []
res.setBits (pre @ [(m - 1,One)],true)
res.CheckInvariant()
res
static member BitOne =
let res = BitVector 1
res.setBits ([(1,One)], true)
res
static member BitZero =
let res = BitVector 1
res.setBits ([(1,Zero)], true)
if (DBG) then res.CheckInvariant()
res
static member BitVectorFromExpr (e:Expr) =
assert (e.FuncDecl.DeclKind = Z3_decl_kind.Z3_OP_BNUM)
let bvNum = e :?> BitVecNum
let res = BitVector.bigint2BV bvNum.BigInteger (int bvNum.SortSize)
res.CheckInvariant()
res
member private r.toBigInt () =
assert (r.isConcreteValue)
assert (r.Length > 0)
let mutable res = BigInteger.Zero
let mutable pos = r.Length - 1
for el in r.Bits do
match el with
| (n, One) ->
for k in 0 .. n - 1 do
res <- res + (BigInteger.One <<< (pos - k) )
pos <- pos - n
| (n, Zero) -> pos <- pos - n
| _ -> printf "Error in BitVector.toInt()"
assert (false)
res
member private r.toBigIntEncoding () =
assert (r.Length > 0)
let mutable res = BigInteger.Zero
let mutable pos = r.Length - 1
for el in r.Bits do
match el with
| (n, One) ->
for k in 0 .. n - 1 do
res <- res + (BigInteger.One <<< (pos - k) )
pos <- pos - n
| (n, U) ->
for k in 0 .. n - 1 do
res <- res + (BigInteger.One <<< (r.Length + pos - k) )
pos <- pos - n
| (n, Zero) -> pos <- pos - n
| _ -> printf "Error in BitVector.toInt()"
assert (false)
res
static member padHighest (bit:Bit) (n:int) (bv:BitVector) =
assert (bv.Length > 0 && n > 0)
let res = BitVector (n + bv.Length)
let (hCnt, hBit) = bv.Bits.Head
let newHighest = if hBit = bit then
(n + hCnt, bit)
else
(n, bit)
if fst newHighest = n then
res.setBits (newHighest::bv.Bits, bv.isConcreteValue)
else
res.setBits (newHighest::(bv.Bits.Tail), bv.isConcreteValue)
res.CheckInvariant()
if (DBG) then res.CheckInvariant()
res
static member padLowest (bit:Bit) (n:int) (bv:BitVector) =
assert (bv.Length > 0 && n >= 0)
let res = BitVector (n + bv.Length)
let mutable revBits = List.rev bv.Bits
let (lCnt,lBit) = revBits.Head
if (bit = lBit) then
res.setBits (List.rev ((lCnt + n, lBit)::(revBits.Tail)), bv.isConcreteValue)
else
res.setBits (List.rev ((n,bit)::revBits), bv.isConcreteValue)
if (DBG) then res.CheckInvariant()
res
static member padHighestZero n bv =
BitVector.padHighest Zero n bv
static member padHighestOne n bv =
BitVector.padHighest One n bv
static member padHighestZeroToLen (len:int) (bv:BitVector) =
assert (len >= bv.Length)
BitVector.padHighest Zero (len - bv.Length) bv
static member padHighestUToLen (len:int) (bv:BitVector) =
assert (len >= bv.Length)
BitVector.padHighest U (len - bv.Length) bv
static member padHighestOneToLen (len:int) (bv:BitVector) =
assert (len >= bv.Length)
BitVector.padHighest One (len - bv.Length) bv
static member padLowestZero n bv =
BitVector.padLowest Zero n bv
static member padLowestOne n bv =
BitVector.padLowest One n bv
static member padLowestZeroToLen (len:int) (bv:BitVector) =
assert (len >= bv.Length)
BitVector.padLowest Zero (len - bv.Length) bv
static member padLowestOneToLen (len:int) (bv:BitVector) =
assert (len >= bv.Length)
BitVector.padLowest One (len - bv.Length) bv
static member TakeHighest (k:int) (bv:BitVector) =
assert (k <= bv.Length)
let mutable srcBits = bv.Bits
if k = bv.Length then
bv
elif k > bv.Length then
UNREACHABLE("Unexpected extraction")
else
let mutable bits = []
let mutable len = 0
while len < k do
let cnt = fst srcBits.Head
let bit = snd srcBits.Head
let nbits = if len + cnt > k then k - len else cnt
len <- len + nbits
bits <- List.append bits [(nbits, bit)]
srcBits <- srcBits.Tail
assert(len = k)
let mutable res = BitVector.Invalid
res <- (res.SetFromBitTuples bits)
res.CheckInvariant()
res
static member TakeLowest (k:int) (bv:BitVector) =
assert (k <= bv.Length)
if k = bv.Length then
bv
elif k > bv.Length then
UNREACHABLE("Illegal extraction")
else
let mutable srcBits = (List.rev bv.Bits)
let mutable bits = []
let mutable len = 0
while len < k do
let cnt = fst srcBits.Head
let bit = snd srcBits.Head
let nbits = if len + cnt > k then k - len else cnt
len <- len + nbits
bits <- (nbits, bit) :: bits
srcBits <- srcBits.Tail
assert(len = k)
let mutable res = BitVector.Invalid
res <- (res.SetFromBitTuples bits)
res.CheckInvariant()
res
static member DropHighest (k:int) (bv:BitVector) =
if k = 0 then
bv
elif k >= bv.Length then
// CMW: This would create an empty bit-vector BitVector(0),
// which seems to be undesirable judging by other pieces of code.
assert(false)
BitVector (0)
else
assert (0 < k && k < bv.Length)
let res = BitVector (bv.Length - k)
let mutable srcBits = bv.Bits
let mutable resBits = []
let mutable dropped = 0
let mutable bit = N
while dropped < k do
let nhead = fst srcBits.Head
bit <- snd srcBits.Head
if (dropped + nhead > k) then
let ndrop = k - dropped
dropped <- dropped + ndrop
srcBits <- (nhead-ndrop, bit)::srcBits.Tail
else
dropped <- dropped + nhead
srcBits <- srcBits.Tail
res.setBits srcBits
res.CheckInvariant()
res
static member meld (nonConcrete:BitVector) (concrete:BitVector) =
// Sequentially replace U bits in nonConcrete with bits from concrete.
assert (not nonConcrete.isConcreteValue)
assert (concrete.isConcreteValue)
assert (nonConcrete.estimateSearchSpace() = concrete.Length)
let mutable src = concrete.Bits
let mutable rev_res = []
let mutable last_cnt = 0
let mutable last_bit = N
let mutable cncrete_cnt = 0
let mutable bit = N
for p in nonConcrete.Bits do
match p with
| (cnt, U) ->
let mutable todo = cnt
while todo > 0 do
assert(cncrete_cnt >= 0)
assert(cncrete_cnt = 0 || bit <> N)
if cncrete_cnt = 0 then
cncrete_cnt <- fst src.Head
bit <- snd src.Head
src <- src.Tail
// Determine how many concrete bits we are instantiating, and if any are left over.
let inst_cnt = if cncrete_cnt > todo then
cncrete_cnt <- cncrete_cnt - todo
todo
else
let c = cncrete_cnt
cncrete_cnt <- 0
c
// If the bit matches the previous one, increase its count
// otherwise, insert last bit-cnt pair and record current pair as the last
if last_bit = bit then
last_cnt <- last_cnt + inst_cnt
else
if last_cnt > 0 then
assert (last_bit <> N)
rev_res <- (last_cnt, last_bit) :: rev_res
last_bit <- bit
last_cnt <- inst_cnt
// Record how many bits we've instantiated this iteration
todo <- todo - inst_cnt
| (cnt, One)
| (cnt, Zero) ->
let bit = snd p
if last_bit = bit then
last_cnt <- last_cnt + cnt
else
if last_cnt > 0 then
assert (last_bit <> N)
rev_res <- (last_cnt, last_bit) :: rev_res
last_bit <- bit
last_cnt <- cnt
| (_,N)-> assert(false)
if last_cnt > 0 then
rev_res <- (last_cnt, last_bit) :: rev_res
let res = BitVector.Invalid.SetFromBitTuples (List.rev rev_res)
res.CheckInvariant()
assert (res.Length = nonConcrete.Length)
//assert (BitVector.bvEQ (BitVector.Unify res nonConcrete) nonConcrete)
res
// Changes #infix_len bits starting from #start_position
// If strictly flag is true, then it changes only the U bits (if any)
// otherwise, it will overwrite the concrete bits in the affected range.
static member changeBits (strictly:bool) (set_to:Bit) (bv:BitVector) (start_position:int) (infix_len:int) =
assert ( start_position + 1 >= infix_len) //From any given position, we can change at most that many bits plus one (due to counting from zero)
assert ( 0 <= start_position && start_position < bv.Length)
assert (infix_len > 0)
let prefix_len = bv.Length - 1 - start_position
let suffix_len = bv.Length - prefix_len - infix_len
assert (prefix_len >= 0)
assert (suffix_len >= 0)
assert (bv.Length = prefix_len + infix_len + suffix_len)
assert (not strictly || set_to = One || set_to = Zero)
let prefix = if prefix_len > 0 then
Some (BitVector.TakeHighest prefix_len bv)
else
None
let infix_o = BitVector.TakeHighest infix_len (BitVector.DropHighest prefix_len bv)
let infix = if not strictly then
match set_to with
| Bit.One -> BitVector.AllOne infix_len
| Bit.Zero -> BitVector.AllZero infix_len
| Bit.U -> BitVector infix_len
| _ -> UNREACHABLE "Introducing N bit"
else
match set_to with
| Bit.One -> infix_o.Maximum
| Bit.Zero -> infix_o.Minimum
| Bit.U -> infix_o
| _ -> UNREACHABLE "Introducing N bit"
let suffix = if suffix_len > 0 then
Some (BitVector.DropHighest (prefix_len + infix_len) bv)
else
None
let highEnd = if prefix.IsSome then
BitVector.bvConcat prefix.Value infix
else
infix
let res = if suffix.IsSome then
BitVector.bvConcat highEnd suffix.Value
else
highEnd
res
static member private changeKthBit (bv:BitVector) (k:int) (cond) (init)=
assert (0 <= k && k < bv.Length)
let suffix = BitVector.DropHighest (bv.Length - (k + 1) ) bv
let kthBit = snd suffix.Bits.Head
if cond kthBit then
let prefix = if bv.Length - 1 - k > 0 then
Some (BitVector.TakeHighest (bv.Length - 1 - k) bv)
else
None
let infix = init
let suffix = if k > 0 then
Some (BitVector.DropHighest (bv.Length - k) bv)
else
None
let res = ( match prefix with
| Some p -> BitVector.bvConcat p
| None -> fun x -> x )
<|
( match suffix with
| Some s -> BitVector.bvConcat infix s
| None -> infix)
Some res
else
None
static member changeKthBitDownwards (bv:BitVector) (k:int)=
assert (bv.isConcreteValue)
BitVector.changeKthBit bv k (fun (x:Bit) -> x = One) (BitVector.BitZero)
static member changeKthBitToU (bv:BitVector) (k:int) =
BitVector.changeKthBit bv k (fun (x:Bit) -> x = Zero || x = One) (BitVector 1)
static member changeKthBitUpwards (bv:BitVector) (k:int) =
assert (bv.isConcreteValue)
BitVector.changeKthBit bv k (fun (x:Bit) -> x = Zero) (BitVector.BitOne)
static member getCommonPrefix (aBV:BitVector) (bBV:BitVector) =
assert(aBV.Length > 0 && bBV.Length > 0)
assert(aBV.Length = bBV.Length)
assert(aBV.Bits <> [] && bBV.Bits <> [])
assert(aBV.isConcreteValue)
assert(bBV.isConcreteValue)
let mutable a = aBV.Bits
let mutable b = bBV.Bits
let mutable res = []
let mutable total = aBV.Length
let mutable todo = aBV.Length
let mutable did = 0
let mutable finished = false //AZ: This will be set to false if an N bit appears in the result
while todo > 0 && not finished do
let mutable (aCnt, aBit) = a.Head
let mutable (bCnt, bBit) = b.Head
assert(aBit <> N && bBit <> N)
let curCnt = min aCnt bCnt
match (aBit, bBit) with
| (One,One)
| (Zero,Zero) -> res <- List.append res [(curCnt, aBit)]
todo <- todo - curCnt
did <- did + curCnt
| (N,_)
| (_,N) -> assert(false)
| _ ->
res <- List.append res [(todo, U)]
did <- did + todo
todo <- 0
finished <- true
if (aCnt = curCnt) then a <- a.Tail else a <- (aCnt - curCnt, aBit)::a.Tail
if (bCnt = curCnt) then b <- b.Tail else b <- (bCnt - curCnt, bBit)::b.Tail
assert(todo = 0 && did = total)
let r = BitVector.Invalid.SetFromBitTuples res
if (DBG) then r.CheckInvariant()
r
// Takes a binary bit operation and applies it to two bit-vectors
// bitOp should return N bit if an error occurs,
// Bitwise whould return a zero length (non) Bitvector then.
static member Bitwise (bitOp: Bit -> Bit -> Bit) (aBV:BitVector) (bBV:BitVector) =
assert(aBV.Length > 0 && bBV.Length > 0)
assert(aBV.Length = bBV.Length)
assert(aBV.Bits <> [] && bBV.Bits <> [])
let mutable a = aBV.Bits
let mutable b = bBV.Bits
let mutable res = []
let mutable total = aBV.Length
let mutable todo = aBV.Length
let mutable did = 0
let mutable valid = true //AZ: This will be set to false if an N bit appears in the result
while todo > 0 && valid do
let mutable (aCnt, aBit) = a.Head
let mutable (bCnt, bBit) = b.Head
assert(aBit <> N && bBit <> N)
let curCnt = min aCnt bCnt
let curBit = bitOp aBit bBit
if curBit = N then
valid <- false //Result is invalid, stop the computation
res <- List.append res [(curCnt, curBit)]
if (aCnt = curCnt) then a <- a.Tail else a <- (aCnt - curCnt, aBit)::a.Tail
if (bCnt = curCnt) then b <- b.Tail else b <- (bCnt - curCnt, bBit)::b.Tail
todo <- todo - curCnt
did <- did + curCnt
assert( (todo = 0 && did = total) || not valid)
if valid then
let r = BitVector.Invalid.SetFromBitTuples res
if (DBG) then r.CheckInvariant()
r
else
BitVector.Invalid
static member Intersect (aBV:BitVector) (bBV:BitVector) =
BitVector.Bitwise Intersect aBV bBV
static member bvAND (aBV:BitVector) (bBV:BitVector) =
BitVector.Bitwise And aBV bBV
static member revBvAND (aBV:BitVector) (bBV:BitVector) =
BitVector.Bitwise (fun a r -> if a = Zero then Bit.U else r) aBV bBV
static member revBvOr (aBV:BitVector) (bBV:BitVector) =
BitVector.Bitwise (fun x y -> if ((x = One && y = Zero) || (x = Zero && y = One)) then Bit.One else Bit.U) aBV bBV
static member revBvXOr (aBV:BitVector) (bBV:BitVector) =
BitVector.Bitwise (fun x y -> if ((x = Zero && y = One) || (x = One && y = Zero)) then Bit.One
elif ((x = Zero && y = Zero) || (x = One && y = One)) then Bit.Zero
else Bit.U) aBV bBV
static member bvOR (aBV:BitVector) (bBV:BitVector) =
BitVector.Bitwise Or aBV bBV
static member bvXOR (aBV:BitVector) (bBV:BitVector) =
BitVector.Bitwise XOr aBV bBV
member r.estimateSearchSpace () =
List.fold (fun a pair -> if (snd pair) = Bit.U then a + (fst pair) else a) 0 r.Bits
member r.Minimum =
assert(r.Length <> 0)
if r.isConcreteValue then
r
else
let f = (fun (i:int, x:Bit) (acc:(int * Bit) list) ->
match x with
| U -> if (acc.Length > 0) && ((snd acc.Head) = Zero) then
((fst acc.Head) + i, Zero) :: acc.Tail
else
(i, Zero) :: acc
| Zero -> if (acc.Length > 0) && ((snd acc.Head) = Zero) then
((fst acc.Head) + i, Zero) :: acc.Tail
else
(i, Zero) :: acc
| _ -> (i, x) :: acc)
let new_rbits = List.foldBack f r.Bits []
let res = BitVector r.Length
res.setBits new_rbits //r.Bits <- new_rbits
if (DBG) then res.CheckInvariant()
assert (res.isConcreteValue)
res
member r.Maximum =
assert(r.Length <> 0)
if r.isConcreteValue then
r
else
let f = (fun (i:int, x:Bit) (acc:(int * Bit) list) ->
match x with
| U -> if (acc.Length > 0) && ((snd acc.Head) = One) then
((fst acc.Head) + i, One) :: acc.Tail
else
(i, One) :: acc
| One -> if (acc.Length > 0) && ((snd acc.Head) = One) then
((fst acc.Head) + i, One) :: acc.Tail
else
(i, One) :: acc
| _ -> (i, x) :: acc)
let new_rbits = List.foldBack f r.Bits []
let res = BitVector r.Length
res.setBits new_rbits //r.Bits <- new_rbits
if (DBG) then res.CheckInvariant()
assert (res.isConcreteValue)
res
member r.lowestSignedValueFromSet () =
if r.isConcreteValue then
r
elif snd r.Bits.Head = U then
let (cnt,bit) = r.Bits.Head
let res = BitVector r.Length
let bits = if cnt > 1 then
(1,One)::(cnt-1,U)::r.Bits.Tail
else
(1,One)::r.Bits.Tail
res.setBits (List.map (fun (x,y) -> if y = U then (x,Zero) else (x,y)) bits, true)
if (DBG) then res.CheckInvariant()
res
else
let res = BitVector r.Length
res.setBits ((List.map (fun (x,y) -> if y = U then (x,Zero) else (x,y)) r.Bits), true)
if (DBG) then res.CheckInvariant()
res
member r.highestSignedValueFromSet () =
if r.isConcreteValue then
r
elif snd r.Bits.Head = U then
let (cnt,bit) = r.Bits.Head
let res = BitVector r.Length
let bits = if cnt > 1 then
(1,Zero)::(cnt-1,U)::r.Bits.Tail
else
(1,Zero)::r.Bits.Tail
res.setBits (List.map (fun (x,y) -> if y = U then (x,Zero) else (x,y)) bits, true)
if (DBG) then res.CheckInvariant()
res
else
let res = BitVector r.Length
res.setBits ((List.map (fun (x,y) -> if y = U then (x, One) else (x,y)) r.Bits), true)
if (DBG) then res.CheckInvariant()
res
//member r.medUpValueFromSet () = Alternate zeroes and ones from undef to undef bit, maybe if done in chunks?
// This will end up exploding the encoding
static member bvShiftLeftInt (bv:BitVector) (n:int) =
assert (n > 0)
let mutable res = BitVector bv.Length
for i in 0 .. n - 1 do
match bv.Bits.Head with
| (n, b) when n > 1 -> res.Bits <- (n-1, b) :: bv.Bits.Tail
| (n, b) when n = 1 -> res.Bits <- bv.Bits.Tail
| _ -> ()
let last = res.Bits.Item (res.Bits.Length - 1)
match last with
| (n, Zero) -> res.Bits <- List.rev ((n+1, Zero) :: (List.rev res.Bits).Tail)
| _ -> res.Bits <- List.append res.Bits [(1, Zero)]
res.CheckInvariant()
res
static member private bvShiftSkeleton (shiftF) (bv:BitVector) (n:BitVector) =
if not n.isConcreteValue then
BitVector bv.Length // Undef.
else
let mutable pos = n.Length
let mutable res = bv
for (cnt, bit) in n.Bits do
match bit with
| One ->
assert (pos >= cnt)
//AZ: We only care about stretches of ones and their value is 2^(fst_pos + 1) - 2 ^ last_pos
res <- shiftF res ( (1 <<< pos) - (1 <<< (pos - cnt)))
| Zero -> ()
| _ -> UNREACHABLE("bvAShiftRight: U or N bit in the shift operand")
pos <- pos - cnt
if (DBG) then res.CheckInvariant()
res
static member _bvShiftLeft (bv:BitVector) (n:int) =
assert ( 0 <= n )
if n = 0 then
bv
elif n >= bv.Length then
BitVector.AllZero bv.Length
else
assert(n <=System.Int32.MaxValue)
let res = BitVector.padLowestZeroToLen bv.Length (BitVector.DropHighest n bv)
if (DBG) then res.CheckInvariant()
res
static member bvShiftLeft (bv:BitVector) (n:BitVector) =
if BitVector.isAllZero bv || BitVector.isAllZero n then
bv
else
BitVector.bvShiftSkeleton (BitVector._bvShiftLeft) bv n
static member _bvLShiftRight (bv:BitVector) (n:int) =
assert ( 0 <= n && n <= bv.Length)
if n = 0 then
bv
elif n >= bv.Length then
BitVector.AllZero bv.Length
else
assert(n <=System.Int32.MaxValue)
let res = BitVector.padHighestZeroToLen bv.Length (BitVector.TakeHighest (bv.Length - n) bv)
if (DBG) then res.CheckInvariant()
res
static member bvLShiftRight (bv:BitVector) (n:BitVector) =
if BitVector.isAllZero bv || BitVector.isAllZero n then
bv
else
BitVector.bvShiftSkeleton (BitVector._bvLShiftRight) bv n
static member _bvAShiftRight (bv:BitVector) (n:int) =
assert ( 0 <= n && n <= bv.Length)
if n = 0 then
bv
elif n >= bv.Length then
let res = BitVector bv.Length
res.Bits <- [(bv.Length, (snd bv.Bits.Head))]
res
else
assert(n <=System.Int32.MaxValue)
let bit = snd (bv.Bits.Head)
let res = match bit with
| One -> BitVector.padHighestOneToLen bv.Length (BitVector.TakeHighest (bv.Length - n) bv)
| Zero -> BitVector.padHighestZeroToLen bv.Length (BitVector.TakeHighest (bv.Length - n) bv)
| U -> BitVector.padHighestUToLen bv.Length (BitVector.TakeHighest (bv.Length - n) bv)
| _ -> UNREACHABLE "N bit in a BV"
if (DBG) then res.CheckInvariant()
res
static member bvAShiftRight (bv:BitVector) (n:BitVector) =
if BitVector.isAllOnes bv || BitVector.isAllZero bv || BitVector.isAllZero n then
bv
else
BitVector.bvShiftSkeleton (BitVector._bvAShiftRight) bv n
static member bvExtract (u:int) (l:int) (aBV:BitVector) =
assert (0 <= l)
assert (l <= u) //Zero extractions are disallowed
assert (u <= aBV.Length)
let res = BitVector.TakeHighest (u - l + 1) (BitVector.DropHighest (aBV.Length - u - 1) aBV)
if (DBG) then res.CheckInvariant()
res
static member bvConcat (aBV:BitVector) (bBV:BitVector) =
assert (aBV.Length > 0 && bBV.Length > 0)
let a = aBV.Bits
let b = bBV.Bits
let mutable res = BitVector.Invalid //result in reverse order
res.Bits <- []
let (bFirstCnt, bFirstBit) = b.Head
let (aLastCnt, aLastBit) = (List.rev a).Head
if aLastBit = bFirstBit then
let fuseEl = (aLastCnt + bFirstCnt, aLastBit)
res.Bits <- (List.rev (List.rev a).Tail) @ (fuseEl :: b.Tail)
else
res.Bits <- a @ b
res.Length <- aBV.Length + bBV.Length
res.CheckInvariant()
res
static member bvRepeat (n:int) (aBV:BitVector) =
assert (n > 0) //Q: Repeat 1 should be simplified?
let a = aBV.Bits
let mutable res = BitVector.Invalid
let (firstCnt, firstBit) = a.Head
let revA = List.rev a
let (lastCnt, lastBit) = revA.Head
res.Bits <- []
if a.Length = 1 then
res.Bits <- ( n * firstCnt, firstBit) :: res.Bits
else if firstBit = lastBit then
//|first|middle|last|first|middle|last|first|middle|last|
//|first|middle| fuse |middle| fuse | tail |
let fuseEl = (firstCnt + lastCnt, firstBit)
let middle = List.rev (revA.Tail)
res.Bits <- a.Tail
for i in 1 .. n - 1 do
res.Bits <- fuseEl :: res.Bits
res.Bits <- middle @ res.Bits
res.Bits <- a.Head :: res.Bits
else
for i in 1 .. n do
res.Bits <- a @ res.Bits
res.Length <- aBV.Length * n
res.CheckInvariant()
res
static member bvNot (aBV:BitVector) =
let res = BitVector aBV.Length
res.setBits (List.map (fun (x,y) -> (x, Not y)) aBV.Bits, aBV.isConcreteValue)
res.CheckInvariant()
res
static member bvSub aBV bBV =
BitVector.bvAdd aBV (BitVector.bvNegate bBV)
//BVADD in triLogic: a,b,c \in {One,Zero,U}
// bvAdd (len, a) (len, b) c
//Group I: a = not b, c <> U
// c' = c, r = (len, not c) //Can be unified with Group IV
//Group II: a = b, c = Not a | U
// c' = a, r = [(len-1, a); (1,c)]
//Group III: a = b = c
// c' = c, r = c
//Group IV: exactly one a or b is U, c <> U
// c' = c, r = (len, U)
//Group V: rest
// c' = U, r = (len, U)