diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-07-21 16:22:34 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-07-21 16:23:14 +0100 |
| commit | 0fe26c78dd1f7127654cb2dfaef8f5caeebd7e73 (patch) | |
| tree | bd20bfc0915adecadf585cfedc6f40fabf8b915a /src/main/java | |
| parent | f12f949dda91f596c8ec401375e48cef584218c2 (diff) | |
Support wasm-patio backend in CLI
Diffstat (limited to 'src/main/java')
4 files changed, 45 insertions, 18 deletions
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<String, Backend> BACKENDS = OrderedPMap.<String, Backend>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<Path>(); 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<Instruction> stackIrInstructions, + StackIrNamespace stackIrNamespace, PatioCodeGenContext context ) { context = PatioCodeGenNamespaces.define( - namespaceName, + stackIrNamespace.namespaceName(), initContext -> - generateInstructions(stackIrInstructions, initContext), + generateInstructions(stackIrNamespace.instructions(), initContext), context ); |
