]> git.sur5r.net Git - cc65/blobdiff - src/cc65/coptstop.c
Fixed a problem in the optimizer function that rewrites ops that use the
[cc65] / src / cc65 / coptstop.c
index 6b1ff5c506b5ddcc367b50797ef1a2d3f7dc5f76..620a73504e52d7aba8fe303dcb61605846d32af7 100644 (file)
@@ -42,6 +42,7 @@
 #include "codeent.h"
 #include "codeinfo.h"
 #include "coptstop.h"
+#include "error.h"
 
 
 
@@ -57,15 +58,18 @@ typedef enum {
   LI_DIRECT             = 0x01,         /* Direct op may be used */
   LI_RELOAD_Y           = 0x02,         /* Reload index register Y */
   LI_REMOVE             = 0x04,         /* Load may be removed */
+  LI_DUP_LOAD           = 0x08,         /* Duplicate load */
 } LI_FLAGS;
 
 /* Structure that tells us how to load the lhs values */
 typedef struct LoadRegInfo LoadRegInfo;
 struct LoadRegInfo {
+    LI_FLAGS            Flags;          /* Tells us how to load */
     int                 LoadIndex;      /* Index of load insn, -1 if invalid */
     CodeEntry*          LoadEntry;      /* The actual entry, 0 if invalid */
-    LI_FLAGS            Flags;          /* Tells us how to load */
-    unsigned char       Offs;           /* Stack offset if data is on stack */
+    int                 XferIndex;      /* Index of transfer insn  */
+    CodeEntry*          XferEntry;      /* The actual transfer entry */
+    int                 Offs;           /* Stack offset if data is on stack */
 };
 
 /* Now combined for both registers */
