diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-07-23 22:03:21 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-07-23 22:03:21 +0100 |
| commit | 4faec7c2c180569a06a5023fc1bcb77812705672 (patch) | |
| tree | 289c57965d0f5b0726d43ecb4bbf42a11deae08e | |
| parent | 2fe51eb35031a9dd0c0b740fd893b9704614464e (diff) | |
Support int-ne in Patio code gen
3 files changed, 97 insertions, 22 deletions
diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIntNotEqualTests]/[method:canGenerateCodeForIntNotEqual(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIntNotEqualTests]/[method:canGenerateCodeForIntNotEqual(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected new file mode 100644 index 00000000..45699e36 --- /dev/null +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenIntNotEqualTests]/[method:canGenerateCodeForIntNotEqual(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -0,0 +1,19 @@ +Compilation unit elements: + + +Locals: + * ("tmp__0" int) + * ("tmp__1" int) + +Args: + +Instructions: + +( + (int-ne (local "tmp__1") (local "b") (local "c")) + (bool-to-int (local "tmp__0") (local "tmp__1")) +) + +Temporary stack: + * tmp__0 + * a diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java index 505d351d..45554f4c 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java @@ -317,30 +317,28 @@ public class PatioCodeGen { ); case IntEqualsInstruction intEquals -> - generateBinaryOperation( - (target, left, right, operationContext) -> { - var targetBoolResult = operationContext.allocateLocal( - PatioCodeGenLocalSpace.TEMPORARY, - PatioTypes.INT - ); - var targetBool = targetBoolResult.value(); - operationContext = targetBoolResult.context(); - - operationContext = operationContext.addInstruction(new PatioIntEquals( - targetBool, + generateComparison( + (target, left, right, operationContext) -> + operationContext.addInstruction(new PatioIntEquals( + target, left, right, intEquals.source() - )); + )), + intEquals.source(), + context + ); - operationContext = operationContext.addInstruction(new PatioBoolToInt( + case IntNotEqualInstruction intNotEqual -> + generateComparison( + (target, left, right, operationContext) -> + operationContext.addInstruction(new PatioIntNotEqual( target, - targetBool, - intEquals.source() - )); - - return operationContext; - }, + left, + right, + intNotEqual.source() + )), + intNotEqual.source(), context ); @@ -462,9 +460,6 @@ public class PatioCodeGen { ), context ); - - default -> - throw new IllegalStateException("Unexpected value: " + stackIrInstruction); }; } @@ -477,6 +472,39 @@ public class PatioCodeGen { ); } + private static PatioCodeGenContext generateComparison( + BinaryOperationCodeGen binaryOperationCodeGen, + Source source, + PatioCodeGenContext context + ) { + return generateBinaryOperation( + (target, left, right, operationContext) -> { + var targetBoolResult = operationContext.allocateLocal( + PatioCodeGenLocalSpace.TEMPORARY, + PatioTypes.INT + ); + var targetBool = targetBoolResult.value(); + operationContext = targetBoolResult.context(); + + operationContext = binaryOperationCodeGen.generate( + targetBool, + left, + right, + operationContext + ); + + operationContext = operationContext.addInstruction(new PatioBoolToInt( + target, + targetBool, + source + )); + + return operationContext; + }, + context + ); + } + private static PatioCodeGenContext generateBinaryOperation( BinaryOperationCodeGen binaryOperationCodeGen, PatioCodeGenContext context diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenIntNotEqualTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenIntNotEqualTests.java new file mode 100644 index 00000000..253781d3 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenIntNotEqualTests.java @@ -0,0 +1,28 @@ +package org.shedlang.compiler.patio; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.patio.ast.operands.PatioLocalRef; +import org.shedlang.compiler.stackir.instructions.Instructions; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class PatioCodeGenIntNotEqualTests { + @Test + public void canGenerateCodeForIntNotEqual(Snapshotter snapshotter) throws IOException { + var stackIr = Instructions.intNotEqual(); + var context = PatioCodeGenContext.EMPTY + .temporaryPush(new PatioLocalRef("a")) + .temporaryPush(new PatioLocalRef("b")) + .temporaryPush(new PatioLocalRef("c")); + + var result = PatioCodeGen.generateInstruction(stackIr, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} |
