Skip to content

Commit c860567

Browse files
hhugoOlivierNicole
authored andcommitted
Compiler: remove last argument of Pushtrap (#1575)
1 parent 6dc6697 commit c860567

18 files changed

+85
-103
lines changed

compiler/lib/code.ml

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ type last =
365365
| Branch of cont
366366
| Cond of Var.t * cont * cont
367367
| Switch of Var.t * cont array * cont array
368-
| Pushtrap of cont * Var.t * cont * Addr.Set.t
368+
| Pushtrap of cont * Var.t * cont
369369
| Poptrap of cont
370370

371371
type block =
@@ -528,17 +528,8 @@ module Print = struct
528528
Array.iteri a1 ~f:(fun i c -> Format.fprintf f "int %d -> %a; " i cont c);
529529
Array.iteri a2 ~f:(fun i c -> Format.fprintf f "tag %d -> %a; " i cont c);
530530
Format.fprintf f "}"
531-
| Pushtrap (cont1, x, cont2, pcs) ->
532-
Format.fprintf
533-
f
534-
"pushtrap %a handler %a => %a continuation %s"
535-
cont
536-
cont1
537-
Var.print
538-
x
539-
cont
540-
cont2
541-
(String.concat ~sep:", " (List.map (Addr.Set.elements pcs) ~f:string_of_int))
531+
| Pushtrap (cont1, x, cont2) ->
532+
Format.fprintf f "pushtrap %a handler %a => %a" cont cont1 Var.print x cont cont2
542533
| Poptrap c -> Format.fprintf f "poptrap %a" cont c
543534

544535
type xinstr =
@@ -609,12 +600,51 @@ let is_empty p =
609600
| _ -> false)
610601
| _ -> false
611602

