From 9e85571a7bf816bcccbb1cb5fbd91ce8add15f65 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 3 Aug 2025 20:51:03 +0200 Subject: [PATCH] fix pointer variable usage detection in other block --- .../michael_bull_kotlin_result_jvm.xml | 14 +++++------ codeCore/build.gradle.kts | 4 +-- codeGenCpu6502/build.gradle.kts | 4 +-- codeGenExperimental/build.gradle.kts | 4 +-- codeGenIntermediate/build.gradle.kts | 4 +-- codeOptimizers/build.gradle.kts | 4 +-- compiler/build.gradle.kts | 2 +- compiler/test/TestPointers.kt | 16 ++++++++++++ compilerAst/build.gradle.kts | 4 +-- compilerAst/src/prog8/compiler/CallGraph.kt | 10 +++++--- docs/source/todo.rst | 1 - examples/test.p8 | 25 ++++++------------- simpleAst/build.gradle.kts | 4 +-- virtualmachine/build.gradle.kts | 4 +-- 14 files changed, 46 insertions(+), 54 deletions(-) diff --git a/.idea/libraries/michael_bull_kotlin_result_jvm.xml b/.idea/libraries/michael_bull_kotlin_result_jvm.xml index 9614991a0..364c32716 100644 --- a/.idea/libraries/michael_bull_kotlin_result_jvm.xml +++ b/.idea/libraries/michael_bull_kotlin_result_jvm.xml @@ -1,19 +1,19 @@ - + - - + + - - + + - - + + diff --git a/codeCore/build.gradle.kts b/codeCore/build.gradle.kts index afbd84098..327ea463a 100644 --- a/codeCore/build.gradle.kts +++ b/codeCore/build.gradle.kts @@ -1,5 +1,3 @@ -import org.jetbrains.kotlin.gradle.dsl.JvmTarget - plugins { kotlin("jvm") } @@ -7,7 +5,7 @@ plugins { dependencies { // should have no dependencies to other modules // implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") - implementation("com.michael-bull.kotlin-result:kotlin-result-jvm:2.0.1") + implementation("com.michael-bull.kotlin-result:kotlin-result-jvm:2.1.0") } sourceSets { diff --git a/codeGenCpu6502/build.gradle.kts b/codeGenCpu6502/build.gradle.kts index 2c23e0383..d64a8ef14 100644 --- a/codeGenCpu6502/build.gradle.kts +++ b/codeGenCpu6502/build.gradle.kts @@ -1,5 +1,3 @@ -import org.jetbrains.kotlin.gradle.dsl.JvmTarget - plugins { kotlin("jvm") } @@ -9,7 +7,7 @@ dependencies { implementation(project(":simpleAst")) // implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") // implementation "org.jetbrains.kotlin:kotlin-reflect" - implementation("com.michael-bull.kotlin-result:kotlin-result-jvm:2.0.1") + implementation("com.michael-bull.kotlin-result:kotlin-result-jvm:2.1.0") testImplementation("io.kotest:kotest-runner-junit5-jvm:5.9.1") testImplementation("io.kotest:kotest-framework-datatest:5.9.1") diff --git a/codeGenExperimental/build.gradle.kts b/codeGenExperimental/build.gradle.kts index cfa1e5e46..34821bd21 100644 --- a/codeGenExperimental/build.gradle.kts +++ b/codeGenExperimental/build.gradle.kts @@ -1,5 +1,3 @@ -import org.jetbrains.kotlin.gradle.dsl.JvmTarget - plugins { kotlin("jvm") } @@ -11,7 +9,7 @@ dependencies { implementation(project(":codeGenIntermediate")) // implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") // implementation "org.jetbrains.kotlin:kotlin-reflect" - implementation("com.michael-bull.kotlin-result:kotlin-result-jvm:2.0.1") + implementation("com.michael-bull.kotlin-result:kotlin-result-jvm:2.1.0") } sourceSets { diff --git a/codeGenIntermediate/build.gradle.kts b/codeGenIntermediate/build.gradle.kts index eff861f5c..4ac385dda 100644 --- a/codeGenIntermediate/build.gradle.kts +++ b/codeGenIntermediate/build.gradle.kts @@ -1,5 +1,3 @@ -import org.jetbrains.kotlin.gradle.dsl.JvmTarget - plugins { kotlin("jvm") } @@ -11,7 +9,7 @@ dependencies { implementation(project(":intermediate")) // implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") // implementation "org.jetbrains.kotlin:kotlin-reflect" - implementation("com.michael-bull.kotlin-result:kotlin-result-jvm:2.0.1") + implementation("com.michael-bull.kotlin-result:kotlin-result-jvm:2.1.0") testImplementation("io.kotest:kotest-runner-junit5-jvm:5.9.1") testImplementation("org.junit.jupiter:junit-jupiter:5.9.1") diff --git a/codeOptimizers/build.gradle.kts b/codeOptimizers/build.gradle.kts index 34f38cb1d..e329a6837 100644 --- a/codeOptimizers/build.gradle.kts +++ b/codeOptimizers/build.gradle.kts @@ -1,5 +1,3 @@ -import org.jetbrains.kotlin.gradle.dsl.JvmTarget - plugins { kotlin("jvm") } @@ -8,7 +6,7 @@ dependencies { implementation(project(":codeCore")) implementation(project(":compilerAst")) // implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") - implementation("com.michael-bull.kotlin-result:kotlin-result-jvm:2.0.1") + implementation("com.michael-bull.kotlin-result:kotlin-result-jvm:2.1.0") // implementation "org.jetbrains.kotlin:kotlin-reflect" } diff --git a/compiler/build.gradle.kts b/compiler/build.gradle.kts index b6651251f..778cb89f3 100644 --- a/compiler/build.gradle.kts +++ b/compiler/build.gradle.kts @@ -19,7 +19,7 @@ dependencies { // implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") // implementation("org.jetbrains.kotlin:kotlin-reflect") implementation("org.jetbrains.kotlinx:kotlinx-cli:0.3.6") - implementation("com.michael-bull.kotlin-result:kotlin-result-jvm:2.0.1") + implementation("com.michael-bull.kotlin-result:kotlin-result-jvm:2.1.0") testImplementation(project(":codeCore")) testImplementation(project(":intermediate")) diff --git a/compiler/test/TestPointers.kt b/compiler/test/TestPointers.kt index 34ed56ddb..58c0da16a 100644 --- a/compiler/test/TestPointers.kt +++ b/compiler/test/TestPointers.kt @@ -1707,4 +1707,20 @@ main { errors.errors[1] shouldContain "assigning this value to struct instance not supported" } + test("pointer variable usage detection in other block") { + val src=""" +main { + sub start() { + other.bptr^^ = true + cx16.r0bL = other.bptr^^ + } +} + +other { + ^^bool bptr +}""" + compileText(VMTarget(), false, src, outputDir, writeAssembly = false) shouldNotBe null + compileText(VMTarget(), true, src, outputDir, writeAssembly = false) shouldNotBe null + } + }) \ No newline at end of file diff --git a/compilerAst/build.gradle.kts b/compilerAst/build.gradle.kts index 493788d33..9c258aa6a 100644 --- a/compilerAst/build.gradle.kts +++ b/compilerAst/build.gradle.kts @@ -1,5 +1,3 @@ -import org.jetbrains.kotlin.gradle.dsl.JvmTarget - plugins { kotlin("jvm") } @@ -8,7 +6,7 @@ dependencies { implementation(project(":codeCore")) // implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") implementation("org.antlr:antlr4-runtime:4.13.2") - implementation("com.michael-bull.kotlin-result:kotlin-result-jvm:2.0.1") + implementation("com.michael-bull.kotlin-result:kotlin-result-jvm:2.1.0") implementation(project(":parser")) } diff --git a/compilerAst/src/prog8/compiler/CallGraph.kt b/compilerAst/src/prog8/compiler/CallGraph.kt index 37ad31dac..16558a92d 100644 --- a/compilerAst/src/prog8/compiler/CallGraph.kt +++ b/compilerAst/src/prog8/compiler/CallGraph.kt @@ -166,9 +166,13 @@ class CallGraph(private val program: Program) : IAstVisitor { } override fun visit(deref: PtrDereference) { - val first = deref.definingScope.lookup(deref.chain.take(1)) - if(first is VarDecl) { - allIdentifiersAndTargets.add(IdentifierReference(listOf(first.name), first.position) to first) + val chain = deref.chain.toMutableList() + while(chain.isNotEmpty()) { + val variable = deref.definingScope.lookup(chain) + if(variable is VarDecl) { + allIdentifiersAndTargets.add(IdentifierReference(listOf(variable.name), variable.position) to variable) + } + chain.removeLastOrNull() } super.visit(deref) } diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 6c61ff569..09a80e378 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -1,7 +1,6 @@ TODO ==== - STRUCTS and TYPED POINTERS -------------------------- diff --git a/examples/test.p8 b/examples/test.p8 index e0b0ab30b..af0a27463 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,23 +1,12 @@ -%import floats -%import textio - -%option no_sysinit -%zeropage basicsafe main { sub start() { - pokebool(3000, true) - pokew(3100, 12345) - pokef(3200, 3.1415927) - poke(3300, 222) - - txt.print_bool(peekbool(3000)) - txt.nl() - txt.print_uw(peekw(3100)) - txt.nl() - txt.print_f(peekf(3200)) - txt.nl() - txt.print_ub(peek(3300)) - txt.nl() + other.bptr^^ = true + ; other.bptr = 2222 + ;cx16.r0bL = other.bptr^^ } } + +other { + ^^bool bptr +} diff --git a/simpleAst/build.gradle.kts b/simpleAst/build.gradle.kts index d96928202..615902edb 100644 --- a/simpleAst/build.gradle.kts +++ b/simpleAst/build.gradle.kts @@ -1,5 +1,3 @@ -import org.jetbrains.kotlin.gradle.dsl.JvmTarget - plugins { kotlin("jvm") } @@ -7,7 +5,7 @@ plugins { dependencies { implementation(project(":codeCore")) // implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") - implementation("com.michael-bull.kotlin-result:kotlin-result-jvm:2.0.1") + implementation("com.michael-bull.kotlin-result:kotlin-result-jvm:2.1.0") } sourceSets { diff --git a/virtualmachine/build.gradle.kts b/virtualmachine/build.gradle.kts index 9a8293d15..6a840d9b2 100644 --- a/virtualmachine/build.gradle.kts +++ b/virtualmachine/build.gradle.kts @@ -1,5 +1,3 @@ -import org.jetbrains.kotlin.gradle.dsl.JvmTarget - plugins { kotlin("jvm") } @@ -9,7 +7,7 @@ dependencies { implementation(project(":codeCore")) implementation(project(":intermediate")) // implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") - implementation("com.michael-bull.kotlin-result:kotlin-result-jvm:2.0.1") + implementation("com.michael-bull.kotlin-result:kotlin-result-jvm:2.1.0") testImplementation("io.kotest:kotest-runner-junit5-jvm:5.9.1") testImplementation("io.kotest:kotest-framework-datatest:5.9.1") testImplementation("org.junit.jupiter:junit-jupiter:5.9.1")