@@ -461,12 +461,15 @@ let cps_last ~st ~alloc_jump_closures pc ((last, last_loc) : last * loc) ~k :
461461 ( alloc_jump_closures
462462 , ( Switch (x, Array. map c1 ~f: cps_jump_cont, Array. map c2 ~f: cps_jump_cont)
463463 , last_loc ) )
464- | Pushtrap (body_cont , exn , ((handler_pc , _ ) as handler_cont ), _ ) -> (
464+ | Pushtrap (body_cont , exn , ((handler_pc , _ ) as handler_cont ), poptraps ) -> (
465465 assert (Hashtbl. mem st.is_continuation handler_pc);
466466 match Addr.Set. mem handler_pc st.blocks_to_transform with
467- | false -> alloc_jump_closures, (last, last_loc)
467+ | false ->
468+ let body_cont = cps_cont_of_direct ~st body_cont in
469+ let handler_cont = cps_cont_of_direct ~st handler_cont in
470+ let last = Pushtrap (body_cont, exn , handler_cont, poptraps) in
471+ alloc_jump_closures, (last, last_loc)
468472 | true ->
469- let handler_cps_cont = cps_cont_of_direct ~st handler_cont in
470473 let constr_cont, exn_handler =
471474 allocate_continuation
472475 ~st
@@ -475,7 +478,8 @@ let cps_last ~st ~alloc_jump_closures pc ((last, last_loc) : last * loc) ~k :
475478 ~direct_pc: handler_pc
476479 pc
477480 exn
478- handler_cps_cont
481+ handler_cont (* We pass the direct pc, the mapping to CPS is made
482+ by the called functions. *)
479483 last_loc
480484 in
481485 mark_single_version ~st exn_handler;
@@ -777,7 +781,25 @@ let rewrite_direct_block
777781 { block with body }, subst
778782
779783(* Apply a substitution in a set of blocks *)
780- let subst_blocks blocks s =
784+ let subst_in_blocks blocks s =
785+ Addr.Map. mapi
786+ (fun pc block ->
787+ if debug ()
788+ then (
789+ debug_print " @[<v>block before first subst: @," ;
790+ Code.Print. block (fun _ _ -> " " ) pc block;
791+ debug_print " @]" );
792+ let res = Subst. block s block in
793+ if debug ()
794+ then (
795+ debug_print " @[<v>block after first subst: @," ;
796+ Code.Print. block (fun _ _ -> " " ) pc res;
797+ debug_print " @]" );
798+ res)
799+ blocks
800+
801+ (* Apply a substitution in a set of blocks, including to bound variables *)
802+ let subst_bound_in_blocks blocks s =
781803 Addr.Map. mapi
782804 (fun pc block ->
783805 if debug ()
@@ -824,9 +846,8 @@ let cps_transform ~lifter_functions ~live_vars ~flow_info ~cps_needed p =
824846 match name_opt with
825847 | Some name -> Var.Set. mem name cps_needed
826848 | None ->
827- (* We are handling the toplevel code. There may remain
828- some CPS calls at toplevel. *)
829- true
849+ (* The toplevel code does not need to be in CPS. *)
850+ false
830851 in
831852 let blocks_to_transform, matching_exn_handler, is_continuation =
832853 if should_compute_needed_transformations
@@ -871,10 +892,8 @@ let cps_transform ~lifter_functions ~live_vars ~flow_info ~cps_needed p =
871892 should_compute_needed_transformations
872893 && not (Var.Set. mem name lifter_functions)
873894 | None ->
874- (* We are handling the toplevel code. If it performs no
875- CPS call, we can leave it in direct style and we
876- don't need to wrap it within a [caml_callback]. *)
877- not (Addr.Set. is_empty blocks_to_transform)
895+ (* The toplevel code does not need to be in CPS. *)
896+ false
878897 in
879898 if debug ()
880899 then (
@@ -963,7 +982,7 @@ let cps_transform ~lifter_functions ~live_vars ~flow_info ~cps_needed p =
963982 Var.Set. fold (fun v m -> Var.Map. add v (Var. fork v) m) bound Var.Map. empty
964983 |> Subst. from_map
965984 in
966- let cps_blocks = subst_blocks cps_blocks s in
985+ let cps_blocks = subst_bound_in_blocks cps_blocks s in
967986 (* Also apply susbstitution to set of CPS calls and lifter functions *)
968987 st.cps_calls := Var.Set. map s ! (st.cps_calls);
969988 st.single_version_closures := Var.Set. map s ! (st.single_version_closures);
@@ -972,7 +991,7 @@ let cps_transform ~lifter_functions ~live_vars ~flow_info ~cps_needed p =
972991 [rewrite_direct], because CPS closures are only ever defined in (toplevel)
973992 direct-style blocks). *)
974993 let direct_subst = Subst. from_map direct_subst in
975- let cps_blocks = subst_blocks cps_blocks direct_subst in
994+ let cps_blocks = subst_in_blocks cps_blocks direct_subst in
976995 (* Also apply susbstitution to set of CPS calls and lifter functions *)
977996 st.cps_calls := Var.Set. map direct_subst ! (st.cps_calls);
978997 st.single_version_closures :=
0 commit comments