603+
let poptraps blocks pc =
604+
let rec loop blocks pc visited depth acc =
605+
if Addr.Set.mem pc visited
606+
then acc, visited
607+
else
608+
let visited = Addr.Set.add pc visited in
609+
let block = Addr.Map.find pc blocks in
610+
match fst block.branch with
611+
| Return _ | Raise _ | Stop -> acc, visited
612+
| Branch (pc', _) -> loop blocks pc' visited depth acc
613+
| Poptrap (pc', _) ->
614+
if depth = 0
615+
then Addr.Set.add pc' acc, visited
616+
else loop blocks pc' visited (depth - 1) acc
617+
| Pushtrap ((pc', _), _, (pc_h, _)) ->
618+
let acc, visited = loop blocks pc' visited (depth + 1) acc in
619+
let acc, visited = loop blocks pc_h visited depth acc in
620+
acc, visited
621+
| Cond (_, (pc1, _), (pc2, _)) ->
622+
let acc, visited = loop blocks pc1 visited depth acc in
623+
let acc, visited = loop blocks pc2 visited depth acc in
624+
acc, visited
625+
| Switch (_, a1, a2) ->
626+
let acc, visited =
627+
Array.fold_right
628+
~init:(acc, visited)
629+
~f:(fun (pc, _) (acc, visited) -> loop blocks pc visited depth acc)
630+
a1
631+
in
632+
let acc, visited =
633+
Array.fold_right
634+
~init:(acc, visited)
635+
~f:(fun (pc, _) (acc, visited) -> loop blocks pc visited depth acc)
636+
a2
637+
in
638+
acc, visited
639+
in
640+
loop blocks pc Addr.Set.empty 0 Addr.Set.empty |> fst
641+
612642
let fold_children blocks pc f accu =
613643
let block = Addr.Map.find pc blocks in
614644
match fst block.branch with
615645
| Return _ | Raise _ | Stop -> accu
616646
| Branch (pc', _) | Poptrap (pc', _) -> f pc' accu
617-
| Pushtrap ((pc', _), _, (pc_h, _), _) ->
647+
| Pushtrap ((pc', _), _, (pc_h, _)) ->
618648
let accu = f pc' accu in
619649
let accu = f pc_h accu in
620650
accu
@@ -632,8 +662,8 @@ let fold_children_skip_try_body blocks pc f accu =
632662
match fst block.branch with
633663
| Return _ | Raise _ | Stop -> accu
634664
| Branch (pc', _) | Poptrap (pc', _) -> f pc' accu
635-
| Pushtrap (_, _, (pc_h, _), pcs) ->
636-
let accu = Addr.Set.fold f pcs accu in
665+
| Pushtrap ((pc', _), _, (pc_h, _)) ->
666+
let accu = Addr.Set.fold f (poptraps blocks pc') accu in
637667
let accu = f pc_h accu in
638668
accu
639669
| Cond (_, (pc1, _), (pc2, _)) ->
@@ -791,7 +821,7 @@ let invariant { blocks; start; _ } =
791821
| Switch (_x, a1, a2) ->
792822
Array.iteri a1 ~f:(fun _ cont -> check_cont cont);
793823
Array.iteri a2 ~f:(fun _ cont -> check_cont cont)
794-
| Pushtrap (cont1, _x, cont2, _pcs) ->
824+
| Pushtrap (cont1, _x, cont2) ->
795825
check_cont cont1;
796826
check_cont cont2
797827
| Poptrap cont -> check_cont cont

compiler/lib/code.mli

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ type last =
212212
| Branch of cont
213213
| Cond of Var.t * cont * cont
214214
| Switch of Var.t * cont array * cont array
215-
| Pushtrap of cont * Var.t * cont * Addr.Set.t
215+
| Pushtrap of cont * Var.t * cont
216216
| Poptrap of cont
217217

218218
type block =
@@ -276,6 +276,8 @@ val fold_children : 'c fold_blocs
276276

277277
val fold_children_skip_try_body : 'c fold_blocs
278278

279+
val poptraps : block Addr.Map.t -> Addr.t -> Addr.Set.t
280+
279281
val traverse :
280282
fold_blocs_poly -> (Addr.t -> 'c -> 'c) -> Addr.t -> block Addr.Map.t -> 'c -> 'c
281283

compiler/lib/deadcode.ml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ and mark_reachable st pc =
102102
mark_var st x;
103103
Array.iter a1 ~f:(fun cont -> mark_cont_reachable st cont);
104104
Array.iter a2 ~f:(fun cont -> mark_cont_reachable st cont)
105-
| Pushtrap (cont1, _, cont2, _) ->
105+
| Pushtrap (cont1, _, cont2) ->
106106
mark_cont_reachable st cont1;
107107
mark_cont_reachable st cont2)
108108

@@ -142,12 +142,8 @@ let filter_live_last blocks st (l, loc) =
142142
( x
143143
, Array.map a1 ~f:(fun cont -> filter_cont blocks st cont)
144144
, Array.map a2 ~f:(fun cont -> filter_cont blocks st cont) )
145-
| Pushtrap (cont1, x, cont2, pcs) ->
146-
Pushtrap
147-
( filter_cont blocks st cont1
148-
, x
149-
, filter_cont blocks st cont2
150-
, Addr.Set.inter pcs st.reachable_blocks )
145+
| Pushtrap (cont1, x, cont2) ->
146+
Pushtrap (filter_cont blocks st cont1, x, filter_cont blocks st cont2)
151147
| Poptrap cont -> Poptrap (filter_cont blocks st cont)
152148
in
153149
l, loc
@@ -208,7 +204,7 @@ let f ({ blocks; _ } as p : Code.program) =
208204
| Switch (_, a1, a2) ->
209205
Array.iter a1 ~f:(fun cont -> add_cont_dep blocks defs cont);
210206
Array.iter a2 ~f:(fun cont -> add_cont_dep blocks defs cont)
211-
| Pushtrap (cont, _, cont_h, _) ->
207+
| Pushtrap (cont, _, cont_h) ->
212208
add_cont_dep blocks defs cont_h;
213209
add_cont_dep blocks defs cont
214210
| 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
@@ -438,7 +438,7 @@ let cps_last ~st ~alloc_jump_closures pc ((last, last_loc) : last * loc) ~k :
438438
( alloc_jump_closures
439439
, ( Switch (x, Array.map c1 ~f:cps_jump_cont, Array.map c2 ~f:cps_jump_cont)
440440
, last_loc ) )
441-
| Pushtrap (body_cont, exn, ((handler_pc, _) as handler_cont), _) -> (
441+
| Pushtrap (body_cont, exn, ((handler_pc, _) as handler_cont)) -> (
442442
assert (Hashtbl.mem st.is_continuation handler_pc);
443443
match Addr.Set.mem handler_pc st.blocks_to_transform with
444444
| false -> alloc_jump_closures, (last, last_loc)
@@ -931,8 +931,7 @@ let remove_empty_blocks ~live_vars (p : Code.program) : Code.program =
931931
| Cond (x, cont1, cont2) -> Cond (x, resolve cont1, resolve cont2)
932932
| Switch (x, a1, a2) ->
933933
Switch (x, Array.map ~f:resolve a1, Array.map ~f:resolve a2)
934-
| Pushtrap (cont1, x, cont2, s) ->
935-
Pushtrap (resolve cont1, x, resolve cont2, s)
934+
| Pushtrap (cont1, x, cont2) -> Pushtrap (resolve cont1, x, resolve cont2)
936935
| Poptrap cont -> Poptrap (resolve cont)
937936
| Return _ | Raise _ | Stop -> branch
938937
in

compiler/lib/eval.ml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,7 @@ let drop_exception_handler blocks =
444444
Addr.Map.fold
445445
(fun pc _ blocks ->
446446
match Addr.Map.find pc blocks with
447-
| { branch = Pushtrap (((addr, _) as cont1), _x, _cont2, addrset), loc; _ } as b
448-
-> (
447+
| { branch = Pushtrap (((addr, _) as cont1), _x, _cont2), loc; _ } as b -> (
449448
try
450449
let visited = do_not_raise addr Addr.Set.empty blocks in
451450
let b = { b with branch = Branch cont1, loc } in
@@ -456,9 +455,7 @@ let drop_exception_handler blocks =
456455
let b = Addr.Map.find pc2 blocks in
457456
let branch =
458457
match b.branch with
459-
| Poptrap ((addr, _) as cont), loc ->
460-
assert (Addr.Set.mem addr addrset);
461-
Branch cont, loc
458+
| Poptrap cont, loc -> Branch cont, loc
462459
| x -> x
463460
in
464461
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
| Switch (_, a1, a2) ->
123123
Array.iter a1 ~f:(fun cont -> cont_deps blocks vars deps defs cont);
124124
Array.iter a2 ~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
@@ -69,7 +69,7 @@ let iter_last_free_var f l =
6969
f x;
7070
Array.iter a1 ~f:(fun c -> iter_cont_free_vars f c);
7171
Array.iter a2 ~f:(fun c -> iter_cont_free_vars f c)
72-
| Pushtrap (cont1, _, cont2, _) ->
72+
| Pushtrap (cont1, _, cont2) ->
7373
iter_cont_free_vars f cont1;
7474
iter_cont_free_vars f cont2
7575

@@ -85,7 +85,7 @@ let iter_instr_bound_vars f i =
8585
let iter_last_bound_vars f l =
8686
match l with
8787
| Return _ | Raise _ | Stop | Branch _ | Cond _ | Switch _ | Poptrap _ -> ()
88-
| Pushtrap (_, x, _, _) -> f x
88+
| Pushtrap (_, x, _) -> f x
8989

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

compiler/lib/generate.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ let fold_children blocks pc f accu =
758758
match fst block.branch with
759759
| Return _ | Raise _ | Stop -> accu
760760
| Branch (pc', _) | Poptrap (pc', _) -> f pc' accu
761-
| Pushtrap ((pc1, _), _, (pc2, _), _) ->
761+
| Pushtrap ((pc1, _), _, (pc2, _)) ->
762762
let accu = f pc1 accu in
763763
let accu = f pc2 accu in
764764
accu
@@ -805,7 +805,7 @@ let build_graph ctx pc =
805805
List.iter pc_succs ~f:(fun pc' ->
806806
let pushtrap =
807807
match fst b.branch with
808-
| Pushtrap ((pc1, _), _, (pc2, _), _remove) ->
808+
| Pushtrap ((pc1, _), _, (pc2, _)) ->
809809
if pc' = pc1
810810
then (
811811
Hashtbl.add poptrap pc Addr.Set.empty;
@@ -1866,7 +1866,7 @@ and compile_conditional st queue last loop_stack backs frontier interm =
18661866
in
18671867
true, flush_all queue [ J.Return_statement e_opt, loc ]
18681868
| Branch cont -> compile_branch st queue cont loop_stack backs frontier interm
1869-
| Pushtrap (c1, x, e1, _) ->
1869+
| Pushtrap (c1, x, e1) ->
18701870
let never_body, body = compile_branch st [] c1 loop_stack backs frontier interm in
18711871
if debug () then Format.eprintf "@,}@]@,@[<hv 2>catch {@;";
18721872
let never_handler, handler =

compiler/lib/global_flow.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ let program_deps st { blocks; _ } =
273273
| _ -> ())
274274
block.body)
275275
h
276-
| Pushtrap (cont, x, cont_h, _) ->
276+
| Pushtrap (cont, x, cont_h) ->
277277
add_var st x;
278278
st.defs.(Var.idx x) <- Phi { known = Var.Set.empty; others = true };
279279
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)