From 107935ed31d2dd4f7ed072ad9253db8142f5527e Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 20 Nov 2021 22:43:08 +0100 Subject: [PATCH] add some more const folding patterns --- .../optimizer/ConstantFoldingOptimizer.kt | 9 ++++ compiler/src/prog8/compiler/Compiler.kt | 4 +- docs/source/todo.rst | 1 - examples/test.p8 | 51 ------------------- 4 files changed, 11 insertions(+), 54 deletions(-) diff --git a/codeOptimizers/src/prog8/optimizer/ConstantFoldingOptimizer.kt b/codeOptimizers/src/prog8/optimizer/ConstantFoldingOptimizer.kt index ca24b08eb..76ef5d137 100644 --- a/codeOptimizers/src/prog8/optimizer/ConstantFoldingOptimizer.kt +++ b/codeOptimizers/src/prog8/optimizer/ConstantFoldingOptimizer.kt @@ -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) (Y - C2) => (X Y) - (C1 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) (Y * C) => (X Y) * C diff --git a/compiler/src/prog8/compiler/Compiler.kt b/compiler/src/prog8/compiler/Compiler.kt index 5a2df381d..db9cc148d 100644 --- a/compiler/src/prog8/compiler/Compiler.kt +++ b/compiler/src/prog8/compiler/Compiler.kt @@ -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, diff --git a/docs/source/todo.rst b/docs/source/todo.rst index d8f1a12ba..2ebd173b1 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -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. diff --git a/examples/test.p8 b/examples/test.p8 index 8730b07b9..ad777465f 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -4,56 +4,5 @@ main { sub start() { - 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) -; - - - ; 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 { - } } }