]> git.sur5r.net Git - cc65/blobdiff - src/cc65/codeent.c
Fixed wrong code generation for
[cc65] / src / cc65 / codeent.c
index 3868a353103f7e0b14d80dac9f66ba7441019a1f..13b7aed146701f961315ee240d2aa8b49bc34109 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2001-2003 Ullrich von Bassewitz                                       */
-/*               Römerstrasse 52                                             */
-/*               D-70794 Filderstadt                                         */
-/* EMail:        uz@cc65.org                                                 */
+/* (C) 2001-2005, Ullrich von Bassewitz                                      */
+/*                Römerstrasse 52                                            */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
@@ -144,7 +144,7 @@ static void SetUseChgInfo (CodeEntry* E, const OPCDesc* D)
      * lookup the information about this function and use it. The jump itself
      * does not change any registers, so we don't need to use the data from D.
      */
-    if ((E->Info & (OF_BRA | OF_CALL)) != 0 && E->JumpTo == 0) {
+    if ((E->Info & (OF_UBRA | OF_CALL)) != 0 && E->JumpTo == 0) {
        /* A subroutine call or jump to external symbol (function exit) */
        GetFuncInfo (E->Arg, &E->Use, &E->Chg);
     } else {
@@ -306,7 +306,7 @@ void CE_ReplaceOPC (CodeEntry* E, opc_t OPC)
 int CodeEntriesAreEqual (const CodeEntry* E1, const CodeEntry* E2)
 /* Check if both code entries are equal */
 {
-    return E1->OPC == E2->OPC && E1->AM == E2->AM && strcmp (E1->Arg, E2->Arg) == 0;
+    return (E1->OPC == E2->OPC && E1->AM == E2->AM && strcmp (E1->Arg, E2->Arg) == 0);
 }
 
 
@@ -323,6 +323,22 @@ void CE_AttachLabel (CodeEntry* E, CodeLabel* L)
 
 
 
+void CE_ClearJumpTo (CodeEntry* E)
+/* Clear the JumpTo entry and the argument (which contained the name of the
+ * label). Note: The function will not clear the backpointer from the label,
+ * so use it with care.
+ */
+{
+    /* Clear the JumpTo entry */
+    E->JumpTo = 0;
+
+    /* Clear the argument and assign the empty one */
+    FreeArg (E->Arg);
+    E->Arg = EmptyArg;
+}
+
+
+
 void CE_MoveLabel (CodeLabel* L, CodeEntry* E)
 /* Move the code label L from it's former owner to the code entry E. */
 {
@@ -336,6 +352,18 @@ void CE_MoveLabel (CodeLabel* L, CodeEntry* E)
 
 
 
+void CE_SetArg (CodeEntry* E, const char* Arg)
+/* Replace the argument by the new one. */
+{
+    /* Free the old argument */
+    FreeArg (E->Arg);
+
+    /* Assign the new one */
+    E->Arg = GetArgCopy (Arg);
+}
+
+
+
 void CE_SetNumArg (CodeEntry* E, long Num)
 /* Set a new numeric argument for the given code entry that must already
  * have a numeric argument.
@@ -357,11 +385,8 @@ void CE_SetNumArg (CodeEntry* E, long Num)
        Internal ("Invalid instruction size in CE_SetNumArg");
     }
 
-    /* Free the old argument */
-    FreeArg (E->Arg);
-
-    /* Assign the new one */
-    E->Arg = GetArgCopy (Buf);
+    /* Replace the argument by the new one */
+    CE_SetArg (E, Buf);
 
     /* Use the new numerical value */
     E->Num = Num;
@@ -369,19 +394,58 @@ void CE_SetNumArg (CodeEntry* E, long Num)
 
 
 
-int CE_KnownImm (const CodeEntry* E)
-/* Return true if the argument of E is a known immediate value */
+int CE_IsConstImm (const CodeEntry* E)
+/* Return true if the argument of E is a constant immediate value */
 {
     return (E->AM == AM65_IMM && (E->Flags & CEF_NUMARG) != 0);
 }
 
 
 
+int CE_IsKnownImm (const CodeEntry* E, unsigned long Num)
+/* Return true if the argument of E is a constant immediate value that is
+ * equal to Num.
+ */
+{
+    return E->AM == AM65_IMM            &&
+           (E->Flags & CEF_NUMARG) != 0 &&
+           E->Num == Num;
+}
+
+
+
 int CE_UseLoadFlags (const CodeEntry* E)
 /* Return true if the instruction uses any flags that are set by a load of
  * a register (N and Z).
  */
 {
+    /* Follow unconditional branches, but beware of endless loops. After this,
+     * E will point to the first entry that is not a branch.
+     */
+    if (E->Info & OF_UBRA) {
+        Collection C = AUTO_COLLECTION_INITIALIZER;
+
+        /* Follow the chain */
+        while (E->Info & OF_UBRA) {
+
+            /* Remember the entry so we can detect loops */
+            CollAppend (&C, (void*) E);
+
+            /* Check the target */
+            if (E->JumpTo == 0 || CollIndex (&C, E->JumpTo->Owner) >= 0) {
+                /* Unconditional jump to external symbol, or endless loop. */
+                DoneCollection (&C);
+                return 0;       /* Flags not used */
+            }
+
+            /* Follow the chain */
+            E = E->JumpTo->Owner;
+        }
+
+        /* Delete the collection */
+        DoneCollection (&C);
+    }
+
     /* A branch will use the flags */
     if (E->Info & OF_FBRA) {
         return 1;
@@ -420,7 +484,7 @@ void CE_FreeRegInfo (CodeEntry* E)
 /* Free an existing register info struct */
 {
     if (E->RI) {
-       FreeRegInfo (E->RI);
+               FreeRegInfo (E->RI);
        E->RI = 0;
     }
 }
@@ -467,7 +531,7 @@ void CE_GenRegInfo (CodeEntry* E, RegContents* InputRegs)
 
        case OP65_AND:
                    if (RegValIsKnown (In->RegA)) {
-               if (CE_KnownImm (E)) {
+               if (CE_IsConstImm (E)) {
                    Out->RegA = In->RegA & (short) E->Num;
                } else if (E->AM == AM65_ZP) {
                    switch (GetKnownReg (E->Use & REG_ZP, In)) {
@@ -624,7 +688,7 @@ void CE_GenRegInfo (CodeEntry* E, RegContents* InputRegs)
 
        case OP65_EOR:
                    if (RegValIsKnown (In->RegA)) {
-               if (CE_KnownImm (E)) {
+               if (CE_IsConstImm (E)) {
                    Out->RegA = In->RegA ^ (short) E->Num;
                } else if (E->AM == AM65_ZP) {
                    switch (GetKnownReg (E->Use & REG_ZP, In)) {
@@ -773,7 +837,7 @@ void CE_GenRegInfo (CodeEntry* E, RegContents* InputRegs)
            break;
 
        case OP65_LDA:
-           if (CE_KnownImm (E)) {
+           if (CE_IsConstImm (E)) {
                Out->RegA = (unsigned char) E->Num;
            } else if (E->AM == AM65_ZP) {
                switch (GetKnownReg (E->Use & REG_ZP, In)) {
@@ -803,7 +867,7 @@ void CE_GenRegInfo (CodeEntry* E, RegContents* InputRegs)
            break;
 
        case OP65_LDX:
-           if (CE_KnownImm (E)) {
+           if (CE_IsConstImm (E)) {
                Out->RegX = (unsigned char) E->Num;
            } else if (E->AM == AM65_ZP) {
                switch (GetKnownReg (E->Use & REG_ZP, In)) {
@@ -833,7 +897,7 @@ void CE_GenRegInfo (CodeEntry* E, RegContents* InputRegs)
            break;
 
        case OP65_LDY:
-           if (CE_KnownImm (E)) {
+           if (CE_IsConstImm (E)) {
                Out->RegY = (unsigned char) E->Num;
            } else if (E->AM == AM65_ZP) {
                switch (GetKnownReg (E->Use & REG_ZP, In)) {
@@ -894,7 +958,7 @@ void CE_GenRegInfo (CodeEntry* E, RegContents* InputRegs)
 
        case OP65_ORA:
            if (RegValIsKnown (In->RegA)) {
-               if (CE_KnownImm (E)) {
+               if (CE_IsConstImm (E)) {
                    Out->RegA = In->RegA | (short) E->Num;
                } else if (E->AM == AM65_ZP) {
                    switch (GetKnownReg (E->Use & REG_ZP, In)) {
@@ -1269,6 +1333,35 @@ static char* RegInfoDesc (unsigned U, char* Buf)
 
 
 
+static char* RegContentDesc (const RegContents* RC, char* Buf)
+/* Return a string containing register contents */
+{
+    char* B = Buf;
+
+    if (RegValIsUnknown (RC->RegA)) {
+        strcpy (B, "A:XX ");
+    } else {
+        sprintf (B, "A:%02X ", RC->RegA);
+    }
+    B += 5;
+    if (RegValIsUnknown (RC->RegX)) {
+        strcpy (B, "X:XX ");
+    } else {
+        sprintf (B, "X:%02X ", RC->RegX);
+    }
+    B += 5;
+    if (RegValIsUnknown (RC->RegY)) {
+        strcpy (B, "Y:XX");
+    } else {
+        sprintf (B, "Y:%02X", RC->RegY);
+    }
+    B += 4;
+
+    return Buf;
+}
+
+
+
 void CE_Output (const CodeEntry* E, FILE* F)
 /* Output the code entry to a file */
 {
@@ -1292,7 +1385,6 @@ void CE_Output (const CodeEntry* E, FILE* F)
     /* Print the operand */
     switch (E->AM) {
 
-       case AM_IMP:
        case AM65_IMP:
            /* implicit */
            break;
@@ -1302,13 +1394,11 @@ void CE_Output (const CodeEntry* E, FILE* F)
            Chars += fprintf (F, "%*sa", 9-Chars, "");
            break;
 
-       case AM_IMM:
        case AM65_IMM:
            /* immidiate */
            Chars += fprintf (F, "%*s#%s", 9-Chars, "", E->Arg);
            break;
 
-       case AM_ABS:
        case AM65_ZP:
        case AM65_ABS:
            /* zeropage and absolute */
@@ -1357,15 +1447,24 @@ void CE_Output (const CodeEntry* E, FILE* F)
        char Use [128];
        char Chg [128];
                fprintf (F,
-                        "%*s; USE: %-12s CHG: %-12s SIZE: %u\n",
+                        "%*s; USE: %-12s CHG: %-12s SIZE: %u",
                         30-Chars, "",
-                RegInfoDesc (E->Use, Use),
-                RegInfoDesc (E->Chg, Chg),
+                RegInfoDesc (E->Use, Use),
+                RegInfoDesc (E->Chg, Chg),
                 E->Size);
-    } else {
-       /* Terminate the line */
-       fprintf (F, "\n");
+
+        if (E->RI) {
+            char RegIn[32];
+            char RegOut[32];
+            fprintf (F,
+                     "    In %s  Out %s",
+                     RegContentDesc (&E->RI->In, RegIn),
+                     RegContentDesc (&E->RI->Out, RegOut));
+        }
     }
+
+    /* Terminate the line */
+    fprintf (F, "\n");
 }