@@ -658,13 +658,34 @@ let configure formatter =
658
658
Code.Var. set_pretty (pretty && not (Config.Flag. shortvar () ));
659
659
Code.Var. set_stable (Config.Flag. stable_var () )
660
660
661
- let full ~standalone ~wrap_with_fun ~profile ~link ~source_map formatter d p =
662
- let exported_runtime = not standalone in
661
+ type 'a target =
662
+ | JavaScript : Pretty_print .t -> Source_map .t option target
663
+ | Wasm
664
+ : (Deadcode .variable_uses * Effects .in_cps * Code .program * Parse_bytecode.Debug .t )
665
+ target
666
+
667
+ let link_and_pack ?(standalone = true ) ?(wrap_with_fun = `Iife ) ?(link = `No ) p =
663
668
let export_runtime =
664
669
match link with
665
670
| `All | `All_from _ -> true
666
671
| `Needed | `No -> false
667
672
in
673
+ p
674
+ |> link' ~export_runtime ~standalone ~link
675
+ |> pack ~wrap_with_fun ~standalone
676
+ |> coloring
677
+ |> check_js
678
+
679
+ let full
680
+ (type result )
681
+ ~(target : result target )
682
+ ~standalone
683
+ ~wrap_with_fun
684
+ ~profile
685
+ ~link
686
+ ~source_map
687
+ d
688
+ p : result =
668
689
let deadcode_sentinal =
669
690
(* If deadcode is disabled, this field is just fresh variable *)
670
691
Code.Var. fresh_n " undef"
@@ -677,56 +698,71 @@ let full ~standalone ~wrap_with_fun ~profile ~link ~source_map formatter d p =
677
698
| O3 -> o3)
678
699
+> exact_calls ~deadcode_sentinal profile
679
700
+> effects ~deadcode_sentinal
680
- +> map_fst (if Config.Flag. effects () then fun x -> x else Generate_closure. f)
701
+ +> map_fst
702
+ (match target with
703
+ | JavaScript _ -> if Config.Flag. effects () then Fun. id else Generate_closure. f
704
+ | Wasm -> Fun. id)
681
705
+> map_fst deadcode'
682
706
in
683
- let emit =
684
- generate
685
- d
686
- ~exported_runtime
687
- ~wrap_with_fun
688
- ~warn_on_unhandled_effect: standalone
689
- ~deadcode_sentinal
690
- +> link' ~export_runtime ~standalone ~link
691
- +> pack ~wrap_with_fun ~standalone
692
- +> coloring
693
- +> check_js
694
- +> output formatter ~source_map ()
695
- in
696
707
if times () then Format. eprintf " Start Optimizing...@." ;
697
708
let t = Timer. make () in
698
709
let r = opt p in
699
710
let () = if times () then Format. eprintf " optimizations : %a@." Timer. print t in
700
- emit r
711
+ match target with
712
+ | JavaScript formatter ->
713
+ let exported_runtime = not standalone in
714
+ let emit formatter =
715
+ generate
716
+ d
717
+ ~exported_runtime
718
+ ~wrap_with_fun
719
+ ~warn_on_unhandled_effect: standalone
720
+ ~deadcode_sentinal
721
+ +> link_and_pack ~standalone ~wrap_with_fun ~link
722
+ +> output formatter ~source_map ()
723
+ in
724
+ let source_map = emit formatter r in
725
+ source_map
726
+ | Wasm ->
727
+ let (p, live_vars), _, in_cps = r in
728
+ live_vars, in_cps, p, d
701
729
702
- let full_no_source_map ~standalone ~wrap_with_fun ~profile ~link formatter d p =
730
+ let full_no_source_map ~formatter ~ standalone ~wrap_with_fun ~profile ~link d p =
703
731
let (_ : Source_map.t option ) =
704
- full ~standalone ~wrap_with_fun ~profile ~link ~source_map: None formatter d p
732
+ full
733
+ ~target: (JavaScript formatter)
734
+ ~standalone
735
+ ~wrap_with_fun
736
+ ~profile
737
+ ~link
738
+ ~source_map: None
739
+ d
740
+ p
705
741
in
706
742
()
707
743
708
744
let f
745
+ ~target
709
746
?(standalone = true )
710
747
?(wrap_with_fun = `Iife )
711
748
?(profile = O1 )
712
749
~link
713
750
?source_map
714
- formatter
715
751
d
716
752
p =
717
- full ~standalone ~wrap_with_fun ~profile ~link ~source_map formatter d p
753
+ full ~target ~ standalone ~wrap_with_fun ~profile ~link ~source_map d p
718
754
719
755
let f' ?(standalone = true ) ?(wrap_with_fun = `Iife ) ?(profile = O1 ) ~link formatter d p =
720
- full_no_source_map ~standalone ~wrap_with_fun ~profile ~link formatter d p
756
+ full_no_source_map ~formatter ~ standalone ~wrap_with_fun ~profile ~link d p
721
757
722
758
let from_string ~prims ~debug s formatter =
723
759
let p, d = Parse_bytecode. from_string ~prims ~debug s in
724
760
full_no_source_map
761
+ ~formatter
725
762
~standalone: false
726
763
~wrap_with_fun: `Anonymous
727
764
~profile: O1
728
765
~link: `No
729
- formatter
730
766
d
731
767
p
732
768
0 commit comments