Skip to content

Commit a4f4ac7

Browse files
committed
Simplify array creation
1 parent 45a4227 commit a4f4ac7

12 files changed

+260
-299
lines changed

jscomp/all.depend

+2-3
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ core/js_exp_make.cmi : core/lam_compat.cmi core/js_op.cmx \
391391
core/js_long.cmi : core/lam_compat.cmi core/j.cmx
392392
core/js_of_lam_exception.cmi : core/j.cmx
393393
core/js_of_lam_module.cmi : core/j.cmx
394-
core/js_of_lam_array.cmi : core/lam_compat.cmi core/j.cmx
394+
core/js_of_lam_array.cmi : core/j.cmx
395395
core/js_of_lam_block.cmi : core/lam_tag_info.cmx core/lam_compat.cmi \
396396
core/js_op.cmx core/j.cmx
397397
core/js_of_lam_string.cmi : core/j.cmx
@@ -570,8 +570,7 @@ core/js_of_lam_exception.cmx : ext/literals.cmx core/js_runtime_modules.cmx \
570570
core/js_exp_make.cmx core/j.cmx core/js_of_lam_exception.cmi
571571
core/js_of_lam_module.cmx : core/js_exp_make.cmx core/j.cmx \
572572
core/js_of_lam_module.cmi
573-
core/js_of_lam_array.cmx : core/lam_compat.cmx core/js_exp_make.cmx \
574-
core/js_of_lam_array.cmi
573+
core/js_of_lam_array.cmx : core/js_exp_make.cmx core/js_of_lam_array.cmi
575574
core/js_of_lam_block.cmx : core/lam_tag_info.cmx core/lam_compat.cmx \
576575
core/js_of_lam_array.cmx core/js_exp_make.cmx core/js_of_lam_block.cmi
577576
core/js_of_lam_string.cmx : core/js_runtime_modules.cmx core/js_exp_make.cmx \

jscomp/core/js_of_lam_array.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ module E = Js_exp_make
5555

5656

5757
(* Parrayref(u|s) *)
58-
let make_array mt (kind : Lam_compat.array_kind) args =
58+
let make_array mt args =
5959
E.array ~comment:"array" mt args
6060

6161
let set_array e e0 e1 =

jscomp/core/js_of_lam_array.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
(** Utilities for creating Array of JS IR *)
3333

34-
val make_array : J.mutable_flag -> Lam_compat.array_kind -> J.expression list -> J.expression
34+
val make_array : J.mutable_flag -> J.expression list -> J.expression
3535
(** create an array *)
3636

3737
val set_array : J.expression -> J.expression -> J.expression -> J.expression

jscomp/core/js_of_lam_block.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ module E = Js_exp_make
3636
*)
3737
let make_block mutable_flag (tag_info : Lam_tag_info.t) tag args =
3838

39-
match mutable_flag, tag_info with
40-
| _, Blk_array -> Js_of_lam_array.make_array mutable_flag Pgenarray args
41-
| _ , _ -> E.make_block tag tag_info args mutable_flag
39+
match tag_info with
40+
| Blk_array -> Js_of_lam_array.make_array mutable_flag args
41+
| _ -> E.make_block tag tag_info args mutable_flag
4242
(* | _, ( Tuple | Variant _ ) -> (\** TODO: check with inline record *\) *)
4343
(* E.arr Immutable *)
4444
(* (E.small_int ?comment:(Lam_compile_util.comment_of_tag_info tag_info) tag *)

jscomp/core/lam_analysis.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ let rec no_side_effects (lam : Lam.t) : bool =
130130
| Pbytesrefs
131131
| Pmakearray _
132132
| Parraylength
133-
| Parrayrefu _
134-
| Parrayrefs _
133+
| Parrayrefu
134+
| Parrayrefs
135135
(* Test if the argument is a block or an immediate integer *)
136136
| Pisint
137137
(* Test if the (integer) argument is outside an interval *)
@@ -182,7 +182,7 @@ let rec no_side_effects (lam : Lam.t) : bool =
182182
(* Bitvect operations *)
183183
| Pbittest
184184
(* Operations on boxed integers (Nativeint.t, Int32.t, Int64.t) *)
185-
| Parraysets _
185+
| Parraysets
186186
| Pbigarrayset _
187187
(* size of the nth dimension of a big array *)
188188
| Pbigarraydim _
@@ -204,7 +204,7 @@ let rec no_side_effects (lam : Lam.t) : bool =
204204
(* byte swap *)
205205
| Pbswap16
206206
| Pbbswap _
207-
| Parraysetu _
207+
| Parraysetu
208208
| Poffsetref _
209209
| Praise
210210
| Plazyforce

jscomp/core/lam_compile_const.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ and translate (x : Lam_constant.t ) : J.expression =
106106
we deoptimized this in js backend? so it is actually mutable
107107
*)
108108
(* TODO-- *)
109-
Js_of_lam_array.make_array Mutable Pfloatarray
109+
Js_of_lam_array.make_array Mutable
110110
(Ext_list.map ars E.float )
111111
(* E.arr Mutable ~comment:"float array" *)
112112
(* (Ext_list.map (fun x -> E.float x ) ars) *)

