aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-06-24 21:03:46 +0100
committerMichael Williamson <mike@zwobble.org>2023-06-24 21:03:46 +0100
commit9846bb9db8bd0aa6e84780bda459f4642eb45213 (patch)
treef3281634346035a198609ceca33c4bd5ea6d214c /src/main/java
parent41d4ba7f621b613a8c332addf2bd54f2c78e2d72 (diff)
Add local names to WASM object file
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmFunction.java13
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmLocal.java6
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatModules.java4
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatNameSection.java53
4 files changed, 64 insertions, 12 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmFunction.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmFunction.java
index 50ad805b..0f295e90 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmFunction.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmFunction.java
@@ -7,8 +7,17 @@ import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmValueType;
import java.util.List;
public record WasmFunction(
- WasmFuncType type,
- List<WasmValueType> locals,
+ List<WasmLocal> params,
+ List<WasmLocal> locals,
+ List<WasmValueType> resultTypes,
List<WasmInstruction> body
) {
+ public WasmFuncType type() {
+ return new WasmFuncType(
+ params.stream()
+ .map(param -> param.type())
+ .toList(),
+ resultTypes
+ );
+ }
}
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmLocal.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmLocal.java
new file mode 100644
index 00000000..d9813336
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmLocal.java
@@ -0,0 +1,6 @@
+package org.shedlang.compiler.backends.wasm.structure.ast.modules;
+
+import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmValueType;
+
+public record WasmLocal(String name, WasmValueType type) {
+}
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 58ae08a8..4f684f8f 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
@@ -215,7 +215,7 @@ public class WasmBinaryFormatModules {
function.locals(),
local -> {
Leb128Encoding.encodeUnsignedInt32(1, writer);
- writeValueType(local, writer);
+ writeValueType(local.type(), writer);
},
writer
);
@@ -257,7 +257,7 @@ public class WasmBinaryFormatModules {
writeTableSection(module, writer);
writeMemorySection(module, writer);
writeCodeSection(module, writer);
- writeNameSection(module, writer);
+ writeNameSection(module, indexTable, writer);
outputStream.write(writer.toBytes());
}
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 67caa7c6..50c98879 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.WasmLocal;
import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmModule;
import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmImportDescriptor;
import org.shedlang.compiler.backends.wasm.structure.binary.WasmBinaryFormatModules.SectionId;
@@ -23,13 +24,16 @@ public class WasmBinaryFormatNameSection {
// == Name Section ==
- static void writeNameSection(WasmModule module, BinaryWriter writer) throws IOException {
- if (module.imports().size() > 0) {
- writeSection(SectionId.CUSTOM, writer, () -> {
- writeString("name", writer);
- writeNameSectionFunctionNameSubsection(module, writer);
- });
- }
+ static void writeNameSection(
+ WasmModule module,
+ WasmIndexTable indexTable,
+ BinaryWriter writer
+ ) throws IOException {
+ writeSection(SectionId.CUSTOM, writer, () -> {
+ writeString("name", writer);
+ writeFunctionNameSubsection(module, writer);
+ writeLocalNameSubsection(module, indexTable, writer);
+ });
}
// == Subsections ==
@@ -71,7 +75,7 @@ public class WasmBinaryFormatNameSection {
// == Function Names ==
- private static void writeNameSectionFunctionNameSubsection(WasmModule module, BinaryWriter writer) throws IOException {
+ private static void writeFunctionNameSubsection(WasmModule module, BinaryWriter writer) throws IOException {
writeNameSubsection(NameSubsectionId.FUNCTION_NAMES, writer, () -> {
var importedFunctions = module.imports().stream()
.flatMap(import_ -> switch (import_.descriptor()) {
@@ -83,4 +87,37 @@ public class WasmBinaryFormatNameSection {
writeNameMap(writer, importedFunctions);
});
}
+
+ // == Local Names ==
+
+ private static void writeLocalNameSubsection(
+ WasmModule module,
+ WasmIndexTable indexTable,
+ BinaryWriter writer
+ ) throws IOException {
+ writeNameSubsection(NameSubsectionId.LOCAL_NAMES, writer, () -> {
+ writeVec(
+ module.functions(),
+ function -> {
+ var funcIndex = indexTable.funcIndex(function.type());
+ writeFuncIndex(funcIndex, writer);
+
+ var names = Stream.concat(
+ function.params().stream(),
+ function.locals().stream()
+ ).map(WasmLocal::name).toList();
+
+ writeVec(
+ names,
+ (nameIndex, name) -> {
+ Leb128Encoding.encodeUnsignedInt32(nameIndex, writer);
+ writeString(name, writer);
+ },
+ writer
+ );
+ },
+ writer
+ );
+ });
+ }
}