Skip to content

Commit ed3dccb

Browse files
committed
clean up
1 parent a838df8 commit ed3dccb

File tree

9 files changed

+190
-172
lines changed

9 files changed

+190
-172
lines changed

jscomp/all.depend

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,8 +578,9 @@ core/lam_stats_export.cmx : ext/string_map.cmx core/lam_stats.cmx \
578578
common/ext_log.cmx ext/ext_list.cmx ext/ext_array.cmx \
579579
core/lam_stats_export.cmi
580580
core/lam_pass_alpha_conversion.cmx : core/lam_stats.cmx \
581-
core/lam_eta_conversion.cmx core/lam_arity_analysis.cmx core/lam.cmx \
582-
ext/ext_list.cmx core/lam_pass_alpha_conversion.cmi
581+
core/lam_eta_conversion.cmx core/lam_arity_analysis.cmx \
582+
core/lam_arity.cmx core/lam.cmx ext/ext_list.cmx \
583+
core/lam_pass_alpha_conversion.cmi
583584
core/lam_pass_collect.cmx : core/lam_util.cmx core/lam_stats.cmx \
584585
core/lam_arity_analysis.cmx core/lam_arity.cmx core/lam.cmx \
585586
ext/ident_set.cmx ext/ident_hashtbl.cmx core/lam_pass_collect.cmi

jscomp/core/lam_arity.ml

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,36 @@ let first_arity_na ( x : t ) =
9090
| Arity_info (_, [], _) -> true
9191
| _ -> false
9292

93-
let extract_arity ( x : t) =
93+
let get_first_arity (x : t) =
9494
match x with
95-
| Arity_na -> None
96-
| Arity_info(_,xs,_) -> Some xs
95+
| Arity_na
96+
| Arity_info (_, [], _) -> None
97+
| Arity_info (_, x::_, _) -> Some x
9798

98-
(* let update_arity (x : t) xs = *)
99+
let extract_arity ( x : t) =
100+
match x with
101+
| Arity_na -> []
102+
| Arity_info(_,xs,_) -> xs
103+
104+
(* let update_arity (x : t) xs = *)
105+
106+
let rec
107+
merge_arities_aux
108+
(acc : int list)
109+
(unused_b : bool)
110+
(xs : int list) (ys : int list) (tail : bool) (tail2 : bool) =
111+
match xs,ys with
112+
| [], [] ->
113+
info unused_b (List.rev acc) (tail && tail2)
114+
(* tail && tail2 *)
115+
| [], y::ys when tail ->
116+
merge_arities_aux (y::acc) unused_b [] ys tail tail2
117+
| x::xs, [] when tail2 ->
118+
merge_arities_aux (x::acc) unused_b [] xs tail tail2
119+
| x::xs, y::ys when x = y ->
120+
merge_arities_aux (y :: acc) unused_b xs ys tail tail2
121+
| _, _ ->
122+
info false (List.rev acc) false
123+
124+
let merge_arities b xs ys t t2 =
125+
merge_arities_aux [] b xs ys t t2

jscomp/core/lam_arity.mli

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,15 @@ val na : t
5151
val info : bool -> int list -> bool -> t
5252

5353
val first_arity_na : t -> bool
54+
val get_first_arity : t -> int option
5455

55-
val extract_arity : t -> int list option
56+
(** when [NA] return empty list*)
57+
val extract_arity : t -> int list
58+
59+
val merge_arities :
60+
bool ->
61+
int list ->
62+
int list ->
63+
bool ->
64+
bool ->
65+
t

jscomp/core/lam_arity_analysis.ml

Lines changed: 35 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -24,45 +24,42 @@
2424

2525

2626
let arity_of_var (meta : Lam_stats.t) (v : Ident.t) =
27-
(** for functional parameter, if it is a high order function,
28-
if it's not from function parameter, we should warn
29-
*)
30-
match Ident_hashtbl.find_opt meta.ident_tbl v with
31-
| Some (FunctionId {arity;_}) -> arity
32-
| Some _
33-
| None ->
34-
Lam_arity.na
27+
(** for functional parameter, if it is a high order function,
28+
if it's not from function parameter, we should warn
29+
*)
30+
match Ident_hashtbl.find_opt meta.ident_tbl v with
31+
| Some (FunctionId {arity;_}) -> arity
32+
| Some _
33+
| None ->
34+
Lam_arity.na
3535

3636

