diff options
| author | Michael Williamson <mike@zwobble.org> | 2024-12-22 17:22:23 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2024-12-22 17:22:23 +0000 |
| commit | e6462d8438d7eccd9c3c812a65036b210cf7ae33 (patch) | |
| tree | 8357a8d304aac8b619f7632cd63a354757739efb | |
| parent | c8edd213b029e9c9a97182ee9da8f149152fe383 (diff) | |
Add symbols for tables
9 files changed, 116 insertions, 26 deletions
diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.binary.WasmBinaryFormatModulesTests]/[method:canSerializeModuleWithElementSegments(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.binary.WasmBinaryFormatModulesTests]/[method:canSerializeModuleWithElementSegments(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index 69d64a0c..175a866e 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.binary.WasmBinaryFormatModulesTests]/[method:canSerializeModuleWithElementSegments(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.binary.WasmBinaryFormatModulesTests]/[method:canSerializeModuleWithElementSegments(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -10,7 +10,7 @@ Function[3]: - func[1] sig=0 <f2> - func[2] sig=0 <f3> Table[1]: - - table[0] type=funcref initial=1 + - table[0] type=funcref initial=1 <Table1> Elem[1]: - segment[0] flags=0 table=0 count=2 - init i32=0 - elem[0] = ref.func:0 <f1> @@ -21,10 +21,11 @@ Code[3]: - func[2] size=2 <f3> Custom: - name: "linking" - - symbol table [count=3] + - symbol table [count=4] - 0: F <f1> func=0 [ binding=global vis=default ] - 1: F <f2> func=1 [ binding=global vis=default ] - 2: F <f3> func=2 [ binding=global vis=default ] + - 3: T <Table1> table=0 [ binding=global vis=default ] Custom: - name: "name" - func[0] <f1> diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.binary.WasmBinaryFormatModulesTests]/[method:canSerializeModuleWithTableImports(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.binary.WasmBinaryFormatModulesTests]/[method:canSerializeModuleWithTableImports(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index 81acd316..cd120dd1 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.binary.WasmBinaryFormatModulesTests]/[method:canSerializeModuleWithTableImports(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.binary.WasmBinaryFormatModulesTests]/[method:canSerializeModuleWithTableImports(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -8,6 +8,9 @@ Import[2]: - table[1] type=funcref initial=2 max=5 <- ModuleName2.EntityName2 Custom: - name: "linking" + - symbol table [count=2] + - 0: T <TableName1> table=0 [ undefined explicit_name binding=global vis=default ] + - 1: T <TableName2> table=1 [ undefined explicit_name binding=global vis=default ] Custom: - name: "name" diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.binary.WasmBinaryFormatModulesTests]/[method:canSerializeModuleWithTables(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.binary.WasmBinaryFormatModulesTests]/[method:canSerializeModuleWithTables(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index 5d8e2af5..d6b7a5e5 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.binary.WasmBinaryFormatModulesTests]/[method:canSerializeModuleWithTables(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.binary.WasmBinaryFormatModulesTests]/[method:canSerializeModuleWithTables(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -4,10 +4,13 @@ module.wasm: file format wasm 0x1 Section Details: Table[2]: - - table[0] type=funcref initial=1 - - table[1] type=externref initial=2 max=3 + - table[0] type=funcref initial=1 <Table1> + - table[1] type=externref initial=2 max=3 <Table2> Custom: - name: "linking" + - symbol table [count=2] + - 0: T <Table1> table=0 [ binding=global vis=default ] + - 1: T <Table2> table=1 [ binding=global vis=default ] Custom: - name: "name" diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmImportDescriptor.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmImportDescriptor.java index fa7aa10f..af21b320 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmImportDescriptor.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmImportDescriptor.java @@ -7,7 +7,7 @@ import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmTableType; public sealed interface WasmImportDescriptor { record Func(String identifier, WasmFuncType type) implements WasmImportDescriptor {} - record Table(WasmTableType type) implements WasmImportDescriptor {} + record Table(String identifier, WasmTableType type) implements WasmImportDescriptor {} record Memory(WasmMemoryType type) implements WasmImportDescriptor {} record Global(String identifier, WasmGlobalType type) implements WasmImportDescriptor {} } diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmModule.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmModule.java index 5ff4df08..7fc9e9f9 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmModule.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmModule.java @@ -122,6 +122,43 @@ public record WasmModule( return indexedTags; } + public sealed interface IndexedTable { + record Import(WasmTableIndex tableIndex, WasmImportDescriptor.Table descriptor) implements IndexedTable { + @Override + public String name() { + return descriptor.identifier(); + } + } + + record Definition(WasmTableIndex tableIndex, WasmTable table) implements IndexedTable { + @Override + public String name() { + return table.name(); + } + } + + WasmTableIndex tableIndex(); + String name(); + } + + public List<IndexedTable> indexedTables() { + var indexedTables = new ArrayList<IndexedTable>(); + + for (var import_ : imports) { + if (import_.descriptor() instanceof WasmImportDescriptor.Table table) { + var tableIndex = new WasmTableIndex(indexedTables.size()); + indexedTables.add(new IndexedTable.Import(tableIndex, table)); + } + } + + for (var table : tables) { + var tableIndex = new WasmTableIndex(indexedTables.size()); + indexedTables.add(new IndexedTable.Definition(tableIndex, table)); + } + + return indexedTables; + } + public WasmModule addType(WasmFuncType type) { return new WasmModule( types.plus(type), diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmTable.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmTable.java index 88178528..7f957f05 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmTable.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmTable.java @@ -2,5 +2,5 @@ package org.shedlang.compiler.backends.wasm.structure.ast.modules; import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmTableType; -public record WasmTable(WasmTableType type) { +public record WasmTable(String name, WasmTableType type) { } diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatLinking.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatLinking.java index 97ab5fc2..fed36ed5 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatLinking.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatLinking.java @@ -218,6 +218,12 @@ public class WasmBinaryFormatLinking { writeTagIndex(tag.tagIndex, writer); writer.writeString(tag.name()); } + case SymbolInfo.Table table -> { + writer.writeByte(SymbolType.TABLE.id); + writeFlags(table.flags, writer); + writeTableIndex(table.tableIndex, writer); + writer.writeString(table.name()); + } } } @@ -398,6 +404,24 @@ public class WasmBinaryFormatLinking { tagNameToSymbolIndex.put(tag.name(), symbolIndex); } + for (var table : module.indexedTables()) { + var flags = switch (table) { + case WasmModule.IndexedTable.Definition definition -> + List.<SymbolFlag>of(); + + case WasmModule.IndexedTable.Import import_ -> + List.of(SymbolFlag.UNDEFINED, SymbolFlag.EXPLICIT_NAME); + }; + + var tableSymbolInfo = new SymbolInfo.Table( + flags, + table.tableIndex(), + table.name() + ); + + symbolInfos.add(tableSymbolInfo); + } + return new SymbolTable( symbolInfos, memoryAddressToSymbolIndex, @@ -426,6 +450,9 @@ public class WasmBinaryFormatLinking { record Tag(List<SymbolFlag> flags, WasmTagIndex tagIndex, String name) implements SymbolInfo { } + + record Table(List<SymbolFlag> flags, WasmTableIndex tableIndex, String name) implements SymbolInfo { + } } private enum SymbolFlag { 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 c0d0089e..889aff61 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 @@ -52,6 +52,10 @@ public class WasmBinaryFormatModules { writer.writeUnsignedInt32(tagIndex.value()); } + static void writeTableIndex(WasmTableIndex tableIndex, WasmBinaryWriter writer) { + writer.writeUnsignedInt32(tableIndex.value()); + } + // == Sections == static void writeSection( diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatModulesTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatModulesTests.java index 7054c4cf..4d7ba09c 100644 --- a/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatModulesTests.java +++ b/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatModulesTests.java @@ -125,18 +125,24 @@ public class WasmBinaryFormatModulesTests { .addImport(new WasmImport( "ModuleName1", "EntityName1", - new WasmImportDescriptor.Table(new WasmTableType( - WasmReferenceType.EXTERN_REF, - WasmLimits.min32(1) - )) + new WasmImportDescriptor.Table( + "TableName1", + new WasmTableType( + WasmReferenceType.EXTERN_REF, + WasmLimits.min32(1) + ) + ) )) .addImport(new WasmImport( "ModuleName2", "EntityName2", - new WasmImportDescriptor.Table(new WasmTableType( - WasmReferenceType.FUNC_REF, - WasmLimits.range32(2, 5) - )) + new WasmImportDescriptor.Table( + "TableName2", + new WasmTableType( + WasmReferenceType.FUNC_REF, + WasmLimits.range32(2, 5) + ) + ) )); var result = serializeModuleWasm32(module); @@ -204,14 +210,20 @@ public class WasmBinaryFormatModulesTests { @Test public void canSerializeModuleWithTables(Snapshotter snapshotter) throws IOException, InterruptedException { var module = WasmModule.EMPTY - .addTable(new WasmTable(new WasmTableType( - WasmReferenceType.FUNC_REF, - WasmLimits.min32(1) - ))) - .addTable(new WasmTable(new WasmTableType( - WasmReferenceType.EXTERN_REF, - WasmLimits.range32(2, 3) - ))); + .addTable(new WasmTable( + "Table1", + new WasmTableType( + WasmReferenceType.FUNC_REF, + WasmLimits.min32(1) + ) + )) + .addTable(new WasmTable( + "Table2", + new WasmTableType( + WasmReferenceType.EXTERN_REF, + WasmLimits.range32(2, 3) + ) + )); var result = serializeModuleWasm32(module); @@ -285,10 +297,13 @@ public class WasmBinaryFormatModulesTests { List.of(), new WasmExpression(List.of()) )) - .addTable(new WasmTable(new WasmTableType( - WasmReferenceType.FUNC_REF, - WasmLimits.min32(1) - ))) + .addTable(new WasmTable( + "Table1", + new WasmTableType( + WasmReferenceType.FUNC_REF, + WasmLimits.min32(1) + ) + )) .addElementSegment(WasmElementSegment.funcIndicesActive(List.of( new WasmFuncIndex(0), new WasmFuncIndex(2) |
