From 68da661edc47725c26df1a09eef40aaf364e7345 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Mon, 11 Jul 2022 15:46:14 +0200 Subject: [PATCH] optimize comparison to true/1 into comparison to zero, optimize while/until conditions --- .../prog8/optimizer/ExpressionSimplifier.kt | 22 ++++++++++++ .../compiler/astprocessing/CodeDesugarer.kt | 15 ++++---- docs/source/todo.rst | 4 --- examples/test.p8 | 34 ++++--------------- 4 files changed, 35 insertions(+), 40 deletions(-) diff --git a/codeOptimizers/src/prog8/optimizer/ExpressionSimplifier.kt b/codeOptimizers/src/prog8/optimizer/ExpressionSimplifier.kt index 9bbd21a63..120572859 100644 --- a/codeOptimizers/src/prog8/optimizer/ExpressionSimplifier.kt +++ b/codeOptimizers/src/prog8/optimizer/ExpressionSimplifier.kt @@ -249,6 +249,28 @@ class ExpressionSimplifier(private val program: Program) : AstWalker() { else -> null } + if(rightVal!=null && leftDt==DataType.BOOL) { + // see if we can replace comparison against true/1 with simpler comparison against zero + if (expr.operator == "==") { + if (rightVal.number == 1.0) { + val zero = NumericLiteral(DataType.UBYTE, 0.0, expr.right.position) + return listOf( + IAstModification.SetExpression({expr.operator="!="}, expr, parent), + IAstModification.ReplaceNode(expr.right, zero, expr) + ) + } + } + if (expr.operator == "!=") { + if (rightVal.number == 1.0) { + val zero = NumericLiteral(DataType.UBYTE, 0.0, expr.right.position) + return listOf( + IAstModification.SetExpression({expr.operator="=="}, expr, parent), + IAstModification.ReplaceNode(expr.right, zero, expr) + ) + } + } + } + if(newExpr != null) return listOf(IAstModification.ReplaceNode(expr, newExpr, parent)) diff --git a/compiler/src/prog8/compiler/astprocessing/CodeDesugarer.kt b/compiler/src/prog8/compiler/astprocessing/CodeDesugarer.kt index 51f0158a8..a5ba6374f 100644 --- a/compiler/src/prog8/compiler/astprocessing/CodeDesugarer.kt +++ b/compiler/src/prog8/compiler/astprocessing/CodeDesugarer.kt @@ -1,10 +1,7 @@ package prog8.compiler.astprocessing import prog8.ast.* -import prog8.ast.expressions.BinaryExpression -import prog8.ast.expressions.DirectMemoryRead -import prog8.ast.expressions.FunctionCallExpression -import prog8.ast.expressions.NumericLiteral +import prog8.ast.expressions.* import prog8.ast.statements.* import prog8.ast.walk.AstWalker import prog8.ast.walk.IAstModification @@ -64,11 +61,12 @@ if CONDITION==0 */ val pos = untilLoop.position val loopLabel = program.makeLabel("untilloop", pos) - val equalsZero = BinaryExpression(untilLoop.condition, "==", NumericLiteral(DataType.UBYTE, 0.0, pos), pos) + val ifCondition = invertCondition(untilLoop.condition) + ?: BinaryExpression(untilLoop.condition, "==", NumericLiteral(DataType.UBYTE, 0.0, pos), pos) val replacement = AnonymousScope(mutableListOf( loopLabel, untilLoop.body, - IfElse(equalsZero, + IfElse(ifCondition, AnonymousScope(mutableListOf(program.jumpLabel(loopLabel)), pos), AnonymousScope(mutableListOf(), pos), pos) @@ -89,10 +87,11 @@ _after: val pos = whileLoop.position val loopLabel = program.makeLabel("whileloop", pos) val afterLabel = program.makeLabel("afterwhile", pos) - val equalsZero = BinaryExpression(whileLoop.condition, "==", NumericLiteral(DataType.UBYTE, 0.0, pos), pos) + val ifCondition = invertCondition(whileLoop.condition) + ?: BinaryExpression(whileLoop.condition, "==", NumericLiteral(DataType.UBYTE, 0.0, pos), pos) val replacement = AnonymousScope(mutableListOf( loopLabel, - IfElse(equalsZero, + IfElse(ifCondition, AnonymousScope(mutableListOf(program.jumpLabel(afterLabel)), pos), AnonymousScope(mutableListOf(), pos), pos), diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 21916b66c..1a6588fb3 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,10 +3,6 @@ TODO For next release ^^^^^^^^^^^^^^^^ -- petaxian has become larger (18630) why so much larger. problematic lines: - gun.p8 95,110 if ( x <= GUN_MAX_LEFT and leftmost ) ... - enemy.p8 456 + bombs.p8 33 (while i ubyte { - return key == 'z' - } - - sub pushing_left() -> ubyte { - return key == 'k' or key == 157 - } - - sub pushing_right() -> ubyte { - return key == 'l' or key == 29 - } - sub start() { - void pushing_fire() - void pushing_left() - void pushing_right() + bool @shared zz + ubyte xx - ubyte rnr = $99 - ubyte wordNr = ((rnr >= $33) as ubyte) + - ((rnr >= $66) as ubyte) + - ((rnr >= $99) as ubyte) + - ((rnr >= $CC) as ubyte) + while xx<42 { + xx++ + } - ubyte wordNr2 = (rnr >= $33) as ubyte + (rnr >= $66) as ubyte + (rnr >= $99) as ubyte + (rnr >= $CC) as ubyte - - txt.print_uw(wordNr) - txt.nl() - txt.print_uw(wordNr2) - txt.nl() + txt.print_ub(xx) } }