fix invalid removal of repeated assignments.

This commit is contained in:
Irmen de Jong 2022-06-06 17:09:22 +02:00
parent d56eb397f9
commit b042b7705e
6 changed files with 50 additions and 42 deletions

View File

@ -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<StStaticVariable> {

View File

@ -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<IAstModification> {
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)

View File

@ -59,7 +59,7 @@ internal class VariousCleanups(val program: Program, val errors: IErrorReporter,
override fun after(assignment: Assignment, parent: Node): Iterable<IAstModification> {
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))
}

View File

@ -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)

View File

@ -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?

View File

@ -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()