3737
(* we need record all aliases -- since not all aliases are eliminated,
3838
mostly are toplevel bindings
3939
We will keep iterating such environment
4040
If not found, we will return [NA]
4141
*)
42-
let rec get_arity
43-
(meta : Lam_stats.t)
44-
(lam : Lam.t) :
45-
Lam_arity.t =
42+
let rec get_arity (meta : Lam_stats.t) (lam : Lam.t) : Lam_arity.t =
4643
match lam with
47-
| Lconst _ -> Lam_arity.non_function_arity_info
4844
| Lvar v -> arity_of_var meta v
45+
| Lconst _ -> Lam_arity.non_function_arity_info
4946
| Llet(_,_,_, l ) -> get_arity meta l
5047
| Lprim {primitive = Pfield (n,_);
5148
args = [ Lglobal_module id ]; _} ->
5249
begin match (Lam_compile_env.cached_find_ml_id_pos id n meta.env).arity with
53-
| Single x -> x
54-
| Submodule _ -> Lam_arity.na
50+
| Single x -> x
51+
| Submodule _ -> Lam_arity.na
5552
end
5653
| Lprim {primitive = Pfield (m,_);
5754
args = [ Lprim{primitive = Pfield(n,_);
58-
args = [ Lglobal_module id]} ]
59-
; _} ->
55+
args = [ Lglobal_module id]} ]
56+
; _} ->
6057
begin match (Lam_compile_env.cached_find_ml_id_pos id n meta.env).arity with
6158
| Submodule subs -> subs.(m)
6259
| Single _ -> Lam_arity.na
63-
end
60+
end
6461
(* TODO: all information except Pccall is complete, we could
65-
get more arity information
62+
get more arity information
6663
*)
6764
| Lprim {primitive = Praise ; _} -> Lam_arity.raise_arity_info
6865
| Lglobal_module _ (* TODO: fix me never going to happen assert false *)
@@ -86,15 +83,15 @@ let rec get_arity
8683
begin match fn with
8784
| Arity_na -> Lam_arity.na
8885
| Arity_info (b, xs, tail ) ->
89-
let rec take (xs : _ list) arg_length =
90-
match xs with
86+
let rec take (arities : _ list) arg_length =
87+
match arities with
9188
| x :: yys ->
9289
if arg_length = x then Lam_arity.info b yys tail
9390
else if arg_length > x then
9491
take yys (arg_length - x)
9592
else Lam_arity.info b
96-
((x - arg_length ) :: yys)
97-
tail
93+
((x - arg_length ) :: yys)
94+
tail
9895
| [] ->
9996
if tail then Lam_arity.info b [] tail
10097
else Lam_arity.na
@@ -115,7 +112,7 @@ let rec get_arity
115112
all_lambdas meta (
116113
let rest =
117114
Ext_list.map_append snd sw_consts
118-
(Ext_list.map snd sw_blocks) in
115+
(Ext_list.map snd sw_blocks) in
119116
match sw_failaction with None -> rest | Some x -> x::rest )
120117
| Lstringswitch(l, sw, d) ->
121118
begin match d with
@@ -139,43 +136,20 @@ and all_lambdas meta (xs : Lam.t list) =
139136
match xs with
140137
| y :: ys ->
141138
let arity = get_arity meta y in
142-
List.fold_left (fun exist (v : Lam.t) ->
143-
match (exist : Lam_arity.t) with
139+
let rec aux (acc : Lam_arity.t) xs =
140+
match acc, xs with
141+
| Arity_na, _ -> acc
142+
| _, [] -> acc
143+
| Arity_info(bbb, xxxs, tail), y::ys ->
144+
match get_arity meta y with
144145
| Arity_na -> Lam_arity.na
145-
| Arity_info (bbb, xxxs, tail) ->
146-
begin
147-
match get_arity meta v with
148-
| Arity_na -> Lam_arity.na
149-
| Arity_info (u,ys,tail2) ->
150-
let rec aux (b,acc) xs ys =
151-
match xs,ys with
152-
| [], [] -> (b, List.rev acc, tail && tail2)
153-
| [], y::ys when tail ->
154-
aux (b,y::acc) [] ys
155-
| x::xs, [] when tail2 ->
156-
aux (b,x::acc) [] xs
157-
| x::xs, y::ys when x = y -> aux (b, (y :: acc)) xs ys
158-
| _, _ -> (false, List.rev acc, false) in
159-
let (b,acc, tail3) = aux ( u &&bbb, []) xxxs ys in
160-
Lam_arity.info b acc tail3
161-
end
162-
) arity ys
163-
| _ -> assert false
146+
| Arity_info (u,yyys,tail2) ->
147+
aux
148+
(Lam_arity.merge_arities ( u && bbb ) xxxs yyys tail tail2)
149+
ys
150+
in aux arity ys
151+
152+
| [] -> Lam_arity.na
153+
164154

165-
(*
166-
let dump_exports_arities (meta : Lam_stats.t ) =
167-
let fmt =
168-
if meta.filename <> "" then
169-
let cmj_file = Ext_path.chop_extension meta.filename ^ Literals.suffix_cmj in
170-
let out = open_out cmj_file in
171-
Format.formatter_of_out_channel out
172-
else
173-
Format.err_formatter in
174-
begin
175-
List.iter (fun (i : Ident.t) ->
176-
pp fmt "@[%s: %s -> %a@]@." meta.filename i.name
177-
pp_arities (get_arity meta (Lvar i))
178-
) meta.exports
179-
end
180-
*)
181155

