From 9fce582cb0aa3cbd73c9a398f66d75bf39c360de Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 20 Jul 2023 14:20:36 +0100 Subject: Support multiple offsets to allow access of nested structures --- .../org/shedlang/compiler/patio/PatioCodeGenClosures.java | 2 +- .../org/shedlang/compiler/patio/ast/PatioSerializer.java | 12 ++++++++++-- .../compiler/patio/ast/instructions/PatioMemoryLoad.java | 4 +++- .../compiler/patio/ast/instructions/PatioMemoryStore.java | 4 +++- .../org/shedlang/compiler/patio/PatioSerializerTests.java | 8 ++++---- .../java/org/shedlang/compiler/patio/instructions/Patio.java | 4 ++-- 6 files changed, 23 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java index e8d991d2..cdeb1dac 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGenClosures.java @@ -23,7 +23,7 @@ class PatioCodeGenClosures { private PatioCodeGenClosures() {} - private static final int FUNCTION_POINTER_OFFSET = 0; + private static final List FUNCTION_POINTER_OFFSET = List.of(0); private static PatioDataLayout closureDataLayoutForCall() { return new PatioStruct(List.of( 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 6a48399e..428994a9 100644 --- a/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java +++ b/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java @@ -108,7 +108,11 @@ public class PatioSerializer { localRefToTree(memoryLoad.target()), treeNamed("pointer", localRefToTree(memoryLoad.pointer())), treeNamed("layout", dataLayoutToTree(memoryLoad.layout())), - treeNamed("offset", treeInt(memoryLoad.offset())) + treeNamed("offset", treeList( + memoryLoad.offset().stream() + .map(offset -> treeInt(offset)) + .toList() + )) ); case PatioMemoryStore memoryStore -> @@ -116,7 +120,11 @@ public class PatioSerializer { treeIdentifier("memory-store"), treeNamed("pointer", localRefToTree(memoryStore.pointer())), treeNamed("layout", dataLayoutToTree(memoryStore.layout())), - treeNamed("offset", treeInt(memoryStore.offset())), + treeNamed("offset", treeList( + memoryStore.offset().stream() + .map(offset -> treeInt(offset)) + .toList() + )), treeNamed("value", localRefToTree(memoryStore.value())) ); diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryLoad.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryLoad.java index 430296ee..7a5f49ce 100644 --- a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryLoad.java +++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryLoad.java @@ -4,6 +4,8 @@ import org.shedlang.compiler.patio.ast.PatioLocalRef; import org.shedlang.compiler.patio.ast.types.PatioDataLayout; import org.shedlang.compiler.sources.Source; +import java.util.List; + /** * Load the value at the given offset from the given pointer and store it in the * target local. @@ -12,7 +14,7 @@ public record PatioMemoryLoad( PatioLocalRef target, PatioLocalRef pointer, PatioDataLayout layout, - int offset, + List offset, Source source ) implements PatioInstruction { } diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryStore.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryStore.java index b32899db..0a9b35a6 100644 --- a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryStore.java +++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioMemoryStore.java @@ -4,13 +4,15 @@ import org.shedlang.compiler.patio.ast.PatioLocalRef; import org.shedlang.compiler.patio.ast.types.PatioDataLayout; import org.shedlang.compiler.sources.Source; +import java.util.List; + /** * Store the given value at the given offset from the given pointer. */ public record PatioMemoryStore( PatioLocalRef pointer, PatioDataLayout layout, - int offset, + List offset, PatioLocalRef value, 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 123748b8..0f504c07 100644 --- a/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java +++ b/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java @@ -91,7 +91,7 @@ public class PatioSerializerTests { "target", "pointer", PatioTypes.structFlat(List.of(PatioTypes.INT, PatioTypes.INT, PatioTypes.DATA_POINTER)), - 1 + List.of(1) ); var string = PatioTesting.serializeInstruction(instruction); @@ -102,7 +102,7 @@ public class PatioSerializerTests { "target" pointer="pointer" layout=(struct (int int data-ptr)) - offset=1 + offset=(1) )""" )); } @@ -112,7 +112,7 @@ public class PatioSerializerTests { var instruction = Patio.memoryStore( "pointer", PatioTypes.structFlat(List.of(PatioTypes.INT, PatioTypes.INT, PatioTypes.DATA_POINTER)), - 1, + List.of(1), "value" ); @@ -123,7 +123,7 @@ public class PatioSerializerTests { memory-store pointer="pointer" layout=(struct (int int data-ptr)) - offset=1 + offset=(1) value="value" )""" )); 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 93715a54..7d927ae9 100644 --- a/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java +++ b/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java @@ -49,7 +49,7 @@ public class Patio { String target, String pointer, PatioDataLayout layout, - int offset + List offset ) { return new PatioMemoryLoad( new PatioLocalRef(target), @@ -63,7 +63,7 @@ public class Patio { public static PatioInstruction memoryStore( String pointer, PatioDataLayout layout, - int offset, + List offset, String value ) { return new PatioMemoryStore( -- cgit v1.2.3