optimize comparison to true/1 into comparison to zero, optimize while/until conditions

This commit is contained in:
Irmen de Jong 2022-07-11 15:46:14 +02:00
parent 88cbb6913d
commit 68da661edc
4 changed files with 35 additions and 40 deletions

View File

@ -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))

View File

@ -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),

View File

@ -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<value)...
- have a proper option to move the evalstack rather than just assembly symbol redefine
- then make the cx16 virtual registers in syslib.p8 use that definition to be able to shift them around on non-cx16 targets

View File

@ -3,36 +3,14 @@
main {
ubyte key
sub pushing_fire() -> 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)
}
}