@@ -399,12 +399,10 @@ let string_length ?comment (e : t) : t =
399
399
(* No optimization for {j||j}*)
400
400
| _ -> { expression_desc = Length (e, String ) ; comment }
401
401
402
+ (* TODO: use [Buffer] instead? *)
402
403
let bytes_length ?comment (e : t ) : t =
403
404
match e.expression_desc with
404
- (* TODO: use array instead? * )
405
405
| Array (l , _ ) -> int ?comment (Int32. of_int (List. length l))
406
- | Str (_ ,v ) -> int ?comment (Int32. of_int @@ String. length v)
407
- (* No optimization for unicode *)
408
406
| _ -> { expression_desc = Length (e, Bytes ) ; comment }
409
407
410
408
let function_length ?comment (e : t ) : t =
@@ -416,7 +414,6 @@ let function_length ?comment (e : t) : t =
416
414
(Int32. of_int
417
415
(if b then params_length - 1
418
416
else params_length))
419
- (* TODO: optimize if [e] is know at compile time *)
420
417
| _ -> { expression_desc = Length (e, Function ) ; comment }
421
418
422
419
(* * no dependency introduced *)
@@ -467,11 +464,11 @@ let obj ?comment properties : t =
467
464
468
465
469
466
(* var (Jident.create_js "true") *)
470
- let caml_true : t = {comment = None ; expression_desc = Bool true }
467
+ let true_ : t = {comment = None ; expression_desc = Bool true }
471
468
472
- let caml_false : t = {comment = None ; expression_desc = Bool false }
469
+ let false_ : t = {comment = None ; expression_desc = Bool false }
473
470
474
- let bool v = if v then caml_true else caml_false
471
+ let bool v = if v then true_ else false_
475
472
476
473
(* * Arith operators *)
477
474
(* Dot .....................**)
@@ -499,13 +496,13 @@ let rec triple_equal ?comment (e0 : t) (e1 : t ) : t =
499
496
| Bool _ | Number _ | Typeof _
500
497
| Fun _ | Array _ | Caml_block _ )
501
498
when no_side_effect e1 ->
502
- caml_false (* TODO: rename it as [caml_false] *)
499
+ false_ (* TODO: rename it as [caml_false] *)
503
500
|
504
501
(Char_of_int _ | Char_to_int _
505
502
| Bool _ | Number _ | Typeof _
506
503
| Fun _ | Array _ | Caml_block _ ), (Null | Undefined )
507
504
when no_side_effect e0 ->
508
- caml_false
505
+ false_
509
506
| Str (_ ,x ), Str (_ ,y ) -> (* CF*)
510
507
bool (Ext_string. equal x y)
511
508
| Char_to_int a , Char_to_int b ->
@@ -523,9 +520,9 @@ let rec triple_equal ?comment (e0 : t) (e1 : t ) : t =
523
520
| Undefined , Optional_block _
524
521
| Optional_block _, Undefined
525
522
| Null , Undefined
526
- | Undefined , Null -> caml_false
523
+ | Undefined , Null -> false_
527
524
| Null , Null
528
- | Undefined , Undefined -> caml_true
525
+ | Undefined , Undefined -> true_
529
526
| _ ->
530
527
{expression_desc = Bin (EqEqEq , e0,e1); comment}
531
528
@@ -540,12 +537,8 @@ let bin ?comment (op : J.binop) e0 e1 : t =
540
537
optimizations
541
538
We wrap all boolean functions here, since OCaml boolean is a
542
539
bit different from Javascript, so that we can change it in the future
543
- *)
544
540
545
- let rec and_ ?comment (e1 : t ) (e2 : t ) : t =
546
- match e1.expression_desc, e2.expression_desc with
547
- (*
548
- {[ a && (b && c) === (a && b ) && c ]}
541
+ {[ a && (b && c) === (a && b ) && c ]}
549
542
is not used: benefit is not clear
550
543
| Int_of_boolean e10, Bin(And, {expression_desc = Int_of_boolean e20 }, e3)
551
544
->
@@ -555,15 +548,15 @@ let rec and_ ?comment (e1 : t) (e2 : t) : t =
555
548
J.Int_of_boolean { expression_desc = Bin (And, e10,e20); comment = None}
556
549
}
557
550
e3
558
- *)
559
- (* Note that
560
- {[ "" && 3 ]}
551
+ Note that
552
+ {[ "" && 3 ]}
561
553
return "" instead of false, so [e1] is indeed useful
562
- *)
563
-
564
- (* optimization if [e1 = e2], then and_ e1 e2 -> e2
554
+ optimization if [e1 = e2], then and_ e1 e2 -> e2
565
555
be careful for side effect
566
- *)
556
+ *)
557
+
558
+ let rec and_ ?comment (e1 : t ) (e2 : t ) : t =
559
+ match e1.expression_desc, e2.expression_desc with
567
560
| Var i, Var j when Js_op_util. same_vident i j
568
561
->
569
562
e1
@@ -600,50 +593,48 @@ let rec or_ ?comment (e1 : t) (e2 : t) =
600
593
(* TODO:
601
594
when comparison with Int
602
595
it is right that !(x > 3 ) -> x <= 3 *)
603
- let rec not ({expression_desc; comment} as e : t ) : t =
604
- match expression_desc with
605
- | Number (Int {i; _} ) ->
606
- bool (i = 0l )
596
+ let rec not ( e : t ) : t =
597
+ match e.expression_desc with
598
+ | Number (Int {i; _} ) -> bool (i = 0l )
607
599
| Js_not e -> e
608
- (* match expression_desc with *)
609
- (* can still hapen after some optimizations *)
610
- | Bin (EqEqEq , e0,e1)
611
- -> {expression_desc = Bin (NotEqEq , e0,e1); comment}
612
- | Bin (NotEqEq , e0 ,e1 ) -> {expression_desc = Bin (EqEqEq , e0,e1); comment}
600
+ | Bool b -> if b then false_ else true_
601
+ | Bin (EqEqEq , e0 ,e1 ) -> {e with expression_desc = Bin (NotEqEq , e0,e1)}
602
+ | Bin (NotEqEq , e0 ,e1 ) -> {e with expression_desc = Bin (EqEqEq , e0,e1)}
613
603
| Bin (Lt, a , b ) -> {e with expression_desc = Bin (Ge ,a,b)}
614
604
| Bin (Ge,a ,b ) -> {e with expression_desc = Bin (Lt ,a,b)}
615
605
| Bin (Le,a ,b ) -> {e with expression_desc = Bin (Gt ,a,b)}
616
606
| Bin (Gt,a ,b ) -> {e with expression_desc = Bin (Le ,a,b)}
617
- | Bool b -> if b then caml_false else caml_true
618
607
| x -> {expression_desc = Js_not e ; comment = None }
619
608
620
609
621
610
622
611
623
612
624
- let rec econd ?comment (b : t ) (t : t ) (f : t ) : t =
625
- match b .expression_desc , t .expression_desc, f .expression_desc with
626
- | Bool false , _ , _ -> f
613
+ let rec econd ?comment (pred : t ) (ifso : t ) (ifnot : t ) : t =
614
+ match pred .expression_desc , ifso .expression_desc, ifnot .expression_desc with
615
+ | Bool false , _ , _ -> ifnot
627
616
| Number ((Int { i = 0l ; _}) ), _, _
628
- -> f (* TODO: constant folding: could be refined *)
629
- | (Number _ | Array _ | Caml_block _ | Optional_block _), _, _ when no_side_effect b
630
- -> t (* a block can not be false in OCAML, CF - relies on flow inference*)
631
- | Bool true , _ , _ -> t
617
+ -> ifnot
618
+ | (Number _ | Array _ | Caml_block _ | Optional_block _), _, _
619
+ when no_side_effect pred
620
+ -> ifso (* a block can not be false in OCAML, CF - relies on flow inference*)
621
+ | Bool true , _ , _ -> ifso
632
622
| (Bin (Ge ,
633
623
({expression_desc = Length _ ;
634
624
_}), {expression_desc = Number (Int { i = 0l ; _})})), _, _
635
- -> f
625
+ -> ifnot
636
626
| (Bin (Gt ,
637
627
({expression_desc = Length _;
638
628
_} as pred ),
639
629
({expression_desc = Number (Int {i = 0l ; }) } as zero) )), _, _
640
630
->
641
631
(* * Add comment when simplified *)
642
- econd ?comment {b with expression_desc = (Bin (NotEqEq ,
643
- pred , zero ))} t f
644
-
645
- | _, (Cond (p1, branch_code0, branch_code1)), _
646
- when Js_analyzer. eq_expression branch_code1 f
632
+ econd ?comment
633
+ {pred with
634
+ expression_desc =
635
+ Bin (NotEqEq , pred, zero)} ifso ifnot
636
+ | _, (Cond (pred1, ifso1, ifnot1)), _
637
+ when Js_analyzer. eq_expression ifnot1 ifnot
647
638
->
648
639
(* {[
649
640
if b then (if p1 then branch_code0 else branch_code1)
@@ -654,15 +645,15 @@ let rec econd ?comment (b : t) (t : t) (f : t) : t =
654
645
if b && p1 then branch_code0 else branch_code1
655
646
]}
656
647
*)
657
- econd (and_ b p1) branch_code0 f
648
+ econd (and_ pred pred1) ifso1 ifnot
658
649
| _, (Cond (p1, branch_code0, branch_code1)), _
659
- when Js_analyzer. eq_expression branch_code0 f
650
+ when Js_analyzer. eq_expression branch_code0 ifnot
660
651
->
661
652
(* the same as above except we revert the [cond] expression *)
662
- econd (and_ b (not p1)) branch_code1 f
653
+ econd (and_ pred (not p1)) branch_code1 ifnot
663
654
664
655
| _, _, (Cond (p1', branch_code0, branch_code1))
665
- when Js_analyzer. eq_expression t branch_code0
656
+ when Js_analyzer. eq_expression ifso branch_code0
666
657
(*
667
658
{[
668
659
if b then branch_code0 else (if p1' then branch_code0 else branch_code1)
@@ -673,21 +664,21 @@ let rec econd ?comment (b : t) (t : t) (f : t) : t =
673
664
]}
674
665
*)
675
666
->
676
- econd (or_ b p1') t branch_code1
667
+ econd (or_ pred p1') ifso branch_code1
677
668
| _, _, (Cond (p1', branch_code0, branch_code1))
678
- when Js_analyzer. eq_expression t branch_code1
669
+ when Js_analyzer. eq_expression ifso branch_code1
679
670
->
680
671
(* the same as above except we revert the [cond] expression *)
681
- econd (or_ b (not p1')) t branch_code0
672
+ econd (or_ pred (not p1')) ifso branch_code0
682
673
683
674
| Js_not e, _, _
684
675
->
685
- econd ?comment e f t
676
+ econd ?comment e ifnot ifso
686
677
| _ ->
687
- if Js_analyzer. eq_expression t f then
688
- if no_side_effect b then t else seq ?comment b t
678
+ if Js_analyzer. eq_expression ifso ifnot then
679
+ if no_side_effect pred then ifso else seq ?comment pred ifso
689
680
else
690
- {expression_desc = Cond (b,t,f ); comment}
681
+ {expression_desc = Cond (pred,ifso,ifnot ); comment}
691
682
692
683
693
684
let rec float_equal ?comment (e0 : t ) (e1 : t ) : t =
@@ -724,7 +715,7 @@ let rec float_equal ?comment (e0 : t) (e1 : t) : t =
724
715
*)
725
716
float_equal ?comment a e1
726
717
| Number (Float {f = f0 ; _} ), Number (Float {f = f1 ; } ) when f0 = f1 ->
727
- caml_true
718
+ true_
728
719
729
720
| Char_to_int a , Char_to_int b ->
730
721
float_equal ?comment a b
@@ -883,14 +874,14 @@ let rec int_comp (cmp : Lam_compat.comparison) ?comment (e0 : t) (e1 : t) =
883
874
} , args, call_info)}
884
875
| Ceq , Optional_block _, Undefined
885
876
| Ceq , Undefined , Optional_block _
886
- -> caml_false
877
+ -> false_
887
878
| Ceq , _ , _ -> int_equal e0 e1
888
879
889
880
| Cneq , Optional_block _, Undefined
890
881
| Cneq , Undefined , Optional_block _
891
882
| Cneq , Caml_block _ , Number _
892
883
| Cneq , Number _, Caml_block _
893
- -> caml_true
884
+ -> true_
894
885
| _ ->
895
886
bin ?comment (Lam_compile_util. jsop_of_comp cmp) e0 e1
896
887
@@ -908,8 +899,8 @@ let bool_comp (cmp : Lam_compat.comparison) ?comment (e0 : t) (e1 : t) =
908
899
| {expression_desc = Bool true }, rest
909
900
| rest , {expression_desc = Bool false } ->
910
901
begin match cmp with
911
- | Clt -> seq rest caml_false
912
- | Cge -> seq rest caml_true
902
+ | Clt -> seq rest false_
903
+ | Cge -> seq rest true_
913
904
| Cle
914
905
| Cgt
915
906
| Ceq
@@ -919,8 +910,8 @@ let bool_comp (cmp : Lam_compat.comparison) ?comment (e0 : t) (e1 : t) =
919
910
| {expression_desc = Bool false }, rest
920
911
->
921
912
begin match cmp with
922
- | Cle -> seq rest caml_true
923
- | Cgt -> seq rest caml_false
913
+ | Cle -> seq rest true_
914
+ | Cgt -> seq rest false_
924
915
| Clt
925
916
| Cge
926
917
| Ceq
@@ -1229,8 +1220,8 @@ let for_sure_js_null_undefined (x : t) =
1229
1220
let is_null_undefined ?comment (x : t ) : t =
1230
1221
match x.expression_desc with
1231
1222
| Null | Undefined
1232
- -> caml_true
1233
- | Number _ | Array _ | Caml_block _ -> caml_false
1223
+ -> true_
1224
+ | Number _ | Array _ | Caml_block _ -> false_
1234
1225
| _ ->
1235
1226
{ comment ;
1236
1227
expression_desc = Is_null_or_undefined x
@@ -1243,18 +1234,18 @@ let eq_null_undefined_boolean ?comment (a : t) (b : t) =
1243
1234
| Bool _ | Number _ | Typeof _
1244
1235
| Fun _ | Array _ | Caml_block _ )
1245
1236
->
1246
- caml_false
1237
+ false_
1247
1238
| (Char_of_int _ | Char_to_int _
1248
1239
| Bool _ | Number _ | Typeof _
1249
1240
| Fun _ | Array _ | Caml_block _ ),
1250
1241
(Null | Undefined )
1251
1242
->
1252
- caml_false
1243
+ false_
1253
1244
| (Null , Undefined )
1254
- | (Undefined, Null) -> caml_false
1245
+ | (Undefined, Null) -> false_
1255
1246
| (Null , Null )
1256
1247
| (Undefined , Undefined )
1257
- -> caml_true
1248
+ -> true_
1258
1249
| _ ->
1259
1250
{expression_desc = Bin (EqEqEq , a, b); comment}
1260
1251
@@ -1267,19 +1258,19 @@ let neq_null_undefined_boolean ?comment (a : t) (b : t) =
1267
1258
| Bool _ | Number _ | Typeof _
1268
1259
| Fun _ | Array _ | Caml_block _ )
1269
1260
->
1270
- caml_true
1261
+ true_
1271
1262
| (Char_of_int _ | Char_to_int _
1272
1263
| Bool _ | Number _ | Typeof _
1273
1264
| Fun _ | Array _ | Caml_block _ ),
1274
1265
(Null | Undefined )
1275
1266
->
1276
- caml_true
1267
+ true_
1277
1268
| (Null , Null )
1278
1269
| (Undefined , Undefined )
1279
- -> caml_false
1270
+ -> false_
1280
1271
| (Null , Undefined )
1281
1272
| (Undefined , Null )
1282
- -> caml_true
1273
+ -> true_
1283
1274
| _ ->
1284
1275
{expression_desc = Bin (NotEqEq , a, b); comment}
1285
1276
0 commit comments