Skip to content

Commit 2ca7dbf

Browse files
committed
Compiler: comsume hints for boxed int comparison
1 parent 536ff4d commit 2ca7dbf

File tree

2 files changed

+51
-35
lines changed

2 files changed

+51
-35
lines changed

compiler/lib/parse_bytecode.ml

+27-1
Original file line numberDiff line numberDiff line change
@@ -1906,11 +1906,37 @@ and compile infos pc state (instrs : instr list) =
19061906
y
19071907
Var.print
19081908
z;
1909+
let prim, y, z =
1910+
match prim with
1911+
| "caml_equal"
1912+
| "caml_notequal"
1913+
| "caml_lessthan"
1914+
| "caml_greaterthan"
1915+
| "caml_lessequal"
1916+
| "caml_greaterequal" -> (
1917+
let prim_of_ext = function
1918+
| "caml_equal" -> Eq, y, z
1919+
| "caml_notequal" -> Neq, y, z
1920+
| "caml_lessthan" -> Lt, y, z
1921+
| "caml_lessequal" -> Le, y, z
1922+
| "caml_greaterequal" -> Le, z, y
1923+
| "caml_greaterthan" -> Lt, z, y
1924+
| _ -> assert false
1925+
in
1926+
match Hints.find infos.hints pc with
1927+
| [ Hints.Hint_int boxed ] -> (
1928+
match boxed with
1929+
| Pnativeint -> prim_of_ext prim
1930+
| Pint32 -> prim_of_ext prim
1931+
| Pint64 -> Extern prim, y, z)
1932+
| _ -> Extern prim, y, z)
1933+
| _ -> Extern prim, y, z
1934+
in
19091935
compile
19101936
infos
19111937
(pc + 2)
19121938
(State.pop 1 state)
1913-
(Let (x, Prim (Extern prim, [ Pv y; Pv z ])) :: instrs)
1939+
(Let (x, Prim (prim, [ Pv y; Pv z ])) :: instrs)
19141940
| C_CALL3 ->
19151941
let prim = primitive_name state (getu code (pc + 1)) in
19161942
let y = State.accu state in

compiler/tests-full/stdlib.cma.expected.js

