From 7a83b6e88268277871788a3d8d86a3b8e044b142 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 22 Jan 2024 19:33:36 +0000 Subject: Replace Patio instructions for size/offset with operands --- .../shedlang/compiler/patio/PatioExampleTests.java | 50 +++++++------- .../compiler/patio/PatioSerializerTests.java | 77 +++++++++------------- .../compiler/patio/instructions/Patio.java | 43 ++++++------ 3 files changed, 77 insertions(+), 93 deletions(-) (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 6a5545ab..d25d5e94 100644 --- a/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java +++ b/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java @@ -767,22 +767,24 @@ public abstract class PatioExampleTests { .withName(FUNCTION_NAME) .withReturnType(PatioTypes.INT) .addLocal(Patio.val("x", PatioTypes.INT)) - .addBodyInstruction(Patio.byteOffsetOf( + .addBodyInstruction(Patio.localStore( Patio.localRef("x"), - PatioDataLayouts.struct(List.of( + Patio.intConst(Patio.intValueByteOffsetOf( PatioDataLayouts.struct(List.of( - PatioDataLayouts.BYTE, + PatioDataLayouts.struct(List.of( + PatioDataLayouts.BYTE, + PatioDataLayouts.BYTE + )), + PatioDataLayouts.struct(List.of( + PatioDataLayouts.BYTE, + PatioDataLayouts.BYTE, + PatioDataLayouts.BYTE, + PatioDataLayouts.BYTE + )), PatioDataLayouts.BYTE )), - PatioDataLayouts.struct(List.of( - PatioDataLayouts.BYTE, - PatioDataLayouts.BYTE, - PatioDataLayouts.BYTE, - PatioDataLayouts.BYTE - )), - PatioDataLayouts.BYTE - )), - List.of(1, 2) + List.of(1, 2) + )) )) .addBodyInstruction(Patio.return_( Patio.localRef("x") @@ -803,20 +805,22 @@ public abstract class PatioExampleTests { .withName(FUNCTION_NAME) .withReturnType(PatioTypes.INT) .addLocal(Patio.val("x", PatioTypes.INT)) - .addBodyInstruction(Patio.byteSizeOf( + .addBodyInstruction(Patio.localStore( Patio.localRef("x"), - PatioDataLayouts.struct(List.of( - PatioDataLayouts.struct(List.of( - PatioDataLayouts.BYTE, - PatioDataLayouts.BYTE - )), + Patio.intConst(Patio.intValueByteSizeOf( PatioDataLayouts.struct(List.of( - PatioDataLayouts.BYTE, - PatioDataLayouts.BYTE, - PatioDataLayouts.BYTE, + PatioDataLayouts.struct(List.of( + PatioDataLayouts.BYTE, + PatioDataLayouts.BYTE + )), + PatioDataLayouts.struct(List.of( + PatioDataLayouts.BYTE, + PatioDataLayouts.BYTE, + PatioDataLayouts.BYTE, + PatioDataLayouts.BYTE + )), PatioDataLayouts.BYTE - )), - PatioDataLayouts.BYTE + )) )) )) .addBodyInstruction(Patio.return_( diff --git a/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java b/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java index 0e4a1c14..637d0e9b 100644 --- a/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java +++ b/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java @@ -180,50 +180,6 @@ public class PatioSerializerTests { assertThat(string, equalTo("(bool-to-int (local \"destination\") (local \"value\"))")); } - @Test - public void canSerializeByteOffsetOf() { - var instruction = Patio.byteOffsetOf( - Patio.localRef("destination"), - PatioDataLayouts.struct(List.of( - PatioDataLayouts.INT, - PatioDataLayouts.BYTE - )), - List.of(1, 2) - ); - - var string = PatioTesting.serializeInstruction(instruction); - - assertThat(string, equalTo(""" - ( - byte-offset-of - (local "destination") - (struct (int byte)) - (1 2) - )""" - )); - } - - @Test - public void canSerializeByteSizeOf() { - var instruction = Patio.byteSizeOf( - Patio.localRef("destination"), - PatioDataLayouts.struct(List.of( - PatioDataLayouts.INT, - PatioDataLayouts.BYTE - )) - ); - - var string = PatioTesting.serializeInstruction(instruction); - - assertThat(string, equalTo(""" - ( - byte-size-of - (local "destination") - (struct (int byte)) - )""" - )); - } - @Test public void canSerializeCallWithoutTarget() { var instruction = Patio.call( @@ -803,11 +759,40 @@ public class PatioSerializerTests { // == Int values == + @Test + public void canSerializeIntValueByteOffsetOf() { + var intValue = Patio.intValueByteOffsetOf( + PatioDataLayouts.struct(List.of( + PatioDataLayouts.INT, + PatioDataLayouts.BYTE + )), + List.of(1, 2) + ); + + var string = PatioTesting.serializeIntValue(intValue); + + assertThat(string, equalTo("(byte-offset-of (struct (int byte)) (1 2))")); + } + + @Test + public void canSerializeIntValueByteSizeOf() { + var intValue = Patio.intValueByteSizeOf( + PatioDataLayouts.struct(List.of( + PatioDataLayouts.INT, + PatioDataLayouts.BYTE + )) + ); + + var string = PatioTesting.serializeIntValue(intValue); + + assertThat(string, equalTo("(byte-size-of (struct (int byte)))")); + } + @Test public void canSerializeIntValueLiteral() { - var operand = Patio.intValueLiteral(42); + var intValue = Patio.intValueLiteral(42); - var string = PatioTesting.serializeIntValue(operand); + var string = PatioTesting.serializeIntValue(intValue); assertThat(string, equalTo("42")); } 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 8129dcff..532f2d2a 100644 --- a/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java +++ b/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java @@ -6,6 +6,8 @@ import org.shedlang.compiler.patio.types.PatioDataLayout; import org.shedlang.compiler.patio.types.PatioDataValue; import org.shedlang.compiler.patio.types.PatioType; import org.shedlang.compiler.patio.values.PatioIntValue; +import org.shedlang.compiler.patio.values.PatioIntValueByteOffsetOf; +import org.shedlang.compiler.patio.values.PatioIntValueByteSizeOf; import org.shedlang.compiler.patio.values.PatioIntValueLiteral; import org.shedlang.compiler.sources.Sources; @@ -34,30 +36,6 @@ public class Patio { return new PatioBoolToInt(destination, value, Sources.NULL); } - public static PatioInstruction byteOffsetOf( - PatioLocalRef destination, - PatioDataLayout layout, - List offset - ) { - return new PatioByteOffsetOf( - destination, - layout, - offset, - Sources.NULL - ); - } - - public static PatioInstruction byteSizeOf( - PatioLocalRef destination, - PatioDataLayout layout - ) { - return new PatioByteSizeOf( - destination, - layout, - Sources.NULL - ); - } - public static PatioInstruction call( Optional destination, String name, @@ -156,6 +134,10 @@ public class Patio { return PatioIntConst.of(value); } + public static PatioOperand intConst(PatioIntValue value) { + return new PatioIntConst(value); + } + public static PatioInstruction intAdd( PatioLocalRef destination, PatioOperand left, @@ -205,6 +187,19 @@ public class Patio { ); } + public static PatioIntValue intValueByteOffsetOf( + PatioDataLayout layout, + List offset + ) { + return new PatioIntValueByteOffsetOf(layout, offset); + } + + public static PatioIntValue intValueByteSizeOf( + PatioDataLayout layout + ) { + return new PatioIntValueByteSizeOf(layout); + } + public static PatioIntValue intValueLiteral(int value) { return new PatioIntValueLiteral(value); } -- cgit v1.2.3