From 16454f55608210af947760d95ec21546247aa3f9 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Tue, 9 Jul 2019 21:59:50 +0200 Subject: [PATCH] optimized when asm --- .../prog8/compiler/target/c64/AsmOptimizer.kt | 33 ++++++++++++++++++- .../prog8/compiler/target/c64/AsmPatterns.kt | 2 +- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/compiler/src/prog8/compiler/target/c64/AsmOptimizer.kt b/compiler/src/prog8/compiler/target/c64/AsmOptimizer.kt index 3fd563aa9..df91b04c0 100644 --- a/compiler/src/prog8/compiler/target/c64/AsmOptimizer.kt +++ b/compiler/src/prog8/compiler/target/c64/AsmOptimizer.kt @@ -32,6 +32,15 @@ fun optimizeAssembly(lines: MutableList): Int { if(removeLines.isNotEmpty()) { for (i in removeLines.reversed()) lines.removeAt(i) + linesByFour = getLinesBy(lines, 4) + numberOfOptimizations++ + } + + removeLines = optimizeCmpSequence(linesByFour) + if(removeLines.isNotEmpty()) { + for (i in removeLines.reversed()) + lines.removeAt(i) + linesByFour = getLinesBy(lines, 4) numberOfOptimizations++ } @@ -40,6 +49,7 @@ fun optimizeAssembly(lines: MutableList): Int { if(removeLines.isNotEmpty()) { for (i in removeLines.reversed()) lines.removeAt(i) + linesByFourteen = getLinesBy(lines, 14) numberOfOptimizations++ } @@ -48,6 +58,27 @@ fun optimizeAssembly(lines: MutableList): Int { return numberOfOptimizations } +fun optimizeCmpSequence(linesByFour: List>>): List { + // the when statement (on bytes) generates a sequence of: + // lda $ce01,x + // cmp #$20 + // beq check_prog8_s72choice_32 + // lda $ce01,x + // cmp #$21 + // beq check_prog8_s73choice_33 + // the repeated lda can be removed + val removeLines = mutableListOf() + for(lines in linesByFour) { + if(lines[0].value.trim()=="lda ${(ESTACK_LO+1).toHex()},x" && + lines[1].value.trim().startsWith("cmp ") && + lines[2].value.trim().startsWith("beq ") && + lines[3].value.trim()=="lda ${(ESTACK_LO+1).toHex()},x") { + removeLines.add(lines[3].index) // remove the second lda + } + } + return removeLines +} + fun optimizeUselessStackByteWrites(linesByFour: List>>): List { // sta on stack, dex, inx, lda from stack -> eliminate this useless stack byte write // this is a lot harder for word values because the instruction sequence varies. @@ -132,7 +163,7 @@ fun optimizeSameAssignments(linesByFourteen: List>>): } private fun getLinesBy(lines: MutableList, windowSize: Int) = -// all lines (that aren't empty or comments) in sliding pairs of 2 +// all lines (that aren't empty or comments) in sliding windows of certain size lines.withIndex().filter { it.value.isNotBlank() && !it.value.trimStart().startsWith(';') }.windowed(windowSize, partialWindows = false) private fun optimizeStoreLoadSame(linesByFour: List>>): List { diff --git a/compiler/src/prog8/compiler/target/c64/AsmPatterns.kt b/compiler/src/prog8/compiler/target/c64/AsmPatterns.kt index 2c1634138..de6a1b801 100644 --- a/compiler/src/prog8/compiler/target/c64/AsmPatterns.kt +++ b/compiler/src/prog8/compiler/target/c64/AsmPatterns.kt @@ -2309,7 +2309,7 @@ internal val patterns = listOf( }, AsmPattern(listOf(Opcode.DUP_B, Opcode.CMP_UB), listOf(Opcode.DUP_B, Opcode.CMP_B)) { segment -> - """ lda ${(ESTACK_LO+1).toHex()},x | cmp #${segment[1].arg!!.integerValue().toHex()} """ + """ lda ${(ESTACK_LO+1).toHex()},x | cmp #${segment[1].arg!!.integerValue().toHex()} """ } )