]> git.sur5r.net Git - cc65/blobdiff - src/cc65/codeinfo.c
Fixed wrong code generation for
[cc65] / src / cc65 / codeinfo.c
index abf8a3bf5bac00aa2c69cd7946e9bc533654fc47..fb6bea66702922544b8b4607099eeba86e2f0e55 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2001      Ullrich von Bassewitz                                       */
-/*               Wacholderweg 14                                             */
-/*               D-70597 Stuttgart                                           */
-/* EMail:        uz@cc65.org                                                 */
+/* (C) 2001-2006, Ullrich von Bassewitz                                      */
+/*                Römerstraße 52                                             */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
 #include <string.h>
 
 /* common */
+#include "chartype.h"
 #include "coll.h"
+#include "debugflag.h"
 
 /* cc65 */
 #include "codeent.h"
 #include "codeseg.h"
 #include "datatype.h"
 #include "error.h"
+#include "reginfo.h"
 #include "symtab.h"
 #include "codeinfo.h"
 
 
 
 
+/* Table with the compare suffixes */
+static const char CmpSuffixTab [][4] = {
+    "eq", "ne", "gt", "ge", "lt", "le", "ugt", "uge", "ult", "ule"
+};
+
 /* Table listing the function names and code info values for known internally
  * used functions. This table should get auto-generated in the future.
  */
