]> git.sur5r.net Git - cc65/blobdiff - src/cc65/codeinfo.c
Adding functionality to StrBuf
[cc65] / src / cc65 / codeinfo.c
index 1f89ef14a050c2fb4f9611ccf44860e02ffc0125..982ce4cec1c40544834e73fb68f4364d3df7b1d8 100644 (file)
 #include "coll.h"
 
 /* cc65 */
+#include "codeent.h"
+#include "codeseg.h"
+#include "datatype.h"
+#include "error.h"
+#include "symtab.h"
 #include "codeinfo.h"
 
 
@@ -61,53 +66,113 @@ struct FuncInfo {
 };
 
 static const FuncInfo FuncInfoTable[] = {
-    { "booleq",                REG_NONE,       REG_AX  },
-    { "boolge",                REG_NONE,       REG_AX  },
-    { "boolgt",                REG_NONE,       REG_AX  },
-    { "boolle",                REG_NONE,       REG_AX  },
-    { "boollt",                REG_NONE,       REG_AX  },
-    { "boolne",                REG_NONE,       REG_AX  },
-    { "booluge",       REG_NONE,       REG_AX  },
-    { "boolugt",       REG_NONE,       REG_AX  },
-    { "boolule",       REG_NONE,       REG_AX  },
-    { "boolult",       REG_NONE,       REG_AX  },
-    { "decax1",        REG_AX,         REG_AX  },
-    { "decax2",        REG_AX,         REG_AX  },
-    { "decax3",        REG_AX,         REG_AX  },
-    { "decax4",        REG_AX,         REG_AX  },
-    { "decax5",        REG_AX,         REG_AX  },
-    { "decax6",        REG_AX,         REG_AX  },
-    { "decax7",        REG_AX,         REG_AX  },
-    { "decax8",        REG_AX,         REG_AX  },
-    { "decaxy",                REG_AXY,        REG_AX  },
-    { "decsp2",        REG_NONE,       REG_A   },
-    { "decsp3",        REG_NONE,       REG_A   },
-    { "decsp4",        REG_NONE,       REG_A   },
-    { "decsp5",        REG_NONE,       REG_A   },
-    { "decsp6",        REG_NONE,       REG_A   },
-    { "decsp7",        REG_NONE,       REG_A   },
-    { "decsp8",        REG_NONE,       REG_A   },
-    { "ldax0sp",       REG_Y,          REG_AX  },
-    { "ldaxysp",       REG_Y,          REG_AX  },
-    { "pusha",         REG_A,          REG_Y   },
-    { "pusha0",                REG_A,          REG_XY  },
-    { "pushax",                REG_AX,         REG_Y   },
-    { "pushw0sp",      REG_NONE,       REG_AXY },
-    { "pushwysp",      REG_Y,          REG_AXY },
-    { "tosicmp",       REG_AX,         REG_AXY },
+    { "addysp",                REG_Y,          REG_NONE        },
+    { "aslax1",         REG_AX,                REG_AX          },
+    { "aslax2",         REG_AX,                REG_AX          },
+    { "aslax3",         REG_AX,                REG_AX          },
+    { "aslax4",         REG_AX,                REG_AX          },
+    { "bnega",          REG_A,          REG_AX          },
+    { "bnegax",         REG_AX,         REG_AX          },
+    { "bnegeax",        REG_AX,         REG_AX          },
+    { "booleq",                REG_NONE,       REG_AX          },
+    { "boolge",                REG_NONE,       REG_AX          },
+    { "boolgt",                REG_NONE,       REG_AX          },
+    { "boolle",                REG_NONE,       REG_AX          },
+    { "boollt",                REG_NONE,       REG_AX          },
+    { "boolne",                REG_NONE,       REG_AX          },
+    { "booluge",       REG_NONE,       REG_AX          },
+    { "boolugt",       REG_NONE,       REG_AX          },
+    { "boolule",       REG_NONE,       REG_AX          },
+    { "boolult",       REG_NONE,       REG_AX          },
+    { "complax",        REG_AX,         REG_AX          },
+    { "decax1",        REG_AX,         REG_AX          },
+    { "decax2",        REG_AX,         REG_AX          },
+    { "decax3",        REG_AX,         REG_AX          },
+    { "decax4",        REG_AX,         REG_AX          },
+    { "decax5",        REG_AX,         REG_AX          },
+    { "decax6",        REG_AX,         REG_AX          },
+    { "decax7",        REG_AX,         REG_AX          },
+    { "decax8",        REG_AX,         REG_AX          },
+    { "decaxy",                REG_AXY,        REG_AX          },
+    { "decsp1",                REG_NONE,       REG_Y           },
+    { "decsp2",        REG_NONE,       REG_A           },
+    { "decsp3",        REG_NONE,       REG_A           },
+    { "decsp4",        REG_NONE,       REG_A           },
+    { "decsp5",        REG_NONE,       REG_A           },
+    { "decsp6",        REG_NONE,       REG_A           },
+    { "decsp7",        REG_NONE,       REG_A           },
+    { "decsp8",        REG_NONE,       REG_A           },
+    { "incax1",         REG_AX,         REG_AX          },
+    { "incax2",         REG_AX,         REG_AX          },
+    { "incsp1",                REG_NONE,       REG_NONE        },
+    { "incsp2",                REG_NONE,       REG_Y           },
+    { "incsp3",                REG_NONE,       REG_Y           },
+    { "incsp4",                REG_NONE,       REG_Y           },
+    { "incsp5",                REG_NONE,       REG_Y           },
+    { "incsp6",                REG_NONE,       REG_Y           },
+    { "incsp7",                REG_NONE,       REG_Y           },
+    { "incsp8",                REG_NONE,       REG_Y           },
+    { "ldaidx",         REG_AXY,        REG_AX          },
+    { "ldauidx",        REG_AXY,        REG_AX          },
+    { "ldax0sp",       REG_Y,          REG_AX          },
+    { "ldaxi",          REG_AX,         REG_AXY         },
+    { "ldaxidx",        REG_AXY,        REG_AX          },
+    { "ldaxysp",       REG_Y,          REG_AX          },
+    { "leaasp",         REG_A,          REG_AX          },
+    { "negax",          REG_AX,         REG_AX          },
+    { "pusha",         REG_A,          REG_Y           },
+    { "pusha0",                REG_A,          REG_XY          },
+    { "pushax",                REG_AX,         REG_Y           },
+    { "pusheax",        REG_AX,         REG_Y           },
+    { "pushw0sp",      REG_NONE,       REG_AXY         },
+    { "pushwysp",      REG_Y,          REG_AXY         },
+    { "shlax1",         REG_AX,                REG_AX          },
+    { "shlax2",         REG_AX,                REG_AX          },
+    { "shlax3",         REG_AX,                REG_AX          },
+    { "shlax4",         REG_AX,                REG_AX          },
+    { "shrax1",         REG_AX,         REG_AX          },
+    { "shrax2",         REG_AX,         REG_AX          },
+    { "shrax3",         REG_AX,         REG_AX          },
+    { "shrax4",         REG_AX,         REG_AX          },
+    { "shreax1",        REG_AX,         REG_AX          },
+    { "shreax2",        REG_AX,         REG_AX          },
+    { "shreax3",        REG_AX,         REG_AX          },
+    { "shreax4",        REG_AX,         REG_AX          },
+    { "staspidx",       REG_A | REG_Y,  REG_Y           },
+    { "stax0sp",        REG_AX,         REG_Y           },
+    { "tosicmp",       REG_AX,         REG_AXY         },
+    { "tosdiva0",       REG_AX,                REG_AXY         },
+    { "tosdivax",       REG_AX,                REG_AXY         },
+    { "tosdiveax",      REG_AX,                REG_AXY         },
+    { "tosmula0",       REG_AX,                REG_AXY         },
+    { "tosmulax",       REG_AX,                REG_AXY         },
+    { "tosmuleax",      REG_AX,                REG_AXY         },
+    { "tosshreax",      REG_AX,                REG_AXY         },
+    { "tosumula0",      REG_AX,                REG_AXY         },
+    { "tosumulax",      REG_AX,                REG_AXY         },
+    { "tosumuleax",     REG_AX,                REG_AXY         },
 };
 #define FuncInfoCount  (sizeof(FuncInfoTable) / sizeof(FuncInfoTable[0]))
 
