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

Commit 5e9685a

Browse files
authored
Create a helper to check valid simd operations (#249)
The "i32x4" case looks trivial now, but we will be adding i64x2 later.
1 parent 30a0912 commit 5e9685a

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

interpreter/text/lexer.mll

+13-9
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ let simd_shape = function
114114
| "f32x4" -> Simd.F32x4
115115
| "f64x2" -> Simd.F64x2
116116
| _ -> assert false
117+
118+
let only shapes s lexbuf =
119+
if not (List.mem s shapes) then
120+
error lexbuf "unknown operator"
117121
}
118122

119123
let sign = '+' | '-'
@@ -389,35 +393,35 @@ rule token = parse
389393
| "output" { OUTPUT }
390394

391395
| (simd_shape as s)".neg"
392-
{ if s <> "i32x4" && s <> "f32x4" && s <> "f64x2" then error lexbuf "unknown operator";
396+
{ only ["i32x4"; "f32x4"; "f64x2"] s lexbuf;
393397
UNARY (simdop s unreachable unreachable i32x4_neg unreachable f32x4_neg f64x2_neg) }
394398
| (simd_float_shape as s)".sqrt" { UNARY (simd_float_op s f32x4_sqrt f64x2_sqrt) }
395399
| (simd_shape as s)".add"
396-
{ if s <> "i32x4" && s <> "f32x4" && s <> "f64x2" then error lexbuf "unknown operator";
400+
{ only ["i32x4"; "f32x4"; "f64x2"] s lexbuf;
397401
BINARY (simdop s unreachable unreachable i32x4_add unreachable f32x4_add f64x2_add) }
398402
| (simd_shape as s)".sub"
399-
{ if s <> "i32x4" && s <> "f32x4" && s <> "f64x2" then error lexbuf "unknown operator";
403+
{ only ["i32x4"; "f32x4"; "f64x2"] s lexbuf;
400404
BINARY (simdop s unreachable unreachable i32x4_sub unreachable f32x4_sub f64x2_sub) }
401405
| (simd_shape as s)".min_s"
402-
{ if s <> "i32x4" then error lexbuf "unknown operator";
406+
{ only ["i32x4"] s lexbuf;
403407
BINARY (simdop s unreachable unreachable i32x4_min_s unreachable unreachable unreachable) }
404408
| (simd_shape as s)".min_u"
405-
{ if s <> "i32x4" then error lexbuf "unknown operator";
409+
{ only ["i32x4"] s lexbuf;
406410
BINARY (simdop s unreachable unreachable i32x4_min_u unreachable unreachable unreachable) }
407411
| (simd_shape as s)".max_s"
408-
{ if s <> "i32x4" then error lexbuf "unknown operator";
412+
{ only ["i32x4"] s lexbuf;
409413
BINARY (simdop s unreachable unreachable i32x4_max_s unreachable unreachable unreachable) }
410414
| (simd_shape as s)".max_u"
411-
{ if s <> "i32x4" then error lexbuf "unknown operator";
415+
{ only ["i32x4"] s lexbuf;
412416
BINARY (simdop s unreachable unreachable i32x4_max_u unreachable unreachable unreachable) }
413417
| (simd_shape as s)".mul"
414-
{ if s <> "i32x4" && s <> "f32x4" && s <> "f64x2" then error lexbuf "unknown operator";
418+
{ only ["i32x4"; "f32x4"; "f64x2"] s lexbuf;
415419
BINARY (simdop s unreachable unreachable i32x4_mul unreachable f32x4_mul f64x2_mul) }
416420
| (simd_float_shape as s)".div" { BINARY (simd_float_op s f32x4_div f64x2_div) }
417421
| (simd_float_shape as s)".min" { BINARY (simd_float_op s f32x4_min f64x2_min) }
418422
| (simd_float_shape as s)".max" { BINARY (simd_float_op s f32x4_max f64x2_max) }
419423
| (simd_shape as s)".abs"
420-
{ if s <> "i32x4" && s <> "f32x4" && s <> "f64x2" then error lexbuf "unknown operator";
424+
{ only ["i32x4"; "f32x4"; "f64x2"] s lexbuf;
421425
UNARY (simdop s unreachable unreachable i32x4_abs unreachable f32x4_abs f64x2_abs) }
422426
| (simd_shape as s) { SIMD_SHAPE (simd_shape s) }
423427

0 commit comments

Comments
 (0)