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

Commit f641145

Browse files
authored
Merge pull request #170 from ngzhian/v128-type-encode
Add encoding for v128 type and v128.const
2 parents 7efcf94 + 7440719 commit f641145

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

interpreter/binary/encode.ml

+4-3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ let encode m =
6262
let vs32 i = vs64 (Int64.of_int32 i)
6363
let f32 x = u32 (F32.to_bits x)
6464
let f64 x = u64 (F64.to_bits x)
65+
let v128 v = String.iter (put s) (V128.to_bits v)
6566

6667
let len i =
6768
if Int32.to_int (Int32.of_int i) <> i then
@@ -95,7 +96,7 @@ let encode m =
9596
| I64Type -> vs7 (-0x02)
9697
| F32Type -> vs7 (-0x03)
9798
| F64Type -> vs7 (-0x04)
98-
| V128Type -> failwith "TODO v128"
99+
| V128Type -> vs7 (-0x05)
99100

100101
let elem_type = function
101102
| FuncRefType -> vs7 (-0x10)
@@ -134,6 +135,7 @@ let encode m =
134135
open Memory
135136

136137
let op n = u8 n
138+
let simd_op n = op 0xfd; op n
137139
let end_ () = op 0x0b
138140

139141
let memop {align; offset; _} = vu32 (Int32.of_int align); vu32 offset
@@ -224,8 +226,7 @@ let encode m =
224226
| Const {it = I64 c; _} -> op 0x42; vs64 c
225227
| Const {it = F32 c; _} -> op 0x43; f32 c
226228
| Const {it = F64 c; _} -> op 0x44; f64 c
227-
| Const {it = V128 c; _} ->
228-
failwith "TODO v128"
229+
| Const {it = V128 c; _} -> simd_op 0x02; v128 c
229230

230231
| Test (I32 I32Op.Eqz) -> op 0x45
231232
| Test (I64 I64Op.Eqz) -> op 0x50

interpreter/exec/simd.ml

+6-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ sig
2020
val to_string : t -> string
2121
val bytewidth : int
2222
val of_strings : shape -> string list -> t
23+
val of_bits : string -> t
24+
val to_bits : t -> string
2325
end
2426

2527
module type S =
@@ -33,14 +35,14 @@ sig
3335
val of_strings : shape -> string list -> t
3436
end
3537

36-
module Make (Rep : RepType) : S with type bits = Rep.t =
38+
module Make (Rep : RepType) : S with type bits = string =
3739
struct
3840
type t = Rep.t
39-
type bits = Rep.t
41+
type bits = string
4042

4143
let default = Rep.make Rep.bytewidth (chr 0)
4244
let to_string = Rep.to_string (* FIXME very very wrong *)
43-
let to_bits x = x
44-
let of_bits x = x
45+
let of_bits = Rep.of_bits
46+
let to_bits = Rep.to_bits
4547
let of_strings = Rep.of_strings
4648
end

interpreter/exec/v128.ml

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ include Simd.Make
22
(struct
33
include Bytes
44
let bytewidth = 16
5+
let of_bits = Bytes.of_string
6+
let to_bits = Bytes.to_string
57

68
let of_strings shape ss =
79
if List.length ss <> Simd.lanes shape then raise (Invalid_argument "wrong length");

0 commit comments

Comments
 (0)