forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlam.ml
1862 lines (1728 loc) · 61.7 KB
/
lam.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
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
(* 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. *)
type array_kind = Lambda.array_kind
type boxed_integer = Lambda.boxed_integer
type comparison = Lambda.comparison
type bigarray_kind = Lambda.bigarray_kind
type bigarray_layout = Lambda.bigarray_layout
type compile_time_constant = Lambda.compile_time_constant
type tag_info = Lambda.tag_info
type mutable_flag = Asttypes.mutable_flag
type field_dbg_info = Lambda.field_dbg_info
type set_field_dbg_info = Lambda.set_field_dbg_info
type ident = Ident.t
type function_arities =
| Determin of bool * (int * Ident.t list option) list * bool
| NA
type constant =
| Const_int of int
| Const_char of char
| Const_string of string (* use record later *)
| Const_unicode of string
| Const_float of string
| Const_int32 of int32
| Const_int64 of int64
| Const_nativeint of nativeint
| Const_pointer of int * Lambda.pointer_info
| Const_block of int * Lambda.tag_info * constant list
| Const_float_array of string list
| Const_immstring of string
type primitive =
| Pbytes_to_string
| Pbytes_of_string
| Pglobal_exception of ident
(* Operations on heap blocks *)
| Pmakeblock of int * tag_info * mutable_flag
| Pfield of int * field_dbg_info
| Psetfield of int * bool * set_field_dbg_info
(* could have field info at least for record *)
| Pfloatfield of int * field_dbg_info
| Psetfloatfield of int * set_field_dbg_info
| Pduprecord of Types.record_representation * int
(* Force lazy values *)
| Plazyforce
(* External call *)
| Pccall of Primitive.description
| Pjs_call of
(* Location.t * [loc] is passed down *)
string * (* prim_name *)
Ast_ffi_types.arg_kind list * (* arg_types *)
Ast_ffi_types.ffi (* ffi *)
(* Ast_ffi_types.arg_kind list * bool * Ast_ffi_types.ffi *)
| Pjs_object_create of Ast_ffi_types.obj_create
(* Exceptions *)
| Praise
(* Boolean operations *)
| Psequand | Psequor | Pnot
(* Integer operations *)
| Pnegint | Paddint | Psubint | Pmulint | Pdivint | Pmodint
| Pandint | Porint | Pxorint
| Plslint | Plsrint | Pasrint
| Pintcomp of comparison
| Poffsetint of int
| Poffsetref of int
(* Float operations *)
| Pintoffloat | Pfloatofint
| Pnegfloat | Pabsfloat
| Paddfloat | Psubfloat | Pmulfloat | Pdivfloat
| Pfloatcomp of comparison
| Pjscomp of comparison
| Pjs_apply (*[f;arg0;arg1; arg2; ... argN]*)
| Pjs_runtime_apply (* [f; [...]] *)
(* String operations *)
| Pstringlength
| Pstringrefu
| Pstringrefs
| Pstringadd
| Pbyteslength
| Pbytesrefu
| Pbytessetu
| Pbytesrefs
| Pbytessets
(* Array operations *)
| Pmakearray of array_kind
| Parraylength of array_kind
| Parrayrefu of array_kind
| Parraysetu of array_kind
| Parrayrefs of array_kind
| Parraysets of array_kind
(* Test if the argument is a block or an immediate integer *)
| Pisint
(* Test if the (integer) argument is outside an interval *)
| Pisout
(* Bitvect operations *)
| Pbittest
(* Operations on boxed integers (Nativeint.t, Int32.t, Int64.t) *)
| Pbintofint of boxed_integer
| Pintofbint of boxed_integer
| Pcvtbint of boxed_integer (*source*) * boxed_integer (*destination*)
| Pnegbint of boxed_integer
| Paddbint of boxed_integer
| Psubbint of boxed_integer
| Pmulbint of boxed_integer
| Pdivbint of boxed_integer
| Pmodbint of boxed_integer
| Pandbint of boxed_integer
| Porbint of boxed_integer
| Pxorbint of boxed_integer
| Plslbint of boxed_integer
| Plsrbint of boxed_integer
| Pasrbint of boxed_integer
| Pbintcomp of boxed_integer * comparison
(* Operations on big arrays: (unsafe, #dimensions, kind, layout) *)
| Pbigarrayref of bool * int * bigarray_kind * bigarray_layout
| Pbigarrayset of bool * int * bigarray_kind * bigarray_layout
(* size of the nth dimension of a big array *)
| Pbigarraydim of int
(* load/set 16,32,64 bits from a string: (unsafe)*)
| Pstring_load_16 of bool
| Pstring_load_32 of bool
| Pstring_load_64 of bool
| Pstring_set_16 of bool
| Pstring_set_32 of bool
| Pstring_set_64 of bool
(* load/set 16,32,64 bits from a
(char, int8_unsigned_elt, c_layout) Bigarray.Array1.t : (unsafe) *)
| Pbigstring_load_16 of bool
| Pbigstring_load_32 of bool
| Pbigstring_load_64 of bool
| Pbigstring_set_16 of bool
| Pbigstring_set_32 of bool
| Pbigstring_set_64 of bool
(* Compile time constants *)
| Pctconst of compile_time_constant
(* byte swap *)
| Pbswap16
| Pbbswap of boxed_integer
(* Integer to external pointer *)
| Pdebugger
| Pjs_unsafe_downgrade of string * Location.t
| Pinit_mod
| Pupdate_mod
| Praw_js_code_exp of string
| Praw_js_code_stmt of string
| Pjs_fn_make of int
| Pjs_fn_run of int
| Pjs_fn_method of int
| Pjs_fn_runmethod of int
| Pundefined_to_opt
| Pnull_to_opt
| Pnull_undefined_to_opt
| Pis_null
| Pis_undefined
| Pis_null_undefined
| Pjs_boolean_to_bool
| Pjs_typeof
| Pjs_function_length
| Pjs_string_of_small_array
| Pjs_is_instance_array
| Pcaml_obj_length
| Pcaml_obj_set_length
type apply_status =
| App_na
| App_ml_full
| App_js_full
module Types = struct
type switch =
{ sw_numconsts: int;
sw_consts: (int * t) list;
sw_numblocks: int;
sw_blocks: (int * t) list;
sw_failaction : t option}
and prim_info =
{ primitive : primitive ;
args : t list ;
loc : Location.t;
}
and apply_info =
{ fn : t ;
args : t list ;
loc : Location.t;
status : apply_status
}
and function_info =
{ arity : int ;
kind : Lambda.function_kind ;
params : ident list ;
body : t
}
and t =
| Lvar of ident
| Lglobal_module of ident
| Lconst of constant
| Lapply of apply_info
| Lfunction of function_info
| Llet of Lambda.let_kind * ident * t * t
| Lletrec of (ident * t) list * t
| Lprim of prim_info
| Lswitch of t * switch
| Lstringswitch of t * (string * t) list * t option
| Lstaticraise of int * t list
| Lstaticcatch of t * (int * ident list) * t
| Ltrywith of t * ident * t
| Lifthenelse of t * t * t
| Lsequence of t * t
| Lwhile of t * t
| Lfor of ident * t * t * Asttypes.direction_flag * t
| Lassign of ident * t
| Lsend of Lambda.meth_kind * t * t * t list * Location.t
| Lifused of ident * t
(* | Levent of t * Lambda.lambda_event
[Levent] in the branch hurt pattern match,
we should use record for trivial debugger info
*)
end
module X = struct
type switch
= Types.switch
=
{ sw_numconsts: int;
sw_consts: (int * t) list;
sw_numblocks: int;
sw_blocks: (int * t) list;
sw_failaction : t option}
and prim_info
= Types.prim_info
=
{ primitive : primitive ;
args : t list ;
loc : Location.t;
}
and apply_info
= Types.apply_info
=
{ fn : t ;
args : t list ;
loc : Location.t;
status : apply_status
}
and function_info
= Types.function_info
=
{ arity : int ;
kind : Lambda.function_kind ;
params : ident list ;
body : t
}
and t
= Types.t
=
| Lvar of ident
| Lglobal_module of ident
| Lconst of constant
| Lapply of apply_info
| Lfunction of function_info
| Llet of Lambda.let_kind * ident * t * t
| Lletrec of (ident * t) list * t
| Lprim of prim_info
| Lswitch of t * switch
| Lstringswitch of t * (string * t) list * t option
| Lstaticraise of int * t list
| Lstaticcatch of t * (int * ident list) * t
| Ltrywith of t * ident * t
| Lifthenelse of t * t * t
| Lsequence of t * t
| Lwhile of t * t
| Lfor of ident * t * t * Asttypes.direction_flag * t
| Lassign of ident * t
| Lsend of Lambda.meth_kind * t * t * t list * Location.t
| Lifused of ident * t
end
include Types
(** apply [f] to direct successor which has type [Lam.t] *)
let inner_map (f : t -> X.t ) (l : t) : X.t =
match l with
| Lvar (_ : ident)
| Lconst (_ : constant) ->
( (* Obj.magic *) l : X.t)
| Lapply ({fn; args; loc; status} ) ->
let fn = f fn in
let args = List.map f args in
Lapply { fn ; args; loc; status }
| Lfunction({body; arity; kind; params } ) ->
let body = f body in
Lfunction {body; arity; kind ; params}
| Llet(str, id, arg, body) ->
let arg = f arg in let body = f body in
Llet(str,id,arg,body)
| Lletrec(decl, body) ->
let body = f body in
let decl = List.map (fun (id, exp) -> id, f exp) decl in
Lletrec(decl,body)
| Lglobal_module _ -> (l : X.t)
| Lprim {args; primitive ; loc} ->
let args = List.map f args in
Lprim { args; primitive; loc}
| Lswitch(arg, {sw_consts; sw_numconsts; sw_blocks; sw_numblocks; sw_failaction}) ->
let arg = f arg in
let sw_consts = List.map (fun (key, case) -> key , f case) sw_consts in
let sw_blocks = List.map (fun (key, case) -> key, f case) sw_blocks in
let sw_failaction = begin match sw_failaction with
| None -> None
| Some a -> Some (f a)
end in
Lswitch(arg, { sw_consts; sw_blocks; sw_failaction; sw_numblocks; sw_numconsts})
| Lstringswitch (arg,cases,default) ->
let arg = f arg in
let cases = List.map (fun (k,act) -> k,f act) cases in
let default = begin match default with
| None -> None
| Some a -> Some (f a)
end in
Lstringswitch(arg,cases,default)
| Lstaticraise (id,args) ->
let args = List.map f args in
Lstaticraise(id,args)
| Lstaticcatch(e1, vars , e2) ->
let e1 = f e1 in
let e2 = f e2 in
Lstaticcatch(e1, vars, e2)
| Ltrywith(e1, exn, e2) ->
let e1 = f e1 in
let e2 = f e2 in
Ltrywith(e1,exn,e2)
| Lifthenelse(e1, e2, e3) ->
let e1 = f e1 in let e2 = f e2 in let e3 = f e3 in
Lifthenelse(e1,e2,e3)
| Lsequence(e1, e2) ->
let e1 = f e1 in let e2 = f e2 in
Lsequence(e1,e2)
| Lwhile(e1, e2) ->
let e1 = f e1 in let e2 = f e2 in
Lwhile(e1,e2)
| Lfor(v, e1, e2, dir, e3) ->
let e1 = f e1 in let e2 = f e2 in let e3 = f e3 in
Lfor(v,e1,e2,dir,e3)
| Lassign(id, e) ->
let e = f e in
Lassign(id,e)
| Lsend (k, met, obj, args, loc) ->
let met = f met in
let obj = f obj in
let args = List.map f args in
Lsend(k,met,obj,args,loc)
| Lifused (v, e) ->
let e = f e in
Lifused(v,e)
let inner_iter (f : t -> unit ) (l : t) : unit =
match l with
| Lvar (_ : ident)
| Lconst (_ : constant) -> ()
| Lapply ({fn; args; loc; status} ) ->
f fn;
List.iter f args
| Lfunction({body; arity; kind; params } ) ->
f body
| Llet(str, id, arg, body) ->
f arg ;
f body;
| Lletrec(decl, body) ->
f body;
List.iter (fun (id, exp) -> f exp) decl
| Lglobal_module (_ )
-> ()
| Lprim {args; primitive ; loc} ->
List.iter f args;
| Lswitch(arg, {sw_consts; sw_numconsts; sw_blocks; sw_numblocks; sw_failaction}) ->
f arg;
List.iter (fun (key, case) -> f case) sw_consts;
List.iter (fun (key, case) -> f case) sw_blocks ;
begin match sw_failaction with
| None -> ()
| Some a -> f a
end
| Lstringswitch (arg,cases,default) ->
f arg;
List.iter (fun (k,act) -> f act) cases ;
begin match default with
| None -> ()
| Some a -> f a
end
| Lstaticraise (id,args) ->
List.iter f args;
| Lstaticcatch(e1, vars , e2) ->
f e1;
f e2
| Ltrywith(e1, exn, e2) ->
f e1;
f e2
| Lifthenelse(e1, e2, e3) ->
f e1; f e2 ; f e3
| Lsequence(e1, e2) ->
f e1 ; f e2
| Lwhile(e1, e2) ->
f e1 ; f e2
| Lfor(v, e1, e2, dir, e3) ->
f e1 ; f e2; f e3
| Lassign(id, e) ->
f e
| Lsend (k, met, obj, args, loc) ->
f met; f obj; List.iter f args
| Lifused (v, e) ->
f e
(*
let add_list lst set =
List.fold_left (fun acc x -> Ident_set.add x acc) set lst
let free_variables l =
let rec free bounded acc (l : t) =
match (l : t) with
| Lvar id ->
if Ident_set.mem id bounded then acc
else Ident_set.add id acc
| Lconst _ -> acc
| Lapply{fn; args; _} ->
let acc = free bounded acc fn in
List.fold_left (fun acc arg -> free bounded acc arg) acc args
| Lfunction{body;params} ->
let bounded = add_list params bounded in
free bounded acc body
| Llet(str, id, arg, body) ->
let acc = free bounded acc arg in
let bounded = Ident_set.add id bounded in
free bounded acc body
| Lletrec(decl, body) ->
let bounded =
List.fold_left (fun acc (x,_) -> Ident_set.add x acc) bounded decl
in
let acc = List.fold_left (fun acc (_,exp) -> free bounded acc exp ) acc decl in
free bounded acc body
| Lprim {args; _} ->
List.fold_left (fun acc arg -> free bounded acc arg) acc args
| Lswitch(arg, {sw_consts; sw_blocks; sw_failaction}) ->
let acc = free bounded acc arg in
let acc = List.fold_left
(fun acc (key, case) -> free bounded acc case) acc sw_consts in
let acc =
List.fold_left
(fun acc (key, case) -> free bounded acc case) acc sw_blocks in
begin match sw_failaction with
| None -> acc
| Some a -> free bounded acc a
end
| Lstringswitch (arg,cases,default) ->
let acc = free bounded acc arg in
let acc = List.fold_left (fun acc (_,act) -> free bounded acc act) acc cases in
begin match default with
| None -> acc
| Some a -> free bounded acc a
end
| Lstaticraise (_,args) ->
List.fold_left (fun acc arg -> free bounded acc arg) acc args
| Lstaticcatch(e1, (_,vars), e2) ->
let acc = free bounded acc e1 in
let bounded = add_list vars bounded in
free bounded acc e2
| Ltrywith(e1, exn, e2) ->
let acc = free bounded acc e1 in
let bounded = Ident_set.add exn bounded in
free bounded acc e2
| Lifthenelse(e1, e2, e3) ->
let acc = free bounded acc e1 in
let acc = free bounded acc e2 in
free bounded acc e3
| Lwhile(e1, e2)
| Lsequence(e1, e2) ->
let acc = free bounded acc e1 in
free bounded acc e2
| Lfor(v, e1, e2, dir, e3) ->
let acc = free bounded acc e1 in
let acc = free bounded acc e2 in
let bounded = Ident_set.add v bounded in
free bounded acc e3
| Lassign(id, e) ->
let acc = free bounded acc e in
if Ident_set.mem id bounded then acc
else Ident_set.add id acc
| Lsend (k, met, obj, args, _) ->
let acc = free bounded acc met in
let acc = free bounded acc obj in
List.fold_left (fun ac arg -> free bounded acc arg) acc args
| Lifused (v, e) ->
free bounded acc e
in free Ident_set.empty Ident_set.empty l
*)
let hit_any_variables (fv : Ident_set.t) l : bool =
let rec hit (l : t) =
begin
match (l : t) with
| Lvar id -> Ident_set.mem id fv
| Lassign(id, e) ->
Ident_set.mem id fv || hit e
| Lstaticcatch(e1, (_,vars), e2) ->
hit e1 || hit e2
| Ltrywith(e1, exn, e2) ->
hit e1 || hit e2
| Lfunction{body;params} ->
hit body;
| Llet(str, id, arg, body) ->
hit arg || hit body
| Lletrec(decl, body) ->
hit body ||
List.exists (fun (id, exp) -> hit exp) decl
| Lfor(v, e1, e2, dir, e3) ->
hit e1 || hit e2 || hit e3
| Lconst _ -> false
| Lapply{fn; args; _} ->
hit fn || List.exists hit args
| Lglobal_module _ (* global persistent module, play safe *)
-> false
| Lprim {args; _} ->
List.exists hit args
| Lswitch(arg, sw) ->
hit arg ||
List.exists (fun (key, case) -> hit case) sw.sw_consts ||
List.exists (fun (key, case) -> hit case) sw.sw_blocks ||
begin match sw.sw_failaction with
| None -> false
| Some a -> hit a
end
| Lstringswitch (arg,cases,default) ->
hit arg ||
List.exists (fun (_,act) -> hit act) cases ||
begin match default with
| None -> false
| Some a -> hit a
end
| Lstaticraise (_,args) ->
List.exists hit args
| Lifthenelse(e1, e2, e3) ->
hit e1 || hit e2 || hit e3
| Lsequence(e1, e2) ->
hit e1 || hit e2
| Lwhile(e1, e2) ->
hit e1 || hit e2
| Lsend (k, met, obj, args, _) ->
hit met || hit obj || List.exists hit args
| Lifused (v, e) ->
hit e
end;
in hit l
let hit_mask (fv : Hash_set_ident_mask.t) l =
let rec hit (l : t) =
begin
match (l : t) with
| Lvar id -> Hash_set_ident_mask.mask_check_all_hit id fv
| Lassign(id, e) ->
Hash_set_ident_mask.mask_check_all_hit id fv || hit e
| Lstaticcatch(e1, (_,vars), e2) ->
hit e1 || hit e2
| Ltrywith(e1, exn, e2) ->
hit e1 || hit e2
| Lfunction{body;params} ->
hit body;
| Llet(str, id, arg, body) ->
hit arg || hit body
| Lletrec(decl, body) ->
hit body ||
List.exists (fun (id, exp) -> hit exp) decl
| Lfor(v, e1, e2, dir, e3) ->
hit e1 || hit e2 || hit e3
| Lconst _ -> false
| Lapply{fn; args; _} ->
hit fn || List.exists hit args
| Lglobal_module id (* playsafe *)
-> false
| Lprim {args; _} ->
List.exists hit args
| Lswitch(arg, sw) ->
hit arg ||
List.exists (fun (key, case) -> hit case) sw.sw_consts ||
List.exists (fun (key, case) -> hit case) sw.sw_blocks ||
begin match sw.sw_failaction with
| None -> false
| Some a -> hit a
end
| Lstringswitch (arg,cases,default) ->
hit arg ||
List.exists (fun (_,act) -> hit act) cases ||
begin match default with
| None -> false
| Some a -> hit a
end
| Lstaticraise (_,args) ->
List.exists hit args
| Lifthenelse(e1, e2, e3) ->
hit e1 || hit e2 || hit e3
| Lsequence(e1, e2) ->
hit e1 || hit e2
| Lwhile(e1, e2) ->
hit e1 || hit e2
| Lsend (k, met, obj, args, _) ->
hit met || hit obj || List.exists hit args
| Lifused (v, e) ->
hit e
end;
in hit l
let free_variables l =
let fv = ref Ident_set.empty in
let rec free (l : t) =
begin
match (l : t) with
| Lvar id -> fv := Ident_set.add id !fv
| Lassign(id, e) ->
free e;
fv := Ident_set.add id !fv
| Lstaticcatch(e1, (_,vars), e2) ->
free e1; free e2;
List.iter (fun id -> fv := Ident_set.remove id !fv) vars
| Ltrywith(e1, exn, e2) ->
free e1; free e2;
fv := Ident_set.remove exn !fv
| Lfunction{body;params} ->
free body;
List.iter (fun param -> fv := Ident_set.remove param !fv) params
| Llet(str, id, arg, body) ->
free arg; free body;
fv := Ident_set.remove id !fv
| Lletrec(decl, body) ->
free body;
List.iter (fun (id, exp) -> free exp) decl;
List.iter (fun (id, exp) -> fv := Ident_set.remove id !fv) decl
| Lfor(v, e1, e2, dir, e3) ->
free e1; free e2; free e3;
fv := Ident_set.remove v !fv
| Lconst _ -> ()
| Lapply{fn; args; _} ->
free fn; List.iter free args
| Lglobal_module _ -> ()
(* according to the existing semantics:
[primitive] is not counted
*)
| Lprim {args; _} ->
List.iter free args
| Lswitch(arg, sw) ->
free arg;
List.iter (fun (key, case) -> free case) sw.sw_consts;
List.iter (fun (key, case) -> free case) sw.sw_blocks;
begin match sw.sw_failaction with
| None -> ()
| Some a -> free a
end
| Lstringswitch (arg,cases,default) ->
free arg ;
List.iter (fun (_,act) -> free act) cases ;
begin match default with
| None -> ()
| Some a -> free a
end
| Lstaticraise (_,args) ->
List.iter free args
| Lifthenelse(e1, e2, e3) ->
free e1; free e2; free e3
| Lsequence(e1, e2) ->
free e1; free e2
| Lwhile(e1, e2) ->
free e1; free e2
| Lsend (k, met, obj, args, _) ->
free met; free obj; List.iter free args
| Lifused (v, e) ->
free e
end;
in free l;
!fv
(**
checks
1. variables are not bound twice
2. all variables are of right scope
*)
let check file lam =
let defined_variables = Ident_hash_set.create 1000 in
let success = ref true in
let use (id : Ident.t) =
if not @@ Ident_hash_set.mem defined_variables id then
begin
Format.fprintf Format.err_formatter "\n[SANITY]:%s/%d used before defined in %s\n" id.name id.stamp file ;
success := false
end
in
let def (id : Ident.t) =
if Ident_hash_set.mem defined_variables id then
begin
Format.fprintf Format.err_formatter "\n[SANITY]:%s/%d bound twice in %s\n" id.name id.stamp file ;
success := false
end
else Ident_hash_set.add defined_variables id
in
let rec iter (l : t) =
begin
match (l : t) with
| Lvar id -> use id
| Lglobal_module _ -> ()
| Lprim {args; _} ->
List.iter iter args
| Lconst _ -> ()
| Lapply{fn; args; _} ->
iter fn; List.iter iter args
| Lfunction{body;params} ->
List.iter def params;
iter body
| Llet(str, id, arg, body) ->
iter arg;
def id;
iter body
| Lletrec(decl, body) ->
List.iter (fun (id, exp) -> def id) decl;
List.iter (fun (id, exp) -> iter exp) decl;
iter body
| Lswitch(arg, sw) ->
iter arg;
List.iter (fun (key, case) -> iter case) sw.sw_consts;
List.iter (fun (key, case) -> iter case) sw.sw_blocks;
begin match sw.sw_failaction with
| None -> ()
| Some a -> iter a
end
| Lstringswitch (arg,cases,default) ->
iter arg ;
List.iter (fun (_,act) -> iter act) cases ;
begin match default with
| None -> ()
| Some a -> iter a
end
| Lstaticraise (_,args) ->
List.iter iter args
| Lstaticcatch(e1, (_,vars), e2) ->
iter e1;
List.iter def vars;
iter e2
| Ltrywith(e1, exn, e2) ->
iter e1;
def exn;
iter e2
| Lifthenelse(e1, e2, e3) ->
iter e1; iter e2; iter e3
| Lsequence(e1, e2) ->
iter e1; iter e2
| Lwhile(e1, e2) ->
iter e1; iter e2
| Lfor(v, e1, e2, dir, e3) ->
iter e1; iter e2;
def v;
iter e3;
| Lassign(id, e) ->
use id ;
iter e
| Lsend (k, met, obj, args, _) ->
iter met; iter obj;
List.iter iter args
| Lifused (v, e) ->
iter e
end;
in
begin
iter lam;
assert (!success) ;
lam
end
type binop = t -> t -> t
type triop = t -> t -> t -> t
type unop = t -> t
exception Not_simple_form
(** Simplfiy such behavior
{[
(apply
(function prim/1024 prim/1023 prim/1022
([js] (js_fn_make_2 prim/1024) prim/1023 prim/1022)) .. )
]}
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 `[@bs.splice]` needs such guarantee.
Since `[@bs.splice] 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
| x::xs,
(Lprim ({primitive = Pjs_fn_make _;
args = [Lvar y] } as p ) ::ys),
r :: rest when Ident.same x y ->
Lprim ({p with args = [ r]}) ::
is_eta_conversion_exn xs ys rest
| [], [], [] -> []
| _, _, _ -> raise_notrace Not_simple_form
let var id : t = Lvar id
let global_module id = Lglobal_module id
let const ct : t = Lconst ct
(** FIXME: more robust inlining check later, we should inline it before we add stub code
*)
let apply fn args loc status : t =
match fn with
| Lfunction {kind ; params ;
body = Lprim {primitive =
(Pundefined_to_opt | Pnull_to_opt | Pnull_undefined_to_opt | Pis_null | Pis_null_undefined | Pjs_boolean_to_bool | Pjs_typeof ) as wrap;
args = [Lprim ({primitive; args = inner_args} as primitive_call)]
}
} ->
begin match is_eta_conversion_exn params inner_args args with
| args
->
Lprim {primitive = wrap ; args = [Lprim { primitive_call with args ; loc = loc }] ; loc }
| exception _ ->
Lapply { fn; args; loc; status }
end
| Lfunction {kind ; params;
body = Lsequence (Lprim ({primitive; args = inner_args}as primitive_call), (Lconst _ as const )) }
->
begin match is_eta_conversion_exn params inner_args args with
| args
->
Lsequence(Lprim { primitive_call with args ; loc = loc }, const)
| exception _ ->
Lapply { fn; args; loc; status }
end
| Lfunction {kind ; params;
body =Lprim ({primitive; args = inner_args}as primitive_call) }
->
begin match is_eta_conversion_exn params inner_args args with
| args
->
Lprim { primitive_call with args ; loc = loc }
| exception _ ->
Lapply { fn; args; loc; status }
end
(* | Lfunction {kind; params ;
body = Lapply {fn = new_fn ; args = inner_args; status }
} when is_eta_conversion params inner_args args ->
Lapply {fn = new_fn ; args ; loc = loc; status }
*)
(* same as previous App status*)
| _ ->
Lapply { fn; args; loc ;
status }
let function_ ~arity ~kind ~params ~body : t =
Lfunction { arity; kind; params ; body}
let let_ kind id e body : t
= Llet (kind,id,e,body)
let letrec bindings body : t =
Lletrec(bindings,body)
let if_ (a : t) (b : t) c =
match a with
| Lconst v ->
begin match v with
| Const_pointer (x, _) | (Const_int x)
->
if x <> 0 then b else c
| (Const_char x) ->
if Char.code x <> 0 then b else c
| (Const_int32 x) ->
if x <> 0l then b else c
| (Const_int64 x) ->
if x <> 0L then b else c
| (Const_nativeint x) ->
if x <> 0n then b else c
| Const_string _
| Const_float _
| Const_unicode _
| Const_block _
| Const_float_array _
| Const_immstring _ -> b
end
| _ -> Lifthenelse (a,b,c)
let switch lam (lam_switch : switch) : t =
match lam with
| Lconst ((Const_pointer (i,_) | (Const_int i)))
->
Ext_list.assoc_by_int lam_switch.sw_failaction i lam_switch.sw_consts
| Lconst (Const_block (i,_,_)) ->
Ext_list.assoc_by_int lam_switch.sw_failaction i lam_switch.sw_blocks
| _ ->
Lswitch(lam,lam_switch)
let stringswitch (lam : t) cases default : t =
match lam with
| Lconst (Const_string a) ->
Ext_list.assoc_by_string default a cases
| _ -> Lstringswitch(lam, cases, default)
let true_ : t =
Lconst (Const_pointer ( 1, Pt_constructor "true"))
let false_ : t =
Lconst (Const_pointer( 0, Pt_constructor "false"))
let unit : t =
Lconst (Const_pointer( 0, Pt_constructor "()"))
let assert_false_unit : t =
Lconst (Const_pointer( 0, Pt_constructor "impossible branch"))
(** [l || r ] *)
let sequor l r = if_ l true_ r
(** [l && r ] *)
let sequand l r = if_ l r false_
let seq a b : t =
Lsequence (a, b)
let append_unit a =
Lsequence (a,unit)
let while_ a b : t =
Lwhile(a,b)
let try_ body id handler : t =
Ltrywith(body,id,handler)
let for_ v e1 e2 dir e3 : t =
Lfor(v,e1,e2,dir,e3)
let event l (_event : Lambda.lambda_event) = l
let ifused v l : t =
Lifused (v,l)
let assign v l : t = Lassign(v,l)
let send u m o ll v : t =
Lsend(u, m, o, ll, v)
let staticcatch a b c : t = Lstaticcatch(a,b,c)
let staticraise a b : t = Lstaticraise(a,b)
let comparison (cmp : Lambda.comparison) a b : bool =
match cmp with
| Ceq -> a = b
| Cneq -> a <> b
| Cgt -> a > b
| Cle -> a <= b
| Clt -> a < b