-/* Structure used to pass information to the RegValUsedInt1 and 2 functions */
-typedef struct RVUInfo RVUInfo;
-struct RVUInfo {
-    Collection VisitedLines;           /* Lines already visited */
+/* Table with names of zero page locations used by the compiler */
+typedef struct ZPInfo ZPInfo;
+struct ZPInfo {
+    unsigned char Len;         /* Length of the following string */
+    char          Name[11];     /* Name of zero page symbol */
 };
-
+static const ZPInfo ZPInfoTable[] = {
+    {          4,      "ptr1"          },
+    {   7,      "regbank"       },
+    {   7,      "regsave"       },
+    {   2,      "sp"            },
+    {   4,      "sreg"          },
+    {   4,      "tmp1"          },
+};
+#define ZPInfoCount            (sizeof(ZPInfoTable) / sizeof(ZPInfoTable[0]))
 
 
 /*****************************************************************************/
-/*                                          Code                                    */
+/*                                          Code                                    */
 /*****************************************************************************/
 
 
@@ -121,193 +186,278 @@ static int CompareFuncInfo (const void* Key, const void* Info)
 
 
 void GetFuncInfo (const char* Name, unsigned char* Use, unsigned char* Chg)
-/* For the given function, lookup register information and combine it with
- * the information already in place. If the function is unknown, assume it
- * will use all registers and load all registers.
- * See codeinfo.h for possible flags.
+/* For the given function, lookup register information and store it into
+ * the given variables. If the function is unknown, assume it will use and
+ * load all registers.
  */
 {
-    /* Search for the function */
-    const FuncInfo* Info = bsearch (Name, FuncInfoTable, FuncInfoCount,
-                                   sizeof(FuncInfo), CompareFuncInfo);
-
-    /* Do we know the function? */
-    if (Info) {
-       /* Use the information we have */
-       *Use |= Info->Use;
-       *Chg |= Info->Chg;
+    /* If the function name starts with an underline, it is an external
+     * function. Search for it in the symbol table. If the function does
+     * not start with an underline, it may be a runtime support function.
+     * Search for it in the list of builtin functions.
+     */
+    if (Name[0] == '_') {
+
+       /* Search in the symbol table, skip the leading underscore */
+       SymEntry* E = FindGlobalSym (Name+1);
+
+       /* Did we find it in the top level table? */
+       if (E && IsTypeFunc (E->Type)) {
+
+           /* A function may use the A or A/X registers if it is a fastcall
+            * function. If it is not a fastcall function but a variadic one,
+            * it will use the Y register (the parameter size is passed here).
+            * In all other cases, no registers are used. However, we assume
+            * that any function will destroy all registers.
+            */
+           FuncDesc* D = E->V.F.Func;
+           if ((D->Flags & FD_FASTCALL) != 0 && D->ParamCount > 0) {
+               /* Will use registers depending on the last param */
+               SymEntry* LastParam = D->SymTab->SymTail;
+               if (SizeOf (LastParam->Type) == 1) {
+                   *Use = REG_A;
+               } else {
+                   *Use = REG_AX;
+               }
+           } else if ((D->Flags & FD_VARIADIC) != 0) {
+               *Use = REG_Y;
+           } else {
+               /* Will not use any registers */
+               *Use = REG_NONE;
+           }
+
+           /* Will destroy all registers */
+           *Chg = REG_AXY;
+
+           /* Done */
+           return;
+       }
+
     } else {
-       *Use |= REG_AXY;
-       *Chg |= REG_AXY;
+
+       /* Search for the function in the list of builtin functions */
+       const FuncInfo* Info = bsearch (Name, FuncInfoTable, FuncInfoCount,
+                                       sizeof(FuncInfo), CompareFuncInfo);
+
+       /* Do we know the function? */
+       if (Info) {
+           /* Use the information we have */
+           *Use = Info->Use;
+           *Chg = Info->Chg;
+           return;
+       }
     }
+
+    /* Function not found - assume all registers used */
+    *Use = REG_AXY;
+    *Chg = REG_AXY;
 }
 
-                                       
-#if 0
 
-static unsigned RVUInt2 (Line* L,
-                        LineColl* LC,      /* To remember visited lines */
-                        unsigned Used,     /* Definitely used registers */
-                        unsigned Unused)   /* Definitely unused registers */
-/* Subfunction for RegValUsed. Will be called recursively in case of branches. */
+
+int IsZPName (const char* Name)
+/* Return true if the given name is a zero page symbol */
 {
-    int I;
+    unsigned I;
+    const ZPInfo* Info;
+
+    /* Because of the low number of symbols, we do a linear search here */
+    for (I = 0, Info = ZPInfoTable; I < ZPInfoCount; ++I, ++Info) {
+               if (strncmp (Name, Info->Name, Info->Len) == 0 &&
+           (Name[Info->Len] == '\0' || Name[Info->Len] == '+')) {
+           /* Found */
+           return 1;
+       }
+    }
 
-    /* Check the following instructions. We classifiy them into primary
-     * loads (register value not used), neutral (check next instruction),
-     * and unknown (assume register was used).
-     */
-    while (1) {
+    /* Not found */
+    return 0;
+}
 
-       unsigned R;
-
-       /* Get the next line and follow jumps */
-       do {
-
-           /* Handle jumps to local labels (continue there) */
-                   if (LineMatch (L, "\tjmp\tL") || LineMatch (L, "\tbra\tL")) {
-               /* Get the target of the jump */
-               L = GetTargetLine (L->Line+5);
-           }
-
-           /* Get the next line, skip local labels */
-           do {
-                       L = NextCodeSegLine (L);
-           } while (L && (IsLocalLabel (L) || L->Line[0] == '\0'));
-
-           /* Bail out if we're done */
-           if (L == 0 || IsExtLabel (L)) {
-               /* End of function reached */
-               goto ExitPoint;
-           }
-
-           /* Check if we had this line already. If so, bail out, if not,
-            * add it to the list of known lines.
-            */
-           if (LCHasLine (LC, L) || !LCAddLine (LC, L)) {
-               goto ExitPoint;
-           }
-
-       } while (LineMatch (L, "\tjmp\tL") || LineMatch (L, "\tbra\tL"));
-
-       /* Special handling of code hints */
-               if (IsHintLine (L)) {
-
-           if (IsHint (L, "a:-") && (Used & REG_A) == 0) {
-               Unused |= REG_A;
-           } else if (IsHint (L, "x:-") && (Used & REG_X) == 0) {
-               Unused |= REG_X;
-           } else if (IsHint (L, "y:-") && (Used & REG_Y) == 0) {
-               Unused |= REG_Y;
-           }
-
-       /* Special handling for branches */
-       } else if (LineMatchX (L, ShortBranches) >= 0 ||
-           LineMatchX (L, LongBranches) >= 0) {
-           const char* Target = L->Line+5;
-           if (Target[0] == 'L') {
-               /* Jump to local label. Check the register usage starting at
-                * the branch target and at the code following the branch.
-                * All registers that are unused in both execution flows are
-                * returned as unused.
-                */
-               unsigned U1, U2;
-                       U2 = RVUInt1 (GetTargetLine (Target), LC, Used, Unused);
-               U1 = RVUInt1 (L, LC, Used, Unused);
-               return U1 | U2;         /* Used in any of the branches */
-           }
-       } else {
-
-           /* Search for the instruction in this line */
-           I = FindCmd (L);
-
-           /* If we don't find it, assume all other registers are used */
-           if (I < 0) {
-               break;
-           }
-
-           /* Evaluate the use flags, check for addressing modes */
-           R = CmdDesc[I].Use;
-           if (IsXAddrMode (L)) {
-               R |= REG_X;
-           } else if (IsYAddrMode (L)) {
-               R |= REG_Y;
-           }
-           if (R) {
-               /* Remove registers that were already new loaded */
-               R &= ~Unused;
-
-               /* Remember the remaining registers */
-               Used |= R;
-           }
-
-           /* Evaluate the load flags */
-           R = CmdDesc[I].Load;
-           if (R) {
-               /* Remove registers that were already used */
-               R &= ~Used;
-
-               /* Remember the remaining registers */
-               Unused |= R;
-           }
 
-       }
 
-               /* If we know about all registers, bail out */
-               if ((Used | Unused) == REG_ALL) {
+static unsigned char GetRegInfo2 (CodeSeg* S,
+                                 CodeEntry* E,
+                                 int Index,
+                                 Collection* Visited,
+                                 unsigned char Used,
+                                 unsigned char Unused)
+/* Recursively called subfunction for GetRegInfo. */
+{
+    /* Follow the instruction flow recording register usage. */
+    while (1) {
+
+       unsigned char R;
+
+       /* Check if we have already visited the current code entry. If so,
+        * bail out.
+        */
+       if (CE_HasMark (E)) {
+           break;
+       }
+
+       /* Mark this entry as already visited */
+       CE_SetMark (E);
+       CollAppend (Visited, E);
+
+       /* Evaluate the used registers */
+       R = E->Use;
+       if (E->OPC == OP65_RTS ||
+           ((E->Info & OF_BRA) != 0 && E->JumpTo == 0)) {
+           /* This instruction will leave the function */
+           R |= S->ExitRegs;
+       }
+               if (R != REG_NONE) {
+           /* We are not interested in the use of any register that has been
+            * used before.
+            */
+           R &= ~Unused;
+           /* Remember the remaining registers */
+           Used |= R;
+       }
+
+       /* Evaluate the changed registers */
+               if ((R = E->Chg) != REG_NONE) {
+           /* We are not interested in the use of any register that has been
+            * used before.
+            */
+           R &= ~Used;
+           /* Remember the remaining registers */
+           Unused |= R;
+       }
+
+               /* If we know about all registers now, bail out */
+               if ((Used | Unused) == REG_AXY) {
            break;
        }
+
+       /* If the instruction is an RTS or RTI, we're done */
+               if ((E->Info & OF_RET) != 0) {
+           break;
+       }
+
+       /* If we have an unconditional branch, follow this branch if possible,
+        * otherwise we're done.
+        */
+       if ((E->Info & OF_UBRA) != 0) {
+
+           /* Does this jump have a valid target? */
+           if (E->JumpTo) {
+
+               /* Unconditional jump */
+               E     = E->JumpTo->Owner;
+               Index = -1;             /* Invalidate */
+
+           } else {
+               /* Jump outside means we're done */
+               break;
+           }
+
+               /* In case of conditional branches, follow the branch if possible and
+        * follow the normal flow (branch not taken) afterwards. If we cannot
+        * follow the branch, we're done.
+        */
+       } else if ((E->Info & OF_CBRA) != 0) {
+
+           if (E->JumpTo) {
+
+               /* Recursively determine register usage at the branch target */
+               unsigned char U1;
+               unsigned char U2;
+
+               U1 = GetRegInfo2 (S, E->JumpTo->Owner, -1, Visited, Used, Unused);
+               if (U1 == REG_AXY) {
+                   /* All registers used, no need for second call */
+                   return REG_AXY;
+               }
+               if (Index < 0) {
+                   Index = CS_GetEntryIndex (S, E);
+               }
+                       if ((E = CS_GetEntry (S, ++Index)) == 0) {
+                   Internal ("GetRegInfo2: No next entry!");
+               }
+               U2 = GetRegInfo2 (S, E, Index, Visited, Used, Unused);
+               return U1 | U2;         /* Used in any of the branches */
+
+           } else {
+               /* Jump to global symbol */
+               break;
+           }
+
+               } else {
+
+           /* Just go to the next instruction */
+           if (Index < 0) {
+               Index = CS_GetEntryIndex (S, E);
+           }
+           E = CS_GetEntry (S, ++Index);
+           if (E == 0) {
+               /* No next entry */
+               Internal ("GetRegInfo2: No next entry!");
+           }
+
+       }
+
     }
 
-ExitPoint:
     /* Return to the caller the complement of all unused registers */
-    return ~Unused & REG_ALL;
+    return Used;
 }
 
 
 
-static unsigned RVUInt1 (Line* L,
-                        LineColl* LC,      /* To remember visited lines */
-                        unsigned Used,     /* Definitely used registers */
-                        unsigned Unused)   /* Definitely unused registers */
-/* Subfunction for RegValUsed. Will be called recursively in case of branches. */
+static unsigned char GetRegInfo1 (CodeSeg* S,
+                                 CodeEntry* E,
+                                 int Index,
+                                 Collection* Visited,
+                                 unsigned char Used,
+                                 unsigned char Unused)
+/* Recursively called subfunction for GetRegInfo. */
 {
     /* Remember the current count of the line collection */
-    unsigned Count = LC->Count;
+    unsigned Count = CollCount (Visited);
 
     /* Call the worker routine */
-    unsigned R = RVUInt2 (L, LC, Used, Unused);
-
-    /* Restore the old count */
-    LC->Count = Count;
+    unsigned char R = GetRegInfo2 (S, E, Index, Visited, Used, Unused);
+
+    /* Restore the old count, unmarking all new entries */
+    unsigned NewCount = CollCount (Visited);
+    while (NewCount-- > Count) {
+       CodeEntry* E = CollAt (Visited, NewCount);
+       CE_ResetMark (E);
+       CollDelete (Visited, NewCount);
+    }
 
-    /* Return the result */
+    /* Return the registers used */
     return R;
 }
 
 
 
-static unsigned RegValUsed (Line* Start)
-/* Check the next instructions after the one in L for register usage. If
- * a register is used as an index, or in a store or other instruction, it
- * is assumed to be used. If a register is loaded with a value, before it
- * was used by one of the actions described above, it is assumed unused.
- * If the end of the lookahead is reached, all registers that are uncertain
- * are marked as used.
- * The result of the search is returned.
+unsigned char GetRegInfo (struct CodeSeg* S, unsigned Index)
+/* Determine register usage information for the instructions starting at the
+ * given index.
  */
 {
-    unsigned R;
+    CodeEntry*     E;
+    Collection     Visited;    /* Visited entries */
+    unsigned char   R;
+
+    /* Get the code entry for the given index */
+    if (Index >= CS_GetEntryCount (S)) {
+       /* There is no such code entry */
+       return REG_NONE;
+    }
+    E = CS_GetEntry (S, Index);
 
-    /* Create a new line collection and enter the start line */
-    LineColl* LC = NewLineColl (256);
-    LCAddLine (LC, Start);
+    /* Initialize the data structure used to collection information */
+    InitCollection (&Visited);
 
     /* Call the recursive subfunction */
-    R = RVUInt1 (Start, LC, REG_NONE, REG_NONE);
+    R = GetRegInfo1 (S, E, Index, &Visited, REG_NONE, REG_NONE);
 
     /* Delete the line collection */
-    FreeLineColl (LC);
+    DoneCollection (&Visited);
 
     /* Return the registers used */
     return R;
@@ -315,28 +465,28 @@ static unsigned RegValUsed (Line* Start)
 
 
 
-static int RegAUsed (Line* Start)
+int RegAUsed (struct CodeSeg* S, unsigned Index)
 /* Check if the value in A is used. */
 {
-    return (RegValUsed (Start) & REG_A) != 0;
+    return (GetRegInfo (S, Index) & REG_A) != 0;
 }
 
 
 
-static int RegXUsed (Line* Start)
+int RegXUsed (struct CodeSeg* S, unsigned Index)
 /* Check if the value in X is used. */
 {
-    return (RegValUsed (Start) & REG_X) != 0;
+    return (GetRegInfo (S, Index) & REG_X) != 0;
 }
 
 
 
-static int RegYUsed (Line* Start)
+int RegYUsed (struct CodeSeg* S, unsigned Index)
 /* Check if the value in Y is used. */
 {
-    return (RegValUsed (Start) & REG_Y) != 0;
+    return (GetRegInfo (S, Index) & REG_Y) != 0;
 }
 
 
 
-#endif     
+