aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-11-20 22:08:51 +0000
committerMichael Williamson <mike@zwobble.org>2023-11-20 22:08:51 +0000
commit1748d528ba9886c663ca1d24c2eac848e89161c5 (patch)
tree537fd629a2ccff3e542bd6c27da11c85194967b8 /src/test/java/org
parentb2f3466b7a79507244956270b5b1f1f895354de6 (diff)
Add tests for alloc-stack
Diffstat (limited to 'src/test/java/org')
-rw-r--r--src/test/java/org/shedlang/compiler/backends/wasm/WasmPatioCodeGenTests.java19
-rw-r--r--src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java51
2 files changed, 66 insertions, 4 deletions
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;
@@ -18,6 +19,56 @@ 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()
.withName(FUNCTION_NAME)