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 /src/main/java | |
| parent | 2fe51eb35031a9dd0c0b740fd893b9704614464e (diff) | |
Support int-ne in Patio code gen
Diffstat (limited to 'src/main/java')
| -rw-r--r-- | src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java | 72 |
1 files changed, 50 insertions, 22 deletions
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 |
