From ad92f39469c19377dcb8bbf59624c7e0263121aa Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 16 May 2026 23:01:49 +0200 Subject: [PATCH] fix error message for conflicting memory() calls --- .../compiler/astprocessing/CodeDesugarer.kt | 14 ++++++++------ .../src/prog8/ast/statements/AstStatements.kt | 2 +- examples/test.p8 | 19 ++++--------------- 3 files changed, 13 insertions(+), 22 deletions(-) diff --git a/compiler/src/prog8/compiler/astprocessing/CodeDesugarer.kt b/compiler/src/prog8/compiler/astprocessing/CodeDesugarer.kt index db71ce099..43e5aed1b 100644 --- a/compiler/src/prog8/compiler/astprocessing/CodeDesugarer.kt +++ b/compiler/src/prog8/compiler/astprocessing/CodeDesugarer.kt @@ -26,14 +26,14 @@ internal class CodeDesugarer(val program: Program, private val target: ICompilat // - replace ptr^^ by @(ptr) if ptr is just an uword. // - replace p1^^ = p2^^ by memcopy. - private val globalReservedSlabs = mutableSetOf() + private val globalReservedSlabs = mutableMapOf() override fun before(program: Program): Iterable { globalReservedSlabs.clear() // Pre-collect existing reservations from the entire program to avoid duplicates in multiple passes val collector = object : IAstVisitor { override fun visit(reservation: MemorySlabReservation) { - globalReservedSlabs.add(reservation.slabName) + globalReservedSlabs[reservation.slabName] = reservation } } program.modules.forEach { module -> @@ -297,9 +297,10 @@ _after: val container = containingStmt.parent as IStatementContainer val mods = mutableListOf(AstReplaceNode(functionCallExpr, ref, parent)) - if (slabName !in globalReservedSlabs) { + val existing = globalReservedSlabs[slabName] + if (existing == null || existing.size != size || existing.align != align) { mods.add(AstInsert.before(containingStmt, reservation, container)) - globalReservedSlabs.add(slabName) + globalReservedSlabs[slabName] = reservation } return mods } @@ -336,11 +337,12 @@ _after: val align = alignNum.toUInt() val container = parent as IStatementContainer - return if (slabName in globalReservedSlabs) { + val existing = globalReservedSlabs[slabName] + return if (existing != null && existing.size == size && existing.align == align) { listOf(AstRemove(functionCallStatement, container)) } else { - globalReservedSlabs.add(slabName) val reservation = MemorySlabReservation(slabName, size, align, functionCallStatement.position) + globalReservedSlabs[slabName] = reservation listOf(AstReplaceNode(functionCallStatement, reservation, parent)) } } diff --git a/compilerAst/src/prog8/ast/statements/AstStatements.kt b/compilerAst/src/prog8/ast/statements/AstStatements.kt index 59f908875..b834aa766 100644 --- a/compilerAst/src/prog8/ast/statements/AstStatements.kt +++ b/compilerAst/src/prog8/ast/statements/AstStatements.kt @@ -235,7 +235,7 @@ class Continue(override val position: Position) : Statement() { } -class MemorySlabReservation( +data class MemorySlabReservation( val slabName: String, val size: UInt, val align: UInt, diff --git a/examples/test.p8 b/examples/test.p8 index 5e3f516b3..a064c7a21 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -3,20 +3,9 @@ main { sub start() { - long @shared v = 1234567890 - long @shared a = v/77777 - - ;txt.iso() - txt.print_l(a) ; expected: 15873 - txt.nl() - txt.print_l(v/88888) ; expected: 13889 - txt.nl() - txt.print_l(v/-5555) ; expected: -222244 - txt.nl() - v = -999999999 - txt.print_l(v/-77777) ; expected: 12857 - txt.nl() - - ;sys.poweroff_system() + cx16.r0 = memory("buffer", 100, 0) + cx16.r1 = memory("buffer", 100, 0) + cx16.r2 = memory("buffer", 100, 2) + ;cx16.r3 = memory("buffer") } }