From 9242508abf6d03221837fb1063eff855aabf6c81 Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Sun, 12 Nov 2023 22:15:03 +0100 Subject: [PATCH] Optimize substraction of 1 --- src/cc65/codegen.c | 70 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 53 insertions(+), 17 deletions(-) diff --git a/src/cc65/codegen.c b/src/cc65/codegen.c index f923ac8c9..0f45f66d9 100644 --- a/src/cc65/codegen.c +++ b/src/cc65/codegen.c @@ -2000,25 +2000,55 @@ void g_subeqstatic (unsigned flags, uintptr_t label, long offs, case CF_INT: if (flags & CF_CONST) { - AddCodeLine ("lda %s", lbuf); - AddCodeLine ("sec"); - AddCodeLine ("sbc #$%02X", (unsigned char)val); - AddCodeLine ("sta %s", lbuf); - if (val < 0x100) { - unsigned L = GetLocalLabel (); - AddCodeLine ("bcs %s", LocalLabelName (L)); - AddCodeLine ("dec %s+1", lbuf); - g_defcodelabel (L); + if (val == 1) { + unsigned L = GetLocalLabel(); if ((flags & CF_NOKEEP) == 0) { - AddCodeLine ("ldx %s+1", lbuf); + if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0) { + AddCodeLine ("lda %s", lbuf); + AddCodeLine ("bne %s", LocalLabelName (L)); + AddCodeLine ("dec %s+1", lbuf); + g_defcodelabel (L); + AddCodeLine ("dea"); + AddCodeLine ("sta %s", lbuf); + AddCodeLine ("ldx %s+1", lbuf); + } else { + AddCodeLine ("ldx %s", lbuf); + AddCodeLine ("bne %s", LocalLabelName (L)); + AddCodeLine ("dec %s+1", lbuf); + g_defcodelabel (L); + AddCodeLine ("dex"); + AddCodeLine ("stx %s", lbuf); + AddCodeLine ("txa"); + AddCodeLine ("ldx %s+1", lbuf); + } + } else { + AddCodeLine ("lda %s", lbuf); + AddCodeLine ("bne %s", LocalLabelName (L)); + AddCodeLine ("dec %s+1", lbuf); + g_defcodelabel (L); + AddCodeLine ("dec %s", lbuf); } } else { - AddCodeLine ("lda %s+1", lbuf); - AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8)); - AddCodeLine ("sta %s+1", lbuf); - if ((flags & CF_NOKEEP) == 0) { - AddCodeLine ("tax"); - AddCodeLine ("lda %s", lbuf); + AddCodeLine ("lda %s", lbuf); + AddCodeLine ("sec"); + AddCodeLine ("sbc #$%02X", (unsigned char)val); + AddCodeLine ("sta %s", lbuf); + if (val < 0x100) { + unsigned L = GetLocalLabel (); + AddCodeLine ("bcs %s", LocalLabelName (L)); + AddCodeLine ("dec %s+1", lbuf); + g_defcodelabel (L); + if ((flags & CF_NOKEEP) == 0) { + AddCodeLine ("ldx %s+1", lbuf); + } + } else { + AddCodeLine ("lda %s+1", lbuf); + AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8)); + AddCodeLine ("sta %s+1", lbuf); + if ((flags & CF_NOKEEP) == 0) { + AddCodeLine ("tax"); + AddCodeLine ("lda %s", lbuf); + } } } } else { @@ -3693,7 +3723,13 @@ void g_dec (unsigned flags, unsigned long val) } else { /* Inline the code */ if (val < 0x300) { - if ((val & 0xFF) != 0) { + 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) { unsigned L = GetLocalLabel(); AddCodeLine ("sec"); AddCodeLine ("sbc #$%02X", (unsigned char) val);