]> git.sur5r.net Git - cc65/blobdiff - src/cc65/coptstop.c
Fixed _textcolor definition.
[cc65] / src / cc65 / coptstop.c
index 1d14ec59c673a6b6446e931161c90ec67580bc23..87514cfdd3ebb06049f684da5e1732200350c1e7 100644 (file)
@@ -437,51 +437,78 @@ static void AdjustStackOffset (StackOpData* D, unsigned Offs)
 
         CodeEntry* E = CS_GetEntry (D->Code, I);
 
-        int NeedCorrection = 0;
+        /* Check if this entry does a stack access, and if so, if it's a plain
+        ** load from stack, since this is needed later.
+        */
+        int Correction = 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;
+            Correction = (E->OPC == OP65_LDA)? 2 : 1;
 
         } else if (CE_IsCallTo (E, "ldaxysp")) {
-
             /* We need to correct this one */
-            NeedCorrection = 1;
-
+            Correction = 1;
         }
 
-        if (NeedCorrection) {
-
+        if (Correction) {
             /* 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);
                 InsertEntry (D, X, I++);
-
             }
 
             /* If we need the value of Y later, be sure to reload it */
             if (RegYUsed (D->Code, I+1)) {
+                CodeEntry* N;
                 const char* Arg = MakeHexArg (E->RI->In.RegY);
-                CodeEntry* X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
-                InsertEntry (D, X, I+1);
+                if (Correction == 2 && (N = CS_GetNextEntry(D->Code, I)) != 0 &&
+                    ((N->Info & OF_ZBRA) != 0) && N->JumpTo != 0) {
+                    /* The Y register is used but the load instruction loads A
+                    ** and is followed by a branch that evaluates the zero flag.
+                    ** This means that we cannot just insert the load insn
+                    ** for the Y register at this place, because it would
+                    ** destroy the Z flag. Instead place load insns at the
+                    ** target of the branch and after it.
+                    ** Note: There is a chance that this code won't work. The
+                    ** jump may be a backwards jump (in which case the stack
+                    ** offset has already been adjusted) or there may be other
+                    ** instructions between the load and the conditional jump.
+                    ** Currently the compiler does not generate such code, but
+                    ** it is possible to force the optimizer into something
+                    ** invalid by use of inline assembler.
+                    */
+
+                    /* Add load insn after the branch */
+                    CodeEntry* X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
+                    InsertEntry (D, X, I+2);
 
-                /* Skip this instruction in the next round */
-                ++I;
+                    /* Add load insn before branch target */
+                    CodeEntry* Y = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
+                    int J = CS_GetEntryIndex (D->Code, N->JumpTo->Owner);
+                    CHECK (J > I);      /* Must not happen */
+                    InsertEntry (D, Y, J);
+
+                    /* Move the label to the new insn */
+                    CodeLabel* L = CS_GenLabel (D->Code, Y);
+                    CS_MoveLabelRef (D->Code, N, L);
+                } else {
+                    CodeEntry* X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
+                    InsertEntry (D, X, I+1);
+                    /* Skip this instruction in the next round */
+                    ++I;
+                }
             }
         }
 
@@ -1833,8 +1860,8 @@ static int PreCondOk (StackOpData* D)
     }
 
     /* Determine the zero page locations to use. We've tracked the used
-     * ZP locations, so try to find some for us that are unused.
-     */
+    ** ZP locations, so try to find some for us that are unused.
+    */
     if ((D->ZPUsage & REG_PTR1) == REG_NONE) {
         D->ZPLo = "ptr1";
         D->ZPHi = "ptr1+1";
@@ -2018,12 +2045,12 @@ unsigned OptStackOps (CodeSeg* S)
                 FinalizeLoadInfo (&Data.Rhs, S);
 
                 /* Check if the lhs loads from zeropage. If this is true, these
-                 * zero page locations have to be added to ZPUsage, because
-                 * they cannot be used for intermediate storage. In addition,
-                 * if one of these zero page locations is destroyed between
-                 * pushing the lhs and the actual operation, we cannot use the
-                 * original zero page locations for the final op, but must
-                 * use another ZP location to save them.
+                ** zero page locations have to be added to ZPUsage, because
+                ** they cannot be used for intermediate storage. In addition,
+                ** if one of these zero page locations is destroyed between
+                ** pushing the lhs and the actual operation, we cannot use the
+                ** original zero page locations for the final op, but must
+                ** use another ZP location to save them.
                 */
                 ChangedRegs &= REG_ZP;
                 if (Data.Lhs.A.LoadEntry && Data.Lhs.A.LoadEntry->AM == AM65_ZP) {