]> git.sur5r.net Git - cc65/commitdiff
Fix for #830 supplied by UvB
authorIrgendwerA8 <c.krueger.b@web.de>
Mon, 22 Apr 2019 15:40:09 +0000 (17:40 +0200)
committerOliver Schmidt <ol.sc@web.de>
Thu, 25 Apr 2019 13:19:53 +0000 (15:19 +0200)
src/cc65/coptstop.c
test/val/bug830.c [new file with mode: 0644]

index 53582d5659661abec300f46eeac45123735d35f1..87514cfdd3ebb06049f684da5e1732200350c1e7 100644 (file)
@@ -437,51 +437,78 @@ static void AdjustStackOffset (StackOpData* D, unsigned Offs)
 
         CodeEntry* E = CS_GetEntry (D->Code, I);
 
 
         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);
         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 */
             /* We need to correct this one */
-            NeedCorrection = 1;
+            Correction = (E->OPC == OP65_LDA)? 2 : 1;
 
         } else if (CE_IsCallTo (E, "ldaxysp")) {
 
         } else if (CE_IsCallTo (E, "ldaxysp")) {
-
             /* We need to correct this one */
             /* 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)) {
             /* 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);
                 /* The Y load is just before the stack access, adjust it */
                 CE_SetNumArg (P, P->Num - Offs);
-
             } else {
             } 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++);
                 /* 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)) {
             }
 
             /* 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);
                 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;
+                }
             }
         }
 
             }
         }
 
diff --git a/test/val/bug830.c b/test/val/bug830.c
new file mode 100644 (file)
index 0000000..05080e2
--- /dev/null
@@ -0,0 +1,13 @@
+#include "unittest.h"
+
+char test[1];
+char *dst = &test[0];
+
+TEST
+{
+    char src = 0;
+    *dst = (src == 0) ? 42 : src;
+
+    ASSERT_AreEqual(42, *dst, "%u", "Incorrect ternary expression evaluation!");
+}
+ENDTEST