Skip to content

Commit

Permalink
feat: remove type section from disassembly
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Disassembly output will no longer print all the
types in the beginning by default.

The type section can be reconstructed from the implicit type
information in the remainder of the disassembly, and is generally
just cognitive overhead for developers, especially in the browser
DevTools (see https://crbug.com/1092763 for relevant downstream
issue in Chromium DevTools). Relying on the implicit type information
is far more readable for humans and also avoids the need to dump all
the types in the beginning, which take up precious space since both
Chromium and Firefox DevTools limit the number of lines that are
displayed for large Wasm modules.

This adds a `skipTypes` option to `WasmDisassembler` instances, which
can be used to restore the old behavior. By default this option is set
to `true`.

This includes a fix to `call_indirect` and `return_call_indirect`,
where we had previously refered to the type by name, and now we
use the abbreviated form for printing types as well [1].

[1]: https://webassembly.github.io/spec/core/text/modules.html#abbreviations

Refs: #56
  • Loading branch information
bmeurer committed Jun 10, 2020
1 parent 7418bc3 commit d61d67d
Show file tree
Hide file tree
Showing 286 changed files with 219 additions and 1,794 deletions.
25 changes: 22 additions & 3 deletions src/WasmDis.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ describe("WasmDisassembler.getResult() with function code", () => {
const fileName = `test.wat`;
const expectedLines = [
"(module",
" (type $type0 (func (result i32)))",
" (export \"export.function\" (func $func0))",
" (func $func0 (result i32)",
" (local $var0 i32)",
Expand All @@ -327,7 +326,7 @@ describe("WasmDisassembler.getResult() with function code", () => {
const result = dis.getResult();
expect(result.done).toBe(true);
expect(result.lines).toEqual(expectedLines);
expect(result.offsets).toEqual([0, 10, 22, 43, 43, 48, 50, 51, 53, 55, 83]);
expect(result.offsets).toEqual([0, 22, 43, 43, 48, 50, 51, 53, 55, 83]);
expect(result.functionBodyOffsets).toEqual(
[
{
Expand Down Expand Up @@ -366,6 +365,12 @@ describe("WasmDisassembler.getResult() without function code", () => {
(export "export.function" (func 0)))`;
const fileName = `test.wat`;
const expectedLines = [
"(module",
" (import \"import\" \"function\" (func $import0))",
" (export \"export.function\" (func $import0))",
")",
];
const expectedLinesWithTypes = [
"(module",
" (type $type0 (func))",
" (import \"import\" \"function\" (func $import0))",
Expand All @@ -386,7 +391,7 @@ describe("WasmDisassembler.getResult() without function code", () => {
const result = dis.getResult();
expect(result.done).toBe(true);
expect(result.lines).toEqual(expectedLines);
expect(result.offsets).toEqual([0, 10, 16, 37, 68, ]);
expect(result.offsets).toEqual([0, 16, 37, 68, ]);
expect(result.functionBodyOffsets).toEqual([]);
});

Expand All @@ -406,4 +411,18 @@ describe("WasmDisassembler.getResult() without function code", () => {
expect(result.offsets).toBeUndefined();
expect(result.functionBodyOffsets).toBeUndefined();
});

test("skipTypes is false", () => {
const { buffer } = parseWat(fileName, watString).toBinary({
write_debug_names: true
});
const parser = new BinaryReader();
parser.setData(buffer.buffer, 0, buffer.byteLength);
const dis = new WasmDisassembler();
dis.skipTypes = false;
dis.disassembleChunk(parser);
const result = dis.getResult();
expect(result.done).toBe(true);
expect(result.lines).toEqual(expectedLinesWithTypes);
});
});
25 changes: 17 additions & 8 deletions src/WasmDis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ export class WasmDisassembler {
private _indent: string;
private _indentLevel: number;
private _addOffsets: boolean;
private _nextLineToAddOffset: number;
private _skipTypes: boolean = true;
private _done: boolean;
private _currentPosition: number;
private _nameResolver: INameResolver;
Expand Down Expand Up @@ -421,6 +421,14 @@ export class WasmDisassembler {
throw new Error('Cannot switch addOffsets during processing.');
this._addOffsets = value;
}
public get skipTypes() {
return this._skipTypes;
}
public set skipTypes(skipTypes: boolean) {
if (this._currentPosition)
throw new Error('Cannot switch skipTypes during processing.');
this._skipTypes = skipTypes;
}
public get labelMode() {
return this._labelMode;
}
Expand Down Expand Up @@ -581,8 +589,7 @@ export class WasmDisassembler {
break;
case OperatorCode.call_indirect:
case OperatorCode.return_call_indirect:
var typeName = this._nameResolver.getTypeName(operator.typeIndex, true);
this.appendBuffer(` (type ${typeName})`);
this.printFuncType(operator.typeIndex);
break;
case OperatorCode.local_get:
case OperatorCode.local_set:
Expand Down Expand Up @@ -1016,11 +1023,13 @@ export class WasmDisassembler {
var funcType = <IFunctionType>reader.result;
var typeIndex = this._types.length;
this._types.push(funcType);
var typeName = this._nameResolver.getTypeName(typeIndex, false);
this.appendBuffer(` (type ${typeName} (func`);
this.printFuncType(typeIndex);
this.appendBuffer('))');
this.newLine();
if (!this._skipTypes) {
var typeName = this._nameResolver.getTypeName(typeIndex, false);
this.appendBuffer(` (type ${typeName} (func`);
this.printFuncType(typeIndex);
this.appendBuffer('))');
this.newLine();
}
break;
case BinaryReaderState.START_SECTION_ENTRY:
var startEntry = <IStartEntry>reader.result;
Expand Down
1 change: 0 additions & 1 deletion test/address.wast.0.wasm.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
(module
(type $type0 (func (param i32)))
(import "spectest" "print" (func $import0 (param i32)))
(memory $memory0 1)
(export "good" (func $func1))
Expand Down
9 changes: 0 additions & 9 deletions test/atomic.0.wasm.out
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
(module
(type $type0 (func (param i64)))
(type $type1 (func (param i32) (result i32)))
(type $type2 (func (param i32) (result i64)))
(type $type3 (func (param i32 i32)))
(type $type4 (func (param i32 i64)))
(type $type5 (func (param i32 i32) (result i32)))
(type $type6 (func (param i32 i64) (result i64)))
(type $type7 (func (param i32 i32 i32) (result i32)))
(type $type8 (func (param i32 i64 i64) (result i64)))
(memory $memory0 1 1 shared)
(export "init" (func $func0))
(export "i32.atomic.load" (func $func1))
Expand Down
4 changes: 0 additions & 4 deletions test/atomic.1.wasm.out
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
(module
(type $type0 (func (param i64)))
(type $type1 (func (param i32 i32) (result i32)))
(type $type2 (func (param i32 i64 i64) (result i32)))
(type $type3 (func (param i32 i32 i64) (result i32)))
(memory $memory0 1 1 shared)
(export "init" (func $func0))
(export "atomic.notify" (func $func1))
Expand Down
3 changes: 0 additions & 3 deletions test/block-typed.wasm.out
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
(module
(type $type0 (func (result i32)))
(type $type1 (func (param i32 i32) (result i32)))
(type $type2 (func (param i32) (result i32)))
(export "run" (func $func0))
(func $func0 (result i32)
(local $var0 i32)
Expand Down
2 changes: 0 additions & 2 deletions test/block.wast.0.wasm.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
(module
(type $type0 (func))
(type $type1 (func (result i32)))
(export "empty" (func $func1))
(export "singular" (func $func2))
(export "multi" (func $func3))
Expand Down
16 changes: 4 additions & 12 deletions test/br.wast.0.wasm.out
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
(module
(type $type0 (func))
(type $type1 (func (result i32)))
(type $type2 (func (result i64)))
(type $type3 (func (result f32)))
(type $type4 (func (result f64)))
(type $type5 (func (param i32 i32) (result i32)))
(type $type6 (func (param i32 i32 i32) (result i32)))
(type $type7 (func (param i32 i32 i32) (result i32)))
(table $table0 1 1 anyfunc)
(memory $memory0 1)
(export "type-i32" (func $func1))
Expand Down Expand Up @@ -343,7 +335,7 @@
i32.const 1
i32.const 2
i32.const 3
call_indirect (type $type7)
call_indirect (param i32 i32 i32) (result i32)
end $label0
)
(func $func35 (result i32)
Expand All @@ -353,7 +345,7 @@
br $label0
i32.const 2
i32.const 3
call_indirect (type $type7)
call_indirect (param i32 i32 i32) (result i32)
end $label0
)
(func $func36 (result i32)
Expand All @@ -363,7 +355,7 @@
i32.const 22
br $label0
i32.const 3
call_indirect (type $type7)
call_indirect (param i32 i32 i32) (result i32)
end $label0
)
(func $func37 (result i32)
Expand All @@ -373,7 +365,7 @@
i32.const 2
i32.const 23
br $label0
call_indirect (type $type7)
call_indirect (param i32 i32 i32) (result i32)
end $label0
)
(func $func38 (result i32)
Expand Down
4 changes: 0 additions & 4 deletions test/br_if.wast.0.wasm.out
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
(module
(type $type0 (func))
(type $type1 (func (param i32) (result i32)))
(type $type2 (func (param i32)))
(type $type3 (func (param i32 i32)))
(export "as-block-first" (func $func1))
(export "as-block-mid" (func $func2))
(export "as-block-last" (func $func3))
Expand Down
17 changes: 4 additions & 13 deletions test/br_table.wast.0.wasm.out
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
(module
(type $type0 (func))
(type $type1 (func (result i32)))
(type $type2 (func (result i64)))
(type $type3 (func (result f32)))
(type $type4 (func (result f64)))
(type $type5 (func (param i32) (result i32)))
(type $type6 (func (param i32 i32) (result i32)))
(type $type7 (func (param i32 i32 i32) (result i32)))
(type $type8 (func (param i32 i32 i32) (result i32)))
(table $table0 1 1 anyfunc)
(memory $memory0 1)
(export "type-i32" (func $func1))
Expand Down Expand Up @@ -504,7 +495,7 @@
i32.const 1
i32.const 2
i32.const 3
call_indirect (type $type8)
call_indirect (param i32 i32 i32) (result i32)
end $label0
)
(func $func42 (result i32)
Expand All @@ -515,7 +506,7 @@
br_table $label0
i32.const 2
i32.const 3
call_indirect (type $type8)
call_indirect (param i32 i32 i32) (result i32)
end $label0
)
(func $func43 (result i32)
Expand All @@ -526,7 +517,7 @@
i32.const 1
br_table $label0
i32.const 3
call_indirect (type $type8)
call_indirect (param i32 i32 i32) (result i32)
end $label0
)
(func $func44 (result i32)
Expand All @@ -537,7 +528,7 @@
i32.const 23
i32.const 1
br_table $label0
call_indirect (type $type8)
call_indirect (param i32 i32 i32) (result i32)
end $label0
)
(func $func45 (result i32)
Expand Down
1 change: 0 additions & 1 deletion test/break-drop.wast.0.wasm.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
(module
(type $type0 (func))
(export "br" (func $func0))
(export "br_if" (func $func1))
(export "br_table" (func $func2))
Expand Down
15 changes: 0 additions & 15 deletions test/call.wast.0.wasm.out
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
(module
(type $type0 (func (result i32)))
(type $type1 (func (result i64)))
(type $type2 (func (result f32)))
(type $type3 (func (result f64)))
(type $type4 (func (param i32) (result i32)))
(type $type5 (func (param i64) (result i64)))
(type $type6 (func (param f32) (result f32)))
(type $type7 (func (param f64) (result f64)))
(type $type8 (func (param f32 i32) (result i32)))
(type $type9 (func (param i32 i64) (result i64)))
(type $type10 (func (param f64 f32) (result f32)))
(type $type11 (func (param i64 f64) (result f64)))
(type $type12 (func (param i64 i64) (result i64)))
(type $type13 (func (param i64) (result i32)))
(type $type14 (func))
(export "type-i32" (func $func12))
(export "type-i64" (func $func13))
(export "type-f32" (func $func14))
Expand Down
Loading

0 comments on commit d61d67d

Please sign in to comment.