Skip to content

Commit 6eae0ef

Browse files
authored
Merge pull request #3906 from BuckleScript/fix_int64_on_node
remove optimizaitons relying on int64 internals, which is fragile when switching to bigint
2 parents e0c6b74 + 8daabff commit 6eae0ef

File tree

8 files changed

+189
-129
lines changed

8 files changed

+189
-129
lines changed

jscomp/core/js_long.ml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ let make ~lo ~hi =
5353
~comment:"int64" (E.zero_int_literal)
5454
record_info [ hi; E.to_uint32 lo ]
5555
Immutable
56-
let get_lo x = E.array_index_by_int x 1l
57-
let get_hi x = E.array_index_by_int x 0l
56+
57+
5858

5959

6060
(* below should not depend on layout *)
@@ -66,7 +66,9 @@ let of_const (v : Int64.t) =
6666
~hi:(Int64.to_int32 (Int64.shift_right v 32))
6767

6868
let to_int32 args =
69-
E.to_int32 @@ get_lo (Ext_list.singleton_exn args)
69+
int64_call "to_int32" args
70+
(* let get_lo x = E.array_index_by_int x 1l *)
71+
(* E.to_int32 @@ get_lo (Ext_list.singleton_exn args) *)
7072

7173

7274
let of_int32 (args : J.expression list) =
@@ -108,15 +110,19 @@ let div args =
108110
current pipe-line fall back to a function call
109111
*)
110112
let bit_op (op : E.t -> E.t -> E.t) runtime_call args =
111-
match args with
113+
int64_call runtime_call args
114+
(*disable optimizations relying on int64 representations
115+
this maybe outdated when we switch to bigint
116+
*)
117+
(* match args with
112118
| [l;r] ->
113119
(* Int64 is a block in ocaml, a little more conservative in inlining *)
114120
if Js_analyzer.is_okay_to_duplicate l &&
115121
Js_analyzer.is_okay_to_duplicate r then
116122
make ~lo:(op (get_lo l) (get_lo r))
117123
~hi:(op (get_hi l) (get_hi r))
118-
else int64_call runtime_call args
119-
| _ -> assert false
124+
else
125+
| _ -> assert false *)
120126

121127
let xor = bit_op E.int32_bxor "xor"
122128
let or_ = bit_op E.int32_bor "or_"

jscomp/test/int64_test.js

Lines changed: 134 additions & 96 deletions
Large diffs are not rendered by default.

jscomp/test/int64_test.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,14 @@ let suites : Mt.pair_suites = Mt.[
124124
));
125125
"mul simple", (fun _ -> Eq (6L, mul 3L 2L ));
126126
"of_int32", (fun _ ->
127-
Eq(Array.map Int64.of_int32 [|0l;(-2147483648l)|], [|0L; (-2147483648L)|]));
127+
Eq(Array.map Int64.of_int32 [|0l;(-2147483648l)|], [|0L; (-2147483648L)|]);
128+
);
129+
"of_int32_singleton",(fun _ ->
130+
Eq ( Int64.of_int32 (-3l), -3L )
131+
) ;
132+
__LOC__ , (fun _ ->
133+
Eq (Int64.of_int32 3l , 3L)
134+
);
128135
"to_int32", (fun _ ->
129136
Eq(Array.map Int64.to_int32
130137
[|0L; 0x0000_0000_8000_0000L|], [|0l;-2147483648l|]));

