add some more const folding patterns

This commit is contained in:
Irmen de Jong 2021-11-20 22:43:08 +01:00
parent 31491c62c5
commit 107935ed31
4 changed files with 11 additions and 54 deletions

View File

@ -210,6 +210,15 @@ class ConstantFoldingOptimizer(private val program: Program) : AstWalker() {
modifications += IAstModification.ReplaceNode(expr, newExpr, parent)
}
}
else 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

View File

@ -76,7 +76,7 @@ fun compileProgram(filepath: Path,
postprocessAst(program, errors, compilationOptions)
// println("*********** AST BEFORE ASSEMBLYGEN *************")
// printAst(program)
// printProgram(program)
if (writeAssembly) {
val result = writeAssembly(program, errors, outputDir, quietAssembler, compilationOptions)
@ -328,7 +328,7 @@ private fun writeAssembly(program: Program,
errors.report()
// println("*********** AST RIGHT BEFORE ASM GENERATION *************")
// printAst(program)
// printProgram(program)
compilerOptions.compTarget.machine.initializeZeropage(compilerOptions)
val assembly = asmGeneratorFor(compilerOptions.compTarget,

View File

@ -3,7 +3,6 @@ TODO
For next compiler release (7.4)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
optimize: add some more constant folders mentioned in test.p8
optimize: there is an optimization in AsmOptimizer that can only be done correctly
if it knows about regular ram vs io space ram distinction.

View File

@ -4,56 +4,5 @@ main {
sub start() {
ubyte xx = 1
ubyte yy = 2
byte b1
byte b2
;; TODO add these constant folders:
;; (X - C1) <plusmin> (Y - C2) => (X <plusmin> Y) - (C1 <plusmin> C2)
;; (X - C1) - (Y - C2) => (X - Y) - (C1 - C2)
;
; result should be: 29 42 40 87 75 35
xx=6
yy=8
yy = (xx+5)+(yy+10)
txt.print_ub(yy) ; 29
txt.nl()
xx=6
yy=8
yy = (xx*3)+(yy*3)
txt.print_ub(yy) ; 42
txt.nl()
xx=13
yy=5
yy = (xx*5)-(yy*5)
txt.print_ub(yy) ; 40
txt.nl()
b1=100
b2=8
b2 = (b1+5)-(b2+10)
txt.print_b(b2) ; 87
txt.nl()
b1=50
b2=40
b2 = (b1-5)+(b2-10)
txt.print_b(b2) ; 75
txt.nl()
b1=50
b2=20
b2 = (b1-5)-(b2-10)
txt.print_b(b2) ; 35
txt.nl()
repeat {
}
}
}