optimize assigning word array value to byte variable

This commit is contained in:
Irmen de Jong 2023-11-04 00:33:50 +01:00
parent 98d2c64d5d
commit 3277544295
3 changed files with 49 additions and 5 deletions

View File

@ -1115,7 +1115,25 @@ internal class BuiltinFunctionsAsmGen(private val program: PtProgram,
else -> throw AssemblyError("invalid reg")
}
} else {
if(arg is PtArrayIndexer && resultRegister in setOf(null, RegisterOrPair.A, RegisterOrPair.Y, RegisterOrPair.X)) {
// just read the lsb byte out of the word array
val arrayVar = if(arg.splitWords) asmgen.asmVariableName(arg.variable)+"_lsb" else asmgen.asmVariableName(arg.variable)
when(resultRegister) {
null, RegisterOrPair.A -> {
asmgen.loadScaledArrayIndexIntoRegister(arg, arg.type, CpuRegister.Y)
asmgen.out(" lda $arrayVar,y")
}
RegisterOrPair.Y -> {
asmgen.loadScaledArrayIndexIntoRegister(arg, arg.type, CpuRegister.X)
asmgen.out(" lda $arrayVar,x")
}
RegisterOrPair.X -> {
asmgen.loadScaledArrayIndexIntoRegister(arg, arg.type, CpuRegister.Y)
asmgen.out(" ldx $arrayVar,y")
}
else -> throw AssemblyError("invalid reg")
}
} else when(resultRegister) {
null, RegisterOrPair.A -> {
asmgen.assignExpressionToRegister(arg, RegisterOrPair.AY)
// NOTE: we rely on the fact that the above assignment to AY, assigns the Lsb to A as the last instruction.

View File

@ -1787,6 +1787,12 @@ internal class AssignmentAsmGen(private val program: PtProgram,
}
}
is PtNumber -> throw AssemblyError("a cast of a literal value should have been const-folded away")
is PtArrayIndexer -> {
if(targetDt in ByteDatatypes && valueDt in WordDatatypes) {
// just assign the lsb from the array value
return assignCastViaLsbFunc(value, target)
}
}
else -> {}
}

View File

@ -1,10 +1,30 @@
%import syslib
%zpreserved $a0,$ff
%zpallowed $70,$b0
%import textio
%zeropage basicsafe
main {
sub start() {
repeat {
}
uword[] wordarray = [0,1,2,3,4,5,6,7,8,9]
ubyte n = 5
n = lsb(wordarray[n])
n = wordarray[n] as ubyte
test(wordarray[n] as ubyte,wordarray[n] as ubyte,0)
test(lsb(wordarray[n]), lsb(wordarray[n]),0)
}
asmsub test(ubyte a1 @A, ubyte a2 @X, ubyte a3 @Y) {
%asm {{
phy
phx
jsr txt.print_ub
jsr txt.spc
pla
jsr txt.print_ub
jsr txt.spc
pla
jsr txt.print_ub
jsr txt.nl
rts
}}
}
}