From 4e0325e3f49a8334edc49c6272432c64a08a5e6f Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Fri, 25 Oct 2024 23:24:30 +0200 Subject: [PATCH] Compiler: comsume hints for boxed int comparison --- compiler/lib/parse_bytecode.ml | 28 ++++++++++- compiler/tests-full/stdlib.cma.expected.js | 58 +++++++++------------- 2 files changed, 51 insertions(+), 35 deletions(-) diff --git a/compiler/lib/parse_bytecode.ml b/compiler/lib/parse_bytecode.ml index cfd2fe7499..e53eaba7a1 100644 --- a/compiler/lib/parse_bytecode.ml +++ b/compiler/lib/parse_bytecode.ml @@ -1917,11 +1917,37 @@ and compile infos pc state (instrs : instr list) = y Var.print z; + let prim, y, z = + match prim with + | "caml_equal" + | "caml_notequal" + | "caml_lessthan" + | "caml_greaterthan" + | "caml_lessequal" + | "caml_greaterequal" -> ( + let prim_of_ext = function + | "caml_equal" -> Eq, y, z + | "caml_notequal" -> Neq, y, z + | "caml_lessthan" -> Lt, y, z + | "caml_lessequal" -> Le, y, z + | "caml_greaterequal" -> Le, z, y + | "caml_greaterthan" -> Lt, z, y + | _ -> assert false + in + match Hints.find infos.hints pc with + | [ Hints.Hint_int boxed ] -> ( + match boxed with + | Pnativeint -> prim_of_ext prim + | Pint32 -> prim_of_ext prim + | Pint64 -> Extern prim, y, z) + | _ -> Extern prim, y, z) + | _ -> Extern prim, y, z + in compile infos (pc + 2) (State.pop 1 state) - (Let (x, Prim (Extern prim, [ Pv y; Pv z ])) :: instrs) + (Let (x, Prim (prim, [ Pv y; Pv z ])) :: instrs) | C_CALL3 -> let prim = primitive_name state (getu code (pc + 1)) in let y = State.accu state in diff --git a/compiler/tests-full/stdlib.cma.expected.js b/compiler/tests-full/stdlib.cma.expected.js index 395d0731e5..f1180ac7b2 100644 --- a/compiler/tests-full/stdlib.cma.expected.js +++ b/compiler/tests-full/stdlib.cma.expected.js @@ -8992,11 +8992,8 @@ "use strict"; var runtime = globalThis.jsoo_runtime, - caml_greaterequal = runtime.caml_greaterequal, caml_hash = runtime.caml_hash, caml_int_compare = runtime.caml_int_compare, - caml_lessequal = runtime.caml_lessequal, - caml_lessthan = runtime.caml_lessthan, caml_maybe_attach_backtrace = runtime.caml_maybe_attach_backtrace, caml_mul = runtime.caml_mul, caml_wrap_exception = runtime.caml_wrap_exception, @@ -9009,7 +9006,7 @@ function succ(n){ /*<>*/ return n + 1 | 0;} function pred(n){ /*<>*/ return n - 1 | 0;} function abs(n){ - /*<>*/ return caml_greaterequal(n, 0) ? n : - n | 0 /*<>*/ ; + /*<>*/ return 0 <= n ? n : - n | 0 /*<>*/ ; } function lognot(n){ /*<>*/ return n ^ -1;} var @@ -9023,9 +9020,7 @@ max_int$0 = /*<>*/ Stdlib[19], unsigned_to_int = /*<>*/ function(n){ - /*<>*/ if - (caml_greaterequal(n, 0) - && /*<>*/ caml_lessequal(n, max_int$0)) + /*<>*/ if(0 <= n && n <= max_int$0) /*<>*/ return [0, n]; /*<>*/ return 0; /*<>*/ }; @@ -9053,7 +9048,8 @@ /*<>*/ throw caml_maybe_attach_backtrace(_b_, 0); } /*<>*/ } - var compare = /*<>*/ caml_int_compare, equal = runtime.caml_equal; + var compare = /*<>*/ caml_int_compare; + function equal(x, y){ /*<>*/ return x === y ? 1 : 0;} function unsigned_compare(n, m){ var y = /*<>*/ m + 2147483648 | 0, @@ -9061,17 +9057,18 @@ /*<>*/ return caml_int_compare(x, y) /*<>*/ ; } function unsigned_lt(n, m){ - /*<>*/ return caml_lessthan - (n + 2147483648 | 0, m + 2147483648 | 0) /*<>*/ ; + /*<>*/ return (n + 2147483648 | 0) < (m + 2147483648 | 0) + ? 1 + : 0; } function min(x, y){ - /*<>*/ return caml_lessequal(x, y) ? x : y /*<>*/ ; + /*<>*/ return x <= y ? x : y /*<>*/ ; } function max(x, y){ - /*<>*/ return caml_greaterequal(x, y) ? x : y /*<>*/ ; + /*<>*/ return y <= x ? x : y /*<>*/ ; } function unsigned_div(n, d){ - /*<>*/ if(caml_lessthan(d, 0)) + /*<>*/ if(d < 0) /*<>*/ return unsigned_lt(n, d) ? zero : one /*<>*/ ; var q = /*<>*/ runtime.caml_div(n >>> 1 | 0, d) << 1, @@ -9276,11 +9273,8 @@ "use strict"; var runtime = globalThis.jsoo_runtime, - caml_greaterequal = runtime.caml_greaterequal, caml_hash = runtime.caml_hash, caml_int_compare = runtime.caml_int_compare, - caml_lessequal = runtime.caml_lessequal, - caml_lessthan = runtime.caml_lessthan, caml_maybe_attach_backtrace = runtime.caml_maybe_attach_backtrace, caml_mul = runtime.caml_mul, caml_wrap_exception = runtime.caml_wrap_exception, @@ -9292,7 +9286,7 @@ function succ(n){ /*<>*/ return n + 1 | 0;} function pred(n){ /*<>*/ return n - 1 | 0;} function abs(n){ - /*<>*/ return caml_greaterequal(n, 0) ? n : - n | 0 /*<>*/ ; + /*<>*/ return 0 <= n ? n : - n | 0 /*<>*/ ; } var size = /*<>*/ Stdlib_Sys[9], @@ -9301,9 +9295,7 @@ function lognot(n){ /*<>*/ return n ^ -1;} var max_int$0 = /*<>*/ Stdlib[19]; function unsigned_to_int(n){ - /*<>*/ if - (caml_greaterequal(n, 0) - && /*<>*/ caml_lessequal(n, max_int$0)) + /*<>*/ if(0 <= n && n <= max_int$0) /*<>*/ return [0, n]; /*<>*/ return 0; /*<>*/ } @@ -9332,17 +9324,18 @@ /*<>*/ return caml_int_compare(x, y) /*<>*/ ; } function unsigned_lt(n, m){ - /*<>*/ return caml_lessthan - (n - min_int | 0, m - min_int | 0) /*<>*/ ; + /*<>*/ return (n - min_int | 0) < (m - min_int | 0) + ? 1 + : 0; } function min(x, y){ - /*<>*/ return caml_lessequal(x, y) ? x : y /*<>*/ ; + /*<>*/ return x <= y ? x : y /*<>*/ ; } function max(x, y){ - /*<>*/ return caml_greaterequal(x, y) ? x : y /*<>*/ ; + /*<>*/ return y <= x ? x : y /*<>*/ ; } function unsigned_div(n, d){ - /*<>*/ if(caml_lessthan(d, 0)) + /*<>*/ if(d < 0) /*<>*/ return unsigned_lt(n, d) ? zero : one /*<>*/ ; var q = /*<>*/ runtime.caml_div(n >>> 1 | 0, d) << 1, @@ -24924,31 +24917,28 @@ var r = bits32(s) >>> 1 | 0, v = /*<>*/ caml_mod(r, n); - /*<>*/ if - (! caml_greaterthan(r - v | 0, (Stdlib_Int32[9] - n | 0) + 1 | 0)) + /*<>*/ if + (((Stdlib_Int32[9] - n | 0) + 1 | 0) >= (r - v | 0)) /*<>*/ return v; } /*<>*/ } function int32(s, bound){ - /*<>*/ return caml_lessequal(bound, 0) + /*<>*/ return bound <= 0 ? /*<>*/ (0, Stdlib[1])(cst_Random_int32) : /*<>*/ int32aux(s, bound) /*<>*/ ; } function int32_in_range(s, min, max){ - /*<>*/ if(caml_greaterthan(min, max)) + /*<>*/ if(max < min) /*<>*/ return (0, Stdlib[1])(cst_Random_int32_in_range) /*<>*/ ; var span = /*<>*/ (0, Stdlib_Int32[6])(max - min | 0); - /*<>*/ if(! caml_lessequal(span, Stdlib_Int32[1])) + /*<>*/ if(span > Stdlib_Int32[1]) /*<>*/ return min + int32aux(s, span) | 0 /*<>*/ ; /*<>*/ for(;;){ var r = /*<>*/ /*<>*/ caml_int64_to_int32 ( /*<>*/ caml_lxm_next(s)); - /*<>*/ if - (! - caml_lessthan(r, min) - && ! /*<>*/ caml_greaterthan(r, max)) + /*<>*/ if(r >= min && max >= r) /*<>*/ return r; } /*<>*/ }