fix romable error check for arrays

This commit is contained in:
Irmen de Jong
2025-12-30 02:26:01 +01:00
parent 5b32cfd172
commit d386343fe7
3 changed files with 31 additions and 14 deletions
@@ -801,9 +801,13 @@ internal class AstChecker(private val program: Program,
// cannot check pointer deref for rom target, assume no there.
if(idx.plainarrayvar!=null) {
val decl = idx.plainarrayvar!!.targetVarDecl()!!
if(decl.type!=VarDeclType.MEMORY && decl.zeropage!=ZeropageWish.REQUIRE_ZEROPAGE) {
// memory mapped arrays are assumed to be in RAM. If they're not.... well, POOF
errors.err("cannot assign to an array or string that is located in ROM (option romable is enabled)", assignTarget.position)
val declvalueNumber = (decl.value as? NumericLiteral)?.number
if(decl.type==VarDeclType.MEMORY && (declvalueNumber==null || declvalueNumber>255) ) {
errors.err("cannot write to this memory mapped string or array (possibly located in ROM because option romable is enabled)", assignTarget.position)
}
if(decl.type==VarDeclType.VAR && decl.value!=null) {
// arrays with initializer value is not placed in BSS so is no longer mutable
errors.err("cannot write to a string or an array with initalization values (located in ROM because option romable is enabled)", assignTarget.position)
}
}
}
+12 -4
View File
@@ -422,18 +422,26 @@ class TestMemory: FunSpec({
main {
ubyte[] array = [1,2,3,4,5]
ubyte[10] ramarray
str name = "foobar"
&ubyte[10] memorymappedarray = 1000
&ubyte[10] memorymappedarray_zp = 200
sub start() {
array[2] = 99
name[2] = 'x'
name[2] = 'x'
ramarray[2] = 42
memorymappedarray[2] = 42
memorymappedarray_zp[2] = 42
}
}"""
val errors=ErrorReporterForTests()
compileText(C64Target(), false, src, outputDir, errors=errors, writeAssembly = false, varshigh=1, slabshigh=1) shouldBe null
errors.errors.size shouldBe 2
errors.errors[0] shouldContain "8:9: cannot assign to an array or string that is located in ROM"
errors.errors[1] shouldContain "9:9: cannot assign to an array or string that is located in ROM"
errors.errors.size shouldBe 3
errors.errors[0] shouldContain "12:9: cannot write to a string or an array with initalization values (located in ROM"
errors.errors[1] shouldContain "13:9: cannot write to a string or an array with initalization values (located in ROM"
errors.errors[2] shouldContain "15:9: cannot write to this memory mapped string or array (possibly located in ROM"
}
test("long addresses not yet supported in memread and memwrite") {
+12 -7
View File
@@ -1,14 +1,19 @@
%import textio
%zeropage basicsafe
%option no_sysinit
%option no_sysinit, romable
main {
sub start() {
long v = $12345678
ubyte @shared ubv = 15
v <<= 31 - ubv
sub start() {
str name = "irmen"
ubyte[] array1 = [11,22,33,44]
ubyte[10] array2
&ubyte[10] memorymappedarray = 1000
&ubyte[10] memorymappedarray_zp = 200
txt.print_ulhex(v, true)
name[1] = 'a'
array1[1] = 'b'
array2[1] = 'c'
memorymappedarray[2] = 99
memorymappedarray_zp[2] = 99
}
}