Skip to content

Commit 10fc030

Browse files
committed
clean up lam convert operations
1 parent 1b34ada commit 10fc030

File tree

4 files changed

+126
-140
lines changed

4 files changed

+126
-140
lines changed

jscomp/core/lam_convert.ml

+53-71
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ let exception_id_destructed (l : Lam.t) (fv : Ident.t): bool =
121121
let abs_int x = if x < 0 then - x else x
122122
let no_over_flow x = abs_int x < 0x1fff_ffff
123123

124-
let lam_is_var (x : Lambda.lambda) (y : Ident.t) =
124+
let lam_is_var (x : Lam.t) (y : Ident.t) =
125125
match x with
126126
| Lvar y2 -> Ident.same y2 y
127127
| _ -> false
@@ -610,36 +610,7 @@ let convert (exports : Ident_set.t) (lam : Lambda.lambda) : Lam.t * Lam_module_i
610610
#else
611611
(e,s)
612612
#end
613-
->
614-
let e = convert_aux e in
615-
begin match s with
616-
| {
617-
sw_failaction = None ;
618-
sw_blocks = [];
619-
sw_numblocks = 0;
620-
sw_consts ;
621-
sw_numconsts ;
622-
} ->
623-
begin match happens_to_be_diff sw_consts with
624-
| Some 0 -> e
625-
| Some i ->
626-
prim
627-
~primitive:Paddint
628-
~args:[e; Lam.const(Const_int i)]
629-
Location.none
630-
| None ->
631-
Lam.switch e
632-
{sw_failaction = None;
633-
sw_blocks = [];
634-
sw_numblocks = true;
635-
sw_consts =
636-
Ext_list.map_snd sw_consts convert_aux;
637-
sw_numconsts =
638-
Ext_list.length_ge sw_consts sw_numconsts
639-
}
640-
end
641-
| _ -> Lam.switch e (convert_switch s)
642-
end
613+
-> convert_switch e s
643614
| Lstringswitch (e, cases, default, _ ) ->
644615
Lam.stringswitch
645616
(convert_aux e)
@@ -697,9 +668,8 @@ let convert (exports : Ident_set.t) (lam : Lambda.lambda) : Lam.t * Lam_module_i
697668
| Levent (e, event) ->
698669
(* disabled by upstream*)
699670
assert false
700-
| Lifused (id, e) ->
671+
| Lifused (id, e) -> convert_aux e (* TODO: remove it ASAP *)
701672

