Skip to content

Commit 7668157

Browse files
authored
Merge pull request #4892 from rescript-lang/simplify
simplify boxed int operations
2 parents fc19d96 + 8f6d0ad commit 7668157

31 files changed

+2295
-2329
lines changed

jscomp/core/js_analyzer.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ let rec eq_expression
273273
| Caml_block_tag _
274274

275275
| Object _
276-
| Number (Uint _ | Nint _)
276+
| Number (Uint _ )
277277

278278
-> false
279279
end

jscomp/core/js_dump.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ and expression_desc cxt ~(level:int) f x : cxt =
675675
-> Int32.to_string i (* check , js convention with ocaml lexical convention *)
676676
| Uint i
677677
-> Format.asprintf "%lu" i
678-
| Nint i -> Nativeint.to_string i in
678+
in
679679
let need_paren =
680680
if s.[0] = '-'
681681
then level > 13 (* Negative numbers may need to be parenthesized. *)

jscomp/core/js_exp_make.ml

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -909,17 +909,17 @@ let rec int32_bor ?comment (e1 : J.expression) (e2 : J.expression) : J.expressio
909909
| Number (Int {i = i1} | Uint i1), Number (Int {i = i2})
910910
-> int ?comment (Int32.logor i1 i2)
911911

912-
| _, (Bin(Lsr,e2, {expression_desc = Number(Int{i=0l} | Uint 0l | Nint 0n) ; _})) ->
912+
| _, (Bin(Lsr,e2, {expression_desc = Number(Int{i=0l} | Uint 0l ) ; _})) ->
913913
int32_bor e1 e2
914-
| (Bin(Lsr,e1, {expression_desc = Number(Int{i=0l} | Uint 0l | Nint 0n) ; _})), _ ->
914+
| (Bin(Lsr,e1, {expression_desc = Number(Int{i=0l} | Uint 0l ) ; _})), _ ->
915915
int32_bor e1 e2
916916
| (Bin(Lsr,_, {expression_desc = Number(Int{i} | Uint i ) ; _})),
917-
Number(Int{i=0l} | Uint 0l | Nint 0n)
917+
Number(Int{i=0l} | Uint 0l )
918918
when i > 0l -> (* a >>> 3 | 0 -> a >>> 3 *)
919919
e1
920920

