mirror of
https://github.com/irmen/prog8.git
synced 2024-12-23 09:32:43 +00:00
add some more const folding patterns
This commit is contained in:
parent
eacf8b896a
commit
31491c62c5
@ -79,7 +79,7 @@ class ConstantFoldingOptimizer(private val program: Program) : AstWalker() {
|
||||
return noModifications
|
||||
}
|
||||
|
||||
/**
|
||||
/*
|
||||
* Try to constfold a binary expression.
|
||||
* Compile-time constant sub expressions will be evaluated on the spot.
|
||||
* For instance, "9 * (4 + 2)" will be optimized into the integer literal 54.
|
||||
@ -186,13 +186,43 @@ class ConstantFoldingOptimizer(private val program: Program) : AstWalker() {
|
||||
}
|
||||
}
|
||||
|
||||
val evaluator = ConstExprEvaluator()
|
||||
|
||||
// const fold when both operands are a const
|
||||
if(leftconst != null && rightconst != null) {
|
||||
val evaluator = ConstExprEvaluator()
|
||||
val result = evaluator.evaluate(leftconst, expr.operator, rightconst)
|
||||
modifications += IAstModification.ReplaceNode(expr, result, parent)
|
||||
}
|
||||
|
||||
|
||||
val leftBinExpr = expr.left as? BinaryExpression
|
||||
val rightBinExpr = expr.right as? BinaryExpression
|
||||
if(expr.operator=="+" || expr.operator=="-") {
|
||||
if(leftBinExpr!=null && rightBinExpr!=null) {
|
||||
val c1 = leftBinExpr.right.constValue(program)
|
||||
val c2 = rightBinExpr.right.constValue(program)
|
||||
if(leftBinExpr.operator=="+" && rightBinExpr.operator=="+") {
|
||||
if (c1 != null && c2 != null) {
|
||||
// (X + C1) <plusmin> (Y + C2) => (X <plusmin> Y) + (C1 <plusmin> C2)
|
||||
val c3 = evaluator.evaluate(c1, expr.operator, c2)
|
||||
val xwithy = BinaryExpression(leftBinExpr.left, expr.operator, rightBinExpr.left, expr.position)
|
||||
val newExpr = BinaryExpression(xwithy, "+", c3, expr.position)
|
||||
modifications += IAstModification.ReplaceNode(expr, newExpr, parent)
|
||||
}
|
||||
}
|
||||
else if(leftBinExpr.operator=="*" && rightBinExpr.operator=="*"){
|
||||
if (c1 != null && c2 != null && c1==c2) {
|
||||
//(X * C) <plusmin> (Y * C) => (X <plusmin> Y) * C
|
||||
val xwithy = BinaryExpression(leftBinExpr.left, expr.operator, rightBinExpr.left, expr.position)
|
||||
val newExpr = BinaryExpression(xwithy, "*", c1, expr.position)
|
||||
modifications += IAstModification.ReplaceNode(expr, newExpr, parent)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return modifications
|
||||
}
|
||||
|
||||
|
@ -6,19 +6,11 @@ main {
|
||||
|
||||
ubyte xx = 1
|
||||
ubyte yy = 2
|
||||
byte b1
|
||||
byte b2
|
||||
|
||||
;; TODO add these constant folders:
|
||||
;
|
||||
;; (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)
|
||||
|
||||
;; (X * C) + (Y * C) => (X + Y) * C
|
||||
;; (X * C) - (Y * C) => (X - Y) * C
|
||||
;; ---> together: (X * C) <plusmin> (Y * C) => (X <plusmin> Y) * C
|
||||
|
||||
;
|
||||
;; (X - C1) + (Y - C2) => (X + Y) - (C1 + C2)
|
||||
;; (X - C1) <plusmin> (Y - C2) => (X <plusmin> Y) - (C1 <plusmin> C2)
|
||||
;; (X - C1) - (Y - C2) => (X - Y) - (C1 - C2)
|
||||
;
|
||||
|
||||
@ -43,22 +35,22 @@ main {
|
||||
txt.print_ub(yy) ; 40
|
||||
txt.nl()
|
||||
|
||||
xx=100
|
||||
yy=8
|
||||
yy = (xx+5)-(yy+10)
|
||||
txt.print_ub(yy) ; 87
|
||||
b1=100
|
||||
b2=8
|
||||
b2 = (b1+5)-(b2+10)
|
||||
txt.print_b(b2) ; 87
|
||||
txt.nl()
|
||||
|
||||
xx=50
|
||||
yy=40
|
||||
yy = (xx-5)+(yy-10)
|
||||
txt.print_ub(yy) ; 75
|
||||
b1=50
|
||||
b2=40
|
||||
b2 = (b1-5)+(b2-10)
|
||||
txt.print_b(b2) ; 75
|
||||
txt.nl()
|
||||
|
||||
xx=50
|
||||
yy=20
|
||||
yy = (xx-5)-(yy-10)
|
||||
txt.print_ub(yy) ; 35
|
||||
b1=50
|
||||
b2=20
|
||||
b2 = (b1-5)-(b2-10)
|
||||
txt.print_b(b2) ; 35
|
||||
txt.nl()
|
||||
|
||||
repeat {
|
||||
|
Loading…
Reference in New Issue
Block a user