Skip to content
This repository was archived by the owner on Dec 22, 2021. It is now read-only.

Commit b7aea06

Browse files
authored
Fix to_hex_string for I8 and I16 (#320)
Print out the full i32 hex string, then extract the lower 2 or 4 chars.
1 parent e06fca2 commit b7aea06

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

interpreter/exec/i16.ml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ include Int.Make (struct
55
include Int32
66

77
let bitwidth = 16
8-
(* TODO incorrect for negative values. *)
9-
let to_hex_string = Printf.sprintf "%lx"
8+
let to_hex_string i =
9+
(* Always print the full 32-bits (8 hex characters), and pad with 0s.
10+
* Then only take the 4 least significant characters. *)
11+
let s = Printf.sprintf "%08lx" i in
12+
String.sub s ((String.length s) - 4) 4
1013

1114
let of_int64 = Int64.to_int32
1215
let to_int64 = Int64.of_int32

interpreter/exec/i8.ml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ include Int.Make (struct
55
include Int32
66

77
let bitwidth = 8
8-
(* TODO incorrect for negative values. *)
9-
let to_hex_string = Printf.sprintf "%lx"
8+
let to_hex_string i =
9+
(* Always print the full 32-bits (8 hex characters), and pad with 0s.
10+
* Then only take the 2 least significant characters. *)
11+
let s = Printf.sprintf "%08lx" i in
12+
String.sub s ((String.length s) - 2) 2
1013

1114
let of_int64 = Int64.to_int32
1215
let to_int64 = Int64.of_int32

interpreter/text/arrange.ml

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -594,18 +594,20 @@ let module_ = module_with_var_opt None
594594
(* Scripts *)
595595

596596
(* Converts a value to string depending on mode. *)
597-
let literal mode lit =
597+
let literal mode lit shape =
598598
let choose_mode bin not_bin = if mode = `Binary then bin else not_bin in
599-
match lit.it with
600-
| Values.I32 i -> choose_mode I32.to_hex_string I32.to_string_s i
601-
| Values.I64 i -> choose_mode I64.to_hex_string I64.to_string_s i
602-
| Values.F32 z -> choose_mode F32.to_hex_string F32.to_string z
603-
| Values.F64 z -> choose_mode F64.to_hex_string F64.to_string z
604-
| Values.V128 v -> choose_mode V128.to_hex_string V128.to_string v
599+
match lit.it, shape with
600+
| Values.I32 i, Some Simd.I8x16 -> choose_mode I8.to_hex_string I8.to_string_s i
601+
| Values.I32 i, Some Simd.I16x8 -> choose_mode I16.to_hex_string I16.to_string_s i
602+
| Values.I32 i, _ -> choose_mode I32.to_hex_string I32.to_string_s i
603+
| Values.I64 i, _ -> choose_mode I64.to_hex_string I64.to_string_s i
604+
| Values.F32 z, _ -> choose_mode F32.to_hex_string F32.to_string z
605+
| Values.F64 z, _ -> choose_mode F64.to_hex_string F64.to_string z
606+
| Values.V128 v, _ -> choose_mode V128.to_hex_string V128.to_string v
605607

606608
(* Converts a literal into a constant instruction. *)
607609
let constant mode lit =
608-
let lit_string = literal mode lit in
610+
let lit_string = literal mode lit None in
609611
Node (constop lit ^ lit_string, [])
610612

611613
let definition mode x_opt def =
@@ -661,7 +663,7 @@ let result_simd mode res shape pats =
661663
* a SimdResult do not need the i32.const instruction *)
662664
let num_pat mode res =
663665
match res.it with
664-
| LitPat lit -> literal mode lit
666+
| LitPat lit -> literal mode lit (Some shape)
665667
| NanPat {it = Values.F32 n; _}
666668
| NanPat {it = Values.F64 n; _} -> nan n
667669
| _ -> assert false

0 commit comments

Comments
 (0)