From cd1862dd9fa66b559aad4a866779df36a13e2ac9 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 27 Sep 2025 23:07:49 +0200 Subject: [PATCH] fix signed long shift right --- .../assignment/AugmentableAssignmentAsmGen.kt | 93 +++---------------- docs/source/todo.rst | 3 +- 2 files changed, 13 insertions(+), 83 deletions(-) diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AugmentableAssignmentAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AugmentableAssignmentAsmGen.kt index 1bc3be27b..898e98636 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AugmentableAssignmentAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AugmentableAssignmentAsmGen.kt @@ -605,7 +605,9 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram, ">>" -> { asmgen.out(""" ldy $sourceVar -- lsr $targetVar+3 +- lda $targetVar+3 + asl a ; save sign bit + ror $targetVar+3 ror $targetVar+2 ror $targetVar+1 ror $targetVar @@ -780,100 +782,27 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram, value in 0..2 -> { repeat(value) { asmgen.out(""" - lsr $variable+3 + lda $variable+3 + asl a ; save sign bit + ror $variable+3 ror $variable+2 ror $variable+1 ror $variable""") } } - value in 3..7 -> { + // TODO optimize for more cases 8, 16, 24 etc but don't forget to take the sign bit into account! + value <= 31 -> { asmgen.out(""" ldy #$value -- lsr $variable+3 +- lda $variable+3 + asl a ; save sign bit + ror $variable+3 ror $variable+2 ror $variable+1 ror $variable dey bne -""") } - value == 8 -> { - asmgen.out(""" - lda $variable+1 - sta $variable - lda $variable+2 - sta $variable+1 - lda $variable+3 - sta $variable+2 - lda #0 - sta $variable+3""") - } - value in 9..15 -> { - val shift = value - 8 - asmgen.out(""" - lda $variable+1 - sta $variable - lda $variable+2 - sta $variable+1 - lda $variable+3 - sta $variable+2 - lda #0 - sta $variable+3 - ldy #$shift -- lsr $variable+2 - ror $variable+1 - ror $variable - dey - bne -""") - } - value == 16 -> { - asmgen.out(""" - lda $variable+3 - sta $variable+1 - lda $variable+2 - sta $variable - lda #0 - sta $variable+2 - sta $variable+3""") - } - value in 17..23 -> { - val shift = value-16 - asmgen.out(""" - lda $variable+3 - sta $variable+1 - lda $variable+2 - sta $variable - lda #0 - sta $variable+2 - sta $variable+3 - ldy #$shift -- lsr $variable+1 - ror $variable - dey - bne -""") - } - value == 24 -> { - asmgen.out(""" - lda $variable+3 - sta $variable - lda #0 - sta $variable+1 - sta $variable+2 - sta $variable+3""") - } - value <= 31 -> { - val shift = value-24 - asmgen.out(""" - lda $variable+3 - ldy #$shift -- lsr a - dey - bne - - sta $variable - lda #0 - sta $variable+1 - sta $variable+2 - sta $variable+3""") - } else -> { asmgen.out(""" lda #0 diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 40b024b17..f6ca6ed40 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -1,7 +1,6 @@ TODO ==== -CHECK THAT >> and << work correctly for negative longs implement the bitwise & | ^ operations as expressions on longs (all types args) @@ -177,6 +176,8 @@ Optimizations - more optimized operator handling of different types, for example uword a ^ byte b now does a type cast of b to word first - optimize longEqualsValue() for const and variable operands to not assign needlessly to R0-R3. +- optimize inplacemodificationLongWithLiteralval() for more shift values such as 8, 16, 24 etc but take sign bit into account! +- optimize inplacemodificationLongWithLiteralval() for more shift values such as 8, 16, 24 etc but take sign bit into account! - Port benchmarks from https://thred.github.io/c-bench-64/ to prog8 and see how it stacks up. - Since fixing the missing zp-var initialization, programs grew in size again because STZ's reappeared. Can we add more intelligent (and correct!) optimizations to remove those STZs that might be redundant again? - in Identifier: use typedarray of strings instead of listOf? Other places?