@@ -66,11 +74,17 @@ struct FuncInfo {
 };
 
 static const FuncInfo FuncInfoTable[] = {
+    { "addeq0sp",       REG_AX,               REG_AXY                        },
+    { "addeqysp",       REG_AXY,              REG_AXY                        },
     { "addysp",                REG_Y,                REG_NONE                       },
     { "aslax1",         REG_AX,                      REG_AX | REG_TMP1              },
     { "aslax2",         REG_AX,                      REG_AX | REG_TMP1              },
     { "aslax3",         REG_AX,                      REG_AX | REG_TMP1              },
     { "aslax4",         REG_AX,                      REG_AX | REG_TMP1              },
+    { "asrax1",         REG_AX,                      REG_AX | REG_TMP1              },
+    { "asrax2",         REG_AX,                      REG_AX | REG_TMP1              },
+    { "asrax3",         REG_AX,                      REG_AX | REG_TMP1              },
+    { "asrax4",         REG_AX,                      REG_AX | REG_TMP1              },
     { "bnega",          REG_A,                REG_AX                        },
     { "bnegax",         REG_AX,               REG_AX                        },
     { "bnegeax",        REG_EAX,              REG_EAX                       },
@@ -84,6 +98,7 @@ static const FuncInfo FuncInfoTable[] = {
     { "boolugt",               REG_NONE,             REG_AX                         },
     { "boolule",               REG_NONE,             REG_AX                         },
     { "boolult",               REG_NONE,             REG_AX                         },
+    { "callax",         REG_AX,               REG_ALL                        },
     { "complax",        REG_AX,               REG_AX                        },
     { "decax1",                REG_AX,               REG_AX                         },
     { "decax2",                REG_AX,               REG_AX                         },
@@ -94,6 +109,7 @@ static const FuncInfo FuncInfoTable[] = {
     { "decax7",                REG_AX,               REG_AX                         },
     { "decax8",                REG_AX,               REG_AX                         },
     { "decaxy",                REG_AXY,              REG_AX | REG_TMP1              },
+    { "deceaxy",        REG_EAXY,             REG_EAX                        },
     { "decsp1",                REG_NONE,             REG_Y                          },
     { "decsp2",                REG_NONE,             REG_A                          },
     { "decsp3",                REG_NONE,             REG_A                          },
@@ -102,8 +118,15 @@ static const FuncInfo FuncInfoTable[] = {
     { "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                        },
+    { "incax1",         REG_AX,               REG_AX                        },
+    { "incax2",         REG_AX,               REG_AX                        },
+    { "incax3",         REG_AX,               REG_AXY | REG_TMP1             },
+    { "incax4",         REG_AX,               REG_AXY | REG_TMP1            },
+    { "incax5",         REG_AX,               REG_AXY | REG_TMP1            },
+    { "incax6",         REG_AX,               REG_AXY | REG_TMP1            },
+    { "incax7",         REG_AX,               REG_AXY | REG_TMP1            },
+    { "incax8",         REG_AX,               REG_AXY | REG_TMP1            },
+    { "incaxy",         REG_AXY,              REG_AXY | REG_TMP1                    },
     { "incsp1",                REG_NONE,             REG_NONE                       },
     { "incsp2",                REG_NONE,             REG_Y                          },
     { "incsp3",                REG_NONE,             REG_Y                          },
@@ -112,23 +135,61 @@ static const FuncInfo FuncInfoTable[] = {
     { "incsp6",                REG_NONE,             REG_Y                          },
     { "incsp7",                REG_NONE,             REG_Y                          },
     { "incsp8",                REG_NONE,             REG_Y                          },
-    { "laddeq",                REG_EAXY | REG_PTR1,  REG_EAXY                       },
-    { "laddeq1",               REG_Y | REG_PTR1,     REG_EAXY                       },
-    { "laddeqa",        REG_AY | REG_PTR1,    REG_EAXY                       },
+    { "laddeq",                REG_EAXY|REG_PTR1_LO, REG_EAXY | REG_PTR1_HI         },
+    { "laddeq0sp",      REG_EAX,              REG_EAXY                       },
+    { "laddeq1",               REG_Y | REG_PTR1_LO,  REG_EAXY | REG_PTR1_HI         },
+    { "laddeqa",        REG_AY | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI         },
+    { "laddeqysp",      REG_EAXY,             REG_EAXY                       },
     { "ldaidx",         REG_AXY,              REG_AX | REG_PTR1                     },
     { "ldauidx",        REG_AXY,              REG_AX | REG_PTR1                     },
-    { "ldax0sp",               REG_Y,                REG_AX                         },
+    { "ldax0sp",               REG_NONE,             REG_AXY                        },
     { "ldaxi",          REG_AX,               REG_AXY | REG_PTR1            },
-    { "ldaxidx",        REG_AXY,              REG_AX | REG_PTR1                     },
-    { "ldaxysp",               REG_Y,                REG_AX                         },
+    { "ldaxidx",        REG_AXY,              REG_AXY | REG_PTR1                    },
+    { "ldaxysp",               REG_Y,                REG_AXY                        },
+    { "ldeax0sp",       REG_NONE,             REG_EAXY                       },
+    { "ldeaxi",         REG_AX,               REG_EAXY | REG_PTR1            },
+    { "ldeaxidx",       REG_AXY,              REG_EAXY | REG_PTR1            },
+    { "ldeaxysp",       REG_Y,                REG_EAXY                       },
     { "leaasp",         REG_A,                REG_AX                        },
+    { "lsubeq",         REG_EAXY|REG_PTR1_LO, REG_EAXY | REG_PTR1_HI         },
+    { "lsubeq0sp",      REG_EAX,              REG_EAXY                       },
+    { "lsubeq1",        REG_Y | REG_PTR1_LO,  REG_EAXY | REG_PTR1_HI         },
+    { "lsubeqa",        REG_AY | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI         },
+    { "lsubeqysp",      REG_EAXY,             REG_EAXY                       },
+    { "mulax10",        REG_AX,               REG_AX | REG_PTR1              },
+    { "mulax3",         REG_AX,               REG_AX | REG_PTR1              },
+    { "mulax5",         REG_AX,               REG_AX | REG_PTR1              },
+    { "mulax6",         REG_AX,               REG_AX | REG_PTR1              },
+    { "mulax7",         REG_AX,               REG_AX | REG_PTR1              },
+    { "mulax9",         REG_AX,               REG_AX | REG_PTR1              },
     { "negax",          REG_AX,               REG_AX                        },
+    { "push0",                 REG_NONE,             REG_AXY                        },
+    { "push1",                 REG_NONE,             REG_AXY                        },
+    { "push2",                 REG_NONE,             REG_AXY                        },
+    { "push3",                 REG_NONE,             REG_AXY                        },
+    { "push4",                 REG_NONE,             REG_AXY                        },
+    { "push5",                 REG_NONE,             REG_AXY                        },
+    { "push6",                 REG_NONE,             REG_AXY                        },
+    { "push7",                 REG_NONE,             REG_AXY                        },
     { "pusha",                 REG_A,                REG_Y                          },
     { "pusha0",                REG_A,                REG_XY                         },
-    { "pushax",                REG_AX,               REG_Y                          },
+    { "pusha0sp",       REG_NONE,             REG_AY                         },
+    { "pushaFF",               REG_A,                REG_Y                          },
+    { "pushax",                REG_AX,               REG_Y                          },
+    { "pushaysp",       REG_Y,                REG_AY                         },
+    { "pushc0",                REG_NONE,             REG_A | REG_Y                  },
+    { "pushc1",                REG_NONE,             REG_A | REG_Y                  },
+    { "pushc2",                REG_NONE,             REG_A | REG_Y                  },
     { "pusheax",        REG_EAX,              REG_Y                         },
+    { "pushw",                 REG_AX,               REG_AXY | REG_PTR1             },
     { "pushw0sp",              REG_NONE,             REG_AXY                        },
+    { "pushwidx",              REG_AXY,              REG_AXY | REG_PTR1             },
     { "pushwysp",              REG_Y,                REG_AXY                        },
+    { "regswap",        REG_AXY,              REG_AXY | REG_TMP1             },
+    { "regswap1",       REG_XY,               REG_A                          },
+    { "regswap2",       REG_XY,               REG_A | REG_Y                  },
+    { "return0",        REG_NONE,             REG_AX                         },
+    { "return1",        REG_NONE,             REG_AX                         },
     { "shlax1",         REG_AX,                      REG_AX | REG_TMP1              },
     { "shlax2",         REG_AX,                      REG_AX | REG_TMP1              },
     { "shlax3",         REG_AX,                      REG_AX | REG_TMP1              },
@@ -142,46 +203,110 @@ static const FuncInfo FuncInfoTable[] = {
     { "shreax3",        REG_EAX,              REG_AX | REG_TMP1                     },
     { "shreax4",        REG_EAX,              REG_AX | REG_TMP1                     },
     { "staspidx",       REG_A | REG_Y,        REG_Y | REG_TMP1 | REG_PTR1    },
-    { "stax0sp",        REG_AX,               REG_Y                         },
+    { "stax0sp",        REG_AX,               REG_Y                         },
+    { "staxspidx",      REG_AXY,              REG_TMP1 | REG_PTR1            },
+    { "staxysp",        REG_AXY,              REG_Y                         },
+    { "steax0sp",       REG_EAX,              REG_Y                          },
+    { "steaxysp",       REG_EAXY,             REG_Y                          },
+    { "subeq0sp",       REG_AX,               REG_AXY                        },
+    { "subeqysp",       REG_AXY,              REG_AXY                        },
+    { "tosadda0",       REG_A,                REG_AXY                        },
+    { "tosaddax",       REG_AX,               REG_AXY                        },
+    { "tosanda0",       REG_A,                REG_AXY                        },
+    { "tosandax",       REG_AX,               REG_AXY                        },
+    { "tosaslax",       REG_A,                REG_AXY | REG_TMP1             },
+    { "tosasleax",      REG_A,                       REG_EAXY | REG_TMP1            },
+    { "tosasrax",       REG_A,                REG_AXY | REG_TMP1             },
+    { "tosasreax",      REG_A,                       REG_EAXY | REG_TMP1            },
+    { "tosdiva0",       REG_AY,              REG_ALL                        },
+    { "tosdivax",       REG_AXY,              REG_ALL                       },
+    { "tosdiveax",      REG_EAXY,             REG_ALL                       },
+    { "toseq00",        REG_NONE,                    REG_AXY | REG_SREG             },
+    { "toseqa0",        REG_A,               REG_AXY | REG_SREG             },
+    { "toseqax",        REG_AX,                      REG_AXY | REG_SREG             },
+    { "toseqeax",       REG_EAX,                     REG_AXY | REG_PTR1             },
+    { "tosge00",        REG_NONE,                    REG_AXY | REG_SREG             },
+    { "tosgea0",        REG_A,               REG_AXY | REG_SREG             },
+    { "tosgeax",        REG_AX,                      REG_AXY | REG_SREG             },
+    { "tosgeeax",       REG_EAX,                     REG_AXY | REG_PTR1             },
+    { "tosgt00",        REG_NONE,                    REG_AXY | REG_SREG             },
+    { "tosgta0",        REG_A,               REG_AXY | REG_SREG             },
+    { "tosgtax",        REG_AX,                      REG_AXY | REG_SREG             },
+    { "tosgteax",       REG_EAX,                     REG_AXY | REG_PTR1             },
     { "tosicmp",               REG_AX,               REG_AXY | REG_SREG             },
-    { "tosdiva0",       REG_AX,                      REG_ALL                        },
-    { "tosdivax",       REG_AX,                      REG_ALL                        },
-    { "tosdiveax",      REG_EAX,                     REG_ALL                        },
-    { "tosmula0",       REG_AX,                      REG_ALL                        },
-    { "tosmulax",       REG_AX,                      REG_ALL                        },
-    { "tosmuleax",      REG_EAX,                     REG_ALL                        },
-    { "tosshreax",      REG_EAX,                     REG_EAXY | REG_PTR1 | REG_PTR2 },
+    { "toslcmp",        REG_EAX,                     REG_A | REG_Y | REG_PTR1       },
+    { "tosle00",        REG_NONE,                    REG_AXY | REG_SREG             },
+    { "toslea0",        REG_A,               REG_AXY | REG_SREG             },
+    { "tosleax",        REG_AX,                      REG_AXY | REG_SREG             },
+    { "tosleeax",       REG_EAX,                     REG_AXY | REG_PTR1             },
+    { "toslt00",        REG_NONE,                    REG_AXY | REG_SREG             },
+    { "toslta0",        REG_A,               REG_AXY | REG_SREG             },
+    { "tosltax",        REG_AX,                      REG_AXY | REG_SREG             },
+    { "toslteax",       REG_EAX,                     REG_AXY | REG_PTR1             },
+    { "tosmula0",       REG_AX,                      REG_ALL                        },
+    { "tosmulax",       REG_AX,                      REG_ALL                        },
+    { "tosmuleax",      REG_EAX,                     REG_ALL                        },
+    { "tosne00",        REG_NONE,                    REG_AXY | REG_SREG             },
+    { "tosnea0",        REG_A,               REG_AXY | REG_SREG             },
+    { "tosneax",        REG_AX,                      REG_AXY | REG_SREG             },
+    { "tosneeax",       REG_EAX,                     REG_AXY | REG_PTR1             },
+    { "tosora0",        REG_A,               REG_AXY | REG_TMP1             },
+    { "tosorax",        REG_AX,                      REG_AXY | REG_TMP1             },
+    { "tosshlax",       REG_A,                REG_AXY | REG_TMP1             },
+    { "tosshleax",      REG_A,                       REG_EAXY | REG_TMP1            },
+    { "tosshrax",       REG_A,                REG_AXY | REG_TMP1             },
+    { "tosshreax",      REG_A,                       REG_EAXY | REG_TMP1            },
+    { "tossuba0",       REG_A,                REG_AXY                        },
+    { "tossubax",       REG_AX,               REG_AXY                        },
+    { "tossubeax",      REG_EAX,              REG_EAXY                       },
+    { "tosuge00",       REG_NONE,                    REG_AXY | REG_SREG             },
+    { "tosugea0",       REG_A,               REG_AXY | REG_SREG             },
+    { "tosugeax",       REG_AX,                      REG_AXY | REG_SREG             },
+    { "tosugeeax",      REG_EAX,                     REG_AXY | REG_PTR1             },
+    { "tosugt00",       REG_NONE,                    REG_AXY | REG_SREG             },
+    { "tosugta0",       REG_A,               REG_AXY | REG_SREG             },
+    { "tosugtax",       REG_AX,                      REG_AXY | REG_SREG             },
+    { "tosugteax",      REG_EAX,                     REG_AXY | REG_PTR1             },
+    { "tosule00",       REG_NONE,                    REG_AXY | REG_SREG             },
+    { "tosulea0",       REG_A,               REG_AXY | REG_SREG             },
+    { "tosuleax",       REG_AX,                      REG_AXY | REG_SREG             },
+    { "tosuleeax",      REG_EAX,                     REG_AXY | REG_PTR1             },
+    { "tosult00",       REG_NONE,                    REG_AXY | REG_SREG             },
+    { "tosulta0",       REG_A,               REG_AXY | REG_SREG             },
+    { "tosultax",       REG_AX,                      REG_AXY | REG_SREG             },
+    { "tosulteax",      REG_EAX,                     REG_AXY | REG_PTR1             },
     { "tosumula0",      REG_AX,                      REG_ALL                        },
     { "tosumulax",      REG_AX,                      REG_ALL                        },
     { "tosumuleax",     REG_EAX,                     REG_ALL                        },
+    { "tsteax",         REG_EAX,              REG_Y                          },
+    { "utsteax",        REG_EAX,              REG_Y                          },
 };
 #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 */
-    unsigned short RegInfo;     /* Register info for this symbol */
-};
 static const ZPInfo ZPInfoTable[] = {
-    {          4,      "ptr1",         REG_PTR1        },
-    {          4,      "ptr2",         REG_PTR2        },
-    {          4,      "ptr3",         REG_PTR3        },
-    {          4,      "ptr4",         REG_PTR4        },
-    {   7,      "regbank",      REG_BANK        },
-    {   7,      "regsave",      REG_SAVE        },
-    {   2,      "sp",           REG_SP          },
-    {   0,      "sreg",         REG_SREG_LO     },
-    {  0,      "sreg+1",       REG_SREG_HI     },
-    {   4,      "tmp1",         REG_TMP1        },
-    {   4,      "tmp2",         REG_TMP2        },
-    {   4,      "tmp3",         REG_TMP3        },
-    {   4,      "tmp4",         REG_TMP4        },
+    {          0, "ptr1",      REG_PTR1_LO,    REG_PTR1        },
+    {   0, "ptr1+1",    REG_PTR1_HI,   REG_PTR1        },
+    {          0, "ptr2",      REG_PTR2_LO,    REG_PTR2        },
+    {   0, "ptr2+1",    REG_PTR2_HI,   REG_PTR2        },
+    {          4, "ptr3",      REG_NONE,       REG_NONE        },
+    {          4, "ptr4",      REG_NONE,       REG_NONE        },
+    {   7, "regbank",   REG_NONE,      REG_NONE        },
+    {   0, "regsave",   REG_SAVE_LO,   REG_SAVE        },
+    {   0, "regsave+1", REG_SAVE_HI,   REG_SAVE        },
+    {   0, "sp",        REG_SP_LO,             REG_SP          },
+    {   0, "sp+1",      REG_SP_HI,      REG_SP          },
+    {   0, "sreg",      REG_SREG_LO,   REG_SREG        },
+    {  0, "sreg+1",    REG_SREG_HI,    REG_SREG        },
+    {   0, "tmp1",      REG_TMP1,      REG_TMP1        },
+    {   0, "tmp2",      REG_NONE,      REG_NONE        },
+    {   0, "tmp3",      REG_NONE,      REG_NONE        },
+    {   0, "tmp4",      REG_NONE,      REG_NONE        },
 };
 #define ZPInfoCount            (sizeof(ZPInfoTable) / sizeof(ZPInfoTable[0]))
 
 
+
 /*****************************************************************************/
 /*                                          Code                                    */
 /*****************************************************************************/
@@ -224,12 +349,14 @@ void GetFuncInfo (const char* Name, unsigned short* Use, unsigned short* Chg)
            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) {
+                unsigned LastParamSize = CheckedSizeOf (D->LastParam->Type);
+               if (LastParamSize == 1) {
                    *Use = REG_A;
-               } else {
+               } else if (LastParamSize == 2) {
                    *Use = REG_AX;
-               }
+               } else {
+                    *Use = REG_EAX;
+                }
            } else if ((D->Flags & FD_VARIADIC) != 0) {
                *Use = REG_Y;
            } else {
@@ -244,6 +371,16 @@ void GetFuncInfo (const char* Name, unsigned short* Use, unsigned short* Chg)
            return;
        }
 
+    } else if (IsDigit (Name[0]) || Name[0] == '$') {
+
+        /* A call to a numeric address. Assume that anything gets used and
+         * destroyed. This is not a real problem, since numeric addresses
+         * are used mostly in inline assembly anyway.
+         */
+        *Use = REG_ALL;
+        *Chg = REG_ALL;
+        return;
+
     } else {
 
        /* Search for the function in the list of builtin functions */
@@ -255,14 +392,25 @@ void GetFuncInfo (const char* Name, unsigned short* Use, unsigned short* Chg)
            /* Use the information we have */
            *Use = Info->Use;
            *Chg = Info->Chg;
-           return;
-       }
+       } else {
+            /* It's an internal function we have no information for. If in
+             * debug mode, output an additional warning, so we have a chance
+             * to fix it. Otherwise assume that the internal function will
+             * use and change all registers.
+             */
+            if (Debug) {
+                fprintf (stderr, "No info about internal function `%s'\n", Name);
+            }
+            *Use = REG_ALL;
+            *Chg = REG_ALL;
+        }
+        return;
     }
 
