aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-06-25 15:44:21 +0100
committerMichael Williamson <mike@zwobble.org>2023-06-25 15:44:21 +0100
commitb91f3ecbba9954f7f46d954918d87266d1518af7 (patch)
tree541dbbe5dee2f65e2e22350e0c3d290de8b85f77 /src
parent9a60f58e4ba7889f87561945fa474adfcdbd46a5 (diff)
Add WASM code gen for push-bool instruction
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java47
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenContext.java51
-rw-r--r--src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenPushBoolTests.java36
-rw-r--r--src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java6
-rw-r--r--src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmObjdump.java28
5 files changed, 164 insertions, 4 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java
new file mode 100644
index 00000000..b6e2a09f
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java
@@ -0,0 +1,47 @@
+package org.shedlang.compiler.backends.wasm;
+
+import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmI32Const;
+import org.shedlang.compiler.stackir.instructions.*;
+
+public class WasmCodeGen {
+ private WasmCodeGen() {
+ }
+
+ public static WasmCodeGenContext generateInstruction(Instruction instruction, WasmCodeGenContext context) {
+ return switch (instruction) {
+ case CallInstruction callInstruction -> {
+ throw new UnsupportedOperationException("TODO");
+ }
+ case DefineFunctionInstruction defineFunctionInstruction -> {
+ throw new UnsupportedOperationException("TODO");
+ }
+ case DiscardInstruction discardInstruction -> {
+ throw new UnsupportedOperationException("TODO");
+ }
+ case LocalLoadInstruction localLoadInstruction -> {
+ throw new UnsupportedOperationException("TODO");
+ }
+ case LocalStoreInstruction localStoreInstruction -> {
+ throw new UnsupportedOperationException("TODO");
+ }
+ case NamespaceFieldStoreInstruction namespaceFieldStoreInstruction -> {
+ throw new UnsupportedOperationException("TODO");
+ }
+ case PushBoolInstruction pushBool ->
+ context.addInstruction(new WasmI32Const(pushBool.value() ? 1 : 0));
+
+ case PushIntInstruction pushIntInstruction -> {
+ throw new UnsupportedOperationException("TODO");
+ }
+ case PushStringInstruction pushStringInstruction -> {
+ throw new UnsupportedOperationException("TODO");
+ }
+ case PushUnitInstruction pushUnitInstruction -> {
+ throw new UnsupportedOperationException("TODO");
+ }
+ case ReturnInstruction returnInstruction -> {
+ throw new UnsupportedOperationException("TODO");
+ }
+ };
+ }
+}
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenContext.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenContext.java
new file mode 100644
index 00000000..125d125c
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenContext.java
@@ -0,0 +1,51 @@
+package org.shedlang.compiler.backends.wasm;
+
+import org.pcollections.Empty;
+import org.pcollections.PVector;
+import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmExpression;
+import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmInstruction;
+import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmFunction;
+import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmModule;
+
+import java.util.List;
+
+public record WasmCodeGenContext(
+ PVector<WasmFunction> functions,
+ PVector<WasmInstruction> instructions
+) {
+ public static final WasmCodeGenContext EMPTY = new WasmCodeGenContext(
+ Empty.vector(),
+ Empty.vector()
+ );
+
+ public WasmCodeGenContext addInstruction(WasmInstruction instruction) {
+ return new WasmCodeGenContext(
+ functions,
+ instructions.plus(instruction)
+ );
+ }
+
+ public WasmCodeGenContext toFunction() {
+ var function = new WasmFunction(
+ "f",
+ List.of(),
+ List.of(),
+ List.of(),
+ new WasmExpression(instructions)
+ );
+ return new WasmCodeGenContext(
+ functions.plus(function),
+ instructions
+ );
+ }
+
+ public WasmModule toModule() {
+ var module = WasmModule.EMPTY;
+
+ for (var function : functions) {
+ module = module.addFunction(function);
+ }
+
+ return module;
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenPushBoolTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenPushBoolTests.java
new file mode 100644
index 00000000..e7b2f57c
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenPushBoolTests.java
@@ -0,0 +1,36 @@
+package org.shedlang.compiler.backends.wasm;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.shedlang.compiler.stackir.instructions.Instructions;
+import org.shedlang.compiler.testing.snapshots.SnapshotResolver;
+import org.shedlang.compiler.testing.snapshots.Snapshotter;
+
+import java.io.IOException;
+
+import static org.shedlang.compiler.backends.wasm.structure.WasmObjdump.objdump;
+
+@ExtendWith({SnapshotResolver.class})
+public class WasmCodeGenPushBoolTests {
+ @Test
+ public void pushFalseIsCompiledToOperandInStack(Snapshotter snapshotter) throws IOException, InterruptedException {
+ var instruction = Instructions.pushBool(false);
+ var context = WasmCodeGenContext.EMPTY;
+
+ var result = WasmCodeGen.generateInstruction(instruction, context);
+
+ var module = result.toFunction().toModule();
+ snapshotter.assertSnapshot(objdump(module));
+ }
+
+ @Test
+ public void pushTrueIsCompiledToOperandInStack(Snapshotter snapshotter) throws IOException, InterruptedException {
+ var instruction = Instructions.pushBool(true);
+ var context = WasmCodeGenContext.EMPTY;
+
+ var result = WasmCodeGen.generateInstruction(instruction, context);
+
+ var module = result.toFunction().toModule();
+ snapshotter.assertSnapshot(objdump(module));
+ }
+}
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 8f36def5..a5998e10 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
@@ -20,6 +20,8 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
+import static org.shedlang.compiler.backends.wasm.structure.WasmObjdump.objdump;
+
@ExtendWith({SnapshotResolver.class})
public class WasmBinaryFormatModuleTests {
@Test
@@ -367,8 +369,4 @@ public class WasmBinaryFormatModuleTests {
ProcessRunner.run(List.of("wasm-validate", "--enable-exceptions", path.toString()))
.throwOnError();
}
-
- private String objdump(Path path) throws IOException, InterruptedException {
- return ProcessRunner.run(List.of("wasm-objdump", "-dx", path.toString())).throwOnError().stdout();
- }
}
diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmObjdump.java b/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmObjdump.java
new file mode 100644
index 00000000..8caadd1a
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmObjdump.java
@@ -0,0 +1,28 @@
+package org.shedlang.compiler.backends.wasm.structure;
+
+import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmModule;
+import org.shedlang.compiler.testing.ProcessRunner;
+
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+
+public class WasmObjdump {
+ public static String objdump(WasmModule module) throws IOException, InterruptedException {
+ Files.createDirectories(Path.of("tests-tmp"));
+ var temporaryDirectory = Files.createTempDirectory(Path.of("tests-tmp"), "WasmObjdump");
+ var modulePath = temporaryDirectory.resolve("module.wasm");
+
+ try (var outputStream = new FileOutputStream(modulePath.toFile())) {
+ WasmBinaryFormat.serializeModule(module, outputStream);
+ }
+
+ return objdump(modulePath);
+ }
+
+ public static String objdump(Path path) throws IOException, InterruptedException {
+ return ProcessRunner.run(List.of("wasm-objdump", "-dx", path.toString())).throwOnError().stdout();
+ }
+}