@@ -23,6 +23,14 @@ let debug = Debug.find "main"
23
23
24
24
let times = Debug. find " times"
25
25
26
+ type optimized_result =
27
+ { program : Code .program
28
+ ; variable_uses : Deadcode .variable_uses
29
+ ; trampolined_calls : Effects .trampolined_calls
30
+ ; in_cps : Effects .in_cps
31
+ ; deadcode_sentinal : Code.Var .t
32
+ }
33
+
26
34
type profile =
27
35
| O1
28
36
| O2
@@ -194,14 +202,13 @@ let generate
194
202
~exported_runtime
195
203
~wrap_with_fun
196
204
~warn_on_unhandled_effect
197
- ~deadcode_sentinal
198
- ((p , live_vars ), trampolined_calls , _ ) =
205
+ { program; variable_uses; trampolined_calls; deadcode_sentinal; in_cps = _ } =
199
206
if times () then Format. eprintf " Start Generation...@." ;
200
207
let should_export = should_export wrap_with_fun in
201
208
Generate. f
202
- p
209
+ program
203
210
~exported_runtime
204
- ~live_vars
211
+ ~live_vars: variable_uses
205
212
~trampolined_calls
206
213
~should_export
207
214
~warn_on_unhandled_effect
@@ -658,13 +665,19 @@ let configure formatter =
658
665
Code.Var. set_pretty (pretty && not (Config.Flag. shortvar () ));
659
666
Code.Var. set_stable (Config.Flag. stable_var () )
660
667
661
- let full ~standalone ~wrap_with_fun ~profile ~link ~source_map formatter d p =
662
- let exported_runtime = not standalone in
668
+ let link_and_pack ?(standalone = true ) ?(wrap_with_fun = `Iife ) ?(link = `No ) p =
663
669
let export_runtime =
664
670
match link with
665
671
| `All | `All_from _ -> true
666
672
| `Needed | `No -> false
667
673
in
674
+ p
675
+ |> link' ~export_runtime ~standalone ~link
676
+ |> pack ~wrap_with_fun ~standalone
677
+ |> coloring
678
+ |> check_js
679
+
680
+ let optimize ~profile p =
668
681
let deadcode_sentinal =
669
682
(* If deadcode is disabled, this field is just fresh variable *)
670
683
Code.Var. fresh_n " dummy"
@@ -677,31 +690,31 @@ let full ~standalone ~wrap_with_fun ~profile ~link ~source_map formatter d p =
677
690
| O3 -> o3)
678
691
+> exact_calls ~deadcode_sentinal profile
679
692
+> effects ~deadcode_sentinal
680
- +> map_fst (if Config.Flag. effects () then fun x -> x else Generate_closure. f)
693
+ +> map_fst
694
+ (match Config. target () , Config.Flag. effects () with
695
+ | `JavaScript , false -> Generate_closure. f
696
+ | `JavaScript , true | `Wasm , _ -> Fun. id)
681
697
+> map_fst deadcode'
682
698
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
699
if times () then Format. eprintf " Start Optimizing...@." ;
697
700
let t = Timer. make () in
698
- let r = opt p in
701
+ let (program, variable_uses), trampolined_calls, in_cps = opt p in
699
702
let () = if times () then Format. eprintf " optimizations : %a@." Timer. print t in
700
- emit r
703
+ { program; variable_uses; trampolined_calls; in_cps; deadcode_sentinal }
704
+
705
+ let full ~standalone ~wrap_with_fun ~profile ~link ~source_map ~formatter d p =
706
+ let optimized_code = optimize ~profile p in
707
+ let exported_runtime = not standalone in
708
+ let emit formatter =
709
+ generate d ~exported_runtime ~wrap_with_fun ~warn_on_unhandled_effect: standalone
710
+ +> link_and_pack ~standalone ~wrap_with_fun ~link
711
+ +> output formatter ~source_map ()
712
+ in
713
+ emit formatter optimized_code
701
714
702
- let full_no_source_map ~standalone ~wrap_with_fun ~profile ~link formatter d p =
715
+ let full_no_source_map ~formatter ~ standalone ~wrap_with_fun ~profile ~link d p =
703
716
let (_ : Source_map.t option ) =
704
- full ~standalone ~wrap_with_fun ~profile ~link ~source_map: None formatter d p
717
+ full ~standalone ~wrap_with_fun ~profile ~link ~source_map: None ~ formatter d p
705
718
in
706
719
()
707
720
@@ -711,22 +724,22 @@ let f
711
724
?(profile = O1 )
712
725
~link
713
726
?source_map
714
- formatter
727
+ ~ formatter
715
728
d
716
729
p =
717
- full ~standalone ~wrap_with_fun ~profile ~link ~source_map formatter d p
730
+ full ~standalone ~wrap_with_fun ~profile ~link ~source_map ~ formatter d p
718
731
719
732
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
733
+ full_no_source_map ~formatter ~ standalone ~wrap_with_fun ~profile ~link d p
721
734
722
735
let from_string ~prims ~debug s formatter =
723
736
let p, d = Parse_bytecode. from_string ~prims ~debug s in
724
737
full_no_source_map
738
+ ~formatter
725
739
~standalone: false
726
740
~wrap_with_fun: `Anonymous
727
741
~profile: O1
728
742
~link: `No
729
- formatter
730
743
d
731
744
p
732
745
0 commit comments