diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-06-24 21:12:17 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-06-24 21:12:17 +0100 |
| commit | 1a43e1686bd55f5607459a80ff3cc8e34bbfde70 (patch) | |
| tree | ced63b374eff6569db2bb4caf3e8f5caf38a402b /src/main/java | |
| parent | 9846bb9db8bd0aa6e84780bda459f4642eb45213 (diff) | |
Fix func index in local names section
Diffstat (limited to 'src/main/java')
2 files changed, 25 insertions, 4 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 7b19cee9..d2b0b28e 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 @@ -5,6 +5,9 @@ import org.pcollections.PVector; import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmFuncType; import org.shedlang.compiler.util.P; +import java.util.ArrayList; +import java.util.List; + public record WasmModule( OrderedPSet<WasmFuncType> types, PVector<WasmFunction> functions, @@ -20,6 +23,23 @@ public record WasmModule( P.vector() ); + public record IndexedFunction(int funcIndex, WasmFunction function) { + } + + public List<IndexedFunction> indexedFunctions() { + var funcIndex = imports.stream() + .filter(import_ -> import_.descriptor() instanceof WasmImportDescriptor.Func) + .count(); + + var indexedFunctions = new ArrayList<IndexedFunction>(); + for (var function : functions) { + indexedFunctions.add(new IndexedFunction((int) funcIndex, function)); + funcIndex++; + } + + return indexedFunctions; + } + public WasmModule addType(WasmFuncType type) { return new WasmModule( types.plus(type), 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 50c98879..2fc0e176 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 @@ -97,10 +97,11 @@ public class WasmBinaryFormatNameSection { ) throws IOException { writeNameSubsection(NameSubsectionId.LOCAL_NAMES, writer, () -> { writeVec( - module.functions(), - function -> { - var funcIndex = indexTable.funcIndex(function.type()); - writeFuncIndex(funcIndex, writer); + module.indexedFunctions(), + indexedFunction -> { + writeFuncIndex(indexedFunction.funcIndex(), writer); + + var function = indexedFunction.function(); var names = Stream.concat( function.params().stream(), |
