Skip to content

Commit dbb507f

Browse files
committed
Compiler: comsume hints for boxed int comparison
1 parent 28b7611 commit dbb507f

File tree

2 files changed

+54
-35
lines changed

2 files changed

+54
-35
lines changed

compiler/lib/parse_bytecode.ml

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1999,11 +1999,40 @@ and compile infos pc state (instrs : instr list) =
19991999
y
20002000
Var.print
20012001
z;
2002+
let prim, y, z =
2003+
match Config.target () with
2004+
| `Wasm -> Extern prim, y, z
2005+
| `JavaScript -> (
2006+
match prim with
2007+
| "caml_equal"
2008+
| "caml_notequal"
2009+
| "caml_lessthan"
2010+
| "caml_greaterthan"
2011+
| "caml_lessequal"
2012+
| "caml_greaterequal" -> (
2013+
let prim_of_ext = function
2014+
| "caml_equal" -> Eq, y, z
2015+
| "caml_notequal" -> Neq, y, z
2016+
| "caml_lessthan" -> Lt, y, z
2017+
| "caml_lessequal" -> Le, y, z
2018+
| "caml_greaterequal" -> Le, z, y
2019+
| "caml_greaterthan" -> Lt, z, y
2020+
| _ -> assert false
2021+
in
2022+
match Hints.find infos.hints pc with
2023+
| [ Hints.Hint_int boxed ] -> (
2024+
match boxed with
2025+
| Pnativeint -> prim_of_ext prim
2026+
| Pint32 -> prim_of_ext prim
2027+
| Pint64 -> Extern prim, y, z)
2028+
| _ -> Extern prim, y, z)
2029+
| _ -> Extern prim, y, z)
2030+
in
20022031
compile
20032032
infos
20042033
(pc + 2)
20052034
(State.pop 1 state)
2006-
(Let (x, Prim (Extern prim, [ Pv y; Pv z ])) :: instrs)
2035+
(Let (x, Prim (prim, [ Pv y; Pv z ])) :: instrs)
20072036
| C_CALL3 ->
20082037
let prim = primitive_name state (getu code (pc + 1)) in
20092038
let y = State.accu state in

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

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9085,11 +9085,8 @@
90859085
"use strict";
90869086
var
90879087
runtime = globalThis.jsoo_runtime,
9088-
caml_greaterequal = runtime.caml_greaterequal,
90899088
caml_hash = runtime.caml_hash,
90909089
caml_int_compare = runtime.caml_int_compare,
9091-
caml_lessequal = runtime.caml_lessequal,
9092-
caml_lessthan = runtime.caml_lessthan,
90939090
caml_maybe_attach_backtrace = runtime.caml_maybe_attach_backtrace,
90949091
caml_mul = runtime.caml_mul,
90959092
caml_wrap_exception = runtime.caml_wrap_exception,
@@ -9102,7 +9099,7 @@
91029099
function succ(n){ /*<<int32.ml:48:21>>*/ return n + 1 | 0;}
91039100
function pred(n){ /*<<int32.ml:49:21>>*/ return n - 1 | 0;}
91049101
function abs(n){
9105-
/*<<int32.ml:50:15>>*/ return caml_greaterequal(n, 0) ? n : - n | 0 /*<<int32.ml:50:40>>*/ ;
9102+
/*<<int32.ml:50:22>>*/ return 0 <= n ? n : - n | 0 /*<<int32.ml:50:40>>*/ ;
91069103
}
91079104
function lognot(n){ /*<<int32.ml:53:29>>*/ return n ^ -1;}
91089105
var
@@ -9116,9 +9113,7 @@
91169113
max_int$0 = /*<<int32.ml:58:20>>*/ Stdlib[19],
91179114
unsigned_to_int =
91189115
/*<<int32.ml:59:6>>*/ function(n){
9119-
/*<<int32.ml:60:11>>*/ if
9120-
(caml_greaterequal(n, 0)
9121-
&& /*<<int32.ml:60:22>>*/ caml_lessequal(n, max_int$0))
9116+
/*<<int32.ml:60:18>>*/ if(0 <= n && n <= max_int$0)
91229117
/*<<int32.ml:61:10>>*/ return [0, n];
91239118
/*<<int32.ml:63:10>>*/ return 0;
91249119
/*<<int32.ml:63:14>>*/ };
@@ -9146,25 +9141,27 @@
91469141
/*<<?>>*/ throw caml_maybe_attach_backtrace(exn, 0);
91479142
}
91489143
/*<<int32.ml:78:24>>*/ }
9149-
var compare = /*<<?>>*/ caml_int_compare, equal = runtime.caml_equal;
9144+
var compare = /*<<?>>*/ caml_int_compare;
9145+
function equal(x, y){ /*<<int32.ml:83:31>>*/ return x === y ? 1 : 0;}
91509146
function unsigned_compare(n, m){
91519147
var
91529148
y = /*<<int32.ml:86:26>>*/ m + 2147483648 | 0,
91539149
x = /*<<int32.ml:86:10>>*/ n + 2147483648 | 0;
91549150
/*<<int32.ml:82:28>>*/ return caml_int_compare(x, y) /*<<int32.ml:86:41>>*/ ;
91559151
}
91569152
function unsigned_lt(n, m){
9157-
/*<<int32.ml:89:2>>*/ return caml_lessthan
9158-
(n + 2147483648 | 0, m + 2147483648 | 0) /*<<int32.ml:89:31>>*/ ;
9153+
/*<<int32.ml:89:31>>*/ return (n + 2147483648 | 0) < (m + 2147483648 | 0)
9154+
? 1
9155+
: 0;
91599156
}
91609157
function min(x, y){
9161-
/*<<int32.ml:91:21>>*/ return caml_lessequal(x, y) ? x : y /*<<int32.ml:91:41>>*/ ;
9158+
/*<<int32.ml:91:27>>*/ return x <= y ? x : y /*<<int32.ml:91:41>>*/ ;
91629159
}
91639160
function max(x, y){
9164-
/*<<int32.ml:92:21>>*/ return caml_greaterequal(x, y) ? x : y /*<<int32.ml:92:41>>*/ ;
9161+
/*<<int32.ml:92:27>>*/ return y <= x ? x : y /*<<int32.ml:92:41>>*/ ;
91659162
}
91669163
function unsigned_div(n, d){
9167-
/*<<int32.ml:98:5>>*/ if(caml_lessthan(d, 0))
9164+
/*<<int32.ml:98:13>>*/ if(d < 0)
91689165
/*<<int32.ml:99:7>>*/ return unsigned_lt(n, d) ? zero : one /*<<int32.ml:103:41>>*/ ;
91699166
var
91709167
q = /*<<int32.ml:101:23>>*/ runtime.caml_div(n >>> 1 | 0, d) << 1,
@@ -9370,11 +9367,8 @@
93709367
"use strict";
93719368
var
93729369
runtime = globalThis.jsoo_runtime,
9373-
caml_greaterequal = runtime.caml_greaterequal,
93749370
caml_hash = runtime.caml_hash,
93759371
caml_int_compare = runtime.caml_int_compare,
9376-
caml_lessequal = runtime.caml_lessequal,
9377-
caml_lessthan = runtime.caml_lessthan,
93789372
caml_maybe_attach_backtrace = runtime.caml_maybe_attach_backtrace,
93799373
caml_mul = runtime.caml_mul,
93809374
caml_wrap_exception = runtime.caml_wrap_exception,
@@ -9386,7 +9380,7 @@
93869380
function succ(n){ /*<<nativeint.ml:44:21>>*/ return n + 1 | 0;}
93879381
function pred(n){ /*<<nativeint.ml:45:21>>*/ return n - 1 | 0;}
93889382
function abs(n){
9389-
/*<<nativeint.ml:46:15>>*/ return caml_greaterequal(n, 0) ? n : - n | 0 /*<<nativeint.ml:46:40>>*/ ;
9383+
/*<<nativeint.ml:46:22>>*/ return 0 <= n ? n : - n | 0 /*<<nativeint.ml:46:40>>*/ ;
93909384
}
93919385
var
93929386
size = /*<<?>>*/ Stdlib_Sys[9],
@@ -9395,9 +9389,7 @@
93959389
function lognot(n){ /*<<nativeint.ml:50:29>>*/ return n ^ -1;}
93969390
var max_int$0 = /*<<nativeint.ml:53:16>>*/ Stdlib[19];
93979391
function unsigned_to_int(n){
9398-
/*<<nativeint.ml:55:7>>*/ if
9399-
(caml_greaterequal(n, 0)
9400-
&& /*<<nativeint.ml:55:18>>*/ caml_lessequal(n, max_int$0))
9392+
/*<<nativeint.ml:55:14>>*/ if(0 <= n && n <= max_int$0)
94019393
/*<<nativeint.ml:56:6>>*/ return [0, n];
94029394
/*<<nativeint.ml:58:6>>*/ return 0;
94039395
/*<<nativeint.ml:58:10>>*/ }
@@ -9426,17 +9418,18 @@
94269418
/*<<nativeint.ml:71:28>>*/ return caml_int_compare(x, y) /*<<nativeint.ml:75:41>>*/ ;
94279419
}
94289420
function unsigned_lt(n, m){
9429-
/*<<nativeint.ml:78:2>>*/ return caml_lessthan
9430-
(n - min_int | 0, m - min_int | 0) /*<<nativeint.ml:78:31>>*/ ;
9421+
/*<<nativeint.ml:78:31>>*/ return (n - min_int | 0) < (m - min_int | 0)
9422+
? 1
9423+
: 0;
94319424
}
94329425
function min(x, y){
9433-
/*<<nativeint.ml:80:21>>*/ return caml_lessequal(x, y) ? x : y /*<<nativeint.ml:80:41>>*/ ;
9426+
/*<<nativeint.ml:80:27>>*/ return x <= y ? x : y /*<<nativeint.ml:80:41>>*/ ;
94349427
}
94359428
function max(x, y){
9436-
/*<<nativeint.ml:81:21>>*/ return caml_greaterequal(x, y) ? x : y /*<<nativeint.ml:81:41>>*/ ;
9429+
/*<<nativeint.ml:81:27>>*/ return y <= x ? x : y /*<<nativeint.ml:81:41>>*/ ;
94379430
}
94389431
function unsigned_div(n, d){
9439-
/*<<nativeint.ml:87:5>>*/ if(caml_lessthan(d, 0))
9432+
/*<<nativeint.ml:87:13>>*/ if(d < 0)
94409433
/*<<nativeint.ml:88:7>>*/ return unsigned_lt(n, d) ? zero : one /*<<nativeint.ml:92:41>>*/ ;
94419434
var
94429435
q = /*<<nativeint.ml:90:23>>*/ runtime.caml_div(n >>> 1 | 0, d) << 1,
@@ -24016,34 +24009,31 @@
2401624009
var
2401724010
r = /*<<random.ml:227:38>>*/ bits32(s) >>> 1 | 0,
2401824011
v = /*<<random.ml:228:12>>*/ caml_mod(r, n);
24019-
/*<<random.ml:230:14>>*/ if
24020-
(! caml_greaterthan(r - v | 0, (Stdlib_Int32[9] - n | 0) + 1 | 0))
24012+
/*<<random.ml:230:46>>*/ if
24013+
(((Stdlib_Int32[9] - n | 0) + 1 | 0) >= (r - v | 0))
2402124014
/*<<random.ml:232:9>>*/ return v;
2402224015
}
2402324016
/*<<random.ml:232:10>>*/ }
2402424017
function int32(s, bound){
24025-
/*<<random.ml:235:7>>*/ return caml_lessequal(bound, 0)
24018+
/*<<random.ml:235:18>>*/ return bound <= 0
2402624019
? /*<<random.ml:236:9>>*/ Stdlib[1].call(null, cst_Random_int32)
2402724020
: /*<<random.ml:237:9>>*/ int32aux(s, bound) /*<<random.ml:237:25>>*/ ;
2402824021
}
2402924022
function int32_in_range(s, min, max){
24030-
/*<<random.ml:246:7>>*/ if(caml_greaterthan(min, max))
24023+
/*<<random.ml:246:16>>*/ if(max < min)
2403124024
/*<<random.ml:247:6>>*/ return Stdlib[1].call
2403224025
(null, cst_Random_int32_in_range) /*<<random.ml:254:39>>*/ ;
2403324026
var
2403424027
span =
2403524028
/*<<random.ml:249:17>>*/ Stdlib_Int32[6].call(null, max - min | 0);
24036-
/*<<random.ml:251:9>>*/ if(! caml_lessequal(span, Stdlib_Int32[1]))
24029+
/*<<random.ml:251:27>>*/ if(span > Stdlib_Int32[1])
2403724030
/*<<random.ml:254:22>>*/ return min + int32aux(s, span) | 0 /*<<random.ml:254:39>>*/ ;
2403824031
/*<<random.ml:251:27>>*/ for(;;){
2403924032
var
2404024033
r =
2404124034
/*<<random.ml:242:27>>*/ /*<<random.ml:242:12>>*/ caml_int64_to_int32
2404224035
( /*<<random.ml:242:27>>*/ caml_lxm_next(s));
24043-
/*<<random.ml:243:7>>*/ if
24044-
(!
24045-
caml_lessthan(r, min)
24046-
&& ! /*<<random.ml:243:18>>*/ caml_greaterthan(r, max))
24036+
/*<<random.ml:243:14>>*/ if(r >= min && max >= r)
2404724037
/*<<random.ml:243:67>>*/ return r;
2404824038
}
2404924039
/*<<random.ml:254:39>>*/ }

0 commit comments

Comments
 (0)