-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassertions.ml
More file actions
executable file
·1391 lines (1225 loc) · 48.2 KB
/
assertions.ml
File metadata and controls
executable file
·1391 lines (1225 loc) · 48.2 KB
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
(* Poling: Proof Of Linearizability Generator
* Poling is built on top of CAVE and shares the same license with CAVE
* See LICENSE.txt for license.
* Contact: He Zhu, Department of Computer Science, Purdue University
* Email: zhu103@purdue.edu
*)
(******************************************************************************)
(* __ ___ CAVE: Concurrent Algorithm VErifier *)
(* / /\ \ / | Copyright (c) 2010, Viktor Vafeiadis *)
(* | /--\ \ / |--- *)
(* \__ / \ \/ |___ See LICENSE.txt for license. *)
(* *)
(******************************************************************************)
(** Symbolic heaps *)
open Misc
open Exp
open Format
let allow_leaks = ref true
let args =
[("-allow_leaks", Arg.Set allow_leaks, " Allow memory leaks");
("-no_leaks", Arg.Clear allow_leaks, " Do not allow memory leaks")]
(* -------------------------------------------------------------------------- *)
(** {2 Data structures} *)
(* -------------------------------------------------------------------------- *)
type link_kind =
| SINGLE of component * Fld.t
| DOUBLE of component * component
| XOR of component
type can_isemp = Cem_PE | Cem_NE
(** Tags for spatial predicates.
Logically, tags are a bitvector representation of the subset of a
set X permission model (cf., Parkinson's thesis). Therefore, they
should always be non-zero.
Note, however, that tags are not used to encode permissions in the
assertion language; they are used only as a way of doing the
[Wande] computations.
Therefore, for normal spatial predicates, the tag should always be
[1]. Tags [2]-[7] are used internally by the [Wande] computation as
follows:
{v
tag 1 : minued, not matched
tag 2 : subtrahend, to be kept
tag 3 : difference, to be kept
tag 6 : subtrahend, to be removed
tag 7 : difference, to be removed
v}
The assertion represented as [1 * 2 * 3 * 6 * 7] is interpreted as
saying: [(2 * 3 * 6 * 7 -*E 1 * 3 * 7) * 2 * 3].
For further information, see also the appendix of the paper "RGSep action
inference" (Vafeiadis, VMCAI 2010), where the implementation of
MAY-SUBTRACT is described.
*)
type tag = int
type can_spred =
| Csp_listsegi of tag * link_kind * exp * exp * exp * exp
* can_isemp * Dset.t
| Csp_arr of tag * exp * exp * exp * Fld.t * can_isemp * Dset.t
| Csp_node of tag * component * exp * Fld.t
| Csp_indpred of tag * string * exp list
(** Spatial part: star-conjuction of can_spred's *)
type can_spatial = can_spred list
type uform = Pure.t * can_spatial (** Canonical form without boxes *)
type cform = uform * (component, cprop) plist (** Canonical form *)
and cprop = cform list
(** Type of RGSep actions used for interference calculations *)
type act = {
a_name : string;
a_exvars : IdSet.t;
a_pure : Pure.t;
a_pre : can_spatial (** context & precondition *);
a_post : cprop }
exception No_frame_found
exception Junk
type prover =
{ find_ptsto : Misc.component -> exp -> can_spatial ->
Fld.t * can_spatial;
expose_ptsto : cprop -> exp -> cprop;
nf_cprop : can_spatial -> cprop -> cprop;
nf_cform : cform -> cprop;
entails_cprop : cprop -> cprop -> Pure.t list;
find_frame_cprop : can_spatial -> cprop -> cprop -> cprop;
adv_entails_atom : cprop -> exp -> bool;
}
(* -------------------------------------------------------------------------- *)
(** {2 Helper functions} *)
(* -------------------------------------------------------------------------- *)
(** Only allow comparisons between integers to catch uses of builtin
equality. Use Pervasives.(=) on base types only *)
let (=) (x:int) (y:int) = x=y
(** [get_first f l] assumes [List.exists f l].
[get_first f (xs @ [y] @ zs) = (y, xs @ zs)] where [f y] and
[forall (not << f) xs].
*)
let get_first f =
let rec go acc l = match l with
| x :: l -> if f x then (x, List.rev_append acc l) else go (x :: acc) l
| [] -> assert(false) (* Should never happen *)
in
go []
(* -------------------------------------------------------------------------- *)
(** {2 Equality and comparison functions} *)
(* -------------------------------------------------------------------------- *)
let compare_lkind k1 k2 = if k1 == k2 then 0 else match k1,k2 with
| SINGLE(s1,cel), SINGLE(s1',cel') ->
let n = compare_component s1 s1' in if n<>0 then n else
Fld.compare cel cel'
| SINGLE _, _ -> -1
| _, SINGLE _ -> 1
| DOUBLE(s1,s2), DOUBLE(s1',s2') ->
let n = compare_component s1 s1' in if n<>0 then n else
compare_component s2 s2'
| DOUBLE _, _ -> -1
| _, DOUBLE _ -> 1
| XOR(s1), XOR(s1') -> compare_component s1 s1'
let compare_can_spred sp1 sp2 = if sp1 == sp2 then 0 else match sp1,sp2 with
| Csp_listsegi (tag,k,e1,e2,e3,e4,ie,ds),
Csp_listsegi(tag',k',e1',e2',e3',e4',ie',ds') ->
let n = tag - tag' in if n<>0 then n else
let n = compare_lkind k k' in if n<>0 then n else
let n = compare_exp e1 e1' in if n<>0 then n else
let n = compare_exp e2 e2' in if n<>0 then n else
let n = compare_exp e3 e3' in if n<>0 then n else
let n = compare_exp e4 e4' in if n<>0 then n else
let n = Obj.magic ie - Obj.magic ie' in if n<>0 then n else
Dset.compare ds ds'
| Csp_listsegi _, _ -> -1
| _, Csp_listsegi _ -> 1
| Csp_arr (tag,e1,e2,e3,fld,ie,ds),
Csp_arr(tag',e1',e2',e3',fld',ie',ds') ->
let n = tag - tag' in if n<>0 then n else
let n = compare_exp e1 e1' in if n<>0 then n else
let n = compare_exp e2 e2' in if n<>0 then n else
let n = compare_exp e3 e3' in if n<>0 then n else
let n = Fld.compare fld fld' in if n<>0 then n else
let n = Obj.magic ie - Obj.magic ie' in if n<>0 then n else
Dset.compare ds ds'
| Csp_arr _, _ -> -1
| _, Csp_arr _ -> 1
| Csp_node (tag1,s1,e1,cel1), Csp_node (tag2,s2,e2,cel2) ->
let n = tag1 - tag2 in if n<>0 then n else
let n = compare_component s1 s2 in if n<>0 then n else
let n = compare_exp e1 e2 in if n<>0 then n else
Fld.compare cel1 cel2
| Csp_node _, _ -> -1
| _, Csp_node _ -> 1
| Csp_indpred (tag1,s1,el1), Csp_indpred (tag2,s2,el2) ->
let n = tag1 - tag2 in if n<>0 then n else
let n = String.compare s1 s2 in if n<>0 then n else
compare_exp_list el1 el2
let abs_compare_can_spred sp1 sp2 = if sp1 == sp2 then 0 else match sp1,sp2 with
| Csp_listsegi (tag,k,e1,e2,e3,e4,ie,ds),
Csp_listsegi(tag',k',e1',e2',e3',e4',ie',ds') ->
let n = tag - tag' in if n<>0 then n else
let n = compare_lkind k k' in if n<>0 then n else
let n = abs_compare_exp e1 e1' in if n<>0 then n else
let n = abs_compare_exp e2 e2' in if n<>0 then n else
let n = abs_compare_exp e3 e3' in if n<>0 then n else
let n = abs_compare_exp e4 e4' in if n<>0 then n else
let n = Obj.magic ie - Obj.magic ie' in if n<>0 then n else
Dset.abs_compare ds ds'
| Csp_listsegi _, _ -> -1
| _, Csp_listsegi _ -> 1
| Csp_arr (tag,e1,e2,e3,fld,ie,ds),
Csp_arr(tag',e1',e2',e3',fld',ie',ds') ->
let n = tag - tag' in if n<>0 then n else
let n = abs_compare_exp e1 e1' in if n<>0 then n else
let n = abs_compare_exp e2 e2' in if n<>0 then n else
let n = abs_compare_exp e3 e3' in if n<>0 then n else
let n = Fld.compare fld fld' in if n<>0 then n else
let n = Obj.magic ie - Obj.magic ie' in if n<>0 then n else
Dset.abs_compare ds ds'
| Csp_arr _, _ -> -1
| _, Csp_arr _ -> 1
| Csp_node (tag1,s1,e1,cel1), Csp_node (tag2,s2,e2,cel2) ->
let n = tag1 - tag2 in if n<>0 then n else
let n = compare_component s1 s2 in if n<>0 then n else
let n = abs_compare_exp e1 e2 in if n<>0 then n else
Fld.compare cel1 cel2
| Csp_node _, _ -> -1
| _, Csp_node _ -> 1
| Csp_indpred (tag1,s1,el1), Csp_indpred (tag2,s2,el2) ->
let n = tag1 - tag2 in if n<>0 then n else
let n = String.compare s1 s2 in if n<>0 then n else
compare_exp_list el1 el2
let rec compare_can_spatial sl1 sl2 = match sl1,sl2 with
| [],[] -> 0
| [],_ -> -1
| _,[] -> 1
| sp1::sl1', sp2::sl2' ->
let n = compare_can_spred sp1 sp2 in if n<>0 then n else
compare_can_spatial sl1' sl2'
let compare_uform (pl1,sl1) (pl2,sl2) =
let n = Pure.compare pl1 pl2
in if n<>0 then n
else compare_can_spatial sl1 sl2
let rec compare_component_cprop (s1,p1) (s2,p2) =
let n = compare_component s1 s2 in if n<>0 then n else
compare_cprop p1 p2
and compare_component_cprop_list spl1 spl2 =
if spl1 == spl2 then 0 else match spl1 with
| PNil -> -1
| PCons (k1, v1, l1) -> begin match spl2 with
| PNil -> 1
| PCons (k2, v2, l2) ->
let n = compare_component k1 k2 in if n<>0 then n else
let n = compare_cprop v1 v2 in if n<>0 then n else
compare_component_cprop_list l1 l2
end
and compare_cform ((cu1,spl1): cform) ((cu2,spl2): cform) =
let n = compare_uform cu1 cu2 in if n<>0 then n else
compare_component_cprop_list spl1 spl2
and compare_cprop cp1 cp2 =
let rec f cfl1 cfl2 = match cfl1,cfl2 with
| [],[] -> 0
| [],_ -> -1
| _,[] -> 1
| cf1::cfl1', cf2::cfl2' ->
let n = compare_cform cf1 cf2
in if n<>0 then n
else f cfl1' cfl2'
in f cp1 cp2
let mem_component_cprop y l =
List.exists (fun x -> compare_component_cprop x y = 0) l
(* -------------------------------------------------------------------------- *)
(** {2 Spatial predicates} *)
(* -------------------------------------------------------------------------- *)
let tag_default = 1
let spred_bad =
Csp_node (tag_default, component_of_string "**internal**", E.zero, Fld.emp)
let spred_node tag nid e fld =
if e == E.zero then spred_bad
else Csp_node (tag, nid, e, fld)
(** [get_tag sp] returns the tag associated with [sp] *)
let get_tag sp = match sp with
| Csp_node (tag, _, _, _)
| Csp_listsegi (tag, _, _, _, _, _, _, _)
| Csp_arr (tag, _, _, _, _, _, _)
| Csp_indpred (tag, _, _) -> tag
(** [set_tag tag sp] returns an updated version of [sp] with its tag
set to [tag]. *)
let set_tag tag sp = match sp with
| Csp_node (tag', nid, e, fld) ->
if tag' == tag then sp else Csp_node (tag, nid, e, fld)
| Csp_listsegi (tag', kind, e1, e2, e3, e4, ie, ds) ->
if tag' == tag then sp else
Csp_listsegi (tag, kind, e1, e2, e3, e4, ie, ds)
| Csp_arr (tag', e1, e2, e3, fld, ie, ds) ->
if tag' == tag then sp else Csp_arr (tag, e1, e2, e3, fld, ie, ds)
| Csp_indpred (tag', i, el) ->
if tag' == tag then sp else Csp_indpred (tag, i, el)
let spat_empty = []
let spat_one sp = [sp]
let spat_star_one sp sl = sp::sl
let spat_star sl1 sl2 = sl1@sl2
let spred_fold f sp acc = match sp with
| Csp_listsegi (_,_,e1,e2,e3,e4,_,ds) ->
Dset.fold f ds (f e4 (f e3 (f e2 (f e1 acc))))
| Csp_arr (_,e1,e2,e3,_,_,ds) ->
Dset.fold f ds (f e3 (f e2 (f e1 acc)))
| Csp_node (_,_,e,fld) -> Fld.fold_val f fld (f e acc)
| Csp_indpred (_,_,el) -> List.fold f el acc
let spat_fold f = List.fold (spred_fold f)
let spat_fold_spred = List.fold
let spat_remove sl x = List.filter (fun y -> compare_can_spred y x <> 0) sl
let spat_get x sl =
let rec gosp r l = match l with
| [] -> None
| sp::l -> match sp with
| Csp_node (_,_,e,_)
| Csp_listsegi (_,SINGLE _,e,_,_,_,_,_) ->
if equal_exp x e then Some (sp, List.rev_append r l) else gosp (sp::r) l
| Csp_listsegi (_,(DOUBLE _ | XOR _),e,_,f,_,_,_) ->
if equal_exp x e || equal_exp x f then
Some (sp, List.rev_append r l)
else gosp (sp::r) l
| Csp_arr (_,e1,e2,_,_,_,_) ->
if equal_exp x e1 (* TODO!!!!!! *) then
Some (sp, List.rev_append r l)
else gosp (sp::r) l
| Csp_indpred _ -> gosp (sp::r) l in
gosp [] sl
(* -------------------------------------------------------------------------- *)
(** {2 Pretty printing} *)
(* -------------------------------------------------------------------------- *)
let rec pp_seq pp f = function
| [] -> ()
| [x] -> pp f x
| x::l -> pp f x; pp_print_char f ','; pp_seq pp f l
let pp_disjoint_set f ds = match Dset.to_list ds with
| [] -> ()
| el -> fprintf f ",{%a}" (pp_seq pp_exp) el
let pp_isemp f ie = match ie with
| Cem_PE -> ()
| Cem_NE -> pp_print_string f "NE"
let pp_tag f tag =
if tag <> tag_default then begin
pp_print_char f '<';
pp_print_int f tag;
pp_print_char f '>'
end
let pp_spred f = function
| Csp_listsegi (tag,SINGLE(snode,cel),e1,f1,e2,_,ie,ds) ->
let (what,ds) =
if Dset.mem f1 ds then ("lseg",Dset.remove f1 ds) else ("lsegi",ds) in
pp_tag f tag;
pp_print_string f what;
pp_isemp f ie;
if cel != Fld.emp then begin
pp_print_char f '[';
pp_fld f cel;
pp_print_char f ']'
end;
pp_print_char f '(';
(if snode == Misc.list_link_tag then () else
pp_print_string f (string_of_component snode^";"));
pp_seq pp_exp f [e1;f1;e2];
pp_disjoint_set f ds;
pp_print_char f ')'
| Csp_listsegi (tag,DOUBLE(t1,t2),e1,f1,e2,f2,ie,el) ->
pp_tag f tag;
fprintf f "dlsegi%a(%s,%s;%a,%a,%a,%a%a)" pp_isemp ie
(string_of_component t1) (string_of_component t2)
pp_exp e1 pp_exp f1 pp_exp e2 pp_exp f2 pp_disjoint_set el
| Csp_listsegi (tag,XOR t1,e1,f1,e2,f2,ie,el) ->
pp_tag f tag;
fprintf f "xlsegi%a(%s%a,%a,%a,%a%a)" pp_isemp ie
(if t1 == Misc.dl_Llink_tag then "" else (string_of_component t1)^";")
pp_exp e1 pp_exp f1 pp_exp e2 pp_exp f2 pp_disjoint_set el
| Csp_node (tag,s, e1, cel) ->
pp_tag f tag;
if cel == Fld.emp then
fprintf f "@[%a|->_@]" pp_exp e1
else
fprintf f "@[%a|->%a@]" pp_exp e1 pp_fld cel
| Csp_arr (tag,e1,e2,e3,cel,ie,ds) ->
pp_tag f tag;
pp_print_string f "arr";
pp_isemp f ie;
if cel != Fld.emp then begin
pp_print_char f '[';
pp_fld f cel;
pp_print_char f ']'
end;
pp_print_char f '(';
pp_seq pp_exp f [e1;e2;e3];
pp_disjoint_set f ds;
pp_print_char f ')'
| Csp_indpred (tag,id,el) ->
pp_tag f tag;
fprintf f "%s(%a)" id (pp_seq pp_exp) el
let rec pp_starsep pp f = function
| x::l ->
pp f x;
(match l with [] -> () | _ ->
Format.pp_print_space f ();
Format.pp_print_string f "* ";
pp_starsep pp f l)
| [] -> Format.pp_print_string f "emp"
let rec pp_orsep pp f = function
| x::l ->
pp f x;
(match l with [] -> () | _ ->
Format.pp_print_space f ();
Format.pp_print_string f "|| ";
pp_orsep pp f l)
| [] -> Format.pp_print_string f "false"
let pp_uform f (pl,sl) =
Format.pp_open_box f 0;
if Pure.is_true pl then
pp_starsep pp_spred f sl
else begin
Pure.pp f pl;
(match sl with [] -> () | _ ->
Format.pp_print_space f ();
Format.pp_print_string f "* ";
pp_starsep pp_spred f sl)
end;
Format.pp_close_box f ()
let rec pp_cform f (uf,scpl) =
match scpl with
| PNil -> pp_uform f uf
| PCons _ -> fprintf f "@[<hv>%a@ * %a@]" pp_uform uf pp_boxes scpl
and pp_cprop f (cp : cprop) =
Format.pp_open_hvbox f 0;
pp_orsep pp_cform f cp;
Format.pp_close_box f ()
and pp_boxes f = function
| PCons (s, cp, l) ->
Format.pp_print_string f (string_of_component s);
Format.pp_open_hvbox f 0;
Format.pp_print_string f ":[";
pp_orsep pp_cform f cp;
Format.pp_print_char f ']';
Format.pp_close_box f ();
begin match l with
| PNil -> ()
| PCons _ ->
Format.pp_print_space f ();
Format.pp_print_string f "* ";
pp_boxes f l
end
| PNil -> ()
(** Pretty print an action *)
let pp_act pp act =
Format.fprintf pp "@[%a@ | %a@ ~~> %a@]"
pp_uform (act.a_pure, List.filter (fun x -> get_tag x = 6) act.a_pre)
(pp_starsep pp_spred) (List.filter (fun x -> get_tag x = 2) act.a_pre)
pp_cprop act.a_post
(* -------------------------------------------------------------------------- *)
(** {2 Implementation} *)
(* -------------------------------------------------------------------------- *)
(** default values *)
let uform_empty : uform = (Pure.ptrue,spat_empty)
let uform_false : uform = (Pure.pfalse,spat_empty)
let cform_false = (uform_false,PNil)
let cprop_pure p = [((p,spat_empty),PNil)]
let cprop_spred sl = [((Pure.ptrue,sl),PNil)]
let cprop_uform uf = [(uf,PNil)]
let cprop_cform cf = [cf]
let cprop_empty = cprop_pure (Pure.ptrue)
let cprop_false = []
let cprop_box s p : cprop = [(uform_empty, PCons(s, p, PNil))]
let mk_array tag e1 e2 ev fld ds (p,sl) =
match E.sub e2 e1 with
| Enum 0 -> (Pure.conj_one (E.eq ev E.empty_list) p, sl)
| Enum 1 ->
let ev' = E.gensym_str_ex "VAL" in
let fld = Fld.set list_data_tag ev' fld in
let sp = spred_node tag node_component e1 fld in
if sp == spred_bad then uform_false else
(Pure.conj_one (E.eq ev (E.item ev')) p, sp :: sl)
| Enum n when n < 0 -> uform_false
| _ -> (p, Csp_arr(tag,e1,e2,ev,fld,Cem_PE,ds) :: sl)
let remove_uform_duplicates l =
let l2 = List.stable_sort compare_uform l in
let rec f = function
| x::y::xs -> if compare_uform x y = 0 then f(y::xs) else x::(f(y::xs))
| xs -> xs
in f l2
let remove_cform_duplicates l =
let l2 = List.stable_sort compare_cform l in
let rec f = function
| x::y::xs -> if compare_cform x y = 0 then f(y::xs) else x::(f(y::xs))
| xs -> xs
in f l2
let rec aggr_remove_cform_duplicates (l: cprop) =
let l2 = List.stable_sort compare_cform l in
let rec merge xs ys = match xs, ys with
| PNil, ys -> ys
| xs, PNil -> xs
| PCons (s,x,xs0), PCons (s',y,ys0) ->
let n = compare_component s s' in
if n = 0 then PCons (s, remove_cform_duplicates (x @ y),
merge xs0 ys0)
else if n < 0 then PCons (s, x, merge xs0 ys)
else PCons (s', y, merge xs ys0) in
let rec f = function
| (x,xsh)::(y,ysh)::xs ->
if compare_uform x y = 0 then f ((y, merge xsh ysh)::xs)
else (x,xsh)::(f((y,ysh)::xs))
| xs -> xs in
f l2
let ( @& ) = Pure.conj_one
let ( @&& ) = Pure.conj
let spred_fv sp acc = spred_fold E.fv sp acc
let spred_exfv sp acc = spred_fold E.exfv sp acc
let spred_fhd sp acc = spred_fold E.fhd sp acc
let (++) = IdSet.union
let spred_fv_fromTID sp acc = match sp with
| Csp_node (_,_,e,fld) ->
let go_fld c e (res,fv) =
let idset = E.fv e IdSet.empty
in
(IdSet.mem Id.tid idset && res,
if is_lock_field c then fv else idset ++ fv)
in
let (res,fv) = Fld.fold go_fld fld (false, E.fv e acc)
in
if res then fv else acc
| Csp_listsegi _
| Csp_arr _
| Csp_indpred _ -> acc
let uform_fv (p,sl) acc =
List.fold spred_fv sl (Pure.fv p acc)
let uform_fhd (p,sl) acc =
List.fold spred_fhd sl (Pure.fhd p acc)
let uform_fv_fromTID (p,sl) acc =
List.fold spred_fv_fromTID sl acc
let rec form_fv (uf,scpl) acc =
PList.fold_val prop_fv scpl (uform_fv uf acc)
and prop_fv (cp: cprop) acc =
List.fold form_fv cp acc
let rec form_fhd (uf,scpl) acc =
PList.fold_val prop_fhd scpl (uform_fhd uf acc)
and prop_fhd (cp: cprop) acc =
List.fold form_fhd cp acc
let rec form_fv_fromTID (uf,scpl) acc =
PList.fold_val prop_fv_fromTID scpl (uform_fv_fromTID uf acc)
and prop_fv_fromTID (cp: cprop) acc =
List.fold form_fv_fromTID cp acc
(** Existential free variables only *)
let abs_fv_uform (p,sl) =
let rec go_exp e fv = match e with
| Enum _ -> fv
| Eident _ ->
let i = Id.of_exp e in
if Id.is_existential i && not (List.memq i fv) then i::fv else fv
| Eeq (e,f) -> go_exp f (go_exp e fv)
| Efun1 (_,e) -> go_exp e fv
| Efun2 (_,e,f) -> go_exp f (go_exp e fv)
| Efun (_,el) -> List.fold go_exp el fv
| Egrp(_,_,nel) -> ie_fold_exp go_exp nel fv in
let go_spred sp fv = match sp with
| Csp_listsegi (_,_,ce1,ce2,ce3,ce4,_,el) ->
go_exp ce1 (
go_exp ce2 (
go_exp ce3 (
go_exp ce4 (List.fold go_exp (Dset.to_list el) fv))))
| Csp_arr (_,ce1,ce2,ce3,_,_,el) ->
go_exp ce1 (
go_exp ce2 (
go_exp ce3 (List.fold go_exp (Dset.to_list el) fv)))
| Csp_node (_,_,e,fld) ->
Fld.fold_val go_exp fld (go_exp e fv)
| Csp_indpred (_,i,el) -> List.fold go_exp el fv in
let fv = List.fold go_spred sl [] in
let fv = go_exp (Pure.to_exp p) fv in
List.rev fv
let map_spred f_e sp = match sp with
| Csp_listsegi (tag,k,e1,e2,e3,e4,ie,ds) ->
let e1' = f_e e1 in
let e2' = f_e e2 in
let e3' = f_e e3 in
let e4' = f_e e4 in
if ie == Cem_NE && (e1' == E.zero || equal_exp e1' e2') then spred_bad
else
let ds' = Dset.map f_e ds in
if e1' == e1 && e2' == e2 && e3' == e3 && e4' == e4 && ds' == ds then sp
else Csp_listsegi (tag, k, e1', e2', e3', e4', ie, ds')
| Csp_arr (tag,e1,e2,e3,fld,ie,ds) ->
let e1' = f_e e1 in
let e2' = f_e e2 in
let e3' = f_e e3 in
if ie == Cem_NE && (equal_exp e1' e2') then spred_bad
else
let ds' = Dset.map f_e ds in
let fld' = Fld.map f_e fld in
if e1' == e1 && e2' == e2 && e3' == e3 && fld' == fld && ds' == ds then sp
else Csp_arr (tag, e1', e2', e3', fld', ie, ds')
| Csp_node (tag,nid,e,fld) ->
let e' = f_e e in
let fld' = Fld.map f_e fld in
if e' == e && fld' == fld then sp
else spred_node tag nid e' fld'
| Csp_indpred (tag,id,el) -> Csp_indpred (tag,id, List.map f_e el)
let map_spred_opt f_e sp = match sp with
| Csp_listsegi (tag,k,e1,e2,e3,e4,ie,ds) ->
let e1' = f_e e1 in
let e2' = f_e e2 in
let e3' = f_e e3 in
let e4' = f_e e4 in
(e1'!=e1 || e2'!=e2 || e3'!=e3 || e4'!=e4,
if ie == Cem_NE && (e1' == E.zero || equal_exp e1' e2') then spred_bad
else
let ds' = Dset.map f_e ds in
if e1' == e1 && e2' == e2 && e3' == e3 && e4' == e4
&& ds' == ds then sp
else Csp_listsegi (tag, k, e1', e2', e3', e4', ie, ds'))
| Csp_arr (tag,e1,e2,e3,fld,ie,ds) ->
let e1' = f_e e1 in
let e2' = f_e e2 in
let e3' = f_e e3 in
(e1'!=e1 || e2'!=e2 || e3'!=e3,
if ie == Cem_NE && (equal_exp e1' e2') then spred_bad
else
let ds' = Dset.map f_e ds in
let fld' = Fld.map f_e fld in
if e1' == e1 && e2' == e2 && e3' == e3
&& fld' == fld && ds' == ds then sp
else Csp_arr (tag, e1', e2', e3', fld', ie, ds'))
| Csp_node (tag, nid, e, fld) ->
let e' = f_e e in
let fld' = Fld.map f_e fld in
(e' != e,
if e' == e && fld' == fld then sp
else spred_node tag nid e' fld')
| Csp_indpred (tag,id,el) -> (true, Csp_indpred (tag, id, List.map f_e el))
let map_spat f_e =
if f_e == identity then identity else
map_sub (map_spred f_e)
let map_spat_opt f_e =
if f_e == identity then (fun x y -> List.rev_append y x)
else
let rec go acc sl = match sl with
| sp::sl ->
let sp = map_spred f_e sp in
if sp == spred_bad then [sp]
else go (sp::acc) sl
| [] -> acc
in go
let map_spat_opt2 f_e =
if f_e == identity then (fun sl -> ([], sl))
else
let rec go sl1 sl2 sl = match sl with
| sp::sl ->
let (changed,sp) = map_spred_opt f_e sp in
if sp == spred_bad then ([sp],[])
else if changed then go (sp::sl1) sl2 sl
else go sl1 (sp::sl2) sl
| [] -> (sl1, sl2)
in go [] []
(** [None = identity substitution] *)
let fhd_to_sub fhd =
match List.filter Id.is_existential (IdSet.elements fhd) with
| [] -> None
| fhd ->
let f x =
let id1 = E.id (Id.gensym_garb x) in
let id2 = E.id (Id.gensym_garb x) in
let exp = E.list [E.item id1; id2] in
(x, (id1,exp)) in
let fhd = List.map f fhd in
let sub =
mk_fun_subst
(fun e -> match e with
| Efun1 (Sfn_hd,(Eident _ as e')) ->
begin try fst (List.assq (Id.of_exp e') fhd)
with Not_found -> e end
| _ -> e) in
Some sub
let rec def_alloc tag x sl = match sl with
| Csp_node (tag',_,y,_) :: sl ->
if equal_exp x y then tag' land tag <> 0 else def_alloc tag x sl
| Csp_listsegi (tag',SINGLE _,y,_,_,_,Cem_NE,_) :: sl ->
if equal_exp x y then tag' land tag <> 0 else def_alloc tag x sl
| Csp_listsegi (tag',(DOUBLE _ | XOR _),y,_,z,_,Cem_NE,_) :: sl ->
if equal_exp x y || equal_exp x z then tag' land tag <> 0
else def_alloc tag x sl
| _ :: sl -> def_alloc tag x sl
| [] -> false
let rec maybe_alloc x sl = match sl with
| Csp_node (_,_,y,_) :: sl -> equal_exp x y || maybe_alloc x sl
| Csp_listsegi (_,SINGLE _,y,_,_,_,_,_) :: sl ->
equal_exp x y || maybe_alloc x sl
| Csp_listsegi(_,(DOUBLE _ | XOR _),y,_,z,_,_,_) :: sl ->
equal_exp x y || equal_exp x z || maybe_alloc x sl
| _ :: sl -> maybe_alloc x sl
| [] -> false
(** Assuming that [(Pure.ptrue,sl)] is already in normal form,
[do_update_pure p_new slu sl res = normalize (p_new, slu @ sl) @ res]. *)
(* called as do_update_pure p sl [] [] *)
let rec do_update_pure p_new slu sl q =
if Pure.is_false p_new then q
else
let sub = Pure.to_sub p_new in
let (sl1, sl2) = map_spat_opt2 sub sl in
let slu = map_spat_opt sub sl1 slu in
(* (p_new, sl2) is already normalized. Focusing on slu. *)
add_nodes p_new slu sl2 q
(** Assuming that [(p,sl)] is already in normal form and [sp] starts at address
[x], [add_node sp x p slu sl res = normalize (p, sp::slu@sl) @ res]. *)
and add_node sp x p slu sl q =
let is_interesting x sp = match sp with
| Csp_node(_,_,y,_) -> equal_exp x y
| Csp_listsegi(_,SINGLE _,y,_,_,_,_,_) -> equal_exp x y
| Csp_listsegi(_,(DOUBLE _ | XOR _),y,_,z,_,_,_) ->
equal_exp x y || equal_exp x z
| _ -> false
in
if List.exists (is_interesting x) sl then
begin
let (sp',sl) = get_first (is_interesting x) sl
in
match sp, sp' with
| Csp_node (tag,nid,_,fld), Csp_node (tag',nid',_,fld') ->
if tag land tag' = 0 && nid == nid' then (* Compatible *)
let (eq,fld) = Fld.components_equal fld fld'
in
do_update_pure (eq @& p) slu (Csp_node (tag lor tag', nid, x, fld) :: sl) q
else q
| (Csp_node (tag,nid,_,fld) as sp0),
Csp_listsegi (tag',(SINGLE(snode,fld') as kind),_,e2,e3,_,ie,ds)
| Csp_listsegi (tag',(SINGLE(snode,fld') as kind),_,e2,e3,_,ie,ds),
(Csp_node (tag,nid,_,fld) as sp0) ->
let q = (* Case: lseg is non-empty *)
if tag land tag' = 0 then (* Consisent => unroll lseg *)
let t = if component_is_field snode then snode else assert false (* TODO *) in
let (e_next,fld) = Fld.get_extend t fld in
let (e_val,fld) = Fld.get_extend list_data_tag fld in
let (eq,fld) = Fld.components_equal fld fld' in
let e_val_rest = E.gensym_str_ex "VAL" in
let eq2 = E.eq (E.list [E.item e_val; e_val_rest]) e3 in
do_update_pure (eq @& eq2 @& p)
(Csp_listsegi (tag',kind,e_next,e2,e_val_rest,E.zero,Cem_PE,ds) :: slu)
(Csp_node (tag lor tag', nid, x, fld) :: sl)
q
else q
in
let q = match ie with
| Cem_PE -> do_update_pure (E.eq x e2 @& E.eq E.empty_list e3 @& p) slu (sp0 :: sl) q
| Cem_NE -> q
in q
| Csp_listsegi (tag, SINGLE(snode,fld),_,e2,e3,_,ie,ds),
Csp_listsegi (tag', SINGLE(snode',fld'),_,e2',e3',_,ie',ds') ->
if tag land tag' = 0 then (* Compatible *)
let _ = if snode == snode' then () else (Misc.pp "@.snode is %a and snode' is %a@." pp_spred sp
pp_spred sp'; assert false) in
let (eq, fldU) = Fld.components_equal fld fld' in
let tagU = tag lor tag' in
let dsU = Dset.union ds ds' in
let e_val = E.gensym_str_ex "VAL" in
let p = eq @& p in
let (ie1, ie1') = match ie, ie' with
| Cem_PE, Cem_NE -> (Cem_NE, Cem_PE)
| _ -> (Cem_PE, Cem_NE)
in
do_update_pure (E.eq e3' (E.list [e3; e_val]) @& p)
(Csp_listsegi (tag', SINGLE(snode',fld'),e2,e2',e_val,E.zero,ie1,ds') :: slu)
(Csp_listsegi (tagU, SINGLE(snode,fldU),x,e2,e3,E.zero,ie,dsU) :: sl)
(do_update_pure (E.eq e3 (E.list [e3'; e_val]) @& p)
(Csp_listsegi (tag, SINGLE(snode,fld),e2',e2,e_val,E.zero,ie1',ds) :: slu)
(Csp_listsegi (tagU, SINGLE(snode,fldU),x,e2',e3',E.zero,ie',dsU) :: sl)
q)
else (* One lseg must be empty *)
let q = match ie with
| Cem_PE ->
do_update_pure (E.eq x e2 @& E.eq E.empty_list e3 @& p) slu (sp' :: sl) q
| Cem_NE -> q
in
let q = match ie' with
| Cem_PE ->
do_update_pure (E.eq x e2' @& E.eq E.empty_list e3' @& p) slu (sp :: sl) q
| Cem_NE -> q
in q
| _ -> assert false (* Not implemented *)
end
else add_nodes p slu (sp :: sl) q
(** Assuming that [(p,sl)] is already in normal form,
[add_nodes p slu sl res = normalize (p, slu @ sl) @ res]. *)
and add_nodes p slu sl q = match slu with
| sp :: slu ->
begin match sp with
| Csp_node (_,_,x,_) ->
begin match x with
| Enum 0 -> q
| _ -> add_node sp x p slu sl q
end
| Csp_listsegi(tag,(SINGLE _ as kind),x,y,v,_,ie,ds) ->
if (match x with Enum 0 -> true | _ ->
Dset.mem x ds
|| equal_exp x y && (maybe_alloc y sl || maybe_alloc y slu)
|| equal_exp v E.empty_list) then
begin
match ie with
| Cem_NE -> q
| Cem_PE -> do_update_pure
(E.eq x y @& E.eq v E.empty_list @& p) slu sl q
end
else
let sp =
if (match ie with Cem_NE -> false | Cem_PE -> true)
&& Pure.is_false (E.eq x y @& E.eq v E.empty_list @& p) then
Csp_listsegi (tag,kind,x,y,v,E.zero,Cem_NE,ds)
else sp
in
add_node sp x p slu sl q
| Csp_arr(tag,x,y,v,fld,ie,ds) ->
if (match x with Enum 0 -> true | _ ->
Dset.mem x ds || equal_exp x y || equal_exp v E.empty_list) then
begin
match ie with
| Cem_NE -> q
| Cem_PE -> do_update_pure
(E.eq x y @& E.eq v E.empty_list @& p) slu sl q
end
else
let sp =
if (match ie with Cem_NE -> false | Cem_PE -> true)
&& Pure.is_false (E.eq x y @& E.eq v E.empty_list @& p) then
Csp_arr (tag,x,y,v,fld,Cem_NE,ds)
else sp
in
add_node sp x p slu sl q
| _ -> assert false
end
| [] -> (p, sl) :: q
let normalize_uform (p,sl) =
do_update_pure p sl [] []
let map_uform f_e (p,sl) =
let p' = Pure.map f_e p in
let sl' = map_spat f_e sl in
if Pure.identical_eq p' p && sl' == sl then [(p', sl')]
else
normalize_uform (p', sl')
(* Very simplistic *)
let normalize_cform ((uf,scpl) : cform) =
if PList.exists (fun _ cp -> cp == []) scpl then
cprop_false
else
List.map (fun uf -> (uf,scpl)) (normalize_uform uf)
let normalize_cprop cp =
List.fold_append normalize_cform cp []
let rec map_cform_app f_e (uf,scpl) res = match map_uform f_e uf with
| [] -> res
| ufl ->
let scpl' = PList.map_val (map_cprop f_e) scpl in
List.fold_cons (fun uf -> (uf,scpl')) ufl res
and map_cprop f_e cp =
List.reduce (map_cform_app f_e) cp
let map_cform f_e cf = map_cform_app f_e cf []
let naive_map_uform sub (p,sl) =
(Pure.map sub p, map_spat sub sl)
let rec naive_map_cform f_e (uf,scpl) =
(naive_map_uform f_e uf, PList.map_val (naive_map_cprop f_e) scpl)
and naive_map_cprop f_e (cp: cprop) =
List.fold (fun cf q -> naive_map_cform f_e cf :: q) cp []
(** Replace annoying [hd(_temp)] with a fresh [_temp']. *)
let uform_kill_hd_vars uf =
match fhd_to_sub (uform_fhd uf IdSet.empty) with
| None -> uf
| Some sub -> naive_map_uform sub uf
(** Replace annoying [hd(_temp)] with a fresh [_temp']. *)
let form_kill_hd_vars (cf : cform) =
let cf =
match fhd_to_sub (form_fhd cf IdSet.empty) with
| None -> cf
| Some sub -> naive_map_cform sub cf
in
cf
(** Replace annoying [hd(_temp)] with a fresh [_temp']. *)
let prop_kill_hd_vars cp =
match fhd_to_sub (prop_fhd cp IdSet.empty) with
| None -> cp
| Some sub -> naive_map_cprop sub cp
let normalize_uform x = normalize_uform (uform_kill_hd_vars x)
let normalize_cform x =
prop_kill_hd_vars (normalize_cform (form_kill_hd_vars x))
let normalize_cprop x =
prop_kill_hd_vars (normalize_cprop (prop_kill_hd_vars x))
let map_cform f_e cf = prop_kill_hd_vars (map_cform f_e cf)
let map_cprop f_e cp = prop_kill_hd_vars (map_cprop f_e cp)
let map_ip_body f_e body =
let f res (p,cf) =
let pl = Pure.normalize (Pure.map f_e p) in
match pl with [] -> res (* Optimisation *) | _ ->
let cfl = map_cform f_e cf in
List.fold_left (fun acc p -> List.fold_left (fun acc cf -> (p,cf)::acc) acc cfl) res pl in
List.fold_left f [] body
(** @deprecated unify [e1] and [e2] instantiating existential variables in [e2]. *)
let unify_exp inst e1 e2 =
let rec go (inst,pl) e1 e2 = match (e1,e2) with
| e1, Eident _ ->
(try
let e3 = List.assq (Id.of_exp e2) inst in
go (inst,pl) e1 e3
with Not_found ->
if existential_id e2 then
let i2 = Id.of_exp e2 in
let sub = mk_single_subst i2 e1 in
let inst = List.map (fun (t,e) -> (t,sub e)) inst in
Some ((i2,e1)::inst, pl)
else if equal_exp e1 e2 then Some (inst,pl)
else None)
| Enum n1, Enum n2 -> if n1 = n2 then Some (inst,pl) else None
| e1, e2 -> if equal_exp e1 e2 then Some (inst,pl) else None
and go_list res el1 el2 = match (el1,el2) with
| [], [] -> Some res
| [],_::_ -> None
| _::_,[] -> None
| e1::el1,e2::el2 ->
begin match go res e1 e2 with
| None -> None
| Some res -> go_list res el1 el2
end
in
go (inst,Pure.ptrue) e1 e2
let ie_leq = function
(Cem_PE, _) -> true
| (Cem_NE, Cem_PE) -> false
| (Cem_NE, Cem_NE) -> true
let can_spred_leq csp1 csp2 = match (csp1,csp2) with
| (Csp_listsegi(tag,SINGLE(s1,cel),e1,f1,e2,f2,ie,el),
Csp_listsegi(tag',SINGLE(s1',cel'),e1',f1',e2',f2',ie',el')) ->
if tag == tag' && s1==s1' && equal_exp e1 e1' && ie_leq (ie, ie')