Skip to content

Commit ced9ad2

Browse files
committed
remove Misc.maybe_map deps
1 parent dd32b4a commit ced9ad2

File tree

9 files changed

+262
-151
lines changed

9 files changed

+262
-151
lines changed

jscomp/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ CORE_SRCS= type_int_to_string type_util ocaml_stdlib_slots bs_conditional_initi
204204
js_of_lam_option js_output lam_compile_global lam_dispatch_primitive lam_beta_reduce\
205205
lam_compile_external_call lam_compile_primitive lam_compile lam_pass_exits\
206206
lam_pass_count\
207+
lam_pass_eliminate_ref\
207208
lam_pass_lets_dce \
208209
lam_pass_remove_alias lam_compile_group\
209210
js_implementation ocaml_batch_compile

jscomp/all.depend

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ core/lam_compile.cmi : core/lam_compile_defs.cmi core/lam.cmi \
292292
core/js_output.cmi core/j.cmx
293293
core/lam_pass_exits.cmi : core/lam.cmi ext/int_hashtbl.cmi
294294
core/lam_pass_count.cmi : core/lam.cmi ext/ident_hashtbl.cmi
295+
core/lam_pass_eliminate_ref.cmi : core/lam.cmi
295296
core/lam_pass_lets_dce.cmi : core/lam.cmi
296297
core/lam_pass_remove_alias.cmi : core/lam_stats.cmi core/lam.cmi
297298
core/lam_compile_group.cmi : core/j.cmx
@@ -493,10 +494,12 @@ core/lam_pass_exits.cmx : core/lam_util.cmx core/lam_beta_reduce.cmx \
493494
core/lam_pass_count.cmx : core/lam_beta_reduce.cmx core/lam.cmx \
494495
ext/ident_map.cmx ext/ident_hashtbl.cmx ext/ext_list.cmx \
495496
core/lam_pass_count.cmi
496-
core/lam_pass_lets_dce.cmx : core/lam_util.cmx core/lam_pass_count.cmx \
497+
core/lam_pass_eliminate_ref.cmx : core/lam.cmx ext/ident_set.cmx \
498+
core/lam_pass_eliminate_ref.cmi
499+
core/lam_pass_lets_dce.cmx : core/lam_util.cmx \
500+
core/lam_pass_eliminate_ref.cmx core/lam_pass_count.cmx \
497501
core/lam_beta_reduce.cmx core/lam_analysis.cmx core/lam.cmx \
498-
ext/ident_set.cmx ext/ident_hashtbl.cmx ext/ext_list.cmx \
499-
core/lam_pass_lets_dce.cmi
502+
ext/ident_hashtbl.cmx ext/ext_list.cmx core/lam_pass_lets_dce.cmi
500503
core/lam_pass_remove_alias.cmx : core/lam_util.cmx core/lam_stats.cmx \
501504
core/lam_module_ident.cmx core/lam_inline_util.cmx \
502505
core/lam_compile_env.cmx core/lam_closure.cmx core/lam_beta_reduce.cmx \

jscomp/bin/whole_compiler.d

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,8 @@ bin/whole_compiler.ml : core/lam_pass_exits.ml
340340
bin/whole_compiler.ml : core/lam_pass_exits.mli
341341
bin/whole_compiler.ml : core/lam_pass_count.ml
342342
bin/whole_compiler.ml : core/lam_pass_count.mli
343+
bin/whole_compiler.ml : core/lam_pass_eliminate_ref.ml
344+
bin/whole_compiler.ml : core/lam_pass_eliminate_ref.mli
343345
bin/whole_compiler.ml : core/lam_pass_lets_dce.ml
344346
bin/whole_compiler.ml : core/lam_pass_lets_dce.mli
345347
bin/whole_compiler.ml : core/lam_inline_util.ml

jscomp/bin/whole_compiler.ml

