diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-06-25 13:29:30 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-06-25 13:29:30 +0100 |
| commit | 263db75901806c8a41721d38137f0cd7f03d35de (patch) | |
| tree | 2eac1b23d03466e4e6c9bb492d421455ed26dfd3 /src/main/java | |
| parent | cd63fb7d39491f917cf65c77d33f465de2e2dbda (diff) | |
Include globals in name section
Diffstat (limited to 'src/main/java')
| -rw-r--r-- | src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatNameSection.java | 56 |
1 files changed, 29 insertions, 27 deletions
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 4c4f948a..e233cb17 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,22 +1,21 @@ 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.WasmFuncIndex; import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmLocal; import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmModule; import java.io.IOException; -import java.util.List; import java.util.stream.Stream; import static org.shedlang.compiler.backends.wasm.structure.binary.WasmBinaryFormatConventions.writeVec; -import static org.shedlang.compiler.backends.wasm.structure.binary.WasmBinaryFormatModules.writeCustomSection; -import static org.shedlang.compiler.backends.wasm.structure.binary.WasmBinaryFormatModules.writeFuncIndex; +import static org.shedlang.compiler.backends.wasm.structure.binary.WasmBinaryFormatModules.*; import static org.shedlang.compiler.backends.wasm.structure.binary.WasmBinaryFormatSize.writeSize; import static org.shedlang.compiler.backends.wasm.structure.binary.WasmBinaryFormatValues.writeString; public class WasmBinaryFormatNameSection { // Corresponds to https://webassembly.github.io/spec/core/appendix/custom.html#name-section + // + // Additional name subsections are implemented as per https://github.com/WebAssembly/extended-name-section/blob/main/proposals/extended-name-section/Overview.md private WasmBinaryFormatNameSection() { } @@ -31,6 +30,7 @@ public class WasmBinaryFormatNameSection { writeCustomSection("name", writer, () -> { writeFunctionNameSubsection(module, writer); writeLocalNameSubsection(module, indexTable, writer); + writeGlobalNameSubsection(module, writer); }); } @@ -49,7 +49,8 @@ public class WasmBinaryFormatNameSection { private enum NameSubsectionId { MODULE_NAME(0), FUNCTION_NAMES(1), - LOCAL_NAMES(2); + LOCAL_NAMES(2), + GLOBAL_NAMES(7); private final byte id; @@ -58,32 +59,18 @@ public class WasmBinaryFormatNameSection { } } - // == Name Maps == - - private record NameAssoc(WasmFuncIndex index, String name) { - - } - - private static void writeNameMap(BinaryWriter writer, List<NameAssoc> assocs) throws IOException { - writeVec( - assocs, - assoc -> { - writeFuncIndex(assoc.index, writer); - writeString(assoc.name, writer); - }, - writer - ); - } - // == Function Names == private static void writeFunctionNameSubsection(WasmModule module, BinaryWriter writer) throws IOException { writeNameSubsection(NameSubsectionId.FUNCTION_NAMES, writer, () -> { - var functionNames = module.indexedFunctions().stream() - .map(function -> new NameAssoc(function.funcIndex(), function.name())) - .toList(); - - writeNameMap(writer, functionNames); + writeVec( + module.indexedFunctions(), + function -> { + writeFuncIndex(function.funcIndex(), writer); + writeString(function.name(), writer); + }, + writer + ); }); } @@ -120,4 +107,19 @@ public class WasmBinaryFormatNameSection { ); }); } + + // == Global Names == + + private static void writeGlobalNameSubsection(WasmModule module, BinaryWriter writer) throws IOException { + writeNameSubsection(NameSubsectionId.GLOBAL_NAMES, writer, () -> { + writeVec( + module.indexedGlobals(), + global -> { + writeGlobalIndex(global.globalIndex(), writer); + writeString(global.name(), writer); + }, + writer + ); + }); + } } |
