-
Notifications
You must be signed in to change notification settings - Fork 2
/
back_dataflow.ml
1106 lines (941 loc) · 24.7 KB
/
back_dataflow.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
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
open Cil
open Cil_types
open Cil_datatype
open Dataflow2
open Logic_const
(*
module Pid = State_builder.SharedCounter
(struct let name = "predicate_counter" end)
let new_pid = Pid.next
*)
let dkey_loop = Caret_option.register_category "back_dataflow:loop"
let dkey_stmt = Caret_option.register_category "back_dataflow:doStmt"
let dkey_if = Caret_option.register_category "back_dataflow:if_cond"
type if_tag = Then | Else
type if_stmt = stmt * if_tag
(** 1. Dataflow *)
let (if_stmt_hashtbl : if_stmt Stmt.Hashtbl.t) = Stmt.Hashtbl.create 12
let update_table if_stmt b1 b2 =
List.iter
(fun s -> Stmt.Hashtbl.add if_stmt_hashtbl s (if_stmt,Then))
b1;
List.iter
(fun s -> Stmt.Hashtbl.add if_stmt_hashtbl s (if_stmt,Else))
b2
let (loop_hashtbl : stmt Stmt.Hashtbl.t) = Stmt.Hashtbl.create 12
let update_loop_table loop_stmt b =
List.iter
(fun s -> Stmt.Hashtbl.add loop_hashtbl s loop_stmt)
b
let lvar_created = ref []
module CafeStartData:(
StmtStartData with type data = predicate * (logic_var list) )
=
StartData (struct type t = predicate * (logic_var list) let size = 103 end)
module type P = sig val pred : predicate * (logic_var list) end
module Efac (Pred : P):
BackwardsTransfer with type t = predicate * (logic_var list) =
struct
module StmtStartData = CafeStartData
let name = "CaFE_backward_analysis"
let debug = true
type t = CafeStartData.data
let treated_stmt = ref Stmt.Set.empty
(* 1. Variable manipulation through the dataflow *)
let var_map = ref Varinfo.Map.empty
let map_add v lv = var_map := Varinfo.Map.add v lv !var_map
let var_map_init () =
let vis =
object
inherit Visitor.frama_c_inplace
method! vlogic_var_use var =
match var.lv_origin with
Some v ->
map_add v var; SkipChildren
| None -> assert false
end
in
Cil.visitCilPredicate (vis :> Cil.cilVisitor) (fst Pred.pred)
let () = ignore (var_map_init ())
let get_lvar var =
try Varinfo.Map.find var !var_map
with
Not_found ->
let () =
Caret_option.debug
"@[%a@] never seen : registering"
Printer.pp_varinfo
var
in
let res = cvar_to_lvar var
in
let () =
map_add var res;
lvar_created:= res ::!lvar_created
in
res
let get_new_lvar var =
let () =
Caret_option.debug
"New variable for %a"
Printer.pp_varinfo var in
if Varinfo.Map.mem var !var_map
then
let res = make_temp_logic_var (Ctype var.vtype)
in
let () =
res.lv_name <- var.vname ^ "_" ^ (string_of_int res.lv_id);
res.lv_origin <- Some var;
map_add var res;
lvar_created := res :: !lvar_created
in
let () =
Caret_option.debug
"New representant for @[%a@] : @[%a@]"
Printer.pp_varinfo
var
Printer.pp_logic_var
res
in
res
else get_lvar var
let change_var_to_term lvar term =
object
inherit Visitor.frama_c_inplace
method! vterm t =
match t.term_node with
TLval ((TVar logic_var), _) ->
if Logic_var.equal lvar logic_var
then
ChangeTo term
else DoChildren
| _ -> DoChildren
end
let update_lvars =
object
inherit Visitor.frama_c_inplace
method! vlogic_var_use lvar =
match lvar.lv_origin with
None -> Caret_option.fatal "Bad initialisation of variable"
| Some v ->
ChangeTo (get_lvar v)
end
let correct_term_from_exp exp =
let () =
Caret_option.debug "Exp : @[%a@]" Printer.pp_exp exp
in
let exp_termed = Logic_utils.expr_to_term ~cast:false exp
in
let term =
Cil.visitCilTerm
(update_lvars :> Cil.cilVisitor)
exp_termed
in
let term =
match term.term_node with
TCastE (typ,t) ->
Logic_const.term t.term_node (Logic_utils.typ_to_logic_type typ)
| _ -> term
in
let () =
Caret_option.debug "Term : @[%a@]" Printer.pp_term term
in
term
let if_conds_as_pred (s : stmt list) : predicate =
let __if_conds_as_preds stmt =
let () = Caret_option.debug ~dkey:dkey_if ~level:3
"If statement : @[%a@]"
Printer.pp_stmt stmt
in
let term =
match stmt.skind with
If (e,_,_,_) -> correct_term_from_exp e
(*else
TBinop (LAnd,t,acc)
(** TODO : HERE WE NEED TO COMPLETE THE MULTIPLE CONDITION **)*)
| _ ->
assert false
in
match term.term_node with
TBinOp(Eq,t1,t2) ->
Prel (Req,t1,t2)
| TBinOp(Ne,t1,t2) ->
Prel (Rneq,t1,t2)
| TBinOp(Lt,t1,t2) ->
Prel (Rlt,t1,t2)
| TBinOp(Gt,t1,t2) ->
Prel (Rgt,t1,t2)
| TBinOp(Ge,t1,t2) ->
Prel (Rge,t1,t2)
| _ ->
Prel
(Rneq, term, (* 0 *)
Logic_const.term
(TConst (Integer (Integer.zero,None)))
Linteger)
in
unamed
(List.fold_left
(fun acc s ->
let pred = __if_conds_as_preds s in
if acc = Ptrue then pred else
Pand(unamed pred, unamed acc )
)
Ptrue
s
)
let update_pred_about_var vinfo exp pred =
let () =
Caret_option.debug
"Updating %a : now %a"
Printer.pp_varinfo vinfo
Printer.pp_exp exp in
let new_term = correct_term_from_exp exp
in
if not (Varinfo.Map.mem vinfo !var_map)
then
let () = Caret_option.debug "%s not registered" vinfo.vname
in
let new_var = get_new_lvar vinfo
in
unamed (Pand
((unamed pred),
(unamed (Prel (Req, (tvar new_var), new_term)))))
else
let logic_var = get_lvar vinfo
in
let () =
Caret_option.debug "@[%a@] replaced by @[%a@]"
Printer.pp_logic_var
logic_var
Printer.pp_term
new_term
in
let visitor = change_var_to_term logic_var new_term
in
(Cil.visitCilPredicate (visitor :> Cil.cilVisitor) (unamed pred))
(*
let uniformize old_vars =
(* creates a predicate binding every variable of old_vars to the actual
var registered. *)
List.fold_left
(fun acc l_var ->
let () =
Caret_option.debug
~level:2
~dkey:dkey_stmt
"Old lvar : @[%a@]"
Printer.pp_logic_var
l_var
in
match l_var.lv_origin with
None -> acc
| Some v_orig ->
try
let new_var = Varinfo.Map.find v_orig !var_map
in
if Logic_var.equal new_var l_var
then acc
else
Pand
(unamed acc,
unamed
(Prel
(Req,
Logic_const.tvar new_var,
Logic_const.tvar l_var)))
with
Not_found (* find *) -> acc
)
Ptrue
old_vars
*)
(* 2. Dataflow functions *)
let pretty fmt (p,_) = Printer.pp_predicate fmt p
let funcExitData = Pred.pred
let combineStmtStartData s ~old newd =
let () =
Caret_option.debug ~dkey:dkey_stmt ~level:2
"Statement %a :\n"
Printer.pp_stmt
s;
Caret_option.debug ~dkey:dkey_stmt ~level:6
"old = @[%a@]\npred = @[%a@] "
Printer.pp_predicate
(fst old)
Printer.pp_predicate
(fst newd)
in
if Stmt.Set.mem s !treated_stmt
then
let () =
Caret_option.debug ~dkey:dkey_stmt ~level:2 "Statement already treated" in
None
else
let () =
Caret_option.debug ~dkey:dkey_stmt ~level:2 "Statement never treated" in
let () = treated_stmt := Stmt.Set.add s !treated_stmt
in
match s.skind with
Loop _ ->
let () =
Caret_option.debug ~dkey:dkey_stmt ~level:4 "This is a loop" in
let p_old,_ = old
in
let p_new,v_new = newd
in
(* TODO : ADD LOOP ANNOTATION !! *)
let new_pred =
unamed (Pand(p_new, p_old))
in
let () =
StmtStartData.add s (new_pred,v_new)
in Some (new_pred,v_new)
| _ ->
let () =
Caret_option.debug ~dkey:dkey_stmt ~level:2 "This is not a loop, we keep the new predicate" in
Some newd
let combineSuccessors ((succ1,vars1):t) ((succ2,vars2):t) :t =
match succ1.pred_content,succ2.pred_content with
Pfalse, _ -> (succ2,vars2)
| _, Pfalse -> (succ1,vars1)
| _,_ ->
let remove_doubles l1 l2 =
let set =
List.fold_left
(fun acc l_v ->
Logic_var.Set.add l_v acc)
(Logic_var.Set.of_list l1)
l2
in
Logic_var.Set.elements set
in
(unamed (Por (succ1,succ2)))
,
(remove_doubles vars1 vars2)
let rec treat_if cond_exp b1 b2 = (* -> prefix * wp(B,T) * wlp (B,\bot) *)
let old_binds =
!var_map
(* As we modify the map in the then part, we need to
remeber the old bindings to use them in the else part. *)
in
let var_changed =
List.fold_left
(fun acc st ->
match st.skind with
Instr (Set ((Var v,_), _, _))
| Instr (Call ((Some (Var v,_)),_,_,_))
| Instr(Local_init (v,_,_))
->
Varinfo.Set.add v acc
| _ -> acc
)
Varinfo.Set.empty
(b1.bstmts @ b2.bstmts)
in
(*let var_no_double = Varinfo.Set.elements var_changed
in
*)
let treat_block block =
let () =
Caret_option.debug ~dkey:dkey_stmt ~level:4
"Block treated : @[%a@]"
Printer.pp_block block in
let stmt_action stmt (wp : predicate option) =
let in_good_if =
begin
match (fst(Stmt.Hashtbl.find if_stmt_hashtbl stmt)).skind
with
If (e,_,_,_) ->
Exp.equal e cond_exp
| _ -> assert false
end
in
if (not in_good_if)
then (wp(* ,wlp *))
else
match stmt.skind with
If (e,b1,b2,_) ->
let pref,wp_top(* ,wlp_bot *) = treat_if e b1 b2
in
let wp_part =
Pimplies
( unamed pref,
unamed wp_top(* wp_part *) )
in
Some (unamed wp_part) (* , wlp_part *)
| Instr i ->
begin
match i with
Local_init (v,AssignInit (SingleInit exp),_)
| Set ((Var v,_), exp, _) ->
let () =
Caret_option.debug ~dkey:dkey_stmt
"Instruction %a = %a"
Printer.pp_varinfo v
Printer.pp_exp exp in
(*let () =
var_used := Varinfo.Set.add v !var_used
in*)
let actual_var = get_lvar v
in
let lval_term =
tvar actual_var
in
let () =
if stmt.preds <> []
then
ignore (get_lvar v)
in
let correct_term = correct_term_from_exp exp
in
let new_pred =
Prel (Req, lval_term, correct_term)
in
let () =
Caret_option.debug ~dkey:dkey_stmt
"Predicate generated: %a"
Printer.pp_predicate_node new_pred
in
if wp = None then Some (unamed new_pred)
else
Some (unamed(Pimplies
(unamed new_pred,
((Extlib.the wp)))))(* , *)
(* (Pimplies *)
(* (new_pred, *)
(* (unamed wlp)))) *)
| _ -> (wp(* ,wlp *))(* todo : treat the rest *)
end
| Return _
| Goto _
| Break _
| Continue _
| Block _
| UnspecifiedSequence _
| Throw _
| TryCatch _
| TryFinally _
| TryExcept _ -> (wp(* ,wlp *))
| Switch _ -> assert false
| Loop (_,_,_,_,_) -> assert false
in
let rec wp_block s_list wp : Cil_types.predicate option =
match s_list with
[] -> Some (unamed Ptrue)
| hd :: [] ->
let () =
Caret_option.debug ~dkey:dkey_stmt ~level:3
"Last statement treated : @[%a@]"
Printer.pp_stmt
hd
in
(stmt_action
hd
None)
| hd :: tl ->
let () =
Caret_option.debug ~dkey:dkey_stmt ~level:3
"Statement treated : @[%a@]"
Printer.pp_stmt
hd;
match wp with
None ->
Caret_option.debug ~dkey:dkey_stmt ~level:3
"No current predicate"
| Some p ->
Caret_option.debug ~dkey:dkey_stmt ~level:3
"Current predicate : %a" Printer.pp_predicate p
in
(stmt_action
hd
(wp_block
tl
wp))
(*List.fold_right
(fun stmt (wp(*, wlp *)) -> *)
in
match wp_block block.bstmts None with
None -> unamed Ptrue
| Some p -> p
in
let () =
Caret_option.debug ~dkey:dkey_stmt ~level:2
"Then block treated"
in
let then_wp(* ,then_wlp *) = treat_block b1
in
let then_binds = !var_map
in
let () =
Varinfo.Map.iter
(fun v lv ->
Caret_option.debug
"Old : @[%a@] -> @[%a@]"
Printer.pp_varinfo v
Printer.pp_logic_var lv
)
old_binds
;
Varinfo.Map.iter
(fun v lv ->
Caret_option.debug
"Then : @[%a@] -> @[%a@]"
Printer.pp_varinfo v
Printer.pp_logic_var lv
)
then_binds
in
let () = var_map := old_binds
in
let () =
Caret_option.debug ~dkey:dkey_stmt ~level:2
"Else block treated"
in
let else_wp(* ,else_wlp *) =
let form = treat_block b2 in
(* If nothing happened in the else block, we need to update the variables
used in the then path *)
if form.pred_content = Ptrue
then (* var_map have not been modified, we will continue with the
then_map as var_map *)
let () = var_map := then_binds in
Varinfo.Map.fold
(fun var lvar acc ->
try
let old_var =
Varinfo.Map.find var old_binds
in
let eqlty =
Prel
(Req,
(Logic_const.tvar old_var),
(Logic_const.tvar lvar))
in
if acc = Ptrue then eqlty
else Pand (unamed eqlty, unamed acc)
with
Not_found ->acc
)
then_binds
form.pred_content
else form.pred_content
(* We now unify the variable out from the then part and the else part *)
in
let term_of_cond = correct_term_from_exp cond_exp
in
let () =
Caret_option.debug
"term of condition = @[%a@]" Printer.pp_term term_of_cond
in
let cond_satisfied =
unamed (
Pif
(term_of_cond, unamed Ptrue, unamed Pfalse))
in
let cond_unsatisfied =
unamed
(Pnot cond_satisfied)
in
let then_wp(* ,then_wlp *) =
Pimplies
(cond_satisfied, (then_wp))(* , *)
(* Pimplies *)
(* (cond_satisfied, (unamed then_wlp)) *)
in
let else_wp(* ,else_wlp *) =
Pimplies
(cond_unsatisfied, (unamed else_wp))(* , *)
(* Pimplies *)
(* (cond_unsatisfied, (unamed else_wlp)) *)
in
let wp_form =
Pand
((unamed then_wp),
(unamed else_wp))
in
(* let wlp_form = *)
(* Pand *)
(* ((unamed then_wlp), *)
(* (unamed else_wlp)) *)
(* in *)
let map_of_varinfos =
Varinfo.Set.fold
(fun v acc_then ->
let new_var =
get_lvar v
in
try
let then_v = Varinfo.Map.find v then_binds
in
let then_mapping =
Prel
(Req,
(Logic_const.tvar new_var),
(Logic_const.tvar then_v))
in
(then_mapping :: acc_then)
with
Not_found -> (* then_v failed, as a new variable has been seen in
else only. In this case, we don't need to talk
about this variable in the then case*)
acc_then
)
var_changed
[]
in
let bind_prefix =
match map_of_varinfos with
[] -> wp_form
| hd :: tl ->
List.fold_left
(fun acc_form bind ->
Pand
(
(unamed bind),
(unamed acc_form)
)
)
hd
tl
in
(bind_prefix,wp_form(* ,wlp_form *))
let loop_as_predicate kf loop_stmt =
(* We get proven annotations *)
let annot_pred =
List.fold_left
(fun (acc:predicate) annot ->
match annot.annot_content with
AInvariant (_,_,pred) ->
let () =
Caret_option.debug ~dkey:dkey_loop ~level:3
"Is annotation %a true ?"
Printer.pp_predicate pred in
let status = Property_status.get
(Property.ip_of_code_annot_single kf loop_stmt annot)
in
let is_true () =
begin
match status with
Property_status.Best (Property_status.True,_) ->
let () =
Caret_option.debug ~dkey:dkey_loop ~level:4
"Yes !" in true
| Property_status.Best _ ->
let () =
Caret_option.debug ~dkey:dkey_loop ~level:4
"No !" in false
| Property_status.Never_tried ->
Caret_option.debug ~dkey:dkey_loop ~level:4
"Never tried to prove it"; false
| Property_status.Inconsistent _ ->
Caret_option.debug ~dkey:dkey_loop ~level:4
"Inconsistent"; false
end
in
if Caret_option.Assert_annot.get () || is_true () then
let () =
Caret_option.debug ~dkey:dkey_loop ~level:4
"True or asserted"
in
if acc.pred_content = Ptrue
then pred
else unamed (Pand (acc,pred))
else
let () =
Caret_option.debug ~dkey:dkey_loop ~level:4
"False"
in
acc
| _ -> acc
)
(unamed Ptrue)
(Annotations.code_annot loop_stmt)
in
let () =
Caret_option.debug ~dkey:dkey_loop ~level:3
"Loop treatment done. Predicate : %a"
Printer.pp_predicate annot_pred
in
annot_pred
let doStmt s =
let () =
Caret_option.debug ~dkey:dkey_stmt "Statement treated : @[%a@]"
Printer.pp_stmt
s
in
if Stmt.Set.mem s !treated_stmt
then
(* We already treated this one, we will not treat it again *)
let () =
Caret_option.debug ~dkey:dkey_stmt "Statement previously treated"
in
Done (StmtStartData.find s)
else
let find_prev_data s =
let rec __find_prev_data (pred_acc, var_acc) succs =
try
let pred,old_vars =
StmtStartData.find (List.hd succs)
in
if pred.pred_content = Ptrue
then __find_prev_data (pred_acc,var_acc) (List.tl succs)
else
let pred_acc =
if pred_acc.pred_content = Pfalse then pred else
unamed
(Por
((pred_acc),
(pred)))
in
let var_acc =
old_vars @ var_acc
in
__find_prev_data
(pred_acc,var_acc)
(List.tl succs)
with
| Not_found -> __find_prev_data (pred_acc, var_acc) (List.tl succs)
| Failure s (*"hd"*) -> assert (s = "hd"); (pred_acc, var_acc)
in
__find_prev_data
(unamed Pfalse,[]) s.succs
in
let prev_data,vars =
find_prev_data s
(* result is useless, as we will not use it. This failure happens when we
start the analysis *)
in
let remove_doubles l =
Logic_var.Set.elements (Logic_var.Set.of_list l)
in
let real_vars = remove_doubles vars
in
let () =
if not (StmtStartData.mem s)
then
let () =
Caret_option.debug
~dkey:dkey_stmt
"Not registered"
in
StmtStartData.add s (prev_data,real_vars)
else
Caret_option.debug
~dkey:dkey_stmt
"Registered"
in
let actual_vars =
Varinfo.Map.fold
(fun _ lv acc -> lv :: acc)
!var_map
[]
in
let data =
if Stmt.Hashtbl.mem if_stmt_hashtbl s || Stmt.Hashtbl.mem loop_hashtbl s
then
Done (prev_data,real_vars)
else
match s.skind with
Instr _
| Return _
| Goto _
| Break _
| Continue _
| Block _
| UnspecifiedSequence _
| Throw _
| TryCatch _
| TryFinally _
| TryExcept _ ->
Default
| If (exp,b1,b2,_) ->
let prev_calculated_pred,_ =
try
CafeStartData.find (List.hd s.succs)
with
Not_found ->
Caret_option.fatal
"@[%a@] has not been found in registered statements."
Printer.pp_stmt
s
in
let bind_prefix,wp(* ,wlp *) = treat_if exp b1 b2
in
let form =
Pand
((unamed wp),
(prev_calculated_pred))
(* Pand *)
(* ((unamed wp), *)
(* (unamed ( *)
(* Por( *)
(* (unamed wlp), *)
(* (unamed prev_calculated_pred))))) *)
in
let form =
Pand (unamed bind_prefix, unamed form)
in
Done (unamed form,actual_vars)
| Switch _ -> assert false
| Loop (_,b,_,_,so) ->
let rec fst_if s =
match s.skind with
If _ -> s
| _ -> fst_if (List.hd s.succs)
in
let if_stmt =
(* Normally, the loop starts with the break condition.
But sometimes, a nasty statements is added in between. *)
fst_if(List.hd (Extlib.the so).succs)
in
let () = (* We compute the condition as a predicate just to visit the
condition and register every varinfo in it. *)
ignore (if_conds_as_pred [if_stmt])
in(*
let old_vars =
Varinfo.Map.fold
(fun _ lv acc ->
if List.exists (Logic_var.equal lv) acc then acc else lv :: acc)
!var_map
old_vars
in*)
let pred = loop_as_predicate (Kernel_function.find_englobing_kf s) s
in
let () = Caret_option.debug ~dkey:dkey_loop ~level:2
"Predicate of loop : %a"
Printer.pp_predicate pred in
(* pred represents at least one step of the loop. We
need to specify the case "loop not taken".*)
(* let uniformize_pred =
uniformize
old_vars
in
*) let new_vars =
Varinfo.Map.fold
(fun _ lv acc -> lv :: acc)
!var_map
[]
in
let loop_cond =
if_conds_as_pred [if_stmt]
in
let no_loop_pred =
Pnot (loop_cond)
in
let rec loop_assigns b = (* TODO : add annotation loop assign *)
List.fold_right
(fun s acc ->
(*Caret_option.debug ~dkey:dkey_stmt ~level:3
"Statement %a registered as treated" Printer.pp_stmt s;
treated_stmt := Stmt.Set.add s !treated_stmt;
StmtStartData.add s data;
*)
match s.skind with
If (_,b1,b2,_) ->
Varinfo.Set.union (loop_assigns b1) (Varinfo.Set.union (loop_assigns b2) acc)
| Block b -> Varinfo.Set.union (loop_assigns b) acc
| Instr(Set ((Var v,_),_,_))
| Instr(Local_init (v,_,_)) ->
Varinfo.Set.add v acc
| _ -> acc
) b.bstmts Varinfo.Set.empty in
let () =
Varinfo.Set.iter
(fun v -> ignore (get_new_lvar v))
(loop_assigns b) in
let data =
unamed(Pand (unamed no_loop_pred,pred))
,new_vars
in
let rec register_block_stmt (b : Cil_types.block) =
List.iter