optimization in assignment to memory

This commit is contained in:
Irmen de Jong 2023-08-04 23:36:51 +02:00
parent 7e58a4c130
commit c36afd872e
3 changed files with 50 additions and 20 deletions

View File

@ -164,24 +164,42 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram,
asmgen.assignExpressionTo(memory.address, AsmAssignTarget(TargetStorageKind.REGISTER, asmgen, DataType.UWORD, memory.definingISub(), target.position, register = RegisterOrPair.AY)) asmgen.assignExpressionTo(memory.address, AsmAssignTarget(TargetStorageKind.REGISTER, asmgen, DataType.UWORD, memory.definingISub(), target.position, register = RegisterOrPair.AY))
asmgen.saveRegisterStack(CpuRegister.A, true) asmgen.saveRegisterStack(CpuRegister.A, true)
asmgen.saveRegisterStack(CpuRegister.Y, false) asmgen.saveRegisterStack(CpuRegister.Y, false)
asmgen.out(" jsr prog8_lib.read_byte_from_address_in_AY | sta P8ZP_SCRATCH_B1") asmgen.out(" jsr prog8_lib.read_byte_from_address_in_AY_into_A")
// TODO optimize the stuff below to not use temp variables??
when(value.kind) { when(value.kind) {
SourceStorageKind.LITERALNUMBER -> inplacemodificationByteVariableWithLiteralval("P8ZP_SCRATCH_B1", DataType.UBYTE, operator, value.number!!.number.toInt()) SourceStorageKind.LITERALNUMBER -> {
SourceStorageKind.VARIABLE -> inplacemodificationByteVariableWithVariable("P8ZP_SCRATCH_B1", DataType.UBYTE, operator, value.asmVarname) inplacemodificationRegisterAwithVariable(operator, "#${value.number!!.number.toInt()}", false)
SourceStorageKind.REGISTER -> inplacemodificationByteVariableWithVariable("P8ZP_SCRATCH_B1", DataType.UBYTE, operator, regName(value)) asmgen.out(" tax")
SourceStorageKind.MEMORY -> inplacemodificationByteVariableWithValue("P8ZP_SCRATCH_B1", DataType.UBYTE, operator, value.memory!!) }
SourceStorageKind.ARRAY -> inplacemodificationByteVariableWithValue("P8ZP_SCRATCH_B1", DataType.UBYTE, operator, value.array!!) SourceStorageKind.VARIABLE -> {
inplacemodificationRegisterAwithVariable(operator, value.asmVarname, false)
asmgen.out(" tax")
}
SourceStorageKind.REGISTER -> {
inplacemodificationRegisterAwithVariable(operator, regName(value), false)
asmgen.out(" tax")
}
SourceStorageKind.MEMORY -> {
asmgen.out(" sta P8ZP_SCRATCH_B1")
inplacemodificationByteVariableWithValue("P8ZP_SCRATCH_B1", DataType.UBYTE, operator, value.memory!!)
asmgen.out(" ldx P8ZP_SCRATCH_B1")
}
SourceStorageKind.ARRAY -> {
asmgen.out(" sta P8ZP_SCRATCH_B1")
inplacemodificationByteVariableWithValue("P8ZP_SCRATCH_B1", DataType.UBYTE, operator, value.array!!)
asmgen.out(" ldx P8ZP_SCRATCH_B1")
}
SourceStorageKind.EXPRESSION -> { SourceStorageKind.EXPRESSION -> {
asmgen.out(" sta P8ZP_SCRATCH_B1")
if(value.expression is PtTypeCast) if(value.expression is PtTypeCast)
inplacemodificationByteVariableWithValue("P8ZP_SCRATCH_B1", DataType.UBYTE, operator, value.expression) inplacemodificationByteVariableWithValue("P8ZP_SCRATCH_B1", DataType.UBYTE, operator, value.expression)
else else
inplacemodificationByteVariableWithValue("P8ZP_SCRATCH_B1", DataType.UBYTE, operator, value.expression!!) inplacemodificationByteVariableWithValue("P8ZP_SCRATCH_B1", DataType.UBYTE, operator, value.expression!!)
asmgen.out(" ldx P8ZP_SCRATCH_B1")
} }
} }
asmgen.restoreRegisterStack(CpuRegister.Y, false) asmgen.restoreRegisterStack(CpuRegister.Y, false)
asmgen.restoreRegisterStack(CpuRegister.A, false) asmgen.restoreRegisterStack(CpuRegister.A, false)
asmgen.out(" ldx P8ZP_SCRATCH_B1 | jsr prog8_lib.write_byte_X_to_address_in_AY") asmgen.out(" jsr prog8_lib.write_byte_X_to_address_in_AY")
} }
} }
} }
@ -310,11 +328,19 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram,
asmgen.out(" lda ${target.array.variable.name}+1,y | sta P8ZP_SCRATCH_W1+1") asmgen.out(" lda ${target.array.variable.name}+1,y | sta P8ZP_SCRATCH_W1+1")
} }
asmgen.saveRegisterStack(CpuRegister.Y, false) asmgen.saveRegisterStack(CpuRegister.Y, false)
// TODO optimize the stuff below to not use temp variables??
when(value.kind) { when(value.kind) {
SourceStorageKind.LITERALNUMBER -> inplacemodificationWordWithLiteralval("P8ZP_SCRATCH_W1", target.datatype, operator, value.number!!.number.toInt()) SourceStorageKind.LITERALNUMBER -> {
SourceStorageKind.VARIABLE -> inplacemodificationWordWithVariable("P8ZP_SCRATCH_W1", target.datatype, operator, value.asmVarname, value.datatype) // TODO optimize the stuff below to not use temp variables??
SourceStorageKind.REGISTER -> inplacemodificationWordWithVariable("P8ZP_SCRATCH_W1", target.datatype, operator, regName(value), value.datatype) inplacemodificationWordWithLiteralval("P8ZP_SCRATCH_W1", target.datatype, operator, value.number!!.number.toInt())
}
SourceStorageKind.VARIABLE -> {
// TODO optimize the stuff below to not use temp variables??
inplacemodificationWordWithVariable("P8ZP_SCRATCH_W1", target.datatype, operator, value.asmVarname, value.datatype)
}
SourceStorageKind.REGISTER -> {
// TODO optimize the stuff below to not use temp variables??
inplacemodificationWordWithVariable("P8ZP_SCRATCH_W1", target.datatype, operator, regName(value), value.datatype)
}
SourceStorageKind.MEMORY -> inplacemodificationWordWithMemread("P8ZP_SCRATCH_W1", target.datatype, operator, value.memory!!) SourceStorageKind.MEMORY -> inplacemodificationWordWithMemread("P8ZP_SCRATCH_W1", target.datatype, operator, value.memory!!)
SourceStorageKind.ARRAY -> inplacemodificationWordWithValue("P8ZP_SCRATCH_W1", target.datatype, operator, value.array!!) SourceStorageKind.ARRAY -> inplacemodificationWordWithValue("P8ZP_SCRATCH_W1", target.datatype, operator, value.array!!)
SourceStorageKind.EXPRESSION -> { SourceStorageKind.EXPRESSION -> {
@ -567,7 +593,6 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram,
asmgen.out(" sta $name") asmgen.out(" sta $name")
} }
// TODO: optimization: use this directly in more places, rather than using a temporary variable
private fun inplacemodificationRegisterAwithVariable(operator: String, variable: String, signed: Boolean) { private fun inplacemodificationRegisterAwithVariable(operator: String, variable: String, signed: Boolean) {
when (operator) { when (operator) {
"+" -> asmgen.out(" clc | adc $variable") "+" -> asmgen.out(" clc | adc $variable")

View File

@ -5,7 +5,7 @@
orig_stackpointer .byte 0 ; stores the Stack pointer register at program start orig_stackpointer .byte 0 ; stores the Stack pointer register at program start
read_byte_from_address_in_AY .proc read_byte_from_address_in_AY_into_A .proc
sta P8ZP_SCRATCH_W2 sta P8ZP_SCRATCH_W2
sty P8ZP_SCRATCH_W2+1 sty P8ZP_SCRATCH_W2+1
ldy #0 ldy #0

View File

@ -3,11 +3,16 @@
main { main {
sub start() { sub start() {
ubyte[10] array uword[4] array = [1,2,3,4]
ubyte idx = 20 ubyte index = 2
idx += 10
ubyte amount = 20 cx16.r0 = 99
array[idx] -= 10 array[index] += 12345
array[idx] -= amount array[index] += cx16.r0
array[index] += index
txt.print_uw(array[index]) ; prints 12449
; code size = $0249
} }
} }