mirror of
https://github.com/irmen/prog8.git
synced 2024-10-09 13:55:19 +00:00
try fixing a weird problem with pointervar[idx] -> memread rewriting
this was introduced in the removal of structs somehow
This commit is contained in:
parent
d5214e2505
commit
664818fd29
@ -2,11 +2,12 @@ package prog8.compiler.astprocessing
|
|||||||
|
|
||||||
import prog8.ast.Node
|
import prog8.ast.Node
|
||||||
import prog8.ast.Program
|
import prog8.ast.Program
|
||||||
|
import prog8.ast.base.DataType
|
||||||
|
import prog8.ast.expressions.ArrayIndexedExpression
|
||||||
import prog8.ast.expressions.BinaryExpression
|
import prog8.ast.expressions.BinaryExpression
|
||||||
|
import prog8.ast.expressions.DirectMemoryRead
|
||||||
import prog8.ast.expressions.StringLiteralValue
|
import prog8.ast.expressions.StringLiteralValue
|
||||||
import prog8.ast.statements.ParameterVarDecl
|
import prog8.ast.statements.*
|
||||||
import prog8.ast.statements.Subroutine
|
|
||||||
import prog8.ast.statements.VarDecl
|
|
||||||
import prog8.ast.walk.AstWalker
|
import prog8.ast.walk.AstWalker
|
||||||
import prog8.ast.walk.IAstModification
|
import prog8.ast.walk.IAstModification
|
||||||
|
|
||||||
@ -65,6 +66,10 @@ internal class AstVariousTransforms(private val program: Program) : AstWalker()
|
|||||||
return noModifications
|
return noModifications
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun after(arrayIndexedExpression: ArrayIndexedExpression, parent: Node): Iterable<IAstModification> {
|
||||||
|
return replacePointerVarIndexWithMemread(program, arrayIndexedExpression, parent)
|
||||||
|
}
|
||||||
|
|
||||||
private fun concatString(expr: BinaryExpression): StringLiteralValue? {
|
private fun concatString(expr: BinaryExpression): StringLiteralValue? {
|
||||||
val rightStrval = expr.right as? StringLiteralValue
|
val rightStrval = expr.right as? StringLiteralValue
|
||||||
val leftStrval = expr.left 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<IAstModification> {
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
@ -4,7 +4,6 @@ import prog8.ast.IFunctionCall
|
|||||||
import prog8.ast.Module
|
import prog8.ast.Module
|
||||||
import prog8.ast.Node
|
import prog8.ast.Node
|
||||||
import prog8.ast.Program
|
import prog8.ast.Program
|
||||||
import prog8.ast.base.ArrayDatatypes
|
|
||||||
import prog8.ast.base.DataType
|
import prog8.ast.base.DataType
|
||||||
import prog8.ast.base.FatalAstException
|
import prog8.ast.base.FatalAstException
|
||||||
import prog8.ast.expressions.*
|
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<IAstModification> {
|
override fun after(arrayIndexedExpression: ArrayIndexedExpression, parent: Node): Iterable<IAstModification> {
|
||||||
|
return replacePointerVarIndexWithMemread(program, arrayIndexedExpression, parent)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun after(expr: BinaryExpression, parent: Node): Iterable<IAstModification> {
|
override fun after(expr: BinaryExpression, parent: Node): Iterable<IAstModification> {
|
||||||
|
@ -3,23 +3,12 @@ main {
|
|||||||
|
|
||||||
sub start() {
|
sub start() {
|
||||||
|
|
||||||
ubyte xx
|
uword enemyRef = $2000
|
||||||
|
ubyte cur
|
||||||
|
|
||||||
xx |= 44
|
cur = enemyRef[1]
|
||||||
xx &= 44
|
+ enemyRef[2]
|
||||||
xx ^= 44
|
+ enemyRef[3]
|
||||||
|
|
||||||
xx |= @($d020)
|
|
||||||
xx &= @($d020)
|
|
||||||
xx ^= @($d020)
|
|
||||||
|
|
||||||
uword ww
|
|
||||||
|
|
||||||
ww |= $4433
|
|
||||||
ww &= $4433
|
|
||||||
ww ^= $4433
|
|
||||||
ww |= @($d020)
|
|
||||||
ww &= @($d020)
|
|
||||||
ww ^= @($d020)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user