Skip to content

Commit

Permalink
get rid of certain redundant !=0 comparisons in logical expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
irmen committed Feb 1, 2024
1 parent 9f8e617 commit c71aa08
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
21 changes: 21 additions & 0 deletions compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 0 additions & 4 deletions docs/source/todo.rst
Original file line number Diff line number Diff line change
@@ -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.

...
Expand Down

0 comments on commit c71aa08

Please sign in to comment.