]> git.sur5r.net Git - cc65/blobdiff - src/cc65/optimize.c
Added the io module
[cc65] / src / cc65 / optimize.c
index 9be2e5d4cc67f77455847e356451a62b0f84ee68..48ce3fbb61233acae5c3c8dd4c2f05f0f501069d 100644 (file)
 #include <string.h>
 #include <ctype.h>
 
-#include "../common/xmalloc.h"
+/* common */
+#include "attrib.h"
+#include "xmalloc.h"
 
+/* cc65 */
 #include "asmlabel.h"
 #include "asmline.h"
 #include "check.h"
@@ -126,7 +129,7 @@ static const struct {
     { "\tcpx\t",                 0,    REG_X,      REG_NONE      },
     { "\tcpy\t",                 0,    REG_Y,      REG_NONE      },
     { "\tdea",           1,    REG_A,      REG_NONE      },
-    { "\tdec\ta",        1,    REG_A,      REG_NONE      },
+    { "\tdec\ta",        1,    REG_A,      REG_NONE      },
     { "\tdec\t",                 0,    REG_NONE,   REG_NONE      },
     { "\tdex",                   1,    REG_X,      REG_NONE      },
     { "\tdey",                   1,    REG_Y,      REG_NONE      },
@@ -226,7 +229,7 @@ static const char* LongBranches [] = {
 
 
 /*****************************************************************************/
-/*                                        Forwards                                  */
+/*                                        Forwards                                  */
 /*****************************************************************************/
 
 
@@ -243,10 +246,19 @@ static unsigned GetLabelNum (const char* L);
 static unsigned RVUInt1 (Line* L, LineColl* LC, unsigned Used, unsigned Unused);
 /* Subfunction for RegValUsed. Will be called recursively in case of branches. */
 
+static Line* NewLineAfter (Line* LineBefore, const char* Format, ...) attribute ((format(printf,2,3)));
+/* Create a new line, insert it after L and return it. The new line is marked
+ * as code line.
+ */
+
+static Line* ReplaceLine (Line* L, const char* Format, ...)
+       attribute ((format(printf,2,3)));
+/* Replace one line by another */
+
 
 
 /*****************************************************************************/
-/*                                       List stuff                                 */
+/*                                       List stuff                                 */
 /*****************************************************************************/
 
 
@@ -1845,8 +1857,8 @@ static int OptPtrOps1 (Line** Start)
            /* Cannot get lines */
            return 0;
        }
-       if (LineFullMatch (L2[3], "\tclc")                      &&
-           LineMatch (L2[4], "\tadc\t#$")                      &&
+       if (LineFullMatch (L2[3], "\tclc")                      &&
+           LineMatch (L2[4], "\tadc\t#$")                      &&
            LineFullMatch (L2[5], "\tbcc\t*+3")                 &&
            LineFullMatch (L2[6], "\tinx")) {
            /* Inlined increment */
@@ -1860,9 +1872,9 @@ static int OptPtrOps1 (Line** Start)
     }
 
     /* Check for the remainder */
-    if (!LineMatch (L3[0], "\tsta\t")                          ||
+    if (!LineMatch (L3[0], "\tsta\t")                          ||
        strcmp (L3[0]->Line+5, L->Line+5) != 0                  ||
-       !LineMatch (L3[1], "\tstx\t")                           ||
+       !LineMatch (L3[1], "\tstx\t")                           ||
        strcmp (L3[1]->Line+5, L2[0]->Line+5) != 0              ||
        !LineFullMatch (L3[2], "\tlda\tregsave")                ||
        !LineFullMatch (L3[3], "\tldx\tregsave+1")) {
@@ -1872,18 +1884,14 @@ static int OptPtrOps1 (Line** Start)
     }
 
     /* Check if AX is actually used following the code above. If not,
-     * we don't need to load A/X from regsave. Since X will never by
+     * we don't need to load A/X from regsave. Since X will never be
      * used without A, check just for A.
      */
-    NeedLoad = 1;
-    if (!RegAUsed (L3[3])) {
-       /* We don't need to load regsave */
-       NeedLoad = 0;
-    }
+    NeedLoad = RegAUsed (L3[3]);
 
     /* Special code for register variables */
     Done = 0;
-    if (LineMatch (L, "\tlda\tregbank+")       &&
+    if (LineMatch (L, "\tlda\tregbank+")       &&
                GetNextCodeLines (L3[3], &L3[4], 1)     &&
        Inc == 1) {
 
@@ -2042,8 +2050,8 @@ static int OptPtrOps1 (Line** Start)
 
        /* If we need to load a/x, add the code */
        if (NeedLoad) {
-           L = NewLineAfter (L, "\ttax");
            L = NewLineAfter (L, "\tlda\tptr1");
+           L = NewLineAfter (L, "\tldx\tptr1+1");
        }
     }
 
@@ -2157,11 +2165,7 @@ static int OptPtrOps2 (Line** Start)
      * we don't need to load A/X from regsave. Since X will never by
      * used without A, check just for A.
      */
-    NeedLoad = 1;
-    if (!RegAUsed (L3[0])) {
-       /* We don't need to load regsave */
-       NeedLoad = 0;
-    }
+    NeedLoad = RegAUsed (L3[0]);
 
     /* Replace the ldy instruction, offset must point to the low byte */
     sprintf (L->Line+7, "%02X", Offs);
@@ -2251,8 +2255,8 @@ static int OptPtrOps2 (Line** Start)
 
     /* If we need to load a/x, add the code */
     if (NeedLoad) {
-       L = NewLineAfter (L, "\ttax");
        L = NewLineAfter (L, "\tlda\tptr1");
+       L = NewLineAfter (L, "\tldx\tptr1+1");
     }
 
     /* Remove the code that is no longer needed */