921-
| Bin (Bor, e1, {expression_desc = Number(Int{i=0l} | Uint 0l | Nint 0n) ; _} ),
922-
Number(Int{i=0l} | Uint 0l | Nint 0n) ->
921+
| Bin (Bor, e1, {expression_desc = Number(Int{i=0l} | Uint 0l ) ; _} ),
922+
Number(Int{i=0l} | Uint 0l ) ->
923923
int32_bor e1 e2
924924
| _ ->
925925
{ comment ;
@@ -945,8 +945,6 @@ let rec int32_bor ?comment (e1 : J.expression) (e2 : J.expression) : J.expressio
945945
let to_int32 ?comment (e : J.expression) : J.expression =
946946
int32_bor ?comment e zero_int_literal
947947
(* TODO: if we already know the input is int32, [x|0] can be reduced into [x] *)
948-
let nint ?comment n : J.expression =
949-
{expression_desc = Number (Nint n); comment }
950948

951949
let uint32 ?comment n : J.expression =
952950
{expression_desc = Number (Uint n); comment }
@@ -1072,16 +1070,10 @@ let rec int32_lsr ?comment
10721070
match e1.expression_desc, e2.expression_desc with
10731071
| Number (Int { i = i1} | Uint i1 ), Number( Int {i = i2} | Uint i2)
10741072
-> aux i1 (Int32.to_int i2)
1075-
| Number (Nint i1), Number( Int {i = i2} | Uint i2)
1076-
->
1077-
aux (Nativeint.to_int32 i1) (Int32.to_int i2)
1078-
| Number (Nint i1), Number (Nint i2)
1079-
->
1080-
aux (Nativeint.to_int32 i1) (Nativeint.to_int i2)
1081-
| (Bin(Lsr, _, _)), Number (Int {i = 0l} | Uint 0l | Nint 0n)
1073+
| (Bin(Lsr, _, _)), Number (Int {i = 0l} | Uint 0l )
10821074
-> e1 (* TODO: more opportunities here *)
1083-
| Bin(Bor, e1, {expression_desc = Number (Int {i=0l;_} | Uint 0l | Nint 0n) ; _}),
1084-
Number (Int {i = 0l} | Uint 0l | Nint 0n)
1075+
| Bin(Bor, e1, {expression_desc = Number (Int {i=0l;_} | Uint 0l ) ; _}),
1076+
Number (Int {i = 0l} | Uint 0l )
10851077
-> int32_lsr ?comment e1 e2
10861078
| _, _ ->
10871079
{ comment ;
@@ -1132,7 +1124,7 @@ let rec is_out ?comment (e : t) (range : t) : t =
11321124
{expression_desc = Number (Int {i = _; _}) }, {expression_desc = Var _; _})
11331125
|Bin((Plus | Minus ) ,
11341126
{expression_desc = Var _; _}, {expression_desc = Number (Int {i = _ ; _}) } ))
1135-
} as e), {expression_desc = Number (Int {i=0l} | Uint 0l | Nint 0n); _})
1127+
} as e), {expression_desc = Number (Int {i=0l} | Uint 0l ); _})
11361128
->
11371129
(* TODO: check correctness *)
11381130
is_out ?comment e range
@@ -1214,7 +1206,7 @@ let int32_asr ?comment e1 e2 : J.expression =
12141206
let int32_div ~checked ?comment
12151207
(e1 : t) (e2 : t) : t =
12161208
match e1.expression_desc, e2.expression_desc with
1217-
| Length _ , Number (Int {i = 2l} | Uint 2l | Nint 2n)
1209+
| Length _ , Number (Int {i = 2l} | Uint 2l )
12181210
-> int32_asr e1 one_int_literal
12191211
| e1_desc , Number (Int {i = i1} ) when i1 <> 0l
12201212
->
@@ -1277,10 +1269,10 @@ let int32_mul ?comment
12771269
(e1 : J.expression)
12781270
(e2 : J.expression) : J.expression =
12791271
match e1, e2 with
1280-
| {expression_desc = Number (Int {i = 0l}| Uint 0l | Nint 0n); _}, x
1272+
| {expression_desc = Number (Int {i = 0l}| Uint 0l ); _}, x
12811273
when Js_analyzer.no_side_effect_expression x ->
12821274
zero_int_literal
1283-
| x, {expression_desc = Number (Int {i = 0l}| Uint 0l | Nint 0n); _}
1275+
| x, {expression_desc = Number (Int {i = 0l}| Uint 0l ); _}
12841276
when Js_analyzer.no_side_effect_expression x ->
12851277
zero_int_literal
12861278
| {expression_desc = Number (Int{i = i0}); _}, {expression_desc = Number (Int {i = i1}); _}
@@ -1312,9 +1304,9 @@ let rec int32_bxor ?comment (e1 : t) (e2 : t) : J.expression =
13121304
match e1.expression_desc, e2.expression_desc with
13131305
| Number (Int {i = i1}), Number (Int {i = i2})
13141306
-> int ?comment (Int32.logxor i1 i2)
1315-
| _, (Bin(Lsr,e2, {expression_desc = Number(Int{i=0l} | Uint 0l | Nint 0n) ; _})) ->
1307+
| _, (Bin(Lsr,e2, {expression_desc = Number(Int{i=0l} | Uint 0l ) ; _})) ->
13161308
int32_bxor e1 e2
1317-
| (Bin(Lsr,e1, {expression_desc = Number(Int{i=0l} | Uint 0l | Nint 0n) ; _})), _ ->
1309+
| (Bin(Lsr,e1, {expression_desc = Number(Int{i=0l} | Uint 0l ) ; _})), _ ->
13181310
int32_bxor e1 e2
13191311

13201312
| _ ->

jscomp/core/js_exp_make.mli

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ val method_ : ?comment:string ->
123123
val econd : ?comment:string -> t -> t -> t -> t
124124

125125
val int : ?comment:string -> ?c:char -> int32 -> t
126-
val nint : ?comment:string -> nativeint -> t
127126
val small_int : int -> t
128127
val float : ?comment:string -> string -> t
129128

