From 1a43e1686bd55f5607459a80ff3cc8e34bbfde70 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 24 Jun 2023 21:12:17 +0100 Subject: Fix func index in local names section --- .../wasm/structure/ast/modules/WasmModule.java | 20 ++++++++++++++++++++ .../binary/WasmBinaryFormatNameSection.java | 9 +++++---- 2 files changed, 25 insertions(+), 4 deletions(-) (limited to 'src/main/java') 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 types, PVector functions, @@ -20,6 +23,23 @@ public record WasmModule( P.vector() ); + public record IndexedFunction(int funcIndex, WasmFunction function) { + } + + public List indexedFunctions() { + var funcIndex = imports.stream() + .filter(import_ -> import_.descriptor() instanceof WasmImportDescriptor.Func) + .count(); + + var indexedFunctions = new ArrayList(); + 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(), -- cgit v1.2.3