]> git.sur5r.net Git - cc65/blobdiff - src/cc65/codeent.c
Changed names of the pragmas to be identical to the corresponding command line
[cc65] / src / cc65 / codeent.c
index 427a17ef1e74acd9b05429d98feec88f91142ebf..8c73081b6705f6416fbf1d0a42f9dcf0646b9cf8 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2001-2003 Ullrich von Bassewitz                                       */
-/*               Römerstrasse 52                                             */
-/*               D-70794 Filderstadt                                         */
-/* EMail:        uz@cc65.org                                                 */
+/* (C) 2001-2009, Ullrich von Bassewitz                                      */
+/*                Roemerstrasse 52                                           */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
@@ -34,7 +34,6 @@
 
 
 #include <stdlib.h>
-#include <string.h>
 
 /* common */
 #include "chartype.h"
 #include "xsprintf.h"
 
 /* cc65 */
+#include "codeent.h"
 #include "codeinfo.h"
 #include "error.h"
 #include "global.h"
 #include "codelab.h"
 #include "opcodes.h"
-#include "codeent.h"
+#include "output.h"
 
 
 
@@ -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 {
@@ -245,8 +245,8 @@ CodeEntry* NewCodeEntry (opc_t OPC, am_t AM, const char* Arg,
     E->OPC    = D->OPC;
     E->AM     = AM;
     E->Size   = GetInsnSize (E->OPC, E->AM);
-    E->Flags  = NumArg (E->Arg, &E->Num)? CEF_NUMARG : 0;
     E->Arg    = GetArgCopy (Arg);
+    E->Flags  = NumArg (E->Arg, &E->Num)? CEF_NUMARG : 0;   /* Needs E->Arg */
     E->Info   = D->Info;
     E->JumpTo = JumpTo;
     E->LI     = UseLineInfo (LI);
@@ -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,26 +484,13 @@ void CE_FreeRegInfo (CodeEntry* E)
 /* Free an existing register info struct */
 {
     if (E->RI) {
-       FreeRegInfo (E->RI);
+               FreeRegInfo (E->RI);
        E->RI = 0;
     }
 }
 
 
 
-#if 0   /* Used for debugging */
-static void DumpRegInfo (const char* Desc, const RegInfo* RI)
-{
-    fprintf (stdout, "%s:\n", Desc);
-    fprintf (stdout, "In:  ");
-    RC_Dump (stdout, &RI->In);
-    fprintf (stdout, "Out: ");
-    RC_Dump (stdout, &RI->Out);
-}
-#endif
-
-
-
 void CE_GenRegInfo (CodeEntry* E, RegContents* InputRegs)
 /* Generate register info for this instruction. If an old info exists, it is
  * overwritten.
@@ -480,37 +531,40 @@ void CE_GenRegInfo (CodeEntry* E, RegContents* InputRegs)
 
        case OP65_AND:
                    if (RegValIsKnown (In->RegA)) {
-               if (CE_KnownImm (E)) {
-                   Out->RegA = In->RegA & (short) E->Num;
-               } else if (E->AM == AM65_ZP) {
-                   switch (GetKnownReg (E->Use & REG_ZP, In)) {
-                       case REG_TMP1:
-                           Out->RegA = In->RegA & In->Tmp1;
-                           break;
-                       case REG_PTR1_LO:
-                           Out->RegA = In->RegA & In->Ptr1Lo;
-                           break;
-                       case REG_PTR1_HI:
-                           Out->RegA = In->RegA & In->Ptr1Hi;
-                           break;
-                       case REG_SREG_LO:
-                           Out->RegA = In->RegA & In->SRegLo;
-                           break;
-                       case REG_SREG_HI:
-                           Out->RegA = In->RegA & In->SRegHi;
-                           break;
-                       default:
-                           Out->RegA = UNKNOWN_REGVAL;
-                           break;
-                   }
-               } else {
-                   Out->RegA = UNKNOWN_REGVAL;
-               }
-           }
+               if (CE_IsConstImm (E)) {
+                   Out->RegA = In->RegA & (short) E->Num;
+               } else if (E->AM == AM65_ZP) {
+                   switch (GetKnownReg (E->Use & REG_ZP, In)) {
+                       case REG_TMP1:
+                           Out->RegA = In->RegA & In->Tmp1;
+                           break;
+                       case REG_PTR1_LO:
+                           Out->RegA = In->RegA & In->Ptr1Lo;
+                           break;
+                       case REG_PTR1_HI:
+                           Out->RegA = In->RegA & In->Ptr1Hi;
+                           break;
+                       case REG_SREG_LO:
+                           Out->RegA = In->RegA & In->SRegLo;
+                           break;
+                       case REG_SREG_HI:
+                           Out->RegA = In->RegA & In->SRegHi;
+                           break;
+                       default:
+                           Out->RegA = UNKNOWN_REGVAL;
+                           break;
+                   }
+                } else {
+                   Out->RegA = UNKNOWN_REGVAL;
+               }
+           } else if (CE_IsKnownImm (E, 0)) {
+                /* A and $00 does always give zero */
+                Out->RegA = 0;
+            }
            break;
 
        case OP65_ASL:
-           if (E->AM == AM65_ACC && In->RegA >= 0) {
+           if (E->AM == AM65_ACC && RegValIsKnown (In->RegA)) {
                Out->RegA = (In->RegA << 1) & 0xFF;
            } else if (E->AM == AM65_ZP) {
                switch (GetKnownReg (E->Chg & REG_ZP, In)) {
@@ -597,7 +651,7 @@ void CE_GenRegInfo (CodeEntry* E, RegContents* InputRegs)
            break;
 
        case OP65_DEC:
-           if (E->AM == AM65_ACC && In->RegA >= 0) {
+           if (E->AM == AM65_ACC && RegValIsKnown (In->RegA)) {
                Out->RegA = (In->RegA - 1) & 0xFF;
            } else if (E->AM == AM65_ZP) {
                switch (GetKnownReg (E->Chg & REG_ZP, In)) {
@@ -612,7 +666,7 @@ void CE_GenRegInfo (CodeEntry* E, RegContents* InputRegs)
                        break;
                    case REG_SREG_LO:
                        Out->SRegLo = (In->SRegLo - 1) & 0xFF;
-                       break;
+                       break;
                    case REG_SREG_HI:
                        Out->SRegHi = (In->SRegHi - 1) & 0xFF;
                        break;
@@ -637,7 +691,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)) {
@@ -673,7 +727,7 @@ void CE_GenRegInfo (CodeEntry* E, RegContents* InputRegs)
            break;
 
        case OP65_INC:
-           if (E->AM == AM65_ACC && In->RegA >= 0) {
+           if (E->AM == AM65_ACC && RegValIsKnown (In->RegA)) {
                Out->RegA = (In->RegA + 1) & 0xFF;
            } else if (E->AM == AM65_ZP) {
                switch (GetKnownReg (E->Chg & REG_ZP, In)) {
@@ -771,7 +825,7 @@ void CE_GenRegInfo (CodeEntry* E, RegContents* InputRegs)
                if (In->RegA == 0xFF) {
                    Out->RegA = 0xFF;
                }
-               if (In->RegX == 0xFF) {
+               if (In->RegX == 0xFF) {
                     Out->RegX = 0xFF;
                 }
             } else if (FindBoolCmpCond (E->Arg) != CMP_INV) {
@@ -786,7 +840,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)) {
@@ -816,7 +870,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)) {
@@ -824,7 +878,7 @@ void CE_GenRegInfo (CodeEntry* E, RegContents* InputRegs)
                        Out->RegX = In->Tmp1;
                        break;
                    case REG_PTR1_LO:
-                       Out->RegX = In->Ptr1Lo;
+                       Out->RegX = In->Ptr1Lo;
                        break;
                    case REG_PTR1_HI:
                        Out->RegX = In->Ptr1Hi;
@@ -846,7 +900,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)) {
@@ -876,8 +930,8 @@ void CE_GenRegInfo (CodeEntry* E, RegContents* InputRegs)
            break;
 
        case OP65_LSR:
-           if (E->AM == AM65_ACC && In->RegA >= 0) {
-               Out->RegA = (In->RegA >> 1) & 0xFF;
+           if (E->AM == AM65_ACC && RegValIsKnown (In->RegA)) {
+               Out->RegA = (In->RegA >> 1) & 0xFF;
            } else if (E->AM == AM65_ZP) {
                switch (GetKnownReg (E->Chg & REG_ZP, In)) {
                    case REG_TMP1:
@@ -907,7 +961,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)) {
@@ -930,11 +984,14 @@ void CE_GenRegInfo (CodeEntry* E, RegContents* InputRegs)
                            Out->RegA = UNKNOWN_REGVAL;
                            break;
                    }
-               } else {
-                   /* A is now unknown */
-                   Out->RegA = UNKNOWN_REGVAL;
-               }
-           }
+               } else {
+                   /* A is now unknown */
+                   Out->RegA = UNKNOWN_REGVAL;
+               }
+           } else if (CE_IsKnownImm (E, 0xFF)) {
+                /* ORA with 0xFF does always give 0xFF */
+                Out->RegA = 0xFF;
+            }
            break;
 
        case OP65_PHA:
@@ -983,7 +1040,7 @@ void CE_GenRegInfo (CodeEntry* E, RegContents* InputRegs)
                        Out->SRegLo = UNKNOWN_REGVAL;
                        break;
                    case REG_SREG_HI:
-                       Out->SRegHi = UNKNOWN_REGVAL;
+                       Out->SRegHi = UNKNOWN_REGVAL;
                        break;
                }
            } else if (E->AM == AM65_ZPX) {
@@ -1153,7 +1210,7 @@ void CE_GenRegInfo (CodeEntry* E, RegContents* InputRegs)
                /* Invalidates all ZP registers */
                RC_InvalidateZP (Out);
            } else if (E->AM == AM65_ZP) {
-               if (In->RegA >= 0) {
+               if (RegValIsKnown (In->RegA)) {
                    switch (GetKnownReg (E->Chg & REG_ZP, In)) {
                        case REG_TMP1:
                            Out->Tmp1 &= ~In->RegA;
@@ -1198,7 +1255,7 @@ void CE_GenRegInfo (CodeEntry* E, RegContents* InputRegs)
                /* Invalidates all ZP registers */
                RC_InvalidateZP (Out);
            } else if (E->AM == AM65_ZP) {
-               if (In->RegA >= 0) {
+               if (RegValIsKnown (In->RegA)) {
                    switch (GetKnownReg (E->Chg & REG_ZP, In)) {
                        case REG_TMP1:
                            Out->Tmp1 |= In->RegA;
@@ -1282,82 +1339,112 @@ static char* RegInfoDesc (unsigned U, char* Buf)
 
 
 
-void CE_Output (const CodeEntry* E, FILE* F)
-/* Output the code entry to a file */
+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)
+/* Output the code entry to the output file */
 {
     const OPCDesc* D;
     unsigned Chars;
+    int Space;
     const char* Target;
 
     /* If we have a label, print that */
     unsigned LabelCount = CollCount (&E->Labels);
     unsigned I;
     for (I = 0; I < LabelCount; ++I) {
-       CL_Output (CollConstAt (&E->Labels, I), F);
+       CL_Output (CollConstAt (&E->Labels, I));
     }
 
     /* Get the opcode description */
     D = GetOPCDesc (E->OPC);
 
     /* Print the mnemonic */
-    Chars = fprintf (F, "\t%s", D->Mnemo);
+    Chars = WriteOutput ("\t%s", D->Mnemo);
+              
+    /* Space to leave before the operand */
+    Space = 9 - Chars;
 
     /* Print the operand */
     switch (E->AM) {
 
-       case AM_IMP:
        case AM65_IMP:
            /* implicit */
            break;
 
        case AM65_ACC:
            /* accumulator */
-           Chars += fprintf (F, "%*sa", 9-Chars, "");
+           Chars += WriteOutput ("%*sa", Space, "");
            break;
 
-       case AM_IMM:
        case AM65_IMM:
            /* immidiate */
-           Chars += fprintf (F, "%*s#%s", 9-Chars, "", E->Arg);
+           Chars += WriteOutput ("%*s#%s", Space, "", E->Arg);
            break;
 
-       case AM_ABS:
        case AM65_ZP:
        case AM65_ABS:
            /* zeropage and absolute */
-           Chars += fprintf (F, "%*s%s", 9-Chars, "", E->Arg);
+           Chars += WriteOutput ("%*s%s", Space, "", E->Arg);
                    break;
 
        case AM65_ZPX:
        case AM65_ABSX:
            /* zeropage,X and absolute,X */
-           Chars += fprintf (F, "%*s%s,x", 9-Chars, "", E->Arg);
+           Chars += WriteOutput ("%*s%s,x", Space, "", E->Arg);
            break;
 
        case AM65_ABSY:
            /* absolute,Y */
-           Chars += fprintf (F, "%*s%s,y", 9-Chars, "", E->Arg);
+           Chars += WriteOutput ("%*s%s,y", Space, "", E->Arg);
            break;
 
        case AM65_ZPX_IND:
            /* (zeropage,x) */
-                   Chars += fprintf (F, "%*s(%s,x)", 9-Chars, "", E->Arg);
+                   Chars += WriteOutput ("%*s(%s,x)", Space, "", E->Arg);
            break;
 
        case AM65_ZP_INDY:
            /* (zeropage),y */
-                   Chars += fprintf (F, "%*s(%s),y", 9-Chars, "", E->Arg);
+                   Chars += WriteOutput ("%*s(%s),y", Space, "", E->Arg);
            break;
 
        case AM65_ZP_IND:
            /* (zeropage) */
-                   Chars += fprintf (F, "%*s(%s)", 9-Chars, "", E->Arg);
+                   Chars += WriteOutput ("%*s(%s)", Space, "", E->Arg);
            break;
 
        case AM65_BRA:
            /* branch */
            Target = E->JumpTo? E->JumpTo->Name : E->Arg;
-           Chars += fprintf (F, "%*s%s", 9-Chars, "", Target);
+           Chars += WriteOutput ("%*s%s", Space, "", Target);
            break;
 
        default:
@@ -1366,19 +1453,26 @@ void CE_Output (const CodeEntry* E, FILE* F)
     }
 
     /* Print usage info if requested by the debugging flag */
-    if (Debug) {
+    if (Debug) {                    
        char Use [128];
        char Chg [128];
-               fprintf (F,
-                        "%*s; USE: %-12s CHG: %-12s SIZE: %u\n",
-                        30-Chars, "",
-                RegInfoDesc (E->Use, Use),
-                RegInfoDesc (E->Chg, Chg),
-                E->Size);
-    } else {
-       /* Terminate the line */
-       fprintf (F, "\n");
+               WriteOutput ("%*s; USE: %-12s CHG: %-12s SIZE: %u",
+                            (int)(30-Chars), "",
+                    RegInfoDesc (E->Use, Use),
+                    RegInfoDesc (E->Chg, Chg),
+                    E->Size);
+
+        if (E->RI) {
+            char RegIn[32];
+            char RegOut[32];
+            WriteOutput ("    In %s  Out %s",
+                         RegContentDesc (&E->RI->In, RegIn),
+                         RegContentDesc (&E->RI->Out, RegOut));
+        }
     }
+
+    /* Terminate the line */
+    WriteOutput ("\n");
 }