mirror of
https://github.com/irmen/prog8.git
synced 2024-10-03 05:55:26 +00:00
when working correctly in asm (corrected dup & cmp)
This commit is contained in:
parent
9d0c65c682
commit
c1343a78f1
@ -2,6 +2,10 @@ package prog8.compiler.target.c64
|
|||||||
|
|
||||||
import prog8.compiler.toHex
|
import prog8.compiler.toHex
|
||||||
|
|
||||||
|
|
||||||
|
// note: see https://wiki.nesdev.com/w/index.php/6502_assembly_optimisations
|
||||||
|
|
||||||
|
|
||||||
fun optimizeAssembly(lines: MutableList<String>): Int {
|
fun optimizeAssembly(lines: MutableList<String>): Int {
|
||||||
|
|
||||||
var numberOfOptimizations = 0
|
var numberOfOptimizations = 0
|
||||||
|
@ -5,6 +5,9 @@ import prog8.compiler.intermediate.Instruction
|
|||||||
import prog8.compiler.intermediate.Opcode
|
import prog8.compiler.intermediate.Opcode
|
||||||
import prog8.compiler.toHex
|
import prog8.compiler.toHex
|
||||||
|
|
||||||
|
// note: see https://wiki.nesdev.com/w/index.php/6502_assembly_optimisations
|
||||||
|
|
||||||
|
|
||||||
internal class AsmPattern(val sequence: List<Opcode>, val altSequence: List<Opcode>?=null, val asm: (List<Instruction>)->String?)
|
internal class AsmPattern(val sequence: List<Opcode>, val altSequence: List<Opcode>?=null, val asm: (List<Instruction>)->String?)
|
||||||
|
|
||||||
internal fun loadAFromIndexedByVar(idxVarInstr: Instruction, readArrayInstr: Instruction): String {
|
internal fun loadAFromIndexedByVar(idxVarInstr: Instruction, readArrayInstr: Instruction): String {
|
||||||
@ -2289,6 +2292,24 @@ internal val patterns = listOf<AsmPattern>(
|
|||||||
ldx ${C64Zeropage.SCRATCH_REG_X}
|
ldx ${C64Zeropage.SCRATCH_REG_X}
|
||||||
"""
|
"""
|
||||||
} else null
|
} else null
|
||||||
|
},
|
||||||
|
|
||||||
|
AsmPattern(listOf(Opcode.DUP_W, Opcode.CMP_UW),
|
||||||
|
listOf(Opcode.DUP_W, Opcode.CMP_W)) { segment ->
|
||||||
|
"""
|
||||||
|
lda ${(ESTACK_HI+1).toHex()},x
|
||||||
|
cmp #>${segment[1].arg!!.integerValue().toHex()}
|
||||||
|
bne +
|
||||||
|
lda ${(ESTACK_LO+1).toHex()},x
|
||||||
|
cmp #<${segment[1].arg!!.integerValue().toHex()}
|
||||||
|
; bne + not necessary?
|
||||||
|
; lda #0 not necessary?
|
||||||
|
+
|
||||||
|
"""
|
||||||
|
},
|
||||||
|
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()} """
|
||||||
}
|
}
|
||||||
|
|
||||||
)
|
)
|
||||||
|
@ -10,6 +10,9 @@ import prog8.vm.stackvm.Syscall
|
|||||||
import prog8.vm.stackvm.syscallsForStackVm
|
import prog8.vm.stackvm.syscallsForStackVm
|
||||||
|
|
||||||
|
|
||||||
|
// note: see https://wiki.nesdev.com/w/index.php/6502_assembly_optimisations
|
||||||
|
|
||||||
|
|
||||||
private var breakpointCounter = 0
|
private var breakpointCounter = 0
|
||||||
|
|
||||||
internal fun simpleInstr2Asm(ins: Instruction, block: IntermediateProgram.ProgramBlock): String? {
|
internal fun simpleInstr2Asm(ins: Instruction, block: IntermediateProgram.ProgramBlock): String? {
|
||||||
@ -63,27 +66,26 @@ internal fun simpleInstr2Asm(ins: Instruction, block: IntermediateProgram.Progra
|
|||||||
Opcode.DISCARD_WORD -> " inx"
|
Opcode.DISCARD_WORD -> " inx"
|
||||||
Opcode.DISCARD_FLOAT -> " inx | inx | inx"
|
Opcode.DISCARD_FLOAT -> " inx | inx | inx"
|
||||||
Opcode.DUP_B -> {
|
Opcode.DUP_B -> {
|
||||||
" dex | lda ${(ESTACK_LO+1).toHex()},x | sta ${ESTACK_LO.toHex()},x"
|
" lda ${(ESTACK_LO+1).toHex()},x | sta ${ESTACK_LO.toHex()},x | dex | ;DUP_B "
|
||||||
}
|
}
|
||||||
Opcode.DUP_W -> {
|
Opcode.DUP_W -> {
|
||||||
" dex | lda ${(ESTACK_LO+1).toHex()},x | sta ${ESTACK_LO.toHex()},x | lda ${(ESTACK_HI+1).toHex()},x | sta ${ESTACK_HI.toHex()},x "
|
" lda ${(ESTACK_LO+1).toHex()},x | sta ${ESTACK_LO.toHex()},x | lda ${(ESTACK_HI+1).toHex()},x | sta ${ESTACK_HI.toHex()},x | dex "
|
||||||
}
|
}
|
||||||
|
|
||||||
Opcode.CMP_B, Opcode.CMP_UB -> {
|
Opcode.CMP_B, Opcode.CMP_UB -> {
|
||||||
" inx | lda ${ESTACK_LO.toHex()},x | inx | cmp ${ESTACK_LO.toHex()},x "
|
" inx | lda ${ESTACK_LO.toHex()},x | cmp #${ins.arg!!.integerValue().toHex()} | ;CMP_B "
|
||||||
}
|
}
|
||||||
|
|
||||||
Opcode.CMP_W, Opcode.CMP_UW -> {
|
Opcode.CMP_W, Opcode.CMP_UW -> {
|
||||||
"""
|
"""
|
||||||
inx
|
inx
|
||||||
inx
|
lda ${ESTACK_HI.toHex()},x
|
||||||
lda ${(ESTACK_HI-1).toHex()},x
|
cmp #>${ins.arg!!.integerValue().toHex()}
|
||||||
cmp ${(ESTACK_HI).toHex()},x
|
|
||||||
bne +
|
bne +
|
||||||
lda ${(ESTACK_LO-1).toHex()},x
|
lda ${ESTACK_LO.toHex()},x
|
||||||
cmp ${(ESTACK_LO).toHex()},x
|
cmp #<${ins.arg!!.integerValue().toHex()}
|
||||||
bne +
|
; bne + not necessary?
|
||||||
lda #0
|
; lda #0 not necessary?
|
||||||
+
|
+
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
|
@ -18,13 +18,32 @@
|
|||||||
c64scr.print_ub(aa+yy)
|
c64scr.print_ub(aa+yy)
|
||||||
c64scr.print("?: ")
|
c64scr.print("?: ")
|
||||||
check(aa, yy)
|
check(aa, yy)
|
||||||
aa+=9
|
aa++
|
||||||
|
c64scr.print_ub(aa+yy)
|
||||||
|
c64scr.print("?: ")
|
||||||
|
check(aa, yy)
|
||||||
|
aa++
|
||||||
c64scr.print_ub(aa+yy)
|
c64scr.print_ub(aa+yy)
|
||||||
c64scr.print("?: ")
|
c64scr.print("?: ")
|
||||||
check(aa, yy)
|
check(aa, yy)
|
||||||
|
|
||||||
c64scr.print_uw(uw)
|
c64scr.print_uw(uw)
|
||||||
c64scr.print("?: ")
|
c64scr.print("?: ")
|
||||||
|
checkuw(uw)
|
||||||
|
uw++
|
||||||
|
c64scr.print_uw(uw)
|
||||||
|
c64scr.print("?: ")
|
||||||
|
checkuw(uw)
|
||||||
|
uw++
|
||||||
|
c64scr.print_uw(uw)
|
||||||
|
c64scr.print("?: ")
|
||||||
|
checkuw(uw)
|
||||||
|
|
||||||
|
c64scr.print("stack (255?): ")
|
||||||
|
c64scr.print_ub(X)
|
||||||
|
}
|
||||||
|
|
||||||
|
sub checkuw(uword uw) {
|
||||||
when uw {
|
when uw {
|
||||||
12345 -> c64scr.print("12345")
|
12345 -> c64scr.print("12345")
|
||||||
12346 -> c64scr.print("12346")
|
12346 -> c64scr.print("12346")
|
||||||
@ -34,9 +53,6 @@
|
|||||||
else -> c64scr.print("not in table")
|
else -> c64scr.print("not in table")
|
||||||
}
|
}
|
||||||
c64.CHROUT('\n')
|
c64.CHROUT('\n')
|
||||||
|
|
||||||
c64scr.print("stack (255?): ")
|
|
||||||
c64scr.print_ub(X)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sub check(ubyte a, ubyte y) {
|
sub check(ubyte a, ubyte y) {
|
||||||
|
Loading…
Reference in New Issue
Block a user