From 936ac762b46f52b2cb03f386c48cb85567ea89c7 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 24 Jun 2023 23:06:35 +0100 Subject: Add explicit WASM index types --- .../backends/wasm/structure/ast/modules/WasmFuncIndex.java | 4 ++++ .../backends/wasm/structure/ast/modules/WasmModule.java | 4 ++-- .../backends/wasm/structure/ast/modules/WasmTypeIndex.java | 4 ++++ .../wasm/structure/binary/WasmBinaryFormatModules.java | 12 ++++++------ .../wasm/structure/binary/WasmBinaryFormatNameSection.java | 9 ++++++--- .../backends/wasm/structure/binary/WasmIndexTable.java | 11 ++++++----- 6 files changed, 28 insertions(+), 16 deletions(-) create mode 100644 src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmFuncIndex.java create mode 100644 src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmTypeIndex.java (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmFuncIndex.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmFuncIndex.java new file mode 100644 index 00000000..ca17f676 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmFuncIndex.java @@ -0,0 +1,4 @@ +package org.shedlang.compiler.backends.wasm.structure.ast.modules; + +public record WasmFuncIndex(int 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 d2b0b28e..0a15cc70 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 @@ -23,7 +23,7 @@ public record WasmModule( P.vector() ); - public record IndexedFunction(int funcIndex, WasmFunction function) { + public record IndexedFunction(WasmFuncIndex funcIndex, WasmFunction function) { } public List indexedFunctions() { @@ -33,7 +33,7 @@ public record WasmModule( var indexedFunctions = new ArrayList(); for (var function : functions) { - indexedFunctions.add(new IndexedFunction((int) funcIndex, function)); + indexedFunctions.add(new IndexedFunction(new WasmFuncIndex((int) funcIndex), function)); funcIndex++; } diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmTypeIndex.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmTypeIndex.java new file mode 100644 index 00000000..c17eac34 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmTypeIndex.java @@ -0,0 +1,4 @@ +package org.shedlang.compiler.backends.wasm.structure.ast.modules; + +public record WasmTypeIndex(int value) { +} 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 4f684f8f..faed48d5 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 @@ -24,12 +24,12 @@ public class WasmBinaryFormatModules { // == Indices == - static void writeTypeIndex(int typeIndex, BinaryWriter writer) { - Leb128Encoding.encodeUnsignedInt32(typeIndex, writer); + static void writeTypeIndex(WasmTypeIndex typeIndex, BinaryWriter writer) { + Leb128Encoding.encodeUnsignedInt32(typeIndex.value(), writer); } - static void writeFuncIndex(int funcIndex, BinaryWriter writer) { - Leb128Encoding.encodeUnsignedInt32(funcIndex, writer); + static void writeFuncIndex(WasmFuncIndex funcIndex, BinaryWriter writer) { + Leb128Encoding.encodeUnsignedInt32(funcIndex.value(), writer); } // == Sections == @@ -130,7 +130,7 @@ public class WasmBinaryFormatModules { switch (descriptor) { case WasmImportDescriptor.Func func -> { writer.writeByte(0x00); - var typeIndex = indexTable.funcIndex(func.type()); + var typeIndex = indexTable.typeIndex(func.type()); writeTypeIndex(typeIndex, writer); } case WasmImportDescriptor.Table table -> { @@ -160,7 +160,7 @@ public class WasmBinaryFormatModules { writeVec( module.functions(), function -> writeTypeIndex( - indexTable.funcIndex(function.type()), + indexTable.typeIndex(function.type()), writer ), writer 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 3f5b7061..82cd99c9 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,6 +1,7 @@ package org.shedlang.compiler.backends.wasm.structure.binary; import org.shedlang.compiler.backends.wasm.structure.BinaryWriter; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmFuncIndex; import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmLocal; import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmModule; import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmImportDescriptor; @@ -63,7 +64,7 @@ public class WasmBinaryFormatNameSection { // == Name Maps == - private record NameAssoc(int index, String name) { + private record NameAssoc(WasmFuncIndex index, String name) { } @@ -85,11 +86,13 @@ public class WasmBinaryFormatNameSection { var functionNames = new ArrayList(); for (var import_ : module.imports()) { if (import_.descriptor() instanceof WasmImportDescriptor.Func func) { - functionNames.add(new NameAssoc(functionNames.size(), func.identifier())); + var funcIndex = new WasmFuncIndex(functionNames.size()); + functionNames.add(new NameAssoc(funcIndex, func.identifier())); } } for (var function : module.functions()) { - functionNames.add(new NameAssoc(functionNames.size(), function.name())); + var fundIndex = new WasmFuncIndex(functionNames.size()); + functionNames.add(new NameAssoc(fundIndex, function.name())); } writeNameMap(writer, functionNames); 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 0b2c5c56..d32e1de9 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,6 +1,7 @@ package org.shedlang.compiler.backends.wasm.structure.binary; 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; import java.util.HashMap; @@ -8,9 +9,9 @@ import java.util.Map; public class WasmIndexTable { public static WasmIndexTable fromModule(WasmModule module) { - var funcTypeToTypeIndex = new HashMap(); + var funcTypeToTypeIndex = new HashMap(); for (var funcType : module.types()) { - funcTypeToTypeIndex.put(funcType, funcTypeToTypeIndex.size()); + funcTypeToTypeIndex.put(funcType, new WasmTypeIndex(funcTypeToTypeIndex.size())); } return new WasmIndexTable( @@ -18,13 +19,13 @@ public class WasmIndexTable { ); } - private final Map funcTypeToTypeIndex; + private final Map funcTypeToTypeIndex; - private WasmIndexTable(Map funcTypeToTypeIndex) { + private WasmIndexTable(Map funcTypeToTypeIndex) { this.funcTypeToTypeIndex = funcTypeToTypeIndex; } - public int funcIndex(WasmFuncType type) { + public WasmTypeIndex typeIndex(WasmFuncType type) { return funcTypeToTypeIndex.get(type); } } -- cgit v1.2.3