Source file Splitter.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
open Cil_types
open Cil_datatype
type tag =
| THEN of stmt
| ELSE of stmt
| CALL of stmt * kernel_function
| CASE of stmt * int64 list
| DEFAULT of stmt
| ASSERT of identified_predicate * int * int
let pretty fmt = function
| THEN _ -> Format.fprintf fmt "Then"
| ELSE _ -> Format.fprintf fmt "Else"
| CASE(_,[]) -> Format.fprintf fmt "Case(s)"
| CASE(_,[k]) -> Format.fprintf fmt "Case %s" (Int64.to_string k)
| CASE(_,k::ks) ->
Format.fprintf fmt "@[Cases %s" (Int64.to_string k) ;
List.iter (fun k -> Format.fprintf fmt ",@,%s" (Int64.to_string k)) ks ;
Format.fprintf fmt "@]"
| CALL(_,kf) -> Format.fprintf fmt "Call %a" Kernel_function.pretty kf
| DEFAULT _ -> Format.fprintf fmt "Default"
| ASSERT(_,k,n) -> Format.fprintf fmt "Disjunction (%d/%d)" k n
let loc = function
| THEN s | ELSE s | CASE(s,_) | CALL(s,_) | DEFAULT s -> Stmt.loc s
| ASSERT(p,_,_) -> p.ip_content.tp_statement.pred_loc
let compare p q =
if p == q then 0 else
match p,q with
| THEN s , THEN t -> Stmt.compare s t
| THEN _ , _ -> (-1)
| _ , THEN _ -> 1
| ELSE s , ELSE t -> Stmt.compare s t
| ELSE _ , _ -> (-1)
| _ , ELSE _ -> 1
| CASE(s1,k1) , CASE(s2,k2) ->
let c = Stmt.compare s1 s2 in
if c = 0 then Stdlib.compare k1 k2 else c
| CASE _ , _ -> (-1)
| _ , CASE _ -> 1
| DEFAULT s , DEFAULT t -> Stmt.compare s t
| DEFAULT _ , _ -> (-1)
| _ , DEFAULT _ -> 1
| CALL(s1,f1) , CALL(s2,f2) ->
let c = Stmt.compare s1 s2 in
if c = 0 then Kernel_function.compare f1 f2 else c
| CALL _ , _ -> (-1)
| _ , CALL _ -> 1
| ASSERT(ip1,k1,_) , ASSERT(ip2,k2,_) ->
let c = Stdlib.compare ip1.ip_id ip2.ip_id in
if c = 0 then k1 - k2 else c
let rec disjunction p =
try unwrap p
with Exit -> [p]
and unwrap p =
match p.pred_content with
| Por(a,b) -> disjunction a @ disjunction b
| Plet(f,a) ->
List.map
(fun q -> { p with pred_content = Plet(f,q) })
(unwrap a)
| Pexists(qs,p) ->
List.map
(fun q -> { p with pred_content = Pexists(qs,q) })
(unwrap p)
| Pat(p,l) ->
List.map
(fun q -> { p with pred_content = Pat(q,l) })
(unwrap p)
| _ -> raise Exit
let predicate ip = ip.ip_content.tp_statement
let rec enumerate ip k n = function
| [] -> []
| p::ps -> (ASSERT(ip,k,n),p) :: enumerate ip (succ k) n ps
let cases ip =
try
let ps = unwrap (predicate ip) in
Some (enumerate ip 1 (List.length ps) ps)
with Exit -> None
let switch_cases stmt ks = CASE(stmt,ks)
let switch_default stmt = DEFAULT stmt
let if_then stmt = THEN stmt
let if_else stmt = ELSE stmt
let call stmt kf = CALL(stmt,kf)
module Tags = Qed.Listset.Make
(struct
type t = tag
let compare = compare
let equal x y = (compare x y = 0)
end)
module M = Qed.Listmap.Make(Tags)
module I = Map.Make(Tags)
type 'a t = 'a M.t
let rec compact merge = function
| ([] | [_]) as m -> m
| ( (k1,v1) as e )::(( (k2,v2)::r ) as m) ->
if Tags.compare k1 k2 = 0 then
collect merge k1 [v2;v1] r
else
e :: compact merge m
and collect merge k vs = function
| [] -> [k,merge vs]
| ((k',v')::r) as m ->
if Tags.compare k k' = 0 then
collect merge k (v'::vs) r
else
(k,merge vs) :: compact merge m
let bytags (k,_) (k',_) = Tags.compare k k'
let apply tag merge m =
let compaction = ref false in
let m = List.sort bytags
(List.map
(fun (tgs,v) ->
if not !compaction && Tags.mem tag tgs then
compaction := true ;
Tags.add tag tgs , v) m)
in if !compaction then compact merge m else m
let length = List.length
let empty = []
let singleton e = [[],e]
let unmark merge m = [[] , merge (List.map snd m)]
let union merge m1 m2 = M.union (fun _ -> merge) m1 m2
let rec merge ~left ~both ~right m1 m2 =
match m1 , m2 with
| [],[] -> []
| _,[] -> List.map (fun (k,v) -> k , left v) m1
| [],_ -> List.map (fun (k,v) -> k , right v) m2
| (k1,v1)::w1 , (k2,v2)::w2 ->
let cmp = Tags.compare k1 k2 in
if cmp < 0 then
(k1 , left v1) :: merge ~left ~both ~right w1 m2
else if cmp > 0 then
(k2 , right v2) :: merge ~left ~both ~right m1 w2
else
(k1 , both v1 v2) :: merge ~left ~both ~right w1 w2
let merge_all merge = function
| [] -> []
| [m] -> m
| [m1;m2] -> M.union (fun _ u v -> merge [u;v]) m1 m2
| ms ->
let t = ref I.empty in
List.iter
(List.iter
(fun (k,v) ->
try
let r = (I.find k !t) in r := v :: !r
with Not_found ->
t := I.add k (ref [v]) !t))
ms ;
I.fold
(fun k r m -> match !r with
| [] -> m
| [v] -> (k,v)::m
| vs -> (k,merge vs)::m)
!t []
let map = M.map
let iter = M.iter
let fold = M.fold
let exists f xs = List.exists (fun (_,x) -> f x) xs
let for_all f xs = List.for_all (fun (_,x) -> f x) xs
let filter f xs = List.filter (fun (_,x) -> f x) xs