From 1815cb1bc38db52fe50aa95e0b4c2ee4b571f931 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Fri, 28 Jan 2022 15:19:08 +0100 Subject: [PATCH] fixed bug in assembly optimizer removing too many instructions --- .../src/prog8/codegen/cpu6502/AsmOptimizer.kt | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmOptimizer.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmOptimizer.kt index 9a313ceaf..d4c6e3cc4 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmOptimizer.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmOptimizer.kt @@ -64,6 +64,10 @@ fun optimizeAssembly(lines: MutableList, machine: IMachineDefinition, pr return numberOfOptimizations } +private fun String.isBranch() = this.startsWith("b") +private fun String.isStoreReg() = this.startsWith("sta") || this.startsWith("sty") || this.startsWith("stx") +private fun String.isLoadReg() = this.startsWith("lda") || this.startsWith("ldy") || this.startsWith("ldx") + private class Modification(val lineIndex: Int, val remove: Boolean, val replacement: String?) private fun apply(modifications: List, lines: MutableList) { @@ -196,9 +200,9 @@ private fun optimizeSameAssignments(linesByFourteen: List4) { val firstvalue = first.substring(4) val secondvalue = second.substring(4) @@ -296,7 +300,7 @@ private fun optimizeSameAssignments(linesByFourteen: List>>, ) { val third = lines[3].value.trimStart() val attemptRemove = - if(third.startsWith("b")) { + if(third.isBranch()) { // a branch instruction follows, we can only remove the load instruction if // another load instruction of the same register precedes the store instruction // (otherwise wrong cpu flags are used)