From 3535c1acda77610700cb2c8ef66f6099c94151d2 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Wed, 13 Mar 2024 20:23:42 +0100 Subject: [PATCH] fix broken boolean != comparison optimization --- .../prog8/optimizer/ExpressionSimplifier.kt | 11 +- compiler/test/arithmetic/bitshift2.p8 | 128 +++++++++--------- docs/source/todo.rst | 3 + examples/test.p8 | 18 ++- 4 files changed, 90 insertions(+), 70 deletions(-) diff --git a/codeOptimizers/src/prog8/optimizer/ExpressionSimplifier.kt b/codeOptimizers/src/prog8/optimizer/ExpressionSimplifier.kt index 9ed4b3ec1..64806d445 100644 --- a/codeOptimizers/src/prog8/optimizer/ExpressionSimplifier.kt +++ b/codeOptimizers/src/prog8/optimizer/ExpressionSimplifier.kt @@ -277,10 +277,13 @@ class ExpressionSimplifier(private val program: Program, private val options: Co } if (expr.operator=="!=") { if(rightDt==DataType.BOOL && leftDt==DataType.BOOL) { - if(rightVal?.asBooleanValue==false) - return listOf(IAstModification.ReplaceNode(expr, expr.left, parent)) - else - return listOf(IAstModification.ReplaceNode(expr, PrefixExpression("not", expr.left, expr.position), parent)) + val rightConstBool = rightVal?.asBooleanValue + if(rightConstBool!=null) { + return if (rightConstBool) + listOf(IAstModification.ReplaceNode(expr, PrefixExpression("not", expr.left, expr.position), parent)) + else + listOf(IAstModification.ReplaceNode(expr, expr.left, parent)) + } } if (rightVal?.number == 1.0) { if(options.strictBool) { diff --git a/compiler/test/arithmetic/bitshift2.p8 b/compiler/test/arithmetic/bitshift2.p8 index 29db8970b..ca339c00e 100644 --- a/compiler/test/arithmetic/bitshift2.p8 +++ b/compiler/test/arithmetic/bitshift2.p8 @@ -8,6 +8,10 @@ main { signed() } + sub print_bool(bool b) { + txt.print_ub(b as ubyte) + } + ubyte[2] ubarray uword[2] uwarray byte[2] barray @@ -227,15 +231,15 @@ main { txt.print("rol_ub error ") txt.print_ub(original) txt.spc() - txt.print_bool(carry) + print_bool(carry) txt.spc() txt.print_ub(value) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.print(" exp: ") txt.print_ub(test) txt.spc() - txt.print_bool(newcarry) + print_bool(newcarry) txt.nl() } @@ -252,15 +256,15 @@ main { txt.print("rol_ub array error ") txt.print_ub(original) txt.spc() - txt.print_bool(carry) + print_bool(carry) txt.spc() txt.print_ub(ubarray[1]) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.print(" exp: ") txt.print_ub(test) txt.spc() - txt.print_bool(newcarry) + print_bool(newcarry) txt.nl() } @@ -277,15 +281,15 @@ main { txt.print("rol_ub mem error ") txt.print_ub(original) txt.spc() - txt.print_bool(carry) + print_bool(carry) txt.spc() txt.print_ub(@($8001)) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.print(" exp: ") txt.print_ub(test) txt.spc() - txt.print_bool(newcarry) + print_bool(newcarry) txt.nl() } } @@ -304,15 +308,15 @@ main { txt.print("ror_ub error ") txt.print_ub(original) txt.spc() - txt.print_bool(carry) + print_bool(carry) txt.spc() txt.print_ub(value) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.print(" exp: ") txt.print_ub(test) txt.spc() - txt.print_bool(newcarry) + print_bool(newcarry) txt.nl() } @@ -329,15 +333,15 @@ main { txt.print("ror_ub array error ") txt.print_ub(original) txt.spc() - txt.print_bool(carry) + print_bool(carry) txt.spc() txt.print_ub(ubarray[1]) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.print(" exp: ") txt.print_ub(test) txt.spc() - txt.print_bool(newcarry) + print_bool(newcarry) txt.nl() } @@ -354,15 +358,15 @@ main { txt.print("ror_ub mem error ") txt.print_ub(original) txt.spc() - txt.print_bool(carry) + print_bool(carry) txt.spc() txt.print_ub(@($8001)) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.print(" exp: ") txt.print_ub(test) txt.spc() - txt.print_bool(newcarry) + print_bool(newcarry) txt.nl() } } @@ -437,15 +441,15 @@ main { txt.print("rol_uw error ") txt.print_uw(original) txt.spc() - txt.print_bool(carry) + print_bool(carry) txt.spc() txt.print_uw(value) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.print(" exp: ") txt.print_uw(test) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.nl() } @@ -462,15 +466,15 @@ main { txt.print("rol_uw array error ") txt.print_uw(original) txt.spc() - txt.print_bool(carry) + print_bool(carry) txt.spc() txt.print_uw(uwarray[1]) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.print(" exp: ") txt.print_uw(test) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.nl() } } @@ -489,15 +493,15 @@ main { txt.print("ror_uw error ") txt.print_uw(original) txt.spc() - txt.print_bool(carry) + print_bool(carry) txt.spc() txt.print_uw(value) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.print(" exp: ") txt.print_uw(test) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.nl() } @@ -514,15 +518,15 @@ main { txt.print("ror_uw array error ") txt.print_uw(original) txt.spc() - txt.print_bool(carry) + print_bool(carry) txt.spc() txt.print_uw(uwarray[1]) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.print(" exp: ") txt.print_uw(test) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.nl() } } @@ -540,11 +544,11 @@ main { txt.spc() txt.print_ub(value) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.print(" exp: ") txt.print_ub(test) txt.spc() - txt.print_bool(newcarry) + print_bool(newcarry) txt.nl() } @@ -560,11 +564,11 @@ main { txt.spc() txt.print_ub(ubarray[1]) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.print(" exp: ") txt.print_ub(test) txt.spc() - txt.print_bool(newcarry) + print_bool(newcarry) txt.nl() } } @@ -582,11 +586,11 @@ main { txt.spc() txt.print_ub(value) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.print(" exp: ") txt.print_ub(test) txt.spc() - txt.print_bool(newcarry) + print_bool(newcarry) txt.nl() } @@ -602,11 +606,11 @@ main { txt.spc() txt.print_ub(ubarray[1]) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.print(" exp: ") txt.print_ub(test) txt.spc() - txt.print_bool(newcarry) + print_bool(newcarry) txt.nl() } } @@ -624,11 +628,11 @@ main { txt.spc() txt.print_uw(value) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.print(" exp: ") txt.print_uw(test) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.nl() } @@ -644,11 +648,11 @@ main { txt.spc() txt.print_uw(uwarray[1]) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.print(" exp: ") txt.print_uw(test) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.nl() } } @@ -666,11 +670,11 @@ main { txt.spc() txt.print_uw(value) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.print(" exp: ") txt.print_uw(test) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.nl() } @@ -686,11 +690,11 @@ main { txt.spc() txt.print_uw(uwarray[1]) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.print(" exp: ") txt.print_uw(test) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.nl() } } @@ -708,11 +712,11 @@ main { txt.spc() txt.print_b(value) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.print(" exp: ") txt.print_b(test) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.nl() } @@ -728,11 +732,11 @@ main { txt.spc() txt.print_b(barray[1]) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.print(" exp: ") txt.print_b(test) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.nl() } } @@ -750,11 +754,11 @@ main { txt.spc() txt.print_b(value) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.print(" exp: ") txt.print_b(test) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.nl() } @@ -770,11 +774,11 @@ main { txt.spc() txt.print_b(barray[1]) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.print(" exp: ") txt.print_b(test) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.nl() } } @@ -792,11 +796,11 @@ main { txt.spc() txt.print_w(value) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.print(" exp: ") txt.print_w(test) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.nl() } @@ -812,11 +816,11 @@ main { txt.spc() txt.print_w(warray[1]) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.print(" exp: ") txt.print_w(test) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.nl() } } @@ -834,11 +838,11 @@ main { txt.spc() txt.print_w(value) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.print(" exp: ") txt.print_w(test) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.nl() } @@ -854,11 +858,11 @@ main { txt.spc() txt.print_w(warray[1]) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.print(" exp: ") txt.print_w(test) txt.spc() - txt.print_bool(carrycheck) + print_bool(carrycheck) txt.nl() } } diff --git a/docs/source/todo.rst b/docs/source/todo.rst index ebb95291c..71e070c08 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -1,6 +1,9 @@ TODO ==== +make bitshift2.p8 runnable on VM +bitshift2.p8 has many errors (was ok on 10.2 before bool merge) + ... diff --git a/examples/test.p8 b/examples/test.p8 index e2d7220cb..8b7a323a8 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -3,13 +3,23 @@ %option no_sysinit main { + bool @shared var1, var2 + bool[2] barray = [false, true] + ubyte success + sub start() { - str name = "irmen" - ubyte @shared cc='m' + no_else() + } - cx16.r0=9999 + sub no_else() { + txt.print("bool no_else: ") + success=0 - if cx16.r0<10000 and 'q' in name + var1=true + var2=false + + if var1!=var2 txt.print("yes") + } }