Skip to content

Commit 0fa114e

Browse files
committed
fixup! Don't noop conversion between ints/float with the same runtime repr. Previously, primitives taking float arguments could receive Int/Int32/NativeInt/Float arg
1 parent e146371 commit 0fa114e

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

compiler/lib/eval.ml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,21 @@ let eval_prim x =
8282
| Eq, [ Int i; Int j ] -> bool Targetint.(i = j)
8383
| Neq, [ Int i; Int j ] -> bool Targetint.(i <> j)
8484
| Ult, [ Int i; Int j ] -> bool (Targetint.(j < zero) || Targetint.(i < j))
85-
| Extern name, l -> (
86-
let name = Primitive.resolve name in
85+
| Extern name', l -> (
86+
let name = Primitive.resolve name' in
8787
match name, l with
88+
| "%identity-ints-repr", [ c ] -> (
89+
match name', c with
90+
| "caml_int32_of_int", Int x -> Some (Int32 (Targetint.to_int32 x))
91+
| "caml_int32_to_int", Int32 x -> Some (Int (Targetint.of_int32_exn x))
92+
| "caml_int32_to_float", Int32 x -> Some (Float (Int32.to_float x))
93+
| "caml_nativeint_of_int", Int x -> Some (NativeInt (Targetint.to_int32 x))
94+
| "caml_nativeint_to_int", NativeInt x -> Some (Int (Targetint.of_int32_exn x))
95+
| "caml_nativeint_to_float", NativeInt x -> Some (Float (Int32.to_float x))
96+
| "caml_nativeint_of_int32", Int32 x -> Some (NativeInt x)
97+
| "caml_nativeint_to_int32", NativeInt x -> Some (Int32 x)
98+
| "caml_float_of_int", Int x -> Some (Float (Targetint.to_float x))
99+
| _ -> assert false)
88100
(* int *)
89101
| "%int_add", _ -> int_binop l Targetint.add
90102
| "%int_sub", _ -> int_binop l Targetint.sub

compiler/lib/flow.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ let the_def_of info x =
344344
(fun x ->
345345
match info.info_defs.(Var.idx x) with
346346
| Expr (Constant (Float _ | Int _ | NativeString _) as e) -> Some e
347+
| Expr (Constant (Int32 _ | NativeInt _) as e) -> Some e
347348
| Expr (Constant (String _) as e) when Config.Flag.safe_string () -> Some e
348349
| Expr e -> if Var.ISet.mem info.info_possibly_mutable x then None else Some e
349350
| _ -> None)

compiler/lib/parse_bytecode.ml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ end
408408
module Constants : sig
409409
val parse : Obj.t -> Code.constant
410410

411-
val inlined : Code.constant -> bool
411+
val inlined : target:[ `JavaScript | `Wasm ] -> Code.constant -> bool
412412
end = struct
413413
(* In order to check that two custom objects share the same kind, we
414414
compare their identifier. The identifier is currently extracted
@@ -474,14 +474,18 @@ end = struct
474474
let i : int = Obj.magic x in
475475
Int (Targetint.of_int_warning_on_overflow i)
476476

477-
let inlined = function
477+
let inlined ~target c =
478+
match c with
478479
| String _ | NativeString _ -> false
479480
| Float _ -> true
480481
| Float_array _ -> false
481482
| Int64 _ -> false
482483
| Tuple _ -> false
483484
| Int _ -> true
484-
| Int32 _ | NativeInt _ -> false
485+
| Int32 _ | NativeInt _ -> (
486+
match target with
487+
| `JavaScript -> true
488+
| `Wasm -> false)
485489
end
486490

487491
let const32 i = Constant (Int (Targetint.of_int32_exn i))
@@ -744,14 +748,15 @@ let get_global state instrs i =
744748
if debug_parser () then Format.printf "(global access %a)@." Var.print x;
745749
x, State.set_accu state x, instrs
746750
| None -> (
747-
if i < Array.length g.constants && Constants.inlined g.constants.(i)
751+
let target = Config.target () in
752+
if i < Array.length g.constants && Constants.inlined ~target g.constants.(i)
748753
then
749754
(* Inlined constant *)
750755
let x, state = State.fresh_var state in
751756
let cst = g.constants.(i) in
752757
x, state, Let (x, Constant cst) :: instrs
753758
else
754-
match i < Array.length g.constants, Config.target () with
759+
match i < Array.length g.constants, target with
755760
| true, _ | false, `JavaScript ->
756761
(* Non-inlined constant, and reference to another compilation
757762
units in case of separate compilation (JavaScript).

0 commit comments

Comments
 (0)