+24-34
Original file line numberDiff line numberDiff line change
@@ -8992,11 +8992,8 @@
89928992
"use strict";
89938993
var
89948994
runtime = globalThis.jsoo_runtime,
8995-
caml_greaterequal = runtime.caml_greaterequal,
89968995
caml_hash = runtime.caml_hash,
89978996
caml_int_compare = runtime.caml_int_compare,
8998-
caml_lessequal = runtime.caml_lessequal,
8999-
caml_lessthan = runtime.caml_lessthan,
90008997
caml_maybe_attach_backtrace = runtime.caml_maybe_attach_backtrace,
90018998
caml_mul = runtime.caml_mul,
90028999
caml_wrap_exception = runtime.caml_wrap_exception,
@@ -9009,7 +9006,7 @@
90099006
function succ(n){ /*<<int32.ml:48:21>>*/ return n + 1 | 0;}
90109007
function pred(n){ /*<<int32.ml:49:21>>*/ return n - 1 | 0;}
90119008
function abs(n){
9012-
/*<<int32.ml:50:15>>*/ return caml_greaterequal(n, 0) ? n : - n | 0 /*<<int32.ml:50:40>>*/ ;
9009+
/*<<int32.ml:50:22>>*/ return 0 <= n ? n : - n | 0 /*<<int32.ml:50:40>>*/ ;
90139010
}
90149011
function lognot(n){ /*<<int32.ml:53:29>>*/ return n ^ -1;}
90159012
var
@@ -9023,9 +9020,7 @@
90239020
max_int$0 = /*<<int32.ml:58:6>>*/ Stdlib[19],
90249021
unsigned_to_int =
90259022
/*<<int32.ml:59:6>>*/ function(n){
9026-
/*<<int32.ml:60:11>>*/ if
9027-
(caml_greaterequal(n, 0)
9028-
&& /*<<int32.ml:60:22>>*/ caml_lessequal(n, max_int$0))
9023+
/*<<int32.ml:60:18>>*/ if(0 <= n && n <= max_int$0)
90299024
/*<<int32.ml:61:10>>*/ return [0, n];
90309025
/*<<int32.ml:63:10>>*/ return 0;
90319026
/*<<int32.ml:63:14>>*/ };
@@ -9053,25 +9048,27 @@
90539048
/*<<?>>*/ throw caml_maybe_attach_backtrace(_b_, 0);
90549049
}
90559050
/*<<int32.ml:78:24>>*/ }
9056-
var compare = /*<<?>>*/ caml_int_compare, equal = runtime.caml_equal;
9051+
var compare = /*<<?>>*/ caml_int_compare;
9052+
function equal(x, y){ /*<<int32.ml:83:31>>*/ return x === y ? 1 : 0;}
90579053
function unsigned_compare(n, m){
90589054
var
90599055
y = /*<<int32.ml:86:26>>*/ m + 2147483648 | 0,
90609056
x = /*<<int32.ml:86:10>>*/ n + 2147483648 | 0;
90619057
/*<<int32.ml:82:28>>*/ return caml_int_compare(x, y) /*<<int32.ml:86:41>>*/ ;
90629058
}
90639059
function unsigned_lt(n, m){
9064-
/*<<int32.ml:89:2>>*/ return caml_lessthan
9065-
(n + 2147483648 | 0, m + 2147483648 | 0) /*<<int32.ml:89:31>>*/ ;
9060+
/*<<int32.ml:89:31>>*/ return (n + 2147483648 | 0) < (m + 2147483648 | 0)
9061+
? 1
9062+
: 0;
90669063
}
90679064
function min(x, y){
9068-
/*<<int32.ml:91:21>>*/ return caml_lessequal(x, y) ? x : y /*<<int32.ml:91:41>>*/ ;
9065+
/*<<int32.ml:91:27>>*/ return x <= y ? x : y /*<<int32.ml:91:41>>*/ ;
90699066
}
90709067
function max(x, y){
9071-
/*<<int32.ml:92:21>>*/ return caml_greaterequal(x, y) ? x : y /*<<int32.ml:92:41>>*/ ;
9068+
/*<<int32.ml:92:27>>*/ return y <= x ? x : y /*<<int32.ml:92:41>>*/ ;
90729069
}
90739070
function unsigned_div(n, d){
9074-
/*<<int32.ml:98:5>>*/ if(caml_lessthan(d, 0))
9071+
/*<<int32.ml:98:13>>*/ if(d < 0)
90759072
/*<<int32.ml:99:7>>*/ return unsigned_lt(n, d) ? zero : one /*<<int32.ml:103:41>>*/ ;
90769073
var
90779074
q = /*<<int32.ml:101:23>>*/ runtime.caml_div(n >>> 1 | 0, d) << 1,
@@ -9276,11 +9273,8 @@
92769273
"use strict";
92779274
var
92789275
runtime = globalThis.jsoo_runtime,
9279-
caml_greaterequal = runtime.caml_greaterequal,
92809276
caml_hash = runtime.caml_hash,
92819277
caml_int_compare = runtime.caml_int_compare,
9282-
caml_lessequal = runtime.caml_lessequal,
9283-
caml_lessthan = runtime.caml_lessthan,
92849278
caml_maybe_attach_backtrace = runtime.caml_maybe_attach_backtrace,
92859279
caml_mul = runtime.caml_mul,
92869280
caml_wrap_exception = runtime.caml_wrap_exception,
@@ -9292,7 +9286,7 @@
92929286
function succ(n){ /*<<nativeint.ml:44:21>>*/ return n + 1 | 0;}
92939287
function pred(n){ /*<<nativeint.ml:45:21>>*/ return n - 1 | 0;}
92949288
function abs(n){
9295-
/*<<nativeint.ml:46:15>>*/ return caml_greaterequal(n, 0) ? n : - n | 0 /*<<nativeint.ml:46:40>>*/ ;
9289+
/*<<nativeint.ml:46:22>>*/ return 0 <= n ? n : - n | 0 /*<<nativeint.ml:46:40>>*/ ;
92969290
}
92979291
var
92989292
size = /*<<?>>*/ Stdlib_Sys[9],
@@ -9301,9 +9295,7 @@
93019295
function lognot(n){ /*<<nativeint.ml:50:29>>*/ return n ^ -1;}
93029296
var max_int$0 = /*<<nativeint.ml:49:28>>*/ Stdlib[19];
93039297
function unsigned_to_int(n){
9304-
/*<<nativeint.ml:55:7>>*/ if
9305-
(caml_greaterequal(n, 0)
9306-
&& /*<<nativeint.ml:55:18>>*/ caml_lessequal(n, max_int$0))
9298+
/*<<nativeint.ml:55:14>>*/ if(0 <= n && n <= max_int$0)
93079299
/*<<nativeint.ml:56:6>>*/ return [0, n];
93089300
/*<<nativeint.ml:58:6>>*/ return 0;
93099301
/*<<nativeint.ml:58:10>>*/ }
@@ -9332,17 +9324,18 @@
93329324
/*<<nativeint.ml:71:28>>*/ return caml_int_compare(x, y) /*<<nativeint.ml:75:41>>*/ ;
93339325
}
93349326
function unsigned_lt(n, m){
9335-
/*<<nativeint.ml:78:2>>*/ return caml_lessthan
9336-
(n - min_int | 0, m - min_int | 0) /*<<nativeint.ml:78:31>>*/ ;
9327+
/*<<nativeint.ml:78:31>>*/ return (n - min_int | 0) < (m - min_int | 0)
9328+
? 1
9329+
: 0;
93379330
}
93389331
function min(x, y){
9339-
/*<<nativeint.ml:80:21>>*/ return caml_lessequal(x, y) ? x : y /*<<nativeint.ml:80:41>>*/ ;
9332+
/*<<nativeint.ml:80:27>>*/ return x <= y ? x : y /*<<nativeint.ml:80:41>>*/ ;
93409333
}
93419334
function max(x, y){
9342-
/*<<nativeint.ml:81:21>>*/ return caml_greaterequal(x, y) ? x : y /*<<nativeint.ml:81:41>>*/ ;
9335+
/*<<nativeint.ml:81:27>>*/ return y <= x ? x : y /*<<nativeint.ml:81:41>>*/ ;
93439336
}
93449337
function unsigned_div(n, d){
9345-
/*<<nativeint.ml:87:5>>*/ if(caml_lessthan(d, 0))
9338+
/*<<nativeint.ml:87:13>>*/ if(d < 0)
93469339
/*<<nativeint.ml:88:7>>*/ return unsigned_lt(n, d) ? zero : one /*<<nativeint.ml:92:41>>*/ ;
93479340
var
93489341
q = /*<<nativeint.ml:90:23>>*/ runtime.caml_div(n >>> 1 | 0, d) << 1,
@@ -24924,31 +24917,28 @@
2492424917
var
2492524918
r = bits32(s) >>> 1 | 0,
2492624919
v = /*<<random.ml:228:12>>*/ caml_mod(r, n);
24927-
/*<<random.ml:230:14>>*/ if
24928-
(! caml_greaterthan(r - v | 0, (Stdlib_Int32[9] - n | 0) + 1 | 0))
24920+
/*<<random.ml:230:46>>*/ if
24921+
(((Stdlib_Int32[9] - n | 0) + 1 | 0) >= (r - v | 0))
2492924922
/*<<random.ml:232:9>>*/ return v;
2493024923
}
2493124924
/*<<random.ml:232:10>>*/ }
2493224925
function int32(s, bound){
24933-
/*<<random.ml:235:7>>*/ return caml_lessequal(bound, 0)
24926+
/*<<random.ml:235:18>>*/ return bound <= 0
2493424927
? /*<<random.ml:236:9>>*/ (0, Stdlib[1])(cst_Random_int32)
2493524928
: /*<<random.ml:237:9>>*/ int32aux(s, bound) /*<<random.ml:237:25>>*/ ;
2493624929
}
2493724930
function int32_in_range(s, min, max){
24938-
/*<<random.ml:246:7>>*/ if(caml_greaterthan(min, max))
24931+
/*<<random.ml:246:16>>*/ if(max < min)
2493924932
/*<<random.ml:247:6>>*/ return (0, Stdlib[1])(cst_Random_int32_in_range) /*<<random.ml:254:39>>*/ ;
2494024933
var span = /*<<random.ml:249:17>>*/ (0, Stdlib_Int32[6])(max - min | 0);
24941-
/*<<random.ml:251:9>>*/ if(! caml_lessequal(span, Stdlib_Int32[1]))
24934+
/*<<random.ml:251:27>>*/ if(span > Stdlib_Int32[1])
2494224935
/*<<random.ml:254:22>>*/ return min + int32aux(s, span) | 0 /*<<random.ml:254:39>>*/ ;
2494324936
/*<<random.ml:252:8>>*/ for(;;){
2494424937
var
2494524938
r =
2494624939
/*<<random.ml:242:27>>*/ /*<<random.ml:242:12>>*/ caml_int64_to_int32
2494724940
( /*<<random.ml:242:27>>*/ caml_lxm_next(s));
24948-
/*<<random.ml:243:7>>*/ if
24949-
(!
24950-
caml_lessthan(r, min)
24951-
&& ! /*<<random.ml:243:18>>*/ caml_greaterthan(r, max))
24941+
/*<<random.ml:243:14>>*/ if(r >= min && max >= r)
2495224942
/*<<random.ml:243:67>>*/ return r;
2495324943
}
2495424944
/*<<random.ml:254:39>>*/ }

0 commit comments

Comments
 (0)