]> git.sur5r.net Git - cc65/blobdiff - src/cc65/coptstore.c
Fixed two compiler warnings.
[cc65] / src / cc65 / coptstore.c
index 6a4916be513317d45b7263b9baab2abe666f4b6a..5deb2a39f2974bc84f06937632a92e789248bc26 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2002-2003 Ullrich von Bassewitz                                       */
-/*               Römerstrasse 52                                             */
-/*               D-70794 Filderstadt                                         */
-/* EMail:        uz@cc65.org                                                 */
+/* (C) 2002-2006, Ullrich von Bassewitz                                      */
+/*                Römerstrasse 52                                            */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
@@ -62,8 +62,7 @@ unsigned OptStore1 (CodeSeg* S)
  *      ldy     #n+1
  *      jsr     ldaxysp
  *
- * and remove the useless load, provided that the next insn doesn't use flags
- * from the load.
+ * and remove the useless load.
  */
 {
     unsigned Changes = 0;
@@ -72,23 +71,21 @@ unsigned OptStore1 (CodeSeg* S)
     unsigned I = 0;
     while (I < CS_GetEntryCount (S)) {
 
-       CodeEntry* L[5];
+       CodeEntry* L[4];
 
        /* Get next entry */
                L[0] = CS_GetEntry (S, I);
 
         /* Check for the sequence */
         if (L[0]->OPC == OP65_LDY                           &&
-           CE_KnownImm (L[0])                              &&
+           CE_IsConstImm (L[0])                            &&
             L[0]->Num < 0xFF                                &&
            !CS_RangeHasLabel (S, I+1, 3)                   &&
-                   CS_GetEntries (S, L+1, I+1, 4)                  &&
+                   CS_GetEntries (S, L+1, I+1, 3)                  &&
             CE_IsCallTo (L[1], "staxysp")                   &&
             L[2]->OPC == OP65_LDY                           &&
-            CE_KnownImm (L[2])                              &&
-            L[2]->Num == L[0]->Num + 1                      &&
-            CE_IsCallTo (L[3], "ldaxysp")                   &&
-            !CE_UseLoadFlags (L[4])) {
+            CE_IsKnownImm (L[2], L[0]->Num + 1)             &&
+            CE_IsCallTo (L[3], "ldaxysp")) {
 
                    /* Register has already the correct value, remove the loads */
            CS_DelEntries (S, I+2, 2);
@@ -387,3 +384,69 @@ unsigned OptStore4 (CodeSeg* S)
 
 
 
+unsigned OptStore5 (CodeSeg* S)
+/* Search for the sequence
+ *
+ *      lda     foo
+ *      ldx     bar
+ *      sta     something
+ *      stx     something-else
+ *
+ * and replace it by
+ *
+ *      lda     foo
+ *      sta     something
+ *      lda     bar
+ *      sta     something-else
+ *
+ * if X is not used later. This replacement doesn't save any cycles or bytes,
+ * but it keeps the value of X, which may be reused later.
+ */
+{
+    unsigned Changes = 0;
+
+    /* Walk over the entries */
+    unsigned I = 0;
+    while (I < CS_GetEntryCount (S)) {
+
+       CodeEntry* L[4];
+
+       /* Get next entry */
+               L[0] = CS_GetEntry (S, I);
+
+        /* Check for the sequence */
+        if (L[0]->OPC == OP65_LDA                           &&
+           !CS_RangeHasLabel (S, I+1, 3)                   &&
+                   CS_GetEntries (S, L+1, I+1, 3)                  &&
+            L[1]->OPC == OP65_LDX                           &&
+            L[2]->OPC == OP65_STA                           &&
+            L[3]->OPC == OP65_STX                           &&
+            !RegXUsed (S, I+4)) {
+
+            CodeEntry* X;
+
+            /* Insert the code after the sequence */
+                   X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
+           CS_InsertEntry (S, X, I+4);
+                   X = NewCodeEntry (OP65_STA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
+           CS_InsertEntry (S, X, I+5);
+
+                   /* Delete the old code */
+            CS_DelEntry (S, I+3);
+           CS_DelEntry (S, I+1);
+
+           /* Remember, we had changes */
+           ++Changes;
+       }
+
+       /* Next entry */
+       ++I;
+
+    }
+
+    /* Return the number of changes made */
+    return Changes;
+}
+
+
+