From 1e053783f35b15efcf9772b163dd93ace3730b69 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 23 Jan 2022 02:42:26 +0100 Subject: [PATCH] fix invalid size copied when assigning non-byte arrays --- .../codegen/target/cpu6502/codegen/AsmGen.kt | 4 +-- .../astprocessing/StatementReorderer.kt | 4 ++- docs/source/todo.rst | 7 ++--- examples/test.p8 | 26 ++++++++++++------- 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/codeGeneration/src/prog8/codegen/target/cpu6502/codegen/AsmGen.kt b/codeGeneration/src/prog8/codegen/target/cpu6502/codegen/AsmGen.kt index a149e6093..b95259935 100644 --- a/codeGeneration/src/prog8/codegen/target/cpu6502/codegen/AsmGen.kt +++ b/codeGeneration/src/prog8/codegen/target/cpu6502/codegen/AsmGen.kt @@ -129,7 +129,7 @@ class AsmGen(private val program: Program, (vardecl.value as StringLiteralValue).value.length } in ArrayDatatypes -> { - vardecl.arraysize!!.constIndex() + vardecl.arraysize!!.constIndex() // TODO wrong size for non-byte arrays??????? or does datatype get taken into account below } else -> null } @@ -1193,7 +1193,7 @@ class AsmGen(private val program: Program, jsr prog8_lib.strcpy""") } arrayVarsInZp.forEach { - val numelements = (it.value as ArrayLiteralValue).value.size + val numelements = (it.value as ArrayLiteralValue).value.size // TODO wrong size for word/float arrays!??? out(""" lda #<${it.name}_init_value ldy #>${it.name}_init_value diff --git a/compiler/src/prog8/compiler/astprocessing/StatementReorderer.kt b/compiler/src/prog8/compiler/astprocessing/StatementReorderer.kt index 3c8a88211..0f7a75886 100644 --- a/compiler/src/prog8/compiler/astprocessing/StatementReorderer.kt +++ b/compiler/src/prog8/compiler/astprocessing/StatementReorderer.kt @@ -370,11 +370,13 @@ internal class StatementReorderer(val program: Program, if(!errors.noErrors()) return noModifications + val numelements = targetVar.arraysize!!.constIndex()!! + val eltsize = program.memsizer.memorySize(ArrayToElementTypes.getValue(sourceVar.datatype)) val memcopy = FunctionCallStatement(IdentifierReference(listOf("sys", "memcopy"), assign.position), mutableListOf( AddressOf(sourceIdent, assign.position), AddressOf(identifier, assign.position), - NumericLiteralValue.optimalInteger(targetVar.arraysize!!.constIndex()!!, assign.position) + NumericLiteralValue.optimalInteger(numelements*eltsize, assign.position) ), true, assign.position diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 53c792054..1d7178336 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,6 +3,8 @@ 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) @@ -10,11 +12,6 @@ For next release - fix crash: word[33] sprites_x = sprites.sprites_x word[33] sprites_y = sprites.sprites_y -- fix assignment code generated for: (memcopying manuall does work correctly) - word[33] sprites_x - word[33] sprites_y - sprites_x = sprites.sprites_x - sprites_y = sprites.sprites_y - Fix: better error message for len() in: ubyte[64] chessboard sub init() { diff --git a/examples/test.p8 b/examples/test.p8 index 22fe9c3d9..3fb8f7940 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -3,15 +3,23 @@ main { 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 -label: - uword @shared addr - addr = label - addr = thing - addr = &label - addr = &thing - } - - sub thing() { + 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() } } + +sprites { + word[3] sprites_x = [111,222,333] + word[3] sprites_y = [666,777,888] +}