Lines changed: 112 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -94741,7 +94741,10 @@ let subst_helper (subst : subst_tbl) query lam =
9474194741
let new_l = simplif l
9474294742
and new_consts = List.map (fun (n, e) -> (n, simplif e)) sw.sw_consts
9474394743
and new_blocks = List.map (fun (n, e) -> (n, simplif e)) sw.sw_blocks
94744-
and new_fail = Misc.may_map simplif sw.sw_failaction in
94744+
and new_fail =
94745+
begin match sw.sw_failaction with
94746+
| None -> None
94747+
| Some x -> Some (simplif x) end in
9474594748
Lam.switch
9474694749
new_l
9474794750
{
@@ -94752,7 +94755,7 @@ let subst_helper (subst : subst_tbl) query lam =
9475294755
| Lstringswitch(l,sw,d) ->
9475394756
Lam.stringswitch
9475494757
(simplif l) (List.map (fun (s,l) -> s,simplif l) sw)
94755-
(Misc.may_map simplif d)
94758+
(begin match d with None -> None | Some d -> Some (simplif d) end)
9475694759
| Ltrywith (l1, v, l2) ->
9475794760
Lam.try_ (simplif l1) v (simplif l2)
9475894761
| Lifthenelse (l1, l2, l3) ->
@@ -95007,46 +95010,49 @@ let collect_occurs lam : occ_tbl =
9500795010

9500895011

9500995012
end
95010-
module Lam_pass_lets_dce : sig
95011-
#1 "lam_pass_lets_dce.mli"
95012-
(***********************************************************************)
95013-
(* *)
95014-
(* OCaml *)
95015-
(* *)
95016-
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
95017-
(* *)
95018-
(* Copyright 1996 Institut National de Recherche en Informatique et *)
95019-
(* en Automatique. All rights reserved. This file is distributed *)
95020-
(* under the terms of the Q Public License version 1.0. *)
95021-
(* *)
95022-
(***********************************************************************)
95023-
(* Adapted for Javascript backend: Hongbo Zhang *)
95013+
module Lam_pass_eliminate_ref : sig
95014+
#1 "lam_pass_eliminate_ref.mli"
95015+
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
95016+
*
95017+
* This program is free software: you can redistribute it and/or modify
95018+
* it under the terms of the GNU Lesser General Public License as published by
95019+
* the Free Software Foundation, either version 3 of the License, or
95020+
* (at your option) any later version.
95021+
*
95022+
* In addition to the permissions granted to you by the LGPL, you may combine
95023+
* or link a "work that uses the Library" with a publicly distributed version
95024+
* of this file to produce a combined library or application, then distribute
95025+
* that combined work under the terms of your choosing, with no requirement
95026+
* to comply with the obligations normally placed on you by section 4 of the
95027+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
95028+
* should you choose to use a later version).
95029+
*
95030+
* This program is distributed in the hope that it will be useful,
95031+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
95032+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
95033+
* GNU Lesser General Public License for more details.
95034+
*
95035+
* You should have received a copy of the GNU Lesser General Public License
95036+
* along with this program; if not, write to the Free Software
95037+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
95038+
95039+
95040+
exception Real_reference
95041+
95042+
val eliminate_ref :
95043+
Ident.t ->
95044+
Lam.t ->
95045+
Lam.t
95046+
9502495047

