From 0d7b14e2d891b1b0d9084b8d7ab69675024a2b27 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 23 Oct 2022 11:57:23 +0200 Subject: [PATCH] fix crash when assigning certain memory read to word variable. Fixes #82 --- .../cpu6502/assignment/AssignmentAsmGen.kt | 19 ++++++------------- compiler/test/TestTypecasts.kt | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt index 96a8ae60e..45a87c6f5 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt @@ -852,7 +852,8 @@ internal class AssignmentAsmGen(private val program: Program, fun assignViaExprEval(addressExpression: Expression) { asmgen.assignExpressionToVariable(addressExpression, "P8ZP_SCRATCH_W2", DataType.UWORD, null) asmgen.loadAFromZpPointerVar("P8ZP_SCRATCH_W2") - assignRegisterByte(target, CpuRegister.A) + asmgen.out(" ldy #0") + assignRegisterpairWord(target, RegisterOrPair.AY) } when (value.addressExpression) { @@ -1972,18 +1973,10 @@ internal class AssignmentAsmGen(private val program: Program, } internal fun assignRegisterByte(target: AsmAssignTarget, register: CpuRegister) { - // we make an exception in the type check for assigning something to a cx16 virtual register, or a register pair - // these will be correctly typecasted from a byte to a word value - if(target.register !in Cx16VirtualRegisters && - target.register!=RegisterOrPair.AX && target.register!=RegisterOrPair.AY && target.register!=RegisterOrPair.XY) { - if(target.kind== TargetStorageKind.VARIABLE) { - val parts = target.asmVarname.split('.') - if (parts.size != 2 || parts[0] != "cx16") - require(target.datatype in ByteDatatypes) - } else { - require(target.datatype in ByteDatatypes) - } - } + // we make an exception in the type check for assigning something to a register pair AX, AY or XY + // these will be correctly typecasted from a byte to a word value here + if(target.register !in setOf(RegisterOrPair.AX, RegisterOrPair.AY, RegisterOrPair.XY)) + require(target.datatype in ByteDatatypes) when(target.kind) { TargetStorageKind.VARIABLE -> { diff --git a/compiler/test/TestTypecasts.kt b/compiler/test/TestTypecasts.kt index 939a55e6b..32d8ccc42 100644 --- a/compiler/test/TestTypecasts.kt +++ b/compiler/test/TestTypecasts.kt @@ -919,4 +919,19 @@ main { }""" compileText(C64Target(), true, text, writeAssembly = true) shouldNotBe null } + + test("memory reads byte into word variable") { + val text = """ + main { + sub start() { + uword @shared ww + uword address = $1000 + ww = @(address+100) + ww = @(address+1000) + cx16.r0 = @(address+100) + cx16.r0 = @(address+1000) + } + }""" + compileText(C64Target(), false, text, writeAssembly = true) shouldNotBe null + } })