Skip to content

Commit b87df8c

Browse files
committed
Clean up lambda.
1 parent 35f60ef commit b87df8c

27 files changed

+318
-317
lines changed

jscomp/core/lam.ml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ type apply_status =
3333
module Types = struct
3434
type switch_names = {consts: string array; blocks: string array}
3535

36-
type switch =
36+
type lambda_switch =
3737
{ sw_numconsts: bool; (* TODO: refine its representation *)
3838
sw_consts: (int * t) list;
3939
sw_numblocks: bool;
4040
sw_blocks: (int * t) list;
41-
sw_failaction : t option}
41+
sw_failaction : t option;
42+
sw_names : switch_names option }
4243
(*
4344
Invariant:
4445
length (sw_consts) <= sw_numconsts
@@ -94,7 +95,7 @@ module Types = struct
9495
| Llet of Lam_compat.let_kind * ident * t * t
9596
| Lletrec of (ident * t) list * t
9697
| Lprim of prim_info
97-
| Lswitch of t * switch * switch_names option
98+
| Lswitch of t * lambda_switch
9899
| Lstringswitch of t * (string * t) list * t option
99100
| Lstaticraise of int * t list
100101
| Lstaticcatch of t * (int * ident list) * t
@@ -110,14 +111,15 @@ end
110111
module X = struct
111112
type switch_names = Types.switch_names = {consts: string array; blocks: string array}
112113

113-
type switch
114-
= Types.switch
114+
type lambda_switch
115+
= Types.lambda_switch
115116
=
116117
{ sw_numconsts: bool;
117118
sw_consts: (int * t) list;
118119
sw_numblocks: bool;
119120
sw_blocks: (int * t) list;
120-
sw_failaction : t option}
121+
sw_failaction: t option;
122+
sw_names: switch_names option }
121123
and prim_info
122124
= Types.prim_info
123125
=
@@ -152,7 +154,7 @@ module X = struct
152154
| Llet of Lam_compat.let_kind * ident * t * t
153155
| Lletrec of (ident * t) list * t
154156
| Lprim of prim_info
155-
| Lswitch of t * switch * switch_names option
157+
| Lswitch of t * lambda_switch
156158
| Lstringswitch of t * (string * t) list * t option
157159
| Lstaticraise of int * t list
158160
| Lstaticcatch of t * (int * ident list) * t
@@ -196,12 +198,12 @@ let inner_map
196198
let args = Ext_list.map args f in
197199
Lprim { args; primitive; loc}
198200

199-
| Lswitch(arg, {sw_consts; sw_numconsts; sw_blocks; sw_numblocks; sw_failaction}, names) ->
201+
| Lswitch(arg, {sw_consts; sw_numconsts; sw_blocks; sw_numblocks; sw_failaction; sw_names}) ->
200202
let arg = f arg in
201203
let sw_consts = Ext_list.map_snd sw_consts f in
202204
let sw_blocks = Ext_list.map_snd sw_blocks f in
203205
let sw_failaction = Ext_option.map sw_failaction f in
204-
Lswitch(arg, { sw_consts; sw_blocks; sw_failaction; sw_numblocks; sw_numconsts}, names)
206+
Lswitch(arg, { sw_consts; sw_blocks; sw_failaction; sw_numblocks; sw_numconsts; sw_names})
205207
| Lstringswitch (arg,cases,default) ->
206208
let arg = f arg in
207209
let cases = Ext_list.map_snd cases f in
@@ -386,15 +388,15 @@ and eq_approx_list ls ls1 = Ext_list.for_all2_no_exn ls ls1 eq_approx
386388

387389

388390

389-
let switch lam (lam_switch : switch) names : t =
391+
let switch lam (lam_switch : lambda_switch) : t =
390392
match lam with
391393
| Lconst ((Const_pointer (i,_) | (Const_int i)))
392394
->
393395
Ext_list.assoc_by_int lam_switch.sw_consts i lam_switch.sw_failaction
394396
| Lconst (Const_block (i,_,_)) ->
395397
Ext_list.assoc_by_int lam_switch.sw_blocks i lam_switch.sw_failaction
396398
| _ ->
397-
Lswitch(lam,lam_switch,names)
399+
Lswitch(lam,lam_switch)
398400

