forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlam_compile_main.ml
433 lines (393 loc) · 16.1 KB
/
lam_compile_main.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
module E = Js_exp_make
module S = Js_stmt_make
open Js_output.Ops
let compile_group ({filename = file_name; env;} as meta : Lam_stats.t)
(x : Lam_group.t) : Js_output.t =
match x, file_name with
(*
We need
2. [E.builtin_dot] for javascript builtin
3. [E.mldot]
*)
(* ATTENTION: check {!Lam_compile_global} for consistency *)
(** Special handling for values in [Pervasives] *)
| Single(_, ({name="stdout"|"stderr"|"stdin";_} as id),_ ),
"pervasives.ml" ->
Js_output.of_stmt @@ S.alias_variable id
~exp:(E.runtime_ref Js_runtime_modules.io id.name)
(*
we delegate [stdout, stderr, and stdin] into [caml_io] module,
the motivation is to help dead code eliminatiion, it's helpful
to make those parts pure (not a function call), then it can be removed
if unused
*)
| Single(_, ({name="infinity";_} as id),_ ), "pervasives.ml"
-> (* TODO: check relative path to compiler*)
Js_output.of_stmt @@ S.alias_variable id ~exp:(E.js_global "Infinity")
| Single(_, ({name="neg_infinity";_} as id),_ ), "pervasives.ml" ->
Js_output.of_stmt @@ S.alias_variable id ~exp:(E.js_global "-Infinity")
| Single(_, ({name="nan";_} as id),_ ), "pervasives.ml" ->
Js_output.of_stmt @@ S.alias_variable id ~exp:(E.js_global "NaN")
(* TODO:
Make it more safe, we should rewrite the last one...
checkout [E.mldot], it would make sense that cross module inlining done there
In general, we would like to do such specialization on primitive specialization
[Lam_dispatch_primitive], here it makes an exception since this function is not a primitive
*)
| Single(_, ({name="^";_} as id),_ ), "pervasives.ml" ->
Js_output.of_stmt @@ S.alias_variable id
~exp:(let a = Ext_ident.create "a" in
let b = Ext_ident.create "b" in
E.ocaml_fun [a;b] [S.return (E.string_append (E.var a) (E.var b))]
)
(* QUICK hack to make hello world example nicer,
Note the arity of [print_endline] is already analyzed before,
so it should be safe
*)
| Single(_, ({name="print_endline";_} as id),_ ), "pervasives.ml" ->
Js_output.of_stmt @@ S.alias_variable id
~exp:(let param = Ext_ident.create "param" in
E.ocaml_fun [param] [S.return
(E.seq (E.call ~info:{arity=Full; call_info = Call_na}
(E.js_global "console.log") [E.var param])
E.zero_int_literal )] )
| Single(_, ({name="prerr_endline";_} as id),_ ), "pervasives.ml" ->
Js_output.of_stmt @@ S.alias_variable id
~exp:(let param = Ext_ident.create "param" in
E.ocaml_fun [param] [S.return
(E.seq (E.call ~info:{arity=Full; call_info = Call_na}
(E.js_global "console.error") [E.var param])
E.zero_int_literal )] )
| Single(_, ({name="string_of_int";_} as id),_ ), "pervasives.ml" ->
Js_output.of_stmt @@ S.alias_variable id
~exp:(
let arg = Ext_ident.create "param" in
E.ocaml_fun [arg] [S.return (E.anything_to_string (E.var arg))]
)
| Single(_, ({name="max_float";_} as id),_ ), "pervasives.ml" ->
Js_output.of_stmt @@ S.alias_variable id
~exp:(E.js_global_dot "Number" "MAX_VALUE")
| Single(_, ({name="min_float";_} as id) ,_ ), "pervasives.ml" ->
Js_output.of_stmt @@ S.alias_variable id
~exp:(E.js_global_dot "Number" "MIN_VALUE")
| Single(_, ({name="epsilon_float";_} as id) ,_ ), "pervasives.ml" ->
Js_output.of_stmt @@ S.alias_variable id
~exp:(E.float "2.220446049250313e-16")
| Single(_, ({name="cat";_} as id) ,_ ), "bytes.ml" ->
Js_output.of_stmt @@ S.alias_variable id
~exp:(let a = Ext_ident.create "a" in
let b = Ext_ident.create "b" in
E.ocaml_fun [a;b] [S.return (E.array_append (E.var a) (E.var b))]
)
(** Special handling for values in [Sys] *)
| Single(_, ({name="max_array_length" | "max_string_length";_} as id) ,_ ), "sys.ml" ->
(* See [js_knowledge] Array size section, can not be expressed by OCaml int,
note that casual handling of {!Sys.max_string_length} could result into
negative value which could cause wrong behavior of {!Buffer.create}
*)
Js_output.of_stmt @@ S.alias_variable id ~exp:(E.float "2147483647") (*2 ^ 31 - 1*)
| Single(_, ({name="max_int";_} as id) ,_ ), ("sys.ml" | "nativeint.ml") ->
(* See [js_knowledge] Max int section, (2. ** 53. -. 1.;;)
can not be expressed by OCaml int
FIXME: we need handle {!Nativeint} and {!Sys} differently
*)
Js_output.of_stmt @@ S.alias_variable id
~exp:(E.float "9007199254740991.")
| Single(_, ({name="min_int";_} as id) ,_ ), ("sys.ml" | "nativeint.ml") ->
(* See [js_knowledge] Max int section, -. (2. ** 53. -. 1.);;
can not be expressed by OCaml int
FIXME: we need handle {!Nativeint} and {!Sys} differently
*)
Js_output.of_stmt @@ S.alias_variable id
~exp:(E.float ("-9007199254740991."))
| Single (kind, id, lam), _ ->
(* let lam = Optimizer.simplify_lets [] lam in *)
(* can not apply again, it's wrong USE it with care*)
(* ([Js_stmt_make.comment (Gen_of_env.query_type id env )], None) ++ *)
Lam_compile.compile_let kind { st = Declare (kind, id);
should_return = ReturnFalse;
jmp_table = Lam_compile_defs.empty_handler_map;
meta
} id lam
| Recursive id_lams, _ ->
Lam_compile.compile_recursive_lets
{ st = EffectCall ;
should_return = ReturnFalse;
jmp_table = Lam_compile_defs.empty_handler_map;
meta
}
id_lams
| Nop lam, _ -> (* TODO: Side effect callls, log and see statistics *)
Lam_compile.compile_lambda {st = EffectCall;
should_return = ReturnFalse;
jmp_table = Lam_compile_defs.empty_handler_map;
meta
} lam
;;
(** Also need analyze its depenency is pure or not *)
let no_side_effects (rest : Lam_group.t list) : string option =
Ext_list.for_all_opt (fun (x : Lam_group.t) ->
match x with
| Single(kind,id,body) ->
begin
match kind with
| Strict | Variable ->
if not @@ Lam_analysis.no_side_effects body
then Some (Printf.sprintf "%s" id.name)
else None
| _ -> None
end
| Recursive bindings ->
Ext_list.for_all_opt (fun (id,lam) ->
if not @@ Lam_analysis.no_side_effects lam
then Some (Printf.sprintf "%s" id.Ident.name )
else None
) bindings
| Nop lam ->
if not @@ Lam_analysis.no_side_effects lam
then
(* (Lam_util.string_of_lambda lam) *)
Some ""
else None (* TODO :*))
rest
(** Actually simplify_lets is kind of global optimization since it requires you to know whether
it's used or not
*)
let compile ~filename output_prefix env _sigs
(lam : Lambda.lambda) =
let export_idents = Translmod.get_export_identifiers() in
let export_ident_sets = Ident_set.of_list export_idents in
(* To make toplevel happy - reentrant for js-demo *)
let () =
#if BS_DEBUG then
export_idents |> List.iter
(fun (id : Ident.t) -> Ext_log.dwarn __LOC__ "export: %s/%d" id.name id.stamp) ;
#end
Lam_compile_env.reset () ;
in
let lam, may_required_modules = Lam.convert export_ident_sets lam in
let _d = fun s lam ->
let result = Lam_util.dump env s lam in
#if BS_DEBUG then
Ext_log.dwarn __LOC__ "START CHECKING PASS %s@." s;
ignore @@ Lam.check (Js_config.get_current_file ()) lam;
Ext_log.dwarn __LOC__ "FINISH CHECKING PASS %s@." s;
#end
result
in
let _j = Js_pass_debug.dump in
let lam = _d "initial" lam in
let lam = Lam_pass_deep_flatten.deep_flatten lam in
let lam = _d "flatten" lam in
let meta =
Lam_pass_collect.count_alias_globals env filename
export_idents export_ident_sets lam in
let lam =
let lam =
lam
|> _d "flattern"
|> Lam_pass_exits.simplify_exits
|> _d "simplyf_exits"
|> (fun lam -> Lam_pass_collect.collect_helper meta lam; lam)
|> Lam_pass_remove_alias.simplify_alias meta
|> _d "simplify_alias"
|> Lam_pass_deep_flatten.deep_flatten
|> _d "flatten"
in (* Inling happens*)
let () = Lam_pass_collect.collect_helper meta lam in
let lam = Lam_pass_remove_alias.simplify_alias meta lam in
let lam = Lam_pass_deep_flatten.deep_flatten lam in
let () = Lam_pass_collect.collect_helper meta lam in
let lam =
lam
|> _d "alpha_before"
|> Lam_pass_alpha_conversion.alpha_conversion meta
|> Lam_pass_exits.simplify_exits in
let () = Lam_pass_collect.collect_helper meta lam in
lam
|> _d "simplify_alias_before"
|> Lam_pass_remove_alias.simplify_alias meta
|> _d "alpha_conversion"
|> Lam_pass_alpha_conversion.alpha_conversion meta
|> _d "before-simplify_lets"
(* we should investigate a better way to put different passes : )*)
|> Lam_pass_lets_dce.simplify_lets
|> _d "before-simplify-exits"
(* |> (fun lam -> Lam_pass_collect.collect_helper meta lam
; Lam_pass_remove_alias.simplify_alias meta lam) *)
(* |> Lam_group_pass.scc_pass
|> _d "scc" *)
|> Lam_pass_exits.simplify_exits
|> _d "simplify_lets"
#if BS_DEBUG then
|> (fun lam ->
let () =
Ext_log.dwarn __LOC__ "Before coercion: %a@." Lam_stats.print meta in
Lam.check (Js_config.get_current_file ()) lam
)
#end
in
let ({Lam_coercion.groups = groups } as coerced_input , meta) =
Lam_coercion.coerce_and_group_big_lambda meta lam
in
#if BS_DEBUG then
let () =
Ext_log.dwarn __LOC__ "After coercion: %a@." Lam_stats.print meta ;
if Js_config.is_same_file () then
let f =
Ext_filename.chop_extension ~loc:__LOC__ filename ^ ".lambda" in
Ext_pervasives.with_file_as_pp f begin fun fmt ->
Format.pp_print_list ~pp_sep:Format.pp_print_newline
(Lam_group.pp_group env) fmt (coerced_input.groups)
end;
in
#end
let maybe_pure = no_side_effects groups in
#if BS_DEBUG then
let () = Ext_log.dwarn __LOC__ "\n@[[TIME:]Pre-compile: %f@]@." (Sys.time () *. 1000.) in
#end
let body =
groups
|> List.map (fun group -> compile_group meta group)
|> Js_output.concat
|> Js_output.to_block
in
#if BS_DEBUG then
let () = Ext_log.dwarn __LOC__ "\n@[[TIME:]Post-compile: %f@]@." (Sys.time () *. 1000.) in
#end
(* The file is not big at all compared with [cmo] *)
(* Ext_marshal.to_file (Ext_filename.chop_extension filename ^ ".mj") js; *)
let js : J.program =
{ J.name = filename ;
exports = meta.exports ;
export_set = Ident_set.of_list meta.exports;
block = body}
in
js
|> _j "initial"
|> Js_pass_flatten.program
|> _j "flattern"
|> Js_pass_tailcall_inline.tailcall_inline
|> _j "inline_and_shake"
|> Js_pass_flatten_and_mark_dead.program
|> _j "flatten_and_mark_dead"
(* |> Js_inline_and_eliminate.inline_and_shake *)
(* |> _j "inline_and_shake" *)
|> (fun js -> ignore @@ Js_pass_scope.program js ; js )
|> Js_shake.shake_program
|> _j "shake"
|> ( fun (program: J.program) ->
let external_module_ids =
Lam_compile_env.get_required_modules
may_required_modules
(Js_fold_basic.calculate_hard_dependencies program.block)
|>
(fun x ->
if !Js_config.sort_imports then
Ext_list.sort_via_array
(fun (id1 : Lam_module_ident.t) (id2 : Lam_module_ident.t) ->
Ext_string.compare (Lam_module_ident.name id1) (Lam_module_ident.name id2)
) x
else
x
)
in
let v =
Lam_stats_export.export_to_cmj
meta
maybe_pure
external_module_ids
coerced_input.export_map
in
(if not @@ !Clflags.dont_write_files then
Js_cmj_format.to_file
(output_prefix ^ Literals.suffix_cmj) v);
{J.program = program ; side_effect = v.effect ; modules = external_module_ids }
)
;;
let (//) = Filename.concat
let lambda_as_module
env
(sigs : Types.signature)
(filename : string)
(output_prefix : string)
(lam : Lambda.lambda) =
begin
Js_config.set_current_file filename ;
#if BS_DEBUG then
Js_config.set_debug_file "demo.ml";
#end
let lambda_output = compile ~filename output_prefix env sigs lam in
let basename =
(* #758, output_prefix is already chopped *)
Ext_namespace.js_name_of_basename (Filename.basename
output_prefix (* -o *)
(* filename *) (* see #757 *)
) in
(* #913
only generate little-case js file
*)
(* Not re-entrant *)
let package_info = Js_packages_state.get_packages_info () in
if Js_packages_info.is_empty package_info then
begin
#if BS_DEBUG then
Ext_log.dwarn __LOC__ "@[script mode@]@.";
#end
(* script mode *)
let output_chan chan =
Js_dump_program.dump_deps_program ~output_prefix NodeJS lambda_output chan in
(if !Js_config.dump_js then output_chan stdout);
if not @@ !Clflags.dont_write_files then
Ext_pervasives.with_file_as_chan
(Filename.dirname filename // basename)
output_chan
end
else begin
package_info.module_systems
|> List.iter begin fun (module_system, _path) ->
let output_chan chan =
Js_dump_program.dump_deps_program ~output_prefix
module_system
lambda_output
chan in
(if !Js_config.dump_js then
output_chan stdout);
if not @@ !Clflags.dont_write_files then
Ext_pervasives.with_file_as_chan
(Lazy.force Ext_filename.package_dir //
_path //
basename
(* #913 only generate little-case js file *)
) output_chan
end
end
end
(* We can use {!Env.current_unit = "Pervasives"} to tell if it is some specific module,
We need handle some definitions in standard libraries in a special way, most are io specific,
includes {!Pervasives.stdin, Pervasives.stdout, Pervasives.stderr}
However, use filename instead of {!Env.current_unit} is more honest, since node-js module system is coupled with the file name
*)