702-
convert_aux e (* TODO: remove it ASAP *)
703673
and convert_let (kind : Lam_compat.let_kind) id (e : Lambda.lambda) body : Lam.t =
704674
match kind, e with
705675
| Alias , Lvar u ->
@@ -756,44 +726,56 @@ let convert (exports : Ident_set.t) (lam : Lambda.lambda) : Lam.t * Lam_module_i
756726
| _ ->
757727
Lam.let_ kind id new_e new_body
758728
and convert_pipe (f : Lambda.lambda) (x : Lambda.lambda) outer_loc =
759-
match f with
760-
(* [x|>f]
761-
TODO: [airty = 0] when arity =0, it can not be escaped user can only
762-
write [f x ] instead of [x |> f ]
763-
*)
764-
#if OCAML_VERSION =~ ">4.03.0" then
765-
| Lfunction {params = [param]; body = Lprim (external_fn, [Lvar inner_arg], inner_loc) }
766-
#else
767-
| Lfunction(_, [param],Lprim(external_fn,[Lvar inner_arg],inner_loc))
768-
#end
769-
when Ident.same param inner_arg
770-
->
771-
convert_aux (Lprim(external_fn, [x], outer_loc))
772-
#if OCAML_VERSION =~ ">4.03.0" then
773-
| Lapply {ap_func = Lfunction{ params; body = Lprim(external_fn,inner_args,inner_loc)}; ap_args = args; ap_loc = outer_loc}
774-
#else
775-
| Lapply(Lfunction(kind, params,Lprim(external_fn,inner_args,inner_loc)), args, outer_loc ) (* x |> f a *)
776-
#end
777-
778-
when Ext_list.for_all2_no_exn inner_args params lam_is_var &&
779-
Ext_list.length_larger_than_n inner_args args 1
780-
->
781-
782-
convert_aux (Lprim(external_fn, Ext_list.append args [x], outer_loc))
783-
| _ ->
784-
let x = convert_aux x in
785-
let f = convert_aux f in
786-
match f with
787-
| Lapply{fn;args} ->
788-
Lam.apply fn (args @[x]) outer_loc App_na
789-
| _ ->
790-
Lam.apply f [x] outer_loc App_na
791-
and convert_switch (s : Lambda.lambda_switch) : Lam.switch =
792-
{ sw_numconsts = Ext_list.length_ge s.sw_consts s.sw_numconsts ;
793-
sw_consts = Ext_list.map_snd s.sw_consts convert_aux;
794-
sw_numblocks = Ext_list.length_ge s.sw_blocks s.sw_numblocks;
795-
sw_blocks = Ext_list.map_snd s.sw_blocks convert_aux;
796-
sw_failaction =Ext_option.map s.sw_failaction convert_aux } in
729+
let x = convert_aux x in
730+
let f = convert_aux f in
731+
match f with
732+
| Lfunction {params = [param]; body = Lprim{primitive; args = [Lvar inner_arg]; loc }}
733+
when Ident.same param inner_arg ->
734+
Lam.prim ~primitive ~args:[x] outer_loc
735+
| Lapply {fn = Lfunction{params; body = Lprim{primitive; args = inner_args}}; args}
736+
when Ext_list.for_all2_no_exn inner_args params lam_is_var &&
737+
Ext_list.length_larger_than_n inner_args args 1
738+
->
739+
Lam.prim ~primitive ~args:(Ext_list.append_one args x) outer_loc
740+
| Lapply{fn;args} ->
741+
Lam.apply fn (Ext_list.append_one args x) outer_loc App_na
742+
| _ ->
743+
Lam.apply f [x] outer_loc App_na
744+
and convert_switch (e : Lambda.lambda) (s : Lambda.lambda_switch) =
745+
let e = convert_aux e in
746+
match s with
747+
| {
748+
sw_failaction = None ;
749+
sw_blocks = [];
750+
sw_numblocks = 0;
751+
sw_consts ;
752+
sw_numconsts ;
753+
} ->
754+
begin match happens_to_be_diff sw_consts with
755+
| Some 0 -> e
756+
| Some i ->
757+
prim
758+
~primitive:Paddint
759+
~args:[e; Lam.const(Const_int i)]
760+
Location.none
761+
| None ->
762+
Lam.switch e
763+
{sw_failaction = None;
764+
sw_blocks = [];
765+
sw_numblocks = true;
766+
sw_consts =
767+
Ext_list.map_snd sw_consts convert_aux;
768+
sw_numconsts =
769+
Ext_list.length_ge sw_consts sw_numconsts
770+
}
771+
end
772+
| _ ->
773+
Lam.switch e
774+
{ sw_numconsts = Ext_list.length_ge s.sw_consts s.sw_numconsts ;
775+
sw_consts = Ext_list.map_snd s.sw_consts convert_aux;
776+
sw_numblocks = Ext_list.length_ge s.sw_blocks s.sw_numblocks;
777+
sw_blocks = Ext_list.map_snd s.sw_blocks convert_aux;
778+
sw_failaction =Ext_option.map s.sw_failaction convert_aux } in
797779
convert_aux lam , may_depends
798780

799781

jscomp/test/array_subtle_test.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
var Mt = require("./mt.js");
44
var Block = require("../../lib/js/block.js");
55
var Caml_array = require("../../lib/js/caml_array.js");
6-
var Js_primitive = require("../../lib/js/js_primitive.js");
76

87
var suites = /* record */[/* contents : [] */0];
98

@@ -68,18 +67,30 @@ eq("File \"array_subtle_test.ml\", line 23, characters 5-12", /* tuple */[
6867
]);
6968

7069
while(v.length > 0) {
71-
Js_primitive.undefined_to_opt(v.pop());
70+
v.pop();
7271
};
7372

7473
eq("File \"array_subtle_test.ml\", line 29, characters 5-12", /* tuple */[
7574
0,
7675
v.length
7776
]);
7877

