From c71aa0895f2efa8ca3be56a22487aa695020464c Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Thu, 1 Feb 2024 21:50:01 +0100 Subject: [PATCH] get rid of certain redundant !=0 comparisons in logical expressions --- .../compiler/astprocessing/VariousCleanups.kt | 21 +++++++++++++++++++ docs/source/todo.rst | 4 ---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt b/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt index 9137372ce..02139fa12 100644 --- a/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt +++ b/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt @@ -151,6 +151,27 @@ internal class VariousCleanups(val program: Program, val errors: IErrorReporter, } } } + + if(expr.operator in LogicalOperators) { + // remove redundant !=0 comparisons from logical expressions such as: a!=0 xor b --> a xor b (only for byte operands!) + val leftExpr = expr.left as? BinaryExpression + if(leftExpr != null && + leftExpr.operator == "!=" && + !leftExpr.left.isSimple && + leftExpr.left.inferType(program).isBytes && + leftExpr.right.constValue(program)?.number == 0.0) { + return listOf(IAstModification.ReplaceNode(leftExpr, leftExpr.left, expr)) + } + val rightExpr = expr.right as? BinaryExpression + if(rightExpr != null && + rightExpr.operator == "!=" && + !rightExpr.left.isSimple && + rightExpr.left.inferType(program).isBytes && + rightExpr.right.constValue(program)?.number == 0.0) { + return listOf(IAstModification.ReplaceNode(rightExpr, rightExpr.left, expr)) + } + } + return noModifications } diff --git a/docs/source/todo.rst b/docs/source/todo.rst index e743852a8..61b777273 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -1,10 +1,6 @@ TODO ==== -maze: if cell & UP!=0 and @(celladdr(cx,cy-1)) & (WALKED|BACKTRACKED) ==0 - ^^ adding this !=0 caused a weird beq + / lda #1 / + to appear in front of the shortcircuit beq... - - (after merge in boolean): move all "OperatorXinplace" from expressionGen to AssignmentGen, see if we can get rid of the Result return type. ...