From 1397d18e897e98ede05e8bbc7b394c0469fb887a Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 22 Dec 2024 11:05:15 +0000 Subject: Implement Patio codegen for method store and load --- .../shedlang/compiler/shidtopatio/ShidToPatio.java | 13 ++++++ .../compiler/shidtopatio/ShidToPatioMethods.java | 53 ++++++++++++++++++++++ .../compiler/backends/llvm/Amd64ExampleTests.java | 1 - .../compiler/backends/llvm/I686ExampleTests.java | 1 - .../compiler/backends/wasm/Wasm32ExampleTests.java | 1 - .../compiler/backends/wasm/Wasm64ExampleTests.java | 1 - .../shidtopatio/ShidToPatioMethodLoadTests.java | 32 +++++++++++++ .../shidtopatio/ShidToPatioMethodStoreTests.java | 33 ++++++++++++++ 8 files changed, 131 insertions(+), 4 deletions(-) create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioMethodLoadTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioMethodStoreTests.java (limited to 'src') diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java index b612dbf3..beb8118a 100644 --- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java +++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java @@ -522,12 +522,24 @@ public class ShidToPatio { } case ShidMethodLoad methodLoad -> { + var destinationResult = context.allocateVal( + ShidToPatioLocalSpace.TEMPORARY, + methodLoad.methodName(), + ShidToPatioTypes.VALUE_TYPE + ); + var destination = destinationResult.value(); + context = destinationResult.context(); + context = ShidToPatioMethods.methodLoad( + destination, methodLoad.type(), methodLoad.methodName(), + methodLoad.source(), context ); + context = context.temporaryPush(destination); + yield context; } @@ -540,6 +552,7 @@ public class ShidToPatio { methodStore.type(), methodStore.methodName(), methodValue, + methodStore.source(), context ); diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioMethods.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioMethods.java index ae1bb925..c621c6cb 100644 --- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioMethods.java +++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioMethods.java @@ -1,17 +1,41 @@ package org.shedlang.compiler.shidtopatio; +import org.shedlang.compiler.patio.PatioDataElementMutable; +import org.shedlang.compiler.patio.PatioVisibility; +import org.shedlang.compiler.patio.instructions.PatioMemoryLoad; +import org.shedlang.compiler.patio.instructions.PatioMemoryStore; +import org.shedlang.compiler.patio.operands.PatioDataPtrConst; +import org.shedlang.compiler.patio.operands.PatioLocalRef; import org.shedlang.compiler.patio.operands.PatioOperand; +import org.shedlang.compiler.patio.types.PatioDataLayouts; +import org.shedlang.compiler.patio.types.PatioDataValues; +import org.shedlang.compiler.sources.Source; import org.shedlang.compiler.types.SimpleStructType; +import java.text.MessageFormat; +import java.util.List; + +import static org.shedlang.compiler.shidtopatio.ShidToPatioNamespaces.namespaceNameToPatioIdentifier; + public class ShidToPatioMethods { private ShidToPatioMethods() { } public static ShidToPatioContext methodLoad( + PatioLocalRef destination, SimpleStructType type, String methodName, + Source source, ShidToPatioContext context ) { + context = context.addInstruction(new PatioMemoryLoad( + destination, + globalOperandMethod(type, methodName), + PatioDataLayouts.fromType(ShidToPatioTypes.VALUE_TYPE), + List.of(), + source + )); + return context; } @@ -19,8 +43,37 @@ public class ShidToPatioMethods { SimpleStructType type, String methodName, PatioOperand methodValue, + Source source, ShidToPatioContext context ) { + context = context.addCompilationUnitElement(new PatioDataElementMutable( + globalNameMethod(type, methodName), + PatioVisibility.PRIVATE, + PatioDataValues.int_(0), + source + )); + + context = context.addInstruction(new PatioMemoryStore( + globalOperandMethod(type, methodName), + PatioDataLayouts.fromType(ShidToPatioTypes.VALUE_TYPE), + List.of(), + methodValue, + source + )); + return context; } + + private static PatioOperand globalOperandMethod(SimpleStructType type, String methodName) { + return PatioDataPtrConst.dataElement(globalNameMethod(type, methodName)); + } + + private static String globalNameMethod(SimpleStructType type, String methodName) { + return MessageFormat.format( + "shed__method__{0}__{1}__{2}", + namespaceNameToPatioIdentifier(type.namespaceName()), + type.name(), + methodName + ); + } } diff --git a/src/test/java/org/shedlang/compiler/backends/llvm/Amd64ExampleTests.java b/src/test/java/org/shedlang/compiler/backends/llvm/Amd64ExampleTests.java index bd754c3f..2fbe9f4b 100644 --- a/src/test/java/org/shedlang/compiler/backends/llvm/Amd64ExampleTests.java +++ b/src/test/java/org/shedlang/compiler/backends/llvm/Amd64ExampleTests.java @@ -23,7 +23,6 @@ public class Amd64ExampleTests extends ExampleTests { } private static final Set DISABLED = Set.of( - "35-struct-methods" ); @Override diff --git a/src/test/java/org/shedlang/compiler/backends/llvm/I686ExampleTests.java b/src/test/java/org/shedlang/compiler/backends/llvm/I686ExampleTests.java index 9de1ff27..07240512 100644 --- a/src/test/java/org/shedlang/compiler/backends/llvm/I686ExampleTests.java +++ b/src/test/java/org/shedlang/compiler/backends/llvm/I686ExampleTests.java @@ -23,7 +23,6 @@ public class I686ExampleTests extends ExampleTests { } private static final Set DISABLED = Set.of( - "35-struct-methods" ); @Override diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/Wasm32ExampleTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/Wasm32ExampleTests.java index 1e56b989..7b0ab151 100644 --- a/src/test/java/org/shedlang/compiler/backends/wasm/Wasm32ExampleTests.java +++ b/src/test/java/org/shedlang/compiler/backends/wasm/Wasm32ExampleTests.java @@ -23,7 +23,6 @@ public class Wasm32ExampleTests extends ExampleTests { } private static final Set DISABLED = Set.of( - "35-struct-methods" ); @Override diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/Wasm64ExampleTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/Wasm64ExampleTests.java index 972da7b7..71d5f17b 100644 --- a/src/test/java/org/shedlang/compiler/backends/wasm/Wasm64ExampleTests.java +++ b/src/test/java/org/shedlang/compiler/backends/wasm/Wasm64ExampleTests.java @@ -23,7 +23,6 @@ public class Wasm64ExampleTests extends ExampleTests { } private static final Set DISABLED = Set.of( - "35-struct-methods" ); @Override diff --git a/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioMethodLoadTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioMethodLoadTests.java new file mode 100644 index 00000000..6fc4abc2 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioMethodLoadTests.java @@ -0,0 +1,32 @@ +package org.shedlang.compiler.shidtopatio; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.patio.operands.PatioLocalRef; +import org.shedlang.compiler.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; +import org.shedlang.compiler.typechecker.TypeCheckerContext; +import org.shedlang.compiler.types.NamespaceName; +import org.shedlang.compiler.types.Types; + +import java.io.IOException; + +import static org.shedlang.compiler.shidtopatio.ShidToPatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioMethodLoadTests { + @Test + public void canGenerateCodeForMethodLoad(Snapshotter snapshotter) throws IOException { + var namespaceName = NamespaceName.of("Example", "Project"); + var structType = Types.struct(namespaceName, "User"); + var shidInstruction = Shid.methodLoad(structType, "name"); + var typeInfo = TypeCheckerContext.EMPTY.toTypeInfos(); + var context = ShidToPatioContext.forTypeInfos(typeInfo) + .temporaryPush(new PatioLocalRef("a")); + + var result = ShidToPatio.generateInstruction(shidInstruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioMethodStoreTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioMethodStoreTests.java new file mode 100644 index 00000000..447a925a --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioMethodStoreTests.java @@ -0,0 +1,33 @@ +package org.shedlang.compiler.shidtopatio; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.patio.operands.PatioLocalRef; +import org.shedlang.compiler.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; +import org.shedlang.compiler.typechecker.TypeCheckerContext; +import org.shedlang.compiler.types.NamespaceName; +import org.shedlang.compiler.types.Types; + +import java.io.IOException; + +import static org.shedlang.compiler.shidtopatio.ShidToPatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioMethodStoreTests { + @Test + public void canGenerateCodeForMethodStore(Snapshotter snapshotter) throws IOException { + var namespaceName = NamespaceName.of("Example", "Project"); + var structType = Types.struct(namespaceName, "User"); + var shidInstruction = Shid.methodStore(structType, "name"); + var typeInfo = TypeCheckerContext.EMPTY.toTypeInfos(); + var context = ShidToPatioContext.forTypeInfos(typeInfo) + .temporaryPush(new PatioLocalRef("a")) + .temporaryPush(new PatioLocalRef("b")); + + var result = ShidToPatio.generateInstruction(shidInstruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} -- cgit v1.2.3