Skip to content

Commit 46ef5fa

Browse files
authored
Compiler: remove last argument of Pushtrap (#1575)
1 parent 123504b commit 46ef5fa

16 files changed

+72
-96
lines changed

compiler/lib/code.ml

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ type last =
360360
| Branch of cont
361361
| Cond of Var.t * cont * cont
362362
| Switch of Var.t * cont array
363-
| Pushtrap of cont * Var.t * cont * Addr.Set.t
363+
| Pushtrap of cont * Var.t * cont
364364
| Poptrap of cont
365365

366366
type block =
@@ -514,17 +514,8 @@ module Print = struct
514514
Format.fprintf f "switch %a {" Var.print x;
515515
Array.iteri a1 ~f:(fun i c -> Format.fprintf f "int %d -> %a; " i cont c);
516516
Format.fprintf f "}"
517-
| Pushtrap (cont1, x, cont2, pcs) ->
518-
Format.fprintf
519-
f
520-
"pushtrap %a handler %a => %a continuation %s"
521-
cont
522-
cont1
523-
Var.print
524-
x
525-
cont
526-
cont2
527-
(String.concat ~sep:", " (List.map (Addr.Set.elements pcs) ~f:string_of_int))
517+
| Pushtrap (cont1, x, cont2) ->
518+
Format.fprintf f "pushtrap %a handler %a => %a" cont cont1 Var.print x cont cont2
528519
| Poptrap c -> Format.fprintf f "poptrap %a" cont c
529520

530521
type xinstr =
@@ -600,7 +591,7 @@ let fold_children blocks pc f accu =
600591
match fst block.branch with
601592
| Return _ | Raise _ | Stop -> accu
602593
| Branch (pc', _) | Poptrap (pc', _) -> f pc' accu
603-
| Pushtrap ((pc', _), _, (pc_h, _), _) ->
594+
| Pushtrap ((pc', _), _, (pc_h, _)) ->
604595
let accu = f pc' accu in
605596
let accu = f pc_h accu in
606597
accu
@@ -635,6 +626,39 @@ let rec traverse' { fold } f pc visited blocks acc =
635626

636627
let traverse fold f pc blocks acc = snd (traverse' fold f pc Addr.Set.empty blocks acc)
637628

629+
let poptraps blocks pc =
630+
let rec loop blocks pc visited depth acc =
631+
if Addr.Set.mem pc visited
632+
then acc, visited
633+
else
634+
let visited = Addr.Set.add pc visited in
635+
let block = Addr.Map.find pc blocks in
636+
match fst block.branch with
637+
| Return _ | Raise _ | Stop -> acc, visited
638+
| Branch (pc', _) -> loop blocks pc' visited depth acc
639+
| Poptrap (pc', _) ->
640+
if depth = 0
641+
then Addr.Set.add pc' acc, visited
642+
else loop blocks pc' visited (depth - 1) acc
643+
| Pushtrap ((pc', _), _, (pc_h, _)) ->
644+
let acc, visited = loop blocks pc' visited (depth + 1) acc in
645+
let acc, visited = loop blocks pc_h visited depth acc in
646+
acc, visited
647+
| Cond (_, (pc1, _), (pc2, _)) ->
648+
let acc, visited = loop blocks pc1 visited depth acc in
649+
let acc, visited = loop blocks pc2 visited depth acc in
650+
acc, visited
651+
| Switch (_, a) ->
652+
let acc, visited =
653+
Array.fold_right
654+
~init:(acc, visited)
655+
~f:(fun (pc, _) (acc, visited) -> loop blocks pc visited depth acc)
656+
a
657+
in
658+
acc, visited
659+
in
660+
loop blocks pc Addr.Set.empty 0 Addr.Set.empty |> fst
661+
638662
let rec preorder_traverse' { fold } f pc visited blocks acc =
639663
if not (Addr.Set.mem pc visited)
640664
then
@@ -737,7 +761,7 @@ let invariant { blocks; start; _ } =
737761
check_cont cont1;
738762
check_cont cont2
739763
| Switch (_x, a1) -> Array.iteri a1 ~f:(fun _ cont -> check_cont cont)
740-
| Pushtrap (cont1, _x, cont2, _pcs) ->
764+
| Pushtrap (cont1, _x, cont2) ->
741765
check_cont cont1;
742766
check_cont cont2
743767
| Poptrap cont -> check_cont cont

compiler/lib/code.mli

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ type last =
207207
| Branch of cont
208208
| Cond of Var.t * cont * cont
209209
| Switch of Var.t * cont array
210-
| Pushtrap of cont * Var.t * cont * Addr.Set.t
210+
| Pushtrap of cont * Var.t * cont
211211
| Poptrap of cont
212212

213213
type block =
@@ -267,6 +267,8 @@ val fold_closures_innermost_first :
267267

268268
val fold_children : 'c fold_blocs
269269

270+
val poptraps : block Addr.Map.t -> Addr.t -> Addr.Set.t
271+
270272
val traverse :
271273
fold_blocs_poly -> (Addr.t -> 'c -> 'c) -> Addr.t -> block Addr.Map.t -> 'c -> 'c
272274

compiler/lib/deadcode.ml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ and mark_reachable st pc =
115115
| Switch (x, a1) ->
116116
mark_var st x;
117117
Array.iter a1 ~f:(fun cont -> mark_cont_reachable st cont)
118-
| Pushtrap (cont1, _, cont2, _) ->
118+
| Pushtrap (cont1, _, cont2) ->
119119
mark_cont_reachable st cont1;
120120
mark_cont_reachable st cont2)
121121

@@ -152,12 +152,8 @@ let filter_live_last blocks st (l, loc) =
152152
Cond (x, filter_cont blocks st cont1, filter_cont blocks st cont2)
153153
| Switch (x, a1) ->
154154
Switch (x, Array.map a1 ~f:(fun cont -> filter_cont blocks st cont))
155-
| Pushtrap (cont1, x, cont2, pcs) ->
156-
Pushtrap
157-
( filter_cont blocks st cont1
158-
, x
159-
, filter_cont blocks st cont2
160-
, Addr.Set.inter pcs st.reachable_blocks )
155+
| Pushtrap (cont1, x, cont2) ->
156+
Pushtrap (filter_cont blocks st cont1, x, filter_cont blocks st cont2)
161157
| Poptrap cont -> Poptrap (filter_cont blocks st cont)
162158
in
163159
l, loc
@@ -213,7 +209,7 @@ let f ({ blocks; _ } as p : Code.program) =
213209
add_cont_dep blocks defs cont1;
214210
add_cont_dep blocks defs cont2
215211
| Switch (_, a1) -> Array.iter a1 ~f:(fun cont -> add_cont_dep blocks defs cont)
216-
| Pushtrap (cont, _, cont_h, _) ->
212+
| Pushtrap (cont, _, cont_h) ->
217213
add_cont_dep blocks defs cont_h;
218214
add_cont_dep blocks defs cont
219215
| Poptrap cont -> add_cont_dep blocks defs cont)

compiler/lib/effects.ml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ let compute_needed_transformations ~cfg ~idom ~cps_needed ~blocks ~start =
191191
List.iter ~f:mark_needed englobing_exn_handlers;
192192
mark_continuation dst x
193193
| _ -> ())
194-
| Pushtrap (_, x, (handler_pc, _), _) -> mark_continuation handler_pc x
194+
| Pushtrap (_, x, (handler_pc, _)) -> mark_continuation handler_pc x
195195
| Poptrap _ | Raise _ -> (
196196
match englobing_exn_handlers with
197197
| handler_pc :: _ -> Hashtbl.add matching_exn_handler pc handler_pc
@@ -203,7 +203,7 @@ let compute_needed_transformations ~cfg ~idom ~cps_needed ~blocks ~start =
203203
(fun pc visited ->
204204
let englobing_exn_handlers =
205205
match block.branch with
206-
| Pushtrap (_, _, (handler_pc, _), _), _ when pc <> handler_pc ->
206+
| Pushtrap (_, _, (handler_pc, _)), _ when pc <> handler_pc ->
207207
handler_pc :: englobing_exn_handlers
208208
| Poptrap _, _ -> List.tl englobing_exn_handlers
209209
| _ -> englobing_exn_handlers
@@ -423,7 +423,7 @@ let cps_last ~st ~alloc_jump_closures pc ((last, last_loc) : last * loc) ~k :
423423
to create a single block per continuation *)
424424
let cps_jump_cont = Fun.memoize (fun x -> cps_jump_cont ~st ~src:pc x last_loc) in
425425
alloc_jump_closures, (Switch (x, Array.map c1 ~f:cps_jump_cont), last_loc)
426-
| Pushtrap (body_cont, exn, ((handler_pc, _) as handler_cont), _) -> (
426+
| Pushtrap (body_cont, exn, ((handler_pc, _) as handler_cont)) -> (
427427
assert (Hashtbl.mem st.is_continuation handler_pc);
428428
match Addr.Set.mem handler_pc st.blocks_to_transform with
429429
| false -> alloc_jump_closures, (last, last_loc)
@@ -910,8 +910,7 @@ let remove_empty_blocks ~live_vars (p : Code.program) : Code.program =
910910
| Branch cont -> Branch (resolve cont)
911911
| Cond (x, cont1, cont2) -> Cond (x, resolve cont1, resolve cont2)
912912
| Switch (x, a1) -> Switch (x, Array.map ~f:resolve a1)
913-
| Pushtrap (cont1, x, cont2, s) ->
914-
Pushtrap (resolve cont1, x, resolve cont2, s)
913+
| Pushtrap (cont1, x, cont2) -> Pushtrap (resolve cont1, x, resolve cont2)
915914
| Poptrap cont -> Poptrap (resolve cont)
916915
| Return _ | Raise _ | Stop -> branch
917916
in

compiler/lib/eval.ml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,7 @@ let drop_exception_handler blocks =
407407
Addr.Map.fold
408408
(fun pc _ blocks ->
409409
match Addr.Map.find pc blocks with
410-
| { branch = Pushtrap (((addr, _) as cont1), _x, _cont2, addrset), loc; _ } as b
411-
-> (
410+
| { branch = Pushtrap (((addr, _) as cont1), _x, _cont2), loc; _ } as b -> (
412411
try
413412
let visited = do_not_raise addr Addr.Set.empty blocks in
414413
let b = { b with branch = Branch cont1, loc } in
@@ -419,9 +418,7 @@ let drop_exception_handler blocks =
419418
let b = Addr.Map.find pc2 blocks in
420419
let branch =
421420
match b.branch with
422-
| Poptrap ((addr, _) as cont), loc ->
423-
assert (Addr.Set.mem addr addrset);
424-
Branch cont, loc
421+
| Poptrap cont, loc -> Branch cont, loc
425422
| x -> x
426423
in
427424
let b = { b with branch } in

compiler/lib/flow.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ let program_deps { blocks; _ } =
122122
cont_deps blocks vars deps defs cont2
123123
| Switch (_, a1) ->
124124
Array.iter a1 ~f:(fun cont -> cont_deps blocks vars deps defs cont)
125-
| Pushtrap (cont, x, cont_h, _) ->
125+
| Pushtrap (cont, x, cont_h) ->
126126
add_param_def vars defs x;
127127
cont_deps blocks vars deps defs cont_h;
128128
cont_deps blocks vars deps defs cont)

compiler/lib/freevars.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ let iter_last_free_var f l =
6868
| Switch (x, a1) ->
6969
f x;
7070
Array.iter a1 ~f:(fun c -> iter_cont_free_vars f c)
71-
| Pushtrap (cont1, _, cont2, _) ->
71+
| Pushtrap (cont1, _, cont2) ->
7272
iter_cont_free_vars f cont1;
7373
iter_cont_free_vars f cont2
7474

@@ -84,7 +84,7 @@ let iter_instr_bound_vars f i =
8484
let iter_last_bound_vars f l =
8585
match l with
8686
| Return _ | Raise _ | Stop | Branch _ | Cond _ | Switch _ | Poptrap _ -> ()
87-
| Pushtrap (_, x, _, _) -> f x
87+
| Pushtrap (_, x, _) -> f x
8888

8989
let iter_block_bound_vars f block =
9090
List.iter ~f block.params;

compiler/lib/generate.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1704,7 +1704,7 @@ and compile_conditional st queue ~fall_through last scope_stack : _ * _ =
17041704
in
17051705
true, flush_all queue [ J.Return_statement e_opt, loc ]
17061706
| Branch cont -> compile_branch st queue cont scope_stack ~fall_through
1707-
| Pushtrap (c1, x, e1, _) ->
1707+
| Pushtrap (c1, x, e1) ->
17081708
let never_body, body = compile_branch st [] c1 scope_stack ~fall_through in
17091709
if debug () then Format.eprintf "@,}@]@,@[<hv 2>catch {@;";
17101710
let never_handler, handler = compile_branch st [] e1 scope_stack ~fall_through in

compiler/lib/global_deadcode.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ let usages prog (global_info : Global_flow.info) : usage_kind Var.Map.t Var.Tbl.
158158
add_cont_deps cont1;
159159
add_cont_deps cont2
160160
| Switch (_, a) -> Array.iter ~f:add_cont_deps a
161-
| Pushtrap (cont, _, cont_h, _) ->
161+
| Pushtrap (cont, _, cont_h) ->
162162
add_cont_deps cont;
163163
add_cont_deps cont_h
164164
| Poptrap cont -> add_cont_deps cont)
@@ -380,7 +380,7 @@ let zero prog sentinal live_table =
380380
| Branch _, _
381381
| Cond (_, _, _), _
382382
| Switch (_, _), _
383-
| Pushtrap (_, _, _, _), _
383+
| Pushtrap (_, _, _), _
384384
| Poptrap _, _ -> block.branch
385385
in
386386
{ block with body; branch }

compiler/lib/global_flow.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ let program_deps st { blocks; _ } =
275275
block.body)
276276
h
277277
| Expr _ | Phi _ -> ())
278-
| Pushtrap (cont, x, cont_h, _) ->
278+
| Pushtrap (cont, x, cont_h) ->
279279
add_var st x;
280280
st.defs.(Var.idx x) <- Phi { known = Var.Set.empty; others = true };
281281
cont_deps blocks st cont_h;

compiler/lib/inline.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ let fold_children blocks pc f accu =
107107
match fst block.branch with
108108
| Return _ | Raise _ | Stop -> accu
109109
| Branch (pc', _) | Poptrap (pc', _) -> f pc' accu
110-
| Pushtrap (_, _, (pc1, _), pcs) -> f pc1 (Addr.Set.fold f pcs accu)
110+
| Pushtrap ((try_body, _), _, (pc1, _)) ->
111+
f pc1 (Addr.Set.fold f (Code.poptraps blocks try_body) accu)
111112
| Cond (_, (pc1, _), (pc2, _)) ->
112113
let accu = f pc1 accu in
113114
let accu = f pc2 accu in

0 commit comments

Comments
 (0)