aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
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/main/java
parent9a60f58e4ba7889f87561945fa474adfcdbd46a5 (diff)
Add WASM code gen for push-bool instruction
Diffstat (limited to 'src/main/java')
-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
2 files changed, 98 insertions, 0 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;
+ }
+}