95025-
(**
95026-
This pass would do beta reduction, and dead code elimination (adapted from compiler's built-in [Simplif] module )
9502795048

95028-
1. beta reduction -> Llet (Strict )
95029-
95030-
2. The global table [occ] associates to each let-bound identifier
95031-
the number of its uses (as a reference):
95032-
- 0 if never used
95033-
- 1 if used exactly once in and *not under a lambda or within a loop
95034-
- > 1 if used several times or under a lambda or within a loop.
9503595049

95036-
The local table [bv] associates to each locally-let-bound variable
95037-
its reference count, as above. [bv] is enriched at let bindings
95038-
but emptied when crossing lambdas and loops.
9503995050

95040-
For this pass, when it' used under a lambda or within a loop, we don't do anything,
95041-
in theory, we can still do something if it's pure but we are conservative here.
9504295051

95043-
[bv] is used to help caculate [occ] it is not useful outside
9504495052

95045-
*)
95046-
val simplify_lets : Lam.t -> Lam.t
9504795053

9504895054
end = struct
95049-
#1 "lam_pass_lets_dce.ml"
95055+
#1 "lam_pass_eliminate_ref.ml"
9505095056
(***********************************************************************)
9505195057
(* *)
9505295058
(* OCaml *)
@@ -95061,7 +95067,7 @@ end = struct
9506195067
(* Adapted for Javascript backend : Hongbo Zhang, *)
9506295068

9506395069

95064-
open Asttypes
95070+
9506595071

9506695072
exception Real_reference
9506795073

@@ -95127,12 +95133,17 @@ let rec eliminate_ref id (lam : Lam.t) =
9512795133
sw_blocks =
9512895134
List.map (fun (n, e) -> (n, eliminate_ref id e)) sw.sw_blocks;
9512995135
sw_failaction =
95130-
Misc.may_map (eliminate_ref id) sw.sw_failaction; }
95136+
match sw.sw_failaction with
95137+
| None -> None
95138+
| Some x -> Some (eliminate_ref id x)
95139+
}
9513195140
| Lstringswitch(e, sw, default) ->
9513295141
Lam.stringswitch
9513395142
(eliminate_ref id e)
9513495143
(List.map (fun (s, e) -> (s, eliminate_ref id e)) sw)
95135-
(Misc.may_map (eliminate_ref id) default)
95144+
(match default with
95145+
| None -> None
95146+
| Some x -> Some (eliminate_ref id x))
9513695147
| Lstaticraise (i,args) ->
9513795148
Lam.staticraise i (List.map (eliminate_ref id) args)
9513895149
| Lstaticcatch(e1, i, e2) ->
@@ -95162,6 +95173,61 @@ let rec eliminate_ref id (lam : Lam.t) =
9516295173

9516395174

9516495175

