fix boolean expression optimization bug

This commit is contained in:
Irmen de Jong 2024-04-12 21:56:25 +02:00
parent 4d37581694
commit a7247f5b8b
4 changed files with 23 additions and 20 deletions

View File

@ -738,7 +738,6 @@ internal class AssignmentAsmGen(private val program: PtProgram,
assignTrue.add(target)
assignTrue.add(PtNumber.fromBoolean(true, assign.position))
} else {
require(assign.target.datatype in ByteDatatypes)
when(assign.target.kind) {
TargetStorageKind.VARIABLE -> {
if(assign.target.datatype in WordDatatypes) {

View File

@ -240,10 +240,13 @@ class ExpressionSimplifier(private val program: Program, private val options: Co
// optimize boolean constant comparisons
if(expr.operator=="==") {
if(rightDt==DataType.BOOL && leftDt==DataType.BOOL) {
if(rightVal?.asBooleanValue==true)
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, expr.left, parent))
else
listOf(IAstModification.ReplaceNode(expr, PrefixExpression("not", expr.left, expr.position), parent))
}
}
if (rightVal?.number == 1.0) {
if (options.strictBool) {

View File

@ -24,6 +24,10 @@ of these library modules automatically as required.
The resulting compiled binary program *only works on the target machine it was compiled for*.
You must recompile the program for every target you want to run it on.
.. note::
Several algorithms and math routines in Prog8's assembly library files are adapted from
code publicly available on https://www.codebase64.org/
Low-fi variable and subroutine definitions in all available library modules
---------------------------------------------------------------------------

View File

@ -1,20 +1,17 @@
%zeropage basicsafe
%import textio
%zeropage basicsafe
main {
sub start() {
uword @shared a,b
b = a ; works
cx16.r1L = lsb(a) ; works
funcw(a) ; works
funcb(lsb(a)) ; fails :-(
}
sub show_bug(byte a, byte b) {
if (a >= 0) == (b > 0) {
txt.print("bug!")
} else {
txt.print("no bug.")
}
txt.nl()
}
sub funcw(uword arg) {
arg++
}
sub funcb(ubyte arg) {
arg++
}
sub start() {
show_bug(-1, 4)
}
}