From 2a2cc6cad69dd83ebd02b314f253119461ec7cfb Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Sat, 9 Dec 2023 16:43:23 +0100 Subject: [PATCH] Fix bug introduced in #2260 bne should have applied to A, not X, but adding a cmp #$00 before makes the change less optimized than the existing. --- src/cc65/codegen.c | 8 +------- test/val/sub3.c | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/cc65/codegen.c b/src/cc65/codegen.c index db487ca40..c2bdfcd63 100644 --- a/src/cc65/codegen.c +++ b/src/cc65/codegen.c @@ -3697,13 +3697,7 @@ void g_dec (unsigned flags, unsigned long val) } else { /* Inline the code */ if (val < 0x300) { - if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && val == 1) { - unsigned L = GetLocalLabel(); - AddCodeLine ("bne %s", LocalLabelName (L)); - AddCodeLine ("dex"); - g_defcodelabel (L); - AddCodeLine ("dea"); - } else if ((val & 0xFF) != 0) { + if ((val & 0xFF) != 0) { unsigned L = GetLocalLabel(); AddCodeLine ("sec"); AddCodeLine ("sbc #$%02X", (unsigned char) val); diff --git a/test/val/sub3.c b/test/val/sub3.c index 2a3646f9a..dd050224e 100644 --- a/test/val/sub3.c +++ b/test/val/sub3.c @@ -168,6 +168,31 @@ void post_dec_assign_test(void) failures++; } +void dex_tests(void) { + static unsigned int a, b; + + a = 257; + b = a - 1; + if (b != 256) { + printf("fail 257 => 256\n"); + failures++; + } + + a = 256; + b = a - 1; + if (b != 255) { + printf("fail 256 => 255\n"); + failures++; + } + + a = 255; + b = a - 1; + if (b != 254) { + printf("fail 255 => 254\n"); + failures++; + } +} + int main(void) { int0 = 5; @@ -186,6 +211,8 @@ int main(void) int1 = 5; post_dec_assign_test(); + dex_tests(); + printf("failures: %d\n",failures); return failures;