diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-11-26 00:41:47 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-11-26 00:41:47 +0000 |
| commit | 33ce639ae0c9c4e4fcbe8265b381ddaeebc43ccb (patch) | |
| tree | 5c2849b5665145cff1c973ce7d053015ae0b3ee1 /src/main/java | |
| parent | 38b6beb8d3034beb59b3c918502bf9ba1a64be8c (diff) | |
Write tag names
Diffstat (limited to 'src/main/java')
4 files changed, 41 insertions, 1 deletions
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 5fdbbbe7..3e17801b 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 @@ -109,6 +109,20 @@ public record WasmModule( return indexedGlobals; } + public record IndexedTag(WasmTagIndex tagIndex, String name) { + } + + public List<IndexedTag> indexedTags() { + var indexedTags = new ArrayList<IndexedTag>(); + + for (var tag : tags) { + var tagIndex = new WasmTagIndex(indexedTags.size()); + indexedTags.add(new IndexedTag(tagIndex, tag.name())); + } + + return indexedTags; + } + 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/WasmTagIndex.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmTagIndex.java new file mode 100644 index 00000000..0c8412ea --- /dev/null +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmTagIndex.java @@ -0,0 +1,4 @@ +package org.shedlang.compiler.backends.wasm.structure.ast.modules; + +public record WasmTagIndex(int value) { +} diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatExceptions.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatExceptions.java index 29b0d9ed..fa1dbe9c 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatExceptions.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatExceptions.java @@ -2,6 +2,7 @@ package org.shedlang.compiler.backends.wasm.structure.binary; import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmModule; import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmTag; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmTagIndex; import java.io.IOException; @@ -26,4 +27,8 @@ public class WasmBinaryFormatExceptions { writer.writeByte(tag.tagType().id()); writer.writeTypeIndex(tag.funcType()); } + + static void writeTagIndex(WasmTagIndex tagIndex, WasmBinaryWriter writer) { + writer.writeUnsignedInt32(tagIndex.value()); + } } 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 101cf9d4..850f1057 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 @@ -7,6 +7,7 @@ import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmModule; import java.io.IOException; import java.util.stream.Stream; +import static org.shedlang.compiler.backends.wasm.structure.binary.WasmBinaryFormatExceptions.writeTagIndex; import static org.shedlang.compiler.backends.wasm.structure.binary.WasmBinaryFormatModules.*; public class WasmBinaryFormatNameSection { @@ -28,6 +29,7 @@ public class WasmBinaryFormatNameSection { writeFunctionNameSubsection(module, writer); writeLocalNameSubsection(module, indexTable, writer); writeGlobalNameSubsection(module, writer); + writeTagNameSubsection(module, writer); }); } @@ -47,7 +49,8 @@ public class WasmBinaryFormatNameSection { MODULE_NAME(0), FUNCTION_NAMES(1), LOCAL_NAMES(2), - GLOBAL_NAMES(7); + GLOBAL_NAMES(7), + TAG_NAMES(11); private final byte id; @@ -113,4 +116,18 @@ public class WasmBinaryFormatNameSection { ); }); } + + // == Tag Names == + + private static void writeTagNameSubsection(WasmModule module, WasmBinaryWriter writer) throws IOException { + writeNameSubsection(NameSubsectionId.TAG_NAMES, writer, () -> { + writer.writeVec( + module.indexedTags(), + tag -> { + writeTagIndex(tag.tagIndex(), writer); + writer.writeString(tag.name()); + } + ); + }); + } } |
