aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-06-24 23:06:35 +0100
committerMichael Williamson <mike@zwobble.org>2023-06-24 23:06:35 +0100
commit936ac762b46f52b2cb03f386c48cb85567ea89c7 (patch)
tree81008fb2aa012b94a21bba0b43430694269ea998 /src/main/java
parent5e15348d603b824f7d8725cb33852881294935ab (diff)
Add explicit WASM index types
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmFuncIndex.java4
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmModule.java4
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmTypeIndex.java4
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatModules.java12
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatNameSection.java9
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmIndexTable.java11
6 files changed, 28 insertions, 16 deletions
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<IndexedFunction> indexedFunctions() {
@@ -33,7 +33,7 @@ public record WasmModule(
var indexedFunctions = new ArrayList<IndexedFunction>();
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<NameAssoc>();
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<WasmFuncType, Integer>();
+ var funcTypeToTypeIndex = new HashMap<WasmFuncType, WasmTypeIndex>();
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<WasmFuncType, Integer> funcTypeToTypeIndex;
+ private final Map<WasmFuncType, WasmTypeIndex> funcTypeToTypeIndex;
- private WasmIndexTable(Map<WasmFuncType, Integer> funcTypeToTypeIndex) {
+ private WasmIndexTable(Map<WasmFuncType, WasmTypeIndex> funcTypeToTypeIndex) {
this.funcTypeToTypeIndex = funcTypeToTypeIndex;
}
- public int funcIndex(WasmFuncType type) {
+ public WasmTypeIndex typeIndex(WasmFuncType type) {
return funcTypeToTypeIndex.get(type);
}
}