@@ -44,6 +44,8 @@ let oo = "Caml_oo"
44
44
45
45
let no_side_effect = Js_analyzer. no_side_effect_expression
46
46
47
+ type binary_op = ?comment:string -> J .expression -> J .expression -> J .expression
48
+ type unary_op = ?comment:string -> J .expression -> J .expression
47
49
(*
48
50
remove pure part of the expression
49
51
and keep the non-pure part while preserve the semantics
@@ -552,7 +554,23 @@ module Exp = struct
552
554
553
555
check: Re-association: avoid integer overflow
554
556
*)
555
- let rec add ?comment (e1 : t ) (e2 : t ) =
557
+ let rec to_int32 ?comment (e : J.expression ) : J.expression =
558
+ let expression_desc = e.expression_desc in
559
+ match expression_desc with
560
+ | Bin (Bor , a, {expression_desc = Number (Int {i = 0 }); _})
561
+ ->
562
+ to_int32 ?comment a
563
+ | _ ->
564
+ { comment ;
565
+ expression_desc = Bin (Bor , {comment = None ; expression_desc }, int 0 )
566
+ }
567
+
568
+ let rec to_uint32 ?comment (e : J.expression ) : J.expression =
569
+ { comment ;
570
+ expression_desc = Bin (Lsr , e , int 0 )
571
+ }
572
+
573
+ let rec float_add ?comment (e1 : t ) (e2 : t ) =
556
574
match e1.expression_desc, e2.expression_desc with
557
575
| Number (Int {i;_} ), Number (Int {i = j ;_} ) ->
558
576
int ?comment (i + j)
@@ -577,15 +595,100 @@ module Exp = struct
577
595
(* bin ?comment Plus e2 e1 *)
578
596
| _ ->
579
597
bin ?comment Plus e1 e2
598
+ let int32_add ?comment e1 e2 =
599
+ (* to_int32 @@ *) float_add ?comment e1 e2
580
600
581
- and minus ?comment e1 e2 =
601
+ let float_minus ?comment e1 e2 =
582
602
bin ?comment Minus e1 e2
583
603
584
- and mul ?comment e1 e2 =
604
+ let int32_minus ?comment e1 e2 : J.expression =
605
+ (* to_int32 @@ *) float_minus ?comment e1 e2
606
+
607
+ let float_mul ?comment e1 e2 =
585
608
bin ?comment Mul e1 e2
586
609
587
- and div ?comment e1 e2 =
610
+ let float_div ?comment e1 e2 =
588
611
bin ?comment Div e1 e2
612
+ let float_notequal ?comment e1 e2 =
613
+ bin ?comment NotEqEq e1 e2
614
+
615
+ let int32_div ?comment e1 e2 : J.expression =
616
+ to_int32 (float_div ?comment e1 e2)
617
+
618
+
619
+ (* TODO: call primitive *)
620
+ let int32_mul ?comment e1 e2 : J.expression =
621
+ { comment ;
622
+ expression_desc = Bin (Mul , e1,e2)
623
+ }
624
+
625
+
626
+ (* TODO: check division by zero *)
627
+ let int32_mod ?comment e1 e2 : J.expression =
628
+ { comment ;
629
+ expression_desc = Bin (Mod , e1,e2)
630
+ }
631
+
632
+ let int32_lsl ?comment e1 e2 : J.expression =
633
+ { comment ;
634
+ expression_desc = Bin (Lsl , e1,e2)
635
+ }
636
+
637
+ (* TODO: optimization *)
638
+ let int32_lsr ?comment
639
+ (e1 : J.expression )
640
+ (e2 : J.expression ) : J.expression =
641
+ match e1.expression_desc, e2.expression_desc with
642
+ | Number (Int { i = i1}), Number ( Int {i = i2})
643
+ ->
644
+ int @@ Int32. to_int
645
+ (Int32. shift_right_logical
646
+ (Int32. of_int i1) i2)
647
+ | _ , Number ( Int {i = i2})
648
+ ->
649
+ if i2 = 0 then
650
+ e1
651
+ else
652
+ { comment ;
653
+ expression_desc = Bin (Lsr , e1,e2) (* uint32 *)
654
+ }
655
+ | _ , _ ->
656
+ to_int32 { comment ;
657
+ expression_desc = Bin (Lsr , e1,e2) (* uint32 *)
658
+ }
659
+
660
+ let int32_asr ?comment e1 e2 : J.expression =
661
+ { comment ;
662
+ expression_desc = Bin (Asr , e1,e2)
663
+ }
664
+
665
+ let int32_bxor ?comment e1 e2 : J.expression =
666
+ { comment ;
667
+ expression_desc = Bin (Bxor , e1,e2)
668
+ }
669
+
670
+ let rec int32_band ?comment (e1 : J.expression ) (e2 : J.expression ) : J.expression =
671
+ match e1.expression_desc with
672
+ | Bin (Bor ,a, {expression_desc = Number (Int {i = 0 })})
673
+ ->
674
+ (* Note that in JS
675
+ {[ -1 >>> 0 & 0xffffffff = -1]} is the same as
676
+ {[ (-1 >>> 0 | 0 ) & 0xffffff ]}
677
+ *)
678
+ int32_band a e2
679
+ | _ ->
680
+ { comment ;
681
+ expression_desc = Bin (Band , e1,e2)
682
+ }
683
+
684
+ let int32_bor ?comment e1 e2 : J.expression =
685
+ { comment ;
686
+ expression_desc = Bin (Bor , e1,e2)
687
+ }
688
+
689
+ (* let int32_bin ?comment op e1 e2 : J.expression = *)
690
+ (* {expression_desc = Int32_bin(op,e1, e2); comment} *)
691
+
589
692
590
693
(* TODO -- alpha conversion
591
694
remember to add parens..
0 commit comments