optimized code for (infrequently used) logical operations on word array

This commit is contained in:
Irmen de Jong 2023-10-29 23:41:34 +01:00
parent 513611c5a6
commit 2ceaa25181
2 changed files with 86 additions and 8 deletions

View File

@ -457,7 +457,6 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram,
} }
private fun inplacemodificationRegisterAXwithVariable(operator: String, variable: String, varDt: DataType): Boolean { 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) { when(operator) {
"+" -> { "+" -> {
return if(varDt in WordDatatypes) { return if(varDt in WordDatatypes) {
@ -507,12 +506,44 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram,
true 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 { 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) { when(operator) {
"+" -> { "+" -> {
return if(number in -128..255) { return if(number in -128..255) {
@ -556,7 +587,40 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram,
true 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
} }
} }

View File

@ -5,10 +5,24 @@
main { main {
sub start() { sub start() {
ubyte lower = 123 uword[] array = [$1010,$2020,$3030,$4040,$5050]
ubyte upper = 0 ubyte index = 2
uword ww = mkword(upper, lower) 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
} }
} }