From 00b7093ad57c7698ff7f6e69f59fd426ee341fbf Mon Sep 17 00:00:00 2001 From: Oscar Spencer Date: Fri, 2 Apr 2021 19:03:55 -0400 Subject: [PATCH] fix: Correct JS conversion of i64 & float64_bits literals (#96) Co-authored-by: Blaine Bublitz --- js/expression.ml | 9 ++++----- js/literal.ml | 12 ++++++++---- src/literal.ml | 4 ++-- test/test.expected | 2 ++ test/test.ml | 8 ++++++++ virtual/literal.mli | 4 ++-- 6 files changed, 26 insertions(+), 13 deletions(-) diff --git a/js/expression.ml b/js/expression.ml index 93e4be4..99e7d30 100644 --- a/js/expression.ml +++ b/js/expression.ml @@ -115,18 +115,17 @@ let const wasm_mod lit = | Int32 value -> let scope = get wasm_mod "i32" in meth_call scope "const" [| inject value |] - | Int64 value -> + | Int64 (low, high) -> let scope = get wasm_mod "i64" in - meth_call scope "const" [| inject value |] + meth_call scope "const" [| inject low; inject high |] | Float32Bits value -> let scope = get wasm_mod "f32" in meth_call scope "const_bits" [| inject value |] - | Float64Bits value -> + | Float64Bits (low, high) -> let scope = get wasm_mod "f64" in - meth_call scope "const_bits" [| inject value |] + meth_call scope "const_bits" [| inject low; inject high |] | Float32 value -> let scope = get wasm_mod "f32" in - (* TODO: Investigate if this needs the Int32 conversion stuff *) meth_call scope "const" [| inject value |] | Float64 value -> let scope = get wasm_mod "f64" in diff --git a/js/literal.ml b/js/literal.ml index ce0a2b0..3b2982b 100644 --- a/js/literal.ml +++ b/js/literal.ml @@ -1,9 +1,9 @@ (* This is a hack around Binaryen's stack allocations *) type jsoo = | Int32 of int32 - | Int64 of int64 + | Int64 of int32 * int32 | Float32Bits of int32 - | Float64Bits of int64 + | Float64Bits of int32 * int32 | Float32 of float | Float64 of float @@ -11,11 +11,15 @@ type t = jsoo let int32 value = Int32 value -let int64 value = Int64 value +let int64 value = + Int64 + (Int64.to_int32 value, Int64.to_int32 (Int64.shift_right_logical value 32)) let float32_bits value = Float32Bits value -let float64_bits value = Float64Bits value +let float64_bits value = + Float64Bits + (Int64.to_int32 value, Int64.to_int32 (Int64.shift_right_logical value 32)) let float32 value = Float32 value diff --git a/src/literal.ml b/src/literal.ml index d0eceb1..1bbb086 100644 --- a/src/literal.ml +++ b/src/literal.ml @@ -15,9 +15,9 @@ external float64 : float -> t = "caml_binaryen_literal_float64" (* Hacks for Binaryen.js stack allocations, Don't use in binaryen.native *) type jsoo = | Int32 of int32 - | Int64 of int64 + | Int64 of int32 * int32 | Float32Bits of int32 - | Float64Bits of int64 + | Float64Bits of int32 * int32 | Float32 of float | Float64 of float diff --git a/test/test.expected b/test/test.expected index 06ab911..7552448 100644 --- a/test/test.expected +++ b/test/test.expected @@ -4,6 +4,8 @@ (memory $0 1) (table $table 1 1 funcref) (elem (i32.const 0) $adder) + (global $max_int64 i64 (i64.const 9223372036854775807)) + (global $test_float64_bits f64 (f64.const 1.23)) (export "adder" (func $adder)) (export "memory" (memory $0)) (start $start) diff --git a/test/test.ml b/test/test.ml index d585427..174165d 100644 --- a/test/test.ml +++ b/test/test.ml @@ -48,6 +48,14 @@ let start = let _ = Export.add_function_export wasm_mod "adder" "adder" +let _ = + Global.add_global wasm_mod "max_int64" Type.int64 false + (Expression.const wasm_mod (Literal.int64 Int64.max_int)) + +let _ = + Global.add_global wasm_mod "test_float64_bits" Type.float64 false + (Expression.const wasm_mod (Literal.float64_bits 0x3FF3AE147AE147AEL)) + let _ = Table.add_table wasm_mod "table" 1 1 [ "adder" ] (Expression.const wasm_mod (Literal.int32 0l)) diff --git a/virtual/literal.mli b/virtual/literal.mli index ee3976f..2d05410 100644 --- a/virtual/literal.mli +++ b/virtual/literal.mli @@ -15,9 +15,9 @@ val float64 : float -> t (* Hacks for Binaryen.js stack allocations *) type jsoo = | Int32 of int32 - | Int64 of int64 + | Int64 of int32 * int32 | Float32Bits of int32 - | Float64Bits of int64 + | Float64Bits of int32 * int32 | Float32 of float | Float64 of float