]> git.sur5r.net Git - cc65/blobdiff - src/cc65/coptadd.c
Fixed a typo
[cc65] / src / cc65 / coptadd.c
index 558dd85ab79bd67eea3bebaa368e9ce7d2186873..d490574d9cdff5dd96091097d4d017bf41945c22 100644 (file)
@@ -33,6 +33,9 @@
 
 
 
+/* common */
+#include "chartype.h"
+
 /* cc65 */
 #include "codeent.h"
 #include "codeinfo.h"
@@ -57,7 +60,7 @@ unsigned OptAdd1 (CodeSeg* S)
  *      jsr     tosaddax
  *
  * and replace it by:
- *      
+ *
  *      ldy     #xx-1
  *      lda     (sp),y
  *      clc
@@ -88,12 +91,12 @@ unsigned OptAdd1 (CodeSeg* S)
            CE_KnownImm (L[0])               &&
            !CS_RangeHasLabel (S, I+1, 5)    &&
                    CS_GetEntries (S, L+1, I+1, 5)   &&
-                   CE_IsCall (L[1], "ldaxysp")      &&
-                   CE_IsCall (L[2], "pushax")       &&
+                   CE_IsCallTo (L[1], "ldaxysp")    &&
+                   CE_IsCallTo (L[2], "pushax")     &&
                    L[3]->OPC == OP65_LDY            &&
            CE_KnownImm (L[3])               &&
-                   CE_IsCall (L[4], "ldaxysp")      &&
-                   CE_IsCall (L[5], "tosaddax")) {
+                   CE_IsCallTo (L[4], "ldaxysp")    &&
+                   CE_IsCallTo (L[5], "tosaddax")) {
 
            CodeEntry* X;
             const char* Arg;
@@ -208,10 +211,10 @@ unsigned OptAdd2 (CodeSeg* S)
            CE_KnownImm (L[0])                  &&
            !CS_RangeHasLabel (S, I+1, 3)       &&
                    CS_GetEntries (S, L+1, I+1, 3)      &&
-           CE_IsCall (L[1], "ldaxysp")         &&
+           CE_IsCallTo (L[1], "ldaxysp")       &&
                    L[2]->OPC == OP65_LDY               &&
            CE_KnownImm (L[2])                  &&
-                   CE_IsCall (L[3], "addeqysp")        &&
+                   CE_IsCallTo (L[3], "addeqysp")      &&
                    (GetRegInfo (S, I+4, REG_AX) & REG_AX) == 0) {
 
            /* Insert new code behind the addeqysp */
@@ -284,6 +287,148 @@ unsigned OptAdd2 (CodeSeg* S)
 
 
 unsigned OptAdd3 (CodeSeg* S)
+/* Search for the sequence
+ *
+ *     jsr     pushax
+ *      lda     xxx
+ *             ldx     yyy
+ *      jsr     tosaddax
+ *
+ * and replace it by
+ *
+ *      clc
+ *      adc     xxx
+ *      pha
+ *      txa
+ *      adc     yyy
+ *      tax
+ *      pla
+ */
+{
+    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 (CE_IsCallTo (L[0], "pushax")                        &&
+                   CS_GetEntries (S, L+1, I+1, 3)                      &&
+            !CS_RangeHasLabel (S, I+1, 3)                       &&
+            L[1]->OPC == OP65_LDA                               &&
+            (L[1]->AM == AM65_ABS || L[1]->AM == AM65_ZP)       &&
+            L[2]->OPC == OP65_LDX                               &&
+            (L[2]->AM == AM65_ABS || L[2]->AM == AM65_ZP)       &&
+            CE_IsCallTo (L[3], "tosaddax")) {
+
+            CodeEntry* X;
+
+            /* Insert new code behind the sequence */
+           X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, L[3]->LI);
+           CS_InsertEntry (S, X, I+4);
+
+            /* adc xxx */
+           X = NewCodeEntry (OP65_ADC, L[1]->AM, L[1]->Arg, 0, L[3]->LI);
+           CS_InsertEntry (S, X, I+5);
+
+            /* pha */
+           X = NewCodeEntry (OP65_PHA, AM65_IMP, 0, 0, L[3]->LI);
+           CS_InsertEntry (S, X, I+6);
+
+            /* txa */
+           X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, L[3]->LI);
+           CS_InsertEntry (S, X, I+7);
+
+            /* adc yyy */
+           X = NewCodeEntry (OP65_ADC, L[2]->AM, L[2]->Arg, 0, L[3]->LI);
+           CS_InsertEntry (S, X, I+8);
+
+            /* tax */
+           X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, L[3]->LI);
+           CS_InsertEntry (S, X, I+9);
+
+            /* pla */
+           X = NewCodeEntry (OP65_PLA, AM65_IMP, 0, 0, L[3]->LI);
+           CS_InsertEntry (S, X, I+10);
+
+           /* Delete the old code */
+           CS_DelEntries (S, I, 4);
+
+           /* Remember, we had changes */
+           ++Changes;
+
+       }
+
+       /* Next entry */
+       ++I;
+
+    }
+
+    /* Return the number of changes made */
+    return Changes;
+}
+
+
+
+unsigned OptAdd4 (CodeSeg* S)
+/* Search for a call to incaxn and replace it by an 8 bit add if the X register
+ * is not used later.
+ */
+{
+    unsigned Changes = 0;
+
+    /* Walk over the entries */
+    unsigned I = 0;
+    while (I < CS_GetEntryCount (S)) {
+
+       CodeEntry* E;
+
+       /* Get next entry */
+               E = CS_GetEntry (S, I);
+
+       /* Check for the sequence */
+        if (E->OPC == OP65_JSR                          &&
+            strncmp (E->Arg, "incax", 5) == 0           &&
+            IsDigit (E->Arg[5])                         &&
+            E->Arg[6] == '\0'                           &&
+            !RegXUsed (S, I+1)) {
+
+            CodeEntry* X;
+            const char* Arg;
+
+            /* Insert new code behind the sequence */
+           X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, E->LI);
+           CS_InsertEntry (S, X, I+1);
+
+           Arg = MakeHexArg (E->Arg[5] - '0');
+                   X = NewCodeEntry (OP65_ADC, AM65_IMM, Arg, 0, E->LI);
+           CS_InsertEntry (S, X, I+2);
+
+                   /* Delete the old code */
+           CS_DelEntry (S, I);
+
+           /* Remember, we had changes */
+           ++Changes;
+
+       }
+
+       /* Next entry */
+       ++I;
+
+    }
+
+    /* Return the number of changes made */
+    return Changes;
+}
+
+
+
+unsigned OptAdd5 (CodeSeg* S)
 /* Search for the sequence
  *
  *     adc     ...
@@ -306,7 +451,7 @@ unsigned OptAdd3 (CodeSeg* S)
                CodeEntry* E = CS_GetEntry (S, I);
 
        /* Check for the sequence */
-               if (E->OPC == OP65_ADC                               &&
+               if (E->OPC == OP65_ADC                               &&
            CS_GetEntries (S, L, I+1, 3)                     &&
                    (L[0]->OPC == OP65_BCC || L[0]->OPC == OP65_JCC) &&
            L[0]->JumpTo != 0                                &&
@@ -335,4 +480,3 @@ unsigned OptAdd3 (CodeSeg* S)
 
 
 
-