+
+ /* txa */
+ X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+
+ if ((LI->X.Flags & LI_DIRECT) != 0) {
+
+ if ((LI->X.Flags & LI_RELOAD_Y) == 0) {
+
+ /* opc xxx */
+ CodeEntry* LoadX = LI->X.LoadEntry;
+ X = NewCodeEntry (OPC, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+
+ } else {
+
+ /* ldy #const */
+ const char* Arg = MakeHexArg (LI->X.Offs);
+ X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+
+ /* opc (sp),y */
+ X = NewCodeEntry (OPC, AM65_ZP_INDY, "sp", 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+ }
+
+ /* In both cases, we can remove the load */
+ LI->X.Flags |= LI_REMOVE;
+
+ } else {
+ /* opc zphi */
+ X = NewCodeEntry (OPC, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+ }
+
+ if (KeepResult) {
+ /* tax */
+ X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+
+ /* pla */
+ X = NewCodeEntry (OP65_PLA, AM65_IMP, 0, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+ }
+}
+
+
+
+static void RemoveRegLoads (StackOpData* D, LoadInfo* LI)
+/* Remove register load insns */
+{
+ /* Both registers may be loaded with one insn, but DelEntry will in this
+ * case clear the other one.
+ */
+ if (LI->A.Flags & LI_REMOVE) {
+ if (LI->A.LoadIndex >= 0) {
+ DelEntry (D, LI->A.LoadIndex);
+ }
+ if (LI->A.XferIndex >= 0) {
+ DelEntry (D, LI->A.XferIndex);
+ }
+ }
+ if (LI->X.Flags & LI_REMOVE) {
+ if (LI->X.LoadIndex >= 0) {
+ DelEntry (D, LI->X.LoadIndex);
+ }
+ if (LI->X.XferIndex >= 0) {
+ DelEntry (D, LI->X.XferIndex);
+ }
+ }
+}
+
+
+
+static void RemoveRemainders (StackOpData* D)
+/* Remove the code that is unnecessary after translation of the sequence */
+{
+ /* Remove the register loads for lhs and rhs */
+ RemoveRegLoads (D, &D->Lhs);
+ RemoveRegLoads (D, &D->Rhs);
+
+ /* Remove the push and the operator routine */
+ DelEntry (D, D->OpIndex);
+ DelEntry (D, D->PushIndex);
+}
+
+
+
+static int IsRegVar (StackOpData* D)
+/* If the value pushed is that of a zeropage variable, replace ZPLo and ZPHi
+ * in the given StackOpData struct by the variable and return true. Otherwise
+ * leave D untouched and return false.
+ */
+{
+ CodeEntry* LoadA = D->Lhs.A.LoadEntry;
+ CodeEntry* LoadX = D->Lhs.X.LoadEntry;
+ unsigned Len;
+
+ /* Must have both load insns */
+ if (LoadA == 0 || LoadX == 0) {
+ return 0;
+ }
+
+ /* Must be loads from zp */
+ if (LoadA->AM != AM65_ZP || LoadX->AM != AM65_ZP) {
+ return 0;
+ }
+
+ /* Must be the same zp loc with high byte in X */
+ Len = strlen (LoadA->Arg);
+ if (strncmp (LoadA->Arg, LoadX->Arg, Len) != 0 ||
+ strcmp (LoadX->Arg + Len, "+1") != 0) {
+ return 0;
+ }
+
+ /* Use the zero page location directly */
+ D->ZPLo = LoadA->Arg;
+ D->ZPHi = LoadX->Arg;
+ return 1;
+}
+
+
+
+/*****************************************************************************/
+/* Actual optimization functions */
+/*****************************************************************************/
+
+
+
+static unsigned Opt_toseqax_tosneax (StackOpData* D, const char* BoolTransformer)
+/* Optimize the toseqax and tosneax sequences. */
+{
+ CodeEntry* X;
+ CodeLabel* L;
+
+ /* Create a call to the boolean transformer function and a label for this
+ * insn. This is needed for all variants. Other insns are inserted *before*
+ * the call.
+ */
+ X = NewCodeEntry (OP65_JSR, AM65_ABS, BoolTransformer, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->OpIndex + 1);
+ L = CS_GenLabel (D->Code, X);
+
+ /* If the lhs is direct (but not stack relative), encode compares with lhs
+ * effectively reverting the order (which doesn't matter for ==).
+ */
+ if ((D->Lhs.A.Flags & (LI_DIRECT | LI_RELOAD_Y)) == LI_DIRECT &&
+ (D->Lhs.X.Flags & (LI_DIRECT | LI_RELOAD_Y)) == LI_DIRECT) {
+
+ CodeEntry* LoadX = D->Lhs.X.LoadEntry;
+ CodeEntry* LoadA = D->Lhs.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++);
+
+ /* 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);
+
+ /* Remove the push and the call to the staspidx function */
+ RemoveRemainders (D);
+
+ /* We changed the sequence */
+ return 1;
+}
+
+
+
+static unsigned Opt_staxspidx (StackOpData* D)
+/* Optimize the staxspidx 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);
+ }
+
+ /* Inline the store */
+
+ /* sta (zp),y */
+ X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, D->ZPLo, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->OpIndex+1);
+
+ if (RegValIsKnown (D->OpEntry->RI->In.RegY)) {
+ /* Value of Y is known */
+ const char* Arg = MakeHexArg (D->OpEntry->RI->In.RegY + 1);
+ X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
+ } else {
+ X = NewCodeEntry (OP65_INY, AM65_IMP, 0, 0, D->OpEntry->LI);
+ }
+ InsertEntry (D, X, D->OpIndex+2);
+
+ if (RegValIsKnown (D->OpEntry->RI->In.RegX)) {
+ /* Value of X is known */
+ const char* Arg = MakeHexArg (D->OpEntry->RI->In.RegX);
+ X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, D->OpEntry->LI);
+ } else {
+ /* Value unknown */
+ X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, D->OpEntry->LI);
+ }
+ InsertEntry (D, X, D->OpIndex+3);
+
+ /* sta (zp),y */
+ X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, D->ZPLo, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->OpIndex+4);
+
+ /* If we remove staxspidx, we must restore the Y register to what the
+ * function would return.
+ */
+ X = NewCodeEntry (OP65_LDY, AM65_IMM, "$00", 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->OpIndex+5);
+
+ /* Remove the push and the call to the staxspidx function */
+ RemoveRemainders (D);
+
+ /* We changed the sequence */
+ return 1;
+}
+
+
+
+static unsigned Opt_tosaddax (StackOpData* D)
+/* Optimize the tosaddax sequence */
+{
+ CodeEntry* X;
+ CodeEntry* N;
+
+ /* We need the entry behind the add */
+ CHECK (D->NextEntry != 0);
+
+ /* Check if the X register is known and zero when the add is done, and
+ * if the add is followed by
+ *
+ * ldy #$00
+ * jsr ldauidx ; or ldaidx
+ *
+ * If this is true, the addition does actually add an offset to a pointer
+ * before it is dereferenced. Since both subroutines take an offset in Y,
+ * we can pass the offset (instead of #$00) and remove the addition
+ * alltogether.
+ */
+ if (D->OpEntry->RI->In.RegX == 0 &&
+ D->NextEntry->OPC == OP65_LDY &&
+ CE_IsKnownImm (D->NextEntry, 0) &&
+ !CE_HasLabel (D->NextEntry) &&
+ (N = CS_GetNextEntry (D->Code, D->OpIndex + 1)) != 0 &&
+ (CE_IsCallTo (N, "ldauidx") ||
+ CE_IsCallTo (N, "ldaidx"))) {
+
+ int Signed = (strcmp (N->Arg, "ldaidx") == 0);
+
+ /* Store the value into the zeropage instead of pushing it */
+ AddStoreX (D);
+ AddStoreA (D);
+
+ /* Replace the ldy by a tay. Be sure to create the new entry before
+ * deleting the ldy, since we will reference the line info from this
+ * insn.
+ */
+ X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, D->NextEntry->LI);
+ DelEntry (D, D->OpIndex + 1);
+ InsertEntry (D, X, D->OpIndex + 1);
+
+ /* Replace the call to ldaidx/ldauidx. Since X is already zero, and
+ * the ptr is in the zero page location, we just need to load from
+ * the pointer, and fix X in case of ldaidx.
+ */
+ X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, D->ZPLo, 0, N->LI);
+ DelEntry (D, D->OpIndex + 2);
+ InsertEntry (D, X, D->OpIndex + 2);
+ if (Signed) {
+
+ CodeLabel* L;
+
+ /* Add sign extension - N is unused now */
+ N = CS_GetNextEntry (D->Code, D->OpIndex + 2);
+ CHECK (N != 0);
+ L = CS_GenLabel (D->Code, N);
+
+ X = NewCodeEntry (OP65_BPL, AM65_BRA, L->Name, L, X->LI);
+ InsertEntry (D, X, D->OpIndex + 3);
+
+ X = NewCodeEntry (OP65_DEX, AM65_IMP, 0, 0, X->LI);
+ InsertEntry (D, X, D->OpIndex + 4);
+ }
+
+ } else {
+
+ /* Store the value into the zeropage instead of pushing it */
+ ReplacePushByStore (D);
+
+ /* Inline the add */
+ D->IP = D->OpIndex+1;
+
+ /* clc */
+ X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+
+ /* Low byte */
+ AddOpLow (D, OP65_ADC, &D->Lhs);
+
+ /* High byte */
+ if (D->PushEntry->RI->In.RegX == 0) {
+
+ /* The high byte is the value in X plus the carry */
+ CodeLabel* L = CS_GenLabel (D->Code, D->NextEntry);
+
+ /* bcc L */
+ X = NewCodeEntry (OP65_BCC, AM65_BRA, L->Name, L, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+
+ /* inx */
+ X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+
+ } else if (D->OpEntry->RI->In.RegX == 0 &&
+ (RegValIsKnown (D->PushEntry->RI->In.RegX) ||
+ (D->Lhs.X.Flags & LI_RELOAD_Y) == 0)) {
+
+ /* The high byte is that of the first operand plus carry */
+ CodeLabel* L;
+ if (RegValIsKnown (D->PushEntry->RI->In.RegX)) {
+ /* Value of first op high byte is known */
+ const char* Arg = MakeHexArg (D->PushEntry->RI->In.RegX);
+ X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, D->OpEntry->LI);
+ } else {
+ /* Value of first op high byte is unknown. Load from ZP or
+ * original storage.
+ */
+ if (D->Lhs.X.Flags & LI_DIRECT) {
+ CodeEntry* LoadX = D->Lhs.X.LoadEntry;
+ X = NewCodeEntry (OP65_LDX, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
+ } else {
+ X = NewCodeEntry (OP65_LDX, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI);
+ }
+ }
+ InsertEntry (D, X, D->IP++);
+
+ /* bcc label */
+ L = CS_GenLabel (D->Code, D->NextEntry);
+ X = NewCodeEntry (OP65_BCC, AM65_BRA, L->Name, L, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+
+ /* inx */
+ X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+ } else {
+ /* High byte is unknown */
+ AddOpHigh (D, OP65_ADC, &D->Lhs, 1);
+ }
+ }
+
+ /* Remove the push and the call to the tosaddax function */
+ RemoveRemainders (D);
+
+ /* We changed the sequence */
+ return 1;
+}
+
+
+
+static unsigned Opt_tosandax (StackOpData* D)
+/* Optimize the tosandax sequence */
+{
+ /* Store the value into the zeropage instead of pushing it */
+ ReplacePushByStore (D);
+
+ /* Inline the and, low byte */
+ D->IP = D->OpIndex + 1;
+ AddOpLow (D, OP65_AND, &D->Lhs);
+
+ /* High byte */
+ AddOpHigh (D, OP65_AND, &D->Lhs, 1);
+
+ /* Remove the push and the call to the tosandax function */
+ RemoveRemainders (D);
+
+ /* We changed the sequence */
+ return 1;