Skip to content

Commit ad446ae

Browse files
authored
Merge pull request #4397 from BuckleScript/internal_cleanup
internal clean up, separate between objects and array
2 parents 3d92552 + c4cd44a commit ad446ae

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1241
-693
lines changed

jscomp/core/lam.ml

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ type apply_status =
3333
module Types = struct
3434

3535
type lambda_switch =
36-
{ sw_numconsts: bool; (* TODO: refine its representation *)
36+
{ sw_consts_full : bool; (* TODO: refine its representation *)
3737
sw_consts: (int * t) list;
38-
sw_numblocks: bool;
38+
sw_blocks_full : bool;
3939
sw_blocks: (int * t) list;
4040
sw_failaction : t option;
4141
sw_names : Lambda.switch_names option }
4242
(*
4343
Invariant:
44-
length (sw_consts) <= sw_numconsts
45-
when length (sw_consts) >= sw_numconsts -> true
44+
length (sw_consts) <= sw_consts_full
45+
when length (sw_consts) >= sw_consts_full -> true
4646
Note that failaction would appear in both
4747
{[
4848
match x with
@@ -55,16 +55,16 @@ module Types = struct
5555
5656
In most cases: {[
5757
let sw =
58-
{sw_numconsts = cstr.cstr_consts; sw_consts = consts;
59-
sw_numblocks = cstr.cstr_nonconsts; sw_blocks = nonconsts;
58+
{sw_consts_full = cstr.cstr_consts; sw_consts = consts;
59+
sw_blocks_full = cstr.cstr_nonconsts; sw_blocks = nonconsts;
6060
sw_failaction = None} in
6161
]}
6262
6363
but there are some edge cases (see https://caml.inria.fr/mantis/view.php?id=6033)
6464
one predicate used is
6565
{[
66-
(sw.sw_numconsts - List.length sw.sw_consts) +
67-
(sw.sw_numblocks - List.length sw.sw_blocks) > 1
66+
(sw.sw_consts_full - List.length sw.sw_consts) +
67+
(sw.sw_blocks_full - List.length sw.sw_blocks) > 1
6868
]}
6969
if [= 1] with [some fail] -- called once
7070
if [= 0] could not have [some fail]
@@ -111,9 +111,9 @@ module X = struct
111111
type lambda_switch
112112
= Types.lambda_switch
113113
=
114-
{ sw_numconsts: bool;
114+
{ sw_consts_full: bool;
115115
sw_consts: (int * t) list;
116-
sw_numblocks: bool;
116+
sw_blocks_full: bool;
117117
sw_blocks: (int * t) list;
118118
sw_failaction: t option;
119119
sw_names: Lambda.switch_names option }
@@ -190,12 +190,12 @@ let inner_map
190190
let args = Ext_list.map args f in
191191
Lprim { args; primitive; loc}
192192

193-
| Lswitch(arg, {sw_consts; sw_numconsts; sw_blocks; sw_numblocks; sw_failaction; sw_names}) ->
193+
| Lswitch(arg, {sw_consts; sw_consts_full; sw_blocks; sw_blocks_full; sw_failaction; sw_names}) ->
194194
let arg = f arg in
195195
let sw_consts = Ext_list.map_snd sw_consts f in
196196
let sw_blocks = Ext_list.map_snd sw_blocks f in
197197
let sw_failaction = Ext_option.map sw_failaction f in
198-
Lswitch(arg, { sw_consts; sw_blocks; sw_failaction; sw_numblocks; sw_numconsts; sw_names})
198+
Lswitch(arg, { sw_consts; sw_blocks; sw_failaction; sw_blocks_full; sw_consts_full; sw_names})
199199
| Lstringswitch (arg,cases,default) ->
200200
let arg = f arg in
201201
let cases = Ext_list.map_snd cases f in
@@ -737,6 +737,10 @@ let if_ (a : t) (b : t) (c : t) : t =
737737

738738
| _ ->
739739
match b, c with
740+
| _, Lconst (Const_pointer (_, Pt_assertfalse))
741+
-> seq a b (* TODO: we could customize more cases *)
742+
| Lconst (Const_pointer (_, Pt_assertfalse)), _
743+
-> seq a c
740744
| Lconst(Const_js_true), Lconst(Const_js_false)
741745
->
742746
if has_boolean_type a != None then a
@@ -757,14 +761,14 @@ let if_ (a : t) (b : t) (c : t) : t =
757761
->
758762
begin match c with
759763
| Lswitch ( Lvar yy as switch_arg,
760-
({sw_blocks = []; sw_numblocks = true; sw_consts ;
761-
sw_numconsts = _; sw_failaction = None} as body)
764+
({sw_blocks = []; sw_blocks_full = true; sw_consts ;
765+
sw_consts_full = _; sw_failaction = None} as body)
762766
)
763767
when Ident.same xx yy
764768
&& complete_range sw_consts ~start:0 ~finish:range
765769
->
766770
Lswitch(switch_arg,
767-
{ body with sw_failaction = Some b; sw_numconsts = false; })
771+
{ body with sw_failaction = Some b; sw_consts_full = false; })
768772
| _ -> Lifthenelse(a,b,c)
769773
end
770774
| _ -> Lifthenelse (a,b,c))

jscomp/core/lam.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
type ident = Ident.t
3030

3131
type lambda_switch =
32-
{ sw_numconsts: bool;
32+
{ sw_consts_full: bool;
3333
sw_consts: (int * t) list;
34-
sw_numblocks: bool;
34+
sw_blocks_full: bool;
3535
sw_blocks: (int * t) list;
3636
sw_failaction: t option;
3737
sw_names: Lambda.switch_names option }

jscomp/core/lam_arity_analysis.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ let rec get_arity (meta : Lam_stats.t) (lam : Lam.t) : Lam_arity.t =
111111
| Lswitch(_, {sw_failaction;
112112
sw_consts;
113113
sw_blocks;
114-
sw_numblocks = _;
115-
sw_numconsts = _;
114+
sw_blocks_full = _;
115+
sw_consts_full = _;
116116
}) ->
117117
all_lambdas meta (
118118
let rest =

jscomp/core/lam_bounded_vars.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,17 @@ let rewrite (map : _ Hash_ident.t)
118118
| Lswitch(l, {sw_failaction;
119119
sw_consts;
120120
sw_blocks;
121-
sw_numblocks;
122-
sw_numconsts;
121+
sw_blocks_full;
122+
sw_consts_full;
123123
sw_names;
124124
}) ->
125125
let l = aux l in
126126
Lam.switch l
127127
{sw_consts =
128128
Ext_list.map_snd sw_consts aux;
129129
sw_blocks = Ext_list.map_snd sw_blocks aux;
130-
sw_numconsts = sw_numconsts;
131-
sw_numblocks = sw_numblocks;
130+
sw_consts_full = sw_consts_full;
131+
sw_blocks_full = sw_blocks_full;
132132
sw_failaction = option_map sw_failaction;
133133
sw_names;
134134
}

jscomp/core/lam_check.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ let check file lam =
132132
iter_list_snd sw.sw_consts;
133133
iter_list_snd sw.sw_blocks;
134134
Ext_option.iter sw.sw_failaction iter;
135-
assert (not (sw.sw_failaction <> None && sw.sw_numconsts && sw.sw_numblocks))
135+
assert (not (sw.sw_failaction <> None && sw.sw_consts_full && sw.sw_blocks_full))
136136
| Lstringswitch (arg,cases,default) ->
137137
iter arg ;
138138
iter_list_snd cases ;

jscomp/core/lam_closure.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ let free_variables
102102
({sw_consts;
103103
sw_blocks;
104104
sw_failaction;
105-
sw_numconsts;
106-
sw_numblocks
105+
sw_consts_full;
106+
sw_blocks_full
107107
})) ->
108108
iter top arg;
109109
let top = Lam_var_stats.new_position_after_lam arg top in
@@ -112,7 +112,7 @@ let free_variables
112112
(match sw_failaction with
113113
| None -> ()
114114
| Some x ->
115-
if sw_numconsts || sw_numblocks
115+
if sw_consts_full || sw_blocks_full
116116
then iter top x
117117
else iter sink_pos x)
118118
| Lstringswitch (arg,cases,default) ->

jscomp/core/lam_compile.ml

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,23 @@ type default_case =
147147
| Complete
148148
| NonComplete
149149

150+
let default_action ~saturated failaction =
151+
match failaction with
152+
| None -> Complete
153+
| Some x ->
154+
if saturated then Complete
155+
else Default x
156+
157+
let get_const_name i (sw_names : Lambda.switch_names option) =
158+
match sw_names with
159+
| None -> None
160+
| Some {consts} -> Some consts.(i)
161+
162+
let get_block_name i (sw_names : Lambda.switch_names option) =
163+
match sw_names with
164+
| None -> None
165+
| Some {blocks} -> Some blocks.(i)
166+
150167
let no_effects_const = lazy true
151168
(* let has_effects_const = lazy false *)
152169

@@ -558,7 +575,9 @@ and compile_general_cases
558575
[switch ?default ?declaration switch_exp body]
559576
)
560577

561-
and compile_cases cxt switch_exp table default get_name =
578+
and compile_cases cxt
579+
(switch_exp : E.t)
580+
table default get_name =
562581
compile_general_cases
563582
get_name
564583
(fun i -> {(E.small_int i) with comment = get_name i})
@@ -569,39 +588,29 @@ and compile_cases cxt switch_exp table default get_name =
569588
switch_exp
570589
table
571590
default
572-
and compile_switch switch_arg sw (lambda_cxt : Lam_compile_context.t) =
591+
and compile_switch
592+
(switch_arg : Lam.t)
593+
(sw : Lam.lambda_switch)
594+
(lambda_cxt : Lam_compile_context.t) =
573595
(* TODO: if default is None, we can do some optimizations
574596
Use switch vs if/then/else
575597
576598
TODO: switch based optimiztion - hash, group, or using array,
577599
also if last statement is throw -- should we drop remaining
578600
statement?
579601
*)
580-
let ({sw_numconsts;
602+
let ({sw_consts_full;
581603
sw_consts;
582-
sw_numblocks;
604+
sw_blocks_full;
583605
sw_blocks;
584606
sw_failaction;
585607
sw_names } : Lam.lambda_switch) = sw in
586-
let sw_num_default =
587-
match sw_failaction with
588-
| None -> Complete
589-
| Some x ->
590-
if sw_numconsts
591-
then Complete
592-
else Default x in
593-
let sw_blocks_default =
594-
match sw_failaction with
595-
| None -> Complete
596-
| Some x ->
597-
if sw_numblocks
598-
then Complete
599-
else Default x in
600-
let get_name is_const i =
601-
match sw_names with
602-
| None -> None
603-
| Some {blocks; consts} ->
604-
Some (if is_const then consts.(i) else blocks.(i)) in
608+
let sw_num_default =
609+
default_action ~saturated:sw_consts_full sw_failaction in
610+
let sw_blocks_default =
611+
default_action ~saturated:sw_blocks_full sw_failaction in
612+
let get_const_name i = get_const_name i sw_names in
613+
let get_block_name i = get_block_name i sw_names in
605614
let compile_whole (cxt : Lam_compile_context.t ) =
606615
match compile_lambda
607616
{cxt with continuation = NeedValue Not_tail}
@@ -610,21 +619,21 @@ and compile_switch switch_arg sw (lambda_cxt : Lam_compile_context.t) =
610619
| {value = None; _} -> assert false
611620
| { block; value = Some e } ->
612621
block @
613-
(if sw_numconsts && sw_consts = [] then
614-
compile_cases cxt (E.tag e) sw_blocks sw_blocks_default (get_name false)
615-
else if sw_numblocks && sw_blocks = [] then
616-
compile_cases cxt e sw_consts sw_num_default (get_name true)
622+
(if sw_consts_full && sw_consts = [] then
623+
compile_cases cxt (E.tag e) sw_blocks sw_blocks_default get_block_name
624+
else if sw_blocks_full && sw_blocks = [] then
625+
compile_cases cxt e sw_consts sw_num_default get_const_name
617626
else
618627
(* [e] will be used twice *)
619628
let dispatch e =
620629
S.if_
621630
(E.is_type_number e )
622-
(compile_cases cxt e sw_consts sw_num_default (get_name true)
631+
(compile_cases cxt e sw_consts sw_num_default get_const_name
623632
)
624633
(* default still needed, could simplified*)
625634
~else_:
626635
(compile_cases cxt (E.tag e ) sw_blocks
627-
sw_blocks_default (get_name false)) in
636+
sw_blocks_default get_block_name) in
628637
match e.expression_desc with
629638
| J.Var _ -> [ dispatch e]
630639
| _ ->

jscomp/core/lam_compile_util.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,8 @@ let comment_of_pointer_info (x : Lam_pointer_info.t)=
6767
match x with
6868
| Pt_constructor {name;_}
6969
| Pt_variant {name} -> Some name
70-
(* | Pt_module_alias -> module_alias *)
70+
| Pt_assertfalse -> Some "assert_false"
7171
| Pt_na -> None
72+
| Pt_module_alias
73+
| Pt_builtin_boolean
74+
| Pt_shape_none -> assert false

jscomp/core/lam_constant_convert.ml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,18 @@ let rec convert_constant ( const : Lambda.structured_constant) : Lam_constant.t
3838
| Const_base (Const_int32 i) -> (Const_int32 i)
3939
| Const_base (Const_int64 i) -> (Const_int64 i)
4040
| Const_base (Const_nativeint i) -> (Const_nativeint i)
41-
| Const_pointer(0, Pt_constructor{name = "()"; cstrs = 1,0})
41+
| Const_pointer(0, Pt_constructor{name = "()"; const = 1; non_const = 0})
4242
-> Const_js_undefined
4343
| Const_pointer(i,p) ->
4444
begin match p with
45-
| Pt_constructor {name;cstrs} -> Const_pointer(i, Pt_constructor {name; cstrs})
46-
| Pt_variant {name} -> Const_pointer(i,Pt_variant {name})
47-
| Pt_module_alias -> Const_module_alias
48-
| Pt_builtin_boolean -> if i = 0 then Const_js_false else Const_js_true
49-
| Pt_shape_none ->
50-
Lam_constant.lam_none
51-
| Pt_na -> Const_pointer(i, Pt_na)
45+
| Pt_module_alias -> Const_module_alias
46+
| Pt_builtin_boolean -> if i = 0 then Const_js_false else Const_js_true
47+
| Pt_shape_none ->
48+
Lam_constant.lam_none
49+
| Pt_assertfalse
50+
| Pt_constructor _
51+
| Pt_variant _
52+
| Pt_na -> Const_pointer(i, p)
5253
end
5354
| Const_float_array (s) -> Const_float_array(s)
5455
| Const_immstring s -> Const_immstring s

jscomp/core/lam_convert.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -755,9 +755,9 @@ let convert (exports : Set_ident.t) (lam : Lambda.lambda) : Lam.t * Lam_module_i
755755
| Alias, Lprim {primitive = Poffsetint offset; args = [Lvar _ as matcher ]},
756756
Lswitch (Lvar switcher3 ,
757757
({
758-
sw_numconsts = false ;
758+
sw_consts_full = false ;
759759
sw_consts ;
760-
sw_blocks = []; sw_numblocks = true;
760+
sw_blocks = []; sw_blocks_full = true;
761761
sw_failaction = Some ifso
762762
} as px)
763763
)
@@ -810,19 +810,19 @@ let convert (exports : Set_ident.t) (lam : Lambda.lambda) : Lam.t * Lam_module_i
810810
Lam.switch e
811811
{sw_failaction = None;
812812
sw_blocks = [];
813-
sw_numblocks = true;
813+
sw_blocks_full = true;
814814
sw_consts =
815815
Ext_list.map_snd sw_consts convert_aux;
816-
sw_numconsts =
816+
sw_consts_full =
817817
Ext_list.length_ge sw_consts sw_numconsts;
818818
sw_names = s.sw_names;
819819
}
820820
end
821821
| _ ->
822822
Lam.switch e
823-
{ sw_numconsts = Ext_list.length_ge s.sw_consts s.sw_numconsts ;
823+
{ sw_consts_full = Ext_list.length_ge s.sw_consts s.sw_numconsts ;
824824
sw_consts = Ext_list.map_snd s.sw_consts convert_aux;
825-
sw_numblocks = Ext_list.length_ge s.sw_blocks s.sw_numblocks;
825+
sw_blocks_full = Ext_list.length_ge s.sw_blocks s.sw_numblocks;
826826
sw_blocks = Ext_list.map_snd s.sw_blocks convert_aux;
827827
sw_failaction =Ext_option.map s.sw_failaction convert_aux;
828828
sw_names = s.sw_names } in

0 commit comments

Comments
 (0)