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(target)
assignTrue.add(PtNumber.fromBoolean(true, assign.position)) assignTrue.add(PtNumber.fromBoolean(true, assign.position))
} else { } else {
require(assign.target.datatype in ByteDatatypes)
when(assign.target.kind) { when(assign.target.kind) {
TargetStorageKind.VARIABLE -> { TargetStorageKind.VARIABLE -> {
if(assign.target.datatype in WordDatatypes) { 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 // optimize boolean constant comparisons
if(expr.operator=="==") { if(expr.operator=="==") {
if(rightDt==DataType.BOOL && leftDt==DataType.BOOL) { if(rightDt==DataType.BOOL && leftDt==DataType.BOOL) {
if(rightVal?.asBooleanValue==true) val rightConstBool = rightVal?.asBooleanValue
return listOf(IAstModification.ReplaceNode(expr, expr.left, parent)) if(rightConstBool!=null) {
else return if(rightConstBool)
return listOf(IAstModification.ReplaceNode(expr, PrefixExpression("not", expr.left, expr.position), parent)) 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 (rightVal?.number == 1.0) {
if (options.strictBool) { 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*. 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. 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 Low-fi variable and subroutine definitions in all available library modules
--------------------------------------------------------------------------- ---------------------------------------------------------------------------

View File

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