jscomp/core/lam_compile_primitive.ml

+99-118
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,22 @@ module E = Js_exp_make
3434
(* If it is the return value, since it is a side-effect call,
3535
we return unit, otherwise just return it
3636
*)
37-
let decorate_side_effect ({continuation = st;_} : Lam_compile_context.t) e : E.t =
37+
let ensure_value_unit (st : Lam_compile_context.continuation) e : E.t =
3838
match st with
3939
| EffectCall (ReturnTrue _ ) | NeedValue (ReturnTrue _)
4040
| Assign _ | Declare _ | NeedValue _ -> E.seq e E.unit
4141
| EffectCall ReturnFalse -> e
4242
(* NeedValue should return a meaningful expression*)
4343

4444
let translate loc
45-
({ meta = { env; _}; _} as cxt : Lam_compile_context.t)
45+
(cxt : Lam_compile_context.t)
4646
(prim : Lam_primitive.t)
4747
(args : J.expression list) : J.expression =
4848
match prim with
4949
| Pis_not_none ->
50-
begin match args with
51-
| [arg] -> Js_of_lam_option.is_not_none arg
52-
| _ -> assert false
53-
end
50+
(match args with
51+
| [arg] -> Js_of_lam_option.is_not_none arg
52+
| _ -> assert false)
5453
| Pcreate_extension s
5554
->
5655
Js_of_lam_exception.make (E.str s)
@@ -63,42 +62,37 @@ let translate loc
6362
| Praw_js_code_stmt s ->
6463
E.raw_js_code Stmt s
6564
| Pjs_runtime_apply ->
66-
begin match args with
67-
| [f ; args] ->
68-
E.flat_call f args
69-
| _ -> assert false
70-
end
65+
(match args with
66+
| [f ; args] ->
67+
E.flat_call f args
68+
| _ -> assert false)
7169
| Pjs_apply ->
72-
begin match args with
73-
| fn :: rest ->
74-
E.call ~info:{arity=Full; call_info = Call_na} fn rest
75-
| _ -> assert false
76-
end
77-
70+
(match args with
71+
| fn :: rest ->
72+
E.call ~info:{arity=Full; call_info = Call_na} fn rest
73+
| _ -> assert false)
7874
| Pnull_to_opt ->
79-
begin match args with
80-
| [e] ->
81-
begin match e.expression_desc with
82-
| Var _ | Undefined | Null ->
83-
Js_of_lam_option.null_to_opt e
84-
| _ ->
85-
E.runtime_call Js_runtime_modules.js_primitive
86-
"null_to_opt" args
87-
end
88-
| _ -> assert false
89-
end
75+
(match args with
76+
| [e] ->
77+
(match e.expression_desc with
78+
| Var _ | Undefined | Null ->
79+
Js_of_lam_option.null_to_opt e
80+
| _ ->
81+
E.runtime_call Js_runtime_modules.js_primitive
82+
"null_to_opt" args)
83+
| _ -> assert false )
84+
9085
| Pundefined_to_opt ->
91-
begin match args with
92-
| [e] ->
93-
begin match e.expression_desc with
94-
| Var _ | Undefined | Null ->
95-
Js_of_lam_option.undef_to_opt e
96-
| _ ->
97-
E.runtime_call Js_runtime_modules.js_primitive
98-
"undefined_to_opt" args
99-
end
100-
| _ -> assert false
101-
end
86+
(match args with
87+
| [e] ->
88+
(match e.expression_desc with
89+
| Var _ | Undefined | Null ->
90+
Js_of_lam_option.undef_to_opt e
91+
| _ ->
92+
E.runtime_call Js_runtime_modules.js_primitive
93+
"undefined_to_opt" args )
94+
| _ -> assert false )
95+
10296
| Pnull_undefined_to_opt ->
10397
begin match args with
10498
| [e] ->
@@ -170,43 +164,43 @@ let translate loc
170164
E.runtime_call Js_runtime_modules.module_ "update_mod" args
171165
| Psome ->
172166
begin match args with
173-
| [arg ] ->
174-
begin match arg.J.expression_desc with
175-
| Null
176-
| Object _
177-
| Number _
178-
| Caml_block _
179-
| Array _
180-
| Str _
181-
->
182-
(* This makes sense when type info
183-
is not available at the definition
184-
site, and inline recovered it
185-
*)
186-
E.optional_not_nest_block arg
187-
| _ -> E.optional_block arg
188-
end
189-
| _ -> assert false
167+
| [arg ] ->
168+
begin match arg.J.expression_desc with
169+
| Null
170+
| Object _
171+
| Number _
172+
| Caml_block _
173+
| Array _
174+
| Str _
175+
->
176+
(* This makes sense when type info
177+
is not available at the definition
178+
site, and inline recovered it
179+
*)
180+
E.optional_not_nest_block arg
181+
| _ -> E.optional_block arg
182+
end
183+
| _ -> assert false
190184
end
191185
| Psome_not_nest ->
192186
begin match args with
193-
| [arg] -> E.optional_not_nest_block arg
194-
| _ -> assert false
187+
| [arg] -> E.optional_not_nest_block arg
188+
| _ -> assert false
195189
end
196190
| Pmakeblock(tag, tag_info, mutable_flag ) -> (* RUNTIME *)
197191
Js_of_lam_block.make_block
198192
(Js_op_util.of_lam_mutable_flag mutable_flag)
199193
tag_info (E.small_int tag) args
200194
| Pval_from_option ->
201195
begin match args with
202-
| [ e ] ->
203-
Js_of_lam_option.val_from_option e
204-
| _ -> assert false
196+
| [ e ] ->
197+
Js_of_lam_option.val_from_option e
198+
| _ -> assert false
205199
end
206200
| Pval_from_option_not_nest ->
207201
begin match args with
208-
| [ e ] -> e
209-
| _ -> assert false
202+
| [ e ] -> e
203+
| _ -> assert false
210204
end
211205
| Pfield (i, fld_info) ->
212206
begin match args with
@@ -583,17 +577,15 @@ let translate loc
583577
*)
584578
| Pbytessetu
585579
| Pbytessets ->
586-
begin match args with
587-
| [e;e0;e1] -> decorate_side_effect cxt
588-
(Js_of_lam_string.set_byte e e0 e1)
589-
590-
| _ -> assert false
591-
end
580+
(match args with
581+
| [e;e0;e1] -> ensure_value_unit cxt.continuation
582+
(Js_of_lam_string.set_byte e e0 e1)
583+
| _ -> assert false)
592584
| Pbytesrefu ->
593-
begin match args with
594-
| [e;e1] -> Js_of_lam_string.ref_byte e e1
595-
| _ -> assert false
596-
end
585+
(match args with
586+
| [e;e1] -> Js_of_lam_string.ref_byte e e1
587+
| _ -> assert false)
588+
597589