-    /* Function not found - assume all CPU registers are input, and all
+    /* Function not found - assume that the primary register is input, and all
      * registers are changed
      */
-    *Use = REG_AXY;
+    *Use = REG_EAXY;
     *Chg = REG_ALL;
 }
 
@@ -294,27 +442,14 @@ static int CompareZPInfo (const void* Name, const void* Info)
 
 
 
-int IsZPName (const char* Name, unsigned short* RegInfo)
-/* Return true if the given name is a zero page symbol. If the RegInfo
- * pointer is not NULL, it is filled with the register info for the
- * zero page location found.
+const ZPInfo* GetZPInfo (const char* Name)
+/* If the given name is a zero page symbol, return a pointer to the info
+ * struct for this symbol, otherwise return NULL.
  */
 {
     /* Search for the zp location in the list */
-    const ZPInfo* Info = bsearch (Name, ZPInfoTable, ZPInfoCount,
-                                 sizeof(ZPInfo), CompareZPInfo);
-
-    /* Did we find it? */
-    if (Info) {
-       /* Found, store register info if requested. */
-       if (RegInfo) {
-           *RegInfo = Info->RegInfo;
-       }
-       return 1;
-    } else {
-       /* Not found */
-       return 0;
-    }
+    return bsearch (Name, ZPInfoTable, ZPInfoCount,
+                   sizeof(ZPInfo), CompareZPInfo);
 }
 
 
