From 071a80360fbb0dfe233acd016592fe44b0550066 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Fri, 7 Jul 2023 21:17:28 +0200 Subject: [PATCH] ir: fix some problem with comparison against zero --- .../prog8/codegen/intermediate/IRCodeGen.kt | 16 ++--- examples/test.p8 | 61 +++++++++++++++---- 2 files changed, 57 insertions(+), 20 deletions(-) diff --git a/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt b/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt index 298262bdb..ba3a606d5 100644 --- a/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt +++ b/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt @@ -1002,10 +1002,10 @@ class IRCodeGen( val opcode = when (condition.operator) { "==" -> Opcode.BEQ "!=" -> Opcode.BNE - "<" -> if (signed) Opcode.BLTS else throw AssemblyError("unsigned < 0 shouldn't occur in codegen") - ">" -> if (signed) Opcode.BGTS else throw AssemblyError("unsigned > 0 shouldn't occur in codegen") - "<=" -> if (signed) Opcode.BLES else throw AssemblyError("unsigned <= 0 shouldn't occur in codegen") - ">=" -> if (signed) Opcode.BGES else throw AssemblyError("unsigned >= 0 shouldn't occur in codegen") + "<" -> if (signed) Opcode.BLTS else Opcode.BLT + ">" -> if (signed) Opcode.BGTS else Opcode.BGT + "<=" -> if (signed) Opcode.BLES else Opcode.BLE + ">=" -> if (signed) Opcode.BGES else Opcode.BGE else -> throw AssemblyError("invalid comparison operator") } if (goto.address != null) @@ -1139,10 +1139,10 @@ class IRCodeGen( elseBranch = when (condition.operator) { "==" -> Opcode.BNE "!=" -> Opcode.BEQ - "<" -> if (signed) Opcode.BGES else throw AssemblyError("unsigned < 0 shouldn't occur in codegen") - ">" -> if (signed) Opcode.BLES else throw AssemblyError("unsigned > 0 shouldn't occur in codegen") - "<=" -> if (signed) Opcode.BGTS else throw AssemblyError("unsigned <= 0 shouldn't occur in codegen") - ">=" -> if (signed) Opcode.BLTS else throw AssemblyError("unsigned >= 0 shouldn't occur in codegen") + "<" -> if (signed) Opcode.BGES else Opcode.BGE + ">" -> if (signed) Opcode.BLES else Opcode.BLE + "<=" -> if (signed) Opcode.BGTS else Opcode.BGT + ">=" -> if (signed) Opcode.BLTS else Opcode.BLT else -> throw AssemblyError("weird operator") } } diff --git a/examples/test.p8 b/examples/test.p8 index 14a13360e..bc08ffdc5 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,18 +1,55 @@ %import textio +%zeropage basicsafe + +; TODO this should also compile without optimizations main { sub start() { - cx16.r0 = 2 - when cx16.r0 { - 1-> { - ;nothing - } - 2 -> { - txt.print("two") - } - 0-> { - ;nothing - } - } + cx16.r0 = 0 + if cx16.r0 < 0 + txt.print(" <0 fail\n") + else + txt.print(" <0 ok\n") + + if cx16.r0 <= 0 + txt.print("<=0 ok\n") + else + txt.print("<=0 fail\n") + + if cx16.r0 > 0 + txt.print(" >0 fail\n") + else + txt.print(" >0 ok\n") + + if cx16.r0 >= 0 + txt.print(">=0 ok\n") + else + txt.print(">=0 fail\n") + + bool bb + bb = cx16.r0<0 + if bb + txt.print(" <0 fail\n") + else + txt.print(" <0 ok\n") + + bb = cx16.r0<=0 + if bb + txt.print("<=0 ok\n") + else + txt.print("<=0 fail\n") + + bb = cx16.r0>0 + if bb + txt.print(" >0 fail\n") + else + txt.print(" >0 ok\n") + + bb = cx16.r0>=0 + if bb + txt.print(">=0 ok\n") + else + txt.print(">=0 fail\n") + } }