mirror of
https://github.com/irmen/prog8.git
synced 2024-11-29 17:50:35 +00:00
optimized when asm
This commit is contained in:
parent
c1343a78f1
commit
16454f5560
@ -32,6 +32,15 @@ fun optimizeAssembly(lines: MutableList<String>): Int {
|
|||||||
if(removeLines.isNotEmpty()) {
|
if(removeLines.isNotEmpty()) {
|
||||||
for (i in removeLines.reversed())
|
for (i in removeLines.reversed())
|
||||||
lines.removeAt(i)
|
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++
|
numberOfOptimizations++
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,6 +49,7 @@ fun optimizeAssembly(lines: MutableList<String>): Int {
|
|||||||
if(removeLines.isNotEmpty()) {
|
if(removeLines.isNotEmpty()) {
|
||||||
for (i in removeLines.reversed())
|
for (i in removeLines.reversed())
|
||||||
lines.removeAt(i)
|
lines.removeAt(i)
|
||||||
|
linesByFourteen = getLinesBy(lines, 14)
|
||||||
numberOfOptimizations++
|
numberOfOptimizations++
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,6 +58,27 @@ fun optimizeAssembly(lines: MutableList<String>): Int {
|
|||||||
return numberOfOptimizations
|
return numberOfOptimizations
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun optimizeCmpSequence(linesByFour: List<List<IndexedValue<String>>>): List<Int> {
|
||||||
|
// 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<Int>()
|
||||||
|
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<IndexedValue<String>>>): List<Int> {
|
fun optimizeUselessStackByteWrites(linesByFour: List<List<IndexedValue<String>>>): List<Int> {
|
||||||
// sta on stack, dex, inx, lda from stack -> eliminate this useless stack byte write
|
// 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.
|
// this is a lot harder for word values because the instruction sequence varies.
|
||||||
@ -132,7 +163,7 @@ fun optimizeSameAssignments(linesByFourteen: List<List<IndexedValue<String>>>):
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun getLinesBy(lines: MutableList<String>, windowSize: Int) =
|
private fun getLinesBy(lines: MutableList<String>, 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)
|
lines.withIndex().filter { it.value.isNotBlank() && !it.value.trimStart().startsWith(';') }.windowed(windowSize, partialWindows = false)
|
||||||
|
|
||||||
private fun optimizeStoreLoadSame(linesByFour: List<List<IndexedValue<String>>>): List<Int> {
|
private fun optimizeStoreLoadSame(linesByFour: List<List<IndexedValue<String>>>): List<Int> {
|
||||||
|
@ -2309,7 +2309,7 @@ internal val patterns = listOf<AsmPattern>(
|
|||||||
},
|
},
|
||||||
AsmPattern(listOf(Opcode.DUP_B, Opcode.CMP_UB),
|
AsmPattern(listOf(Opcode.DUP_B, Opcode.CMP_UB),
|
||||||
listOf(Opcode.DUP_B, Opcode.CMP_B)) { segment ->
|
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()} """
|
||||||
}
|
}
|
||||||
|
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user