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

Commit ac82e16

Browse files
authored
Merge pull request #138 from penzn/simd-interpreter
[Interpreter] infrastructure
2 parents 43de658 + 9ec00ce commit ac82e16

File tree

13 files changed

+165
-23
lines changed

13 files changed

+165
-23
lines changed

interpreter/binary/encode.ml

+16
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ let encode m =
9595
| I64Type -> vs7 (-0x02)
9696
| F32Type -> vs7 (-0x03)
9797
| F64Type -> vs7 (-0x04)
98+
| V128Type -> failwith "TODO v128"
9899

99100
let elem_type = function
100101
| FuncRefType -> vs7 (-0x10)
@@ -195,6 +196,10 @@ let encode m =
195196
op 0x35; memop mo
196197
| Load {ty = F32Type | F64Type; sz = Some _; _} ->
197198
assert false
199+
| Load {ty = V128Type; sz = None; _} ->
200+
failwith "TODO v128"
201+
| Load {ty = V128Type; sz = Some _; _} ->
202+
failwith "TODO v128"
198203

199204
| Store ({ty = I32Type; sz = None; _} as mo) -> op 0x36; memop mo
200205
| Store ({ty = I64Type; sz = None; _} as mo) -> op 0x37; memop mo
@@ -207,6 +212,10 @@ let encode m =
207212
| Store ({ty = I64Type; sz = Some Pack16; _} as mo) -> op 0x3d; memop mo
208213
| Store ({ty = I64Type; sz = Some Pack32; _} as mo) -> op 0x3e; memop mo
209214
| Store {ty = F32Type | F64Type; sz = Some _; _} -> assert false
215+
| Store {ty = V128Type; sz = None; _} ->
216+
failwith "TODO v128"
217+
| Store {ty = V128Type; sz = Some _; _} ->
218+
failwith "TODO v128"
210219

211220
| MemorySize -> op 0x3f; u8 0x00
212221
| MemoryGrow -> op 0x40; u8 0x00
@@ -215,11 +224,14 @@ let encode m =
215224
| Const {it = I64 c; _} -> op 0x42; vs64 c
216225
| Const {it = F32 c; _} -> op 0x43; f32 c
217226
| Const {it = F64 c; _} -> op 0x44; f64 c
227+
| Const {it = V128 c; _} ->
228+
failwith "TODO v128"
218229

219230
| Test (I32 I32Op.Eqz) -> op 0x45
220231
| Test (I64 I64Op.Eqz) -> op 0x50
221232
| Test (F32 _) -> assert false
222233
| Test (F64 _) -> assert false
234+
| Test (V128 _) -> assert false
223235

224236
| Compare (I32 I32Op.Eq) -> op 0x46
225237
| Compare (I32 I32Op.Ne) -> op 0x47
@@ -256,6 +268,7 @@ let encode m =
256268
| Compare (F64 F64Op.Gt) -> op 0x64
257269
| Compare (F64 F64Op.Le) -> op 0x65
258270
| Compare (F64 F64Op.Ge) -> op 0x66
271+
| Compare (V128 _) -> failwith "TODO v128"
259272

260273
| Unary (I32 I32Op.Clz) -> op 0x67
261274
| Unary (I32 I32Op.Ctz) -> op 0x68
@@ -280,6 +293,7 @@ let encode m =
280293
| Unary (F64 F64Op.Trunc) -> op 0x9d
281294
| Unary (F64 F64Op.Nearest) -> op 0x9e
282295
| Unary (F64 F64Op.Sqrt) -> op 0x9f
296+
| Unary (V128 _) -> failwith "TODO v128"
283297

284298
| Binary (I32 I32Op.Add) -> op 0x6a
285299
| Binary (I32 I32Op.Sub) -> op 0x6b
@@ -328,6 +342,7 @@ let encode m =
328342
| Binary (F64 F64Op.Min) -> op 0xa4
329343
| Binary (F64 F64Op.Max) -> op 0xa5
330344
| Binary (F64 F64Op.CopySign) -> op 0xa6
345+
| Binary (V128 _) -> failwith "TODO v128"
331346

