mirror of
https://github.com/irmen/prog8.git
synced 2025-01-12 04:30:03 +00:00
optimize comparison to true/1 into comparison to zero, optimize while/until conditions
This commit is contained in:
parent
88cbb6913d
commit
68da661edc
@ -249,6 +249,28 @@ class ExpressionSimplifier(private val program: Program) : AstWalker() {
|
|||||||
else -> null
|
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)
|
if(newExpr != null)
|
||||||
return listOf(IAstModification.ReplaceNode(expr, newExpr, parent))
|
return listOf(IAstModification.ReplaceNode(expr, newExpr, parent))
|
||||||
|
|
||||||
|
@ -1,10 +1,7 @@
|
|||||||
package prog8.compiler.astprocessing
|
package prog8.compiler.astprocessing
|
||||||
|
|
||||||
import prog8.ast.*
|
import prog8.ast.*
|
||||||
import prog8.ast.expressions.BinaryExpression
|
import prog8.ast.expressions.*
|
||||||
import prog8.ast.expressions.DirectMemoryRead
|
|
||||||
import prog8.ast.expressions.FunctionCallExpression
|
|
||||||
import prog8.ast.expressions.NumericLiteral
|
|
||||||
import prog8.ast.statements.*
|
import prog8.ast.statements.*
|
||||||
import prog8.ast.walk.AstWalker
|
import prog8.ast.walk.AstWalker
|
||||||
import prog8.ast.walk.IAstModification
|
import prog8.ast.walk.IAstModification
|
||||||
@ -64,11 +61,12 @@ if CONDITION==0
|
|||||||
*/
|
*/
|
||||||
val pos = untilLoop.position
|
val pos = untilLoop.position
|
||||||
val loopLabel = program.makeLabel("untilloop", pos)
|
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(
|
val replacement = AnonymousScope(mutableListOf(
|
||||||
loopLabel,
|
loopLabel,
|
||||||
untilLoop.body,
|
untilLoop.body,
|
||||||
IfElse(equalsZero,
|
IfElse(ifCondition,
|
||||||
AnonymousScope(mutableListOf(program.jumpLabel(loopLabel)), pos),
|
AnonymousScope(mutableListOf(program.jumpLabel(loopLabel)), pos),
|
||||||
AnonymousScope(mutableListOf(), pos),
|
AnonymousScope(mutableListOf(), pos),
|
||||||
pos)
|
pos)
|
||||||
@ -89,10 +87,11 @@ _after:
|
|||||||
val pos = whileLoop.position
|
val pos = whileLoop.position
|
||||||
val loopLabel = program.makeLabel("whileloop", pos)
|
val loopLabel = program.makeLabel("whileloop", pos)
|
||||||
val afterLabel = program.makeLabel("afterwhile", 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(
|
val replacement = AnonymousScope(mutableListOf(
|
||||||
loopLabel,
|
loopLabel,
|
||||||
IfElse(equalsZero,
|
IfElse(ifCondition,
|
||||||
AnonymousScope(mutableListOf(program.jumpLabel(afterLabel)), pos),
|
AnonymousScope(mutableListOf(program.jumpLabel(afterLabel)), pos),
|
||||||
AnonymousScope(mutableListOf(), pos),
|
AnonymousScope(mutableListOf(), pos),
|
||||||
pos),
|
pos),
|
||||||
|
@ -3,10 +3,6 @@ TODO
|
|||||||
|
|
||||||
For next release
|
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
|
- 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
|
- then make the cx16 virtual registers in syslib.p8 use that definition to be able to shift them around on non-cx16 targets
|
||||||
|
|
||||||
|
@ -3,36 +3,14 @@
|
|||||||
|
|
||||||
|
|
||||||
main {
|
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() {
|
sub start() {
|
||||||
void pushing_fire()
|
bool @shared zz
|
||||||
void pushing_left()
|
ubyte xx
|
||||||
void pushing_right()
|
|
||||||
|
|
||||||
ubyte rnr = $99
|
while xx<42 {
|
||||||
ubyte wordNr = ((rnr >= $33) as ubyte) +
|
xx++
|
||||||
((rnr >= $66) as ubyte) +
|
}
|
||||||
((rnr >= $99) as ubyte) +
|
|
||||||
((rnr >= $CC) as ubyte)
|
|
||||||
|
|
||||||
ubyte wordNr2 = (rnr >= $33) as ubyte + (rnr >= $66) as ubyte + (rnr >= $99) as ubyte + (rnr >= $CC) as ubyte
|
txt.print_ub(xx)
|
||||||
|
|
||||||
txt.print_uw(wordNr)
|
|
||||||
txt.nl()
|
|
||||||
txt.print_uw(wordNr2)
|
|
||||||
txt.nl()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user