2929
3030
3131
32-
33- let annotate (meta : Lam_stats.t )
34- rec_flag
35- (k :Ident.t ) (v : Lam_arity.t ) lambda =
36- (* Ext_log.dwarn __LOC__ "%s/%d" k.name k.stamp; *)
32+ (* * Check, it is shared across ident_tbl,
33+ Only [Lassign] will break such invariant,
34+ how about guarantee that [Lassign] only check the local ref
35+ and we track which ids are [Lassign]ed
36+ *)
37+ (* *
38+ might not be the same due to refinement
39+ assert (old.arity = v)
40+ *)
41+ let annotate (meta : Lam_stats.t ) rec_flag (k :Ident.t ) (arity : Lam_arity.t ) lambda =
3742 match Ident_hashtbl. find_opt meta.ident_tbl k with
3843 | None -> (* * FIXME: need do a sanity check of arity is NA or Determin(_,[],_) *)
39- Ident_hashtbl. add meta.ident_tbl k
40- (FunctionId {
41- arity = v; lambda; rec_flag})
44+ Ident_hashtbl. add meta.ident_tbl k
45+ (FunctionId {arity; lambda; rec_flag})
4246 | Some (FunctionId old ) ->
43- (* * Check, it is shared across ident_tbl,
44- Only [Lassign] will break such invariant,
45- how about guarantee that [Lassign] only check the local ref
46- and we track which ids are [Lassign]ed
47- *)
48- (* *
49- might not be the same due to refinement
50- assert (old.arity = v)
51- *)
52- old.arity < - v (* due to we keep refining arity analysis after each round*)
53-
5447
48+ old.arity < - arity (* due to we keep refining arity analysis after each round*)
5549 | _ -> assert false (* TODO -- avoid exception *)
5650
5751
5852(* * it only make senses recording arities for
5953 function definition,
6054 alias propgation - and toplevel identifiers, this needs to be exported
61- *)
55+ *)
6256let collect_helper (meta : Lam_stats.t ) (lam : Lam.t ) =
6357 let rec collect_bind rec_flag
6458 (kind : Lam.let_kind )
@@ -90,83 +84,83 @@ let collect_helper (meta : Lam_stats.t) (lam : Lam.t) =
9084 (Lam_util. kind_of_lambda_block Null_undefined ls )
9185 | Lglobal_module v
9286 ->
93- Lam_util. alias_ident_or_global meta ident v (Module v) kind;
87+ Lam_util. alias_ident_or_global meta ident v (Module v) kind;
9488 | Lvar v
9589 ->
96- (
97- (* if Ident.global v then *)
98- Lam_util. alias_ident_or_global meta ident v NA kind
99- (* enven for not subsitution, it still propogate some properties *)
100- (* else () *)
101- )
102- | Lfunction { params; body = l }
103- (* * TODO record parameters ident ?, but it will be broken after inlining *)
90+ (
91+ (* if Ident.global v then *)
92+ Lam_util. alias_ident_or_global meta ident v NA kind
93+ (* enven for not subsitution, it still propogate some properties *)
94+ (* else () *)
95+ )
96+ | Lfunction { params; body}
97+ (* * TODO record parameters ident ?, but it will be broken after inlining *)
10498 ->
105- (* * TODO could be optimized in one pass?
106- -- since collect would iter everywhere,
107- so -- it would still iterate internally
108- *)
99+ (* * TODO could be optimized in one pass?
100+ -- since collect would iter everywhere,
101+ so -- it would still iterate internally
102+ *)
109103
110104 List. iter (fun p -> Ident_hashtbl. add meta.ident_tbl p Parameter ) params;
111105 let arity = Lam_arity_analysis. get_arity meta lam in
112106 annotate meta rec_flag ident arity lam;
113- collect l
107+ collect body
114108 | x ->
115- collect x ;
116- if Ident_set. mem ident meta.export_idents then
117- annotate meta rec_flag ident (Lam_arity_analysis. get_arity meta x ) lam
109+ collect x ;
110+ if Ident_set. mem ident meta.export_idents then
111+ annotate meta rec_flag ident (Lam_arity_analysis. get_arity meta x ) lam
118112
119113
120114 and collect (lam : Lam.t ) =
121115 match lam with
122116
123- (** TODO:
124- how about module aliases..
125- record dependency
126- --- tricky -- if we inlining,
127- is it safe to remove it? probably not...
128- * )
117+ (** TODO:
118+ how about module aliases..
119+ record dependency
120+ --- tricky -- if we inlining,
121+ is it safe to remove it? probably not...
122+ * )
129123 | Lconst _ -> ()
130124 | Lvar _ -> ()
131125 | Lapply {fn = l1 ; args = ll ; _} ->
132- collect l1; List. iter collect ll
126+ collect l1; List. iter collect ll
133127 | Lfunction { params; body = l } -> (* functor ? *)
134- List. iter (fun p -> Ident_hashtbl. add meta.ident_tbl p Parameter ) params;
135- collect l
128+ List. iter (fun p -> Ident_hashtbl. add meta.ident_tbl p Parameter ) params;
129+ collect l
136130 | Llet (kind ,ident ,arg ,body ) ->
137- collect_bind Non_rec kind ident arg ; collect body
131+ collect_bind Non_rec kind ident arg ; collect body
138132 | Lletrec (bindings , body ) ->
139- List. iter (fun (ident ,arg ) -> collect_bind Rec Strict ident arg ) bindings;
140- collect body
133+ List. iter (fun (ident ,arg ) -> collect_bind Rec Strict ident arg ) bindings;
134+ collect body
141135 | Lglobal_module _ -> ()
142136 | Lprim {args; _} -> List. iter collect args
143137 | Lswitch (l , {sw_failaction; sw_consts; sw_blocks} ) ->
144- collect l;
145- List. iter (fun (_ , l ) -> collect l) sw_consts;
146- List. iter (fun (_ , l ) -> collect l) sw_blocks;
147- begin match sw_failaction with
138+ collect l;
139+ List. iter (fun (_ , l ) -> collect l) sw_consts;
140+ List. iter (fun (_ , l ) -> collect l) sw_blocks;
141+ begin match sw_failaction with
148142 | None -> ()
149143 | Some x -> collect x
150- end
144+ end
151145 | Lstringswitch (l , sw , d ) ->
152- collect l ;
153- List. iter (fun (_ , l ) -> collect l) sw ;
154- begin match d with
146+ collect l ;
147+ List. iter (fun (_ , l ) -> collect l) sw ;
148+ begin match d with
155149 | Some d -> collect d
156150 | None -> ()
157- end
151+ end
158152 | Lstaticraise (code ,ls ) ->
159- List. iter collect ls
153+ List. iter collect ls
160154 | Lstaticcatch (l1 , (_ ,_ ), l2 ) -> collect l1; collect l2
161155 | Ltrywith (l1 , _ , l2 ) -> collect l1; collect l2
162156 | Lifthenelse (l1 , l2 , l3 ) -> collect l1; collect l2; collect l3
163157 | Lsequence (l1 , l2 ) -> collect l1; collect l2
164158 | Lwhile (l1 , l2 ) -> collect l1; collect l2
165159 | Lfor (_ , l1 , l2 , dir , l3 ) -> collect l1; collect l2; collect l3
166160 | Lassign (v , l ) ->
167- (* Lalias-bound variables are never assigned, so don't increase
168- v's refcollect *)
169- collect l
161+ (* Lalias-bound variables are never assigned, so don't increase
162+ v's refcollect *)
163+ collect l
170164 | Lsend (_ , m , o , ll , _ ) -> List. iter collect (m::o::ll)
171165 | Lifused (_ , l ) -> collect l in collect lam
172166
@@ -181,11 +175,11 @@ let count_alias_globals
181175 let meta : Lam_stats.t =
182176 {alias_tbl = Ident_hashtbl. create 31 ;
183177 ident_tbl = Ident_hashtbl. create 31 ;
184-
178+
185179 exports = export_idents;
186180 filename;
187181 env;
188182 export_idents = export_sets;
189- } in
183+ } in
190184 collect_helper meta lam ;
191185 meta
0 commit comments