399401
let stringswitch (lam : t) cases default : t =
400402
match lam with
@@ -731,15 +733,13 @@ let if_ (a : t) (b : t) (c : t) : t =
731733
begin match c with
732734
| Lswitch ( Lvar yy as switch_arg,
733735
({sw_blocks = []; sw_numblocks = true; sw_consts ;
734-
sw_numconsts; sw_failaction = None} as body),
735-
names
736+
sw_numconsts; sw_failaction = None; sw_names; } as body)
736737
)
737738
when Ident.same xx yy
738739
&& complete_range sw_consts ~start:0 ~finish:range
739740
->
740741
Lswitch(switch_arg,
741-
{ body with sw_failaction = Some b; sw_numconsts = false },
742-
names)
742+
{ body with sw_failaction = Some b; sw_numconsts = false; sw_names; })
743743
| _ -> Lifthenelse(a,b,c)
744744
end
745745
| _ -> Lifthenelse (a,b,c))

jscomp/core/lam.mli

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ type ident = Ident.t
3030

3131
type switch_names = {consts: string array; blocks: string array}
3232

33-
type switch =
33+
type lambda_switch =
3434
{ sw_numconsts: bool;
3535
sw_consts: (int * t) list;
3636
sw_numblocks: bool;
3737
sw_blocks: (int * t) list;
38-
sw_failaction : t option}
38+
sw_failaction: t option;
39+
sw_names: switch_names option }
3940
and apply_status =
4041
| App_na
4142
| App_ml_full
@@ -66,7 +67,7 @@ and t = private
6667
| Llet of Lam_compat.let_kind * ident * t * t
6768
| Lletrec of (ident * t) list * t
6869
| Lprim of prim_info
69-
| Lswitch of t * switch * switch_names option
70+
| Lswitch of t * lambda_switch
7071
| Lstringswitch of t * (string * t) list * t option
7172
| Lstaticraise of int * t list
7273
| Lstaticcatch of t * (int * ident list) * t
@@ -116,7 +117,7 @@ val letrec : (ident * t) list -> t -> t
116117
val if_ : t -> t -> t -> t
117118

118119
(** constant folding*)
119-
val switch : t -> switch -> switch_names option -> t
120+
val switch : t -> lambda_switch -> t
120121
(** constant folding*)
121122
val stringswitch : t -> (string * t) list -> t option -> t
122123

jscomp/core/lam_analysis.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,11 @@ let destruct_pattern (body : Lam.t) params args =
361361
| x::xs, [] -> assert false
362362
in
363363
match body with
364-
| Lswitch (Lvar v , switch, names)
364+
| Lswitch (Lvar v , switch)
365365
->
366366
begin match aux v params args with
367367
| Some (Lam.Lconst _ as lam) ->
368-
size (Lam.switch lam switch names) < small_inline_size
368+
size (Lam.switch lam switch) < small_inline_size
369369
| Some _ | None -> false
370370
end
371371
| Lifthenelse(Lvar v, then_, else_)

jscomp/core/lam_arity_analysis.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ let rec get_arity (meta : Lam_stats.t) (lam : Lam.t) : Lam_arity.t =
110110
sw_blocks;
111111
sw_numblocks = _;
112112
sw_numconsts = _;
113-
}, _names) ->
113+
}) ->
114114
all_lambdas meta (
115115
let rest =
116116
Ext_list.map_append sw_consts

jscomp/core/lam_bounded_vars.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,18 @@ let rewrite (map : _ Ident_hashtbl.t)
120120
sw_blocks;
121121
sw_numblocks;
122122
sw_numconsts;
123-
}, names) ->
123+
sw_names;
124+
}) ->
124125
let l = aux l in
125126
Lam.switch l
126127
{sw_consts =
127128
Ext_list.map_snd sw_consts aux;
128129
sw_blocks = Ext_list.map_snd sw_blocks aux;
129130
sw_numconsts = sw_numconsts;
130131
sw_numblocks = sw_numblocks;
131-
sw_failaction = option_map sw_failaction
132+
sw_failaction = option_map sw_failaction;
133+
sw_names;
132134
}
133-
names
134135
| Lstringswitch(l, sw, d) ->
135136
let l = aux l in
136137
Lam.stringswitch l

jscomp/core/lam_check.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ let check file lam =
7474
| Lletrec(decl, body) ->
7575
check_list_snd decl cxt;
7676
check_staticfails body cxt
77-
| Lswitch(arg, sw, _names) ->
77+
| Lswitch(arg, sw) ->
7878
check_staticfails arg cxt ;
7979
check_list_snd sw.sw_consts cxt;
8080
check_list_snd sw.sw_blocks cxt;
@@ -127,7 +127,7 @@ let check file lam =
127127
iter_list_snd decl;
128128
iter body
129129

