Skip to content

Commit

Permalink
feat: fix text representation of some ref and relaxed_simd ops (#144)
Browse files Browse the repository at this point in the history
* feat: fix ref.cast and ref.test syntax

* feat: fix relaxed_dot_i8x16_xxx names
  • Loading branch information
yurydelendik committed Mar 15, 2024
1 parent 0a666d5 commit 41a16d5
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
}
}
17 changes: 15 additions & 2 deletions src/WasmDis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,11 @@ export class WasmDisassembler {
return "noexnref";
}
}
private refTypeToString(typeIndex: number, nullable: boolean): string {
return this.typeToString(
new RefType(nullable ? TypeKind.ref_null : TypeKind.ref, typeIndex)
);
}
private typeToString(type: Type): string {
switch (type.kind) {
case TypeKind.i32:
Expand Down Expand Up @@ -1133,9 +1138,17 @@ export class WasmDisassembler {
break;
}
case OperatorCode.ref_cast:
case OperatorCode.ref_test: {
const refType = this.refTypeToString(operator.refType, false);
this.appendBuffer(` ${refType}`);
break;
}
case OperatorCode.ref_cast_null:
case OperatorCode.ref_test:
case OperatorCode.ref_test_null:
case OperatorCode.ref_test_null: {
const refType = this.refTypeToString(operator.refType, true);
this.appendBuffer(` ${refType}`);
break;
}
case OperatorCode.struct_new_default:
case OperatorCode.struct_new:
case OperatorCode.array_new_default:
Expand Down
16 changes: 8 additions & 8 deletions src/WasmParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,8 +589,8 @@ export const enum OperatorCode {
f64x2_relaxed_min = 0xfd10f,
f64x2_relaxed_max = 0xfd110,
i16x8_relaxed_q15mulr_s = 0xfd111,
i16x8_dot_i8x16_i7x16_s = 0xfd112,
i32x4_dot_i8x16_i7x16_add_s = 0xfd113,
i16x8_relaxed_dot_i8x16_i7x16_s = 0xfd112,
i32x4_relaxed_dot_i8x16_i7x16_add_s = 0xfd113,

// GC proposal.
struct_new = 0xfb00,
Expand Down Expand Up @@ -1183,8 +1183,8 @@ export const OperatorCodeNames = [
"f64x2.relaxed_min",
"f64x2.relaxed_max",
"i16x8.relaxed_q15mulr_s",
"i16x8.dot_i8x16_i7x16_s",
"i32x4.dot_i8x16_i7x16_add_s",
"i16x8.relaxed_dot_i8x16_i7x16_s",
"i32x4.relaxed_dot_i8x16_i7x16_add_s",
].forEach((s, i) => {
OperatorCodeNames[0xfd000 | i] = s;
});
Expand Down Expand Up @@ -1295,9 +1295,9 @@ export const OperatorCodeNames = [
"array.init_data",
"array.init_elem",
"ref.test",
"ref.test null",
"ref.test",
"ref.cast",
"ref.cast",
"ref.cast null",
"br_on_cast",
"br_on_cast_fail",
"any.convert_extern",
Expand Down Expand Up @@ -3125,8 +3125,8 @@ export class BinaryReader {
case OperatorCode.f64x2_relaxed_min:
case OperatorCode.f64x2_relaxed_max:
case OperatorCode.i16x8_relaxed_q15mulr_s:
case OperatorCode.i16x8_dot_i8x16_i7x16_s:
case OperatorCode.i32x4_dot_i8x16_i7x16_add_s:
case OperatorCode.i16x8_relaxed_dot_i8x16_i7x16_s:
case OperatorCode.i32x4_relaxed_dot_i8x16_i7x16_add_s:
break;
default:
this.error = new Error(
Expand Down
16 changes: 8 additions & 8 deletions test/WasmDis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1217,15 +1217,15 @@ describe("GC proposal support", () => {
0xd0,
0x6e, // ref.null any
0xfb,
0x14,
0x00, // ref.test $structA
0x15,
0x00, // ref.test null $structA
0x1a, // drop

0xd0,
0x6e, // ref.null any
0xfb,
0x16,
0x00, // ref.cast $structA
0x17,
0x00, // ref.cast null $structA
0x1a, // drop

0x0b, // end (block)
Expand Down Expand Up @@ -1440,7 +1440,7 @@ describe("GC proposal support", () => {
" i32.const 44",
" struct.set $structA $foo",
" local.get $var0",
" ref.cast $structA",
" ref.cast (ref $structA)",
" struct.get $structA $bar",
" drop",
// ---
Expand All @@ -1458,7 +1458,7 @@ describe("GC proposal support", () => {
" ref.eq",
" drop",
" ref.null eq",
" ref.test $structA",
" ref.test (ref $structA)",
" drop",
" )",
" (func $unknown1",
Expand Down Expand Up @@ -1486,10 +1486,10 @@ describe("GC proposal support", () => {
" br_on_cast flags=0 any $structA $label0",
" br_on_cast_fail flags=1 any $structA $label0",
" ref.null any",
" ref.test $structA",
" ref.test (ref null $structA)",
" drop",
" ref.null any",
" ref.cast $structA",
" ref.cast (ref null $structA)",
" drop",
" end $label0",
" )",
Expand Down
4 changes: 2 additions & 2 deletions test/__snapshots__/parse-and-disassemble.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -30882,13 +30882,13 @@ exports[`Parsing and disassembling relaxed_simd.wasm generates expected output 1
(func $func18 (param $var0 v128) (param $var1 v128) (result v128)
local.get $var0
local.get $var1
i16x8.dot_i8x16_i7x16_s
i16x8.relaxed_dot_i8x16_i7x16_s
)
(func $func19 (param $var0 v128) (param $var1 v128) (param $var2 v128) (result v128)
local.get $var0
local.get $var1
local.get $var2
i32x4.dot_i8x16_i7x16_add_s
i32x4.relaxed_dot_i8x16_i7x16_add_s
)
)
"
Expand Down
4 changes: 3 additions & 1 deletion test/wabt-compatibility.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ const WABT_FEATURES = {

describe("Disassembling", () => {
describe.each(getWasmFixtures())("%s", (fileName, filePath) => {
if (fileName.includes("exnref.wasm")) {
if (
["relaxed_simd.wasm", "exnref.wasm"].some((i) => fileName.includes(i))
) {
return;
}
test("generates wabt compatible text", async () => {
Expand Down

0 comments on commit 41a16d5

Please sign in to comment.