From 2ceaa25181c865cd0b44ba45ef978b74a74ab6df Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 29 Oct 2023 23:41:34 +0100 Subject: [PATCH] optimized code for (infrequently used) logical operations on word array --- .../assignment/AugmentableAssignmentAsmGen.kt | 72 +++++++++++++++++-- examples/test.p8 | 22 ++++-- 2 files changed, 86 insertions(+), 8 deletions(-) diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AugmentableAssignmentAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AugmentableAssignmentAsmGen.kt index fbeadb3cc..c0bfed34a 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AugmentableAssignmentAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AugmentableAssignmentAsmGen.kt @@ -457,7 +457,6 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram, } private fun inplacemodificationRegisterAXwithVariable(operator: String, variable: String, varDt: DataType): Boolean { - // note: we only optimize addition and subtraction, and these are the same for unsigned or signed. when(operator) { "+" -> { return if(varDt in WordDatatypes) { @@ -507,12 +506,44 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram, true } } - else -> return false // TODO optimize more operators, such as the bitwise logical ones? Might need to know if signed + "|" -> { + asmgen.out(""" + ora $variable + tay + txa + ora $variable+1 + tax + tya + """) + return true + } + "&" -> { + asmgen.out(""" + and $variable + tay + txa + and $variable+1 + tax + tya + """) + return true + } + "^" -> { + asmgen.out(""" + eor $variable + tay + txa + eor $variable+1 + tax + tya + """) + return true + } + else -> return false } } private fun inplacemodificationRegisterAXwithLiteralval(operator: String, number: Int): Boolean { - // note: we only optimize addition and subtraction, and these are the same for unsigned or signed. when(operator) { "+" -> { return if(number in -128..255) { @@ -556,7 +587,40 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram, true } } - else -> return false // TODO optimize more operators, such as the bitwise logical ones? Might need to know if signed + "|" -> { + asmgen.out(""" + ora #<$number + tay + txa + ora #>$number + tax + tya + """) + return true + } + "&" -> { + asmgen.out(""" + and #<$number + tay + txa + and #>$number + tax + tya + """) + return true + } + "^" -> { + asmgen.out(""" + eor #<$number + tay + txa + eor #>$number + tax + tya + """) + return true + } + else -> return false } } diff --git a/examples/test.p8 b/examples/test.p8 index b9f18a3c6..39318bd5d 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -5,10 +5,24 @@ main { sub start() { - ubyte lower = 123 - ubyte upper = 0 - uword ww = mkword(upper, lower) + uword[] array = [$1010,$2020,$3030,$4040,$5050] + ubyte index = 2 + uword value = $0205 - txt.print_uwhex(ww, true) + array[index] ^= $0205 + txt.print_uwhex(array[2], true) + txt.nl() + + array[index]+=9 + + txt.print_uwhex(array[2], true) + txt.nl() + + array[index] = $3030 + array[index] |= value + txt.print_uwhex(array[2], true) + txt.nl() + + ; TODO met var array[index]|=index } }