Skip to content

Commit 72876ed

Browse files
committed
Compiler: remove last argument of Pushtrap
1 parent 273f6b5 commit 72876ed

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
@@ -355,7 +355,7 @@ type last =
355355
| Branch of cont
356356
| Cond of Var.t * cont * cont
357357
| Switch of Var.t * cont array
358-
| Pushtrap of cont * Var.t * cont * Addr.Set.t
358+
| Pushtrap of cont * Var.t * cont
359359
| Poptrap of cont
360360

361361
type block =
@@ -503,17 +503,8 @@ module Print = struct
503503
Format.fprintf f "switch %a {" Var.print x;
504504
Array.iteri a1 ~f:(fun i c -> Format.fprintf f "int %d -> %a; " i cont c);
505505
Format.fprintf f "}"
506-
| Pushtrap (cont1, x, cont2, pcs) ->
507-
Format.fprintf
508-
f
509-
"pushtrap %a handler %a => %a continuation %s"
510-
cont
511-
cont1
512-
Var.print
513-
x
514-
cont
515-
cont2
516-
(String.concat ~sep:", " (List.map (Addr.Set.elements pcs) ~f:string_of_int))
506+
| Pushtrap (cont1, x, cont2) ->
507+
Format.fprintf f "pushtrap %a handler %a => %a" cont cont1 Var.print x cont cont2
517508
| Poptrap c -> Format.fprintf f "poptrap %a" cont c
518509