332347
| Convert (I32 I32Op.ExtendSI32) -> assert false
333348
| Convert (I32 I32Op.ExtendUI32) -> assert false
@@ -362,6 +377,7 @@ let encode m =
362377
| Convert (F64 F64Op.PromoteF32) -> op 0xbb
363378
| Convert (F64 F64Op.DemoteF64) -> assert false
364379
| Convert (F64 F64Op.ReinterpretInt) -> op 0xbf
380+
| Convert (V128 _) -> failwith "TODO v128"
365381

366382
let const c =
367383
list instr c.it; end_ ()

interpreter/exec/eval_numeric.ml

+42-6
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,31 @@ end
116116
module F32Op = FloatOp (F32) (Values.F32Value)
117117
module F64Op = FloatOp (F64) (Values.F64Value)
118118

119+
(* Vector operators *)
120+
121+
module VectorOp (VXX : Vector.S) (Value : ValueType with type t = VXX.t) =
122+
struct
123+
(* TODO
124+
open Ast.VectorOp
125+
126+
let to_value = Value.to_value
127+
let of_value = of_arg Value.of_value
128+
*)
129+
130+
(* FIXME *)
131+
let unop op = failwith "TODO v128"
132+
133+
(* FIXME *)
134+
let binop op = failwith "TODO v128"
135+
136+
(* FIXME *)
137+
let testop op = failwith "TODO v128"
138+
139+
(* FIXME *)
140+
let relop op = failwith "TODO v128"
141+
end
142+
143+
module V128Op = VectorOp (V128) (Values.V128Value)
119144

120145
(* Conversion operators *)
121146

@@ -181,17 +206,28 @@ struct
181206
| DemoteF64 -> raise (TypeError (1, v, F64Type))
182207
end
183208

209+
module V128CvtOp =
210+
struct
211+
(* TODO
212+
open Ast.VectorOp
213+
*)
214+
215+
(* FIXME *)
216+
let cvtop op v = failwith "TODO v128"
217+
end
218+
184219

185220
(* Dispatch *)
186221

187-
let op i32 i64 f32 f64 = function
222+
let op i32 i64 f32 f64 v128 = function
188223
| I32 x -> i32 x
189224
| I64 x -> i64 x
190225
| F32 x -> f32 x
191226
| F64 x -> f64 x
227+
| V128 x -> v128 x
192228

193-
let eval_unop = op I32Op.unop I64Op.unop F32Op.unop F64Op.unop
194-
let eval_binop = op I32Op.binop I64Op.binop F32Op.binop F64Op.binop
195-
let eval_testop = op I32Op.testop I64Op.testop F32Op.testop F64Op.testop
196-
let eval_relop = op I32Op.relop I64Op.relop F32Op.relop F64Op.relop
197-
let eval_cvtop = op I32CvtOp.cvtop I64CvtOp.cvtop F32CvtOp.cvtop F64CvtOp.cvtop
229+
let eval_unop = op I32Op.unop I64Op.unop F32Op.unop F64Op.unop V128Op.unop
230+
let eval_binop = op I32Op.binop I64Op.binop F32Op.binop F64Op.binop V128Op.binop
231+
let eval_testop = op I32Op.testop I64Op.testop F32Op.testop F64Op.testop V128Op.testop
232+
let eval_relop = op I32Op.relop I64Op.relop F32Op.relop F64Op.relop V128Op.relop
233+
let eval_cvtop = op I32CvtOp.cvtop I64CvtOp.cvtop F32CvtOp.cvtop F64CvtOp.cvtop V128CvtOp.cvtop

interpreter/exec/v128.ml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
include Vector.Make
2+
(struct
3+
include Bytes
4+
let bytewidth = 16
5+
end)

interpreter/exec/vector.ml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
open Char
2+
3+
module type RepType =
4+
sig
5+
type t
6+
7+
val make : int -> char -> t
8+
(* ^ bits_make ? *)
9+
val to_string : t -> string
10+
val bytewidth : int
11+
end
12+
13+
module type S =
14+
sig
15+
type t
16+
type bits
17+
val default : t (* FIXME good name for default value? *)
18+
val to_string : t -> string
19+
val to_bits : t -> bits
20+
end
21+
22+
module Make (Rep : RepType) : S with type bits = Rep.t =
23+
struct
24+
type t = Rep.t
25+
type bits = Rep.t
26+
27+
let default = Rep.make Rep.bytewidth (chr 0)
28+
let to_string = Rep.to_string (* FIXME very very wrong *)
29+
let to_bits x = x
30+
end

