From d6e6efc43aa4f650725bbeba5450c6c44b56fd8f Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 28 Dec 2023 00:30:37 +0000 Subject: Add Patio example test for alloc-heap --- .../shedlang/compiler/patio/PatioExampleTests.java | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'src/test/java') diff --git a/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java b/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java index d25aa5a9..3855a497 100644 --- a/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java +++ b/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java @@ -88,6 +88,56 @@ public abstract class PatioExampleTests { assertThat(result, isSilentExit(42)); } + @Test + public void allocHeap() 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.allocHeap(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 allocStack() throws IOException, InterruptedException { var patioFunction = PatioFunctionDefinition.builder() -- cgit v1.2.3