forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs_exp_make.ml
1423 lines (1197 loc) · 43.3 KB
/
js_exp_make.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. *)
let no_side_effect = Js_analyzer.no_side_effect_expression
type t = J.expression
(*
[remove_pure_sub_exp x]
Remove pure part of the expression (minor optimization)
and keep the non-pure part while preserve the semantics
(modulo return value)
It will return None if [x] is pure
*)
let rec remove_pure_sub_exp (x : t) : t option =
match x.expression_desc with
| Var _
| Str _
| Number _ -> None (* Can be refined later *)
| Array_index (a,b) ->
if is_pure_sub_exp a && is_pure_sub_exp b then None
else Some x
| Array (xs,_mutable_flag) ->
if Ext_list.for_all xs is_pure_sub_exp then None
else Some x
| Seq (a,b) ->
(match remove_pure_sub_exp a , remove_pure_sub_exp b with
| None, None -> None
| Some u, Some v ->
Some { x with expression_desc = Seq(u,v)}
(* may still have some simplification*)
| None, (Some _ as v) -> v
| (Some _ as u), None -> u)
| _ -> Some x
and is_pure_sub_exp (x : t ) = remove_pure_sub_exp x = None
(* let mk ?comment exp : t =
{expression_desc = exp ; comment } *)
let var ?comment id : t =
{expression_desc = Var (Id id); comment }
(* only used in property access,
Invariant: it should not call an external module .. *)
let js_global ?comment (v : string) =
var ?comment (Ext_ident.create_js v )
let undefined : t =
{expression_desc = Undefined ; comment = None}
let nil : t =
{expression_desc = Null ; comment = None}
let call ?comment ~info e0 args : t =
{expression_desc = Call(e0,args,info); comment }
(* TODO: optimization when es is known at compile time
to be an array
*)
let flat_call ?comment e0 es : t =
{expression_desc = FlatCall (e0,es); comment }
let runtime_var_dot ?comment (x : string) (e1 : string) : J.expression =
{ expression_desc =
Var (Qualified({id = Ident.create_persistent x; kind = Runtime}, Some e1));
comment }
let ml_var_dot ?comment ( id : Ident.t) e : J.expression =
{expression_desc = Var (Qualified({id; kind = Ml}, Some e)); comment }
(**
module as a value
{[
var http = require("http")
]}
*)
let external_var_field ?comment ~external_name:name (id : Ident.t) ~field ~default : t =
{expression_desc = Var (Qualified({id; kind = External {name; default}}, Some field)); comment }
let external_var ?comment ~external_name (id : Ident.t) : t =
{expression_desc =
Var (Qualified({id; kind = External {name=external_name; default = false}}, None)); comment }
let ml_module_as_var ?comment (id : Ident.t) : t =
{expression_desc = Var (Qualified ({id; kind = Ml}, None)); comment}
(* Static_index .....................**)
let runtime_call ?comment module_name fn_name args =
call ?comment
~info:Js_call_info.builtin_runtime_call
(runtime_var_dot module_name fn_name) args
let runtime_ref module_name fn_name =
runtime_var_dot module_name fn_name
let str ?(pure=true) ?comment s : t =
{expression_desc = Str (pure,s); comment}
let unicode ?comment s : t =
{expression_desc = Unicode (s); comment}
let raw_js_code ?comment info s : t =
{expression_desc = Raw_js_code {code = String.trim s; code_info = info} ; comment }
let array ?comment mt es : t =
{expression_desc = Array (es,mt) ; comment}
let some_comment = None
let optional_block e : J.expression =
{ expression_desc = Optional_block (e,false) ;
comment = some_comment
}
let optional_not_nest_block e : J.expression =
{
expression_desc = Optional_block(e,true);
comment = None
}
(** used in normal property
like [e.length], no dependency introduced
*)
let dot ?comment (e0 : t) (e1 : string) : t =
{ expression_desc = Static_index (e0, e1,None); comment}
let module_access (e : t) (name : string) (pos : int32) =
let name = Ext_ident.convert name in
match e.expression_desc with
| Caml_block (l, _, _,_) when no_side_effect e ->
(match Ext_list.nth_opt l (Int32.to_int pos) with
| Some x -> x
| None ->
{ expression_desc = Static_index (e, name,Some pos); comment=None}
)
| _ ->
{ expression_desc = Static_index (e, name,Some pos); comment=None}
let make_block ?comment
(tag : t)
(tag_info : J.tag_info)
(es : t list)
(mutable_flag : J.mutable_flag) : t =
{
expression_desc = Caml_block( es, mutable_flag, tag,tag_info) ;
comment
}
module L = Literals
(* ATTENTION: this is relevant to how we encode string, boolean *)
let typeof ?comment (e : t) : t =
match e.expression_desc with
| Number _
| Length _
-> str ?comment L.js_type_number
| Str _
-> str ?comment L.js_type_string
| Array _
-> str ?comment L.js_type_object
| Bool _ -> str ?comment L.js_type_boolean
| _ -> {expression_desc = Typeof e ; comment }
let new_ ?comment e0 args : t =
{ expression_desc = New (e0, Some args ); comment}
let unit : t =
{expression_desc = Undefined ; comment = None}
(* let math ?comment v args : t =
{comment ; expression_desc = Math(v,args)} *)
(* we can do constant folding here, but need to make sure the result is consistent
{[
let f x = string_of_int x
;; f 3
]}
{[
string_of_int 3
]}
Used in [string_of_int] and format "%d"
TODO: optimize
*)
(* Attention: Shared *mutable state* is evil,
[Js_fun_env.empty] is a mutable state ..
*)
let ocaml_fun
?comment
?immutable_mask
params block : t =
let len = List.length params in
{
expression_desc =
Fun (false, params,block, Js_fun_env.make ?immutable_mask len );
comment
}
let method_
?comment
?immutable_mask
params block : t =
let len = List.length params in
{
expression_desc =
Fun (true, params,block, Js_fun_env.make ?immutable_mask len );
comment
}
(** ATTENTION: This is coupuled with {!Caml_obj.caml_update_dummy} *)
let dummy_obj ?comment (info : Lam_tag_info.t) : t =
(* TODO:
for record it is [{}]
for other it is [[]]
*)
match info with
| Blk_record _
| Blk_module _
| Blk_constructor _
| Blk_record_inlined _
| Blk_poly_var
| Blk_extension
| Blk_record_ext _
->
{comment ; expression_desc = Object []}
| Blk_tuple | Blk_array
| Blk_na _
| Blk_class | Blk_module_export
->
{comment ; expression_desc = Array ([],Mutable)}
(* TODO: complete
pure ...
*)
let rec seq ?comment (e0 : t) (e1 : t) : t =
match e0.expression_desc, e1.expression_desc with
| (Seq( a, {expression_desc = Number _ | Undefined ; })
| Seq( {expression_desc = Number _ | Undefined; },a)), _
->
seq ?comment a e1
| _, ( Seq( {expression_desc = Number _ | Undefined; }, a)) ->
(* Return value could not be changed*)
seq ?comment e0 a
| _, ( Seq(a,( {expression_desc = Number _ | Undefined; } as v ) ))->
(* Return value could not be changed*)
seq ?comment (seq e0 a) v
| (Number _ | Var _ | Undefined) , _ -> e1
| _ ->
{expression_desc = Seq(e0,e1); comment}
let fuse_to_seq x xs =
if xs = [] then x
else Ext_list.fold_left xs x seq
(* let empty_string_literal : t =
{expression_desc = Str (true,""); comment = None} *)
let zero_int_literal : t =
{expression_desc = Number (Int {i = 0l; c = None}) ; comment = None}
let one_int_literal : t =
{expression_desc = Number (Int {i = 1l; c = None}) ; comment = None}
let two_int_literal : t =
{expression_desc = Number (Int {i = 2l; c = None}) ; comment = None}
let three_int_literal : t =
{expression_desc = Number (Int {i = 3l; c = None}) ; comment = None}
let four_int_literal : t =
{expression_desc = Number (Int {i = 4l; c = None}) ; comment = None}
let five_int_literal : t =
{expression_desc = Number (Int {i = 5l; c = None}) ; comment = None}
let six_int_literal : t =
{expression_desc = Number (Int {i = 6l; c = None}) ; comment = None}
let seven_int_literal : t =
{expression_desc = Number (Int {i = 7l; c = None}) ; comment = None}
let eight_int_literal : t =
{expression_desc = Number (Int {i = 8l; c = None}) ; comment = None}
let nine_int_literal : t =
{expression_desc = Number (Int {i = 9l; c = None}) ; comment = None}
let obj_int_tag_literal : t =
{expression_desc = Number (Int {i = 248l; c = None}) ; comment = None}
let int ?comment ?c i : t =
{expression_desc = Number (Int {i; c}) ; comment}
let small_int i : t =
match i with
| 0 -> zero_int_literal
| 1 -> one_int_literal
| 2 -> two_int_literal
| 3 -> three_int_literal
| 4 -> four_int_literal
| 5 -> five_int_literal
| 6 -> six_int_literal
| 7 -> seven_int_literal
| 8 -> eight_int_literal
| 9 -> nine_int_literal
| 248 -> obj_int_tag_literal
| i -> int (Int32.of_int i)
let array_index ?comment (e0 : t) (e1 : t) : t =
match e0.expression_desc, e1.expression_desc with
| Array (l,_) , Number (Int {i; _}) (* Float i -- should not appear here *)
when no_side_effect e0->
(match Ext_list.nth_opt l (Int32.to_int i) with
| None -> { expression_desc = Array_index (e0,e1); comment}
| Some x -> x ) (* FIX #3084*)
| _ ->
{ expression_desc = Array_index (e0,e1); comment}
let array_index_by_int ?comment (e : t) (pos : int32) : t =
match e.expression_desc with
| Array (l,_) (* Float i -- should not appear here *)
| Caml_block (l,_, _, _) when no_side_effect e
->
(match Ext_list.nth_opt l (Int32.to_int pos) with
| Some x-> x
| None ->
{ expression_desc = Array_index (e, int ?comment pos); comment = None}
)
| _ -> { expression_desc = Array_index (e, int ?comment pos); comment = None}
let record_access (e : t) (name : string) (pos : int32) =
(* let name = Ext_ident.convert name in *)
match e.expression_desc with
| Array (l,_) (* Float i -- should not appear here *)
| Caml_block (l,_, _, _) when no_side_effect e
->
(match Ext_list.nth_opt l (Int32.to_int pos) with
| Some x-> x
| None ->
{ expression_desc = Static_index (e, name, Some pos); comment = None}
)
| _ -> { expression_desc = Static_index (e, name, Some pos); comment = None}
(* The same as {!record_access} except tag*)
let inline_record_access = record_access
let variant_access (e : t) (pos : int32) =
inline_record_access e ("_" ^ Int32.to_string pos) pos
let cons_access (e : t) (pos : int32) =
inline_record_access e
(match pos with
| 0l -> Literals.hd
| 1l -> Literals.tl
| _ -> ("_" ^ Int32.to_string pos)) pos
let poly_var_tag_access (e : t) =
match e.expression_desc with
| Caml_block (l,_, _, _) when no_side_effect e
->
(match l with
| x ::_ -> x
| [] ->
assert false
)
| _ -> { expression_desc = Static_index (e, Literals.polyvar_hash, Some 0l); comment = None}
let poly_var_value_access (e : t) =
match e.expression_desc with
| Caml_block (l,_, _, _) when no_side_effect e
->
(match l with
| _ :: v :: _ -> v
| _ ->
assert false
)
| _ -> { expression_desc = Static_index (e, Literals.polyvar_value, Some 1l); comment = None}
let extension_access (e : t) name (pos : int32) : t =
match e.expression_desc with
| Array (l,_) (* Float i -- should not appear here *)
| Caml_block (l,_, _, _) when no_side_effect e
->
(match Ext_list.nth_opt l (Int32.to_int pos) with
| Some x-> x
| None ->
let name =
match name with Some n -> n | None ->
"_" ^ Int32.to_string pos in
{ expression_desc = Static_index (e, name, Some pos); comment = None}
)
| _ ->
let name =
match name with Some n -> n | None ->
"_" ^ Int32.to_string pos in
{ expression_desc = Static_index (e, name, Some pos); comment = None}
let string_index ?comment (e0 : t) (e1 : t) : t =
match e0.expression_desc, e1.expression_desc with
| Str (_,s) , Number (Int {i; _})
-> (* Don't optimize {j||j} *)
let i = Int32.to_int i in
if i >= 0 && i < String.length s then
(* TODO: check exception when i is out of range..
RangeError?
*)
str (String.make 1 s.[i])
else { expression_desc = String_index (e0,e1); comment}
| _ ->
{ expression_desc = String_index (e0,e1); comment}
let assign ?comment e0 e1 : t =
{expression_desc = Bin(Eq, e0,e1); comment}
let assign_by_exp
(e : t) index
value : t =
match e.expression_desc with
| Array _ (*
Temporary block -- address not held
Optimize cases like this which is really
rare {[
(ref x) := 3
]}
*)
| Caml_block _ when no_side_effect e && no_side_effect index ->
value
| _ ->
assign { expression_desc =
Array_index (e, index); comment = None} value
let assign_by_int
?comment
e0 (index : int32) value =
assign_by_exp e0 (int ?comment index) value
let record_assign
(e : t)
(pos : int32)
(name : string)
(value : t) =
match e.expression_desc with
| Array _ (*
Temporary block -- address not held
Optimize cases like this which is really
rare {[
(ref x) := 3
]}
*)
| Caml_block _ when no_side_effect e ->
value
| _ ->
assign { expression_desc =
Static_index (e, name, Some pos); comment = None} value
let extension_assign
(e : t)
(pos : int32)
name
(value : t) =
match e.expression_desc with
| Array _ (*
Temporary block -- address not held
Optimize cases like this which is really
rare {[
(ref x) := 3
]}
*)
| Caml_block _ when no_side_effect e ->
value
| _ ->
assign { expression_desc =
Static_index (e, name, Some pos); comment = None} value
(* This is a property access not external module *)
let array_length ?comment (e : t) : t =
match e.expression_desc with
(* TODO: use array instead? *)
| (Array (l, _) | Caml_block(l,_,_,_)) when no_side_effect e
-> int ?comment (Int32.of_int (List.length l))
| _ -> { expression_desc = Length (e, Array) ; comment }
let string_length ?comment (e : t) : t =
match e.expression_desc with
| Str(_,v) -> int ?comment (Int32.of_int (String.length v))
(* No optimization for {j||j}*)
| _ -> { expression_desc = Length (e, String) ; comment }
(* TODO: use [Buffer] instead? *)
let bytes_length ?comment (e : t) : t =
match e.expression_desc with
| Array (l, _) -> int ?comment (Int32.of_int (List.length l))
| _ -> { expression_desc = Length (e, Bytes) ; comment }
let function_length ?comment (e : t) : t =
match e.expression_desc with
| Fun(b, params, _, _) ->
let params_length =
List.length params in
int ?comment
(Int32.of_int
(if b then params_length - 1
else params_length))
| _ -> { expression_desc = Length (e, Function) ; comment }
(** no dependency introduced *)
(* let js_global_dot ?comment (x : string) (e1 : string) : t =
{ expression_desc = Static_index (js_global x, e1,None); comment}
let char_of_int ?comment (v : t) : t =
match v.expression_desc with
| Number (Int {i; _}) ->
str (String.make 1(Char.chr (Int32.to_int i)))
| Char_to_int v -> v
| _ -> {comment ; expression_desc = Char_of_int v} *)
let char_to_int ?comment (v : t) : t =
match v.expression_desc with
| Str (_, x) -> (* No optimization for .. *)
assert (String.length x = 1) ;
int ~comment:(Printf.sprintf "%S" x )
(Int32.of_int @@ Char.code x.[0])
| Char_of_int v -> v
| _ -> {comment; expression_desc = Char_to_int v }
let rec string_append ?comment (e : t) (el : t) : t =
match e.expression_desc , el.expression_desc with
| Str(_,a), String_append ({expression_desc = Str(_,b)}, c) ->
string_append ?comment (str (a ^ b)) c
| String_append (c,{expression_desc = Str(_,b)}), Str(_,a) ->
string_append ?comment c (str (b ^ a))
| String_append (a,{expression_desc = Str(_,b)}),
String_append ({expression_desc = Str(_,c)} ,d) ->
string_append ?comment (string_append a (str (b ^ c))) d
| Str (_,a), Str (_,b) -> str ?comment (a ^ b)
| _, _ -> {comment ; expression_desc = String_append(e,el)}
let obj ?comment properties : t =
{expression_desc = Object properties; comment }
(* currently only in method call, no dependency introduced
*)
(* Static_index .....................**)
(* var (Jident.create_js "true") *)
let true_ : t = {comment = None; expression_desc = Bool true }
let false_ : t = {comment = None; expression_desc = Bool false }
let bool v = if v then true_ else false_
(** Arith operators *)
(* Static_index .....................**)
let float ?comment f : t =
{expression_desc = Number (Float {f}); comment}
let zero_float_lit : t =
{expression_desc = Number (Float {f = "0." }); comment = None}
let float_mod ?comment e1 e2 : J.expression =
{ comment ;
expression_desc = Bin (Mod, e1,e2)
}
let rec triple_equal ?comment (e0 : t) (e1 : t ) : t =
match e0.expression_desc, e1.expression_desc with
| (Null| Undefined),
(Char_of_int _ | Char_to_int _
| Bool _ | Number _ | Typeof _
| Fun _ | Array _ | Caml_block _ )
when no_side_effect e1 ->
false_ (* TODO: rename it as [caml_false] *)
|
(Char_of_int _ | Char_to_int _
| Bool _ | Number _ | Typeof _
| Fun _ | Array _ | Caml_block _ ), (Null|Undefined)
when no_side_effect e0 ->
false_
| Str (_,x), Str (_,y) -> (* CF*)
bool (Ext_string.equal x y)
| Char_to_int a , Char_to_int b ->
triple_equal ?comment a b
| Char_to_int a , Number (Int {i=_; c = Some v})
| Number (Int {i=_; c = Some v}), Char_to_int a ->
triple_equal ?comment a (str (String.make 1 v))
| Number (Int {i = i0; _}), Number (Int {i = i1; _})
->
bool (i0 = i1)
| Char_of_int a , Char_of_int b
| Optional_block (a,_), Optional_block (b,_)
->
triple_equal ?comment a b
| Undefined, Optional_block _
| Optional_block _, Undefined
| Null, Undefined
| Undefined, Null -> false_
| Null, Null
| Undefined, Undefined -> true_
| _ ->
{expression_desc = Bin(EqEqEq, e0,e1); comment}
let bin ?comment (op : J.binop) (e0 : t) (e1 : t) : t =
match op, e0.expression_desc, e1.expression_desc with
| EqEqEq,_,_ -> triple_equal ?comment e0 e1
| Ge, Length (e,_), Number (Int {i = 0l}) when no_side_effect e ->
true_ (** x.length >=0 | [x] is pure -> true*)
| Gt, Length (_,_), Number (Int {i = 0l}) ->
(* [e] is kept so no side effect check needed *)
{expression_desc = Bin(NotEqEq,e0, e1); comment }
| _ -> {expression_desc = Bin(op,e0,e1); comment}
(* TODO: Constant folding, Google Closure will do that?,
Even if Google Clsoure can do that, we will see how it interact with other
optimizations
We wrap all boolean functions here, since OCaml boolean is a
bit different from Javascript, so that we can change it in the future
{[ a && (b && c) === (a && b ) && c ]}
is not used: benefit is not clear
| Int_of_boolean e10, Bin(And, {expression_desc = Int_of_boolean e20 }, e3)
->
and_ ?comment
{ e1 with expression_desc
=
J.Int_of_boolean { expression_desc = Bin (And, e10,e20); comment = None}
}
e3
Note that
{[ "" && 3 ]}
return "" instead of false, so [e1] is indeed useful
optimization if [e1 = e2], then and_ e1 e2 -> e2
be careful for side effect
*)
let and_ ?comment (e1 : t) (e2 : t) : t =
match e1.expression_desc, e2.expression_desc with
| Var i, Var j when Js_op_util.same_vident i j
->
e1
| Var i, Bin (And, {expression_desc = Var j ; _}, _)
when Js_op_util.same_vident i j
->
e2
| Var i, Bin (And , l, ({expression_desc = Var j ; _} as r))
when Js_op_util.same_vident i j ->
{ e2 with expression_desc = Bin(And, r,l)}
| Bin(NotEqEq, {expression_desc = Var i}, {expression_desc = Undefined } ),
Bin(EqEqEq, {expression_desc = Var j}, {expression_desc = Str _ | Number _ | Unicode _})
when Js_op_util.same_vident i j
-> e2
| _, _ ->
{ expression_desc = Bin(And, e1,e2) ; comment }
let or_ ?comment (e1 : t) (e2 : t) =
match e1.expression_desc, e2.expression_desc with
| Var i, Var j when Js_op_util.same_vident i j
->
e1
| Var i,
Bin (Or, {expression_desc = Var j ; _}, _)
when Js_op_util.same_vident i j
-> e2
| Var i,
Bin (Or , l, ({expression_desc = Var j ; _} as r))
when Js_op_util.same_vident i j
->
{ e2 with expression_desc = Bin(Or,r,l)}
| _, _ ->
{expression_desc = Bin(Or, e1,e2); comment }
(* return a value of type boolean *)
(* TODO:
when comparison with Int
it is right that !(x > 3 ) -> x <= 3 *)
let not ( e : t) : t =
match e.expression_desc with
| Number (Int {i; _}) -> bool (i = 0l )
| Js_not e -> e
| Bool b -> if b then false_ else true_
| Bin(EqEqEq , e0,e1) -> {e with expression_desc = Bin(NotEqEq, e0,e1)}
| Bin(NotEqEq , e0,e1) -> {e with expression_desc = Bin(EqEqEq, e0,e1)}
| Bin(Lt, a, b) -> {e with expression_desc = Bin (Ge,a,b)}
| Bin(Ge,a,b) -> {e with expression_desc = Bin (Lt,a,b)}
| Bin(Le,a,b) -> {e with expression_desc = Bin (Gt,a,b)}
| Bin(Gt,a,b) -> {e with expression_desc = Bin (Le,a,b)}
| _ -> {expression_desc = Js_not e ; comment = None}
let not_empty_branch (x : t) =
match x.expression_desc with
| Number (Int {i = 0l}) | Undefined -> false
| _ -> true
let rec econd ?comment (pred : t) (ifso : t) (ifnot : t) : t =
match pred.expression_desc , ifso.expression_desc, ifnot.expression_desc with
| Bool false, _, _ -> ifnot
| Number ((Int { i = 0l; _}) ), _, _
-> ifnot
| (Number _ | Array _ | Caml_block _ ), _, _
when no_side_effect pred
-> ifso (* a block can not be false in OCAML, CF - relies on flow inference*)
| Bool true, _, _ -> ifso
| _, (Cond (pred1, ifso1, ifnot1)), _
when Js_analyzer.eq_expression ifnot1 ifnot
->
(* {[
if b then (if p1 then branch_code0 else branch_code1)
else branch_code1
]}
is equivalent to
{[
if b && p1 then branch_code0 else branch_code1
]}
*)
econd (and_ pred pred1) ifso1 ifnot
| _, (Cond (pred1, ifso1, ifnot1)), _
when Js_analyzer.eq_expression ifso1 ifnot
->
econd (and_ pred (not pred1)) ifnot1 ifnot
| _, _, (Cond (pred1, ifso1, ifnot1))
when Js_analyzer.eq_expression ifso ifso1
->
econd (or_ pred pred1) ifso ifnot1
| _, _, (Cond (pred1, ifso1, ifnot1))
when Js_analyzer.eq_expression ifso ifnot1
->
econd (or_ pred (not pred1)) ifso ifso1
| Js_not e, _, _ when not_empty_branch ifnot
->
econd ?comment e ifnot ifso
| _, Seq (a,{expression_desc = Undefined}), Seq(b,{expression_desc = Undefined})
-> seq (econd ?comment pred a b ) undefined
| _ ->
if Js_analyzer.eq_expression ifso ifnot then
if no_side_effect pred then ifso else seq ?comment pred ifso
else
{expression_desc = Cond(pred,ifso,ifnot); comment}
let rec float_equal ?comment (e0 : t) (e1 : t) : t =
match e0.expression_desc, e1.expression_desc with
| Number (Int {i = i0 ; _}), Number (Int {i = i1; }) ->
bool (i0 = i1)
| Undefined , Undefined -> true_
| (Bin(Bor,
{expression_desc = Number(Int {i = 0l; _})},
({expression_desc = Caml_block_tag _; _} as a ))
|
Bin(Bor,
({expression_desc = Caml_block_tag _; _} as a),
{expression_desc = Number (Int {i = 0l; _})})),
Number (Int {i = 0l; _})
-> (** (x.tag | 0) === 0 *)
not a
| (Bin(Bor,
{expression_desc = Number(Int {i = 0l; _})},
({expression_desc = Caml_block_tag _; _} as a ))
|
Bin(Bor,
({expression_desc = Caml_block_tag _; _} as a),
{expression_desc = Number (Int {i = 0l; _})}))
, Number _ -> (* for sure [i <> 0 ]*)
(* since a is integer, if we guarantee there is no overflow
of a
then [a | 0] is a nop unless a is undefined
(which is applicable when applied to tag),
obviously tag can not be overflowed.
if a is undefined, then [ a|0===0 ] is true
while [a === 0 ] is not true
[a|0 === non_zero] is false and [a===non_zero] is false
so we can not eliminate when the tag is zero
*)
float_equal ?comment a e1
| Number (Float {f = f0; _}), Number (Float {f = f1 ; }) when f0 = f1 ->
true_
| Char_to_int a , Char_to_int b ->
float_equal ?comment a b
| Char_to_int a , Number (Int {i = _; c = Some v})
| Number (Int {i = _; c = Some v}), Char_to_int a ->
float_equal ?comment a (str (String.make 1 v))
| Char_of_int a , Char_of_int b ->
float_equal ?comment a b
| _ ->
{expression_desc = Bin(EqEqEq, e0,e1); comment}
let int_equal = float_equal
let string_equal ?comment (e0 : t) (e1 : t) : t =
match e0.expression_desc, e1.expression_desc with
| Str (_, a0), Str(_, b0)
-> bool (Ext_string.equal a0 b0)
| Unicode a0, Unicode b0 -> bool (Ext_string.equal a0 b0)
| _ , _
->
{expression_desc = Bin(EqEqEq, e0,e1); comment}
let is_type_number ?comment (e : t) : t =
string_equal ?comment (typeof e) (str "number")
let is_type_string ?comment (e : t) : t =
string_equal ?comment (typeof e) (str "string")
(* we are calling [Caml_primitive.primitive_name], since it's under our
control, we should make it follow the javascript name convention, and
call plain [dot]
*)
let tag ?comment e : t =
{expression_desc =
Bin (Bor, {expression_desc = Caml_block_tag e; comment }, zero_int_literal );
comment = None }
(* according to the compiler, [Btype.hash_variant],
it's reduced to 31 bits for hash
*)
(* FIXME: unused meth_name *)
let public_method_call _meth_name obj label cache args =
let len = List.length args in
(* econd (int_equal (tag obj ) obj_int_tag_literal) *)
if len <= 7 then
runtime_call Js_runtime_modules.caml_oo_curry
("js" ^ string_of_int (len + 1) )
(label:: ( int cache) :: obj::args)
else
runtime_call Js_runtime_modules.caml_oo_curry "js"
[label;
int cache;
obj ;
array NA (obj::args)
]
(* TODO: handle arbitrary length of args ..
we can reduce part of the overhead by using
`__js` -- a easy ppx {{ x ##.hh }}
the downside is that no way to swap ocaml/js implementation
for object part, also need encode arity..
how about x#|getElementById|2|
*)
(* Note that [lsr] or [bor] are js semantics *)
let rec int32_bor ?comment (e1 : J.expression) (e2 : J.expression) : J.expression =
match e1.expression_desc, e2.expression_desc with
| Number (Int {i = i1} | Uint i1), Number (Int {i = i2})
-> int ?comment (Int32.logor i1 i2)
| _, (Bin(Lsr,e2, {expression_desc = Number(Int{i=0l} | Uint 0l | Nint 0n) ; _})) ->
int32_bor e1 e2
| (Bin(Lsr,e1, {expression_desc = Number(Int{i=0l} | Uint 0l | Nint 0n) ; _})), _ ->
int32_bor e1 e2
| (Bin(Lsr,_, {expression_desc = Number(Int{i} | Uint i ) ; _})),
Number(Int{i=0l} | Uint 0l | Nint 0n)
when i > 0l -> (* a >>> 3 | 0 -> a >>> 3 *)
e1
| Bin (Bor, e1, {expression_desc = Number(Int{i=0l} | Uint 0l | Nint 0n) ; _} ),
Number(Int{i=0l} | Uint 0l | Nint 0n) ->
int32_bor e1 e2
| _ ->
{ comment ;
expression_desc = Bin (Bor, e1,e2)
}
(* Arithmatic operations
TODO: distinguish between int and float
TODO: Note that we have to use Int64 to avoid integer overflow, this is fine
since Js only have .
like code below
{[
MAX_INT_VALUE - (MAX_INT_VALUE - 100) + 20
]}
{[
MAX_INT_VALUE - x + 30
]}
check: Re-association: avoid integer overflow
*)
let to_int32 ?comment (e : J.expression) : J.expression =
int32_bor ?comment e zero_int_literal
(* TODO: if we already know the input is int32, [x|0] can be reduced into [x] *)
let nint ?comment n : J.expression =
{expression_desc = Number (Nint n); comment }
let uint32 ?comment n : J.expression =
{expression_desc = Number (Uint n); comment }
let string_comp (cmp : J.binop) ?comment (e0: t) (e1 : t) =
match e0.expression_desc ,e1.expression_desc with
| Str(_,a0),Str(_,b0) ->
begin match cmp with
| EqEqEq -> bool (a0 = b0)
| NotEqEq -> bool (a0 <> b0)
| _ -> bin ?comment cmp e0 e1
end
| _ -> bin ?comment cmp e0 e1
let obj_length ?comment e : t =
to_int32 {expression_desc = Length (e, Caml_block); comment }
let rec int_comp (cmp : Lam_compat.comparison) ?comment (e0 : t) (e1 : t) =
match cmp, e0.expression_desc, e1.expression_desc with
| _, Call ({
expression_desc =
Var (Qualified
({kind = Runtime},
Some ("caml_int_compare" | "caml_int32_compare"))); _},
[l;r], _),
Number (Int {i = 0l})
-> int_comp cmp l r (* = 0 > 0 < 0 *)
| Ceq, Call ({
expression_desc =
Var (Qualified
({id = _; kind = Runtime} as iid,
Some ("caml_compare"))); _} as fn,
([_;_] as args), call_info),
Number (Int {i = 0l})
->
{e0 with expression_desc =
Call(
{fn with expression_desc =
Var(Qualified (iid, Some "caml_equal"))
} , args, call_info)}
| Ceq, Optional_block _, Undefined
| Ceq, Undefined, Optional_block _
-> false_
| Ceq, _, _ -> int_equal e0 e1
| Cneq, Optional_block _, Undefined
| Cneq, Undefined , Optional_block _
| Cneq, Caml_block _ , Number _
| Cneq, Number _, Caml_block _