From 664818fd298c38f8e077528ea91e4768f62c87cf Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Fri, 30 Apr 2021 01:34:03 +0200 Subject: [PATCH] try fixing a weird problem with pointervar[idx] -> memread rewriting this was introduced in the removal of structs somehow --- .../astprocessing/AstVariousTransforms.kt | 33 +++++++++++++++++-- .../astprocessing/StatementReorderer.kt | 20 +---------- examples/test.p8 | 21 +++--------- 3 files changed, 36 insertions(+), 38 deletions(-) diff --git a/compiler/src/prog8/compiler/astprocessing/AstVariousTransforms.kt b/compiler/src/prog8/compiler/astprocessing/AstVariousTransforms.kt index 9741770aa..d4bf179b8 100644 --- a/compiler/src/prog8/compiler/astprocessing/AstVariousTransforms.kt +++ b/compiler/src/prog8/compiler/astprocessing/AstVariousTransforms.kt @@ -2,11 +2,12 @@ package prog8.compiler.astprocessing import prog8.ast.Node import prog8.ast.Program +import prog8.ast.base.DataType +import prog8.ast.expressions.ArrayIndexedExpression import prog8.ast.expressions.BinaryExpression +import prog8.ast.expressions.DirectMemoryRead import prog8.ast.expressions.StringLiteralValue -import prog8.ast.statements.ParameterVarDecl -import prog8.ast.statements.Subroutine -import prog8.ast.statements.VarDecl +import prog8.ast.statements.* import prog8.ast.walk.AstWalker import prog8.ast.walk.IAstModification @@ -65,6 +66,10 @@ internal class AstVariousTransforms(private val program: Program) : AstWalker() return noModifications } + override fun after(arrayIndexedExpression: ArrayIndexedExpression, parent: Node): Iterable { + return replacePointerVarIndexWithMemread(program, arrayIndexedExpression, parent) + } + private fun concatString(expr: BinaryExpression): StringLiteralValue? { val rightStrval = expr.right as? StringLiteralValue val leftStrval = expr.left as? StringLiteralValue @@ -91,3 +96,25 @@ internal class AstVariousTransforms(private val program: Program) : AstWalker() } } } + + + +internal fun replacePointerVarIndexWithMemread(program: Program, arrayIndexedExpression: ArrayIndexedExpression, parent: Node): Iterable { + val arrayVar = arrayIndexedExpression.arrayvar.targetVarDecl(program) + if(arrayVar!=null && arrayVar.datatype == DataType.UWORD) { + // rewrite pointervar[index] into @(pointervar+index) + val indexer = arrayIndexedExpression.indexer + val add = BinaryExpression(arrayIndexedExpression.arrayvar, "+", indexer.indexExpr, arrayIndexedExpression.position) + return if(parent is AssignTarget) { + // we're part of the target of an assignment, we have to actually change the assign target itself + val memwrite = DirectMemoryWrite(add, arrayIndexedExpression.position) + val newtarget = AssignTarget(null, null, memwrite, arrayIndexedExpression.position) + listOf(IAstModification.ReplaceNode(parent, newtarget, parent.parent)) + } else { + val memread = DirectMemoryRead(add, arrayIndexedExpression.position) + listOf(IAstModification.ReplaceNode(arrayIndexedExpression, memread, parent)) + } + } + + return emptyList() +} diff --git a/compiler/src/prog8/compiler/astprocessing/StatementReorderer.kt b/compiler/src/prog8/compiler/astprocessing/StatementReorderer.kt index 62d60dfb6..059f0740d 100644 --- a/compiler/src/prog8/compiler/astprocessing/StatementReorderer.kt +++ b/compiler/src/prog8/compiler/astprocessing/StatementReorderer.kt @@ -4,7 +4,6 @@ import prog8.ast.IFunctionCall import prog8.ast.Module import prog8.ast.Node import prog8.ast.Program -import prog8.ast.base.ArrayDatatypes import prog8.ast.base.DataType import prog8.ast.base.FatalAstException import prog8.ast.expressions.* @@ -87,24 +86,7 @@ internal class StatementReorderer(val program: Program, val errors: IErrorReport } override fun after(arrayIndexedExpression: ArrayIndexedExpression, parent: Node): Iterable { - - val arrayVar = arrayIndexedExpression.arrayvar.targetVarDecl(program) - if(arrayVar!=null && arrayVar.datatype == DataType.UWORD) { - // rewrite pointervar[index] into @(pointervar+index) - val indexer = arrayIndexedExpression.indexer - val add = BinaryExpression(arrayIndexedExpression.arrayvar, "+", indexer.indexExpr, arrayIndexedExpression.position) - return if(parent is AssignTarget) { - // we're part of the target of an assignment, we have to actually change the assign target itself - val memwrite = DirectMemoryWrite(add, arrayIndexedExpression.position) - val newtarget = AssignTarget(null, null, memwrite, arrayIndexedExpression.position) - listOf(IAstModification.ReplaceNode(parent, newtarget, parent.parent)) - } else { - val memread = DirectMemoryRead(add, arrayIndexedExpression.position) - listOf(IAstModification.ReplaceNode(arrayIndexedExpression, memread, parent)) - } - } - - return noModifications + return replacePointerVarIndexWithMemread(program, arrayIndexedExpression, parent) } override fun after(expr: BinaryExpression, parent: Node): Iterable { diff --git a/examples/test.p8 b/examples/test.p8 index f13f17a94..b86bfda6a 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -3,23 +3,12 @@ main { sub start() { - ubyte xx + uword enemyRef = $2000 + ubyte cur - xx |= 44 - xx &= 44 - xx ^= 44 + cur = enemyRef[1] + + enemyRef[2] + + enemyRef[3] - xx |= @($d020) - xx &= @($d020) - xx ^= @($d020) - - uword ww - - ww |= $4433 - ww &= $4433 - ww ^= $4433 - ww |= @($d020) - ww &= @($d020) - ww ^= @($d020) } }