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
type item =
| Cond of {
key : Ast.location;
cond : Ast.cond;
then_ : item list;
else_ : item list option;
}
| Body of { rank : int; items : item list }
type run = int
type key = int * int
let key_of (l : Ast.location) : key = (l.loc_start.pos_cnum, l.loc_end.pos_cnum)
module Bdd_tbl = Hashtbl.Make (struct
type t = Cond_solver.t
let equal = Cond_solver.equal
let hash = Cond_solver.hash
end)
type t = {
decisions : (run * key, bool) Hashtbl.t;
owners : (key * bool, run) Hashtbl.t;
node_owner : (key, run) Hashtbl.t;
assumptions : (run, Cond_solver.t) Hashtbl.t;
env : Cond_solver.env;
n_runs : int;
truncated : bool;
items : item list;
}
let max_runs = 4096
let rec direct items =
List.concat_map
(function
| Cond { key; _ } -> [ key_of key ] | Body { items; _ } -> direct items)
items
let rec branches items =
List.concat_map
(function
| Cond { key; then_; else_; _ } ->
let k = key_of key in
((k, true) :: branches then_)
@ Option.fold ~none:[] ~some:(fun e -> (k, false) :: branches e) else_
| Body { items; _ } -> branches items)
items
let make ?(exhaustive = false) diagnostics items =
let env = Cond_solver.create () in
let formulas = Hashtbl.create 16 in
let formula location cond =
let k = key_of location in
match Hashtbl.find_opt formulas k with
| Some f -> f
| None ->
let f = Cond_solver.of_cond env diagnostics ~location cond in
Hashtbl.replace formulas k f;
f
in
let decisions = Hashtbl.create 16 in
let owners = Hashtbl.create 16 in
let node_owner = Hashtbl.create 16 in
let assumptions = Hashtbl.create 16 in
let n_runs = ref 0 in
let truncated = ref false in
List.iter (fun k -> Hashtbl.replace node_owner k 0) (direct items);
let walk r ~seed ~decide =
let asm = ref seed in
let bodies = ref [] in
let claim k side inside =
if not (Hashtbl.mem owners (k, side)) then begin
Hashtbl.replace owners (k, side) r;
List.iter (fun k' -> Hashtbl.replace node_owner k' r) (direct inside)
end
in
let rec conds items =
List.iter
(function
| Cond { key; cond; then_; else_ } ->
let k = key_of key in
let f = formula key cond in
let d = decide k ~asm:!asm f ~has_else:(else_ <> None) in
Hashtbl.replace decisions (r, k) d;
asm := Cond_solver.and_ !asm (if d then f else Cond_solver.not_ f);
let inside = if d then Some then_ else else_ in
Option.iter
(fun inside ->
claim k d inside;
conds inside)
inside
| Body { rank; items } -> bodies := (rank, items) :: !bodies)
items
in
conds items;
let bodies =
List.stable_sort (fun (a, _) (b, _) -> compare a b) (List.rev !bodies)
in
List.iter (fun (_, items) -> conds items) bodies;
Hashtbl.replace assumptions r !asm
in
let queue = Queue.create () in
Queue.push Cond_solver.true_ queue;
let seen = Bdd_tbl.create 16 in
let pending = Hashtbl.create 16 in
while (not (Queue.is_empty queue)) && not !truncated do
let seed = Queue.pop queue in
if not (Bdd_tbl.mem seen seed) then
if !n_runs >= max_runs then truncated := true
else begin
Bdd_tbl.add seen seed ();
let r = !n_runs in
incr n_runs;
walk r ~seed ~decide:(fun k ~asm f ~has_else ->
let d = Cond_solver.is_satisfiable (Cond_solver.and_ asm f) in
let other = not d in
if
exhaustive
|| (other || has_else)
&& (not (Hashtbl.mem owners (k, other)))
&& not (Hashtbl.mem pending (k, other))
then begin
let seed' =
Cond_solver.and_ asm (if other then f else Cond_solver.not_ f)
in
if Cond_solver.is_satisfiable seed' then begin
Hashtbl.replace pending (k, other) ();
Queue.push seed' queue
end
end;
d)
end
done;
let all = if exhaustive then [] else branches items in
let progress = ref true in
while !progress do
progress := false;
List.iter
(fun (k, side) ->
if not (Hashtbl.mem owners (k, side)) then
match Hashtbl.find_opt node_owner k with
| None -> ()
| Some parent ->
progress := true;
let r = !n_runs in
incr n_runs;
walk r ~seed:Cond_solver.false_
~decide:(fun k' ~asm:_ _f ~has_else:_ ->
if k' = k then side
else
match Hashtbl.find_opt decisions (parent, k') with
| Some d -> d
| None -> false))
all
done;
{
decisions;
owners;
node_owner;
assumptions;
env;
n_runs = !n_runs;
truncated = !truncated;
items;
}
let runs t = List.init t.n_runs Fun.id
let truncated t = t.truncated
let assumption t r = Hashtbl.find t.assumptions r
let explain t ?style f = Cond_solver.explain t.env ?style f
let primary _ = 0
let select t r (location : Ast.location) =
match Hashtbl.find_opt t.decisions (r, key_of location) with
| Some d -> d
| None ->
failwith
(Printf.sprintf
"Cond_plan.select: run %d never reaches the conditional at %d-%d" r
location.loc_start.pos_cnum location.loc_end.pos_cnum)
let owner t location side = Hashtbl.find_opt t.owners (key_of location, side)
let select_owned t (location : Ast.location) =
let k = key_of location in
match Hashtbl.find_opt t.node_owner k with
| Some r -> select t r location
| None ->
failwith
(Printf.sprintf "Cond_plan.select_owned: unplanned conditional at %d-%d"
location.loc_start.pos_cnum location.loc_end.pos_cnum)
let text_shape fields =
let rec instrs l = List.concat_map instr l
and instr (i : _ Ast.Text.instr) =
match i.desc with
| If_annotation { cond; then_body; else_body } ->
[
Cond
{
key = i.info;
cond;
then_ = instrs then_body.desc;
else_ =
Option.map
(fun (b : (_ list, _) Ast.annotated) -> instrs b.desc)
else_body;
};
]
| Block { block; _ } | Loop { block; _ } | TryTable { block; _ } ->
instrs block.desc
| If { if_block; else_block; _ } ->
instrs if_block.desc @ instrs else_block.desc
| Try { block; catches; catch_all; _ } ->
instrs block.desc
@ List.concat_map
(fun (_, (b : (_ list, _) Ast.annotated)) -> instrs b.desc)
catches
@ Option.fold ~none:[]
~some:(fun (b : (_ list, _) Ast.annotated) -> instrs b.desc)
catch_all
| Folded (h, operands) -> instrs operands @ instr h
| _ -> []
in
let body rank l =
match instrs l with [] -> [] | items -> [ Body { rank; items } ]
in
let rec fields_ l =
List.concat_map
(fun (f : (_ Ast.Text.modulefield, _) Ast.annotated) ->
match f.desc with
| Module_if_annotation { cond; then_fields; else_fields } ->
[
Cond
{
key = f.info;
cond;
then_ = fields_ then_fields.desc;
else_ =
Option.map
(fun (e : (_ list, _) Ast.annotated) -> fields_ e.desc)
else_fields;
};
]
| Func { instrs = l; _ } -> body 1 l
| Global { init; _ } -> body 0 init
| Data { mode = Active (_, off); _ } -> body 0 off
| Elem { init; mode; _ } ->
body 0
(List.concat init
@
match mode with
| Active (_, off) -> off
| Passive | Declare -> [])
| Table { init = Init_expr e; _ } -> body 0 e
| Table { init = Init_segment exprs; _ } -> body 0 (List.concat exprs)
| _ -> [])
l
in
fields_ fields
let rec located_branches items =
List.concat_map
(function
| Cond { key; then_; else_; _ } ->
((key, true) :: located_branches then_)
@ Option.fold ~none:[]
~some:(fun e -> (key, false) :: located_branches e)
else_
| Body { items; _ } -> located_branches items)
items
let dead_branches t =
List.filter
(fun (location, side) ->
let k = key_of location in
(not (Hashtbl.mem t.owners (k, side)))
&& Hashtbl.mem t.node_owner k)
(located_branches t.items)