From 95be1c9e222992c73db8c088cb2f717d4d6fabdf Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 3 Sep 2023 00:42:41 +0200 Subject: [PATCH] fix optimized swapped in-place byte comparisons --- .../assignment/AugmentableAssignmentAsmGen.kt | 114 +++++++++--------- docs/source/todo.rst | 1 - examples/test.p8 | 29 +++-- 3 files changed, 76 insertions(+), 68 deletions(-) diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AugmentableAssignmentAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AugmentableAssignmentAsmGen.kt index cb0424a9d..9389d205c 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AugmentableAssignmentAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/assignment/AugmentableAssignmentAsmGen.kt @@ -790,11 +790,12 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram, "<" -> { if(!signed) { asmgen.out(""" - tay + cmp $variable + bcc + lda #0 - cpy $variable - rol a - eor #1""") + beq ++ ++ lda #1 ++""") } else { // see http://www.6502.org/tutorials/compare_beyond.html @@ -941,17 +942,63 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram, } } "<" -> { + // variable A>variable? if(!signed) { - TODO("swap operand order") asmgen.out(""" tay lda #0 cpy $variable + beq + rol a - eor #1""") ++""") } else { - TODO("swap operand order") + // see http://www.6502.org/tutorials/compare_beyond.html + asmgen.out(""" + clc + sbc $variable + bvc + + eor #$80 ++ bpl + + lda #0 + beq ++ ++ lda #1 ++""") + } + } + "<=" -> { + // variable<=A --> A>=variable? + if(!signed) { + asmgen.out(""" + tay + lda #0 + cpy $variable + rol a""") + } else { + // see http://www.6502.org/tutorials/compare_beyond.html + asmgen.out(""" + sec + sbc $variable + bvc + + eor #$80 ++ bpl + + lda #0 + beq ++ ++ lda #1 ++""") + } + } + ">" -> { + // variable>A --> A=" -> { + // variable>=A --> A<=variable? if(!signed) { - TODO("swap operand order") asmgen.out(""" cmp $variable bcc + @@ -977,7 +1024,6 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram, + lda #1 +""") } else { - TODO("swap operand order") // see http://www.6502.org/tutorials/compare_beyond.html asmgen.out(""" clc @@ -988,54 +1034,6 @@ internal class AugmentableAssignmentAsmGen(private val program: PtProgram, lda #0 beq ++ + lda #1 -+""") - } - } - ">" -> { - if(!signed) { - TODO("swap operand order") - asmgen.out(""" - tay - lda #0 - cpy $variable - beq + - rol a -+""") - } else { - TODO("swap operand order") - // see http://www.6502.org/tutorials/compare_beyond.html - asmgen.out(""" - clc - sbc $variable - bvc + - eor #$80 -+ bpl + - lda #0 - beq ++ -+ lda #1 -+""") - } - } - ">=" -> { - if(!signed) { - TODO("swap operand order") - asmgen.out(""" - tay - lda #0 - cpy $variable - rol a""") - } else { - TODO("swap operand order") - // see http://www.6502.org/tutorials/compare_beyond.html - asmgen.out(""" - sec - sbc $variable - bvc + - eor #$80 -+ bpl + - lda #0 - beq ++ -+ lda #1 +""") } } diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 8ecec31fe..df8c618ef 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,7 +3,6 @@ TODO - fix on c64 target: examples/cube3d-float (broken since 9.3 with the evalstack removal) it works on x16 target, oddly enough. More detailed and simpler code for this problem in floatproblem64.p8 / floatproblem64.asm (the minified version) -- fix: search for TODO("swap operand order") - optimize: search for TODO optimize: don't use scratch var - prefix prog8 subroutines with p8s_ instead of p8_ to not let them clash with variables in the asm? - allow 'chained' array indexing for expressions: value = ptrarray[0][0] diff --git a/examples/test.p8 b/examples/test.p8 index 91a8d6a6c..0d728d72b 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -3,15 +3,26 @@ main { sub start() { - ubyte index = 100 - ubyte[] t_index = [1,2,3,4,5] - ubyte nibble = 0 + byte index = 100 + byte[] t_index = [-1,-2,-3,-4,-5] - index += t_index[4] - index += t_index[nibble] - nibble++ - index -= t_index[3] - index -= t_index[nibble] - txt.print_ub(index) ; 100 + index = 4 + index = index>t_index[4] + txt.print_b(index) + txt.nl() + index = index>t_index[4] + txt.print_b(index) + txt.nl() + +; index = index < t_index[4] +; index = index < t_index[nibble] +; txt.print_ub(index) +; txt.nl() +; +; nibble++ +; index = index > t_index[3] +; index = index > t_index[nibble] +; txt.print_ub(index) ; 100 +; txt.nl() } }