diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-06-27 18:36:02 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-06-27 18:36:02 +0100 |
| commit | a1134fbafcb5a2dfcb5b22f90e3cfe7abd6c3f72 (patch) | |
| tree | ed8d5fd5aa11a16f34e3db76792083afcb5bf0a9 /src/main/java | |
| parent | 685f1508af816723f8ab7bb712ee2ba63c412395 (diff) | |
Introduce WasmFuncName
Diffstat (limited to 'src/main/java')
5 files changed, 38 insertions, 22 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmFuncName.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmFuncName.java new file mode 100644 index 00000000..70ea1b30 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmFuncName.java @@ -0,0 +1,4 @@ +package org.shedlang.compiler.backends.wasm.structure.ast.modules; + +public record WasmFuncName(String value) { +} 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 5365b5ee..2175e46c 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 @@ -69,21 +69,6 @@ public record WasmModule( return indexedFunctions; } - public List<IndexedFunction.Definition> indexedFunctionDefinitions() { - var startIndex = imports.stream() - .filter(import_ -> import_.descriptor() instanceof WasmImportDescriptor.Func) - .count(); - - var indexedFunctions = new ArrayList<IndexedFunction.Definition>(); - - for (var function : functions) { - var funcIndex = new WasmFuncIndex((int) startIndex + indexedFunctions.size()); - indexedFunctions.add(new IndexedFunction.Definition(funcIndex, function)); - } - - return indexedFunctions; - } - public sealed interface IndexedGlobal { record Import(WasmGlobalIndex globalIndex, WasmImportDescriptor.Global descriptor) implements IndexedGlobal { @Override 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 b1b35c9d..9052c390 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 @@ -34,6 +34,11 @@ public class WasmBinaryFormatModules { writer.writeUnsignedInt32(funcIndex.value()); } + static void writeFuncIndex(WasmFuncName funcName, WasmIndexTable indexTable, WasmBinaryWriter writer) { + var funcIndex = indexTable.funcIndex(funcName); + writeFuncIndex(funcIndex, writer); + } + static void writeGlobalIndex(WasmGlobalIndex globalIndex, WasmBinaryWriter writer) { writer.writeUnsignedInt32(globalIndex.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 63672039..101cf9d4 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,5 +1,6 @@ package org.shedlang.compiler.backends.wasm.structure.binary; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmFuncName; import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmLocal; import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmModule; @@ -78,11 +79,9 @@ public class WasmBinaryFormatNameSection { ) throws IOException { writeNameSubsection(NameSubsectionId.LOCAL_NAMES, writer, () -> { writer.writeVec( - module.indexedFunctionDefinitions(), - indexedFunction -> { - writeFuncIndex(indexedFunction.funcIndex(), writer); - - var function = indexedFunction.function(); + module.functions(), + function -> { + writeFuncIndex(new WasmFuncName(function.name()), indexTable, writer); var names = Stream.concat( function.params().stream(), diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmIndexTable.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmIndexTable.java index d32e1de9..e9b855f2 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmIndexTable.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmIndexTable.java @@ -1,5 +1,7 @@ package org.shedlang.compiler.backends.wasm.structure.binary; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmFuncIndex; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmFuncName; import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmModule; import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmTypeIndex; import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmFuncType; @@ -14,18 +16,39 @@ public class WasmIndexTable { funcTypeToTypeIndex.put(funcType, new WasmTypeIndex(funcTypeToTypeIndex.size())); } + var functionNameToFuncIndex = new HashMap<String, WasmFuncIndex>(); + for (var indexedFunction : module.indexedFunctions()) { + functionNameToFuncIndex.put(indexedFunction.name(), indexedFunction.funcIndex()); + } + return new WasmIndexTable( - funcTypeToTypeIndex + funcTypeToTypeIndex, + functionNameToFuncIndex ); } private final Map<WasmFuncType, WasmTypeIndex> funcTypeToTypeIndex; + private final Map<String, WasmFuncIndex> functionNameToFuncIndex; - private WasmIndexTable(Map<WasmFuncType, WasmTypeIndex> funcTypeToTypeIndex) { + private WasmIndexTable( + Map<WasmFuncType, WasmTypeIndex> funcTypeToTypeIndex, + Map<String, WasmFuncIndex> functionNameToFuncIndex + ) { this.funcTypeToTypeIndex = funcTypeToTypeIndex; + this.functionNameToFuncIndex = functionNameToFuncIndex; } public WasmTypeIndex typeIndex(WasmFuncType type) { return funcTypeToTypeIndex.get(type); } + + public WasmFuncIndex funcIndex(WasmFuncName name) { + var funcIndex = functionNameToFuncIndex.get(name.value()); + if (funcIndex == null) { + // TODO: better error + throw new RuntimeException("missing func index for function: " + name.value()); + } else { + return funcIndex; + } + } } |
