From b042b7705e0bf3c88442447e3f834216fb8e0b67 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Mon, 6 Jun 2022 17:09:22 +0200 Subject: [PATCH] fix invalid removal of repeated assignments. --- .../codegen/cpu6502/VariableAllocator.kt | 6 +- .../astprocessing/AstOnetimeTransforms.kt | 7 +- .../compiler/astprocessing/VariousCleanups.kt | 2 +- .../prog8/ast/expressions/AstExpressions.kt | 2 +- docs/source/todo.rst | 1 - examples/test.p8 | 74 ++++++++++--------- 6 files changed, 50 insertions(+), 42 deletions(-) diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/VariableAllocator.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/VariableAllocator.kt index 500f8ea69..aebf3a2d1 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/VariableAllocator.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/VariableAllocator.kt @@ -116,9 +116,9 @@ internal class VariableAllocator(private val symboltable: SymbolTable, } } - println(" number of allocated vars: $numberOfAllocatableVariables") - println(" put into zeropage: $numVariablesAllocatedInZP, non-zp allocatable: ${numberOfNonIntegerVariables+numberOfExplicitNonZpVariables}") - println(" zeropage free space: ${zeropage.free.size} bytes") +// println(" number of allocated vars: $numberOfAllocatableVariables") +// println(" put into zeropage: $numVariablesAllocatedInZP, non-zp allocatable: ${numberOfNonIntegerVariables+numberOfExplicitNonZpVariables}") +// println(" zeropage free space: ${zeropage.free.size} bytes") } private fun collectAllVariables(st: SymbolTable): Collection { diff --git a/compiler/src/prog8/compiler/astprocessing/AstOnetimeTransforms.kt b/compiler/src/prog8/compiler/astprocessing/AstOnetimeTransforms.kt index f57f839a4..436f2eba8 100644 --- a/compiler/src/prog8/compiler/astprocessing/AstOnetimeTransforms.kt +++ b/compiler/src/prog8/compiler/astprocessing/AstOnetimeTransforms.kt @@ -1,11 +1,9 @@ package prog8.compiler.astprocessing -import prog8.ast.IFunctionCall import prog8.ast.Node import prog8.ast.Program import prog8.ast.expressions.ArrayIndexedExpression import prog8.ast.expressions.BinaryExpression -import prog8.ast.expressions.DirectMemoryRead import prog8.ast.expressions.Expression import prog8.ast.statements.AssignTarget import prog8.ast.statements.DirectMemoryWrite @@ -15,7 +13,6 @@ import prog8.ast.walk.IAstModification import prog8.code.core.CompilationOptions import prog8.code.core.DataType import prog8.code.target.VMTarget -import prog8.compiler.InplaceModifyingBuiltinFunctions internal class AstOnetimeTransforms(private val program: Program, private val options: CompilationOptions) : AstWalker() { @@ -33,10 +30,14 @@ internal class AstOnetimeTransforms(private val program: Program, private val op } private fun replacePointerVarIndexWithMemreadOrMemwrite(arrayIndexedExpression: ArrayIndexedExpression, parent: Node): Iterable { + if(options.compTarget.name== VMTarget.NAME) + return noModifications + val arrayVar = arrayIndexedExpression.arrayvar.targetVarDecl(program) if(arrayVar!=null && arrayVar.datatype == DataType.UWORD) { if(parent is AssignTarget) { // rewrite assignment target pointervar[index] into @(pointervar+index) + //println("REWRITE POINTER INDEXED: ${arrayVar.name}[${arrayIndexedExpression.indexer.indexExpr}] as assignment target at ${parent.position}") // TODO val indexer = arrayIndexedExpression.indexer val add: Expression = if(indexer.indexExpr.constValue(program)?.number==0.0) diff --git a/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt b/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt index 010d6e479..31babc35f 100644 --- a/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt +++ b/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt @@ -59,7 +59,7 @@ internal class VariousCleanups(val program: Program, val errors: IErrorReporter, override fun after(assignment: Assignment, parent: Node): Iterable { val nextAssign = assignment.nextSibling() as? Assignment if(nextAssign!=null && nextAssign.target.isSameAs(assignment.target, program)) { - if(nextAssign.value isSameAs assignment.value && assignment.value !is IFunctionCall) // don't remove function calls even when they're duplicates + if(!nextAssign.isAugmentable && nextAssign.value isSameAs assignment.value && assignment.value !is IFunctionCall) // don't remove function calls even when they're duplicates return listOf(IAstModification.Remove(assignment, parent as IStatementContainer)) } diff --git a/compilerAst/src/prog8/ast/expressions/AstExpressions.kt b/compilerAst/src/prog8/ast/expressions/AstExpressions.kt index 9353c4b6b..d2cb50b42 100644 --- a/compilerAst/src/prog8/ast/expressions/AstExpressions.kt +++ b/compilerAst/src/prog8/ast/expressions/AstExpressions.kt @@ -326,7 +326,7 @@ class ArrayIndexedExpression(var arrayvar: IdentifierReference, } override fun toString(): String { - return "ArrayIndexed(ident=$arrayvar, arraysize=$indexer; pos=$position)" + return "ArrayIndexed(ident=$arrayvar, idx=$indexer; pos=$position)" } override fun copy() = ArrayIndexedExpression(arrayvar.copy(), indexer.copy(), position) diff --git a/docs/source/todo.rst b/docs/source/todo.rst index adcf266d4..60ae3b42f 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,7 +3,6 @@ TODO For next release ^^^^^^^^^^^^^^^^ -- vm: fix optimized pointervar indexing codegen when it is an assignment target (i.e. writing to ptr[ix]) - 6502: fix optimized pointervar indexing codegen when it is an assignment target (i.e. writing to ptr[ix]) - pipe operator: (targets other than 'Virtual'): allow non-unary function calls in the pipe that specify the other argument(s) in the calls. Already working for VM target. - add McCarthy evaluation to shortcircuit and/or expressions. First do ifs by splitting them up? Then do expressions that compute a value? diff --git a/examples/test.p8 b/examples/test.p8 index e4b2025e1..9008a21de 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -40,41 +40,49 @@ main { uword bitmapbuf = &data ; 11 22 33 - txt.print_ub(bitmapbuf[0]) - txt.spc() - txt.print_ub(bitmapbuf[1]) - txt.spc() - txt.print_ub(bitmapbuf[2]) - txt.nl() - rol(bitmapbuf[0]) - rol(bitmapbuf[0]) - txt.print_ub(bitmapbuf[0]) ; 44 - txt.spc() - ror(bitmapbuf[0]) - ror(bitmapbuf[0]) - txt.print_ub(bitmapbuf[0]) ; 11 - txt.nl() +; txt.print_ub(bitmapbuf[0]) +; txt.spc() +; txt.print_ub(bitmapbuf[1]) +; txt.spc() +; txt.print_ub(bitmapbuf[2]) +; txt.nl() +; rol(bitmapbuf[0]) +; rol(bitmapbuf[0]) +; txt.print_ub(bitmapbuf[0]) ; 44 +; txt.spc() +; ror(bitmapbuf[0]) +; ror(bitmapbuf[0]) +; txt.print_ub(bitmapbuf[0]) ; 11 +; txt.nl() +; +; ; 22 44 66 +; txt.print_ub(bitmapbuf[0]*2) +; txt.spc() +; txt.print_ub(bitmapbuf[1]*2) +; txt.spc() +; txt.print_ub(bitmapbuf[2]*2) +; txt.nl() +; +; value = one+one+one+one+one +; txt.print_ub(value) ; 5 +; txt.nl() - ; 22 44 66 - txt.print_ub(bitmapbuf[0]*2) - txt.spc() - txt.print_ub(bitmapbuf[1]*2) - txt.spc() - txt.print_ub(bitmapbuf[2]*2) - txt.nl() - - value = one+one+one+one+one - txt.print_ub(value) ; 5 - txt.nl() - - bitmapbuf[0] = one + bitmapbuf[0] = one+one+one + bitmapbuf[0] = one+one+one + bitmapbuf[0] = one+one+one bitmapbuf[1] = one+one - bitmapbuf[2] = one+one+one - bitmapbuf[2] += 4 - bitmapbuf[2] -= 2 - bitmapbuf[2] -= 2 + bitmapbuf[1] = one+one + bitmapbuf[1] = one+one + bitmapbuf[2] = one + bitmapbuf[2] = one + bitmapbuf[2] = one + bitmapbuf[2] += 100 + bitmapbuf[2] -= 1 + bitmapbuf[2] -= 1 + bitmapbuf[2] -= 1 + bitmapbuf[2] -= 1 - ; 1 2 3 + ; 3 2 97 txt.print_ub(bitmapbuf[0]) txt.spc() txt.print_ub(bitmapbuf[1]) @@ -83,7 +91,7 @@ main { txt.nl() for value in data { - txt.print_ub(value) + txt.print_ub(value) ; 3 2 97 4 5 6 7 8 9 10 txt.spc() } txt.nl()