@@ -74140,8 +74140,13 @@ let pp_ident_tbl fmt (ident_tbl : ident_tbl) =
74140
74140
ident_tbl
74141
74141
74142
74142
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
74145
74150
end
74146
74151
module Lam_util : sig
74147
74152
#1 "lam_util.mli"
@@ -89944,12 +89949,17 @@ end = struct
89944
89949
(*
89945
89950
Invariant: The last one is always [exports]
89946
89951
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
+ (*
89953
89963
- {[ Ident.same id eid]} is more correct,
89954
89964
however, it will introduce a coercion, which is not necessary,
89955
89965
as long as its name is the same, we want to avoid
@@ -89986,16 +89996,13 @@ end = struct
89986
89996
type t = {
89987
89997
export_list : Ident.t list ;
89988
89998
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 *)
89991
90003
}
89992
90004
89993
- let init export_set =
89994
- { export_list = [];
89995
- export_set ;
89996
- export_map = Ident_map.empty ;
89997
- groups = []
89998
- }
90005
+
89999
90006
let handle_exports
90000
90007
(original_exports : Ident.t list)
90001
90008
(original_export_set : Ident_set.t)
@@ -90004,28 +90011,47 @@ let handle_exports
90004
90011
let tbl = String_hash_set.create len in
90005
90012
let ({export_list ; export_set ; groups = coercion_groups } as result) =
90006
90013
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
90009
90016
if not @@ String_hash_set.check_add tbl original_name then
90010
90017
Bs_exception.error (Bs_duplicate_exports original_name);
90011
- (match ( lam : Lam.t) with
90018
+ (match lam with
90012
90019
| Lvar id
90013
90020
when Ident.name id = original_name ->
90014
90021
{ acc with
90015
90022
export_list = id :: acc.export_list ;
90016
90023
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
+ }
90019
90027
| _ ->
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
+ *)
90021
90045
{ acc with
90022
90046
export_list = original_export_id :: acc.export_list;
90023
90047
export_map = Ident_map.add original_export_id lam acc.export_map;
90024
90048
groups = Single(Strict, original_export_id, lam) :: acc.groups
90025
90049
});
90026
90050
)
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) *)
90029
90055
in
90030
90056
90031
90057
let (export_map, coerced_input) =
@@ -90034,6 +90060,9 @@ let handle_exports
90034
90060
(match (x : Lam_group.t) with
90035
90061
| Single (_,id,lam) when Ident_set.mem id export_set
90036
90062
-> 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
+ *)
90037
90066
| _ -> export_map), x :: acc ) (result.export_map, result.groups) reverse_input in
90038
90067
{ result with export_map ; groups = Lam_dce.remove export_list coerced_input }
90039
90068
@@ -90061,12 +90090,17 @@ let rec flatten
90061
90090
| x ->
90062
90091
x, acc
90063
90092
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
+ *)
90064
90098
let coerce_and_group_big_lambda
90065
90099
old_exports
90066
90100
old_export_sets
90067
90101
lam =
90068
90102
match flatten [] lam with
90069
- | Lam. Lprim {primitive = Pmakeblock _; args = lambda_exports }, reverse_input
90103
+ | Lprim {primitive = Pmakeblock _; args = lambda_exports }, reverse_input
90070
90104
->
90071
90105
handle_exports old_exports old_export_sets lambda_exports reverse_input
90072
90106
| _ -> assert false
@@ -100664,16 +100698,7 @@ let compile ~filename output_prefix env _sigs
100664
100698
exports = coerced_input.export_list
100665
100699
} in
100666
100700
(* 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
+
100677
100702
(** Also need analyze its depenency is pure or not *)
100678
100703
let no_side_effects rest =
100679
100704
Ext_list.for_all_opt (fun (x : Lam_group.t) ->
0 commit comments