From f6136891cc7b775cb2bbfe27acdd8108c9e076cc Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 13 Feb 2021 13:27:06 +0100 Subject: [PATCH] optimized for loop over const bytes, fixed downto 1 --- .../target/c64/codegen/ForLoopsAsmGen.kt | 38 ++++++++----------- docs/source/todo.rst | 1 - examples/test.p8 | 35 +++++++++++------ 3 files changed, 40 insertions(+), 34 deletions(-) diff --git a/compiler/src/prog8/compiler/target/c64/codegen/ForLoopsAsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/ForLoopsAsmGen.kt index f32429a04..7cfc8f506 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/ForLoopsAsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/ForLoopsAsmGen.kt @@ -369,7 +369,7 @@ $loopLabel""") throw AssemblyError("step 0, 1 and -1 should have been handled specifically $stmt") } 2 -> { - if(range.last==255) { + if(range.last==255 || range.last==254) { asmgen.out(""" inc $varname beq $endLabel @@ -377,12 +377,11 @@ $loopLabel""") bne $loopLabel""") } else { asmgen.out(""" + inc $varname + inc $varname lda $varname - cmp #${range.last} - beq $endLabel - inc $varname - inc $varname - jmp $loopLabel""") + cmp #${range.last+2} + bne $loopLabel""") } } -2 -> { @@ -399,12 +398,11 @@ $loopLabel""") dec $varname bne $loopLabel""") else -> asmgen.out(""" + dec $varname + dec $varname lda $varname - cmp #${range.last} - beq $endLabel - dec $varname - dec $varname - jmp $loopLabel""") + cmp #${range.last-2} + bne $loopLabel""") } } else -> { @@ -480,11 +478,10 @@ $loopLabel""") $endLabel""") } else { asmgen.out(""" - lda $varname - cmp #${range.last} - beq $endLabel inc $varname - jmp $loopLabel + lda $varname + cmp #${range.last+1} + bne $loopLabel $endLabel""") } asmgen.loopEndLabels.pop() @@ -512,16 +509,15 @@ $endLabel""") 1 -> { asmgen.out(""" dec $varname - jmp $loopLabel + bne $loopLabel $endLabel""") } else -> { asmgen.out(""" - lda $varname - cmp #${range.last} - beq $endLabel dec $varname - jmp $loopLabel + lda $varname + cmp #${range.last-1} + bne $loopLabel $endLabel""") } } @@ -546,7 +542,6 @@ $loopLabel""") bne + lda $varname+1 cmp #>${range.last} - bne + beq $endLabel + inc $varname bne $loopLabel @@ -574,7 +569,6 @@ $loopLabel""") bne + lda $varname+1 cmp #>${range.last} - bne + beq $endLabel + lda $varname bne + diff --git a/docs/source/todo.rst b/docs/source/todo.rst index b7a03c2de..9929b0c63 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -2,7 +2,6 @@ TODO ==== -- optimize for loop iterations better to allow proper inx, cpx #value, bne loop instructions (like repeat loop) - implement the linked_list millfork benchmark - refactor the asmgen into their own submodule? diff --git a/examples/test.p8 b/examples/test.p8 index d07ae9c86..39c0ba4aa 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -4,16 +4,29 @@ main { sub start() { - txt.print("hello\n") - txt.column(3) - txt.print("hello2\n") - txt.column(8) - txt.print("hello3\n") - txt.column(34) - txt.print("hello4\n") - txt.column(1) - txt.print("hello5\n") - txt.column(0) - txt.print("hello6\n") + ubyte ib + uword iw + ubyte xx + + xx=0 + + for ib in 241 to 253 step 2 { + txt.print_ub(ib) + txt.nl() + xx++ + } + + for ib in 10 downto 2 step -2 { + txt.print_ub(ib) + txt.nl() + xx-- + } + for ib in 6 downto 0 step -2 { + txt.print_ub(ib) + txt.nl() + xx-- + } + + txt.print_ub(xx) } }