diff --git a/src/cc65/coptind.c b/src/cc65/coptind.c index 04deb0119..811adff04 100644 --- a/src/cc65/coptind.c +++ b/src/cc65/coptind.c @@ -1147,8 +1147,9 @@ unsigned OptDupLoads (CodeSeg* S) /* Get next entry */ CodeEntry* E = CS_GetEntry (S, I); - /* Assume we won't delete the entry */ + /* Assume we won't delete or replace the entry */ int Delete = 0; + opc_t NewOPC = OP65_INVALID; /* Get a pointer to the input registers of the insn */ const RegContents* In = &E->RI->In; @@ -1218,7 +1219,7 @@ unsigned OptDupLoads (CodeSeg* S) E->AM != AM65_ABSY && E->AM != AM65_ZPY) { /* Use the A register instead */ - CE_ReplaceOPC (E, OP65_STA); + NewOPC = OP65_STA; } break; @@ -1242,11 +1243,11 @@ unsigned OptDupLoads (CodeSeg* S) */ } else if (RegValIsKnown (In->RegY)) { if (In->RegY == In->RegA) { - CE_ReplaceOPC (E, OP65_STA); + NewOPC = OP65_STA; } else if (In->RegY == In->RegX && E->AM != AM65_ABSX && E->AM != AM65_ZPX) { - CE_ReplaceOPC (E, OP65_STX); + NewOPC = OP65_STX; } } break; @@ -1319,6 +1320,14 @@ unsigned OptDupLoads (CodeSeg* S) } else { + if (NewOPC != OP65_INVALID) { + /* Replace the opcode */ + CE_ReplaceOPC (E, NewOPC); + + /* Remember, we had changes */ + ++Changes; + } + /* Next entry */ ++I; diff --git a/src/cc65/opcodes.h b/src/cc65/opcodes.h index bdb0c28a2..24a289c26 100644 --- a/src/cc65/opcodes.h +++ b/src/cc65/opcodes.h @@ -128,7 +128,10 @@ typedef enum { OP65_TYA, /* Number of opcodes available */ - OP65_COUNT + OP65_COUNT, + + /* Invalid opcode */ + OP65_INVALID = OP65_COUNT, } opc_t; /* 65XX addressing modes */