130-
| Lswitch(arg, sw, _names) ->
130+
| Lswitch(arg, sw) ->
131131
iter arg;
132132
iter_list_snd sw.sw_consts;
133133
iter_list_snd sw.sw_blocks;

jscomp/core/lam_closure.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ let free_variables
104104
sw_failaction;
105105
sw_numconsts;
106106
sw_numblocks
107-
}), _names) ->
107+
})) ->
108108
iter top arg;
109109
let top = Lam_var_stats.new_position_after_lam arg top in
110110
List.iter (fun (_, case) -> iter top case) sw_consts;

jscomp/core/lam_compile.ml

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -515,16 +515,16 @@ and compile_general_cases
515515
[switch ?default ?declaration switch_exp body]
516516
)
517517

518-
and compile_cases ~is_tag cxt switch_exp table default names =
518+
and compile_cases ~is_tag cxt switch_exp table default sw_names =
519519
compile_general_cases
520520
(fun i ->
521-
match names with
521+
match sw_names with
522522
| None -> None
523523
| Some {Lam.blocks; consts} ->
524524
Some (if is_tag then blocks.(i) else consts.(i)))
525525
(fun i ->
526526
let comment =
527-
match names with
527+
match sw_names with
528528
| None -> None
529529
| Some {Lam.blocks; consts} ->
530530
Some (if is_tag then blocks.(i) else consts.(i)) in
@@ -536,7 +536,7 @@ and compile_cases ~is_tag cxt switch_exp table default names =
536536
switch_exp
537537
table
538538
default
539-
and compile_switch switch_arg sw (lambda_cxt : Lam_compile_context.t) (names : Lam.switch_names option) =
539+
and compile_switch switch_arg sw (lambda_cxt : Lam_compile_context.t) =
540540
(* TODO: if default is None, we can do some optimizations
541541
Use switch vs if/then/else
542542
@@ -548,7 +548,8 @@ and compile_switch switch_arg sw (lambda_cxt : Lam_compile_context.t) (names : L
548548
sw_consts;
549549
sw_numblocks;
550550
sw_blocks;
551-
sw_failaction } : Lam.switch) = sw in
551+
sw_failaction;
552+
sw_names } : Lam.lambda_switch) = sw in
552553
let sw_num_default =
553554
match sw_failaction with
554555
| None -> Complete
@@ -572,20 +573,20 @@ and compile_switch switch_arg sw (lambda_cxt : Lam_compile_context.t) (names : L
572573
| { block; value = Some e } ->
573574
block @
574575
(if sw_numconsts && sw_consts = [] then
575-
compile_cases ~is_tag:true cxt (E.tag e) sw_blocks sw_blocks_default names
576+
compile_cases ~is_tag:true cxt (E.tag e) sw_blocks sw_blocks_default sw_names
576577
else if sw_numblocks && sw_blocks = [] then
577-
compile_cases ~is_tag:false cxt e sw_consts sw_num_default names
578+
compile_cases ~is_tag:false cxt e sw_consts sw_num_default sw_names
578579
else
579580
(* [e] will be used twice *)
580581
let dispatch e =
581582
S.if_
582583
(E.is_type_number e )
583-
(compile_cases ~is_tag:false cxt e sw_consts sw_num_default names
584+
(compile_cases ~is_tag:false cxt e sw_consts sw_num_default sw_names
584585
)
585586
(* default still needed, could simplified*)
586587
~else_:
587588
(compile_cases ~is_tag:true cxt (E.tag e ) sw_blocks
588-
sw_blocks_default names) in
589+
sw_blocks_default sw_names) in
589590
match e.expression_desc with
590591
| J.Var _ -> [ dispatch e]
591592
| _ ->
@@ -1602,11 +1603,8 @@ and compile_lambda
16021603
compile_ifthenelse predicate t_branch f_branch lambda_cxt
16031604
| Lstringswitch(l, cases, default) ->
16041605
compile_stringswitch l cases default lambda_cxt
1605-
| Lswitch(switch_arg, sw, names) ->
1606-
(* Format.eprintf "XXX Lswitch consts:%d blocks:%d@."
1607-
(Array.length names.consts)
1608-
(Array.length names.blocks); *)
1609-
compile_switch switch_arg sw lambda_cxt names
1606+
| Lswitch(switch_arg, sw) ->
1607+
compile_switch switch_arg sw lambda_cxt
16101608
| Lstaticraise(i, largs) ->
16111609
compile_staticraise i largs lambda_cxt
16121610
| Lstaticcatch _ ->

jscomp/core/lam_convert.ml

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ let exception_id_destructed (l : Lam.t) (fv : Ident.t): bool =
9696
hit fn || hit_list args
9797
| Lglobal_module _ (* global persistent module, play safe *)
9898
-> false
99-
| Lswitch(arg, sw, _names) ->
99+
| Lswitch(arg, sw) ->
100100
hit arg ||
101101
hit_list_snd sw.sw_consts ||
102102
hit_list_snd sw.sw_blocks ||
@@ -623,10 +623,10 @@ let convert (exports : Ident_set.t) (lam : Lambda.lambda) : Lam.t * Lam_module_i
623623
#if OCAML_VERSION =~ ">4.03.0" then
624624
(e,s, _loc)
625625
#else
626-
(e,s, names)
626+
(e,s)
627627
#end
628628
->
629-
convert_switch e s names
629+
convert_switch e s
630630
| Lstringswitch (e, cases, default, _ ) ->
631631
Lam.stringswitch
632632
(convert_aux e)
@@ -727,8 +727,7 @@ let convert (exports : Ident_set.t) (lam : Lambda.lambda) : Lam.t * Lam_module_i
727727
sw_consts ;
728728
sw_blocks = []; sw_numblocks = true;
729729
sw_failaction = Some ifso
730-
} as px),
731-
names
730+
} as px)
732731
)
733732
when Ident.same switcher3 id &&
734733
not (Lam_hit.hit_variable id ifso ) &&
@@ -740,7 +739,6 @@ let convert (exports : Ident_set.t) (lam : Lambda.lambda) : Lam.t * Lam_module_i
740739
Ext_list.map sw_consts
741740
(fun (i,act) -> i - offset, act)
742741
}
743-
names
744742
| _ ->
745743
Lam.let_ kind id new_e new_body
746744
and convert_pipe (f : Lambda.lambda) (x : Lambda.lambda) outer_loc =
@@ -759,9 +757,9 @@ let convert (exports : Ident_set.t) (lam : Lambda.lambda) : Lam.t * Lam_module_i
759757
Lam.apply fn (Ext_list.append_one args x) outer_loc App_na
760758
| _ ->
761759
Lam.apply f [x] outer_loc App_na
762-
and convert_switch (e : Lambda.lambda) (s : Lambda.lambda_switch) (names : Lambda.switch_names option) =
760+
and convert_switch (e : Lambda.lambda) (s : Lambda.lambda_switch) =
763761
let e = convert_aux e in
764-
let names = match names with
762+
let sw_names = match s.sw_names with
765763
| None -> None
766764
| Some {consts; blocks} ->
767765
Some {Lam.consts; blocks} in
@@ -788,18 +786,18 @@ let convert (exports : Ident_set.t) (lam : Lambda.lambda) : Lam.t * Lam_module_i
788786
sw_consts =
789787
Ext_list.map_snd sw_consts convert_aux;
790788
sw_numconsts =
791-
Ext_list.length_ge sw_consts sw_numconsts
789+
Ext_list.length_ge sw_consts sw_numconsts;
790+
sw_names;
792791
}
793-
names
794792
end
795793
| _ ->
796794
Lam.switch e
797795
{ sw_numconsts = Ext_list.length_ge s.sw_consts s.sw_numconsts ;
798796
sw_consts = Ext_list.map_snd s.sw_consts convert_aux;
799797
sw_numblocks = Ext_list.length_ge s.sw_blocks s.sw_numblocks;
800798
sw_blocks = Ext_list.map_snd s.sw_blocks convert_aux;
801-
sw_failaction =Ext_option.map s.sw_failaction convert_aux }
802-
names in
799+
sw_failaction =Ext_option.map s.sw_failaction convert_aux;
800+
sw_names; } in
803801
convert_aux lam , may_depends
804802

805803

jscomp/core/lam_exit_count.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ let count_helper (lam : Lam.t) : collection =
7575
Ext_list.iter_snd bindings count;
7676
count body
7777
| Lprim {args; _} -> List.iter count args
78-
| Lswitch(l, sw, _names) ->
78+
| Lswitch(l, sw) ->
7979
count_default sw ;
8080
count l;
8181
Ext_list.iter_snd sw.sw_consts count;

0 commit comments

Comments
 (0)