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
for ((vardecl, scopedname) in varsRequiringZp) {
val arraySize: Int? = when(vardecl.datatype) {
val numElements: Int? = when(vardecl.datatype) {
DataType.STR -> {
(vardecl.value as StringLiteralValue).value.length
}
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
}
val result = zeropage.allocate(scopedname, vardecl.datatype, arraySize, vardecl.position, errors)
val result = zeropage.allocate(scopedname, vardecl.datatype, numElements, vardecl.position, errors)
result.fold(
success = { varsInZeropage.add(vardecl) },
failure = { errors.err(it.message!!, vardecl.position) }
@ -1193,7 +1193,8 @@ class AsmGen(private val program: Program,
jsr prog8_lib.strcpy""")
}
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("""
lda #<${it.name}_init_value
ldy #>${it.name}_init_value
@ -1203,8 +1204,8 @@ class AsmGen(private val program: Program,
ldy #>${it.name}
sta cx16.r1L
sty cx16.r1H
lda #<$numelements
ldy #>$numelements
lda #<$size
ldy #>$size
jsr sys.memcopy""")
}
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 }
}
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"}
if(options.zeropage== ZeropageType.DONTUSE)
@ -50,7 +50,7 @@ abstract class Zeropage(protected val options: CompilationOptions) {
when (datatype) {
in IntegerDatatypes -> options.compTarget.memorySize(datatype)
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)
errors.warn("allocating a large value in zeropage; str/array $memsize bytes", position)
else

View File

@ -3,8 +3,6 @@ TODO
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:
sub sprite_y_for_row(ubyte row) -> word {
return (8-row as byte)

View File

@ -1,21 +1,18 @@
%import textio
%zeropage basicsafe
%import floats
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() {
word[3] @shared sprites_x ; = sprites.sprites_x
sprites_x = sprites.sprites_x
word[3] @shared sprites_y ; = sprites.sprites_y
sprites_y = sprites.sprites_y
txt.print_w(sprites.sprites_x[2])
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()
txt.print_ub(arr1[3])
txt.spc()
txt.print_uw(arr2[1])
txt.spc()
floats.print_f(arr3[1])
repeat {
}
}
}