fix invalid size copied when initializing arrays in Zeropage

This commit is contained in:
Irmen de Jong 2022-01-23 13:00:01 +01:00
parent 1e053783f3
commit 0e87db9eb7
4 changed files with 20 additions and 24 deletions

View File

@ -124,16 +124,16 @@ class AsmGen(private val program: Program,
.sortedBy { options.compTarget.memorySize(it.first.datatype) } // allocate the smallest DT first .sortedBy { options.compTarget.memorySize(it.first.datatype) } // allocate the smallest DT first
for ((vardecl, scopedname) in varsRequiringZp) { for ((vardecl, scopedname) in varsRequiringZp) {
val arraySize: Int? = when(vardecl.datatype) { val numElements: Int? = when(vardecl.datatype) {
DataType.STR -> { DataType.STR -> {
(vardecl.value as StringLiteralValue).value.length (vardecl.value as StringLiteralValue).value.length
} }
in ArrayDatatypes -> { in ArrayDatatypes -> {
vardecl.arraysize!!.constIndex() // TODO wrong size for non-byte arrays??????? or does datatype get taken into account below vardecl.arraysize!!.constIndex()
} }
else -> null else -> null
} }
val result = zeropage.allocate(scopedname, vardecl.datatype, arraySize, vardecl.position, errors) val result = zeropage.allocate(scopedname, vardecl.datatype, numElements, vardecl.position, errors)
result.fold( result.fold(
success = { varsInZeropage.add(vardecl) }, success = { varsInZeropage.add(vardecl) },
failure = { errors.err(it.message!!, vardecl.position) } failure = { errors.err(it.message!!, vardecl.position) }
@ -1193,7 +1193,8 @@ class AsmGen(private val program: Program,
jsr prog8_lib.strcpy""") jsr prog8_lib.strcpy""")
} }
arrayVarsInZp.forEach { arrayVarsInZp.forEach {
val numelements = (it.value as ArrayLiteralValue).value.size // TODO wrong size for word/float arrays!??? val numelements = (it.value as ArrayLiteralValue).value.size
val size = numelements * program.memsizer.memorySize(ArrayToElementTypes.getValue(it.datatype))
out(""" out("""
lda #<${it.name}_init_value lda #<${it.name}_init_value
ldy #>${it.name}_init_value ldy #>${it.name}_init_value
@ -1203,8 +1204,8 @@ class AsmGen(private val program: Program,
ldy #>${it.name} ldy #>${it.name}
sta cx16.r1L sta cx16.r1L
sty cx16.r1H sty cx16.r1H
lda #<$numelements lda #<$size
ldy #>$numelements ldy #>$size
jsr sys.memcopy""") jsr sys.memcopy""")
} }
out(" jmp +") out(" jmp +")

View File

@ -40,7 +40,7 @@ abstract class Zeropage(protected val options: CompilationOptions) {
return free.windowed(2).any { it[0] == it[1] - 1u } return free.windowed(2).any { it[0] == it[1] - 1u }
} }
fun allocate(name: List<String>, datatype: DataType, arraySize: Int?, position: Position?, errors: IErrorReporter): Result<Pair<UInt, Int>, ZeropageAllocationError> { fun allocate(name: List<String>, datatype: DataType, numElements: Int?, position: Position?, errors: IErrorReporter): Result<Pair<UInt, Int>, ZeropageAllocationError> {
require(name.isEmpty() || !allocations.values.any { it.first==name } ) {"name can't be allocated twice"} require(name.isEmpty() || !allocations.values.any { it.first==name } ) {"name can't be allocated twice"}
if(options.zeropage== ZeropageType.DONTUSE) if(options.zeropage== ZeropageType.DONTUSE)
@ -50,7 +50,7 @@ abstract class Zeropage(protected val options: CompilationOptions) {
when (datatype) { when (datatype) {
in IntegerDatatypes -> options.compTarget.memorySize(datatype) in IntegerDatatypes -> options.compTarget.memorySize(datatype)
DataType.STR, in ArrayDatatypes -> { DataType.STR, in ArrayDatatypes -> {
val memsize = arraySize!! * options.compTarget.memorySize(ArrayToElementTypes.getValue(datatype)) val memsize = numElements!! * options.compTarget.memorySize(ArrayToElementTypes.getValue(datatype))
if(position!=null) if(position!=null)
errors.warn("allocating a large value in zeropage; str/array $memsize bytes", position) errors.warn("allocating a large value in zeropage; str/array $memsize bytes", position)
else else

View File

@ -3,8 +3,6 @@ TODO
For next release For next release
^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^
- check/fix allocateAllZeropageVariables() for wrong array byte size?
- check/fix entrypointInitialization() for non-byte arrays in ZP, memcopy size wrong?
- Fix compiler stack overflow crash: - Fix compiler stack overflow crash:
sub sprite_y_for_row(ubyte row) -> word { sub sprite_y_for_row(ubyte row) -> word {
return (8-row as byte) return (8-row as byte)

View File

@ -1,21 +1,18 @@
%import textio %import textio
%zeropage basicsafe %import floats
main { main {
ubyte[4] @shared @requirezp arr1 = [1,2,3,4]
uword[2] @shared @requirezp arr2 = [111,222]
float[2] @shared @requirezp arr3 = [1.111, 2.222]
sub start() { sub start() {
word[3] @shared sprites_x ; = sprites.sprites_x txt.print_ub(arr1[3])
sprites_x = sprites.sprites_x txt.spc()
word[3] @shared sprites_y ; = sprites.sprites_y txt.print_uw(arr2[1])
sprites_y = sprites.sprites_y txt.spc()
floats.print_f(arr3[1])
txt.print_w(sprites.sprites_x[2]) repeat {
txt.nl() }
txt.print_w(sprites.sprites_y[2])
txt.nl()
txt.print_w(sprites_x[2])
txt.nl()
txt.print_w(sprites_y[2])
txt.nl()
} }
} }