Skip to content

Commit ddaec59

Browse files
committed
Compiler: comsume hints for boxed int comparison
1 parent b7eb470 commit ddaec59

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
@@ -9110,11 +9110,8 @@
91109110
"use strict";
91119111
var
91129112
runtime = globalThis.jsoo_runtime,
9113-
caml_greaterequal = runtime.caml_greaterequal,
91149113
caml_hash = runtime.caml_hash,
91159114
caml_int_compare = runtime.caml_int_compare,
9116-
caml_lessequal = runtime.caml_lessequal,
9117-
caml_lessthan = runtime.caml_lessthan,
91189115
caml_maybe_attach_backtrace = runtime.caml_maybe_attach_backtrace,
91199116
caml_mul = runtime.caml_mul,
91209117
caml_wrap_exception = runtime.caml_wrap_exception,
@@ -9127,7 +9124,7 @@
91279124
function succ(n){ /*<<int32.ml:48:21>>*/ return n + 1 | 0;}
91289125
function pred(n){ /*<<int32.ml:49:21>>*/ return n - 1 | 0;}
91299126
function abs(n){
9130-
/*<<int32.ml:50:15>>*/ return caml_greaterequal(n, 0) ? n : - n | 0 /*<<int32.ml:50:40>>*/ ;
9127+
/*<<int32.ml:50:22>>*/ return 0 <= n ? n : - n | 0 /*<<int32.ml:50:40>>*/ ;
91319128
}
91329129
function lognot(n){ /*<<int32.ml:53:29>>*/ return n ^ -1;}
91339130
var
@@ -9141,9 +9138,7 @@
91419138
max_int$0 = /*<<int32.ml:58:20>>*/ Stdlib[19],
91429139
unsigned_to_int =
91439140
/*<<int32.ml:59:6>>*/ function(n){
9144-
/*<<int32.ml:60:11>>*/ if
9145-
(caml_greaterequal(n, 0)
9146-
&& /*<<int32.ml:60:22>>*/ caml_lessequal(n, max_int$0))
9141+
/*<<int32.ml:60:18>>*/ if(0 <= n && n <= max_int$0)
91479142
/*<<int32.ml:61:10>>*/ return [0, n];
91489143
/*<<int32.ml:63:10>>*/ return 0;
91499144
/*<<int32.ml:63:14>>*/ };
@@ -9171,25 +9166,27 @@
91719166
/*<<?>>*/ throw caml_maybe_attach_backtrace(exn, 0);
91729167
}
91739168
/*<<int32.ml:78:24>>*/ }
9174-
var compare = /*<<?>>*/ caml_int_compare, equal = runtime.caml_equal;
9169+
var compare = /*<<?>>*/ caml_int_compare;
9170+
function equal(x, y){ /*<<int32.ml:83:31>>*/ return x === y ? 1 : 0;}
91759171
function unsigned_compare(n, m){
91769172
var
91779173
y = /*<<int32.ml:86:26>>*/ m + 2147483648 | 0,
91789174
x = /*<<int32.ml:86:10>>*/ n + 2147483648 | 0;
91799175
/*<<int32.ml:82:28>>*/ return caml_int_compare(x, y) /*<<int32.ml:86:41>>*/ ;
91809176
}
91819177
function unsigned_lt(n, m){
9182-
/*<<int32.ml:89:2>>*/ return caml_lessthan
9183-
(n + 2147483648 | 0, m + 2147483648 | 0) /*<<int32.ml:89:31>>*/ ;
9178+
/*<<int32.ml:89:31>>*/ return (n + 2147483648 | 0) < (m + 2147483648 | 0)
9179+
? 1
9180+
: 0;
91849181
}
91859182
function min(x, y){
9186-
/*<<int32.ml:91:21>>*/ return caml_lessequal(x, y) ? x : y /*<<int32.ml:91:41>>*/ ;
9183+
/*<<int32.ml:91:27>>*/ return x <= y ? x : y /*<<int32.ml:91:41>>*/ ;
91879184
}
91889185
function max(x, y){
9189-
/*<<int32.ml:92:21>>*/ return caml_greaterequal(x, y) ? x : y /*<<int32.ml:92:41>>*/ ;
9186+
/*<<int32.ml:92:27>>*/ return y <= x ? x : y /*<<int32.ml:92:41>>*/ ;
91909187
}
91919188
function unsigned_div(n, d){
9192-
/*<<int32.ml:98:5>>*/ if(caml_lessthan(d, 0))
9189+
/*<<int32.ml:98:13>>*/ if(d < 0)
91939190
/*<<int32.ml:99:7>>*/ return unsigned_lt(n, d) ? zero : one /*<<int32.ml:103:41>>*/ ;
91949191
var
91959192
q = /*<<int32.ml:101:23>>*/ runtime.caml_div(n >>> 1 | 0, d) << 1,
@@ -9395,11 +9392,8 @@
93959392
"use strict";
93969393
var
93979394
runtime = globalThis.jsoo_runtime,
9398-
caml_greaterequal = runtime.caml_greaterequal,
93999395
caml_hash = runtime.caml_hash,
94009396
caml_int_compare = runtime.caml_int_compare,
9401-
caml_lessequal = runtime.caml_lessequal,
9402-
caml_lessthan = runtime.caml_lessthan,
94039397
caml_maybe_attach_backtrace = runtime.caml_maybe_attach_backtrace,
94049398
caml_mul = runtime.caml_mul,
94059399
caml_wrap_exception = runtime.caml_wrap_exception,
@@ -9411,7 +9405,7 @@
94119405
function succ(n){ /*<<nativeint.ml:44:21>>*/ return n + 1 | 0;}
94129406
function pred(n){ /*<<nativeint.ml:45:21>>*/ return n - 1 | 0;}
94139407
function abs(n){
9414-
/*<<nativeint.ml:46:15>>*/ return caml_greaterequal(n, 0) ? n : - n | 0 /*<<nativeint.ml:46:40>>*/ ;
9408+
/*<<nativeint.ml:46:22>>*/ return 0 <= n ? n : - n | 0 /*<<nativeint.ml:46:40>>*/ ;
94159409
}
94169410
var
94179411
size = /*<<?>>*/ Stdlib_Sys[9],
@@ -9420,9 +9414,7 @@
94209414
function lognot(n){ /*<<nativeint.ml:50:29>>*/ return n ^ -1;}
94219415
var max_int$0 = /*<<nativeint.ml:53:16>>*/ Stdlib[19];
94229416
function unsigned_to_int(n){
9423-
/*<<nativeint.ml:55:7>>*/ if
9424-
(caml_greaterequal(n, 0)
9425-
&& /*<<nativeint.ml:55:18>>*/ caml_lessequal(n, max_int$0))
9417+
/*<<nativeint.ml:55:14>>*/ if(0 <= n && n <= max_int$0)
94269418
/*<<nativeint.ml:56:6>>*/ return [0, n];
94279419
/*<<nativeint.ml:58:6>>*/ return 0;
94289420
/*<<nativeint.ml:58:10>>*/ }
@@ -9451,17 +9443,18 @@
94519443
/*<<nativeint.ml:71:28>>*/ return caml_int_compare(x, y) /*<<nativeint.ml:75:41>>*/ ;
94529444
}
94539445
function unsigned_lt(n, m){
9454-
/*<<nativeint.ml:78:2>>*/ return caml_lessthan
9455-
(n - min_int | 0, m - min_int | 0) /*<<nativeint.ml:78:31>>*/ ;
9446+
/*<<nativeint.ml:78:31>>*/ return (n - min_int | 0) < (m - min_int | 0)
9447+
? 1
9448+
: 0;
94569449
}
94579450
function min(x, y){
9458-
/*<<nativeint.ml:80:21>>*/ return caml_lessequal(x, y) ? x : y /*<<nativeint.ml:80:41>>*/ ;
9451+
/*<<nativeint.ml:80:27>>*/ return x <= y ? x : y /*<<nativeint.ml:80:41>>*/ ;
94599452
}
94609453
function max(x, y){
9461-
/*<<nativeint.ml:81:21>>*/ return caml_greaterequal(x, y) ? x : y /*<<nativeint.ml:81:41>>*/ ;
9454+
/*<<nativeint.ml:81:27>>*/ return y <= x ? x : y /*<<nativeint.ml:81:41>>*/ ;
94629455
}
94639456
function unsigned_div(n, d){
9464-
/*<<nativeint.ml:87:5>>*/ if(caml_lessthan(d, 0))
9457+
/*<<nativeint.ml:87:13>>*/ if(d < 0)
94659458
/*<<nativeint.ml:88:7>>*/ return unsigned_lt(n, d) ? zero : one /*<<nativeint.ml:92:41>>*/ ;
94669459
var
94679460
q = /*<<nativeint.ml:90:23>>*/ runtime.caml_div(n >>> 1 | 0, d) << 1,
@@ -23943,34 +23936,31 @@
2394323936
var
2394423937
r = /*<<random.ml:227:38>>*/ bits32(s) >>> 1 | 0,
2394523938
v = /*<<random.ml:228:12>>*/ caml_mod(r, n);
23946-
/*<<random.ml:230:14>>*/ if
23947-
(! caml_greaterthan(r - v | 0, (Stdlib_Int32[9] - n | 0) + 1 | 0))
23939+
/*<<random.ml:230:46>>*/ if
23940+
(((Stdlib_Int32[9] - n | 0) + 1 | 0) >= (r - v | 0))
2394823941
/*<<random.ml:232:9>>*/ return v;
2394923942
}
2395023943
/*<<random.ml:232:10>>*/ }
2395123944
function int32(s, bound){
23952-
/*<<random.ml:235:7>>*/ return caml_lessequal(bound, 0)
23945+
/*<<random.ml:235:18>>*/ return bound <= 0
2395323946
? /*<<random.ml:236:9>>*/ Stdlib[1].call(null, cst_Random_int32)
2395423947
: /*<<random.ml:237:9>>*/ int32aux(s, bound) /*<<random.ml:237:25>>*/ ;
2395523948
}
2395623949
function int32_in_range(s, min, max){
23957-
/*<<random.ml:246:7>>*/ if(caml_greaterthan(min, max))
23950+
/*<<random.ml:246:16>>*/ if(max < min)
2395823951
/*<<random.ml:247:6>>*/ return Stdlib[1].call
2395923952
(null, cst_Random_int32_in_range) /*<<random.ml:254:39>>*/ ;
2396023953
var
2396123954
span =
2396223955
/*<<random.ml:249:17>>*/ Stdlib_Int32[6].call(null, max - min | 0);
23963-
/*<<random.ml:251:9>>*/ if(! caml_lessequal(span, Stdlib_Int32[1]))
23956+
/*<<random.ml:251:27>>*/ if(span > Stdlib_Int32[1])
2396423957
/*<<random.ml:254:22>>*/ return min + int32aux(s, span) | 0 /*<<random.ml:254:39>>*/ ;
2396523958
/*<<random.ml:251:27>>*/ for(;;){
2396623959
var
2396723960
r =
2396823961
/*<<random.ml:242:27>>*/ /*<<random.ml:242:12>>*/ caml_int64_to_int32
2396923962
( /*<<random.ml:242:27>>*/ caml_lxm_next(s));
23970-
/*<<random.ml:243:7>>*/ if
23971-
(!
23972-
caml_lessthan(r, min)
23973-
&& ! /*<<random.ml:243:18>>*/ caml_greaterthan(r, max))
23963+
/*<<random.ml:243:14>>*/ if(r >= min && max >= r)
2397423964
/*<<random.ml:243:67>>*/ return r;
2397523965
}
2397623966
/*<<random.ml:254:39>>*/ }

0 commit comments

Comments
 (0)