From 5d7ddebcad3f930a997d20caa28da4a87f11d480 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Wed, 10 Apr 2024 22:33:22 +0200 Subject: [PATCH] fix bool to uword cast in 6502 codegen --- .../cpu6502/assignment/AssignmentAsmGen.kt | 6 +-- compiler/test/TestTypecasts.kt | 15 +++++- compiler/test/ast/TestVariousCompilerAst.kt | 22 ++++++++ docs/source/todo.rst | 2 + examples/test.p8 | 53 +++++-------------- 5 files changed, 53 insertions(+), 45 deletions(-) diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt index 9424b564c..e5d3e3136 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt @@ -1804,8 +1804,8 @@ internal class AssignmentAsmGen(private val program: PtProgram, when (valueDt) { DataType.BOOL -> { - if(targetDt in ByteDatatypes) { - // optimization to assign boolean expression to byte target (just assign the 0 or 1 directly, no cast needed) + if(targetDt in IntegerDatatypes) { + // optimization to assign boolean expression to integer target (just assign the 0 or 1 directly) val assignDirect = AsmAssignment( AsmAssignSource.fromAstSource(value, program, asmgen), target, @@ -1814,7 +1814,7 @@ internal class AssignmentAsmGen(private val program: PtProgram, ) assignExpression(assignDirect, target.scope) } else { - throw AssemblyError("expected bool or byte target type") + TODO("assign bool to non-integer type $targetDt") } } in ByteDatatypes -> { diff --git a/compiler/test/TestTypecasts.kt b/compiler/test/TestTypecasts.kt index 0a185ee0d..3fbb3e0bc 100644 --- a/compiler/test/TestTypecasts.kt +++ b/compiler/test/TestTypecasts.kt @@ -11,7 +11,6 @@ import prog8.ast.expressions.* import prog8.ast.printProgram import prog8.ast.statements.Assignment import prog8.ast.statements.IfElse -import prog8.ast.statements.VarDecl import prog8.code.ast.PtAsmSub import prog8.code.ast.PtSub import prog8.code.core.DataType @@ -826,4 +825,18 @@ main { }""" compileText(C64Target(), true, src, writeAssembly = true) shouldNotBe null } + + test("bool to word cast") { + val src=""" +main { + sub start() { + bool @shared flag, flag2 + cx16.r0L = (flag and flag2) as ubyte + cx16.r0 = (flag and flag2) as uword + } +}""" + + compileText(VMTarget(), false, src, writeAssembly = true) shouldNotBe null + compileText(C64Target(), false, src, writeAssembly = true) shouldNotBe null + } }) diff --git a/compiler/test/ast/TestVariousCompilerAst.kt b/compiler/test/ast/TestVariousCompilerAst.kt index dfdd370c3..d15bef564 100644 --- a/compiler/test/ast/TestVariousCompilerAst.kt +++ b/compiler/test/ast/TestVariousCompilerAst.kt @@ -590,5 +590,27 @@ main { val valT = (st[17] as Assignment).value (valT as IdentifierReference).nameInSource shouldBe listOf("r") } + + test("various multi var decl symbol lookups") { + val src=""" +main { + sub start() { + uword @shared a,b + b = a ; works + cx16.r1L = lsb(a) ; works + funcw(a) ; works + funcb(lsb(a)) ; fails :-( TODO FIX + } + + sub funcw(uword arg) { + arg++ + } + + sub funcb(ubyte arg) { + arg++ + } +}""" + compileText(Cx16Target(), false, src) shouldNotBe null + } }) diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 0c203eb06..b6b89d0eb 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -1,6 +1,8 @@ TODO ==== +fix the symbol lookup error lsb(a) when a is in a multi vardecl. + ... diff --git a/examples/test.p8 b/examples/test.p8 index 4a23d8142..bfc191859 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,49 +1,20 @@ -%import textio %zeropage basicsafe -%option no_sysinit +%import textio main { - ubyte[255] array1 - ubyte[255] array2 - uword block1 = memory("b1", 6000 ,0) - uword block2 = memory("b2", 6000 ,0) - sub start() { - cbm.SETTIM(0,0,0) + uword @shared a,b + b = a ; works + cx16.r1L = lsb(a) ; works + funcw(a) ; works + funcb(lsb(a)) ; fails :-( + } - repeat 2000 { - sys.memcopy(array1, array2, sizeof(array1)) - } + sub funcw(uword arg) { + arg++ + } - txt.print_uw(cbm.RDTIM16()) - txt.nl() - - cbm.SETTIM(0,0,0) - - repeat 2000 { - cx16.memory_copy(array1, array2, sizeof(array1)) - } - - txt.print_uw(cbm.RDTIM16()) - txt.nl() - - cbm.SETTIM(0,0,0) - - repeat 100 { - sys.memcopy(block1, block2, 6000) - } - - txt.print_uw(cbm.RDTIM16()) - txt.nl() - - cbm.SETTIM(0,0,0) - - repeat 100 { - cx16.memory_copy(block1, block2, 6000) - } - - txt.print_uw(cbm.RDTIM16()) - txt.nl() + sub funcb(ubyte arg) { + arg++ } } -