From a1134fbafcb5a2dfcb5b22f90e3cfe7abd6c3f72 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 27 Jun 2023 18:36:02 +0100 Subject: Introduce WasmFuncName --- .../wasm/structure/ast/modules/WasmFuncName.java | 4 ++++ .../wasm/structure/ast/modules/WasmModule.java | 15 ------------ .../structure/binary/WasmBinaryFormatModules.java | 5 ++++ .../binary/WasmBinaryFormatNameSection.java | 9 ++++---- .../wasm/structure/binary/WasmIndexTable.java | 27 ++++++++++++++++++++-- 5 files changed, 38 insertions(+), 22 deletions(-) create mode 100644 src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmFuncName.java (limited to 'src/main/java') 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 indexedFunctionDefinitions() { - var startIndex = imports.stream() - .filter(import_ -> import_.descriptor() instanceof WasmImportDescriptor.Func) - .count(); - - var indexedFunctions = new ArrayList(); - - 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(); + for (var indexedFunction : module.indexedFunctions()) { + functionNameToFuncIndex.put(indexedFunction.name(), indexedFunction.funcIndex()); + } + return new WasmIndexTable( - funcTypeToTypeIndex + funcTypeToTypeIndex, + functionNameToFuncIndex ); } private final Map funcTypeToTypeIndex; + private final Map functionNameToFuncIndex; - private WasmIndexTable(Map funcTypeToTypeIndex) { + private WasmIndexTable( + Map funcTypeToTypeIndex, + Map 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; + } + } } -- cgit v1.2.3