jscomp/test/swap_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ var suites_64 = List.map((function (param) {
917917
])
918918
]),
919919
"swap64 %d"
920-
]), a[1] | 0),
920+
]), Caml_int64.to_int32(a)),
921921
(function (param) {
922922
return /* Eq */Block.__(0, [
923923
Caml_int64.swap(a),

lib/4.06.1/unstable/js_compiler.ml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117310,8 +117310,8 @@ let make ~lo ~hi =
117310117310
~comment:"int64" (E.zero_int_literal)
117311117311
record_info [ hi; E.to_uint32 lo ]
117312117312
Immutable
117313-
let get_lo x = E.array_index_by_int x 1l
117314-
let get_hi x = E.array_index_by_int x 0l
117313+
117314+
117315117315

117316117316

117317117317
(* below should not depend on layout *)
@@ -117323,7 +117323,9 @@ let of_const (v : Int64.t) =
117323117323
~hi:(Int64.to_int32 (Int64.shift_right v 32))
117324117324

117325117325
let to_int32 args =
117326-
E.to_int32 @@ get_lo (Ext_list.singleton_exn args)
117326+
int64_call "to_int32" args
117327+
(* let get_lo x = E.array_index_by_int x 1l *)
117328+
(* E.to_int32 @@ get_lo (Ext_list.singleton_exn args) *)
117327117329

117328117330

117329117331
let of_int32 (args : J.expression list) =
@@ -117365,15 +117367,19 @@ let div args =
117365117367
current pipe-line fall back to a function call
117366117368
*)
117367117369
let bit_op (op : E.t -> E.t -> E.t) runtime_call args =
117368-
match args with
117370+
int64_call runtime_call args
117371+
(*disable optimizations relying on int64 representations
117372+
this maybe outdated when we switch to bigint
117373+
*)
117374+
(* match args with
117369117375
| [l;r] ->
117370117376
(* Int64 is a block in ocaml, a little more conservative in inlining *)
117371117377
if Js_analyzer.is_okay_to_duplicate l &&
117372117378
Js_analyzer.is_okay_to_duplicate r then
117373117379
make ~lo:(op (get_lo l) (get_lo r))
117374117380
~hi:(op (get_hi l) (get_hi r))
117375-
else int64_call runtime_call args
117376-
| _ -> assert false
117381+
else
117382+
| _ -> assert false *)
117377117383

117378117384
let xor = bit_op E.int32_bxor "xor"
117379117385
let or_ = bit_op E.int32_bor "or_"

lib/4.06.1/whole_compiler.ml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108809,8 +108809,8 @@ let make ~lo ~hi =
108809108809
~comment:"int64" (E.zero_int_literal)
108810108810
record_info [ hi; E.to_uint32 lo ]
108811108811
Immutable
108812-
let get_lo x = E.array_index_by_int x 1l
108813-
let get_hi x = E.array_index_by_int x 0l
108812+
108813+
108814108814

108815108815

108816108816
(* below should not depend on layout *)
@@ -108822,7 +108822,9 @@ let of_const (v : Int64.t) =
108822108822
~hi:(Int64.to_int32 (Int64.shift_right v 32))
108823108823

108824108824
let to_int32 args =
108825-
E.to_int32 @@ get_lo (Ext_list.singleton_exn args)
108825+
int64_call "to_int32" args
108826+
(* let get_lo x = E.array_index_by_int x 1l *)
108827+
(* E.to_int32 @@ get_lo (Ext_list.singleton_exn args) *)
108826108828

108827108829

108828108830
let of_int32 (args : J.expression list) =
@@ -108864,15 +108866,19 @@ let div args =
108864108866
current pipe-line fall back to a function call
108865108867
*)
108866108868
let bit_op (op : E.t -> E.t -> E.t) runtime_call args =
108867-
match args with
108869+
int64_call runtime_call args
108870+
(*disable optimizations relying on int64 representations
108871+
this maybe outdated when we switch to bigint
108872+
*)
108873+
(* match args with
108868108874
| [l;r] ->
108869108875
(* Int64 is a block in ocaml, a little more conservative in inlining *)
108870108876
if Js_analyzer.is_okay_to_duplicate l &&
108871108877
Js_analyzer.is_okay_to_duplicate r then
108872108878
make ~lo:(op (get_lo l) (get_lo r))
108873108879
~hi:(op (get_hi l) (get_hi r))
108874-
else int64_call runtime_call args
108875-
| _ -> assert false
108880+
else
108881+
| _ -> assert false *)
108876108882

108877108883
let xor = bit_op E.int32_bxor "xor"
108878108884
let or_ = bit_op E.int32_bor "or_"

lib/js/caml_format.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -602,29 +602,29 @@ function caml_int64_format(fmt, x) {
602602
/* lo */0
603603
], match$1[0]);
604604
var modulus = match$1[1];
605-
s = String.fromCharCode(cvtbl.charCodeAt(modulus[1] | 0)) + s;
605+
s = String.fromCharCode(cvtbl.charCodeAt(Caml_int64.to_int32(modulus))) + s;
606606
while(Caml_int64.neq(quotient, /* int64 */[
607607
/* hi */0,
608608
/* lo */0
609609
])) {
610610
var match$2 = Caml_int64.div_mod(quotient, wbase);
611611
quotient = match$2[0];
612612
modulus = match$2[1];
613-
s = String.fromCharCode(cvtbl.charCodeAt(modulus[1] | 0)) + s;
613+
s = String.fromCharCode(cvtbl.charCodeAt(Caml_int64.to_int32(modulus))) + s;
614614
};
615615
} else {
616616
var match$3 = Caml_int64.div_mod(x$1, wbase);
617617
var quotient$1 = match$3[0];
618618
var modulus$1 = match$3[1];
619-
s = String.fromCharCode(cvtbl.charCodeAt(modulus$1[1] | 0)) + s;
619+
s = String.fromCharCode(cvtbl.charCodeAt(Caml_int64.to_int32(modulus$1))) + s;
620620
while(Caml_int64.neq(quotient$1, /* int64 */[
621621
/* hi */0,
622622
/* lo */0
623623
])) {
624624
var match$4 = Caml_int64.div_mod(quotient$1, wbase);
625625
quotient$1 = match$4[0];
626626
modulus$1 = match$4[1];
627-
s = String.fromCharCode(cvtbl.charCodeAt(modulus$1[1] | 0)) + s;
627+
s = String.fromCharCode(cvtbl.charCodeAt(Caml_int64.to_int32(modulus$1))) + s;
628628
};
629629
}
630630
break;
@@ -652,29 +652,29 @@ function caml_int64_format(fmt, x) {
652652
/* lo */3435973836
653653
], match$5[0]), match$6[0]);
654654
var modulus$2 = match$6[1];
655-
s = String.fromCharCode(cvtbl$1.charCodeAt(modulus$2[1] | 0)) + s;
655+
s = String.fromCharCode(cvtbl$1.charCodeAt(Caml_int64.to_int32(modulus$2))) + s;
656656
while(Caml_int64.neq(quotient$2, /* int64 */[
657657
/* hi */0,
658658
/* lo */0
659659
])) {
660660
var match$7 = Caml_int64.div_mod(quotient$2, wbase$1);
661661
quotient$2 = match$7[0];
662662
modulus$2 = match$7[1];
663-
s = String.fromCharCode(cvtbl$1.charCodeAt(modulus$2[1] | 0)) + s;
663+
s = String.fromCharCode(cvtbl$1.charCodeAt(Caml_int64.to_int32(modulus$2))) + s;
664664
};
665665
} else {
666666
var match$8 = Caml_int64.div_mod(x$1, wbase$1);
667667
var quotient$3 = match$8[0];
668668
var modulus$3 = match$8[1];
669-
s = String.fromCharCode(cvtbl$1.charCodeAt(modulus$3[1] | 0)) + s;
669+
s = String.fromCharCode(cvtbl$1.charCodeAt(Caml_int64.to_int32(modulus$3))) + s;
670670
while(Caml_int64.neq(quotient$3, /* int64 */[
671671
/* hi */0,
672672
/* lo */0
673673
])) {
674674
var match$9 = Caml_int64.div_mod(quotient$3, wbase$1);
675675
quotient$3 = match$9[0];
676676
modulus$3 = match$9[1];
677-
s = String.fromCharCode(cvtbl$1.charCodeAt(modulus$3[1] | 0)) + s;
677+
s = String.fromCharCode(cvtbl$1.charCodeAt(Caml_int64.to_int32(modulus$3))) + s;
678678
};
679679
}
680680
break;

