mirror of
https://github.com/irmen/prog8.git
synced 2024-12-23 09:32:43 +00:00
add more const foldings
This commit is contained in:
parent
5d362047e2
commit
6af3209d4d
@ -101,6 +101,33 @@ class ConstantFoldingOptimizer(private val program: Program) : AstWalker() {
|
||||
val rightconst = expr.right.constValue(program)
|
||||
val modifications = mutableListOf<IAstModification>()
|
||||
|
||||
if(expr.operator=="==" && rightconst!=null) {
|
||||
val leftExpr = expr.left as? BinaryExpression
|
||||
if(leftExpr!=null) {
|
||||
val leftRightConst = leftExpr.right.constValue(program)
|
||||
if(leftRightConst!=null) {
|
||||
when (leftExpr.operator) {
|
||||
"+" -> {
|
||||
// X + С1 == C2 --> X == C2 - C1
|
||||
val newRightConst = NumericLiteralValue(rightconst.type, rightconst.number - leftRightConst.number, rightconst.position)
|
||||
return listOf(
|
||||
IAstModification.ReplaceNode(leftExpr, leftExpr.left, expr),
|
||||
IAstModification.ReplaceNode(expr.right, newRightConst, expr)
|
||||
)
|
||||
}
|
||||
"-" -> {
|
||||
// X - С1 == C2 --> X == C2 + C1
|
||||
val newRightConst = NumericLiteralValue(rightconst.type, rightconst.number + leftRightConst.number, rightconst.position)
|
||||
return listOf(
|
||||
IAstModification.ReplaceNode(leftExpr, leftExpr.left, expr),
|
||||
IAstModification.ReplaceNode(expr.right, newRightConst, expr)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(expr.operator == "**" && leftconst!=null) {
|
||||
// optimize various simple cases of ** :
|
||||
// optimize away 1 ** x into just 1 and 0 ** x into just 0
|
||||
|
@ -19,12 +19,6 @@ import kotlin.math.pow
|
||||
|
||||
Investigate what optimizations binaryen has, also see https://egorbo.com/peephole-optimizations.html
|
||||
|
||||
Add some const foldings (that may already be done as well):
|
||||
X + С1 == C2 => X == C2 - C1
|
||||
((X + C1) + C2) => (X + (C1 + C2))
|
||||
((X + C1) + (Y + C2)) => ((X + Y) + (C1 + C2))
|
||||
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
@ -3,7 +3,10 @@ TODO
|
||||
|
||||
For next compiler release (7.4)
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
...
|
||||
BUG: Fix C-64sound issue in petaxian (regression since 7.3, sound on c64 build works fine on older versions)
|
||||
BUG: ubyte z1
|
||||
ubyte z2
|
||||
z2=z1+z2+5 CRASHES COMPILER
|
||||
|
||||
|
||||
Blocked by an official Commander-x16 v39 release
|
||||
|
@ -13,22 +13,43 @@ main {
|
||||
|
||||
; TODO: bitwise operations with a negative constant number -> replace the number by its positive 2 complement
|
||||
|
||||
if aw<1
|
||||
yy++
|
||||
if aw<=0
|
||||
yy++
|
||||
; (X + C1) + (Y + C2) => (X + Y) + (C1 + C2)
|
||||
; (X + C1) - (Y + C2) => (X - Y) + (C1 - C2)
|
||||
; ---> together: (X + C1) <plusmin> (Y + C2) => (X <plusmin> Y) + (C1 <plusmin> C2)
|
||||
|
||||
if yy<1
|
||||
yy++
|
||||
if yy<=0
|
||||
yy++
|
||||
if bb<1
|
||||
yy++
|
||||
if bb<=0
|
||||
yy++
|
||||
|
||||
txt.print_ub(yy)
|
||||
txt.print_uw(aw)
|
||||
; (X - C1) + (Y - C2) => (X + Y) - (C1 + C2)
|
||||
; (X - C1) - (Y - C2) => (X - Y) - (C1 - C2)
|
||||
|
||||
xx=6
|
||||
yy=8
|
||||
|
||||
|
||||
yy = (xx+5)+(yy+10)
|
||||
; yy = (xx+yy)+(5+10) ; TODO crashes compiler
|
||||
txt.print_ub(yy) ; 29
|
||||
txt.nl()
|
||||
|
||||
xx=100
|
||||
yy=8
|
||||
;yy = (xx+5)-(yy+10)
|
||||
yy = (xx-yy)+(5-10) as ubyte
|
||||
txt.print_ub(yy) ; 87
|
||||
txt.nl()
|
||||
|
||||
xx=50
|
||||
yy=40
|
||||
yy = (xx-5)+(yy-10)
|
||||
txt.print_ub(yy) ; 75
|
||||
txt.nl()
|
||||
|
||||
xx=50
|
||||
yy=20
|
||||
yy = (xx-5)-(yy-10)
|
||||
txt.print_ub(yy) ; 35
|
||||
txt.nl()
|
||||
|
||||
repeat {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user