From 298b25cf7d4ecdbe35a6599fa8ccb5910aa291ad Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Wed, 6 Jul 2022 00:28:43 +0200 Subject: [PATCH] fix compiler crash on certain typecasting assignment --- .../cpu6502/assignment/AssignmentAsmGen.kt | 14 +++++++++++++ compiler/test/TestTypecasts.kt | 20 +++++++++++++++++++ docs/source/todo.rst | 1 - 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt index 6c30eabfc..a18f7bd8d 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt @@ -791,6 +791,20 @@ internal class AssignmentAsmGen(private val program: Program, assignTypeCastedFloatFAC1("P8ZP_SCRATCH_W1", target.datatype) assignVariableToRegister("P8ZP_SCRATCH_W1", target.register!!, target.datatype in SignedDatatypes) } else { + if(!(valueDt isAssignableTo targetDt)) { + if(valueDt in WordDatatypes && targetDt in ByteDatatypes) { + // word to byte, just take the lsb + return assignCastViaLsbFunc(value, target) + } else if(valueDt in WordDatatypes && targetDt in WordDatatypes) { + // word to word, just assign + assignExpressionToRegister(value, target.register!!, targetDt==DataType.BYTE || targetDt==DataType.WORD) + } else if(valueDt in ByteDatatypes && targetDt in ByteDatatypes) { + // byte to byte, just assign + assignExpressionToRegister(value, target.register!!, targetDt==DataType.BYTE || targetDt==DataType.WORD) + } + else + throw AssemblyError("can't cast $valueDt to $targetDt, this should have been checked in the astchecker") + } assignExpressionToRegister(value, target.register!!, targetDt==DataType.BYTE || targetDt==DataType.WORD) } return diff --git a/compiler/test/TestTypecasts.kt b/compiler/test/TestTypecasts.kt index 67bef934a..156a04bfe 100644 --- a/compiler/test/TestTypecasts.kt +++ b/compiler/test/TestTypecasts.kt @@ -16,6 +16,26 @@ import prog8tests.helpers.compileText class TestTypecasts: FunSpec({ + test("word to byte casts") { + val text=""" + %import textio + main { + sub func(ubyte arg) -> word { + return arg-99 + } + + sub start() { + txt.print_ub(func(0) as ubyte) + txt.print_uw(func(0) as ubyte) + txt.print_w(func(0) as ubyte) + } + }""" + val result = compileText(C64Target(), false, text, writeAssembly = false)!! + val stmts = result.program.entrypoint.statements + stmts.size shouldBe 3 + + } + test("add missing & to function arguments") { val text=""" main { diff --git a/docs/source/todo.rst b/docs/source/todo.rst index c62b28708..0d85fa708 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,7 +3,6 @@ TODO For next release ^^^^^^^^^^^^^^^^ -- fix compiler crash txt.print_w(value(99) as ubyte) where value()->word - fix imageviewer compilation crash - petaxian is larger again after introduction of BOOL type (against main branch). WHY??? FIX.