Skip to content

Commit 8bcf707

Browse files
committed
clean up, and coments
1 parent ddd04ff commit 8bcf707

8 files changed

+204
-61
lines changed

jscomp/bin/whole_compiler.ml

+60-35
Original file line numberDiff line numberDiff line change
@@ -74140,8 +74140,13 @@ let pp_ident_tbl fmt (ident_tbl : ident_tbl) =
7414074140
ident_tbl
7414174141

7414274142
let print fmt (v : t) =
74143-
pp fmt "@[Alias table:@ %a@]" pp_alias_tbl v.alias_tbl ;
74144-
pp fmt "@[Ident table:@ %a@]" pp_ident_tbl v.ident_tbl
74143+
pp fmt "@[Alias table:@ @[%a@]@]" pp_alias_tbl v.alias_tbl ;
74144+
pp fmt "@[Ident table:@ @[%a@]@]" pp_ident_tbl v.ident_tbl ;
74145+
pp fmt "@[exports:@ @[%a@]@]"
74146+
(Format.pp_print_list
74147+
~pp_sep:(fun fmt () -> pp fmt "@ ;")
74148+
Ident.print
74149+
) v.exports
7414574150
end
7414674151
module Lam_util : sig
7414774152
#1 "lam_util.mli"
@@ -89944,12 +89949,17 @@ end = struct
8994489949
(*
8994589950
Invariant: The last one is always [exports]
8994689951
Compile definitions
89947-
Compile exports
89948-
Assume Pmakeblock(_,_),
89949-
lambda_exports are pure
89950-
compile each binding with a return value
89951-
This might be wrong in toplevel
89952-
TODO: add this check as early as possible in the beginning
89952+
Compile exports
89953+
Assume Pmakeblock(_,_),
89954+
lambda_exports are pure
89955+
compile each binding with a return value
89956+
89957+
Such invariant might be wrong in toplevel (since it is all bindings)
89958+
89959+
We should add this check as early as possible
89960+
*)
89961+
89962+
(*
8995389963
- {[ Ident.same id eid]} is more correct,
8995489964
however, it will introduce a coercion, which is not necessary,
8995589965
as long as its name is the same, we want to avoid
@@ -89986,16 +89996,13 @@ end = struct
8998689996
type t = {
8998789997
export_list : Ident.t list ;
8998889998
export_set : Ident_set.t;
89989-
export_map : Lam.t Ident_map.t ;
89990-
groups : Lam_group.t list ;
89999+
export_map : Lam.t Ident_map.t ;
90000+
(** not used in code generation, mostly used for store some information in cmj files
90001+
*)
90002+
groups : Lam_group.t list ; (* all code to be compiled later = original code + rebound coercions *)
8999190003
}
8999290004

89993-
let init export_set =
89994-
{ export_list = [];
89995-
export_set ;
89996-
export_map = Ident_map.empty ;
89997-
groups = []
89998-
}
90005+
8999990006
let handle_exports
9000090007
(original_exports : Ident.t list)
9000190008
(original_export_set : Ident_set.t)
@@ -90004,28 +90011,47 @@ let handle_exports
9000490011
let tbl = String_hash_set.create len in
9000590012
let ({export_list ; export_set ; groups = coercion_groups } as result) =
9000690013
List.fold_right2
90007-
(fun (original_export_id : Ident.t) lam (acc : t) ->
90008-
let original_name = original_export_id.name in
90014+
(fun (original_export_id : Ident.t) (lam : Lam.t) (acc : t) ->
90015+
let original_name = original_export_id.name in
9000990016
if not @@ String_hash_set.check_add tbl original_name then
9001090017
Bs_exception.error (Bs_duplicate_exports original_name);
90011-
(match (lam : Lam.t) with
90018+
(match lam with
9001290019
| Lvar id
9001390020
when Ident.name id = original_name ->
9001490021
{ acc with
9001590022
export_list = id :: acc.export_list ;
9001690023
export_set =
90017-
(Ident_set.add id (Ident_set.remove original_export_id acc.export_set))
90018-
}
90024+
if id.stamp = original_export_id.stamp then acc.export_set
90025+
else (Ident_set.add id (Ident_set.remove original_export_id acc.export_set))
90026+
}
9001990027
| _ ->
90020-
(* Invariant: [eid] can not be bound before *)
90028+
(*
90029+
Example:
90030+
{[
90031+
let N = [a0,a1,a2,a3]
90032+
in [[ N[0], N[2]]]
90033+
90034+
]}
90035+
After optimization
90036+
{[
90037+
[ [ a0, a2] ]
90038+
]}
90039+
Here [N] is elminated while N is still exported identifier
90040+
Invariant: [eid] can not be bound before
90041+
FIX: this invariant is not guaranteed.
90042+
Bug manifested: when querying arity info about N, it returns an array
90043+
of size 4 instead of 2
90044+
*)
9002190045
{ acc with
9002290046
export_list = original_export_id :: acc.export_list;
9002390047
export_map = Ident_map.add original_export_id lam acc.export_map;
9002490048
groups = Single(Strict, original_export_id, lam) :: acc.groups
9002590049
});
9002690050
)
90027-
original_exports lambda_exports
90028-
(init original_export_set)
90051+
original_exports
90052+
lambda_exports
90053+
{export_list = []; export_set = original_export_set; export_map = Ident_map.empty; groups = []}
90054+
(* (init original_export_set) *)
9002990055
in
9003090056

9003190057
let (export_map, coerced_input) =
@@ -90034,6 +90060,9 @@ let handle_exports
9003490060
(match (x : Lam_group.t) with
9003590061
| Single (_,id,lam) when Ident_set.mem id export_set
9003690062
-> Ident_map.add id lam export_map
90063+
(** relies on the Invariant that [eoid] can not be bound before
90064+
FIX: such invariant may not hold
90065+
*)
9003790066
| _ -> export_map), x :: acc ) (result.export_map, result.groups) reverse_input in
9003890067
{ result with export_map ; groups = Lam_dce.remove export_list coerced_input }
9003990068

@@ -90061,12 +90090,17 @@ let rec flatten
9006190090
| x ->
9006290091
x, acc
9006390092

90093+
(** Invarinat to hold:
90094+
[export_map] is sound, for every rebinded export id, its key is indeed in
90095+
[export_map] since we know its old bindings are no longer valid, i.e
90096+
Lam_stats.t is not valid
90097+
*)
9006490098
let coerce_and_group_big_lambda
9006590099
old_exports
9006690100
old_export_sets
9006790101
lam =
9006890102
match flatten [] lam with
90069-
| Lam.Lprim {primitive = Pmakeblock _; args = lambda_exports }, reverse_input
90103+
| Lprim {primitive = Pmakeblock _; args = lambda_exports }, reverse_input
9007090104
->
9007190105
handle_exports old_exports old_export_sets lambda_exports reverse_input
9007290106
| _ -> assert false
@@ -100664,16 +100698,7 @@ let compile ~filename output_prefix env _sigs
100664100698
exports = coerced_input.export_list
100665100699
} in
100666100700
(* TODO: turn in on debug mode later*)
100667-
let () =
100668-
100669-
if Js_config.is_same_file () then
100670-
let f =
100671-
Ext_filename.chop_extension ~loc:__LOC__ filename ^ ".lambda" in
100672-
Ext_pervasives.with_file_as_pp f begin fun fmt ->
100673-
Format.pp_print_list ~pp_sep:Format.pp_print_newline
100674-
(Lam_group.pp_group env) fmt (coerced_input.groups)
100675-
end;
100676-
in
100701+
100677100702
(** Also need analyze its depenency is pure or not *)
100678100703
let no_side_effects rest =
100679100704
Ext_list.for_all_opt (fun (x : Lam_group.t) ->

jscomp/core/lam_coercion.ml

+52-23
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,17 @@
2929
(*
3030
Invariant: The last one is always [exports]
3131
Compile definitions
32-
Compile exports
33-
Assume Pmakeblock(_,_),
34-
lambda_exports are pure
35-
compile each binding with a return value
36-
This might be wrong in toplevel
37-
TODO: add this check as early as possible in the beginning
32+
Compile exports
33+
Assume Pmakeblock(_,_),
34+
lambda_exports are pure
35+
compile each binding with a return value
36+
37+
Such invariant might be wrong in toplevel (since it is all bindings)
38+
39+
We should add this check as early as possible
40+
*)
41+
42+
(*
3843
- {[ Ident.same id eid]} is more correct,
3944
however, it will introduce a coercion, which is not necessary,
4045
as long as its name is the same, we want to avoid
@@ -71,16 +76,13 @@
7176
type t = {
7277
export_list : Ident.t list ;
7378
export_set : Ident_set.t;
74-
export_map : Lam.t Ident_map.t ;
75-
groups : Lam_group.t list ;
79+
export_map : Lam.t Ident_map.t ;
80+
(** not used in code generation, mostly used for store some information in cmj files
81+
*)
82+
groups : Lam_group.t list ; (* all code to be compiled later = original code + rebound coercions *)
7683
}
7784

78-
let init export_set =
79-
{ export_list = [];
80-
export_set ;
81-
export_map = Ident_map.empty ;
82-
groups = []
83-
}
85+
8486
let handle_exports
8587
(original_exports : Ident.t list)
8688
(original_export_set : Ident_set.t)
@@ -89,28 +91,47 @@ let handle_exports
8991
let tbl = String_hash_set.create len in
9092
let ({export_list ; export_set ; groups = coercion_groups } as result) =
9193
List.fold_right2
92-
(fun (original_export_id : Ident.t) lam (acc : t) ->
93-
let original_name = original_export_id.name in
94+
(fun (original_export_id : Ident.t) (lam : Lam.t) (acc : t) ->
95+
let original_name = original_export_id.name in
9496
if not @@ String_hash_set.check_add tbl original_name then
9597
Bs_exception.error (Bs_duplicate_exports original_name);
96-
(match (lam : Lam.t) with
98+
(match lam with
9799
| Lvar id
98100
when Ident.name id = original_name ->
99101
{ acc with
100102
export_list = id :: acc.export_list ;
101103
export_set =
102-
(Ident_set.add id (Ident_set.remove original_export_id acc.export_set))
103-
}
104+
if id.stamp = original_export_id.stamp then acc.export_set
105+
else (Ident_set.add id (Ident_set.remove original_export_id acc.export_set))
106+
}
104107
| _ ->
105-
(* Invariant: [eid] can not be bound before *)
108+
(*
109+
Example:
110+
{[
111+
let N = [a0,a1,a2,a3]
112+
in [[ N[0], N[2]]]
113+
114+
]}
115+
After optimization
116+
{[
117+
[ [ a0, a2] ]
118+
]}
119+
Here [N] is elminated while N is still exported identifier
120+
Invariant: [eid] can not be bound before
121+
FIX: this invariant is not guaranteed.
122+
Bug manifested: when querying arity info about N, it returns an array
123+
of size 4 instead of 2
124+
*)
106125
{ acc with
107126
export_list = original_export_id :: acc.export_list;
108127
export_map = Ident_map.add original_export_id lam acc.export_map;
109128
groups = Single(Strict, original_export_id, lam) :: acc.groups
110129
});
111130
)
112-
original_exports lambda_exports
113-
(init original_export_set)
131+
original_exports
132+
lambda_exports
133+
{export_list = []; export_set = original_export_set; export_map = Ident_map.empty; groups = []}
134+
(* (init original_export_set) *)
114135
in
115136

116137
let (export_map, coerced_input) =
@@ -119,6 +140,9 @@ let handle_exports
119140
(match (x : Lam_group.t) with
120141
| Single (_,id,lam) when Ident_set.mem id export_set
121142
-> Ident_map.add id lam export_map
143+
(** relies on the Invariant that [eoid] can not be bound before
144+
FIX: such invariant may not hold
145+
*)
122146
| _ -> export_map), x :: acc ) (result.export_map, result.groups) reverse_input in
123147
{ result with export_map ; groups = Lam_dce.remove export_list coerced_input }
124148

@@ -146,12 +170,17 @@ let rec flatten
146170
| x ->
147171
x, acc
148172

173+
(** Invarinat to hold:
174+
[export_map] is sound, for every rebinded export id, its key is indeed in
175+
[export_map] since we know its old bindings are no longer valid, i.e
176+
Lam_stats.t is not valid
177+
*)
149178
let coerce_and_group_big_lambda
150179
old_exports
151180
old_export_sets
152181
lam =
153182
match flatten [] lam with
154-
| Lam.Lprim {primitive = Pmakeblock _; args = lambda_exports }, reverse_input
183+
| Lprim {primitive = Pmakeblock _; args = lambda_exports }, reverse_input
155184
->
156185
handle_exports old_exports old_export_sets lambda_exports reverse_input
157186
| _ -> assert false

jscomp/test/.depend

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ gpr_405_test.cmi :
1717
infer_type_test.cmi : ../runtime/js.cmi
1818
inline_edge_cases.cmi :
1919
inline_map_test.cmi :
20+
inner_define.cmi :
2021
literals.cmi :
2122
map_test.cmi :
2223
mt.cmi :
@@ -271,7 +272,7 @@ inline_regression_test.cmj : ../stdlib/string.cmj mt.cmj \
271272
../stdlib/filename.cmj
272273
inline_string_test.cmj : ../runtime/js.cmj
273274
inner_call.cmj : ../runtime/js.cmj inner_define.cmj
274-
inner_define.cmj :
275+
inner_define.cmj : inner_define.cmi
275276
inner_unused.cmj : ../stdlib/set.cmj ../runtime/js.cmj
276277
installation_test.cmj : ../others/node.cmj mt.cmj ../runtime/js.cmj \
277278
app_root_finder.cmj

jscomp/test/inner_call.js

+10
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,14 @@ var Inner_define = require("./inner_define.js");
55

66
console.log(Curry._2(Inner_define.N[/* add */0], 1, 2));
77

8+
function f(x) {
9+
return /* tuple */[
10+
Curry._1(Inner_define.N0[/* f1 */0], x),
11+
Curry._2(Inner_define.N0[/* f2 */1], x, x),
12+
Curry._3(Inner_define.N0[/* f3 */2], x, x, x),
13+
Curry._2(Inner_define.N1[/* f2 */0], x, x)
14+
];
15+
}
16+
17+
exports.f = f;
818
/* Not a pure module */

jscomp/test/inner_call.ml

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11

22

33

4-
Js.log (Inner_define.N.add 1 2)
4+
Js.log (Inner_define.N.add 1 2)
5+
6+
7+
open Inner_define
8+
9+
let f x =
10+
N0.f1 x , N0.f2 x x, N0.f3 x x x,
11+
N1.f2 x x

jscomp/test/inner_define.js

+34-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,38 @@ function add(x, y) {
77

88
var N = /* module */[/* add */add];
99

10-
exports.N = N;
10+
function f1() {
11+
return /* () */0;
12+
}
13+
14+
function f2(_, _$1) {
15+
return /* () */0;
16+
}
17+
18+
function f3(_, _$1, _$2) {
19+
return /* () */0;
20+
}
21+
22+
var N0 = /* module */[
23+
/* f1 */f1,
24+
/* f2 */f2,
25+
/* f3 */f3
26+
];
27+
28+
function f2$1(_, _$1) {
29+
return /* () */0;
30+
}
31+
32+
function f3$1(_, _$1, _$2) {
33+
return /* () */0;
34+
}
35+
36+
var N1 = [
37+
f2$1,
38+
f3$1
39+
];
40+
41+
exports.N = N;
42+
exports.N0 = N0;
43+
exports.N1 = N1;
1144
/* No side effect */

0 commit comments

Comments
 (0)