interpreter/host/spectest.ml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ let global (GlobalType (t, _) as gt) =
1414
| I64Type -> I64 666L
1515
| F32Type -> F32 (F32.of_float 666.6)
1616
| F64Type -> F64 (F64.of_float 666.6)
17+
| V128Type -> failwith "TODO v128"
1718
in Global.alloc gt v
1819

1920
let table = Table.alloc (TableType ({min = 10l; max = Some 20l}, FuncRefType))

interpreter/runtime/memory.ml

+2
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ let load_value mem a o t =
111111
| I64Type -> I64 n
112112
| F32Type -> F32 (F32.of_bits (Int64.to_int32 n))
113113
| F64Type -> F64 (F64.of_bits n)
114+
| V128Type -> failwith "TODO v128"
114115

115116
let store_value mem a o v =
116117
let x =
@@ -119,6 +120,7 @@ let store_value mem a o v =
119120
| I64 x -> x
120121
| F32 x -> Int64.of_int32 (F32.to_bits x)
121122
| F64 x -> F64.to_bits x
123+
| V128 x -> failwith "TODO v128" (* FIXME V128.to_bits x requires store to accept something other than int64 *)
122124
in storen mem a o (Types.size (Values.type_of v)) x
123125

124126
let extend x n = function

interpreter/script/js.ml

+7-1
Original file line numberDiff line numberDiff line change
@@ -191,24 +191,29 @@ let eq_of = function
191191
| I64Type -> Values.I64 I64Op.Eq
192192
| F32Type -> Values.F32 F32Op.Eq
193193
| F64Type -> Values.F64 F64Op.Eq
194+
| V128Type -> failwith "TODO v128"
194195

195196
let and_of = function
196197
| I32Type | F32Type -> Values.I32 I32Op.And
197198
| I64Type | F64Type -> Values.I64 I64Op.And
199+
| V128Type -> failwith "TODO v128"
198200

199201
let reinterpret_of = function
200202
| I32Type -> I32Type, Nop
201203
| I64Type -> I64Type, Nop
202204
| F32Type -> I32Type, Convert (Values.I32 I32Op.ReinterpretFloat)
203205
| F64Type -> I64Type, Convert (Values.I64 I64Op.ReinterpretFloat)
206+
| V128Type -> failwith "TODO v128"
204207

205208
let canonical_nan_of = function
206209
| I32Type | F32Type -> Values.I32 (F32.to_bits F32.pos_nan)
207210
| I64Type | F64Type -> Values.I64 (F64.to_bits F64.pos_nan)
211+
| V128Type -> failwith "TODO v128"
208212

209213
let abs_mask_of = function
210214
| I32Type | F32Type -> Values.I32 Int32.max_int
211215
| I64Type | F64Type -> Values.I64 Int64.max_int
216+
| V128Type -> failwith "TODO v128"
212217

213218
let invoke ft lits at =
214219
[ft @@ at], FuncImport (1l @@ at) @@ at,
@@ -270,7 +275,7 @@ let wrap module_name item_name wrap_action wrap_assertion at =
270275

271276
let is_js_value_type = function
272277
| I32Type -> true
273-
| I64Type | F32Type | F64Type -> false
278+
| I64Type | F32Type | F64Type | V128Type -> false
274279

275280
let is_js_global_type = function
276281
| GlobalType (t, mut) -> is_js_value_type t && mut = Immutable
@@ -320,6 +325,7 @@ let of_literal lit =
320325
| Values.I64 i -> "int64(\"" ^ I64.to_string_s i ^ "\")"
321326
| Values.F32 z -> of_float (F32.to_float z)
322327
| Values.F64 z -> of_float (F64.to_float z)
328+
| Values.V128 v -> failwith "TODO v128" (* FIXME should this be even valid *)
323329

324330
let rec of_definition def =
325331
match def.it with

interpreter/syntax/ast.ml

+16-5
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,27 @@ struct
4444
| ReinterpretInt
4545
end
4646

47+
(* FIXME *)
48+
module VectorOp =
49+
struct
50+
type unop = TodoUnOp
51+
type binop = TodoBinOp
52+
type testop = TodoTestOp
53+
type relop = TodoRelOp
54+
type cvtop = TodoCvtOp
55+
end
56+
4757
module I32Op = IntOp
4858
module I64Op = IntOp
4959
module F32Op = FloatOp
5060
module F64Op = FloatOp
61+
module V128Op = VectorOp
5162

