From b91f3ecbba9954f7f46d954918d87266d1518af7 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 25 Jun 2023 15:44:21 +0100 Subject: Add WASM code gen for push-bool instruction --- .../compiler/backends/wasm/WasmCodeGen.java | 47 ++++++++++++++++++++ .../compiler/backends/wasm/WasmCodeGenContext.java | 51 ++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java create mode 100644 src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenContext.java (limited to 'src/main/java') 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 functions, + PVector 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; + } +} -- cgit v1.2.3