@@ -149,7 +153,10 @@ struct StackOpData {
 static void ClearLoadRegInfo (LoadRegInfo* RI)
 /* Clear a LoadRegInfo struct */
 {
+    RI->Flags     = LI_NONE;
     RI->LoadIndex = -1;
+    RI->XferIndex = -1;
+    RI->Offs      = 0;
 }
 
 
@@ -157,13 +164,17 @@ static void ClearLoadRegInfo (LoadRegInfo* RI)
 static void FinalizeLoadRegInfo (LoadRegInfo* RI, CodeSeg* S)
 /* Prepare a LoadRegInfo struct for use */
 {
-    /* Get the entry */
+    /* Get the entries */
     if (RI->LoadIndex >= 0) {
         RI->LoadEntry = CS_GetEntry (S, RI->LoadIndex);
     } else {
         RI->LoadEntry = 0;
     }
-    RI->Flags = LI_NONE;
+    if (RI->XferIndex >= 0) {
+        RI->XferEntry = CS_GetEntry (S, RI->XferIndex);
+    } else {
+        RI->XferEntry = 0;
+    }
 }
 
 
@@ -178,7 +189,7 @@ static void ClearLoadInfo (LoadInfo* LI)
 
 
 
-static void AdjustLoadRegInfo (LoadRegInfo* RI, int DelIndex, int Change)
+static void AdjustLoadRegInfo (LoadRegInfo* RI, int Index, int Change)
 /* Adjust a load register info struct after deleting or inserting an entry
  * with a given index
  */
@@ -186,18 +197,28 @@ static void AdjustLoadRegInfo (LoadRegInfo* RI, int DelIndex, int Change)
     CHECK (abs (Change) == 1);
     if (Change < 0) {
         /* Deletion */
-        if (DelIndex < RI->LoadIndex) {
+        if (Index < RI->LoadIndex) {
             --RI->LoadIndex;
-        } else if (DelIndex == RI->LoadIndex) {
+        } else if (Index == RI->LoadIndex) {
             /* Has been removed */
             RI->LoadIndex = -1;
             RI->LoadEntry = 0;
         }
+        if (Index < RI->XferIndex) {
+            --RI->XferIndex;
+        } else if (Index == RI->XferIndex) {
+            /* Has been removed */
+            RI->XferIndex = -1;
+            RI->XferEntry = 0;
+        }
     } else {
         /* Insertion */
-        if (DelIndex <= RI->LoadIndex) {
+        if (Index <= RI->LoadIndex) {
             ++RI->LoadIndex;
         }
+        if (Index <= RI->XferIndex) {
+            ++RI->XferIndex;
+        }
     }
 }
 
@@ -214,12 +235,12 @@ static void FinalizeLoadInfo (LoadInfo* LI, CodeSeg* S)
 
 
 
-static void AdjustLoadInfo (LoadInfo* LI, int DelIndex, int Change)
+static void AdjustLoadInfo (LoadInfo* LI, int Index, int Change)
 /* Adjust a load info struct after deleting entry with a given index */
 {
-    AdjustLoadRegInfo (&LI->A, DelIndex, Change);
-    AdjustLoadRegInfo (&LI->X, DelIndex, Change);
-    AdjustLoadRegInfo (&LI->Y, DelIndex, Change);
+    AdjustLoadRegInfo (&LI->A, Index, Change);
+    AdjustLoadRegInfo (&LI->X, Index, Change);
+    AdjustLoadRegInfo (&LI->Y, Index, Change);
 }
 
 
@@ -228,37 +249,114 @@ static void TrackLoads (LoadInfo* LI, CodeEntry* E, int I)
 /* Track loads for a code entry */
 {
     if (E->Info & OF_LOAD) {
+
+        LoadRegInfo* RI = 0;
+
+        /* Determine, which register was loaded */
         if (E->Chg & REG_A) {
-            LI->A.LoadIndex = I;
+            RI = &LI->A;
+        } else if (E->Chg & REG_X) {
+            RI = &LI->X;
+        } else if (E->Chg & REG_Y) {
+            RI = &LI->Y;
         }
-        if (E->Chg & REG_X) {
-            LI->X.LoadIndex = I;
+        CHECK (RI != 0);
+
+        /* If we had a load or xfer op before, this is a duplicate load which
+         * can cause problems if it encountered between the pushax and the op,
+         * so remember it.
+         */
+        if (RI->LoadIndex >= 0 || RI->XferIndex >= 0) {
+            RI->Flags |= LI_DUP_LOAD;
         }
-        if (E->Chg & REG_Y) {
-            LI->Y.LoadIndex = I;
+
+        /* Remember the load */
+        RI->LoadIndex = I;
+        RI->XferIndex = -1;
+
+        /* Set load flags */
+        RI->Flags    &= ~(LI_DIRECT | LI_RELOAD_Y);
+        if (E->AM == AM65_IMM || E->AM == AM65_ZP || E->AM == AM65_ABS) {
+            /* These insns are all ok and replaceable */
+            RI->Flags |= LI_DIRECT;
+        } else if (E->AM == AM65_ZP_INDY &&
+                   RegValIsKnown (E->RI->In.RegY) &&
+                   strcmp (E->Arg, "sp") == 0) {
+            /* A load from the stack with known offset is also ok, but in this
+             * case we must reload the index register later. Please note that
+             * a load indirect via other zero page locations is not ok, since
+             * these locations may change between the push and the actual
+             * operation.
+             */
+            RI->Offs  = (unsigned char) E->RI->In.RegY;
+            RI->Flags |= (LI_DIRECT | LI_RELOAD_Y);
         }
+
+
     } else if (E->Info & OF_XFR) {
+
+        /* Determine source and target of the transfer and handle the TSX insn */
+        LoadRegInfo* Src;
+        LoadRegInfo* Tgt;
         switch (E->OPC) {
-            case OP65_TAX: LI->X.LoadIndex = LI->A.LoadIndex; break;
-            case OP65_TAY: LI->Y.LoadIndex = LI->A.LoadIndex; break;
-            case OP65_TXA: LI->A.LoadIndex = LI->X.LoadIndex; break;
-            case OP65_TYA: LI->A.LoadIndex = LI->Y.LoadIndex; break;
-            default:                                          break;
+            case OP65_TAX:      Src = &LI->A; Tgt = &LI->X; break;
+            case OP65_TAY:      Src = &LI->A; Tgt = &LI->Y; break;
+            case OP65_TXA:      Src = &LI->X; Tgt = &LI->A; break;
+            case OP65_TYA:      Src = &LI->Y; Tgt = &LI->A; break;
+            case OP65_TSX:      ClearLoadRegInfo (&LI->X);  return;
+            case OP65_TXS:                                  return;
+            default:            Internal ("Unknown XFR insn in TrackLoads");
+        }
+
+        /* If we had a load or xfer op before, this is a duplicate load which
+         * can cause problems if it encountered between the pushax and the op,
+         * so remember it.
+         */
+        if (Tgt->LoadIndex >= 0 || Tgt->XferIndex >= 0) {
+            Tgt->Flags |= LI_DUP_LOAD;
+        }
+
+        /* Transfer the data */
+        Tgt->LoadIndex  = Src->LoadIndex;
+        Tgt->XferIndex  = I;
+        Tgt->Offs       = Src->Offs;
+        Tgt->Flags     &= ~(LI_DIRECT | LI_RELOAD_Y);
+        Tgt->Flags     |= Src->Flags & (LI_DIRECT | LI_RELOAD_Y);
+
+    } else if (CE_IsCallTo (E, "ldaxysp") && RegValIsKnown (E->RI->In.RegY)) {
+
+        /* If we had a load or xfer op before, this is a duplicate load which
+         * can cause problems if it encountered between the pushax and the op,
+         * so remember it for both registers involved.
+         */
+        if (LI->A.LoadIndex >= 0 || LI->A.XferIndex >= 0) {
+            LI->A.Flags |= LI_DUP_LOAD;
         }
-    } else if (CE_IsCallTo (E, "ldaxysp")) {
+        if (LI->X.LoadIndex >= 0 || LI->X.XferIndex >= 0) {
+            LI->X.Flags |= LI_DUP_LOAD;
+        }
+
         /* Both registers set, Y changed */
         LI->A.LoadIndex = I;
+        LI->A.XferIndex = -1;
+        LI->A.Flags    |= (LI_DIRECT | LI_RELOAD_Y);
+        LI->A.Offs      = (unsigned char) E->RI->In.RegY - 1;
+
         LI->X.LoadIndex = I;
-        LI->Y.LoadIndex = -1;
+        LI->X.XferIndex = -1;
+        LI->X.Flags    |= (LI_DIRECT | LI_RELOAD_Y);
+        LI->X.Offs      = (unsigned char) E->RI->In.RegY;
+
+        ClearLoadRegInfo (&LI->Y);
     } else {
         if (E->Chg & REG_A) {
-            LI->A.LoadIndex = -1;
+            ClearLoadRegInfo (&LI->A);
         }
         if (E->Chg & REG_X) {
-            LI->X.LoadIndex = -1;
+            ClearLoadRegInfo (&LI->X);
         }
         if (E->Chg & REG_Y) {
-            LI->Y.LoadIndex = -1;
+            ClearLoadRegInfo (&LI->Y);
         }
     }
 }
@@ -266,83 +364,11 @@ static void TrackLoads (LoadInfo* LI, CodeEntry* E, int I)
 
 
 /*****************************************************************************/
-/*                                         Helpers                                  */
+/*                                         Helpers                                  */
 /*****************************************************************************/
 
 
 
-static void AdjustStackOffset (StackOpData* D, unsigned Offs)
-/* Adjust the offset for all stack accesses in the range PushIndex to OpIndex.
- * OpIndex is adjusted according to the insertions.
- */
-{
-    /* Walk over all entries */
-    int I = D->PushIndex + 1;
-    while (I < D->OpIndex) {
-
-       CodeEntry* E = CS_GetEntry (D->Code, I);
-
-        int NeedCorrection = 0;
-       if ((E->Use & REG_SP) != 0) {
-
-           /* Check for some things that should not happen */
-           CHECK (E->AM == AM65_ZP_INDY || E->RI->In.RegY >= (short) Offs);
-           CHECK (strcmp (E->Arg, "sp") == 0);
-
-            /* We need to correct this one */
-            NeedCorrection = 1;
-
-        } else if (CE_IsCallTo (E, "ldaxysp")) {
-
-            /* We need to correct this one */
-            NeedCorrection = 1;
-
-        }
-
-        if (NeedCorrection) {
-
-           /* Get the code entry before this one. If it's a LDY, adjust the
-            * value.
-            */
-           CodeEntry* P = CS_GetPrevEntry (D->Code, I);
-           if (P && P->OPC == OP65_LDY && CE_IsConstImm (P)) {
-
-               /* The Y load is just before the stack access, adjust it */
-               CE_SetNumArg (P, P->Num - Offs);
-
-           } else {
-
-               /* Insert a new load instruction before the stack access */
-               const char* Arg = MakeHexArg (E->RI->In.RegY - Offs);
-               CodeEntry* X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
-               CS_InsertEntry (D->Code, X, I++);
-
-               /* One more inserted entries */
-               ++D->OpIndex;
-
-           }
-
-            /* If we need the value of Y later, be sure to reload it */
-            if (RegYUsed (D->Code, I+1)) {
-               const char* Arg = MakeHexArg (E->RI->In.RegY);
-               CodeEntry* X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
-               CS_InsertEntry (D->Code, X, I+1);
-
-               /* One more inserted entries */
-               ++D->OpIndex;
-
-               /* Skip this instruction in the next round */
-               ++I;
-            }
-       }
-
-       /* Next entry */
-       ++I;
-    }
-}
-
-
-
 static void InsertEntry (StackOpData* D, CodeEntry* E, int Index)
 /* Insert a new entry. Depending on Index, D->PushIndex and D->OpIndex will
  * be adjusted by this function.
@@ -393,64 +419,78 @@ static void DelEntry (StackOpData* D, int Index)
 
 
 
-static void CheckOneDirectOp (LoadRegInfo* LI, unsigned char Offs)
-/* Check if the given entry is a lda instruction with an addressing mode
- * that allows us to replace it by another operation (like ora). If so, we may
- * use this location for the or and must not save the value in the zero
- * page location.
+static void AdjustStackOffset (StackOpData* D, unsigned Offs)
+/* Adjust the offset for all stack accesses in the range PushIndex to OpIndex.
+ * OpIndex is adjusted according to the insertions.
  */
 {
-    /* Get the load entry */
-    CodeEntry* E = LI->LoadEntry;
-    if (E == 0) {
-        /* No load insn */
-        return;
-    }
+    /* Walk over all entries */
+    int I = D->PushIndex + 1;
+    while (I < D->OpIndex) {
+
+       CodeEntry* E = CS_GetEntry (D->Code, I);
+
+        int NeedCorrection = 0;
+       if ((E->Use & REG_SP) != 0) {
+
+           /* Check for some things that should not happen */
+           CHECK (E->AM == AM65_ZP_INDY || E->RI->In.RegY >= (short) Offs);
+           CHECK (strcmp (E->Arg, "sp") == 0);
+
+            /* We need to correct this one */
+            NeedCorrection = 1;
+
+        } else if (CE_IsCallTo (E, "ldaxysp")) {
+
+            /* We need to correct this one */
+            NeedCorrection = 1;
 
-    /* Check the load entry */
-    if (E) {
-        /* Must check the call first since addressing mode is ABS, so second
-         * "if" will catch otherwise.
-         */
-        if (CE_IsCallTo (E, "ldaxysp")) {
-            /* Same as single loads from stack. Since we must distinguish
-             * between A and X here, the necessary offset is passed to the
-             * function as a parameter.
-             */
-            LI->Offs = (unsigned char) E->RI->In.RegY - Offs;
-            LI->Flags |= (LI_DIRECT | LI_RELOAD_Y);
-        } else if (E->AM == AM65_IMM || E->AM == AM65_ZP || E->AM == AM65_ABS) {
-            /* These insns are all ok and replaceable */
-            LI->Flags |= LI_DIRECT;
-        } else if (E->AM == AM65_ZP_INDY &&
-                   RegValIsKnown (E->RI->In.RegY) &&
-                   strcmp (E->Arg, "sp") == 0) {
-            /* A load from the stack with known offset is also ok, but in this
-             * case we must reload the index register later. Please note that
-             * a load indirect via other zero page locations is not ok, since
-             * these locations may change between the push and the actual
-             * operation.
-             */
-            LI->Offs  = (unsigned char) E->RI->In.RegY;
-            LI->Flags |= (LI_DIRECT | LI_RELOAD_Y);
         }
-    }
-}
 
+        if (NeedCorrection) {
 
+           /* Get the code entry before this one. If it's a LDY, adjust the
+            * value.
+            */
+           CodeEntry* P = CS_GetPrevEntry (D->Code, I);
+           if (P && P->OPC == OP65_LDY && CE_IsConstImm (P)) {
 
-static void CheckDirectOp (StackOpData* D)
-/* Check if the given entry is a lda instruction with an addressing mode
- * that allows us to replace it by another operation (like ora). If so, we may
- * use this location for the or and must not save the value in the zero
- * page location.
- */
-{
-    /* Check flags for all load instructions */
-    CheckOneDirectOp (&D->Lhs.A, 1);
-    CheckOneDirectOp (&D->Lhs.X, 0);
-    CheckOneDirectOp (&D->Rhs.A, 1);
-    CheckOneDirectOp (&D->Rhs.X, 0);
+               /* The Y load is just before the stack access, adjust it */
+               CE_SetNumArg (P, P->Num - Offs);
+
+           } else {
+
+               /* Insert a new load instruction before the stack access */
+               const char* Arg = MakeHexArg (E->RI->In.RegY - Offs);
+               CodeEntry* X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
+               InsertEntry (D, X, I++);
+
+           }
+
+            /* If we need the value of Y later, be sure to reload it */
+            if (RegYUsed (D->Code, I+1)) {
+               const char* Arg = MakeHexArg (E->RI->In.RegY);
+               CodeEntry* X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
+               InsertEntry (D, X, I+1);
+
+               /* Skip this instruction in the next round */
+               ++I;
+            }
+       }
+
+       /* Next entry */
+       ++I;
+    }
+
+    /* If we have rhs load insns that load from stack, we'll have to adjust
+     * the offsets for these also.
+     */
+    if (D->Rhs.A.Flags & LI_RELOAD_Y) {
+        D->Rhs.A.Offs -= Offs;
+    }
+    if (D->Rhs.X.Flags & LI_RELOAD_Y) {
+        D->Rhs.X.Offs -= Offs;
+    }
 }
 
 
@@ -493,7 +533,7 @@ static void ReplacePushByStore (StackOpData* D)
 
 
 
-static void AddOpLow (StackOpData* D, opc_t OPC)
+static void AddOpLow (StackOpData* D, opc_t OPC, LoadInfo* LI)
 /* Add an op for the low byte of an operator. This function honours the
  * OP_DIRECT and OP_RELOAD_Y flags and generates the necessary instructions.
  * All code is inserted at the current insertion point.
@@ -501,21 +541,21 @@ static void AddOpLow (StackOpData* D, opc_t OPC)
 {
     CodeEntry* X;
 
-    if ((D->Lhs.A.Flags & LI_DIRECT) != 0) {
+    if ((LI->A.Flags & LI_DIRECT) != 0) {
                /* Op with a variable location. If the location is on the stack, we
          * need to reload the Y register.
          */
-        if ((D->Lhs.A.Flags & LI_RELOAD_Y) == 0) {
+        if ((LI->A.Flags & LI_RELOAD_Y) == 0) {
 
             /* opc ... */
-            CodeEntry* LoadA = D->Lhs.A.LoadEntry;
+            CodeEntry* LoadA = LI->A.LoadEntry;
             X = NewCodeEntry (OPC, LoadA->AM, LoadA->Arg, 0, D->OpEntry->LI);
             InsertEntry (D, X, D->IP++);
 
         } else {
 
             /* ldy #offs */
-            const char* Arg = MakeHexArg (D->Lhs.A.Offs);
+            const char* Arg = MakeHexArg (LI->A.Offs);
             X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
             InsertEntry (D, X, D->IP++);
 
@@ -526,7 +566,7 @@ static void AddOpLow (StackOpData* D, opc_t OPC)
         }
 
         /* In both cases, we can remove the load */
-        D->Lhs.A.Flags |= LI_REMOVE;
+        LI->A.Flags |= LI_REMOVE;
 
     } else {
 
@@ -539,7 +579,7 @@ static void AddOpLow (StackOpData* D, opc_t OPC)
 
 
 
-static void AddOpHigh (StackOpData* D, opc_t OPC)
+static void AddOpHigh (StackOpData* D, opc_t OPC, LoadInfo* LI, int KeepResult)
 /* Add an op for the high byte of an operator. Special cases (constant values
  * or similar) have to be checked separately, the function covers only the
  * generic case. Code is inserted at the insertion point.
@@ -547,27 +587,29 @@ static void AddOpHigh (StackOpData* D, opc_t OPC)
 {
     CodeEntry* X;
 
-    /* pha */
-    X = NewCodeEntry (OP65_PHA, AM65_IMP, 0, 0, D->OpEntry->LI);
-    InsertEntry (D, X, D->IP++);
+    if (KeepResult) {
+       /* pha */
+       X = NewCodeEntry (OP65_PHA, AM65_IMP, 0, 0, D->OpEntry->LI);
+       InsertEntry (D, X, D->IP++);
+    }
 
     /* txa */
     X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, D->OpEntry->LI);
     InsertEntry (D, X, D->IP++);
 
-    if ((D->Lhs.X.Flags & LI_DIRECT) != 0) {
+    if ((LI->X.Flags & LI_DIRECT) != 0) {
 
-        if ((D->Lhs.X.Flags & LI_RELOAD_Y) == 0) {
+        if ((LI->X.Flags & LI_RELOAD_Y) == 0) {
 
             /* opc xxx */
-            CodeEntry* LoadX = D->Lhs.X.LoadEntry;
-           X = NewCodeEntry (OPC, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
+            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 (D->Lhs.X.Offs);
+            const char* Arg = MakeHexArg (LI->X.Offs);
             X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
             InsertEntry (D, X, D->IP++);
 
@@ -577,7 +619,7 @@ static void AddOpHigh (StackOpData* D, opc_t OPC)
         }
 
         /* In both cases, we can remove the load */
-        D->Lhs.X.Flags |= LI_REMOVE;
+        LI->X.Flags |= LI_REMOVE;
 
     } else {
         /* opc zphi */
@@ -585,13 +627,15 @@ static void AddOpHigh (StackOpData* D, opc_t OPC)
         InsertEntry (D, X, D->IP++);
     }
 
-    /* tax */
-    X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 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++);
+       /* pla */
+       X = NewCodeEntry (OP65_PLA, AM65_IMP, 0, 0, D->OpEntry->LI);
+       InsertEntry (D, X, D->IP++);
+    }
 }
 
 
@@ -602,11 +646,21 @@ static void RemoveRegLoads (StackOpData* D, LoadInfo* LI)
     /* Both registers may be loaded with one insn, but DelEntry will in this
      * case clear the other one.
      */
-    if (LI->A.LoadIndex >= 0 && (LI->A.Flags & LI_REMOVE)) {
-        DelEntry (D, LI->A.LoadIndex);
+    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.LoadIndex >= 0 && (LI->X.Flags & LI_REMOVE)) {
-        DelEntry (D, LI->X.LoadIndex);
+    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);
+        }
     }
 }
 
@@ -667,8 +721,119 @@ static int IsRegVar (StackOpData* D)
 
 
 
+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 if possible */
+/* Optimize the __bzero sequence */
 {
     CodeEntry*  X;
     const char* Arg;
@@ -762,7 +927,7 @@ static unsigned Opt___bzero (StackOpData* D)
 
 
 static unsigned Opt_staspidx (StackOpData* D)
-/* Optimize the staspidx sequence if possible */
+/* Optimize the staspidx sequence */
 {
     CodeEntry* X;
 
@@ -787,7 +952,7 @@ static unsigned Opt_staspidx (StackOpData* D)
 
 
 static unsigned Opt_staxspidx (StackOpData* D)
-/* Optimize the staxspidx sequence if possible */
+/* Optimize the staxspidx sequence */
 {
     CodeEntry* X;
 
@@ -843,7 +1008,7 @@ static unsigned Opt_staxspidx (StackOpData* D)
 
 
 static unsigned Opt_tosaddax (StackOpData* D)
-/* Optimize the tosaddax sequence if possible */
+/* Optimize the tosaddax sequence */
 {
     CodeEntry*  X;
     CodeEntry*  N;
@@ -920,17 +1085,26 @@ static unsigned Opt_tosaddax (StackOpData* D)
         InsertEntry (D, X, D->IP++);
 
         /* Low byte */
-        AddOpLow (D, OP65_ADC);
+        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) {
+
+        } 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)) {
@@ -938,8 +1112,15 @@ static unsigned Opt_tosaddax (StackOpData* D)
                 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 */
-                X = NewCodeEntry (OP65_LDX, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI);
+                /* 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++);
 
@@ -953,7 +1134,7 @@ static unsigned Opt_tosaddax (StackOpData* D)
             InsertEntry (D, X, D->IP++);
         } else {
             /* High byte is unknown */
-            AddOpHigh (D, OP65_ADC);
+            AddOpHigh (D, OP65_ADC, &D->Lhs, 1);
         }
     }
 
@@ -967,26 +1148,17 @@ static unsigned Opt_tosaddax (StackOpData* D)
 
 
 static unsigned Opt_tosandax (StackOpData* D)
-/* Optimize the tosandax sequence if possible */
+/* Optimize the tosandax sequence */
 {
-    CodeEntry*  X;
-
     /* 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);
+    AddOpLow (D, OP65_AND, &D->Lhs);
 
     /* High byte */
-    if (D->PushEntry->RI->In.RegX == 0 || D->OpEntry->RI->In.RegX == 0) {
-       /* The high byte is zero */
-               X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, D->OpEntry->LI);
-               InsertEntry (D, X, D->IP++);
-    } else {
-       /* High byte is unknown */
-        AddOpHigh (D, OP65_AND);
-    }
+    AddOpHigh (D, OP65_AND, &D->Lhs, 1);
 
     /* Remove the push and the call to the tosandax function */
     RemoveRemainders (D);
@@ -997,8 +1169,16 @@ static unsigned Opt_tosandax (StackOpData* D)
 
 
 
+static unsigned Opt_toseqax (StackOpData* D)
+/* Optimize the toseqax sequence */
+{
+    return Opt_toseqax_tosneax (D, "booleq");
+}
+
+
+
 static unsigned Opt_tosgeax (StackOpData* D)
-/* Optimize the tosgeax sequence if possible. */
+/* Optimize the tosgeax sequence */
 {
     CodeEntry*  X;
     CodeLabel* L;
@@ -1007,60 +1187,13 @@ static unsigned Opt_tosgeax (StackOpData* D)
     D->IP = D->OpIndex+1;
 
     /* Must be true because of OP_RHS_LOAD */
-    CHECK ((D->Rhs.A.Flags & LI_DIRECT) != 0);
-
-    /* If the location is on the stack, we need to reload the Y register. */
-    if ((D->Rhs.A.Flags & LI_RELOAD_Y) == 0) {
-
-        /* cmp ... */
-        CodeEntry* LoadA = D->Rhs.A.LoadEntry;
-        X = NewCodeEntry (OP65_CMP, LoadA->AM, LoadA->Arg, 0, D->OpEntry->LI);
-        InsertEntry (D, X, D->IP++);
-
-    } else {
-
-        /* ldy #offs */
-        const char* Arg = MakeHexArg (D->Rhs.A.Offs);
-        X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
-        InsertEntry (D, X, D->IP++);
-
-        /* cmp (sp),y */
-        X = NewCodeEntry (OP65_CMP, AM65_ZP_INDY, "sp", 0, D->OpEntry->LI);
-        InsertEntry (D, X, D->IP++);
-    }
-
-    /* In both cases, we can remove the load */
-    D->Rhs.A.Flags |= LI_REMOVE;
-
-    /* txa */
-    X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, D->OpEntry->LI);
-    InsertEntry (D, X, D->IP++);
-
-    /* Must be true because of OP_RHS_LOAD */
-    CHECK ((D->Rhs.X.Flags & LI_DIRECT) != 0);
-
-    /* If the location is on the stack, we need to reload the Y register. */
-    if ((D->Rhs.X.Flags & LI_RELOAD_Y) == 0) {
-
-        /* sbc ... */
-        CodeEntry* LoadX = D->Rhs.X.LoadEntry;
-        X = NewCodeEntry (OP65_SBC, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
-        InsertEntry (D, X, D->IP++);
-
-    } else {
-
-        /* ldy #offs */
-        const char* Arg = MakeHexArg (D->Rhs.X.Offs);
-        X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
-        InsertEntry (D, X, D->IP++);
+    CHECK ((D->Rhs.A.Flags & D->Rhs.X.Flags & LI_DIRECT) != 0);
 
-        /* sbc (sp),y */
-        X = NewCodeEntry (OP65_SBC, AM65_ZP_INDY, "sp", 0, D->OpEntry->LI);
-        InsertEntry (D, X, D->IP++);
-    }
+    /* Add code for low operand */
+    AddOpLow (D, OP65_CMP, &D->Rhs);
 
-    /* In both cases, we can remove the load */
-    D->Rhs.X.Flags |= LI_REMOVE;
+    /* Add code for high operand */
+    AddOpHigh (D, OP65_SBC, &D->Rhs, 0);
 
     /* eor #$80 */
     X = NewCodeEntry (OP65_EOR, AM65_IMM, "$80", 0, D->OpEntry->LI);
@@ -1098,7 +1231,7 @@ static unsigned Opt_tosgeax (StackOpData* D)
 
 
 static unsigned Opt_tosltax (StackOpData* D)
-/* Optimize the tosltax sequence if possible. */
+/* Optimize the tosltax sequence */
 {
     CodeEntry*  X;
     CodeLabel* L;
@@ -1108,60 +1241,13 @@ static unsigned Opt_tosltax (StackOpData* D)
     D->IP = D->OpIndex+1;
 
     /* Must be true because of OP_RHS_LOAD */
-    CHECK ((D->Rhs.A.Flags & LI_DIRECT) != 0);
-
-    /* If the location is on the stack, we need to reload the Y register. */
-    if ((D->Rhs.A.Flags & LI_RELOAD_Y) == 0) {
-
-        /* cmp ... */
-        CodeEntry* LoadA = D->Rhs.A.LoadEntry;
-        X = NewCodeEntry (OP65_CMP, LoadA->AM, LoadA->Arg, 0, D->OpEntry->LI);
-        InsertEntry (D, X, D->IP++);
-
-    } else {
-
-        /* ldy #offs */
-        const char* Arg = MakeHexArg (D->Rhs.A.Offs);
-        X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
-        InsertEntry (D, X, D->IP++);
-
-        /* cmp (sp),y */
-        X = NewCodeEntry (OP65_CMP, AM65_ZP_INDY, "sp", 0, D->OpEntry->LI);
-        InsertEntry (D, X, D->IP++);
-    }
-
-    /* In both cases, we can remove the load */
-    D->Rhs.A.Flags |= LI_REMOVE;
-
-    /* txa */
-    X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, D->OpEntry->LI);
-    InsertEntry (D, X, D->IP++);
-
-    /* Must be true because of OP_RHS_LOAD */
-    CHECK ((D->Rhs.X.Flags & LI_DIRECT) != 0);
+    CHECK ((D->Rhs.A.Flags & D->Rhs.X.Flags & LI_DIRECT) != 0);
 
-    /* If the location is on the stack, we need to reload the Y register. */
-    if ((D->Rhs.X.Flags & LI_RELOAD_Y) == 0) {
+    /* Add code for low operand */
+    AddOpLow (D, OP65_CMP, &D->Rhs);
 
-        /* sbc ... */
-        CodeEntry* LoadX = D->Rhs.X.LoadEntry;
-        X = NewCodeEntry (OP65_SBC, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
-        InsertEntry (D, X, D->IP++);
-
-    } else {
-
-        /* ldy #offs */
-        const char* Arg = MakeHexArg (D->Rhs.X.Offs);
-        X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
-        InsertEntry (D, X, D->IP++);
-
-        /* sbc (sp),y */
-        X = NewCodeEntry (OP65_SBC, AM65_ZP_INDY, "sp", 0, D->OpEntry->LI);
-        InsertEntry (D, X, D->IP++);
-    }
-
-    /* In both cases, we can remove the load */
-    D->Rhs.X.Flags |= LI_REMOVE;
+    /* Add code for high operand */
+    AddOpHigh (D, OP65_SBC, &D->Rhs, 0);
 
     /* eor #$80 */
     X = NewCodeEntry (OP65_EOR, AM65_IMM, "$80", 0, D->OpEntry->LI);
@@ -1198,30 +1284,26 @@ static unsigned Opt_tosltax (StackOpData* D)
 
 
 
-static unsigned Opt_tosorax (StackOpData* D)
-/* Optimize the tosorax sequence if possible */
+static unsigned Opt_tosneax (StackOpData* D)
+/* Optimize the tosneax sequence */
 {
-    CodeEntry*  X;
+    return Opt_toseqax_tosneax (D, "boolne");
+}
+
 
+
+static unsigned Opt_tosorax (StackOpData* D)
+/* Optimize the tosorax sequence */
+{
     /* Store the value into the zeropage instead of pushing it */
     ReplacePushByStore (D);
 
     /* Inline the or, low byte */
     D->IP = D->OpIndex + 1;
-    AddOpLow (D, OP65_ORA);
+    AddOpLow (D, OP65_ORA, &D->Lhs);
 
     /* High byte */
-    if (RegValIsKnown (D->PushEntry->RI->In.RegX) &&
-        RegValIsKnown (D->OpEntry->RI->In.RegX)) {
-               /* Both values known, precalculate the result */
-        unsigned char Result = D->PushEntry->RI->In.RegX | D->OpEntry->RI->In.RegX;
-        const char* Arg = MakeHexArg (Result);
-        X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, D->OpEntry->LI);
-        InsertEntry (D, X, D->IP++);
-    } else if (D->PushEntry->RI->In.RegX != 0) {
-               /* High byte is unknown */
-        AddOpHigh (D, OP65_ORA);
-    }
+    AddOpHigh (D, OP65_ORA, &D->Lhs, 1);
 
     /* Remove the push and the call to the tosorax function */
     RemoveRemainders (D);
@@ -1233,9 +1315,7 @@ static unsigned Opt_tosorax (StackOpData* D)
 
 
 static unsigned Opt_tossubax (StackOpData* D)
-/* Optimize the tossubax sequence if possible. Note: subtraction is not
- * commutative!
- */
+/* Optimize the tossubax sequence. Note: subtraction is not commutative! */
 {
     CodeEntry*  X;
 
@@ -1248,74 +1328,97 @@ static unsigned Opt_tossubax (StackOpData* D)
     InsertEntry (D, X, D->IP++);
 
     /* Must be true because of OP_RHS_LOAD */
-    CHECK ((D->Rhs.A.Flags & LI_DIRECT) != 0);
+    CHECK ((D->Rhs.A.Flags & D->Rhs.X.Flags & LI_DIRECT) != 0);
 
-    /* If the location is on the stack, we need to reload the Y register. */
-    if ((D->Rhs.A.Flags & LI_RELOAD_Y) == 0) {
+    /* Add code for low operand */
+    AddOpLow (D, OP65_SBC, &D->Rhs);
 
-        /* sbc ... */
-        CodeEntry* LoadA = D->Rhs.A.LoadEntry;
-        X = NewCodeEntry (OP65_SBC, LoadA->AM, LoadA->Arg, 0, D->OpEntry->LI);
-        InsertEntry (D, X, D->IP++);
+    /* Add code for high operand */
+    AddOpHigh (D, OP65_SBC, &D->Rhs, 1);
 
-    } else {
+    /* Remove the push and the call to the tossubax function */
+    RemoveRemainders (D);
 
-        /* ldy #offs */
-        const char* Arg = MakeHexArg (D->Rhs.A.Offs);
-        X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
-        InsertEntry (D, X, D->IP++);
+    /* We changed the sequence */
+    return 1;
+}
 
-        /* sbc (sp),y */
-        X = NewCodeEntry (OP65_SBC, AM65_ZP_INDY, "sp", 0, D->OpEntry->LI);
-        InsertEntry (D, X, D->IP++);
-    }
 
-    /* In both cases, we can remove the load */
-    D->Rhs.A.Flags |= LI_REMOVE;
 
-    /* pha */
-    X = NewCodeEntry (OP65_PHA, AM65_IMP, 0, 0, D->OpEntry->LI);
+static unsigned Opt_tosugeax (StackOpData* D)
+/* Optimize the tosugeax sequence */
+{
+    CodeEntry*  X;
+
+
+    /* Inline the sbc */
+    D->IP = D->OpIndex+1;
+
+    /* Must be true because of OP_RHS_LOAD */
+    CHECK ((D->Rhs.A.Flags & D->Rhs.X.Flags & LI_DIRECT) != 0);
+
+    /* Add code for low operand */
+    AddOpLow (D, OP65_CMP, &D->Rhs);
+
+    /* Add code for high operand */
+    AddOpHigh (D, OP65_SBC, &D->Rhs, 0);
+
+    /* lda #$00 */
+    X = NewCodeEntry (OP65_LDA, AM65_IMM, "$00", 0, D->OpEntry->LI);
     InsertEntry (D, X, D->IP++);
 
-    /* txa */
-    X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, D->OpEntry->LI);
+    /* ldx #$00 */
+    X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, D->OpEntry->LI);
     InsertEntry (D, X, D->IP++);
 
-    /* Must be true because of OP_RHS_LOAD */
-    CHECK ((D->Rhs.X.Flags & LI_DIRECT) != 0);
+    /* rol a */
+    X = NewCodeEntry (OP65_ROL, AM65_ACC, "a", 0, D->OpEntry->LI);
+    InsertEntry (D, X, D->IP++);
 
-    /* If the location is on the stack, we need to reload the Y register. */
-    if ((D->Rhs.X.Flags & LI_RELOAD_Y) == 0) {
+    /* Remove the push and the call to the tosugeax function */
+    RemoveRemainders (D);
 
-        /* sbc ... */
-        CodeEntry* LoadX = D->Rhs.X.LoadEntry;
-        X = NewCodeEntry (OP65_SBC, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
-        InsertEntry (D, X, D->IP++);
+    /* We changed the sequence */
+    return 1;
+}
 
-    } else {
 
-        /* ldy #offs */
-        const char* Arg = MakeHexArg (D->Rhs.X.Offs);
-        X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
-        InsertEntry (D, X, D->IP++);
 
-        /* sbc (sp),y */
-        X = NewCodeEntry (OP65_SBC, AM65_ZP_INDY, "sp", 0, D->OpEntry->LI);
-        InsertEntry (D, X, D->IP++);
-    }
+static unsigned Opt_tosugtax (StackOpData* D)
+/* Optimize the tosugtax sequence */
+{
+    CodeEntry*  X;
 
-    /* In both cases, we can remove the load */
-    D->Rhs.X.Flags |= LI_REMOVE;
 
-    /* tax */
-    X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, D->OpEntry->LI);
+    /* Inline the sbc */
+    D->IP = D->OpIndex+1;
+
+    /* Must be true because of OP_RHS_LOAD */
+    CHECK ((D->Rhs.A.Flags & D->Rhs.X.Flags & LI_DIRECT) != 0);
+
+    /* sec */
+    X = NewCodeEntry (OP65_SEC, AM65_IMP, 0, 0, D->OpEntry->LI);
     InsertEntry (D, X, D->IP++);
 
-    /* pla */
-    X = NewCodeEntry (OP65_PLA, AM65_IMP, 0, 0, D->OpEntry->LI);
+    /* Add code for low operand */
+    AddOpLow (D, OP65_SBC, &D->Rhs);
+
+    /* We need the zero flag, so remember the immediate result */
+    X = NewCodeEntry (OP65_STA, AM65_ZP, "tmp1", 0, D->OpEntry->LI);
     InsertEntry (D, X, D->IP++);
 
-    /* Remove the push and the call to the tossubax function */
+    /* Add code for high operand */
+    AddOpHigh (D, OP65_SBC, &D->Rhs, 0);
+
+    /* Set Z flag */
+    X = NewCodeEntry (OP65_ORA, AM65_ZP, "tmp1", 0, D->OpEntry->LI);
+    InsertEntry (D, X, D->IP++);
+
+    /* Transform to boolean */
+    X = NewCodeEntry (OP65_JSR, AM65_ABS, "boolugt", 0, D->OpEntry->LI);
+    InsertEntry (D, X, D->IP++);
+
+    /* Remove the push and the call to the operator function */
     RemoveRemainders (D);
 
     /* We changed the sequence */
@@ -1324,8 +1427,8 @@ static unsigned Opt_tossubax (StackOpData* D)
 
 
 
-static unsigned Opt_tosugeax (StackOpData* D)
-/* Optimize the tosugeax sequence if possible. */
+static unsigned Opt_tosuleax (StackOpData* D)
+/* Optimize the tosuleax sequence */
 {
     CodeEntry*  X;
 
@@ -1334,74 +1437,62 @@ static unsigned Opt_tosugeax (StackOpData* D)
     D->IP = D->OpIndex+1;
 
     /* Must be true because of OP_RHS_LOAD */
-    CHECK ((D->Rhs.A.Flags & LI_DIRECT) != 0);
-
-    /* If the location is on the stack, we need to reload the Y register. */
-    if ((D->Rhs.A.Flags & LI_RELOAD_Y) == 0) {
+    CHECK ((D->Rhs.A.Flags & D->Rhs.X.Flags & LI_DIRECT) != 0);
 
-        /* cmp ... */
-        CodeEntry* LoadA = D->Rhs.A.LoadEntry;
-        X = NewCodeEntry (OP65_CMP, LoadA->AM, LoadA->Arg, 0, D->OpEntry->LI);
-        InsertEntry (D, X, D->IP++);
+    /* sec */
+    X = NewCodeEntry (OP65_SEC, AM65_IMP, 0, 0, D->OpEntry->LI);
+    InsertEntry (D, X, D->IP++);
 
-    } else {
+    /* Add code for low operand */
+    AddOpLow (D, OP65_SBC, &D->Rhs);
 
-        /* ldy #offs */
-        const char* Arg = MakeHexArg (D->Rhs.A.Offs);
-        X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
-        InsertEntry (D, X, D->IP++);
+    /* We need the zero flag, so remember the immediate result */
+    X = NewCodeEntry (OP65_STA, AM65_ZP, "tmp1", 0, D->OpEntry->LI);
+    InsertEntry (D, X, D->IP++);
 
-        /* cmp (sp),y */
-        X = NewCodeEntry (OP65_CMP, AM65_ZP_INDY, "sp", 0, D->OpEntry->LI);
-        InsertEntry (D, X, D->IP++);
-    }
+    /* Add code for high operand */
+    AddOpHigh (D, OP65_SBC, &D->Rhs, 0);
 
-    /* In both cases, we can remove the load */
-    D->Rhs.A.Flags |= LI_REMOVE;
+    /* Set Z flag */
+    X = NewCodeEntry (OP65_ORA, AM65_ZP, "tmp1", 0, D->OpEntry->LI);
+    InsertEntry (D, X, D->IP++);
 
-    /* txa */
-    X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, D->OpEntry->LI);
+    /* Transform to boolean */
+    X = NewCodeEntry (OP65_JSR, AM65_ABS, "boolule", 0, D->OpEntry->LI);
     InsertEntry (D, X, D->IP++);
 
-    /* Must be true because of OP_RHS_LOAD */
-    CHECK ((D->Rhs.X.Flags & LI_DIRECT) != 0);
+    /* Remove the push and the call to the operator function */
+    RemoveRemainders (D);
 
-    /* If the location is on the stack, we need to reload the Y register. */
-    if ((D->Rhs.X.Flags & LI_RELOAD_Y) == 0) {
+    /* We changed the sequence */
+    return 1;
+}
 
-        /* sbc ... */
-        CodeEntry* LoadX = D->Rhs.X.LoadEntry;
-        X = NewCodeEntry (OP65_SBC, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
-        InsertEntry (D, X, D->IP++);
 
-    } else {
 
-        /* ldy #offs */
-        const char* Arg = MakeHexArg (D->Rhs.X.Offs);
-        X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
-        InsertEntry (D, X, D->IP++);
+static unsigned Opt_tosultax (StackOpData* D)
+/* Optimize the tosultax sequence */
+{
+    CodeEntry*  X;
 
-        /* sbc (sp),y */
-        X = NewCodeEntry (OP65_SBC, AM65_ZP_INDY, "sp", 0, D->OpEntry->LI);
-        InsertEntry (D, X, D->IP++);
-    }
 
-    /* In both cases, we can remove the load */
-    D->Rhs.X.Flags |= LI_REMOVE;
+    /* Inline the sbc */
+    D->IP = D->OpIndex+1;
 
-    /* lda #$00 */
-    X = NewCodeEntry (OP65_LDA, AM65_IMM, "$00", 0, D->OpEntry->LI);
-    InsertEntry (D, X, D->IP++);
+    /* Must be true because of OP_RHS_LOAD */
+    CHECK ((D->Rhs.A.Flags & D->Rhs.X.Flags & LI_DIRECT) != 0);
 
-    /* ldx #$00 */
-    X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, D->OpEntry->LI);
-    InsertEntry (D, X, D->IP++);
+    /* Add code for low operand */
+    AddOpLow (D, OP65_CMP, &D->Rhs);
 
-    /* rol a */
-    X = NewCodeEntry (OP65_ROL, AM65_ACC, "a", 0, D->OpEntry->LI);
+    /* Add code for high operand */
+    AddOpHigh (D, OP65_SBC, &D->Rhs, 0);
+
+    /* Transform to boolean */
+    X = NewCodeEntry (OP65_JSR, AM65_ABS, "boolult", 0, D->OpEntry->LI);
     InsertEntry (D, X, D->IP++);
 
-    /* Remove the push and the call to the tosugeax function */
+    /* Remove the push and the call to the operator function */
     RemoveRemainders (D);
 
     /* We changed the sequence */
@@ -1411,7 +1502,7 @@ static unsigned Opt_tosugeax (StackOpData* D)
 
 
 static unsigned Opt_tosxorax (StackOpData* D)
-/* Optimize the tosxorax sequence if possible */
+/* Optimize the tosxorax sequence */
 {
     CodeEntry*  X;
 
@@ -1421,7 +1512,7 @@ static unsigned Opt_tosxorax (StackOpData* D)
 
     /* Inline the xor, low byte */
     D->IP = D->OpIndex + 1;
-    AddOpLow (D, OP65_EOR);
+    AddOpLow (D, OP65_EOR, &D->Lhs);
 
     /* High byte */
     if (RegValIsKnown (D->PushEntry->RI->In.RegX) &&
@@ -1432,7 +1523,7 @@ static unsigned Opt_tosxorax (StackOpData* D)
        InsertEntry (D, X, D->IP++);
     } else if (D->PushEntry->RI->In.RegX != 0) {
        /* High byte is unknown */
-        AddOpHigh (D, OP65_EOR);
+        AddOpHigh (D, OP65_EOR, &D->Lhs, 1);
     }
 
     /* Remove the push and the call to the tosandax function */
@@ -1456,11 +1547,16 @@ static const OptFuncDesc FuncTable[] = {
     { "staxspidx",  Opt_staxspidx, REG_AX,   OP_NONE                    },
     { "tosaddax",   Opt_tosaddax,  REG_NONE, OP_NONE                    },
     { "tosandax",   Opt_tosandax,  REG_NONE, OP_NONE                    },
+    { "toseqax",    Opt_toseqax,   REG_NONE, OP_NONE                    },
     { "tosgeax",    Opt_tosgeax,   REG_NONE, OP_RHS_LOAD_DIRECT         },
     { "tosltax",    Opt_tosltax,   REG_NONE, OP_RHS_LOAD_DIRECT         },
+    { "tosneax",    Opt_tosneax,   REG_NONE, OP_NONE                    },
     { "tosorax",    Opt_tosorax,   REG_NONE, OP_NONE                    },
     { "tossubax",   Opt_tossubax,  REG_NONE, OP_RHS_LOAD_DIRECT         },
     { "tosugeax",   Opt_tosugeax,  REG_NONE, OP_RHS_LOAD_DIRECT         },
+    { "tosugtax",   Opt_tosugtax,  REG_NONE, OP_RHS_LOAD_DIRECT         },
+    { "tosuleax",   Opt_tosuleax,  REG_NONE, OP_RHS_LOAD_DIRECT         },
+    { "tosultax",   Opt_tosultax,  REG_NONE, OP_RHS_LOAD_DIRECT         },
     { "tosxorax",   Opt_tosxorax,  REG_NONE, OP_NONE                    },
 };
 #define FUNC_COUNT (sizeof(FuncTable) / sizeof(FuncTable[0]))
@@ -1501,16 +1597,24 @@ static int HarmlessCall (const char* Name)
     static const char* Tab[] = {
         "aslax1",
         "aslax2",
+        "aslax3",
+        "aslax4",
         "asrax1",
         "asrax2",
+        "asrax3",
+        "asrax4",
         "bnegax",
         "ldaxidx",
         "ldaxysp",
         "negax",
         "shlax1",
         "shlax2",
+        "shlax3",
+        "shlax4",
         "shrax1",
         "shrax2",
+        "shrax3",
+        "shrax4",
     };
 
     void* R = bsearch (Name,
@@ -1583,6 +1687,10 @@ static int PreCondOk (StackOpData* D)
             }
         }
     }
+    if ((D->Rhs.A.Flags | D->Rhs.X.Flags) & LI_DUP_LOAD) {
+        /* Cannot optimize */
+        return 0;
+    }
 
     /* Determine the zero page locations to use */
     if ((D->UsedRegs & REG_PTR1) == REG_NONE) {
@@ -1665,6 +1773,10 @@ unsigned OptStackOps (CodeSeg* S)
                 /* While searching, track register load insns, so we can tell
                  * what is in a register once pushax is encountered.
                  */
+                if (CE_HasLabel (E)) {
+                    /* Currently we don't track across branches */
+                    ClearLoadInfo (&Data.Lhs);
+                }
                 if (CE_IsCallTo (E, "pushax")) {
                     Data.PushIndex = I;
                     State = FoundPush;
@@ -1679,6 +1791,10 @@ unsigned OptStackOps (CodeSeg* S)
                  * follow and in the meantime, track zeropage usage and check
                  * for code that will disable us from translating the sequence.
                  */
+                if (CE_HasLabel (E)) {
+                    /* Currently we don't track across branches */
+                    ClearLoadInfo (&Data.Rhs);
+                }
                 if (E->OPC == OP65_JSR) {
 
                     /* Subroutine call: Check if this is one of the functions,
@@ -1746,9 +1862,6 @@ unsigned OptStackOps (CodeSeg* S)
                 FinalizeLoadInfo (&Data.Lhs, S);
                 FinalizeLoadInfo (&Data.Rhs, S);
 
-                /* Set flags for direct operations */
-                CheckDirectOp (&Data);
-
                 /* If the Lhs loads do load from zeropage, we have to include
                  * them into UsedRegs registers used. The Rhs loads have already
                  * been tracked.
@@ -1771,6 +1884,12 @@ unsigned OptStackOps (CodeSeg* S)
                     break;
                 }
 
+                /* Prepare the remainder of the data structure. */
+                Data.PrevEntry = CS_GetPrevEntry (S, Data.PushIndex);
+                Data.PushEntry = CS_GetEntry (S, Data.PushIndex);
+                Data.OpEntry   = CS_GetEntry (S, Data.OpIndex);
+                Data.NextEntry = CS_GetNextEntry (S, Data.OpIndex);
+
                 /* Adjust stack offsets to account for the upcoming removal */
                 AdjustStackOffset (&Data, 2);
 
@@ -1779,12 +1898,6 @@ unsigned OptStackOps (CodeSeg* S)
                  */
                 CS_GenRegInfo (S);
 
-                /* Prepare the remainder of the data structure. */
-                Data.PrevEntry = CS_GetPrevEntry (S, Data.PushIndex);
-                Data.PushEntry = CS_GetEntry (S, Data.PushIndex);
-                Data.OpEntry   = CS_GetEntry (S, Data.OpIndex);
-                Data.NextEntry = CS_GetNextEntry (S, Data.OpIndex);
-
                 /* Call the optimizer function */
                 Changes += Data.OptFunc->Func (&Data);