From 4faec7c2c180569a06a5023fc1bcb77812705672 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 23 Jul 2023 22:03:21 +0100 Subject: Support int-ne in Patio code gen --- .../org/shedlang/compiler/patio/PatioCodeGen.java | 72 +++++++++++++++------- .../patio/PatioCodeGenIntNotEqualTests.java | 28 +++++++++ 2 files changed, 78 insertions(+), 22 deletions(-) create mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenIntNotEqualTests.java (limited to 'src') 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)); + } +} -- cgit v1.2.3