Source file recover_trycatch.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
open Wax_lang
open Ast
let no_params (t : functype) = t.params = [||]
let target_labels (desc : location instr_desc) : label list =
match desc with
| Br (l, _)
| Br_if (l, _)
| Br_on_null (l, _)
| Br_on_non_null (l, _)
| Br_on_cast (l, _, _)
| Br_on_cast_fail (l, _, _)
| Br_on_cast_desc_eq (l, _, _, _)
| Br_on_cast_desc_eq_fail (l, _, _, _) ->
[ l ]
| Br_table (ls, _) -> ls
| Dispatch { cases; default; _ } -> cases @ [ default ]
| TryTable { catches; _ } ->
List.map
(function
| Catch (_, l) | CatchRef (_, l) | CatchAll l | CatchAllRef l -> l)
catches
| Resume (_, hs, _)
| ResumeThrow (_, _, hs, _)
| ResumeThrowRef (_, hs, _)
| On (_, hs) ->
List.filter_map
(function OnLabel (_, l) -> Some l | OnSwitch _ -> None)
hs
| _ -> []
let targets name instrs =
let found = ref false in
List.iter
(Ast_utils.iter_instr (fun i ->
if
List.exists (fun (l : label) -> l.desc = name) (target_labels i.desc)
then found := true))
instrs;
!found
let rec (i : location instr) =
let sub e rebuild =
Option.map
(fun (l, t, inner, e') -> (l, t, inner, { i with desc = rebuild e' }))
(extract_first_block e)
in
match i.desc with
| Block { label = Some l; typ; block = { desc = inner; _ } } ->
Some (l, typ, inner, { i with desc = Hole })
| Let (pats, Some e) -> sub e (fun e' -> Let (pats, Some e'))
| Br (lb, Some e) -> sub e (fun e' -> Br (lb, Some e'))
| Br_if (lb, e) -> sub e (fun e' -> Br_if (lb, e'))
| Br_table (ls, e) -> sub e (fun e' -> Br_table (ls, e'))
| Return (Some e) -> sub e (fun e' -> Return (Some e'))
| Set (x, op, e) -> sub e (fun e' -> Set (x, op, e'))
| Tee (x, e) -> sub e (fun e' -> Tee (x, e'))
| Cast (e, ty) -> sub e (fun e' -> Cast (e', ty))
| Test (e, ty) -> sub e (fun e' -> Test (e', ty))
| NonNull e -> sub e (fun e' -> NonNull e')
| StructGet (e, f) -> sub e (fun e' -> StructGet (e', f))
| GetDescriptor e -> sub e (fun e' -> GetDescriptor e')
| UnOp (op, e) -> sub e (fun e' -> UnOp (op, e'))
| BinOp (op, a, b) -> sub a (fun a' -> BinOp (op, a', b))
| ThrowRef e -> sub e (fun e' -> ThrowRef e')
| Throw (t, a :: args) -> sub a (fun a' -> Throw (t, a' :: args))
| Call (({ desc = Get _; _ } as f), a :: args) ->
sub a (fun a' -> Call (f, a' :: args))
| TailCall (({ desc = Get _; _ } as f), a :: args) ->
sub a (fun a' -> TailCall (f, a' :: args))
| _ -> None
let rec descend join block =
match block with
| [
{
desc =
Br
( (j : label),
Some { desc = TryTable { label = None; typ; catches; block }; _ } );
_;
};
]
when j.desc = join && no_params typ && typ.results <> [||] ->
Some ([], typ, catches, block)
| [
{ desc = TryTable { label = None; typ; catches; block }; _ };
{ desc = Br ((j : label), None); _ };
]
when j.desc = join && no_params typ && typ.results = [||] ->
Some ([], typ, catches, block)
| { desc = Block { label = Some l; typ; block = { desc = inner; _ } }; _ }
:: body
when no_params typ -> (
match descend join inner with
| Some (arms, ttyp, catches, tbody) ->
Some ((l, typ.results, body) :: arms, ttyp, catches, tbody)
| None -> None)
| first :: body -> (
match extract_first_block first with
| Some (l, typ, inner, first') when no_params typ -> (
match descend join inner with
| Some (arms, ttyp, catches, tbody) ->
Some
((l, typ.results, first' :: body) :: arms, ttyp, catches, tbody)
| None -> None)
| _ -> None)
| [] -> None
let rec rewrite_instr (i : location instr) : location instr =
match try_fold i with
| Some folded -> folded
| None ->
let d = rewrite_desc i.desc in
if d == i.desc then i else { i with desc = d }
and rewrite_list l = Ast_utils.smart_map rewrite_instr l
and try_fold (i : location instr) : location instr option =
match i.desc with
| Block { label = Some join; typ; block } when no_params typ -> (
match descend join.desc block.desc with
| Some (rev_arms, ttyp, catches, tbody) when catches <> [] -> (
let blocks = List.rev rev_arms in
if List.length blocks <> List.length catches then None
else if
not (Array.length ttyp.results = Array.length typ.results)
then None
else
let arm (catch : catch) ((l : label), types, body) =
let tag, ref_, target =
match catch with
| Catch (t, tl) -> (Some t, false, tl)
| CatchRef (t, tl) -> (Some t, true, tl)
| CatchAll tl -> (None, false, tl)
| CatchAllRef tl -> (None, true, tl)
in
if target.desc <> l.desc then None
else
Some
{
arm_tag = tag;
arm_ref = ref_;
arm_types = types;
arm_body = no_loc body;
}
in
let rec build catches blocks =
match (catches, blocks) with
| [], [] -> Some []
| c :: cs, b :: bs -> (
match (arm c b, build cs bs) with
| Some a, Some rest -> Some (a :: rest)
| _ -> None)
| _ -> None
in
match build catches blocks with
| Some arms
when
List.for_all
(fun a -> a.arm_tag <> None)
(match List.rev arms with
| [] -> []
| _ :: init_rev -> init_rev)
&& (let all_bodies =
tbody.desc
@ List.concat_map (fun a -> a.arm_body.desc) arms
in
List.for_all
(fun ((l : label), _, _) ->
not (targets l.desc all_bodies))
blocks)
&&
let names =
List.map (fun ((l : label), _, _) -> l.desc) blocks
in
List.length (List.sort_uniq compare names)
= List.length names ->
let all_bodies =
tbody.desc @ List.concat_map (fun a -> a.arm_body.desc) arms
in
let label =
if targets join.desc all_bodies then Some join else None
in
Some
{
i with
desc =
TryCatch
{
label;
typ;
block = { tbody with desc = rewrite_list tbody.desc };
arms =
List.map
(fun a ->
{
a with
arm_body =
{
a.arm_body with
desc = rewrite_list a.arm_body.desc;
};
})
arms;
};
}
| _ -> None)
| _ -> None)
| _ -> None
and rewrite_desc (desc : location instr_desc) : location instr_desc =
Ast_utils.map_desc ~instr:rewrite_instr ~block:rewrite_list desc
let rec field_desc (f : location modulefield) =
let map_fields =
List.map (fun (a : (location modulefield, _) Ast.annotated) ->
{ a with desc = field_desc a.desc })
in
match f with
| Func ({ body = label, instrs; _ } as r) ->
Func { r with body = (label, rewrite_list instrs) }
| Conditional ({ then_fields; else_fields; _ } as r) ->
Conditional
{
r with
then_fields = { then_fields with desc = map_fields then_fields.desc };
else_fields =
Option.map
(fun (b :
( (location modulefield, location) Ast.annotated list,
location )
Ast.annotated) -> { b with desc = map_fields b.desc })
else_fields;
}
| ( Type _ | Module_annotation _ | Import _ | Import_group _ | Global _
| Tag _ | Memory _ | Data _ | Table _ | Elem _ ) as f ->
f
let module_ (m : location module_) : location module_ =
List.map
(fun (a : (location modulefield, location) Ast.annotated) ->
{ a with desc = field_desc a.desc })
m