From 9846bb9db8bd0aa6e84780bda459f4642eb45213 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 24 Jun 2023 21:03:46 +0100 Subject: Add local names to WASM object file --- ...mpiler.testing.snapshots.Snapshotter)].expected | 2 + ...mpiler.testing.snapshots.Snapshotter)].expected | 31 ++++++++----- ...mpiler.testing.snapshots.Snapshotter)].expected | 2 + ...mpiler.testing.snapshots.Snapshotter)].expected | 2 + ...mpiler.testing.snapshots.Snapshotter)].expected | 2 + .../wasm/structure/ast/modules/WasmFunction.java | 13 +++++- .../wasm/structure/ast/modules/WasmLocal.java | 6 +++ .../structure/binary/WasmBinaryFormatModules.java | 4 +- .../binary/WasmBinaryFormatNameSection.java | 53 ++++++++++++++++++---- .../structure/WasmBinaryFormatModuleTests.java | 20 ++++++-- 10 files changed, 108 insertions(+), 27 deletions(-) create mode 100644 src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmLocal.java diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormatModuleTests]/[method:canSerializeEmptyModule(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormatModuleTests]/[method:canSerializeEmptyModule(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index c4cedbf7..57f52280 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormatModuleTests]/[method:canSerializeEmptyModule(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormatModuleTests]/[method:canSerializeEmptyModule(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -3,5 +3,7 @@ module.wasm: file format wasm 0x1 Section Details: +Custom: + - name: "name" Code Disassembly: diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormatModuleTests]/[method:canSerializeModuleWithFunctions(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormatModuleTests]/[method:canSerializeModuleWithFunctions(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index b4fc2e46..983a04d5 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormatModuleTests]/[method:canSerializeModuleWithFunctions(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormatModuleTests]/[method:canSerializeModuleWithFunctions(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -4,22 +4,31 @@ module.wasm: file format wasm 0x1 Section Details: Type[2]: - - type[0] () -> i32 - - type[1] () -> i64 + - type[0] (f32, f64) -> i32 + - type[1] (f32) -> i64 Function[2]: - func[0] sig=0 - func[1] sig=1 Code[2]: - func[0] size=8 - - func[1] size=4 + - func[1] size=6 +Custom: + - name: "name" + - func[0] local[0] + - func[0] local[1] + - func[0] local[2] + - func[0] local[3] + - func[1] local[0] + - func[1] local[1] Code Disassembly: -00002c func[0]: - 00002d: 01 7d | local[0] type=f32 - 00002f: 01 7c | local[1] type=f64 - 000031: 41 00 | i32.const 0 - 000033: 0b | end -000039 func[1]: - 00003a: 42 00 | i64.const 0 - 00003c: 0b | end \ No newline at end of file +00002f func[0]: + 000030: 01 7e | local[2] type=i64 + 000032: 01 7c | local[3] type=f64 + 000034: 41 00 | i32.const 0 + 000036: 0b | end +00003c func[1]: + 00003d: 01 7e | local[1] type=i64 + 00003f: 42 00 | i64.const 0 + 000041: 0b | end \ No newline at end of file diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormatModuleTests]/[method:canSerializeModuleWithMemories(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormatModuleTests]/[method:canSerializeModuleWithMemories(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index 09d32e1e..c1d363ad 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormatModuleTests]/[method:canSerializeModuleWithMemories(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormatModuleTests]/[method:canSerializeModuleWithMemories(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -5,5 +5,7 @@ Section Details: Memory[1]: - memory[0] pages: initial=1 +Custom: + - name: "name" Code Disassembly: diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormatModuleTests]/[method:canSerializeModuleWithTables(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormatModuleTests]/[method:canSerializeModuleWithTables(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index e074d3a7..07eec2fe 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormatModuleTests]/[method:canSerializeModuleWithTables(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormatModuleTests]/[method:canSerializeModuleWithTables(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -6,5 +6,7 @@ Section Details: Table[2]: - table[0] type=funcref initial=1 - table[1] type=externref initial=2 max=3 +Custom: + - name: "name" Code Disassembly: diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormatModuleTests]/[method:canSerializeModuleWithTypes(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormatModuleTests]/[method:canSerializeModuleWithTypes(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index 766f64e1..b6ed1048 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormatModuleTests]/[method:canSerializeModuleWithTypes(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormatModuleTests]/[method:canSerializeModuleWithTypes(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -6,5 +6,7 @@ Section Details: Type[2]: - type[0] () -> nil - type[1] (i32, i64) -> (f32, f64) +Custom: + - name: "name" Code Disassembly: diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmFunction.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmFunction.java index 50ad805b..0f295e90 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmFunction.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmFunction.java @@ -7,8 +7,17 @@ import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmValueType; import java.util.List; public record WasmFunction( - WasmFuncType type, - List locals, + List params, + List locals, + List resultTypes, List body ) { + public WasmFuncType type() { + return new WasmFuncType( + params.stream() + .map(param -> param.type()) + .toList(), + resultTypes + ); + } } diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmLocal.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmLocal.java new file mode 100644 index 00000000..d9813336 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmLocal.java @@ -0,0 +1,6 @@ +package org.shedlang.compiler.backends.wasm.structure.ast.modules; + +import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmValueType; + +public record WasmLocal(String name, WasmValueType type) { +} diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatModules.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatModules.java index 58ae08a8..4f684f8f 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatModules.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatModules.java @@ -215,7 +215,7 @@ public class WasmBinaryFormatModules { function.locals(), local -> { Leb128Encoding.encodeUnsignedInt32(1, writer); - writeValueType(local, writer); + writeValueType(local.type(), writer); }, writer ); @@ -257,7 +257,7 @@ public class WasmBinaryFormatModules { writeTableSection(module, writer); writeMemorySection(module, writer); writeCodeSection(module, writer); - writeNameSection(module, writer); + writeNameSection(module, indexTable, writer); outputStream.write(writer.toBytes()); } diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatNameSection.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatNameSection.java index 67caa7c6..50c98879 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatNameSection.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatNameSection.java @@ -1,6 +1,7 @@ package org.shedlang.compiler.backends.wasm.structure.binary; import org.shedlang.compiler.backends.wasm.structure.BinaryWriter; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmLocal; import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmModule; import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmImportDescriptor; import org.shedlang.compiler.backends.wasm.structure.binary.WasmBinaryFormatModules.SectionId; @@ -23,13 +24,16 @@ public class WasmBinaryFormatNameSection { // == Name Section == - static void writeNameSection(WasmModule module, BinaryWriter writer) throws IOException { - if (module.imports().size() > 0) { - writeSection(SectionId.CUSTOM, writer, () -> { - writeString("name", writer); - writeNameSectionFunctionNameSubsection(module, writer); - }); - } + static void writeNameSection( + WasmModule module, + WasmIndexTable indexTable, + BinaryWriter writer + ) throws IOException { + writeSection(SectionId.CUSTOM, writer, () -> { + writeString("name", writer); + writeFunctionNameSubsection(module, writer); + writeLocalNameSubsection(module, indexTable, writer); + }); } // == Subsections == @@ -71,7 +75,7 @@ public class WasmBinaryFormatNameSection { // == Function Names == - private static void writeNameSectionFunctionNameSubsection(WasmModule module, BinaryWriter writer) throws IOException { + private static void writeFunctionNameSubsection(WasmModule module, BinaryWriter writer) throws IOException { writeNameSubsection(NameSubsectionId.FUNCTION_NAMES, writer, () -> { var importedFunctions = module.imports().stream() .flatMap(import_ -> switch (import_.descriptor()) { @@ -83,4 +87,37 @@ public class WasmBinaryFormatNameSection { writeNameMap(writer, importedFunctions); }); } + + // == Local Names == + + private static void writeLocalNameSubsection( + WasmModule module, + WasmIndexTable indexTable, + BinaryWriter writer + ) throws IOException { + writeNameSubsection(NameSubsectionId.LOCAL_NAMES, writer, () -> { + writeVec( + module.functions(), + function -> { + var funcIndex = indexTable.funcIndex(function.type()); + writeFuncIndex(funcIndex, writer); + + var names = Stream.concat( + function.params().stream(), + function.locals().stream() + ).map(WasmLocal::name).toList(); + + writeVec( + names, + (nameIndex, name) -> { + Leb128Encoding.encodeUnsignedInt32(nameIndex, writer); + writeString(name, writer); + }, + writer + ); + }, + writer + ); + }); + } } diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java index cb1e7a76..5941c790 100644 --- a/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java +++ b/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java @@ -45,13 +45,25 @@ public class WasmBinaryFormatModuleTests { public void canSerializeModuleWithFunctions(Snapshotter snapshotter) throws IOException, InterruptedException { var module = WasmModule.EMPTY .addFunction(new WasmFunction( - new WasmFuncType(List.of(), List.of(WasmValueType.I32)), - List.of(WasmValueType.F32, WasmValueType.F64), + List.of( + new WasmLocal("paramA", WasmValueType.F32), + new WasmLocal("paramB", WasmValueType.F64) + ), + List.of( + new WasmLocal("localA", WasmValueType.I64), + new WasmLocal("localB", WasmValueType.F64) + ), + List.of(WasmValueType.I32), List.of(new WasmI32Const(0)) )) .addFunction(new WasmFunction( - new WasmFuncType(List.of(), List.of(WasmValueType.I64)), - List.of(), + List.of( + new WasmLocal("paramA", WasmValueType.F32) + ), + List.of( + new WasmLocal("localA", WasmValueType.I64) + ), + List.of(WasmValueType.I64), List.of(new WasmI64Const(0)) )); -- cgit v1.2.3