598590
| Pbytesrefs ->
599591
begin match args with
@@ -633,48 +625,38 @@ let translate loc
633625
| _ -> assert false
634626
end
635627
| Psetfield (i, field_info) ->
636-
begin match args with
637-
| [e0;e1] -> (** RUNTIME *)
638-
decorate_side_effect cxt
639-
(Js_of_lam_block.set_field field_info e0 (Int32.of_int i) e1)
640-
(*TODO: get rid of [E.unit ()]*)
641-
| _ -> assert false
642-
end
628+
(match args with
629+
| [e0;e1] -> (** RUNTIME *)
630+
ensure_value_unit cxt.continuation
631+
(Js_of_lam_block.set_field field_info e0 (Int32.of_int i) e1)
632+
(*TODO: get rid of [E.unit ()]*)
633+
| _ -> assert false)
643634
| Psetfloatfield (i,field_info)
644635
-> (** RUNTIME -- RETURN VALUE SHOULD BE UNIT *)
645-
begin
646-
match args with
647-
| [e;e0] ->
648-
decorate_side_effect cxt
649-
(Js_of_lam_float_record.set_double_field field_info e (Int32.of_int i) e0 )
650-
| _ -> assert false
651-
end
652-
653-
654-
| Pfloatfield (i, field_info) -> (** RUNTIME *)
655-
begin
656-
match args with
657-
| [e] ->
658-
Js_of_lam_float_record.get_double_feild field_info e
659-
(Int32.of_int i)
660-
| _ -> assert false
661-
end
662-
| Parrayrefu _kind ->
663-
begin match args with
664-
| [e;e1] -> Js_of_lam_array.ref_array e e1 (* Todo: Constant Folding *)
665-
| _ -> assert false
666-
end
667-
| Parrayrefs _kind ->
636+
(match args with
637+
| [e;e0] ->
638+
ensure_value_unit cxt.continuation
639+
(Js_of_lam_float_record.set_double_field field_info e (Int32.of_int i) e0 )
640+
| _ -> assert false)
641+
| Pfloatfield (i, field_info) -> (** RUNTIME *)
642+
(match args with
643+
| [e] ->
644+
Js_of_lam_float_record.get_double_feild field_info e
645+
(Int32.of_int i)
646+
| _ -> assert false )
647+
| Parrayrefu ->
648+
(match args with
649+
| [e;e1] -> Js_of_lam_array.ref_array e e1 (* Todo: Constant Folding *)
650+
| _ -> assert false)
651+
| Parrayrefs ->
668652
Lam_dispatch_primitive.translate loc "caml_array_get" args
669-
| Pmakearray kind ->
670-
Js_of_lam_array.make_array Mutable kind args
671-
| Parraysetu _kind ->
672-
begin match args with (* wrong*)
673-
| [e;e0;e1] -> decorate_side_effect cxt @@ Js_of_lam_array.set_array e e0 e1
674-
| _ -> assert false
675-
end
676-
677-
| Parraysets _kind ->
653+
| Pmakearray _kind ->
654+
Js_of_lam_array.make_array Mutable args
655+
| Parraysetu ->
656+
(match args with (* wrong*)
657+
| [e;e0;e1] -> ensure_value_unit cxt.continuation (Js_of_lam_array.set_array e e0 e1)
658+
| _ -> assert false)
659+
| Parraysets ->
678660
Lam_dispatch_primitive.translate loc "caml_array_set" args
679661
| Pccall prim ->
680662
Lam_dispatch_primitive.translate loc prim.prim_name args
@@ -780,12 +762,11 @@ let translate loc
780762
| Pstring_load_64 unsafe
781763
-> Js_long.get64 args
782764

