Repository navigation
Expand file tree
/
Copy pathlambda.ml
More file actions
1280 lines (1183 loc) · 42.8 KB
/
Copy pathlambda.ml
File metadata and controls
1280 lines (1183 loc) · 42.8 KB
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1996 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
type hoisted_function = {binding: Ident.t; path: string list; loc: Location.t}
type tag_info =
| Blk_constructor of {
name: string;
num_nonconst: int;
runtime: Variant_runtime.tagged_block;
}
| Blk_record_inlined of {
name: string;
num_nonconst: int;
fields: (string * bool (* optional *)) array;
mutable_flag: Asttypes.mutable_flag;
runtime: Variant_runtime.block;
}
| Blk_tuple
| Blk_poly_var
| Blk_record of {
fields: (string * bool (* optional *)) array;
mutable_flag: Asttypes.mutable_flag;
}
| Blk_module of string list
| Blk_module_export of Ident.t list
| Blk_extension
| Blk_record_ext of {
fields: string array;
mutable_flag: Asttypes.mutable_flag;
}
(* Label used by the lambda printer for a block; blocks carry no
numeric tag anymore *)
let tag_label_of_tag_info (tag : tag_info) =
match tag with
| Blk_constructor {name} | Blk_record_inlined {name} -> name
| Blk_tuple | Blk_poly_var | Blk_record _ | Blk_module _ | Blk_module_export _
| Blk_extension | Blk_record_ext _ ->
"0"
let mutable_flag_of_tag_info (tag : tag_info) =
match tag with
| Blk_record_inlined {mutable_flag}
| Blk_record {mutable_flag}
| Blk_record_ext {mutable_flag} ->
mutable_flag
| Blk_tuple | Blk_constructor _ | Blk_poly_var | Blk_module _
| Blk_module_export _ | Blk_extension ->
Immutable
let blk_record fields mutable_flag = Blk_record {fields; mutable_flag}
let blk_record_ext fields mutable_flag = Blk_record_ext {fields; mutable_flag}
let blk_record_inlined fields name num_nonconst ~runtime mutable_flag =
Blk_record_inlined {fields; name; num_nonconst; mutable_flag; runtime}
let ref_tag_info : tag_info =
Blk_record {fields = [|("contents", false)|]; mutable_flag = Mutable}
type field_dbg_info =
| Fld_record of {name: string}
| Fld_module of {name: string}
| Fld_record_inline of {name: string}
| Fld_record_extension of {name: string}
| Fld_tuple
| Fld_poly_var_tag
| Fld_poly_var_content
| Fld_extension
| Fld_variant
| Fld_cons
let fld_record name = Fld_record {name}
let fld_record_extension name = Fld_record_extension {name}
let ref_field_info : field_dbg_info = Fld_record {name = "contents"}
type set_field_dbg_info =
| Fld_record_set of string
| Fld_record_inline_set of string
| Fld_record_extension_set of string
let ref_field_set_info : set_field_dbg_info = Fld_record_set "contents"
let fld_record_set name = Fld_record_set name
let fld_record_inline name = Fld_record_inline {name}
let fld_record_inline_set name = Fld_record_inline_set name
let fld_record_extension_set name = Fld_record_extension_set name
type immediate_or_pointer = Immediate | Pointer
(* The target of a dynamic [import], resolved at translation: the argument
of the import primitive is a module reference, never an expression. *)
type import_source =
| Import_module of {module_: Ident.t; path: string list}
(* a ReScript module, or a value/submodule reached from one
([path = []]: the module itself). The module is resolved here, at
translation; the JS export name for a nested [path] is resolved
where the cmj tables live, at emission. *)
| Import_external of {
module_: External_ffi_types.external_module_name;
path: string list;
(* access path inside the module: @scope segments then the
name; [] means the external is the module itself *)
}
(* `%identity` / `%ignore` / unary `+`: builtins that erase at translation
rather than primitives. See [builtin]. *)
type eliminated = Identity | Ignore
type primitive =
| Pdebugger
| Ptypeof
| Psome
| Psome_not_nest
(** [Some x] where [x] cannot itself be [undefined], so no wrapping is
needed. *)
(* Operations on heap blocks *)
| Pmakeblock of tag_info
| Pfield of int * field_dbg_info
| Psetfield of int * set_field_dbg_info
| Pduprecord
| Precord_rest of string list (* excluded runtime field names *)
(* JS FFI calls, expanded from the external's spec at translation *)
| Pjs_call of {
prim_name: string;
arg_types: External_arg_spec.params;
ffi: External_ffi_types.external_decl;
transformed_jsx: bool;
}
| Pjs_object_create of External_arg_spec.obj_params
| Pjs_object_get of string
| Pjs_object_set of string
(* Exceptions *)
| Praise
(* object operations *)
| Pobjcomp of comparison
| Pobjorder
| Pobjmin
| Pobjmax
| Pobjtag
| Pobjsize
(* Boolean operations *)
| Psequand
| Psequor
| Pnot
| Pboolcomp of comparison
| Pboolorder
| Pboolmin
| Pboolmax
(* Integer operations *)
| Pnegint
| Paddint
| Psubint
| Pmulint
| Pdivint
| Pmodint
| Ppowint
| Pandint
| Porint
| Pxorint
| Pnotint
| Plslint
| Plsrint
| Pasrint
| Pintcomp of comparison
| Pintorder
| Pintmin
| Pintmax
(* Float operations *)
| Pintoffloat
| Pfloatofint
| Pnegfloat
| Pmodfloat
| Paddfloat
| Psubfloat
| Pmulfloat
| Pdivfloat
| Ppowfloat
| Pfloatcomp of comparison
| Pfloatorder
| Pfloatmin
| Pfloatmax
(* BigInt operations *)
| Pnegbigint
| Paddbigint
| Psubbigint
| Ppowbigint
| Pmulbigint
| Pdivbigint
| Pmodbigint
| Pandbigint
| Porbigint
| Pxorbigint
| Pnotbigint
| Plslbigint
| Pasrbigint
| Pbigintcomp of comparison
| Pbigintorder
| Pbigintmin
| Pbigintmax
(* String operations *)
| Pstringlength
| Pstringrefu
| Pstringrefs
| Pstringcomp of comparison
| Pstringorder
| Pstringmin
| Pstringmax
| Pstringadd
(* Array operations *)
| Pmakearray
| Parraylength
| Parrayrefu
| Parraysetu
| Parrayrefs
| Parraysets
(* List primitives *)
| Pmakelist
(* dict primitives *)
| Pmakedict
| Pdict_has
(* promise *)
| Pawait
(* module *)
| Pimport of import_source
| Pinit_mod
| Pupdate_mod
(* hash *)
| Phash
| Phash_mixint
| Phash_mixstring
| Phash_finalmix
(* Test if the argument is a block or an immediate integer *)
| Pisint
(* Test if the (integer) argument is outside an interval *)
(* Test if the argument is null or undefined *)
| Pis_null_undefined
(* exn *)
| Pcreate_extension of string
(* js *)
| Pjscomp of comparison
| Pnull_to_opt
| Pnull_undefined_to_opt
(* Produced by Lam_pass_remove_alias, not by translation *)
| Pis_null
| Pis_undefined
| Pis_not_none
| Pval_from_option
| Pval_from_option_not_nest
| Pis_poly_var_block
(* Validated JavaScript source from [raw], [ffi], or [re], together with its
expression/program kind. For example, [%raw("x + 1")] carries ["x + 1"]
as code, not as a decoded runtime string. *)
| Praw_js_code of Js_raw_info.t
| Pjs_fn_method
(* A JavaScript tagged template operation. For [sql`id = ${id}`], the payload
is ["id = "; ""] and the primitive arguments are [sql; id]. Segment text
remains raw and may contain invalid escapes. *)
| Ptagged_template of string list
(* An ordinary backquoted-template operation. For [`a ${value}\n`], the
payload contains the source and semantic forms of ["a "] and ["\\n"],
and the primitive arguments contain [value]. The source forms are retained
for JavaScript output; semantic forms are used by optimizations. *)
| Ptemplate of Asttypes.template_segment list
and comparison = Ceq | Cneq | Clt | Cgt | Cle | Cge
type structured_constant =
| Const_int of int32
| Const_char of int
(* The decoded Unicode code point; literal source spelling is no longer
present at this layer. *)
| Const_string of string
(* A decoded runtime string value; literal source spelling is no longer
present at this layer. *)
| Const_float of string
| Const_bigint of bool * string
| Const_block of tag_info * structured_constant list
| Const_constructor of Variant_runtime.tag
(** Constant constructor of a nominal variant, from its canonical
runtime descriptor. Integer-represented ones are [Const_int]. *)
| Const_polyvar of string
(** Tagless polymorphic variant; numeric-looking names are [Const_int]. *)
| Const_assertfalse
| Const_module_alias
| Const_js_false
| Const_js_true
| Const_js_null
| Const_some of structured_constant
| Const_js_undefined of {is_unit: bool}
(** [is_unit] tells the unit value apart from JS [undefined]; both emit
[undefined]. *)
(* What a `%builtin` name in the primitive table means. Only [Primitive]
reaches the IR: [mk_builtin] erases the other cases at translation, so
they need no [primitive] constructor to stand in for them. *)
type builtin =
| Primitive of primitive
| Eliminated of eliminated
| Constant of structured_constant
type inline_attribute =
| Always_inline (* [@inline] or [@inline always] *)
| Never_inline (* [@inline never] *)
| Default_inline (* no [@inline] attribute *)
type let_kind = Strict | Alias | StrictOpt | Variable
type function_attribute = {
inline: inline_attribute;
is_a_functor: bool;
return_unit: bool;
async: bool;
directive: string option;
one_unit_arg: bool;
}
type t =
| Lvar of Ident.t
| Lglobal_module of Ident.t
(** A reference to another compilation unit: a name the module system
resolves, not a value this one computes. *)
| Lconst of structured_constant
| Lapply of lambda_apply
| Lfunction of lfunction
| Llet of let_kind * Ident.t * t * t
| Lletrec of (Ident.t * t) list * t
| Lprim of prim_info
| Lswitch of t * lambda_switch
| Lstringswitch of t * (string * t) list * t option
| Lstaticraise of int * t list
| Lstaticcatch of t * (int * Ident.t list) * t
| Ltrywith of t * Ident.t * t
| Lifthenelse of t * t * t
| Lsequence of t * t
| Lbreak
| Lcontinue
| Lwhile of t * t
| Lfor of Ident.t * t * t * Asttypes.direction_flag * t
| Lfor_of of Ident.t * t * t
| Lfor_await_of of Ident.t * t * t
| Lassign of Ident.t * t
and lfunction = {
params: Ident.t list;
body: t;
attr: function_attribute; (* specified with [@inline] attribute *)
loc: Location.t;
}
and prim_info = {primitive: primitive; args: t list; loc: Location.t}
and ap_info = {ap_loc: Location.t; ap_inlined: inline_attribute}
and lambda_apply = {
ap_func: t;
ap_args: t list;
ap_info: ap_info;
ap_transformed_jsx: bool;
}
and switch_key =
| Switch_int of int
| Switch_constructor of Variant_runtime.constructor_case
and switch_dispatch =
| Switch_direct
| Switch_variant of Variant_runtime.matching_facts
and 'a switch = {
sw_consts_full: bool;
sw_consts: (switch_key * 'a) list;
sw_blocks_full: bool;
sw_blocks: (switch_key * 'a) list;
sw_failaction: 'a option;
sw_dispatch: switch_dispatch;
}
and lambda_switch = t switch
(* This is actually a dummy value
not necessary "()", it can be used as a place holder for module
alias etc.
*)
let const_int (i : int) = Const_int (Int32.of_int i)
let const_string s = Const_string (String_literal.normalize_semantic s)
let const_of_typed (c : Asttypes.constant) : structured_constant =
match c with
| Asttypes.Const_int i -> Const_int (Int32.of_int i)
| Asttypes.Const_char i -> Const_char i
| Asttypes.Const_string s -> Const_string s
| Asttypes.Const_float f -> Const_float f
| Asttypes.Const_bigint (sign, i) -> Const_bigint (sign, i)
let const_unit = Const_js_undefined {is_unit = true}
(* The JS value of a constant constructor: unit has its own constant, and a
constructor represented as a number is a genuine number at runtime, so
folding sees it as an ordinary integer. *)
let const_constructor (tag : Variant_runtime.tag) =
if tag.name = "()" then const_unit
else
match tag.literal with
| Some (Variant_runtime.Int v) -> Const_int (Int32.of_int v)
| _ -> Const_constructor tag
(* A constructor with an optional shape carries no payload when constant. *)
let const_shape_none = Const_js_undefined {is_unit = false}
(* The JS value of a polymorphic variant's name: a numeric-looking name is a
number at runtime, anything else is a string. Used both for a tagless
variant and for the name field of one carrying a payload. *)
let const_polyvar name =
if Ext_string.is_valid_hash_number name then
Const_int (Ext_string.hash_number_as_i32_exn name)
else Const_polyvar name
let const_polyvar_name name =
match const_polyvar name with
| Const_polyvar s -> Const_string s
| c -> c
let const_module_alias = Const_module_alias
let lambda_assert_false = Lconst Const_assertfalse
let lambda_module_alias = Lconst const_module_alias
let lambda_unit = Lconst const_unit
let lambda_true = Lconst Const_js_true
let lambda_false = Lconst Const_js_false
(* [r := r.contents + delta]. The reference is mentioned twice, so bind it
unless it is already a variable. *)
let eq_comparison (p : comparison) (p1 : comparison) = p = p1
let eq_field_dbg_info (x : field_dbg_info) (y : field_dbg_info) = x = y
let eq_set_field_dbg_info (x : set_field_dbg_info) (y : set_field_dbg_info) =
x = y
let eq_tag_info (x : tag_info) y = x = y
let eq_primitive_approx (lhs : primitive) (rhs : primitive) =
match lhs with
| Praise
(* generic comparison *)
| Pobjorder | Pobjmin | Pobjmax | Pobjtag | Pobjsize
(* bool primitives *)
| Psequand | Psequor | Pnot | Pboolcomp _ | Pboolorder | Pboolmin | Pboolmax
(* int primitives *)
| Pisint | Pnegint | Paddint | Psubint | Pmulint | Pdivint | Pmodint | Ppowint
| Pnotint | Pandint | Porint | Pxorint | Plslint | Plsrint | Pasrint
| Pintorder | Pintmin | Pintmax
(* float primitives *)
| Pintoffloat | Pfloatofint | Pnegfloat | Paddfloat | Psubfloat | Pmulfloat
| Pdivfloat | Pmodfloat | Ppowfloat | Pfloatorder | Pfloatmin | Pfloatmax
(* bigint primitives *)
| Pnegbigint | Paddbigint | Psubbigint | Pmulbigint | Pdivbigint | Pmodbigint
| Ppowbigint | Pnotbigint | Pandbigint | Porbigint | Pxorbigint | Plslbigint
| Pasrbigint | Pbigintorder | Pbigintmin | Pbigintmax
(* string primitives *)
| Pstringlength | Pstringrefu | Pstringrefs | Pstringadd | Pstringcomp _
| Pstringorder | Pstringmin | Pstringmax
(* List primitives *)
| Pmakelist
(* dict primitives *)
| Pmakedict | Pdict_has
(* promise *)
| Pawait
(* etc *)
| Pval_from_option | Pval_from_option_not_nest | Pnull_to_opt
| Pnull_undefined_to_opt | Pis_null | Pis_not_none | Psome | Psome_not_nest
| Pis_undefined | Pis_null_undefined | Ptypeof | Pis_poly_var_block
| Pdebugger | Pinit_mod | Pupdate_mod | Pduprecord | Pmakearray | Parraylength
| Parrayrefu | Parraysetu | Parrayrefs | Parraysets | Pjs_fn_method | Phash
| Phash_mixstring | Phash_mixint | Phash_finalmix | Precord_rest _ ->
rhs = lhs
(* Reachable only via the optimizer's term-equality comparison, which the
test suite doesn't exercise for template primitives. *)
| Ptagged_template _ | Ptemplate _ -> ( ((rhs = lhs) [@coverage off]))
| Pcreate_extension a -> (
match rhs with
| Pcreate_extension b -> a = (b : string)
| _ -> false)
(* | Pcaml_obj_set_length -> rhs = Pcaml_obj_set_length *)
| Pfield (n0, info0) -> (
match rhs with
| Pfield (n1, info1) -> n0 = n1 && eq_field_dbg_info info0 info1
| _ -> false)
| Psetfield (i0, info0) -> (
match rhs with
| Psetfield (i1, info1) -> i0 = i1 && eq_set_field_dbg_info info0 info1
| _ -> false)
| Pmakeblock info0 -> (
match rhs with
| Pmakeblock info1 -> eq_tag_info info0 info1
| _ -> false)
| Pjs_call {prim_name; arg_types; ffi; _} -> (
match rhs with
| Pjs_call rhs ->
prim_name = rhs.prim_name && arg_types = rhs.arg_types && ffi = rhs.ffi
| _ -> false)
| Pimport src -> (
match rhs with
| Pimport src2 -> src = src2
| _ -> false)
| Pjs_object_create obj_create -> (
match rhs with
| Pjs_object_create obj_create1 -> obj_create = obj_create1
| _ -> false)
| Pobjcomp comparison -> (
match rhs with
| Pobjcomp comparison1 -> eq_comparison comparison comparison1
| _ -> false)
| Pintcomp comparison -> (
match rhs with
| Pintcomp comparison1 -> eq_comparison comparison comparison1
| _ -> false)
| Pfloatcomp comparison -> (
match rhs with
| Pfloatcomp comparison1 -> eq_comparison comparison comparison1
| _ -> false)
| Pbigintcomp comparison -> (
match rhs with
| Pbigintcomp comparison1 -> eq_comparison comparison comparison1
| _ -> false)
| Pjscomp comparison -> (
match rhs with
| Pjscomp comparison1 -> eq_comparison comparison comparison1
| _ -> false)
| Pjs_object_get name -> (
match rhs with
| Pjs_object_get rhs_name -> name = rhs_name
| _ -> false)
| Pjs_object_set name -> (
match rhs with
| Pjs_object_set rhs_name -> name = rhs_name
| _ -> false)
| Praw_js_code _ -> false
(* TOO lazy, here comparison is only approximation*)
(* The source-level name a field access carries, when it has one. *)
let str_of_field_info (x : field_dbg_info) : string option =
match x with
| Fld_extension | Fld_variant | Fld_cons | Fld_poly_var_tag
| Fld_poly_var_content | Fld_tuple ->
None
| Fld_record {name}
| Fld_module {name}
| Fld_record_inline {name}
| Fld_record_extension {name} ->
Some name
let is_immutable_block (info : tag_info) =
mutable_flag_of_tag_info info = Immutable
(* A constant that has to be built at run time rather than shared. *)
let rec const_is_allocating (c : structured_constant) : bool =
match c with
| Const_some t -> const_is_allocating t
| Const_block _ -> true
| Const_js_null | Const_js_undefined _ | Const_js_true | Const_js_false
| Const_int _ | Const_assertfalse | Const_constructor _ | Const_char _
| Const_string _ | Const_float _ | Const_bigint _ | Const_polyvar _
| Const_module_alias ->
false
let rec const_eq_approx (x : structured_constant) (y : structured_constant) =
match x with
| Const_module_alias -> y = Const_module_alias
| Const_js_null -> y = Const_js_null
| Const_js_undefined b -> y = Const_js_undefined b
| Const_js_true -> y = Const_js_true
| Const_js_false -> y = Const_js_false
| Const_int ix -> (
match y with
| Const_int iy -> ix = iy
| _ -> false)
| Const_assertfalse -> y = Const_assertfalse
| Const_constructor ix -> (
match y with
| Const_constructor iy -> ix = iy
| _ -> false)
| Const_char ix -> (
match y with
| Const_char iy -> ix = iy
| _ -> false)
| Const_string sx -> (
match y with
| Const_string sy -> sx = sy
| _ -> false)
| Const_float ix -> (
match y with
| Const_float iy -> ix = iy
| _ -> false)
| Const_bigint (sx, ix) -> (
match y with
| Const_bigint (sy, iy) -> sx = sy && ix = iy
| _ -> false)
| Const_polyvar ix -> (
match y with
| Const_polyvar iy -> ix = iy
| _ -> false)
| Const_block (ix, ixs) -> (
match y with
| Const_block (iy, iys) ->
ix = iy && Ext_list.for_all2_no_exn ixs iys const_eq_approx
| _ -> false)
| Const_some ix -> (
match y with
| Const_some iy -> const_eq_approx ix iy
| _ -> false)
let cmp_int32 (cmp : comparison) (a : int32) b : bool =
match cmp with
| Ceq -> a = b
| Cneq -> a <> b
| Cgt -> a > b
| Cle -> a <= b
| Clt -> a < b
| Cge -> a >= b
let cmp_float (cmp : comparison) (a : float) b : bool =
match cmp with
| Ceq -> a = b
| Cneq -> a <> b
| Cgt -> a > b
| Cle -> a <= b
| Clt -> a < b
| Cge -> a >= b
(* Constructors. The type is private outside this module, so every term is
built through one of these. They are plain for now; the normalizations that
Lambda.prim / Lambda.if_ / Lambda.switch perform will move here when the two layers
become one type. *)
let var id : t = Lvar id
let global_module id : t = Lglobal_module id
let const ct : t = Lconst ct
let function_ ~loc ~attr ~params ~body : t = Lfunction {params; body; attr; loc}
let let_ kind id e body : t = Llet (kind, id, e, body)
let letrec bindings body : t = Lletrec (bindings, body)
let staticraise i args : t = Lstaticraise (i, args)
let staticcatch body catch handler : t = Lstaticcatch (body, catch, handler)
let try_ body id handler : t = Ltrywith (body, id, handler)
let break : t = Lbreak
let continue : t = Lcontinue
let while_ cond body : t = Lwhile (cond, body)
let for_ id from_ to_ dir body : t = Lfor (id, from_, to_, dir, body)
let for_of id iterable body : t = Lfor_of (id, iterable, body)
let for_await_of id iterable body : t = Lfor_await_of (id, iterable, body)
let assign id body : t = Lassign (id, body)
exception Not_simple_form
(**
[is_eta_conversion_exn params inner_args outer_args]
case 1:
{{
(fun params -> wrap (primitive (inner_args)) args
}}
when [inner_args] are the same as [params], it can be simplified as
[wrap (primitive args)]
where [wrap] used to be simple instructions
Note that [external] functions are forced to do eta-conversion
when combined with [|>] operator, we need to make sure beta-reduction
is applied though since `[@variadic]` needs such guarantee.
Since `[@variadic] is the tail position
*)
let rec is_eta_conversion_exn params inner_args outer_args : t list =
match (params, inner_args, outer_args) with
| x :: xs, Lvar y :: ys, r :: rest when Ident.same x y ->
r :: is_eta_conversion_exn xs ys rest
| [], [], [] -> []
| _, _, _ -> raise_notrace Not_simple_form
let rec eq_approx (l1 : t) (l2 : t) =
match l1 with
| Lglobal_module i1 -> (
match l2 with
| Lglobal_module i2 -> Ident.same i1 i2
| _ -> false)
| Lvar i1 -> (
match l2 with
| Lvar i2 -> Ident.same i1 i2
| _ -> false)
| Lconst c1 -> (
match l2 with
| Lconst c2 -> const_eq_approx c1 c2
| _ -> false)
| Lapply app1 -> (
match l2 with
| Lapply app2 ->
eq_approx app1.ap_func app2.ap_func
&& eq_approx_list app1.ap_args app2.ap_args
| _ -> false)
| Lifthenelse (a, b, c) -> (
match l2 with
| Lifthenelse (a0, b0, c0) ->
eq_approx a a0 && eq_approx b b0 && eq_approx c c0
| _ -> false)
| Lsequence (a, b) -> (
match l2 with
| Lsequence (a0, b0) -> eq_approx a a0 && eq_approx b b0
| _ -> false)
| Lbreak -> l2 = Lbreak
| Lcontinue -> l2 = Lcontinue
| Lwhile (p, b) -> (
match l2 with
| Lwhile (p0, b0) -> eq_approx p p0 && eq_approx b b0
| _ -> false)
| Lassign (v0, l0) -> (
match l2 with
| Lassign (v1, l1) -> Ident.same v0 v1 && eq_approx l0 l1
| _ -> false)
| Lstaticraise (id, ls) -> (
match l2 with
| Lstaticraise (id1, ls1) -> id = id1 && eq_approx_list ls ls1
| _ -> false)
| Lprim info1 -> (
match l2 with
| Lprim info2 ->
eq_primitive_approx info1.primitive info2.primitive
&& eq_approx_list info1.args info2.args
| _ -> false)
| Lstringswitch (arg, patterns, default) -> (
match l2 with
| Lstringswitch (arg2, patterns2, default2) ->
eq_approx arg arg2 && eq_option default default2
&& Ext_list.for_all2_no_exn patterns patterns2
(fun ((k : string), v) (k2, v2) -> k = k2 && eq_approx v v2)
| _ -> false)
| Lfunction _
| Llet (_, _, _, _)
| Lletrec _ | Lswitch _ | Lstaticcatch _ | Ltrywith _
| Lfor (_, _, _, _, _)
| Lfor_of (_, _, _)
| Lfor_await_of (_, _, _) ->
false
and eq_option l1 l2 =
match l1 with
| None -> l2 = None
| Some l1 -> (
match l2 with
| Some l2 -> eq_approx l1 l2
| None -> false)
and eq_approx_list ls ls1 = Ext_list.for_all2_no_exn ls ls1 eq_approx
(* Classify constants by the JavaScript value emitted by [Lam_compile_const]
and [Js_dump], not by their source constructor. Unknown representations
must retain runtime dispatch. *)
type value_kind =
| Is_literal of Variant_runtime.literal_tag
| Is_object
| Is_array
| Unknown_value
let runtime_value_kind (c : structured_constant) =
match c with
| Const_string s -> Is_literal (String s)
| Const_int i -> Is_literal (Int (Int32.to_int i))
| Const_float f -> Is_literal (Float f)
| Const_js_true -> Is_literal (Bool true)
| Const_js_false -> Is_literal (Bool false)
| Const_js_null -> Is_literal Null
| Const_js_undefined _ -> Is_literal Undefined
| Const_polyvar name -> Is_literal (String name)
| Const_constructor {name = "[]"; literal = None} -> Is_literal (Int 0)
| Const_constructor {name; literal = None} -> Is_literal (String name)
| Const_constructor {literal = Some (BigInt _)} -> Unknown_value
| Const_constructor {literal = Some literal} -> Is_literal literal
| Const_block (Blk_tuple, _) -> Is_array
| Const_block (Blk_record {fields}, _) ->
if
Array.length fields <> 0
&& Ext_array.for_alli fields (fun i (name, _) -> string_of_int i = name)
then Is_array
else Is_object
| Const_block
( ( Blk_constructor _ | Blk_record_inlined _ | Blk_poly_var
| Blk_record_ext _ | Blk_module _ | Blk_module_export _ | Blk_extension
),
_ ) ->
Is_object
| Const_char _ | Const_bigint _ | Const_some _ | Const_module_alias
| Const_assertfalse ->
Unknown_value
(* Runtime equality, rather than equality of tags: [@as(1)] and a payload
[1.0] are the same JavaScript number. Bigint spellings are not compared;
both bigint payloads and bigint constructors are classified as unknown.
Integer tags are emitted through [Int32.of_int], just like [Const_int]. *)
let literal_denotes_same (a : Variant_runtime.literal_tag)
(b : Variant_runtime.literal_tag) =
match (a, b) with
| String x, String y -> x = y
| Int x, Int y -> Int32.of_int x = Int32.of_int y
| Float x, Float y -> float_of_string x = float_of_string y
| Int x, Float y | Float y, Int x ->
Int32.to_float (Int32.of_int x) = float_of_string y
| Bool x, Bool y -> x = y
| Null, Null | Undefined, Undefined -> true
| (String _ | Int _ | Float _ | Bool _ | BigInt _ | Null | Undefined), _ ->
false
(* Mirror [Dynamic_checks]: literals take precedence; the object case excludes
arrays when an array case exists. Int and float share one runtime type. *)
let value_has_block_type ~block_types (kind : value_kind)
(block_type : Variant_runtime.block_type) =
match (block_type, kind) with
| (IntType | FloatType), Is_literal (Int _ | Float _) -> true
| StringType, Is_literal (String _) -> true
| BooleanType, Is_literal (Bool _) -> true
| ObjectType, Is_object -> true
| InstanceType Array, Is_array -> true
| ObjectType, Is_array ->
not (List.mem (Variant_runtime.InstanceType Array) block_types)
| UnknownType, (Is_literal _ | Is_object | Is_array) -> true
| UnknownType, Unknown_value -> false
| ( ( IntType | FloatType | StringType | BooleanType | ObjectType | BigintType
| FunctionType | InstanceType _ ),
_ ) ->
false
let switch lam (lam_switch : lambda_switch) : t =
let action_or_switch = function
| Some action -> action
| None -> (
match lam_switch.sw_failaction with
| Some action -> action
| None -> Lswitch (lam, lam_switch))
in
(* An untagged variant is dispatched on the value, so a constant scrutinee is
decided here rather than by the constructor it was written with - which
has no runtime existence and may be shared with a literal constructor.
[`Undecided] means this layer cannot name the constant's runtime shape, so
the switch has to stay; it is not the same as "no case matches". *)
let untagged_action (facts : Variant_runtime.matching_facts) cst =
let find_in cases matches =
`Case
(Ext_list.find_opt cases (fun (key, action) ->
match key with
| Switch_constructor case when matches case -> Some action
| Switch_int _ | Switch_constructor _ -> None))
in
let literal_of_tag (tag : Variant_runtime.tag) =
match tag.literal with
| Some literal -> literal
| None -> Variant_runtime.String tag.name
in
let matches_block = value_has_block_type ~block_types:facts.block_types in
let kind = runtime_value_kind cst in
match kind with
| Unknown_value -> `Undecided
| Is_literal literal
when Ext_list.exists facts.literal_tags (literal_denotes_same literal) ->
(* The literal side wins, exactly as it does at runtime. *)
find_in lam_switch.sw_consts (function
| Constant tag -> literal_denotes_same literal (literal_of_tag tag)
| Block _ -> false)
| Is_literal _ | Is_object | Is_array ->
(* Not a declared literal, so the payload's runtime shape decides. *)
if Ext_list.exists facts.block_types (matches_block kind) then
find_in lam_switch.sw_blocks (function
| Block (Untagged {block_type}) -> matches_block kind block_type
| Constant _ | Block (Tagged _) -> false)
else `Undecided
in
match (lam, lam_switch.sw_dispatch) with
| Lconst cst, Switch_variant ({block_types = _ :: _} as facts) -> (
match untagged_action facts cst with
| `Case action -> action_or_switch action
| `Undecided -> Lswitch (lam, lam_switch))
| Lconst (Const_constructor cstr_name), _ ->
let action =
Ext_list.find_opt lam_switch.sw_consts (fun (key, action) ->
match key with
| Switch_constructor (Constant tag) when cstr_name = tag ->
Some action
| Switch_int _ | Switch_constructor _ -> None)
in
action_or_switch action
| Lconst (Const_int i), _ ->
(* Because of inlining and dead code, we might be looking at a value of unexpected type
e.g. an integer, so the const case might not be found *)
let i = Int32.to_int i in
let action =
Ext_list.find_opt lam_switch.sw_consts (fun (key, action) ->
match key with
| Switch_int ordinal when ordinal = i -> Some action
| Switch_constructor
(Constant {literal = Some (Variant_runtime.Int value)})
when value = i ->
Some action
| Switch_int _ | Switch_constructor _ -> None)
in
action_or_switch action
| Lconst (Const_block (tag_info, _)), _ ->
let runtime =
match tag_info with
| Blk_constructor {runtime} -> Some (Variant_runtime.Tagged runtime)
| Blk_record_inlined {runtime} -> Some runtime
| Blk_tuple | Blk_poly_var | Blk_record _ | Blk_record_ext _
| Blk_module _ | Blk_module_export _ | Blk_extension ->
None
in
let action =
Ext_list.find_opt lam_switch.sw_blocks (fun (key, action) ->
match key with
| Switch_constructor (Block case_runtime)
when runtime = Some case_runtime ->
Some action
| Switch_int _ | Switch_constructor _ -> None)
in
action_or_switch action
| _ -> Lswitch (lam, lam_switch)
let stringswitch (lam : t) cases default : t =
match lam with
| Lconst (Const_string s) -> Ext_list.assoc_by_string cases s default
| _ -> Lstringswitch (lam, cases, default)
let rec seq (a : t) b : t =
match a with
| Lprim {primitive = Pmakeblock _; args = x :: xs} ->
seq (Ext_list.fold_left xs x seq) b
| Lprim {primitive = Pnull_to_opt | Pnull_undefined_to_opt; args = [a]} ->
seq a b
| _ -> Lsequence (a, b)
module Lift = struct
let int i : t = Lconst (Const_int i)
let bool b = if b then lambda_true else lambda_false
let string s : t = Lconst (Const_string s)
let char b : t = Lconst (Const_char b)
end
let prim ~primitive:(prim : primitive) ~args loc : t =
let default () : t = Lprim {primitive = prim; args; loc} in
match args with
| [Lconst a] -> (
match (prim, a) with
| Pnegint, Const_int i -> Lift.int (Int32.neg i)
(* | Pfloatofint, ( (Const_int a)) *)
(* -> Lift.float (float_of_int a) *)
| Pintoffloat, Const_float a ->
Lift.int (Int32.of_float (float_of_string a))
(* | Pnegfloat -> Lift.float (-. a) *)
| Pstringlength, Const_string s ->
Lift.int (Int32.of_int (String_literal.utf16_length s))
(* | Pnegbint Pnativeint, ( (Const_nativeint i)) *)
(* -> *)
(* Lift.nativeint (Nativeint.neg i) *)
| Pnot, Const_js_true -> lambda_false
| Pnot, Const_js_false -> lambda_true
| _ -> default ())
| [Lconst a; Lconst b] -> (
match (prim, a, b) with
| Pintcomp cmp, Const_int a, Const_int b -> Lift.bool (cmp_int32 cmp a b)
| Pfloatcomp cmp, Const_float a, Const_float b ->
(* FIXME: could raise? *)
Lift.bool (cmp_float cmp (float_of_string a) (float_of_string b))
| Pbigintcomp _, Const_bigint _, Const_bigint _ -> default ()
| Pintcomp ((Ceq | Cneq) as op), Const_polyvar a, Const_polyvar b ->
Lift.bool
(match op with
| Ceq -> a = (b : string)
| Cneq -> a <> b
| _ -> assert false)
| ( Pintcomp ((Ceq | Cneq) as op),