jscomp/core/lam_compile.ml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,11 @@ and compile_external_field_apply
177177
params body args_lambda)
178178
| _ ->
179179
let rec aux (acc : J.expression)
180-
(arity : Lam_arity.t) args (len : int) : E.t =
180+
arity args (len : int) : E.t =
181181
if len = 0 then
182182
acc (** All arguments consumed so far *)
183183
else match arity with
184-
| Arity_info (aaaaa, x :: rest, bbbbb) ->
184+
| x :: rest ->
185185
let x =
186186
if x = 0
187187
then 1
@@ -191,7 +191,7 @@ and compile_external_field_apply
191191
let first_part, continue = Ext_list.split_at x args in
192192
aux
193193
(E.call ~info:{arity=Full; call_info = Call_ml} acc first_part)
194-
(Lam_arity.info aaaaa rest bbbbb)
194+
rest
195195
continue (len - x)
196196
else (* GPR #1423 *)
197197
if List.for_all Js_analyzer.is_okay_to_duplicate args then
@@ -204,19 +204,17 @@ and compile_external_field_apply
204204
(* alpha conversion now? --
205205
Since we did an alpha conversion before so it is not here
206206
*)
207-
| Arity_info (_, [], _ ) ->
207+
| [] ->
208208
(* can not happen, unless it's an exception ? *)
209209
E.call ~info:Js_call_info.dummy acc args
210-
| Arity_na ->
211-
E.call ~info:Js_call_info.dummy acc args
212210
in
213211
let fn = E.ml_var_dot id name in
214212
let initial_args_len = List.length args in
215213
let expression =
216214
match arity with
217215
| Submodule _ -> E.call ~info:Js_call_info.dummy fn args
218216
| Single x ->
219-
aux fn x args initial_args_len
217+
aux fn (Lam_arity.extract_arity x) args initial_args_len
220218
in
221219
Js_output.output_of_block_and_expression
222220
cxt.st

jscomp/core/lam_pass_alpha_conversion.ml

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,10 @@ let alpha_conversion (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
3737
| Lapply {fn = l1; args = ll; loc ; status}
3838
-> (* detect functor application *)
3939
begin
40-
match Lam_arity_analysis.get_arity meta l1 with
41-
| Arity_na ->
42-
Lam.apply (simpl l1) (Ext_list.map simpl ll) loc status
43-
| Arity_info (_, args, _) ->
44-
let len = List.length ll in
45-
let rec take args =
46-
match args with
40+
let args_arity = Lam_arity.extract_arity (Lam_arity_analysis.get_arity meta l1) in
41+
let len = List.length ll in
42+
let take args_arity =
43+
match args_arity with
4744
| x :: xs ->
4845
if x = len
4946
then
@@ -63,8 +60,8 @@ let alpha_conversion (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
6360
loc App_ml_full
6461
)
6562
(Ext_list.map simpl rest) loc status (* TODO refien *)
66-
| _ -> Lam.apply (simpl l1) (Ext_list.map simpl ll) loc status
67-
in take args
63+
| [] -> Lam.apply (simpl l1) (Ext_list.map simpl ll) loc status
64+
in take args_arity
6865
end
6966

7067
| Llet (str, v, l1, l2) ->
@@ -76,16 +73,17 @@ let alpha_conversion (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
7673
| Lprim {primitive = (Lam.Pjs_fn_make len) as primitive ; args = [arg]
7774
; loc } ->
7875

79-
begin match Lam_arity_analysis.get_arity meta arg with
80-
| Arity_info (_, x::_, _)
76+
begin match
77+
Lam_arity.get_first_arity
78+
(Lam_arity_analysis.get_arity meta arg) with
79+
| Some x
8180
->
8281
let arg = simpl arg in
8382
Lam_eta_conversion.unsafe_adjust_to_arity loc
8483
~to_:len
8584
~from:x
8685
arg
87-
| Arity_info(_,[],_)
88-
| Arity_na -> Lam.prim ~primitive ~args:[simpl arg] loc
86+
| None -> Lam.prim ~primitive ~args:[simpl arg] loc
8987
end
9088
| Lprim {primitive; args ; loc} ->
9189
Lam.prim ~primitive ~args:(Ext_list.map simpl args) loc

jscomp/test/.depend

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ arith_parser.cmj : ../stdlib/parsing.cmj ../stdlib/obj.cmj \
6161
../stdlib/lexing.cmj arith_syntax.cmj
6262
arith_syntax.cmj :
6363
arity_deopt.cmj : mt.cmj
64+
arity_infer.cmj :
6465
array_data_util.cmj : ../others/belt.cmj
6566
array_safe_get.cmj : ../stdlib/array.cmj
6667
array_subtle_test.cmj : mt.cmj ../runtime/js.cmj ../stdlib/array.cmj

jscomp/test/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ OTHERS := test_literals a test_ari test_export2 test_internalOO test_obj_simple_
251251
gpr_2614_test\
252252
gpr_2700_test\
253253
gpr_2731_test\
254+
arity_infer\
254255
# bs_uncurry_test
255256
# needs Lam to get rid of Uncurry arity first
256257
# simple_derive_test

0 commit comments

Comments
 (0)