optimization in assignment to array

This commit is contained in:
Irmen de Jong 2023-08-04 23:05:33 +02:00
parent 19a4bf1088
commit 7e58a4c130
2 changed files with 48 additions and 99 deletions

View File

@ -103,41 +103,17 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram,
} }
DataType.FLOAT -> { DataType.FLOAT -> {
when(value.kind) { when(value.kind) {
SourceStorageKind.LITERALNUMBER -> inplacemodificationFloatWithLiteralval( SourceStorageKind.LITERALNUMBER -> inplacemodificationFloatWithLiteralval(target.asmVarname, operator, value.number!!.number)
target.asmVarname, SourceStorageKind.VARIABLE -> inplacemodificationFloatWithVariable(target.asmVarname, operator, value.asmVarname)
operator, SourceStorageKind.REGISTER -> inplacemodificationFloatWithVariable(target.asmVarname, operator, regName(value))
value.number!!.number
)
SourceStorageKind.VARIABLE -> inplacemodificationFloatWithVariable(
target.asmVarname,
operator,
value.asmVarname
)
SourceStorageKind.REGISTER -> inplacemodificationFloatWithVariable(
target.asmVarname,
operator,
regName(value)
)
SourceStorageKind.MEMORY -> TODO("memread into float") SourceStorageKind.MEMORY -> TODO("memread into float")
SourceStorageKind.ARRAY -> inplacemodificationFloatWithValue( SourceStorageKind.ARRAY -> inplacemodificationFloatWithValue(target.asmVarname, operator, value.array!!)
target.asmVarname,
operator,
value.array!!
)
SourceStorageKind.EXPRESSION -> { SourceStorageKind.EXPRESSION -> {
if(value.expression is PtTypeCast) { if(value.expression is PtTypeCast) {
if (tryInplaceModifyWithRemovedRedundantCast(value.expression, target, operator)) return if (tryInplaceModifyWithRemovedRedundantCast(value.expression, target, operator)) return
inplacemodificationFloatWithValue( inplacemodificationFloatWithValue(target.asmVarname, operator, value.expression)
target.asmVarname,
operator,
value.expression
)
} else { } else {
inplacemodificationFloatWithValue( inplacemodificationFloatWithValue(target.asmVarname, operator, value.expression!!)
target.asmVarname,
operator,
value.expression!!
)
} }
} }
} }
@ -255,41 +231,17 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram,
} }
DataType.FLOAT -> { DataType.FLOAT -> {
when(value.kind) { when(value.kind) {
SourceStorageKind.LITERALNUMBER -> inplacemodificationFloatWithLiteralval( SourceStorageKind.LITERALNUMBER -> inplacemodificationFloatWithLiteralval(targetVarName, operator, value.number!!.number)
targetVarName, SourceStorageKind.VARIABLE -> inplacemodificationFloatWithVariable(targetVarName, operator, value.asmVarname)
operator, SourceStorageKind.REGISTER -> inplacemodificationFloatWithVariable(targetVarName, operator, regName(value))
value.number!!.number
)
SourceStorageKind.VARIABLE -> inplacemodificationFloatWithVariable(
targetVarName,
operator,
value.asmVarname
)
SourceStorageKind.REGISTER -> inplacemodificationFloatWithVariable(
targetVarName,
operator,
regName(value)
)
SourceStorageKind.MEMORY -> TODO("memread into float array") SourceStorageKind.MEMORY -> TODO("memread into float array")
SourceStorageKind.ARRAY -> inplacemodificationFloatWithValue( SourceStorageKind.ARRAY -> inplacemodificationFloatWithValue(targetVarName, operator, value.array!!)
targetVarName,
operator,
value.array!!
)
SourceStorageKind.EXPRESSION -> { SourceStorageKind.EXPRESSION -> {
if(value.expression is PtTypeCast) { if(value.expression is PtTypeCast) {
if (tryInplaceModifyWithRemovedRedundantCast(value.expression, target, operator)) return if (tryInplaceModifyWithRemovedRedundantCast(value.expression, target, operator)) return
inplacemodificationFloatWithValue( inplacemodificationFloatWithValue(targetVarName, operator, value.expression)
targetVarName,
operator,
value.expression
)
} else { } else {
inplacemodificationFloatWithValue( inplacemodificationFloatWithValue(targetVarName, operator, value.expression!!)
targetVarName,
operator,
value.expression!!
)
} }
} }
} }
@ -305,24 +257,44 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram,
&& tryInplaceModifyWithRemovedRedundantCast(value.expression, target, operator)) && tryInplaceModifyWithRemovedRedundantCast(value.expression, target, operator))
return return
asmgen.loadScaledArrayIndexIntoRegister(target.array, DataType.UBYTE, CpuRegister.Y) asmgen.loadScaledArrayIndexIntoRegister(target.array, DataType.UBYTE, CpuRegister.Y)
asmgen.out(" lda ${target.array.variable.name},y | sta P8ZP_SCRATCH_B1")
asmgen.saveRegisterStack(CpuRegister.Y, false) asmgen.saveRegisterStack(CpuRegister.Y, false)
// TODO optimize the stuff below to not use temp variables?? asmgen.out(" lda ${target.array.variable.name},y")
when(value.kind) { when(value.kind) {
SourceStorageKind.LITERALNUMBER -> inplacemodificationByteVariableWithLiteralval("P8ZP_SCRATCH_B1", target.datatype, operator, value.number!!.number.toInt()) SourceStorageKind.LITERALNUMBER -> {
SourceStorageKind.VARIABLE -> inplacemodificationByteVariableWithVariable("P8ZP_SCRATCH_B1", target.datatype, operator, value.asmVarname) inplacemodificationRegisterAwithVariable(operator, "#${value.number!!.number.toInt()}", target.datatype in SignedDatatypes)
SourceStorageKind.REGISTER -> inplacemodificationByteVariableWithVariable("P8ZP_SCRATCH_B1", target.datatype, operator, regName(value)) asmgen.restoreRegisterStack(CpuRegister.Y, true)
SourceStorageKind.MEMORY -> inplacemodificationByteVariableWithValue("P8ZP_SCRATCH_B1", target.datatype, operator, value.memory!!) }
SourceStorageKind.ARRAY -> inplacemodificationByteVariableWithValue("P8ZP_SCRATCH_B1", target.datatype, operator, value.array!!) SourceStorageKind.VARIABLE -> {
inplacemodificationRegisterAwithVariable(operator, value.asmVarname, target.datatype in SignedDatatypes)
asmgen.restoreRegisterStack(CpuRegister.Y, true)
}
SourceStorageKind.REGISTER -> {
inplacemodificationRegisterAwithVariable(operator, regName(value), target.datatype in SignedDatatypes)
asmgen.restoreRegisterStack(CpuRegister.Y, true)
}
SourceStorageKind.MEMORY -> {
asmgen.out(" sta P8ZP_SCRATCH_B1")
inplacemodificationByteVariableWithValue("P8ZP_SCRATCH_B1", target.datatype, operator, value.memory!!)
asmgen.restoreRegisterStack(CpuRegister.Y, false)
asmgen.out(" lda P8ZP_SCRATCH_B1")
}
SourceStorageKind.ARRAY -> {
asmgen.out(" sta P8ZP_SCRATCH_B1")
inplacemodificationByteVariableWithValue("P8ZP_SCRATCH_B1", target.datatype, operator, value.array!!)
asmgen.restoreRegisterStack(CpuRegister.Y, false)
asmgen.out(" lda 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", target.datatype, operator, value.expression) inplacemodificationByteVariableWithValue("P8ZP_SCRATCH_B1", target.datatype, operator, value.expression)
else else
inplacemodificationByteVariableWithValue("P8ZP_SCRATCH_B1", target.datatype, operator, value.expression!!) inplacemodificationByteVariableWithValue("P8ZP_SCRATCH_B1", target.datatype, operator, value.expression!!)
asmgen.restoreRegisterStack(CpuRegister.Y, false)
asmgen.out(" lda P8ZP_SCRATCH_B1")
} }
} }
asmgen.restoreRegisterStack(CpuRegister.Y, false) asmgen.out(" sta ${target.array.variable.name},y")
asmgen.out(" lda P8ZP_SCRATCH_B1 | sta ${target.array.variable.name},y")
} }
in WordDatatypes -> { in WordDatatypes -> {
if(value.kind==SourceStorageKind.EXPRESSION if(value.kind==SourceStorageKind.EXPRESSION
@ -376,42 +348,18 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram,
ldy #>$tempvar ldy #>$tempvar
jsr floats.copy_float""") // copy from array into float temp var, clobbers A,Y jsr floats.copy_float""") // copy from array into float temp var, clobbers A,Y
when(value.kind) { when(value.kind) {
SourceStorageKind.LITERALNUMBER -> inplacemodificationFloatWithLiteralval( SourceStorageKind.LITERALNUMBER -> inplacemodificationFloatWithLiteralval(tempvar, operator, value.number!!.number)
tempvar, SourceStorageKind.VARIABLE -> inplacemodificationFloatWithVariable(tempvar, operator, value.asmVarname)
operator, SourceStorageKind.REGISTER -> inplacemodificationFloatWithVariable(tempvar, operator, regName(value))
value.number!!.number
)
SourceStorageKind.VARIABLE -> inplacemodificationFloatWithVariable(
tempvar,
operator,
value.asmVarname
)
SourceStorageKind.REGISTER -> inplacemodificationFloatWithVariable(
tempvar,
operator,
regName(value)
)
SourceStorageKind.MEMORY -> TODO("memread into float") SourceStorageKind.MEMORY -> TODO("memread into float")
SourceStorageKind.ARRAY -> inplacemodificationFloatWithValue( SourceStorageKind.ARRAY -> inplacemodificationFloatWithValue(tempvar, operator, value.array!!)
tempvar,
operator,
value.array!!
)
SourceStorageKind.EXPRESSION -> { SourceStorageKind.EXPRESSION -> {
if(value.expression is PtTypeCast) { if(value.expression is PtTypeCast) {
if (tryInplaceModifyWithRemovedRedundantCast(value.expression, target, operator)) if (tryInplaceModifyWithRemovedRedundantCast(value.expression, target, operator))
return return
inplacemodificationFloatWithValue( inplacemodificationFloatWithValue(tempvar, operator, value.expression)
tempvar,
operator,
value.expression
)
} else { } else {
inplacemodificationFloatWithValue( inplacemodificationFloatWithValue(tempvar, operator, value.expression!!)
tempvar,
operator,
value.expression!!
)
} }
} }
} }

View File

@ -7,6 +7,7 @@ main {
ubyte idx = 20 ubyte idx = 20
idx += 10 idx += 10
ubyte amount = 20 ubyte amount = 20
array[idx] -= 10
array[idx] -= amount array[idx] -= amount
} }
} }