@@ -347,7 +482,7 @@ static unsigned GetRegInfo2 (CodeSeg* S,
        /* Evaluate the used registers */
        R = E->Use;
        if (E->OPC == OP65_RTS ||
-           ((E->Info & OF_BRA) != 0 && E->JumpTo == 0)) {
+                   ((E->Info & OF_UBRA) != 0 && E->JumpTo == 0)) {
            /* This instruction will leave the function */
            R |= S->ExitRegs;
        }
@@ -403,30 +538,37 @@ static unsigned GetRegInfo2 (CodeSeg* S,
         */
        } else if ((E->Info & OF_CBRA) != 0) {
 
-           if (E->JumpTo) {
+            /* Recursively determine register usage at the branch target */
+           unsigned U1;
+           unsigned U2;
 
-               /* Recursively determine register usage at the branch target */
-               unsigned U1;
-               unsigned U2;
+           if (E->JumpTo) {
 
+                /* Jump to internal label */
                U1 = GetRegInfo2 (S, E->JumpTo->Owner, -1, Visited, Used, Unused, Wanted);
-               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, Wanted);
-               return U1 | U2;         /* Used in any of the branches */
 
-           } else {
-               /* Jump to global symbol */
-               break;
-           }
+            } else {
+
+                /* Jump to external label. This will effectively exit the
+                 * function, so we use the exitregs information here.
+                 */
+                U1 = S->ExitRegs;
+
+            }
+
+            /* Get the next entry */
+            if (Index < 0) {
+                Index = CS_GetEntryIndex (S, E);
+            }
+            if ((E = CS_GetEntry (S, ++Index)) == 0) {
+                Internal ("GetRegInfo2: No next entry!");
+            }
+
+            /* Follow flow if branch not taken */
+            U2 = GetRegInfo2 (S, E, Index, Visited, Used, Unused, Wanted);
+
+            /* Registers are used if they're use in any of the branches */
+            return U1 | U2;
 
                } else {
 
@@ -534,5 +676,104 @@ int RegYUsed (struct CodeSeg* S, unsigned Index)
 
 
 
+int RegAXUsed (struct CodeSeg* S, unsigned Index)
+/* Check if the value in A or(!) the value in X are used. */
+{
+    return (GetRegInfo (S, Index, REG_AX) & REG_AX) != 0;
+}
+
+
+
+int RegEAXUsed (struct CodeSeg* S, unsigned Index)
+/* Check if any of the four bytes in EAX are used. */
+{
+    return (GetRegInfo (S, Index, REG_EAX) & REG_EAX) != 0;
+}
+
+
+
+unsigned GetKnownReg (unsigned Use, const RegContents* RC)
+/* Return the register or zero page location from the set in Use, thats
+ * contents are known. If Use does not contain any register, or if the
+ * register in question does not have a known value, return REG_NONE.
+ */
+{
+    if ((Use & REG_A) != 0) {
+       return (RC == 0 || RC->RegA >= 0)? REG_A : REG_NONE;
+    } else if ((Use & REG_X) != 0) {
+       return (RC == 0 || RC->RegX >= 0)? REG_X : REG_NONE;
+    } else if ((Use & REG_Y) != 0) {
+       return (RC == 0 || RC->RegY >= 0)? REG_Y : REG_NONE;
+    } else if ((Use & REG_TMP1) != 0) {
+       return (RC == 0 || RC->Tmp1 >= 0)? REG_TMP1 : REG_NONE;
+    } else if ((Use & REG_PTR1_LO) != 0) {
+       return (RC == 0 || RC->Ptr1Lo >= 0)? REG_PTR1_LO : REG_NONE;
+    } else if ((Use & REG_PTR1_HI) != 0) {
+       return (RC == 0 || RC->Ptr1Hi >= 0)? REG_PTR1_HI : REG_NONE;
+    } else if ((Use & REG_SREG_LO) != 0) {
+       return (RC == 0 || RC->SRegLo >= 0)? REG_SREG_LO : REG_NONE;
+    } else if ((Use & REG_SREG_HI) != 0) {
+       return (RC == 0 || RC->SRegHi >= 0)? REG_SREG_HI : REG_NONE;
+    } else {
+       return REG_NONE;
+    }
+}
+
+
+
+static cmp_t FindCmpCond (const char* Code, unsigned CodeLen)
+/* Search for a compare condition by the given code using the given length */
+{
+    unsigned I;
+
+    /* Linear search */
+    for (I = 0; I < sizeof (CmpSuffixTab) / sizeof (CmpSuffixTab [0]); ++I) {
+       if (strncmp (Code, CmpSuffixTab [I], CodeLen) == 0) {
+           /* Found */
+           return I;
+       }
+    }
+
+    /* Not found */
+    return CMP_INV;
+}
+
+
+
+cmp_t FindBoolCmpCond (const char* Name)
+/* Check if the given string is the name of one of the boolean transformer
+ * subroutine, and if so, return the condition that is evaluated by this
+ * routine. Return CMP_INV if the condition is not recognised.
+ */
+{
+    /* Check for the correct subroutine name */
+    if (strncmp (Name, "bool", 4) == 0) {
+       /* Name is ok, search for the code in the table */
+       return FindCmpCond (Name+4, strlen(Name)-4);
+    } else {
+       /* Not found */
+       return CMP_INV;
+    }
+}
+
+
+
+cmp_t FindTosCmpCond (const char* Name)
+/* Check if this is a call to one of the TOS compare functions (tosgtax).
+ * Return the condition code or CMP_INV on failure.
+ */
+{
+    unsigned Len = strlen (Name);
+
+    /* Check for the correct subroutine name */
+    if (strncmp (Name, "tos", 3) == 0 && strcmp (Name+Len-2, "ax") == 0) {
+       /* Name is ok, search for the code in the table */
+       return FindCmpCond (Name+3, Len-3-2);
+    } else {
+       /* Not found */
+       return CMP_INV;
+    }
+}
+