78+
function f(v) {
79+
var match = v.pop();
80+
if (match !== undefined) {
81+
console.log("hi");
82+
} else {
83+
console.log("hi2");
84+
}
85+
console.log((v.pop(), /* () */0));
86+
return /* () */0;
87+
}
88+
7989
Mt.from_pair_suites("array_subtle_test.ml", suites[0]);
8090

8191
exports.suites = suites;
8292
exports.test_id = test_id;
8393
exports.eq = eq;
8494
exports.v = v;
95+
exports.f = f;
8596
/* Not a pure module */

jscomp/test/array_subtle_test.ml

+7
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,11 @@ let () =
2727
ignore @@ Js.Array.pop v
2828
done;
2929
eq __LOC__ (0, Js.Array.length v )
30+
31+
32+
let f v =
33+
(match Js.Array.pop v with
34+
| Some x -> Js.log "hi"
35+
| None -> Js.log "hi2");
36+
Js.log (ignore @@ Js.Array.pop v)
3037
let () = Mt.from_pair_suites __FILE__ !suites

lib/whole_compiler.ml

+53-67
Original file line numberDiff line numberDiff line change
@@ -107222,7 +107222,7 @@ let exception_id_destructed (l : Lam.t) (fv : Ident.t): bool =
107222107222
let abs_int x = if x < 0 then - x else x
107223107223
let no_over_flow x = abs_int x < 0x1fff_ffff
107224107224

107225-
let lam_is_var (x : Lambda.lambda) (y : Ident.t) =
107225+
let lam_is_var (x : Lam.t) (y : Ident.t) =
107226107226
match x with
107227107227
| Lvar y2 -> Ident.same y2 y
107228107228
| _ -> false
@@ -107676,36 +107676,7 @@ let convert (exports : Ident_set.t) (lam : Lambda.lambda) : Lam.t * Lam_module_i
107676107676

107677107677
(e,s)
107678107678

107679-
->
107680-
let e = convert_aux e in
107681-
begin match s with
107682-
| {
107683-
sw_failaction = None ;
107684-
sw_blocks = [];
107685-
sw_numblocks = 0;
107686-
sw_consts ;
107687-
sw_numconsts ;
107688-
} ->
107689-
begin match happens_to_be_diff sw_consts with
107690-
| Some 0 -> e
107691-
| Some i ->
107692-
prim
107693-
~primitive:Paddint
107694-
~args:[e; Lam.const(Const_int i)]
107695-
Location.none
107696-
| None ->
107697-
Lam.switch e
107698-
{sw_failaction = None;
107699-
sw_blocks = [];
107700-
sw_numblocks = true;
107701-
sw_consts =
107702-
Ext_list.map_snd sw_consts convert_aux;
107703-
sw_numconsts =
107704-
Ext_list.length_ge sw_consts sw_numconsts
107705-
}
107706-
end
107707-
| _ -> Lam.switch e (convert_switch s)
107708-
end
107679+
-> convert_switch e s
107709107680
| Lstringswitch (e, cases, default, _ ) ->
107710107681
Lam.stringswitch
107711107682
(convert_aux e)
@@ -107763,9 +107734,8 @@ let convert (exports : Ident_set.t) (lam : Lambda.lambda) : Lam.t * Lam_module_i
107763107734
| Levent (e, event) ->
107764107735
(* disabled by upstream*)
107765107736
assert false
107766-
| Lifused (id, e) ->
107737+
| Lifused (id, e) -> convert_aux e (* TODO: remove it ASAP *)
107767107738