519510
type xinstr =
@@ -589,7 +580,7 @@ let fold_children blocks pc f accu =
589580
match fst block.branch with
590581
| Return _ | Raise _ | Stop -> accu
591582
| Branch (pc', _) | Poptrap (pc', _) -> f pc' accu
592-
| Pushtrap ((pc', _), _, (pc_h, _), _) ->
583+
| Pushtrap ((pc', _), _, (pc_h, _)) ->
593584
let accu = f pc' accu in
594585
let accu = f pc_h accu in
595586
accu
@@ -624,6 +615,39 @@ let rec traverse' { fold } f pc visited blocks acc =
624615

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

618+
let poptraps blocks pc =
619+
let rec loop blocks pc visited depth acc =
620+
if Addr.Set.mem pc visited
621+
then acc, visited
622+
else
623+
let visited = Addr.Set.add pc visited in
624+
let block = Addr.Map.find pc blocks in
625+
match fst block.branch with
626+
| Return _ | Raise _ | Stop -> acc, visited
627+
| Branch (pc', _) -> loop blocks pc' visited depth acc
628+
| Poptrap (pc', _) ->
629+
if depth = 0
630+
then Addr.Set.add pc' acc, visited
631+
else loop blocks pc' visited (depth - 1) acc
632+
| Pushtrap ((pc', _), _, (pc_h, _)) ->
633+
let acc, visited = loop blocks pc' visited (depth + 1) acc in
634+
let acc, visited = loop blocks pc_h visited depth acc in
635+
acc, visited
636+
| Cond (_, (pc1, _), (pc2, _)) ->
637+
let acc, visited = loop blocks pc1 visited depth acc in
638+
let acc, visited = loop blocks pc2 visited depth acc in
639+
acc, visited
640+
| Switch (_, a) ->
641+
let acc, visited =
642+
Array.fold_right
643+
~init:(acc, visited)
644+
~f:(fun (pc, _) (acc, visited) -> loop blocks pc visited depth acc)
645+
a
646+
in
647+
acc, visited
648+
in
649+
loop blocks pc Addr.Set.empty 0 Addr.Set.empty |> fst
650+
627651
let rec preorder_traverse' { fold } f pc visited blocks acc =
628652
if not (Addr.Set.mem pc visited)
629653
then
@@ -725,7 +749,7 @@ let invariant { blocks; start; _ } =
725749
check_cont cont1;
726750
check_cont cont2
727751
| Switch (_x, a1) -> Array.iteri a1 ~f:(fun _ cont -> check_cont cont)
728-
| Pushtrap (cont1, _x, cont2, _pcs) ->
752+
| Pushtrap (cont1, _x, cont2) ->
729753
check_cont cont1;
730754
check_cont cont2
731755
| Poptrap cont -> check_cont cont

compiler/lib/code.mli

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ type last =
202202
| Branch of cont
203203
| Cond of Var.t * cont * cont
204204
| Switch of Var.t * cont array
205-
| Pushtrap of cont * Var.t * cont * Addr.Set.t
205+
| Pushtrap of cont * Var.t * cont
206206
| Poptrap of cont
207207

208208
type block =
@@ -262,6 +262,8 @@ val fold_closures_innermost_first :
262262

263263
val fold_children : 'c fold_blocs
264264

265+
val poptraps : block Addr.Map.t -> Addr.t -> Addr.Set.t
266+
265267
val traverse :
266268
fold_blocs_poly -> (Addr.t -> 'c -> 'c) -> Addr.t -> block Addr.Map.t -> 'c -> 'c
267269

compiler/lib/deadcode.ml

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

@@ -151,12 +151,8 @@ let filter_live_last blocks st (l, loc) =
151151
Cond (x, filter_cont blocks st cont1, filter_cont blocks st cont2)
152152
| Switch (x, a1) ->
153153
Switch (x, Array.map a1 ~f:(fun cont -> filter_cont blocks st cont))
154-
| Pushtrap (cont1, x, cont2, pcs) ->
155-
Pushtrap
156-
( filter_cont blocks st cont1
157-
, x
158-
, filter_cont blocks st cont2
159-
, Addr.Set.inter pcs st.reachable_blocks )
154+
| Pushtrap (cont1, x, cont2) ->
155+
Pushtrap (filter_cont blocks st cont1, x, filter_cont blocks st cont2)
160156
| Poptrap cont -> Poptrap (filter_cont blocks st cont)
161157
in
162158
l, loc
@@ -212,7 +208,7 @@ let f ({ blocks; _ } as p : Code.program) =
212208
add_cont_dep blocks defs cont1;
213209
add_cont_dep blocks defs cont2
214210
| Switch (_, a1) -> Array.iter a1 ~f:(fun cont -> add_cont_dep blocks defs cont)
215-
| Pushtrap (cont, _, cont_h, _) ->
211+
| Pushtrap (cont, _, cont_h) ->
216212
add_cont_dep blocks defs cont_h;
217213
add_cont_dep blocks defs cont
218214
| 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
@@ -406,8 +406,7 @@ let drop_exception_handler blocks =
406406
Addr.Map.fold
407407
(fun pc _ blocks ->
408408
match Addr.Map.find pc blocks with
409-
| { branch = Pushtrap (((addr, _) as cont1), _x, _cont2, addrset), loc; _ } as b
410-
-> (
409+
| { branch = Pushtrap (((addr, _) as cont1), _x, _cont2), loc; _ } as b -> (
411410
try
412411
let visited = do_not_raise addr Addr.Set.empty blocks in
413412
let b = { b with branch = Branch cont1, loc } in
@@ -418,9 +417,7 @@ let drop_exception_handler blocks =
418417
let b = Addr.Map.find pc2 blocks in
419418
let branch =
420419
match b.branch with
421-
| Poptrap ((addr, _) as cont), loc ->
422-
assert (Addr.Set.mem addr addrset);
423-
Branch cont, loc
420+
| Poptrap cont, loc -> Branch cont, loc
424421
| x -> x
425422
in
426423
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
@@ -67,7 +67,7 @@ let iter_last_free_var f l =
6767
| Switch (x, a1) ->
6868
f x;
6969
Array.iter a1 ~f:(fun c -> iter_cont_free_vars f c)
70-
| Pushtrap (cont1, _, cont2, _) ->
70+
| Pushtrap (cont1, _, cont2) ->
7171
iter_cont_free_vars f cont1;
7272
iter_cont_free_vars f cont2
7373

@@ -83,7 +83,7 @@ let iter_instr_bound_vars f i =
8383
let iter_last_bound_vars f l =
8484
match l with
8585
| Return _ | Raise _ | Stop | Branch _ | Cond _ | Switch _ | Poptrap _ -> ()
86-
| Pushtrap (_, x, _, _) -> f x
86+
| Pushtrap (_, x, _) -> f x
8787

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

compiler/lib/generate.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1709,7 +1709,7 @@ and compile_conditional st queue ~fall_through last scope_stack : _ * _ =
17091709
in
17101710
true, flush_all queue [ J.Return_statement e_opt, loc ]
17111711
| Branch cont -> compile_branch st queue cont scope_stack ~fall_through
1712-
| Pushtrap (c1, x, e1, _) ->
1712+
| Pushtrap (c1, x, e1) ->
17131713
let never_body, body = compile_branch st [] c1 scope_stack ~fall_through in
17141714
if debug () then Format.eprintf "@,}@]@,@[<hv 2>catch {@;";
17151715
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
@@ -157,7 +157,7 @@ let usages prog (global_info : Global_flow.info) : usage_kind Var.Map.t Var.Tbl.
157157
add_cont_deps cont1;
158158
add_cont_deps cont2
159159
| Switch (_, a) -> Array.iter ~f:add_cont_deps a
160-
| Pushtrap (cont, _, cont_h, _) ->
160+
| Pushtrap (cont, _, cont_h) ->
161161
add_cont_deps cont;
162162
add_cont_deps cont_h
163163
| Poptrap cont -> add_cont_deps cont)
@@ -374,7 +374,7 @@ let zero prog sentinal live_table =
374374
| Branch _, _
375375
| Cond (_, _, _), _
376376
| Switch (_, _), _
377-
| Pushtrap (_, _, _, _), _
377+
| Pushtrap (_, _, _), _
378378
| Poptrap _, _ -> block.branch
379379
in
380380
{ block with body; branch }

compiler/lib/global_flow.ml

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

0 commit comments

Comments
 (0)