lib/js/random.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,7 @@ function int64(s, bound) {
132132
var b1 = Caml_int64.of_int32(bits(s$1));
133133
var b2 = Caml_int64.lsl_(Caml_int64.of_int32(bits(s$1)), 30);
134134
var b3 = Caml_int64.lsl_(Caml_int64.of_int32(bits(s$1) & 7), 60);
135-
var r = Caml_int64.or_(b1, /* int64 */[
136-
/* hi */b2[0] | b3[0],
137-
/* lo */((b2[1] | b3[1]) >>> 0)
138-
]);
135+
var r = Caml_int64.or_(b1, Caml_int64.or_(b2, b3));
139136
var v = Caml_int64.mod_(r, n);
140137
if (Caml_int64.gt(Caml_int64.sub(r, v), Caml_int64.add(Caml_int64.sub(Int64.max_int, n), /* int64 */[
141138
/* hi */0,
@@ -149,7 +146,7 @@ function int64(s, bound) {
149146
}
150147

151148
var nativeint = Nativeint.size === 32 ? int32 : (function (s, bound) {
152-
return int64(s, Caml_int64.of_int32(bound))[1] | 0;
149+
return Caml_int64.to_int32(int64(s, Caml_int64.of_int32(bound)));
153150
});
154151

155152
function rawfloat(s) {

0 commit comments

Comments
 (0)