From 1748d528ba9886c663ca1d24c2eac848e89161c5 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 20 Nov 2023 22:08:51 +0000 Subject: Add tests for alloc-stack --- .../backends/wasm/WasmPatioCodeGenTests.java | 19 ++++++-- .../shedlang/compiler/patio/PatioExampleTests.java | 51 ++++++++++++++++++++++ 2 files changed, 66 insertions(+), 4 deletions(-) (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 index 20221870..798f773f 100644 --- a/src/test/java/org/shedlang/compiler/backends/wasm/WasmPatioCodeGenTests.java +++ b/src/test/java/org/shedlang/compiler/backends/wasm/WasmPatioCodeGenTests.java @@ -3,10 +3,9 @@ package org.shedlang.compiler.backends.wasm; 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.instructions.WasmLocalGet; +import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmMemoryGrow; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.*; import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmFuncType; import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmValueType; import org.shedlang.compiler.patio.PatioExampleTests; @@ -25,6 +24,18 @@ public class WasmPatioCodeGenTests extends PatioExampleTests { protected ExecutionResult run(PatioCompilationUnit patio) throws IOException, InterruptedException { var wasmModule = WasmPatioCodeGen.generateCompilationUnit(patio); + // TODO: put these in separate module, remove duplication + wasmModule = wasmModule.addFunction(new WasmFunction( + "wasm_memory_grow", + List.of(new WasmLocal("pages", WasmValueType.I32)), + List.of(), + List.of(WasmValueType.I32), + new WasmExpression(List.of( + new WasmLocalGet(new WasmLocalIndex(0)), + new WasmMemoryGrow() + )) + )); + wasmModule = wasmModule.addFunction(new WasmFunction( "_start", List.of(), diff --git a/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java b/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java index ae9c141e..f7ef017a 100644 --- a/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java +++ b/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java @@ -3,6 +3,7 @@ package org.shedlang.compiler.patio; import org.junit.jupiter.api.Test; import org.shedlang.compiler.patio.ast.PatioCompilationUnit; import org.shedlang.compiler.patio.ast.PatioFunctionDefinition; +import org.shedlang.compiler.patio.ast.types.PatioDataLayouts; import org.shedlang.compiler.patio.ast.types.PatioTypes; import org.shedlang.compiler.patio.instructions.Patio; import org.shedlang.compiler.testing.ExecutionResult; @@ -17,6 +18,56 @@ import static org.zwobble.precisely.Matchers.*; public abstract class PatioExampleTests { protected static final String FUNCTION_NAME = "run"; + @Test + public void allocStack() throws IOException, InterruptedException { + var patioFunction = PatioFunctionDefinition.builder() + .withName(FUNCTION_NAME) + .withReturnType(PatioTypes.INT) + .addLocal(Patio.val("x", PatioTypes.DATA_POINTER)) + .addLocal(Patio.val("y", PatioTypes.INT)) + .addLocal(Patio.val("z", PatioTypes.INT)) + .addLocal(Patio.val("r", PatioTypes.INT)) + .addBodyInstruction(Patio.allocStack(Patio.localRef("x"), PatioDataLayouts.INT)) + .addBodyInstruction(Patio.memoryStore( + Patio.localRef("x"), + PatioDataLayouts.INT, + List.of(), + Patio.int_(42) + )) + .addBodyInstruction(Patio.memoryLoad( + Patio.localRef("y"), + Patio.localRef("x"), + PatioDataLayouts.INT, + List.of() + )) + .addBodyInstruction(Patio.intAdd( + Patio.localRef("z"), + Patio.localRef("y"), + Patio.int_(5) + )) + .addBodyInstruction(Patio.memoryStore( + Patio.localRef("x"), + PatioDataLayouts.INT, + List.of(), + Patio.localRef("z") + )) + .addBodyInstruction(Patio.memoryLoad( + Patio.localRef("r"), + Patio.localRef("x"), + PatioDataLayouts.INT, + List.of() + )) + .addBodyInstruction(Patio.return_( + Patio.localRef("r") + )) + .build(); + var patio = Patio.compilationUnit(List.of(patioFunction)); + + var result = run(patio); + + assertThat(result, isSilentExit(47)); + } + @Test public void intAdd() throws IOException, InterruptedException { var patioFunction = PatioFunctionDefinition.builder() -- cgit v1.2.3