From 0fe26c78dd1f7127654cb2dfaef8f5caeebd7e73 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 21 Jul 2023 16:22:34 +0100 Subject: Support wasm-patio backend in CLI --- .../org/shedlang/compiler/backends/Backends.java | 3 +- .../shedlang/compiler/backends/PatioBackend.java | 8 ++--- .../compiler/backends/wasm/WasmBackend.java | 39 +++++++++++++++++++--- .../org/shedlang/compiler/patio/PatioCodeGen.java | 13 ++++---- 4 files changed, 45 insertions(+), 18 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/backends/Backends.java b/src/main/java/org/shedlang/compiler/backends/Backends.java index 93801fbc..f0a75510 100644 --- a/src/main/java/org/shedlang/compiler/backends/Backends.java +++ b/src/main/java/org/shedlang/compiler/backends/Backends.java @@ -10,5 +10,6 @@ public class Backends { public static final OrderedPMap BACKENDS = OrderedPMap.empty() .plus("llvm", new LlvmBackend()) - .plus("wasm", new WasmBackend()); + .plus("wasm", WasmBackend.DIRECT) + .plus("wasm-patio", WasmBackend.PATIO); } diff --git a/src/main/java/org/shedlang/compiler/backends/PatioBackend.java b/src/main/java/org/shedlang/compiler/backends/PatioBackend.java index 9e8e5560..682bf79f 100644 --- a/src/main/java/org/shedlang/compiler/backends/PatioBackend.java +++ b/src/main/java/org/shedlang/compiler/backends/PatioBackend.java @@ -22,14 +22,10 @@ public class PatioBackend implements Backend { var context = PatioCodeGenContext.forTypeInfo(typeInfo); for (var namespace : namespaces) { - var namespaceName = namespace.namespaceName(); - var instructions = namespace.instructions(); - - System.out.println(namespaceName); + System.out.println(namespace.namespaceName()); var compilationUnit = PatioCodeGen.generateNamespace( - namespaceName, - instructions, + namespace, context ); diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java index d127525e..65d6068f 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java @@ -5,6 +5,8 @@ import org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormat; import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmModule; import org.shedlang.compiler.config.ProjectConfig; import org.shedlang.compiler.filesystem.ShedCompilerFiles; +import org.shedlang.compiler.patio.PatioCodeGen; +import org.shedlang.compiler.patio.PatioCodeGenContext; import org.shedlang.compiler.stackir.StackIrNamespace; import org.shedlang.compiler.typechecker.TypeInfo; @@ -18,6 +20,38 @@ import java.util.List; import static org.shedlang.compiler.backends.ProcessRunner.runInheritIo; public class WasmBackend implements Backend { + public static final WasmBackend DIRECT = new WasmBackend(new CodeGen() { + @Override + public WasmModule generateNamespace(StackIrNamespace stackIrNamespace, TypeInfo typeInfo) { + return WasmCodeGen.generateNamespace(stackIrNamespace, typeInfo); + } + }); + + public static final WasmBackend PATIO = new WasmBackend(new CodeGen() { + @Override + public WasmModule generateNamespace(StackIrNamespace stackIrNamespace, TypeInfo typeInfo) { + var patioCompilationUnit = PatioCodeGen.generateNamespace( + stackIrNamespace, + PatioCodeGenContext.forTypeInfo(typeInfo) + ); + + return WasmPatioCodeGen.generateCompilationUnit(patioCompilationUnit); + } + }); + + interface CodeGen { + WasmModule generateNamespace( + StackIrNamespace stackIrNamespace, + TypeInfo typeInfo + ); + } + + private final CodeGen codeGen; + + private WasmBackend(CodeGen codeGen) { + this.codeGen = codeGen; + } + @Override public void compile( ProjectConfig config, @@ -27,10 +61,7 @@ public class WasmBackend implements Backend { ) throws IOException, InterruptedException { var objectFilePaths = new ArrayList(); for (var namespace : namespaces) { - var wasmModule = WasmCodeGen.generateNamespace( - namespace, - typeInfo - ); + var wasmModule = this.codeGen.generateNamespace(namespace, typeInfo); var objectPath = writeObjectFile( wasmModule, diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java index a96f07f4..47e7c496 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java @@ -1,15 +1,15 @@ package org.shedlang.compiler.patio; import org.shedlang.compiler.patio.ast.PatioCompilationUnit; +import org.shedlang.compiler.patio.ast.instructions.PatioIntAdd; import org.shedlang.compiler.patio.ast.instructions.PatioLocalStore; +import org.shedlang.compiler.patio.ast.instructions.PatioReturn; import org.shedlang.compiler.patio.ast.operands.PatioInt; import org.shedlang.compiler.patio.ast.operands.PatioLocalRef; -import org.shedlang.compiler.patio.ast.instructions.PatioIntAdd; -import org.shedlang.compiler.patio.ast.instructions.PatioReturn; import org.shedlang.compiler.sources.Source; import org.shedlang.compiler.sources.Sources; +import org.shedlang.compiler.stackir.StackIrNamespace; import org.shedlang.compiler.stackir.instructions.*; -import org.shedlang.compiler.types.NamespaceName; import java.util.ArrayList; import java.util.Collections; @@ -22,14 +22,13 @@ public class PatioCodeGen { private PatioCodeGen() {} public static PatioCompilationUnit generateNamespace( - NamespaceName namespaceName, - List stackIrInstructions, + StackIrNamespace stackIrNamespace, PatioCodeGenContext context ) { context = PatioCodeGenNamespaces.define( - namespaceName, + stackIrNamespace.namespaceName(), initContext -> - generateInstructions(stackIrInstructions, initContext), + generateInstructions(stackIrNamespace.instructions(), initContext), context ); -- cgit v1.2.3