From 209cafc729a344efa002c545cae1f944061709d3 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 29 Jul 2023 14:02:31 +0100 Subject: Use PatioExampleTests for WASM Patio code gen --- .../backends/wasm/WasmPatioCodeGenTests.java | 75 ++++++++++++++++++++++ .../shedlang/compiler/patio/PatioExampleTests.java | 6 +- 2 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 src/test/java/org/shedlang/compiler/backends/wasm/WasmPatioCodeGenTests.java (limited to 'src/test/java') diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/WasmPatioCodeGenTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/WasmPatioCodeGenTests.java new file mode 100644 index 00000000..fbb22bc1 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/backends/wasm/WasmPatioCodeGenTests.java @@ -0,0 +1,75 @@ +package org.shedlang.compiler.backends.wasm; + +import org.shedlang.compiler.backends.llvm.ExecutionResult; +import org.shedlang.compiler.backends.wasm.structure.WasmBinaryFormat; +import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmCall; +import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmExpression; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmFuncName; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmFunction; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmImport; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmImportDescriptor; +import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmFuncType; +import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmValueType; +import org.shedlang.compiler.filesystem.ShedCompilerFiles; +import org.shedlang.compiler.patio.PatioExampleTests; +import org.shedlang.compiler.patio.ast.PatioCompilationUnit; +import org.shedlang.compiler.testing.TestProcessRunner; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; + +import static org.shedlang.compiler.backends.ProcessRunner.runInheritIo; + +public class WasmPatioCodeGenTests extends PatioExampleTests { + @Override + protected ExecutionResult run(PatioCompilationUnit patio) throws IOException, InterruptedException { + var wasmModule = WasmPatioCodeGen.generateCompilationUnit(patio); + + wasmModule = wasmModule.addFunction(new WasmFunction( + "_start", + List.of(), + List.of(), + List.of(), + new WasmExpression(List.of( + new WasmCall(new WasmFuncName(FUNCTION_NAME)), + new WasmCall(new WasmFuncName("proc_exit")) + )) + )); + + wasmModule = wasmModule.addImport(new WasmImport( + "wasi_snapshot_preview1", + "proc_exit", + new WasmImportDescriptor.Func("proc_exit", new WasmFuncType( + List.of(WasmValueType.I32), + List.of() + )) + )); + + Files.createDirectories(Path.of("tests-tmp")); + var workingDirectory = Files.createTempDirectory(Path.of("tests-tmp"), "WasmPatioCodeGen"); + + var objectPath = workingDirectory.resolve("main.o"); + try (var outputStream = new FileOutputStream(objectPath.toFile())) { + WasmBinaryFormat.serializeModule(wasmModule, outputStream); + } + + var ldArgs = new ArrayList(); + ldArgs.add("wasm-ld"); + + ldArgs.add("-o"); + var executablePath = workingDirectory.resolve("main.wasm"); + ldArgs.add(executablePath.toString()); + + ldArgs.add(objectPath.toString()); + + ldArgs.add(ShedCompilerFiles.getRoot().resolve("runtime/build-wasm/libshed.o").toString()); + + runInheritIo(ldArgs); + + return TestProcessRunner.runCaptureOutput(List.of("wasmtime", executablePath.toString())); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java b/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java index 8b03edae..d0fffc12 100644 --- a/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java +++ b/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java @@ -30,7 +30,7 @@ public abstract class PatioExampleTests { )) .addBodyInstruction(Patio.localStore( Patio.localRef("y"), - Patio.int_(100) + Patio.int_(5) )) .addBodyInstruction(Patio.intAdd( Patio.localRef("z"), @@ -43,7 +43,9 @@ public abstract class PatioExampleTests { var result = run(patio); - assertThat(result.exitCode(), equalTo(142)); + assertThat(result.stderr(), equalTo("")); + assertThat(result.stdout(), equalTo("")); + assertThat(result.exitCode(), equalTo(47)); } protected abstract ExecutionResult run(PatioCompilationUnit patio) throws IOException, InterruptedException; -- cgit v1.2.3