783-
| Plazyforce
784-
(* | Plazyforce -> *)
785-
(* let parm = Ident.create "prim" in *)
786-
(* Lfunction(Curried, [parm], *)
787-
(* Matching.inline_lazy_force (Lvar parm) Location.none) *)
788-
(* It is inlined, this should not appear here *)
765+
| Plazyforce
766+
(* let parm = Ident.create "prim" in
767+
Lfunction(Curried, [parm],
768+
Matching.inline_lazy_force (Lvar parm) Location.none)
769+
It is inlined, this should not appear here *)
789770
| Pbittest
790771

791772
| Pstring_set_16 _

jscomp/core/lam_convert.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,10 @@ let lam_prim ~primitive:( p : Lambda.primitive) ~args loc : t =
297297
| Pfloatcomp x -> prim ~primitive:(Pfloatcomp x) ~args loc
298298
| Pmakearray x -> prim ~primitive:(Pmakearray x) ~args loc
299299
| Parraylength _ -> prim ~primitive:Parraylength ~args loc
300-
| Parrayrefu x -> prim ~primitive:(Parrayrefu x) ~args loc
301-
| Parraysetu x -> prim ~primitive:(Parraysetu x) ~args loc
302-
| Parrayrefs x -> prim ~primitive:(Parrayrefs x) ~args loc
303-
| Parraysets x -> prim ~primitive:(Parraysets x) ~args loc
300+
| Parrayrefu _ -> prim ~primitive:(Parrayrefu ) ~args loc
301+
| Parraysetu _ -> prim ~primitive:(Parraysetu ) ~args loc
302+
| Parrayrefs _ -> prim ~primitive:(Parrayrefs ) ~args loc
303+
| Parraysets _ -> prim ~primitive:(Parraysets ) ~args loc
304304
| Pbintofint x -> prim ~primitive:(Pbintofint x) ~args loc
305305
| Pintofbint x -> prim ~primitive:(Pintofbint x) ~args loc
306306
| Pnegbint x -> prim ~primitive:(Pnegbint x) ~args loc

0 commit comments

Comments
 (0)