/* cc65 */
#include "codeent.h"
#include "codeseg.h"
+#include "datatype.h"
#include "error.h"
+#include "symtab.h"
#include "codeinfo.h"
{ "decax3", REG_AX, REG_AX },
{ "decax4", REG_AX, REG_AX },
{ "decax5", REG_AX, REG_AX },
- { "decax6", 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 },
- { "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 },
- { "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 },
+ { "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 },
+ { "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 },
+ { "ldaui", REG_AX, REG_AXY },
+ { "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 },
+ { "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 },
};
#define FuncInfoCount (sizeof(FuncInfoTable) / sizeof(FuncInfoTable[0]))
+/* 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 */
/*****************************************************************************/
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 {
- /* Assume all registers used */
- *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;
+}
+
+
+
+int IsZPName (const char* Name)
+/* Return true if the given name is a zero page symbol */
+{
+ 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;
+ }
}
+
+ /* Not found */
+ return 0;
}
CollAppend (Visited, E);
/* Evaluate the used registers */
- if ((R = E->Use) != REG_NONE) {
+ R = E->Use;
+ if (E->OPC == OPC_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.
*/