@@ -182,10 +182,9 @@ let compile ~filename non_export env _sigs lam =
182
182
let lam = Lam_pass_remove_alias. simplify_alias meta lam in
183
183
let lam = Lam_group. deep_flatten lam in
184
184
let () = Lam_pass_collect. collect_helper meta lam in
185
- let () = ignore @@ _d lam in
186
-
187
185
let lam =
188
186
lam
187
+ |> _d
189
188
|> Lam_pass_alpha_conversion. alpha_conversion meta
190
189
|> Lam_pass_exits. simplify_exits in
191
190
let () = Lam_pass_collect. collect_helper meta lam in
@@ -215,19 +214,39 @@ let compile ~filename non_export env _sigs lam =
215
214
216
215
begin
217
216
match (lam : Lambda.lambda ) with
218
- | Lprim (Psetglobal id , [biglambda ]) (* ATT: might be wrong in toplevel * ) ->
217
+ | Lprim (Psetglobal id, [biglambda])
218
+ ->
219
+ (* Invariant: The last one is always [exports]
220
+ Compile definitions
221
+ Compile exports
222
+ Assume Pmakeblock(_,_),
223
+ lambda_exports are pure
224
+ compile each binding with a return value
225
+ This might be wrong in toplevel
226
+ *)
227
+
219
228
begin
220
229
match Lam_group. flatten [] biglambda with
221
230
| Lprim ( (Pmakeblock (_ ,_ ,_ ), lambda_exports )), rest ->
222
- let coercion_groups, new_exports =
231
+ let coercion_groups, new_exports, new_export_set, export_map =
223
232
if non_export then
224
- [] , []
233
+ [] , [] , Ident_set. empty, Ident_map. empty
225
234
else
226
235
List. fold_right2
227
- (fun eid lam (coercions , new_exports ) ->
236
+ (fun eid lam (coercions , new_exports , new_export_set , export_map ) ->
228
237
match (lam : Lambda.lambda ) with
229
- | Lvar id when Ident. name id = Ident. name eid ->
230
- (coercions, id :: new_exports)
238
+ | Lvar id
239
+ when Ident. name id = Ident. name eid ->
240
+ (* {[ Ident.same id eid]} is more correct,
241
+ however, it will introduce
242
+ a coercion, which is not necessary,
243
+ as long as its name is the same, we want to avoid
244
+ another coercion
245
+ *)
246
+ (coercions,
247
+ id :: new_exports,
248
+ Ident_set. add id new_export_set,
249
+ export_map)
231
250
| _ -> (* * TODO : bug
232
251
check [map.ml] here coercion, we introduced
233
252
rebound which is not corrrect
@@ -243,15 +262,25 @@ let compile ~filename non_export env _sigs lam =
243
262
however
244
263
*)
245
264
(Lam_group. Single (Strict ,eid, lam) :: coercions,
246
- eid :: new_exports))
247
- meta.exports lambda_exports ([] ,[] )
265
+ eid :: new_exports,
266
+ Ident_set. add eid new_export_set,
267
+ Ident_map. add eid lam export_map))
268
+ meta.exports lambda_exports
269
+ ([] ,[] , Ident_set. empty, Ident_map. empty)
248
270
in
249
271
250
272
let meta = { meta with
251
- export_idents = Lam_util. ident_set_of_list new_exports ;
273
+ export_idents = new_export_set ;
252
274
exports = new_exports
253
275
} in
254
- let rest = List. rev_append rest coercion_groups in
276
+ let (export_map, rest) =
277
+ List. fold_left
278
+ (fun (export_map , acc ) x ->
279
+ (match (x : Lam_group.t ) with
280
+ | Single (_,id,lam) when Ident_set. mem id new_export_set
281
+ -> Ident_map. add id lam export_map
282
+ | _ -> export_map), x :: acc ) (export_map, coercion_groups) rest in
283
+
255
284
let () =
256
285
if not @@ Ext_string. is_empty filename
257
286
then
@@ -261,13 +290,6 @@ let compile ~filename non_export env _sigs lam =
261
290
Format. pp_print_list ~pp_sep: Format. pp_print_newline
262
291
(Lam_group. pp_group env) fmt rest ;
263
292
in
264
- (* Invariant: The last one is always [exports]
265
- Compile definitions
266
- Compile exports
267
- Assume Pmakeblock(_,_),
268
- lambda_exports are pure
269
- compile each binding with a return value
270
- *)
271
293
let rest = Lam_dce. remove meta.exports rest
272
294
in
273
295
let module E = struct exception Not_pure of string end in
@@ -335,8 +357,8 @@ let compile ~filename non_export env _sigs lam =
335
357
336
358
(* Exporting ... *)
337
359
let v =
338
- Lam_stats_util . export_to_cmj meta maybe_pure external_module_ids
339
- (if non_export then [] else lambda_exports )
360
+ Lam_stats_export . export_to_cmj meta maybe_pure external_module_ids
361
+ (if non_export then Ident_map. empty else export_map )
340
362
in
341
363
(if not @@ Ext_string. is_empty filename then
342
364
Js_cmj_format. to_file
0 commit comments