optimize const word repeat setup

This commit is contained in:
Irmen de Jong 2024-05-18 15:20:10 +02:00
parent aa2437cfb8
commit 6e8a89e6f1
2 changed files with 7 additions and 8 deletions

View File

@ -819,17 +819,15 @@ class AsmGen6502Internal (
loopEndLabels.pop() loopEndLabels.pop()
} }
private fun repeatWordCount(count: Int, stmt: PtRepeatLoop) { private fun repeatWordCount(iterations: Int, stmt: PtRepeatLoop) {
require(count in 257..65535) { "invalid repeat count ${stmt.position}" } require(iterations in 257..65535) { "invalid repeat count ${stmt.position}" }
val repeatLabel = makeLabel("repeat") val repeatLabel = makeLabel("repeat")
val counterVar = createRepeatCounterVar(DataType.UWORD, isTargetCpu(CpuType.CPU65c02), stmt) val counterVar = createRepeatCounterVar(DataType.UWORD, isTargetCpu(CpuType.CPU65c02), stmt)
// the iny + double dec is microoptimization of the 16 bit loop val loopcount = if(iterations and 0x00ff == 0) iterations else iterations + 0x0100 // so that the loop can simply use a double-dec
out(""" out("""
ldy #>$count ldy #>$loopcount
lda #<$count lda #<$loopcount
beq + sta $counterVar
iny
+ sta $counterVar
sty $counterVar+1 sty $counterVar+1
$repeatLabel""") $repeatLabel""")
translate(stmt.statements) translate(stmt.statements)

View File

@ -56,6 +56,7 @@ string {
sub find(str st, ubyte character) -> ubyte { sub find(str st, ubyte character) -> ubyte {
; Locates the first position of the given character in the string, ; Locates the first position of the given character in the string,
; returns Carry set if found + index in A, or Carry clear if not found (and A will be 255, an invalid index). ; returns Carry set if found + index in A, or Carry clear if not found (and A will be 255, an invalid index).
; NOTE: because this isn't an asmsub, there's only a SINGLE return value here. On the c64/cx16 targets etc there are 2 return values.
ubyte ix ubyte ix
for ix in 0 to length(st)-1 { for ix in 0 to length(st)-1 {
if st[ix]==character { if st[ix]==character {