107768-
convert_aux e (* TODO: remove it ASAP *)
107769107739
and convert_let (kind : Lam_compat.let_kind) id (e : Lambda.lambda) body : Lam.t =
107770107740
match kind, e with
107771107741
| Alias , Lvar u ->
@@ -107822,40 +107792,56 @@ let convert (exports : Ident_set.t) (lam : Lambda.lambda) : Lam.t * Lam_module_i
107822107792
| _ ->
107823107793
Lam.let_ kind id new_e new_body
107824107794
and convert_pipe (f : Lambda.lambda) (x : Lambda.lambda) outer_loc =
107825-
match f with
107826-
(* [x|>f]
107827-
TODO: [airty = 0] when arity =0, it can not be escaped user can only
107828-
write [f x ] instead of [x |> f ]
107829-
*)
107830-
107831-
| Lfunction(_, [param],Lprim(external_fn,[Lvar inner_arg],inner_loc))
107832-
107833-
when Ident.same param inner_arg
107834-
->
107835-
convert_aux (Lprim(external_fn, [x], outer_loc))
107836-
107837-
| Lapply(Lfunction(kind, params,Lprim(external_fn,inner_args,inner_loc)), args, outer_loc ) (* x |> f a *)
107838-
107839-
107840-
when Ext_list.for_all2_no_exn inner_args params lam_is_var &&
107841-
Ext_list.length_larger_than_n inner_args args 1
107842-
->
107843-
107844-
convert_aux (Lprim(external_fn, Ext_list.append args [x], outer_loc))
107845-
| _ ->
107846-
let x = convert_aux x in
107847-
let f = convert_aux f in
107848-
match f with
107849-
| Lapply{fn;args} ->
107850-
Lam.apply fn (args @[x]) outer_loc App_na
107851-
| _ ->
107852-
Lam.apply f [x] outer_loc App_na
107853-
and convert_switch (s : Lambda.lambda_switch) : Lam.switch =
107854-
{ sw_numconsts = Ext_list.length_ge s.sw_consts s.sw_numconsts ;
107855-
sw_consts = Ext_list.map_snd s.sw_consts convert_aux;
107856-
sw_numblocks = Ext_list.length_ge s.sw_blocks s.sw_numblocks;
107857-
sw_blocks = Ext_list.map_snd s.sw_blocks convert_aux;
107858-
sw_failaction =Ext_option.map s.sw_failaction convert_aux } in
107795+
let x = convert_aux x in
107796+
let f = convert_aux f in
107797+
match f with
107798+
| Lfunction {params = [param]; body = Lprim{primitive; args = [Lvar inner_arg]; loc }}
107799+
when Ident.same param inner_arg ->
107800+
Lam.prim ~primitive ~args:[x] outer_loc
107801+
| Lapply {fn = Lfunction{params; body = Lprim{primitive; args = inner_args}}; args}
107802+
when Ext_list.for_all2_no_exn inner_args params lam_is_var &&
107803+
Ext_list.length_larger_than_n inner_args args 1
107804+
->
107805+
Lam.prim ~primitive ~args:(Ext_list.append_one args x) outer_loc
107806+
| Lapply{fn;args} ->
107807+
Lam.apply fn (Ext_list.append_one args x) outer_loc App_na
107808+
| _ ->
107809+
Lam.apply f [x] outer_loc App_na
107810+
and convert_switch (e : Lambda.lambda) (s : Lambda.lambda_switch) =
107811+
let e = convert_aux e in
107812+
match s with
107813+
| {
107814+
sw_failaction = None ;
107815+
sw_blocks = [];
107816+
sw_numblocks = 0;
107817+
sw_consts ;
107818+
sw_numconsts ;
107819+
} ->
107820+
begin match happens_to_be_diff sw_consts with
107821+
| Some 0 -> e
107822+
| Some i ->
107823+
prim
107824+
~primitive:Paddint
107825+
~args:[e; Lam.const(Const_int i)]
107826+
Location.none
107827+
| None ->
107828+
Lam.switch e
107829+
{sw_failaction = None;
107830+
sw_blocks = [];
107831+
sw_numblocks = true;
107832+
sw_consts =
107833+
Ext_list.map_snd sw_consts convert_aux;
107834+
sw_numconsts =
107835+
Ext_list.length_ge sw_consts sw_numconsts
107836+
}
107837+
end
107838+
| _ ->
107839+
Lam.switch e
107840+
{ sw_numconsts = Ext_list.length_ge s.sw_consts s.sw_numconsts ;
107841+
sw_consts = Ext_list.map_snd s.sw_consts convert_aux;
107842+
sw_numblocks = Ext_list.length_ge s.sw_blocks s.sw_numblocks;
107843+
sw_blocks = Ext_list.map_snd s.sw_blocks convert_aux;
107844+
sw_failaction =Ext_option.map s.sw_failaction convert_aux } in
107859107845
convert_aux lam , may_depends
107860107846

107861107847

0 commit comments

Comments
 (0)