From 8f9d1cfa3087a47777e78d014717f4fdc1e44143 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Mon, 24 Oct 2022 23:43:47 +0200 Subject: [PATCH] fix regression: indexing pointer variable with word (>255) didn't work anymore since release 8.2 or so --- .../compiler/astprocessing/CodeDesugarer.kt | 28 +++++++++++++++++++ compiler/test/ast/TestVarious.kt | 18 +++++++++++- docs/source/todo.rst | 2 ++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/compiler/src/prog8/compiler/astprocessing/CodeDesugarer.kt b/compiler/src/prog8/compiler/astprocessing/CodeDesugarer.kt index a5ba6374f..a1ad5b372 100644 --- a/compiler/src/prog8/compiler/astprocessing/CodeDesugarer.kt +++ b/compiler/src/prog8/compiler/astprocessing/CodeDesugarer.kt @@ -23,6 +23,7 @@ internal class CodeDesugarer(val program: Program, // - replace while and do-until loops by just jumps. // - replace peek() and poke() by direct memory accesses. // - repeat-forever loops replaced by label+jump. + // - pointer[word] replaced by @(pointer+word) override fun before(breakStmt: Break, parent: Node): Iterable { @@ -135,4 +136,31 @@ _after: } return noModifications } + + override fun after(arrayIndexedExpression: ArrayIndexedExpression, parent: Node): Iterable { + // replace pointervar[word] by @(pointervar+word) + val indexExpr = arrayIndexedExpression.indexer.indexExpr + val indexerDt = indexExpr.inferType(program) + if(indexerDt.isWords) { + val arrayVar = arrayIndexedExpression.arrayvar.targetVarDecl(program)!! + if(arrayVar.datatype==DataType.UWORD) { + val add: Expression = + if(indexExpr.constValue(program)?.number==0.0) + arrayIndexedExpression.arrayvar.copy() + else + BinaryExpression(arrayIndexedExpression.arrayvar.copy(), "+", indexExpr, arrayIndexedExpression.position) + return if(parent is AssignTarget) { + // assignment to array + val memwrite = DirectMemoryWrite(add, arrayIndexedExpression.position) + val newtarget = AssignTarget(null, null, memwrite, arrayIndexedExpression.position) + listOf(IAstModification.ReplaceNode(parent, newtarget, parent.parent)) + } else { + // read from array + val memread = DirectMemoryRead(add, arrayIndexedExpression.position) + listOf(IAstModification.ReplaceNode(arrayIndexedExpression, memread, parent)) + } + } + } + return noModifications + } } diff --git a/compiler/test/ast/TestVarious.kt b/compiler/test/ast/TestVarious.kt index 9c07e71a5..dd58614b5 100644 --- a/compiler/test/ast/TestVarious.kt +++ b/compiler/test/ast/TestVarious.kt @@ -100,7 +100,6 @@ main { } }""" val result = compileText(C64Target(), optimize=false, src, writeAssembly=true)!! - printProgram(result.program) val stmts = result.program.entrypoint.statements stmts.size shouldBe 6 val name1 = stmts[0] as VarDecl @@ -114,5 +113,22 @@ main { (name2.targetVarDecl(result.program)!!.value as StringLiteral).value shouldBe "xx1xx2" (rept2.targetVarDecl(result.program)!!.value as StringLiteral).value shouldBe "xyzxyzxyzxyz" } + + test("pointervariable indexing allowed with >255") { + val src=""" +main { + sub start() { + uword pointer = ${'$'}2000 + @(pointer+${'$'}1000) = 123 + ubyte @shared ub = @(pointer+${'$'}1000) + pointer[${'$'}1000] = 99 + ub = pointer[${'$'}1000] + uword index = ${'$'}1000 + pointer[index] = 55 + ub = pointer[index] + } +}""" + compileText(C64Target(), optimize=false, src, writeAssembly=false) shouldNotBe null + } }) diff --git a/docs/source/todo.rst b/docs/source/todo.rst index d7637501c..3a130d76e 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -4,6 +4,8 @@ TODO For next release ^^^^^^^^^^^^^^^^ - ir: asmsub contents remains blank in IR file +- add cx16diskio.vload_raw() to load headerless files into vram +- mention the syntax highlighting files in the readme and the docs, and add note to the IDEA one that it can also be used in Rider ...