From 8ae1e2dad3d43a75874e7cbb479601b5abae3b78 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 20 Jul 2023 14:04:22 +0100 Subject: Pass data layout to alloc --- .../java/org/shedlang/compiler/patio/ast/PatioSerializer.java | 2 +- .../shedlang/compiler/patio/ast/instructions/PatioAlloc.java | 11 ++++++++--- .../org/shedlang/compiler/patio/PatioSerializerTests.java | 9 ++++++--- .../java/org/shedlang/compiler/patio/instructions/Patio.java | 4 ++-- 4 files changed, 17 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java b/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java index 9447ab14..c38851f7 100644 --- a/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java +++ b/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java @@ -73,7 +73,7 @@ public class PatioSerializer { treeList( treeIdentifier("alloc"), localRefToTree(alloc.target()), - treeInt(alloc.bytes()) + dataLayoutToTree(alloc.layout()) ); case PatioCallPointer callPointer -> diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioAlloc.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioAlloc.java index 5dad5de6..279ae0db 100644 --- a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioAlloc.java +++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioAlloc.java @@ -1,11 +1,16 @@ package org.shedlang.compiler.patio.ast.instructions; import org.shedlang.compiler.patio.ast.PatioLocalRef; +import org.shedlang.compiler.patio.ast.types.PatioDataLayout; import org.shedlang.compiler.sources.Source; /** - * Allocate the given number of bytes, and store the pointer to the allocated - * memory in the target local. + * Allocate space for the given data layout, and store the pointer to the + * allocated memory in the target local. */ -public record PatioAlloc(PatioLocalRef target, int bytes, Source source) implements PatioInstruction { +public record PatioAlloc( + PatioLocalRef target, + PatioDataLayout layout, + Source source +) implements PatioInstruction { } diff --git a/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java b/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java index 5f7f83c9..ac997815 100644 --- a/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java +++ b/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java @@ -1,9 +1,9 @@ package org.shedlang.compiler.patio; import org.junit.jupiter.api.Test; +import org.shedlang.compiler.patio.ast.PatioFunctionDefinition; import org.shedlang.compiler.patio.ast.types.PatioTypes; import org.shedlang.compiler.patio.instructions.Patio; -import org.shedlang.compiler.patio.ast.PatioFunctionDefinition; import java.util.List; @@ -35,11 +35,14 @@ public class PatioSerializerTests { @Test public void canSerializeAlloc() { - var instruction = Patio.alloc("x", 42); + var instruction = Patio.alloc( + "x", + PatioTypes.struct(List.of(PatioTypes.INT, PatioTypes.INT, PatioTypes.DATA_POINTER)) + ); var string = PatioTesting.serializeInstruction(instruction); - assertThat(string, equalTo("(alloc \"x\" 42)")); + assertThat(string, equalTo("(alloc \"x\" (struct (int int data-ptr)))")); } @Test diff --git a/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java b/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java index 42808bd4..93715a54 100644 --- a/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java +++ b/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java @@ -10,8 +10,8 @@ import java.util.List; public class Patio { private Patio() {} - public static PatioInstruction alloc(String target, int bytes) { - return new PatioAlloc(new PatioLocalRef(target), bytes, Sources.NULL); + public static PatioInstruction alloc(String target, PatioDataLayout layout) { + return new PatioAlloc(new PatioLocalRef(target), layout, Sources.NULL); } public static PatioInstruction callPointer( -- cgit v1.2.3