From 8cbfe64f197270c102ef1cb30181cc77ffade4e1 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 7 May 2023 21:03:14 +0200 Subject: [PATCH] optimize some carry flag assembly --- .../cpu6502/assignment/AssignmentAsmGen.kt | 19 ++++---- .../assignment/AugmentableAssignmentAsmGen.kt | 8 ++++ compiler/res/prog8lib/c128/syslib.p8 | 4 +- compiler/res/prog8lib/c64/syslib.p8 | 4 +- compiler/res/prog8lib/cx16/syslib.p8 | 2 +- compiler/res/prog8lib/string.p8 | 2 +- docs/source/todo.rst | 3 +- examples/test.p8 | 43 ++++++++----------- 8 files changed, 42 insertions(+), 43 deletions(-) diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt index 4df9f6812..a97817c8b 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AssignmentAsmGen.kt @@ -740,12 +740,11 @@ internal class AssignmentAsmGen(private val program: PtProgram, +""") else asmgen.out(""" - cmp P8ZP_SCRATCH_B1 + tay + lda #0 + cpy P8ZP_SCRATCH_B1 beq + - bcs ++ -+ lda #0 - beq ++ -+ lda #1 + rol a +""") } "<=" -> { @@ -783,11 +782,9 @@ internal class AssignmentAsmGen(private val program: PtProgram, else asmgen.out(""" cmp P8ZP_SCRATCH_B1 - bcc + lda #0 - beq ++ -+ lda #1 -+""") + rol a + eor #1""") } ">=" -> { assignExpressionOperandsLeftScratchRightA() @@ -803,10 +800,10 @@ internal class AssignmentAsmGen(private val program: PtProgram, + lda #1 +""") else - asmgen.out(""" + asmgen.out(""" cmp P8ZP_SCRATCH_B1 - bcc + beq + + bcc + lda #0 beq ++ + lda #1 diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AugmentableAssignmentAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AugmentableAssignmentAsmGen.kt index e7284e706..ab1b61231 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AugmentableAssignmentAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AugmentableAssignmentAsmGen.kt @@ -771,6 +771,7 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram, } "<" -> { if(dt==DataType.UBYTE) { + // TODO optimize Carry expr with rol? asmgen.out(""" lda $name cmp $otherName @@ -797,6 +798,7 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram, } "<=" -> { if(dt==DataType.UBYTE) { + // TODO optimize Carry expr with rol? asmgen.out(""" lda $otherName cmp $name @@ -822,6 +824,7 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram, } ">" -> { if(dt==DataType.UBYTE) { + // TODO optimize Carry expr with rol? asmgen.out(""" lda $name cmp $otherName @@ -848,6 +851,7 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram, } ">=" -> { if(dt==DataType.UBYTE) { + // TODO optimize Carry expr with rol? asmgen.out(""" lda $name cmp $otherName @@ -962,6 +966,7 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram, } "<" -> { if(dt==DataType.UBYTE) { + // TODO optimize Carry expr with rol? asmgen.out(""" lda $name cmp #$value @@ -988,6 +993,7 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram, } "<=" -> { if(dt==DataType.UBYTE) { + // TODO optimize Carry expr with rol? asmgen.out(""" lda #$value cmp $name @@ -1013,6 +1019,7 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram, } ">" -> { if(dt==DataType.UBYTE) { + // TODO optimize Carry expr with rol? asmgen.out(""" lda $name cmp #$value @@ -1039,6 +1046,7 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram, } ">=" -> { if(dt==DataType.UBYTE) { + // TODO optimize Carry expr with rol? asmgen.out(""" lda $name cmp #$value diff --git a/compiler/res/prog8lib/c128/syslib.p8 b/compiler/res/prog8lib/c128/syslib.p8 index 51f9b8b57..f87cf34e7 100644 --- a/compiler/res/prog8lib/c128/syslib.p8 +++ b/compiler/res/prog8lib/c128/syslib.p8 @@ -310,7 +310,7 @@ asmsub set_irq(uword handler @AY, ubyte useKernal @Pc) clobbers(A) { sta _modified+1 sty _modified+2 lda #0 - adc #0 + rol a sta _use_kernal sei lda #<_irq_handler @@ -404,7 +404,7 @@ asmsub set_rasterirq(uword handler @AY, uword rasterpos @R0, ubyte useKernal @P sta _modified+1 sty _modified+2 lda #0 - adc #0 + rol a sta set_irq._use_kernal lda cx16.r0 ldy cx16.r0+1 diff --git a/compiler/res/prog8lib/c64/syslib.p8 b/compiler/res/prog8lib/c64/syslib.p8 index d33634e18..e54291077 100644 --- a/compiler/res/prog8lib/c64/syslib.p8 +++ b/compiler/res/prog8lib/c64/syslib.p8 @@ -356,7 +356,7 @@ asmsub set_irq(uword handler @AY, ubyte useKernal @Pc) clobbers(A) { sta _modified+1 sty _modified+2 lda #0 - adc #0 + rol a sta _use_kernal sei lda #<_irq_handler @@ -450,7 +450,7 @@ asmsub set_rasterirq(uword handler @AY, uword rasterpos @R0, ubyte useKernal @P sta _modified+1 sty _modified+2 lda #0 - adc #0 + rol a sta set_irq._use_kernal lda cx16.r0 ldy cx16.r0+1 diff --git a/compiler/res/prog8lib/cx16/syslib.p8 b/compiler/res/prog8lib/cx16/syslib.p8 index 04b17f75d..85333f7aa 100644 --- a/compiler/res/prog8lib/cx16/syslib.p8 +++ b/compiler/res/prog8lib/cx16/syslib.p8 @@ -697,7 +697,7 @@ asmsub set_irq(uword handler @AY, ubyte useKernal @Pc) clobbers(A) { sta _modified+1 sty _modified+2 lda #0 - adc #0 + rol a sta _use_kernal sei lda #<_irq_handler diff --git a/compiler/res/prog8lib/string.p8 b/compiler/res/prog8lib/string.p8 index dcf5fc38f..5c43c05fa 100644 --- a/compiler/res/prog8lib/string.p8 +++ b/compiler/res/prog8lib/string.p8 @@ -292,7 +292,7 @@ str = P8ZP_SCRATCH_W1 sta modify_pattern2+2 jsr _match lda #0 - adc #0 + rol a ldx P8ZP_SCRATCH_REG rts diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 2af475381..5c44c4108 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,8 +3,7 @@ TODO For next minor release ^^^^^^^^^^^^^^^^^^^^^^ -- find bcc/bcs + lda branches that could be a rol? -- find adc #0 that could be a rol? +- find bcc/bcs + lda branches that could be a rol? (see "TODO optimize Carry expr with rol") - try to optimize newexpr a bit more ... diff --git a/examples/test.p8 b/examples/test.p8 index bff14ebfd..33da9ccdf 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,59 +1,54 @@ %import textio -%option no_sysinit %zeropage basicsafe main { sub start() { ubyte[10] envelope_attacks = 99 - ; signed word: - ; > - - - ; expect yep nope nope nope yep nope - cx16.r0s = -1000 - cx16.r2s = -999 - if (cx16.r0s < cx16.r2s) or (envelope_attacks[cx16.r1L]==0) { + ; expect nope yep yep nope yep yep + cx16.r0L = 8 + cx16.r2L = 9 + if (cx16.r0L >= cx16.r2L) or (envelope_attacks[cx16.r1L]==0) { txt.print("\nyep\n") } else { txt.print("\nnope\n") } - cx16.r0s = -999 - cx16.r2s = -999 - if (cx16.r0s < cx16.r2s) or (envelope_attacks[cx16.r1L]==0) { + cx16.r0L = 9 + cx16.r2L = 9 + if (cx16.r0L >= cx16.r2L) or (envelope_attacks[cx16.r1L]==0) { txt.print("\nyep\n") } else { txt.print("\nnope\n") } - cx16.r0s = -998 - cx16.r2s = -999 - if (cx16.r0s < cx16.r2s) or (envelope_attacks[cx16.r1L]==0) { + cx16.r0L = 10 + cx16.r2L = 9 + if (cx16.r0L >= cx16.r2L) or (envelope_attacks[cx16.r1L]==0) { txt.print("\nyep\n") } else { txt.print("\nnope\n") } - cx16.r0s = 0 - cx16.r2s = -999 - if (cx16.r0s < cx16.r2s) or (envelope_attacks[cx16.r1L]==0) { + cx16.r0L = 0 + cx16.r2L = 9 + if (cx16.r0L >= cx16.r2L) or (envelope_attacks[cx16.r1L]==0) { txt.print("\nyep\n") } else { txt.print("\nnope\n") } - cx16.r0s = -999 - cx16.r2s = 0 - if (cx16.r0s < cx16.r2s) or (envelope_attacks[cx16.r1L]==0) { + cx16.r0L = 9 + cx16.r2L = 0 + if (cx16.r0L >= cx16.r2L) or (envelope_attacks[cx16.r1L]==0) { txt.print("\nyep\n") } else { txt.print("\nnope\n") } - cx16.r0s = $7fff - cx16.r2s = $7eff - if (cx16.r0s < cx16.r2s) or (envelope_attacks[cx16.r1L]==0) { + cx16.r0L = 255 + cx16.r2L = 9 + if (cx16.r0L >= cx16.r2L) or (envelope_attacks[cx16.r1L]==0) { txt.print("\nyep\n") } else { txt.print("\nnope\n")