+ D->IP = D->OpIndex+1;
+
+ /* cpx */
+ X = NewCodeEntry (OP65_CPX, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+
+ /* bne L */
+ X = NewCodeEntry (OP65_BNE, AM65_BRA, L->Name, L, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+
+ /* cmp */
+ X = NewCodeEntry (OP65_CMP, LoadA->AM, LoadA->Arg, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+
+ /* Lhs load entries can be removed */
+ D->Lhs.X.Flags |= LI_REMOVE;
+ D->Lhs.A.Flags |= LI_REMOVE;
+
+ } else if ((D->Rhs.A.Flags & (LI_DIRECT | LI_RELOAD_Y)) == LI_DIRECT &&
+ (D->Rhs.X.Flags & (LI_DIRECT | LI_RELOAD_Y)) == LI_DIRECT) {
+
+ CodeEntry* LoadX = D->Rhs.X.LoadEntry;
+ CodeEntry* LoadA = D->Rhs.A.LoadEntry;
+
+ D->IP = D->OpIndex+1;
+
+ /* cpx */
+ X = NewCodeEntry (OP65_CPX, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+
+ /* bne L */
+ X = NewCodeEntry (OP65_BNE, AM65_BRA, L->Name, L, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+
+ /* cmp */
+ X = NewCodeEntry (OP65_CMP, LoadA->AM, LoadA->Arg, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+
+ /* Rhs load entries can be removed */
+ D->Rhs.X.Flags |= LI_REMOVE;
+ D->Rhs.A.Flags |= LI_REMOVE;
+
+ } else if ((D->Rhs.A.Flags & LI_DIRECT) != 0 &&
+ (D->Rhs.X.Flags & LI_DIRECT) != 0) {
+
+ D->IP = D->OpIndex+1;
+
+ /* Add operand for low byte */
+ AddOpLow (D, OP65_CMP, &D->Rhs);
+
+ /* bne L */
+ X = NewCodeEntry (OP65_BNE, AM65_BRA, L->Name, L, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+
+ /* Add operand for high byte */
+ AddOpHigh (D, OP65_CMP, &D->Rhs, 0);
+
+ } else {
+
+ /* Save lhs into zeropage, then compare */
+ AddStoreX (D);
+ AddStoreA (D);
+
+ D->IP = D->OpIndex+1;
+
+ /* cpx */
+ X = NewCodeEntry (OP65_CPX, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+
+ /* bne L */
+ X = NewCodeEntry (OP65_BNE, AM65_BRA, L->Name, L, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+
+ /* cmp */
+ X = NewCodeEntry (OP65_CMP, AM65_ZP, D->ZPLo, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+
+ }
+
+ /* Remove the push and the call to the tosgeax function */
+ RemoveRemainders (D);
+
+ /* We changed the sequence */
+ return 1;
+}
+
+
+
+static unsigned Opt___bzero (StackOpData* D)
+/* Optimize the __bzero sequence */
+{
+ CodeEntry* X;
+ const char* Arg;
+ CodeLabel* L;
+
+ /* Check if we're using a register variable */
+ if (!IsRegVar (D)) {
+ /* Store the value into the zeropage instead of pushing it */
+ AddStoreX (D);
+ AddStoreA (D);
+ }
+
+ /* If the return value of __bzero is used, we have to add code to reload
+ * a/x from the pointer variable.
+ */
+ if (RegAXUsed (D->Code, D->OpIndex+1)) {
+ X = NewCodeEntry (OP65_LDA, AM65_ZP, D->ZPLo, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->OpIndex+1);
+ X = NewCodeEntry (OP65_LDX, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->OpIndex+2);
+ }
+
+ /* X is always zero, A contains the size of the data area to zero.
+ * Note: A may be zero, in which case the operation is null op.
+ */
+ if (D->OpEntry->RI->In.RegA != 0) {
+
+ /* lda #$00 */
+ X = NewCodeEntry (OP65_LDA, AM65_IMM, "$00", 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->OpIndex+1);
+
+ /* The value of A is known */
+ if (D->OpEntry->RI->In.RegA <= 0x81) {
+
+ /* Loop using the sign bit */
+
+ /* ldy #count-1 */
+ Arg = MakeHexArg (D->OpEntry->RI->In.RegA - 1);
+ X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->OpIndex+2);
+
+ /* L: sta (zp),y */
+ X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, D->ZPLo, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->OpIndex+3);
+ L = CS_GenLabel (D->Code, X);
+
+ /* dey */
+ X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->OpIndex+4);
+
+ /* bpl L */
+ X = NewCodeEntry (OP65_BPL, AM65_BRA, L->Name, L, D->OpEntry->LI);
+ InsertEntry (D, X, D->OpIndex+5);
+
+ } else {
+
+ /* Loop using an explicit compare */
+
+ /* ldy #$00 */
+ X = NewCodeEntry (OP65_LDY, AM65_IMM, "$00", 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->OpIndex+2);
+
+ /* L: sta (zp),y */
+ X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, D->ZPLo, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->OpIndex+3);
+ L = CS_GenLabel (D->Code, X);
+
+ /* iny */
+ X = NewCodeEntry (OP65_INY, AM65_IMP, 0, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->OpIndex+4);
+
+ /* cpy #count */
+ Arg = MakeHexArg (D->OpEntry->RI->In.RegA);
+ X = NewCodeEntry (OP65_CPY, AM65_IMM, Arg, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->OpIndex+5);
+
+ /* bne L */
+ X = NewCodeEntry (OP65_BNE, AM65_BRA, L->Name, L, D->OpEntry->LI);
+ InsertEntry (D, X, D->OpIndex+6);
+ }
+
+ }
+
+ /* Remove the push and the call to the __bzero function */
+ RemoveRemainders (D);
+
+ /* We changed the sequence */
+ return 1;
+}
+
+
+
+static unsigned Opt_staspidx (StackOpData* D)
+/* Optimize the staspidx sequence */
+{
+ CodeEntry* X;
+
+ /* Check if we're using a register variable */
+ if (!IsRegVar (D)) {
+ /* Store the value into the zeropage instead of pushing it */
+ AddStoreX (D);
+ AddStoreA (D);
+ }
+
+ /* Replace the store subroutine call by a direct op */
+ X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, D->ZPLo, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->OpIndex+1);