jscomp/core/js_op.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ type number =
170170
| Float of float_lit
171171
| Int of int_or_char
172172
| Uint of int32
173-
| Nint of nativeint
173+
174174
(* becareful when constant folding +/-,
175175
since we treat it as js nativeint, bitwise operators:
176176
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators

jscomp/core/lam.ml

Lines changed: 25 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -534,10 +534,7 @@ let prim ~primitive:(prim : Lam_primitive.t) ~args loc : t =
534534
(* | Pnegbint Pnativeint, ( (Const_nativeint i)) *)
535535
(* -> *)
536536
(* Lift.nativeint (Nativeint.neg i) *)
537-
| Pnegbint Pint32, Const_int {i = a}
538-
->
539-
Lift.int (Int32.neg a)
540-
| Pnegbint Pint64, Const_int64 a
537+
| Pnegint64, Const_int64 a
541538
->
542539
Lift.int64 (Int64.neg a)
543540
| Pnot, Const_js_true -> false_
@@ -548,12 +545,10 @@ let prim ~primitive:(prim : Lam_primitive.t) ~args loc : t =
548545

549546
| [Lconst a ; Lconst b] ->
550547
begin match prim, a, b with
551-
| (Pbintcomp(_, cmp) | Pintcomp (cmp)), (Const_int {i = a}), (Const_int {i = b})
552-
-> Lift.bool (Lam_compat.cmp_int32 cmp a b)
553-
| Pbintcomp(_, cmp), (Const_int64 a), (Const_int64 b)
548+
| Pint64comp cmp, (Const_int64 a), (Const_int64 b)
554549
-> Lift.bool (Lam_compat.cmp_int64 cmp a b)
555-
| Pbintcomp(_, cmp), (Const_nativeint a), (Const_nativeint b)
556-
-> Lift.bool (Lam_compat.cmp_nativeint cmp a b)
550+
| Pintcomp cmp, (Const_int a), (Const_int b)
551+
-> Lift.bool (Lam_compat.cmp_int32 cmp a.i b.i)
557552
| Pfloatcomp cmp, (Const_float a), (Const_float b)
558553
-> (** FIXME: could raise? *)
559554
Lift.bool (Lam_compat.cmp_float cmp (float_of_string a) (float_of_string b))
@@ -596,61 +591,34 @@ let prim ~primitive:(prim : Lam_primitive.t) ~args loc : t =
596591
| Plsrint -> int_ (Int32.shift_right_logical aa (Int32.to_int bb))
597592
| Pasrint -> int_ (Int32.shift_right aa (Int32.to_int bb))
598593
| _ -> default ()
599-
end
600-
| (Paddbint Pint32
601-
| Psubbint Pint32
602-
| Pmulbint Pint32
603-
| Pdivbint Pint32
604-
| Pmodbint Pint32
605-
| Pandbint Pint32
606-
| Porbint Pint32
607-
| Pxorbint Pint32
608-
), (Const_int {i = aa}), (Const_int {i = bb})
609-
->
610-
begin match prim with
611-
| Paddbint _ -> Lift.int (Int32.add aa bb)
612-
| Psubbint _ -> Lift.int (Int32.sub aa bb)
613-
| Pmulbint _ -> Lift.int (Int32.mul aa bb)
614-
| Pdivbint _ -> (try Lift.int (Int32.div aa bb) with _ -> default ())
615-
| Pmodbint _ -> (try Lift.int (Int32.rem aa bb) with _ -> default ())
616-
| Pandbint _ -> Lift.int (Int32.logand aa bb)
617-
| Porbint _ -> Lift.int (Int32.logor aa bb)
618-
| Pxorbint _ -> Lift.int (Int32.logxor aa bb)
619-
| _ -> default ()
620-
end
621-
| Plslbint Pint32, (Const_int {i = aa}), (Const_int {i = b})
622-
-> Lift.int (Int32.shift_left aa (Int32.to_int b ))
623-
| Plsrbint Pint32, (Const_int {i = aa}), (Const_int {i = b})
624-
-> Lift.int (Int32.shift_right_logical aa (Int32.to_int b ))
625-
| Pasrbint Pint32, (Const_int {i = aa}), (Const_int {i = b})
626-
-> Lift.int (Int32.shift_right aa (Int32.to_int b ))
627-
628-
| (Paddbint Pint64
629-
| Psubbint Pint64
630-
| Pmulbint Pint64
631-
| Pdivbint Pint64
632-
| Pmodbint Pint64
633-
| Pandbint Pint64
634-
| Porbint Pint64
635-
| Pxorbint Pint64
594+
end
595+
596+
| (Paddint64
597+
| Psubint64
598+
| Pmulint64
599+
| Pdivint64
600+
| Pmodint64
601+
| Pandint64
602+
| Porint64
603+
| Pxorint64
636604
), (Const_int64 aa), (Const_int64 bb)
637605
->
638606
begin match prim with
639-
| Paddbint _ -> Lift.int64 (Int64.add aa bb)
640-
| Psubbint _ -> Lift.int64 (Int64.sub aa bb)
641-
| Pmulbint _ -> Lift.int64 (Int64.mul aa bb)
642-
| Pdivbint _ -> (try Lift.int64 (Int64.div aa bb) with _ -> default ())
643-
| Pmodbint _ -> (try Lift.int64 (Int64.rem aa bb) with _ -> default ())
644-
| Pandbint _ -> Lift.int64 (Int64.logand aa bb)
645-
| Porbint _ -> Lift.int64 (Int64.logor aa bb)
646-
| Pxorbint _ -> Lift.int64 (Int64.logxor aa bb)
607+
| Paddint64 -> Lift.int64 (Int64.add aa bb)
608+
| Psubint64 -> Lift.int64 (Int64.sub aa bb)
609+
| Pmulint64 -> Lift.int64 (Int64.mul aa bb)
610+
| Pdivint64 -> (try Lift.int64 (Int64.div aa bb) with _ -> default ())
611+
| Pmodint64 -> (try Lift.int64 (Int64.rem aa bb) with _ -> default ())
612+
| Pandint64 -> Lift.int64 (Int64.logand aa bb)
613+
| Porint64 -> Lift.int64 (Int64.logor aa bb)
614+
| Pxorint64 -> Lift.int64 (Int64.logxor aa bb)
647615
| _ -> default ()
648616
end
649-
| Plslbint Pint64, (Const_int64 aa), (Const_int {i = b})
617+
| Plslint64, (Const_int64 aa), (Const_int {i = b})
650618
-> Lift.int64 (Int64.shift_left aa (Int32.to_int b ))
651-
| Plsrbint Pint64, (Const_int64 aa), (Const_int {i = b})
619+
| Plsrint64, (Const_int64 aa), (Const_int {i = b})
652620
-> Lift.int64 (Int64.shift_right_logical aa (Int32.to_int b ))
653-
| Pasrbint Pint64, (Const_int64 aa), (Const_int {i = b})
621+
| Pasrint64, (Const_int64 aa), (Const_int {i = b})
654622
-> Lift.int64 (Int64.shift_right aa (Int32.to_int b ))
655623

656624
| Psequand, Const_js_false,
@@ -744,8 +712,6 @@ let rec eval_const_as_bool (v : Lam_constant.t ) : bool =
744712
Char.code x <> 0
745713
| (Const_int64 x) ->
746714
x <> 0L
747-
| (Const_nativeint x) ->
748-
x <> 0n
749715
| Const_js_false
750716
| Const_js_null
751717
| Const_module_alias

jscomp/core/lam_analysis.ml

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ let not_zero_constant ( x : Lam_constant.t) =
2828
match x with
2929
| Const_int {i } -> i <> 0l
3030
| Const_int64 i -> i <> 0L
31-
| Const_nativeint i -> i <> 0n
3231
| _ -> false
3332

3433

@@ -72,8 +71,8 @@ let rec no_side_effects (lam : Lam.t) : bool =
7271
end
7372
| Pmodint
7473
| Pdivint
75-
| Pdivbint _
76-
| Pmodbint _
74+
| Pdivint64
75+
| Pmodint64
7776
-> begin match args with
7877
| [_ ; Lconst cst ] -> not_zero_constant cst
7978
| _ -> false
@@ -134,20 +133,19 @@ let rec no_side_effects (lam : Lam.t) : bool =
134133
| Pis_poly_var_const
135134
(* Test if the (integer) argument is outside an interval *)
136135
| Pisout _
137-
| Pbintofint _
138-
| Pintofbint _
139-
| Pcvtbint _
140-
| Pnegbint _
141-
| Paddbint _
142-
| Psubbint _
143-
| Pmulbint _
144-
| Pandbint _
145-
| Porbint _
146-
| Pxorbint _
147-
| Plslbint _
148-
| Plsrbint _
149-
| Pasrbint _
150-
| Pbintcomp _
136+
| Pint64ofint
137+
| Pintofint64
138+
| Pnegint64
139+
| Paddint64
140+
| Psubint64
141+
| Pmulint64
142+
| Pandint64
143+
| Porint64
144+
| Pxorint64
145+
| Plslint64
146+
| Plsrint64
147+
| Pasrint64
148+
| Pint64comp _
151149
(* Operations on big arrays: (unsafe, #dimensions, kind, layout) *)
152150

153151
(* Compile time constants *)
@@ -275,7 +273,6 @@ and size_constant x =
275273
| Const_int _ | Const_char _
276274

277275
| Const_float _ | Const_int64 _
278-
| Const_nativeint _
279276
| Const_pointer _
280277
| Const_js_null | Const_js_undefined | Const_module_alias
281278
| Const_js_true | Const_js_false

jscomp/core/lam_compile_const.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ and translate (x : Lam_constant.t ) : J.expression =
8080
(* E.float (Int64.to_string i) *)
8181
Js_long.of_const i
8282
(* https://github.com/google/closure-library/blob/master/closure%2Fgoog%2Fmath%2Flong.js *)
83-
| Const_nativeint i -> E.nint i
83+
8484
| Const_float f -> E.float f (* TODO: preserve float *)
8585
| Const_string i (*TODO: here inline js*) ->
8686
E.str i

0 commit comments

Comments
 (0)