aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-06-24 21:12:17 +0100
committerMichael Williamson <mike@zwobble.org>2023-06-24 21:12:17 +0100
commit1a43e1686bd55f5607459a80ff3cc8e34bbfde70 (patch)
treeced63b374eff6569db2bb4caf3e8f5caf38a402b
parent9846bb9db8bd0aa6e84780bda459f4642eb45213 (diff)
Fix func index in local names section
-rw-r--r--snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormatModuleTests]/[method:localNamesHaveCorrectFuncIndexWhenThereAreImportedFunctions(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected24
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmModule.java20
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatNameSection.java9
-rw-r--r--src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java22
4 files changed, 71 insertions, 4 deletions
diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormatModuleTests]/[method:localNamesHaveCorrectFuncIndexWhenThereAreImportedFunctions(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormatModuleTests]/[method:localNamesHaveCorrectFuncIndexWhenThereAreImportedFunctions(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected
new file mode 100644
index 00000000..a1eb454f
--- /dev/null
+++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormatModuleTests]/[method:localNamesHaveCorrectFuncIndexWhenThereAreImportedFunctions(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected
@@ -0,0 +1,24 @@
+
+module.wasm: file format wasm 0x1
+
+Section Details:
+
+Type[2]:
+ - type[0] (f32) -> i32
+ - type[1] () -> nil
+Import[1]:
+ - func[0] sig=1 <Identifier1> <- ModuleName1.EntityName1
+Function[1]:
+ - func[1] sig=0
+Code[1]:
+ - func[1] size=4
+Custom:
+ - name: "name"
+ - func[0] <Identifier1>
+ - func[1] local[0] <paramA>
+
+Code Disassembly:
+
+00004c func[1]:
+ 00004d: 41 00 | i32.const 0
+ 00004f: 0b | end \ No newline at end of file
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(),
diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java
index 5941c790..5495d798 100644
--- a/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java
+++ b/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java
@@ -92,6 +92,28 @@ public class WasmBinaryFormatModuleTests {
}
@Test
+ public void localNamesHaveCorrectFuncIndexWhenThereAreImportedFunctions(Snapshotter snapshotter) throws IOException, InterruptedException {
+ var module = WasmModule.EMPTY
+ .addFunction(new WasmFunction(
+ List.of(
+ new WasmLocal("paramA", WasmValueType.F32)
+ ),
+ List.of(),
+ List.of(WasmValueType.I32),
+ List.of(new WasmI32Const(0))
+ ))
+ .addImport(new WasmImport(
+ "ModuleName1",
+ "EntityName1",
+ new WasmImportDescriptor.Func("Identifier1", new WasmFuncType(List.of(), List.of()))
+ ));
+
+ var result = serializeModule(module);
+
+ snapshotter.assertSnapshot(result);
+ }
+
+ @Test
public void canSerializeModuleWithTableImports(Snapshotter snapshotter) throws IOException, InterruptedException {
var module = WasmModule.EMPTY
.addImport(new WasmImport(