Source file binary_test.ml
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
module type S = sig
include Sva_sig.BITVECTOR;;
val concretize: bitvector -> Lattices.BVSet.t
val abstract: size:int -> Lattices.BVSet.t -> bitvector
val pretty: Format.formatter -> bitvector -> unit
val parse_string: string -> bitvector
end
module Collecting = struct
include Binary_collecting
let equal = Bitvector_Lattice.equal
let subset = Lattices.BVSet.ZSet.subset
let inter = Bitvector_Lattice.inter ~size:(Units.In_bits.of_int (-1))
end
module Quadrivalent = Lattices.Quadrivalent
module Test(Implem:S) = struct
module StringSet = Set.Make(String);;
let rec all n =
if n == 0 then StringSet.singleton ""
else
let allprev = all (n - 1) in
let cat a b = String.concat "" [a;b] in
StringSet.union (StringSet.map (cat "0") allprev) @@
StringSet.union (StringSet.map (cat "1") allprev) @@
(StringSet.map (cat "?") allprev)
;;
let test_soundness1 absop concop a =
Collecting.subset (concop (Implem.concretize a))
(Implem.concretize (absop a)) || failwith "Soundness check failed"
;;
let test_completeness1 absop concop a =
Collecting.equal (concop (Implem.concretize a))
(Implem.concretize (absop a)) || failwith "Completeness check failed"
;;
let test_best_transformer1 ~size absop concop a =
Collecting.equal (Implem.concretize @@ Implem.abstract ~size @@ concop (Implem.concretize a))
(Implem.concretize (absop a)) || failwith "Maximally precise check failed"
;;
let test_soundness2 absop concop a b =
Collecting.subset (concop (Implem.concretize a) (Implem.concretize b))
(Implem.concretize (absop a b)) ||
true
;;
let test_soundness2_pred absop concop a b =
Quadrivalent.includes (absop a b) (concop (Implem.concretize a) (Implem.concretize b))
|| failwith "Soundness check failed"
;;
let test_completeness2 absop concop a b =
Collecting.equal (concop (Implem.concretize a) (Implem.concretize b))
(Implem.concretize (absop a b)) || failwith "Completeness check failed"
;;
let test_completeness2_pred absop concop a b =
Quadrivalent.equal (concop (Implem.concretize a) (Implem.concretize b))
(absop a b) || failwith (Format.asprintf "Completeness/best transformer check failed: %a op %a = %a, best result is %a"
Implem.pretty a Implem.pretty b Quadrivalent.pretty (absop a b)
Quadrivalent.pretty (concop (Implem.concretize a) (Implem.concretize b)))
;;
let test_best_transformer2 ~size absop concop a b =
Collecting.equal (Implem.concretize @@ Implem.abstract ~size @@ concop (Implem.concretize a) (Implem.concretize b))
(Implem.concretize (absop a b)) || failwith (Format.asprintf "Maximal precision check failed: %a op %a returns %a, optimal result is %a"
Implem.pretty a Implem.pretty b Implem.pretty (absop a b)
Implem.pretty (Implem.abstract ~size @@ concop (Implem.concretize a) (Implem.concretize b)))
;;
let all_test_f1 f bits absop concop =
(all bits) |> StringSet.iter (fun a ->
let a = Implem.parse_string a in
assert(f absop concop a));;
let all_test_f2 f bits absop concop =
(all bits) |> StringSet.iter (fun a ->
(all bits) |> StringSet.iter (fun b ->
let a = Implem.parse_string a in
let b = Implem.parse_string b in
assert(f absop concop a b)));;
let all_test_f2' f (bitsa,bitsb) absop concop =
(all bitsa) |> StringSet.iter (fun a ->
(all bitsb) |> StringSet.iter (fun b ->
let a = Implem.parse_string a in
let b = Implem.parse_string b in
assert(f absop concop a b)));;
let all_test_f3 f (bitsa,bitsb,bitsr) absop concop =
(all bitsa) |> StringSet.iter (fun a ->
(all bitsb) |> StringSet.iter (fun b ->
(all bitsr) |> StringSet.iter (fun r ->
let a = Implem.parse_string a in
let b = Implem.parse_string b in
let r = Implem.parse_string r in
assert(f absop concop a b r))));;
let all_test_f3' f (bitsa,bitsb,bitsr) absop concop =
(all bitsa) |> StringSet.iter (fun a ->
(all bitsb) |> StringSet.iter (fun b ->
[Quadrivalent.True;Quadrivalent.False;
Quadrivalent.Bottom;Quadrivalent.Top] |> List.iter (fun r ->
let a = Implem.parse_string a in
let b = Implem.parse_string b in
assert(f absop concop a b r))));;
let all_test_soundness1 = all_test_f1 test_soundness1;;
let all_test_completeness1 = all_test_f1 test_completeness1;;
let all_test_best1 size = all_test_f1 (test_best_transformer1 ~size);;
let all_test_soundness2 = all_test_f2 test_soundness2;;
let all_test_soundness2_pred = all_test_f2 test_soundness2_pred;;
let all_test_completeness2 = all_test_f2 test_completeness2;;
let all_test_completeness2_pred = all_test_f2 test_completeness2_pred;;
let all_test_best2 size = all_test_f2 (test_best_transformer2 ~size);;
module BF = Implem.Bitvector_Forward;;
module BR = Implem.Bitvector_Backward;;
module CBF = Collecting.Bitvector_Forward;;
module CBB = Collecting.Bitvector_Backward;;
let size1 = Units.In_bits.of_int 1;;
let size2 = Units.In_bits.of_int 2;;
let size3 = Units.In_bits.of_int 3;;
let size4 = Units.In_bits.of_int 4;;
module Test() = struct
all_test_completeness2 1 (BF.band ~size:size1) (CBF.band ~size:size1);;
Format.printf "band is sound and complete@.";;
all_test_completeness2 1 (BF.bor ~size:size1) (CBF.bor ~size:size1);;
Format.printf "bor is sound and complete@.";;
all_test_completeness2 1 (BF.bxor ~size:size1) (CBF.bxor ~size:size1);;
Format.printf "bxor is sound and complete@.";;
let flags = (Operator.Flags.Biadd.pack ~nsw:false ~nuw:false ~nusw:false) in
all_test_soundness2 4 (BF.biadd ~size:size4 ~flags) (CBF.biadd ~size:size4 ~flags);;
Format.printf "biadd is sound@.";;
let flags = (Operator.Flags.Bisub.pack ~nsw:false ~nuw:false ~nusw:false) in
all_test_soundness2 4 (BF.bisub ~size:size4 ~flags) (CBF.bisub ~size:size4 ~flags);;
Format.printf "bisub is sound@.";;
all_test_completeness1 2 (BF.buext ~size:size4 ~oldsize:size2) (CBF.buext ~size:size4 ~oldsize:size2);;
Format.printf "buext is sound and complete@.";;
all_test_soundness1 2 (BF.bsext ~size:size4 ~oldsize:size2) (CBF.bsext ~size:size4 ~oldsize:size2);;
Format.printf "bsext is sound@.";;
all_test_best1 4 2 (BF.bsext ~size:size4 ~oldsize:size2) (CBF.bsext ~size:size4 ~oldsize:size2);;
Format.printf "bsext is maximally precise@.";;
let flags = (Operator.Flags.Bshl.pack ~nsw:false ~nuw:false) in
all_test_soundness2 4 (BF.bshl ~size:size4 ~flags) (CBF.bshl ~size:size4 ~flags);;
Format.printf "bshl is sound@.";;
let flags = (Operator.Flags.Bshl.pack ~nsw:false ~nuw:false) in
all_test_best2 4 4 (BF.bshl ~size:size4 ~flags) (CBF.bshl ~size:size4 ~flags);;
Format.printf "bshl is maximally precise@.";;
all_test_soundness2 4 (BF.blshr ~size:size4) (CBF.blshr ~size:size4);;
Format.printf "blshr is sound@.";;
all_test_best2 4 4 (BF.blshr ~size:size4) (CBF.blshr ~size:size4);;
Format.printf "blshr is maximally precise@.";;
all_test_soundness2 4 (BF.bashr ~size:size4) (CBF.bashr ~size:size4);;
Format.printf "bashr is sound@.";;
all_test_best2 4 4 (BF.bashr ~size:size4) (CBF.bashr ~size:size4);;
Format.printf "bashr is maximally precise@.";;
all_test_completeness2_pred 2 (BF.beq ~size:size2) (CBF.beq ~size:size2);;
Format.printf "beq is sound and complete@.";;
all_test_completeness2_pred 2 (BF.bisle ~size:size4) (CBF.bisle ~size:size4);;
Format.printf "bisle is sound and complete@.";;
all_test_completeness2_pred 2 (BF.biule ~size:size4) (CBF.biule ~size:size4);;
Format.printf "biule is sound and complete@.";;
all_test_completeness2 3 (BF.bconcat ~size1:size3 ~size2:size3) (CBF.bconcat ~size1:size3 ~size2:size3);;
Format.printf "bconcat is sound and complete@.";;
all_test_completeness1 4 (BF.bextract ~index:size1 ~size:size2 ~oldsize:size4) (CBF.bextract ~index:size1 ~size:size2 ~oldsize:size4);;
Format.printf "bextract is sound and complete@.";;
all_test_soundness2 4 (BF.bimul ~size:size4 ~flags:(Operator.Flags.Bimul.pack ~nsw:false ~nuw:false))
(CBF.bimul ~size:size4 ~flags:(Operator.Flags.Bimul.pack ~nsw:false ~nuw:false));;
Format.printf "bimul is sound@.";;
end;;
all_test_soundness2 4 (BF.biudiv ~size:size4) (CBF.biudiv ~size:size4);;
Format.printf "biudiv is sound@.";;
all_test_soundness2 4 (BF.biumod ~size:size4) (CBF.biumod ~size:size4);;
Format.printf "biumod is sound@.";;
all_test_soundness2 3 (BF.bisdiv ~size:size3) (CBF.bisdiv ~size:size3);;
Format.printf "bisdiv is sound@.";;
all_test_soundness2 4 (BF.bismod ~size:size4) (CBF.bismod ~size:size4);;
Format.printf "bismod is sound@.";;
let test_backward_soundness1 absop concop a r =
match absop a r with
| None -> true
| Some x ->
assert(Collecting.subset (Implem.concretize x) (Implem.concretize a));
assert(
Collecting.subset
(Collecting.inter (Implem.concretize r) (concop @@ Implem.concretize a))
(concop @@ Implem.concretize x)
);
true
;;
let test_backward_soundness2 absop concop a b r =
let newa,newb =
match absop a b r with
| None,None -> a,b
| Some a,None -> a,b
| None, Some b -> a,b
| Some a, Some b -> a,b
in
assert(Collecting.subset (Implem.concretize newa) (Implem.concretize a));
assert(Collecting.subset (Implem.concretize newb) (Implem.concretize b));
Format.printf "Result: a=%a b=%a r=%a newa=%a newb=%a@."
Implem.pretty a Implem.pretty b Implem.pretty r
Implem.pretty newa Implem.pretty newb;
assert(Collecting.subset
(Collecting.inter (Implem.concretize r) (concop (Implem.concretize a) (Implem.concretize b)))
(concop (Implem.concretize newa) (Implem.concretize newb))
);
true
;;
let test_backward_soundness2_pred absop concop a b r =
let newa,newb =
match absop a b r with
| None,None -> a,b
| Some a,None -> a,b
| None, Some b -> a,b
| Some a, Some b -> a,b
in
assert(Collecting.subset (Implem.concretize newa) (Implem.concretize a));
assert(Collecting.subset (Implem.concretize newb) (Implem.concretize b));
Format.printf "Result: a=%a b=%a r=%a newa=%a newb=%a@."
Implem.pretty a Implem.pretty b Quadrivalent.pretty r
Implem.pretty newa Implem.pretty newb;
assert(Quadrivalent.includes
(concop (Implem.concretize newa) (Implem.concretize newb))
(Quadrivalent.inter r (concop (Implem.concretize a) (Implem.concretize b)))
);
true
;;
let test_backward_completeness2_pred absop concop a b r =
let newa,newb =
match absop a b r with
| None,None -> a,b
| Some a,None -> a,b
| None, Some b -> a,b
| Some a, Some b -> a,b
in
assert(Collecting.subset (Implem.concretize newa) (Implem.concretize a));
assert(Collecting.subset (Implem.concretize newb) (Implem.concretize b));
Format.printf "Result: a=%a b=%a r=%a newa=%a newb=%a@."
Implem.pretty a Implem.pretty b Quadrivalent.pretty r
Implem.pretty newa Implem.pretty newb;
assert(Quadrivalent.equal
(Quadrivalent.inter r (concop (Implem.concretize a) (Implem.concretize b)))
(concop (Implem.concretize newa) (Implem.concretize newb))
);
true
;;
let forward_then_backward_pred fwd bwd =
fun a b r ->
let r = Quadrivalent.inter r @@ fwd a b in
bwd a b r
;;
let all_test_backward_soundness1 = all_test_f2' test_backward_soundness1;;
let all_test_backward_soundness2 = all_test_f3 test_backward_soundness2;;
let all_test_backward_soundness2_pred = all_test_f3' test_backward_soundness2_pred;;
module BTest() = struct
all_test_backward_soundness2 (2,2,2) (BR.band ~size:size2) (CBF.band ~size:size2);;
Format.printf "rev_band is sound@.";;
all_test_backward_soundness2 (2,2,2) (BR.bor ~size:size2) (CBF.bor ~size:size2);;
Format.printf "rev_bor is sound@.";;
all_test_backward_soundness2 (2,2,2) (BR.bxor ~size:size2) (CBF.bxor ~size:size2);;
Format.printf "rev_bxor is sound@.";;
all_test_backward_soundness2 (2,2,4) (BR.bconcat ~size1:size2 ~size2:size2) (CBF.bconcat ~size1:size2 ~size2:size2);;
Format.printf "rev_bconcat is sound@.";;
all_test_backward_soundness1 (4,2) (BR.bextract ~size:size2 ~index:size1 ~oldsize:size4) (CBF.bextract ~size:size2 ~index:size1 ~oldsize:size4);;
Format.printf "rev_bextract is sound@.";;
all_test_backward_soundness1 (2,4) (BR.buext ~size:size4 ~oldsize:size2) (CBF.buext ~size:size4 ~oldsize:size2);;
Format.printf "rev_buext is sound@.";;
all_test_backward_soundness1 (2,4) (BR.bsext ~size:size4 ~oldsize:size2) (CBF.bsext ~size:size4 ~oldsize:size2);;
Format.printf "rev_bsext is sound@.";;
let flags = (Operator.Flags.Bshl.pack ~nsw:false ~nuw:false) in
all_test_backward_soundness2 (4,4,4) (BR.bshl ~size:size4 ~flags) (CBF.bshl ~size:size4 ~flags);;
Format.printf "rev_bshl is sound@.";;
all_test_backward_soundness2 (4,4,4) (BR.blshr ~size:size4) (CBF.blshr ~size:size4);;
Format.printf "rev_blshr is sound@.";;
all_test_backward_soundness2 (2,2,2) (BR.bashr ~size:size2) (CBF.bashr ~size:size2);;
Format.printf "rev_bashr is sound@.";;
all_test_backward_soundness2 (4,4,4) (BR.bashr ~size:size4) (CBF.bashr ~size:size4);;
Format.printf "rev_bashr is sound@.";;
all_test_backward_soundness2_pred (3,3,3) (BR.beq ~size:size3) (CBF.beq ~size:size3);;
Format.printf "rev_beq is sound@.";;
all_test_backward_soundness2_pred (3,3,3) (BR.biule ~size:size3) (CBF.biule ~size:size3);;
Format.printf "rev_biule is sound@.";;
all_test_backward_soundness2_pred (3,3,3) (BR.bisle ~size:size3) (CBF.bisle ~size:size3);;
Format.printf "rev_bisle is sound@.";;
let flags = (Operator.Flags.Biadd.pack ~nsw:false ~nuw:false ~nusw:false) in
all_test_backward_soundness2 (3,3,3) (BR.biadd ~size:size3 ~flags) (CBF.biadd ~size:size3 ~flags);;
Format.printf "rev_biadd is sound@.";;
let flags = (Operator.Flags.Bisub.pack ~nsw:false ~nuw:false ~nusw:false) in
all_test_backward_soundness2 (3,3,3) (BR.bisub ~size:size3 ~flags) (CBF.bisub ~size:size3 ~flags);;
Format.printf "rev_bisub is sound@.";;
let flags = (Operator.Flags.Bimul.pack ~nsw:false ~nuw:false) in
all_test_backward_soundness2 (3,3,3) (BR.bimul ~flags ~size:size3) (CBF.bimul ~flags ~size:size3);;
Format.printf "rev_bimul is sound@.";;
let flags = (Operator.Flags.Bimul.pack ~nsw:true ~nuw:false) in
all_test_backward_soundness2 (3,3,3) (BR.bimul ~flags ~size:size3) (CBF.bimul ~flags ~size:size3);;
Format.printf "rev_bimul is sound@.";;
let flags = (Operator.Flags.Bimul.pack ~nsw:false ~nuw:true) in
all_test_backward_soundness2 (3,3,3) (BR.bimul ~flags ~size:size3) (CBF.bimul ~flags ~size:size3);;
Format.printf "rev_bimul is sound@.";;
let flags = (Operator.Flags.Bimul.pack ~nsw:true ~nuw:true) in
all_test_backward_soundness2 (3,3,3) (BR.bimul ~flags ~size:size3) (CBF.bimul ~flags ~size:size3);;
Format.printf "rev_bimul is sound@.";;
all_test_backward_soundness2 (3,3,3) (BR.biudiv ~size:size3) (CBF.biudiv ~size:size3);;
Format.printf "rev_biudiv is sound@.";;
all_test_backward_soundness2 (3,3,3) (BR.bisdiv ~size:size3) (CBF.bisdiv ~size:size3);;
Format.printf "rev_bisdiv is sound@.";;
all_test_backward_soundness2 (3,3,3) (BR.biumod ~size:size3) (CBF.biumod ~size:size3);;
Format.printf "rev_biumod is sound@.";;
all_test_backward_soundness2 (3,3,3) (BR.bismod ~size:size3) (CBF.bismod ~size:size3);;
Format.printf "rev_bismod is sound@.";;
end;;
end;;