52-
type unop = (I32Op.unop, I64Op.unop, F32Op.unop, F64Op.unop) Values.op
53-
type binop = (I32Op.binop, I64Op.binop, F32Op.binop, F64Op.binop) Values.op
54-
type testop = (I32Op.testop, I64Op.testop, F32Op.testop, F64Op.testop) Values.op
55-
type relop = (I32Op.relop, I64Op.relop, F32Op.relop, F64Op.relop) Values.op
56-
type cvtop = (I32Op.cvtop, I64Op.cvtop, F32Op.cvtop, F64Op.cvtop) Values.op
63+
type unop = (I32Op.unop, I64Op.unop, F32Op.unop, F64Op.unop, V128Op.unop) Values.op
64+
type binop = (I32Op.binop, I64Op.binop, F32Op.binop, F64Op.binop, V128Op.binop) Values.op
65+
type testop = (I32Op.testop, I64Op.testop, F32Op.testop, F64Op.testop, V128Op.testop) Values.op
66+
type relop = (I32Op.relop, I64Op.relop, F32Op.relop, F64Op.relop, V128Op.relop) Values.op
67+
type cvtop = (I32Op.cvtop, I64Op.cvtop, F32Op.cvtop, F64Op.cvtop, V128Op.cvtop) Values.op
5768

5869
type 'a memop =
5970
{ty : value_type; align : int; offset : Memory.offset; sz : 'a option}

interpreter/syntax/types.ml

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(* Types *)
22

3-
type value_type = I32Type | I64Type | F32Type | F64Type
3+
type value_type = I32Type | I64Type | F32Type | F64Type | V128Type
44
type elem_type = FuncRefType
55
type stack_type = value_type list
66
type func_type = FuncType of stack_type * stack_type
@@ -22,6 +22,7 @@ type extern_type =
2222
let size = function
2323
| I32Type | F32Type -> 4
2424
| I64Type | F64Type -> 8
25+
| V128Type -> 16
2526

2627

2728
(* Subtyping *)
@@ -73,6 +74,7 @@ let string_of_value_type = function
7374
| I64Type -> "i64"
7475
| F32Type -> "f32"
7576
| F64Type -> "f64"
77+
| V128Type -> "v128"
7678

7779
let string_of_value_types = function
7880
| [t] -> string_of_value_type t

interpreter/syntax/values.ml

+13-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ open Types
33

44
(* Values and operators *)
55

6-
type ('i32, 'i64, 'f32, 'f64) op =
7-
I32 of 'i32 | I64 of 'i64 | F32 of 'f32 | F64 of 'f64
6+
type ('i32, 'i64, 'f32, 'f64, 'v128) op =
7+
I32 of 'i32 | I64 of 'i64 | F32 of 'f32 | F64 of 'f64 | V128 of 'v128
88

9-
type value = (I32.t, I64.t, F32.t, F64.t) op
9+
type value = (I32.t, I64.t, F32.t, F64.t, V128.t) op
1010

1111

1212
(* Typing *)
@@ -16,12 +16,14 @@ let type_of = function
1616
| I64 _ -> I64Type
1717
| F32 _ -> F32Type
1818
| F64 _ -> F64Type
19+
| V128 _ -> V128Type
1920

2021
let default_value = function
2122
| I32Type -> I32 I32.zero
2223
| I64Type -> I64 I64.zero
2324
| F32Type -> F32 F32.zero
2425
| F64Type -> F64 F64.zero
26+
| V128Type -> V128 V128.default
2527

2628

2729
(* Conversion *)
@@ -33,6 +35,7 @@ let string_of_value = function
3335
| I64 i -> I64.to_string_s i
3436
| F32 z -> F32.to_string z
3537
| F64 z -> F64.to_string z
38+
| V128 v -> V128.to_string v
3639

3740
let string_of_values = function
3841
| [v] -> string_of_value v
@@ -77,3 +80,10 @@ struct
7780
let to_value i = F64 i
7881
let of_value = function F64 z -> z | _ -> raise (Value F64Type)
7982
end
83+
84+
module V128Value =
85+
struct
86+
type t = V128.t
87+
let to_value i = V128 i
88+
let of_value = function V128 z -> z | _ -> raise (Value V128Type)
89+
end

0 commit comments

Comments
 (0)