From 6e8a89e6f1cd57bcbb5ec22153dc333a70cc27f3 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 18 May 2024 15:20:10 +0200 Subject: [PATCH] optimize const word repeat setup --- codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt | 14 ++++++-------- compiler/res/prog8lib/virtual/string.p8 | 1 + 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt index 1ac21b985..aba1ff727 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt @@ -819,17 +819,15 @@ class AsmGen6502Internal ( loopEndLabels.pop() } - private fun repeatWordCount(count: Int, stmt: PtRepeatLoop) { - require(count in 257..65535) { "invalid repeat count ${stmt.position}" } + private fun repeatWordCount(iterations: Int, stmt: PtRepeatLoop) { + require(iterations in 257..65535) { "invalid repeat count ${stmt.position}" } val repeatLabel = makeLabel("repeat") 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(""" - ldy #>$count - lda #<$count - beq + - iny -+ sta $counterVar + ldy #>$loopcount + lda #<$loopcount + sta $counterVar sty $counterVar+1 $repeatLabel""") translate(stmt.statements) diff --git a/compiler/res/prog8lib/virtual/string.p8 b/compiler/res/prog8lib/virtual/string.p8 index 531fb39cb..cc71e3433 100644 --- a/compiler/res/prog8lib/virtual/string.p8 +++ b/compiler/res/prog8lib/virtual/string.p8 @@ -56,6 +56,7 @@ string { sub find(str st, ubyte character) -> ubyte { ; 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). + ; 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 for ix in 0 to length(st)-1 { if st[ix]==character {