95176+
end
95177+
module Lam_pass_lets_dce : sig
95178+
#1 "lam_pass_lets_dce.mli"
95179+
(***********************************************************************)
95180+
(* *)
95181+
(* OCaml *)
95182+
(* *)
95183+
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
95184+
(* *)
95185+
(* Copyright 1996 Institut National de Recherche en Informatique et *)
95186+
(* en Automatique. All rights reserved. This file is distributed *)
95187+
(* under the terms of the Q Public License version 1.0. *)
95188+
(* *)
95189+
(***********************************************************************)
95190+
(* Adapted for Javascript backend: Hongbo Zhang *)
95191+
95192+
(**
95193+
This pass would do beta reduction, and dead code elimination (adapted from compiler's built-in [Simplif] module )
95194+
95195+
1. beta reduction -> Llet (Strict )
95196+
95197+
2. The global table [occ] associates to each let-bound identifier
95198+
the number of its uses (as a reference):
95199+
- 0 if never used
95200+
- 1 if used exactly once in and *not under a lambda or within a loop
95201+
- > 1 if used several times or under a lambda or within a loop.
95202+
95203+
The local table [bv] associates to each locally-let-bound variable
95204+
its reference count, as above. [bv] is enriched at let bindings
95205+
but emptied when crossing lambdas and loops.
95206+
95207+
For this pass, when it' used under a lambda or within a loop, we don't do anything,
95208+
in theory, we can still do something if it's pure but we are conservative here.
95209+
95210+
[bv] is used to help caculate [occ] it is not useful outside
95211+
95212+
*)
95213+
val simplify_lets : Lam.t -> Lam.t
95214+
95215+
end = struct
95216+
#1 "lam_pass_lets_dce.ml"
95217+
(***********************************************************************)
95218+
(* *)
95219+
(* OCaml *)
95220+
(* *)
95221+
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
95222+
(* *)
95223+
(* Copyright 1996 Institut National de Recherche en Informatique et *)
95224+
(* en Automatique. All rights reserved. This file is distributed *)
95225+
(* under the terms of the Q Public License version 1.0. *)
95226+
(* *)
95227+
(***********************************************************************)
95228+
(* Adapted for Javascript backend : Hongbo Zhang, *)
95229+
95230+
9516595231
let lets_helper (count_var : Ident.t -> Lam_pass_count.used_info) lam =
9516695232
let subst : Lam.t Ident_hashtbl.t = Ident_hashtbl.create 32 in
9516795233
let string_table : string Ident_hashtbl.t = Ident_hashtbl.create 32 in
@@ -95183,8 +95249,9 @@ let lets_helper (count_var : Ident.t -> Lam_pass_count.used_info) lam =
9518395249
begin
9518495250
try (** TODO: record all references variables *)
9518595251
Lam_util.refine_let
95186-
~kind:Variable v slinit (eliminate_ref v slbody)
95187-
with Real_reference ->
95252+
~kind:Variable v slinit
95253+
(Lam_pass_eliminate_ref.eliminate_ref v slbody)
95254+
with Lam_pass_eliminate_ref.Real_reference ->
9518895255
Lam_util.refine_let
9518995256
~kind v (Lam.prim ~primitive ~args:[slinit] loc)
9519095257
slbody
@@ -95344,15 +95411,19 @@ let lets_helper (count_var : Ident.t -> Lam_pass_count.used_info) lam =
9534495411
let new_l = simplif l
9534595412
and new_consts = List.map (fun (n, e) -> (n, simplif e)) sw.sw_consts
9534695413
and new_blocks = List.map (fun (n, e) -> (n, simplif e)) sw.sw_blocks
95347-
and new_fail = Misc.may_map simplif sw.sw_failaction in
95414+
and new_fail =
95415+
match sw.sw_failaction with
95416+
| None -> None
95417+
| Some x -> Some (simplif x)
95418+
in
9534895419
Lam.switch
9534995420
new_l
9535095421
{sw with sw_consts = new_consts ; sw_blocks = new_blocks;
9535195422
sw_failaction = new_fail}
9535295423
| Lstringswitch (l,sw,d) ->
9535395424
Lam.stringswitch
9535495425
(simplif l) (List.map (fun (s,l) -> s,simplif l) sw)
95355-
(Misc.may_map simplif d)
95426+
(match d with None -> None | Some d -> Some (simplif d))
9535695427
| Lstaticraise (i,ls) ->
9535795428
Lam.staticraise i (List.map simplif ls)
9535895429
| Lstaticcatch(l1, (i,args), l2) ->

jscomp/core/lam_pass_eliminate_ref.ml

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
(***********************************************************************)
2+
(* *)
3+
(* OCaml *)
4+
(* *)
5+
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
6+
(* *)
7+
(* Copyright 1996 Institut National de Recherche en Informatique et *)
8+
(* en Automatique. All rights reserved. This file is distributed *)
9+
(* under the terms of the Q Public License version 1.0. *)
10+
(* *)
11+
(***********************************************************************)
12+
(* Adapted for Javascript backend : Hongbo Zhang, *)
13+
14+
15+
16+
17+
exception Real_reference
18+
19+
let rec eliminate_ref id (lam : Lam.t) =
20+
match lam with (** we can do better escape analysis in Javascript backend *)
21+
| Lvar v ->
22+
if Ident.same v id then raise_notrace Real_reference else lam
23+
| Lprim {primitive = Pfield (0,_); args = [Lvar v]} when Ident.same v id ->
24+
Lam.var id
25+
| Lfunction{ kind; params; body} as lam ->
26+
if Ident_set.mem id (Lam.free_variables lam)
27+
then raise_notrace Real_reference
28+
else lam
29+
(* In Javascript backend, its okay, we can reify it later
30+
a failed case
31+
{[
32+
for i = ..
33+
let v = ref 0
34+
for j = ..
35+
incr v
36+
a[j] = ()=>{!v}
37+
38+
]}
39+
here v is captured by a block, and it's a loop mutable value,
40+
we have to generate
41+
{[
42+
for i = ..
43+
let v = ref 0
44+
(function (v){for j = ..
45+
a[j] = ()=>{!v}}(v)
46+
47+
]}
48+
now, v is a real reference
49+
TODO: we can refine analysis in later
50+
*)
51+
(* Lfunction(kind, params, eliminate_ref id body) *)
52+
| Lprim {primitive = Psetfield(0, _,_);
53+
args = [Lvar v; e]} when Ident.same v id ->
54+
Lam.assign id (eliminate_ref id e)
55+
| Lprim {primitive = Poffsetref delta ;
56+
args = [Lvar v]; loc } when Ident.same v id ->
57+
Lam.assign id (Lam.prim ~primitive:(Poffsetint delta) ~args:[Lam.var id] loc)
58+
| Lconst _ -> lam
59+
| Lapply{fn = e1; args = el; loc; status} ->
60+
Lam.apply
61+
(eliminate_ref id e1)
62+
(List.map (eliminate_ref id) el)
63+
loc status
64+
| Llet(str, v, e1, e2) ->
65+
Lam.let_ str v (eliminate_ref id e1) (eliminate_ref id e2)
66+
| Lletrec(idel, e2) ->
67+
Lam.letrec
68+
(List.map (fun (v, e) -> (v, eliminate_ref id e)) idel)
69+
(eliminate_ref id e2)
70+
| Lprim {primitive ; args ; loc} ->
71+
Lam.prim ~primitive ~args:(List.map (eliminate_ref id) args) loc
72+
| Lswitch(e, sw) ->
73+
Lam.switch(eliminate_ref id e)
74+
{sw_numconsts = sw.sw_numconsts;
75+
sw_consts =
76+
List.map (fun (n, e) -> (n, eliminate_ref id e)) sw.sw_consts;
77+
sw_numblocks = sw.sw_numblocks;
78+
sw_blocks =
79+
List.map (fun (n, e) -> (n, eliminate_ref id e)) sw.sw_blocks;
80+
sw_failaction =
81+
match sw.sw_failaction with
82+
| None -> None
83+
| Some x -> Some (eliminate_ref id x)
84+
}
85+
| Lstringswitch(e, sw, default) ->
86+
Lam.stringswitch
87+
(eliminate_ref id e)
88+
(List.map (fun (s, e) -> (s, eliminate_ref id e)) sw)
89+
(match default with
90+
| None -> None
91+
| Some x -> Some (eliminate_ref id x))
92+
| Lstaticraise (i,args) ->
93+
Lam.staticraise i (List.map (eliminate_ref id) args)
94+
| Lstaticcatch(e1, i, e2) ->
95+
Lam.staticcatch (eliminate_ref id e1) i (eliminate_ref id e2)
96+
| Ltrywith(e1, v, e2) ->
97+
Lam.try_ (eliminate_ref id e1) v (eliminate_ref id e2)
98+
| Lifthenelse(e1, e2, e3) ->
99+
Lam.if_ (eliminate_ref id e1) (eliminate_ref id e2) (eliminate_ref id e3)
100+
| Lsequence(e1, e2) ->
101+
Lam.seq (eliminate_ref id e1) (eliminate_ref id e2)
102+
| Lwhile(e1, e2) ->
103+
Lam.while_ (eliminate_ref id e1) (eliminate_ref id e2)
104+
| Lfor(v, e1, e2, dir, e3) ->
105+
Lam.for_ v
106+
(eliminate_ref id e1)
107+
(eliminate_ref id e2)
108+
dir
109+
(eliminate_ref id e3)
110+
| Lassign(v, e) ->
111+
Lam.assign v (eliminate_ref id e)
112+
| Lsend(k, m, o, el, loc) ->
113+
Lam.send k
114+
(eliminate_ref id m) (eliminate_ref id o)
115+
(List.map (eliminate_ref id) el) loc
116+
| Lifused(v, e) ->
117+
Lam.ifused v (eliminate_ref id e)
118+
119+

0 commit comments

Comments
 (0)