]> git.sur5r.net Git - cc65/blob - src/cc65/codeinfo.c
fb6bea66702922544b8b4607099eeba86e2f0e55
[cc65] / src / cc65 / codeinfo.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                codeinfo.c                                 */
4 /*                                                                           */
5 /*                  Additional information about 6502 code                   */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2001-2006, Ullrich von Bassewitz                                      */
10 /*                Römerstraße 52                                             */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdlib.h>
37 #include <string.h>
38
39 /* common */
40 #include "chartype.h"
41 #include "coll.h"
42 #include "debugflag.h"
43
44 /* cc65 */
45 #include "codeent.h"
46 #include "codeseg.h"
47 #include "datatype.h"
48 #include "error.h"
49 #include "reginfo.h"
50 #include "symtab.h"
51 #include "codeinfo.h"
52
53
54
55 /*****************************************************************************/
56 /*                                   Data                                    */
57 /*****************************************************************************/
58
59
60
61 /* Table with the compare suffixes */
62 static const char CmpSuffixTab [][4] = {
63     "eq", "ne", "gt", "ge", "lt", "le", "ugt", "uge", "ult", "ule"
64 };
65
66 /* Table listing the function names and code info values for known internally
67  * used functions. This table should get auto-generated in the future.
68  */
69 typedef struct FuncInfo FuncInfo;
70 struct FuncInfo {
71     const char*     Name;       /* Function name */
72     unsigned short  Use;        /* Register usage */
73     unsigned short  Chg;        /* Changed/destroyed registers */
74 };
75
76 static const FuncInfo FuncInfoTable[] = {
77     { "addeq0sp",       REG_AX,               REG_AXY                        },
78     { "addeqysp",       REG_AXY,              REG_AXY                        },
79     { "addysp",         REG_Y,                REG_NONE                       },
80     { "aslax1",         REG_AX,               REG_AX | REG_TMP1              },
81     { "aslax2",         REG_AX,               REG_AX | REG_TMP1              },
82     { "aslax3",         REG_AX,               REG_AX | REG_TMP1              },
83     { "aslax4",         REG_AX,               REG_AX | REG_TMP1              },
84     { "asrax1",         REG_AX,               REG_AX | REG_TMP1              },
85     { "asrax2",         REG_AX,               REG_AX | REG_TMP1              },
86     { "asrax3",         REG_AX,               REG_AX | REG_TMP1              },
87     { "asrax4",         REG_AX,               REG_AX | REG_TMP1              },
88     { "bnega",          REG_A,                REG_AX                         },
89     { "bnegax",         REG_AX,               REG_AX                         },
90     { "bnegeax",        REG_EAX,              REG_EAX                        },
91     { "booleq",         REG_NONE,             REG_AX                         },
92     { "boolge",         REG_NONE,             REG_AX                         },
93     { "boolgt",         REG_NONE,             REG_AX                         },
94     { "boolle",         REG_NONE,             REG_AX                         },
95     { "boollt",         REG_NONE,             REG_AX                         },
96     { "boolne",         REG_NONE,             REG_AX                         },
97     { "booluge",        REG_NONE,             REG_AX                         },
98     { "boolugt",        REG_NONE,             REG_AX                         },
99     { "boolule",        REG_NONE,             REG_AX                         },
100     { "boolult",        REG_NONE,             REG_AX                         },
101     { "callax",         REG_AX,               REG_ALL                        },
102     { "complax",        REG_AX,               REG_AX                         },
103     { "decax1",         REG_AX,               REG_AX                         },
104     { "decax2",         REG_AX,               REG_AX                         },
105     { "decax3",         REG_AX,               REG_AX                         },
106     { "decax4",         REG_AX,               REG_AX                         },
107     { "decax5",         REG_AX,               REG_AX                         },
108     { "decax6",         REG_AX,               REG_AX                         },
109     { "decax7",         REG_AX,               REG_AX                         },
110     { "decax8",         REG_AX,               REG_AX                         },
111     { "decaxy",         REG_AXY,              REG_AX | REG_TMP1              },
112     { "deceaxy",        REG_EAXY,             REG_EAX                        },
113     { "decsp1",         REG_NONE,             REG_Y                          },
114     { "decsp2",         REG_NONE,             REG_A                          },
115     { "decsp3",         REG_NONE,             REG_A                          },
116     { "decsp4",         REG_NONE,             REG_A                          },
117     { "decsp5",         REG_NONE,             REG_A                          },
118     { "decsp6",         REG_NONE,             REG_A                          },
119     { "decsp7",         REG_NONE,             REG_A                          },
120     { "decsp8",         REG_NONE,             REG_A                          },
121     { "incax1",         REG_AX,               REG_AX                         },
122     { "incax2",         REG_AX,               REG_AX                         },
123     { "incax3",         REG_AX,               REG_AXY | REG_TMP1             },
124     { "incax4",         REG_AX,               REG_AXY | REG_TMP1             },
125     { "incax5",         REG_AX,               REG_AXY | REG_TMP1             },
126     { "incax6",         REG_AX,               REG_AXY | REG_TMP1             },
127     { "incax7",         REG_AX,               REG_AXY | REG_TMP1             },
128     { "incax8",         REG_AX,               REG_AXY | REG_TMP1             },
129     { "incaxy",         REG_AXY,              REG_AXY | REG_TMP1             },
130     { "incsp1",         REG_NONE,             REG_NONE                       },
131     { "incsp2",         REG_NONE,             REG_Y                          },
132     { "incsp3",         REG_NONE,             REG_Y                          },
133     { "incsp4",         REG_NONE,             REG_Y                          },
134     { "incsp5",         REG_NONE,             REG_Y                          },
135     { "incsp6",         REG_NONE,             REG_Y                          },
136     { "incsp7",         REG_NONE,             REG_Y                          },
137     { "incsp8",         REG_NONE,             REG_Y                          },
138     { "laddeq",         REG_EAXY|REG_PTR1_LO, REG_EAXY | REG_PTR1_HI         },
139     { "laddeq0sp",      REG_EAX,              REG_EAXY                       },
140     { "laddeq1",        REG_Y | REG_PTR1_LO,  REG_EAXY | REG_PTR1_HI         },
141     { "laddeqa",        REG_AY | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI         },
142     { "laddeqysp",      REG_EAXY,             REG_EAXY                       },
143     { "ldaidx",         REG_AXY,              REG_AX | REG_PTR1              },
144     { "ldauidx",        REG_AXY,              REG_AX | REG_PTR1              },
145     { "ldax0sp",        REG_NONE,             REG_AXY                        },
146     { "ldaxi",          REG_AX,               REG_AXY | REG_PTR1             },
147     { "ldaxidx",        REG_AXY,              REG_AXY | REG_PTR1             },
148     { "ldaxysp",        REG_Y,                REG_AXY                        },
149     { "ldeax0sp",       REG_NONE,             REG_EAXY                       },
150     { "ldeaxi",         REG_AX,               REG_EAXY | REG_PTR1            },
151     { "ldeaxidx",       REG_AXY,              REG_EAXY | REG_PTR1            },
152     { "ldeaxysp",       REG_Y,                REG_EAXY                       },
153     { "leaasp",         REG_A,                REG_AX                         },
154     { "lsubeq",         REG_EAXY|REG_PTR1_LO, REG_EAXY | REG_PTR1_HI         },
155     { "lsubeq0sp",      REG_EAX,              REG_EAXY                       },
156     { "lsubeq1",        REG_Y | REG_PTR1_LO,  REG_EAXY | REG_PTR1_HI         },
157     { "lsubeqa",        REG_AY | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI         },
158     { "lsubeqysp",      REG_EAXY,             REG_EAXY                       },
159     { "mulax10",        REG_AX,               REG_AX | REG_PTR1              },
160     { "mulax3",         REG_AX,               REG_AX | REG_PTR1              },
161     { "mulax5",         REG_AX,               REG_AX | REG_PTR1              },
162     { "mulax6",         REG_AX,               REG_AX | REG_PTR1              },
163     { "mulax7",         REG_AX,               REG_AX | REG_PTR1              },
164     { "mulax9",         REG_AX,               REG_AX | REG_PTR1              },
165     { "negax",          REG_AX,               REG_AX                         },
166     { "push0",          REG_NONE,             REG_AXY                        },
167     { "push1",          REG_NONE,             REG_AXY                        },
168     { "push2",          REG_NONE,             REG_AXY                        },
169     { "push3",          REG_NONE,             REG_AXY                        },
170     { "push4",          REG_NONE,             REG_AXY                        },
171     { "push5",          REG_NONE,             REG_AXY                        },
172     { "push6",          REG_NONE,             REG_AXY                        },
173     { "push7",          REG_NONE,             REG_AXY                        },
174     { "pusha",          REG_A,                REG_Y                          },
175     { "pusha0",         REG_A,                REG_XY                         },
176     { "pusha0sp",       REG_NONE,             REG_AY                         },
177     { "pushaFF",        REG_A,                REG_Y                          },
178     { "pushax",         REG_AX,               REG_Y                          },
179     { "pushaysp",       REG_Y,                REG_AY                         },
180     { "pushc0",         REG_NONE,             REG_A | REG_Y                  },
181     { "pushc1",         REG_NONE,             REG_A | REG_Y                  },
182     { "pushc2",         REG_NONE,             REG_A | REG_Y                  },
183     { "pusheax",        REG_EAX,              REG_Y                          },
184     { "pushw",          REG_AX,               REG_AXY | REG_PTR1             },
185     { "pushw0sp",       REG_NONE,             REG_AXY                        },
186     { "pushwidx",       REG_AXY,              REG_AXY | REG_PTR1             },
187     { "pushwysp",       REG_Y,                REG_AXY                        },
188     { "regswap",        REG_AXY,              REG_AXY | REG_TMP1             },
189     { "regswap1",       REG_XY,               REG_A                          },
190     { "regswap2",       REG_XY,               REG_A | REG_Y                  },
191     { "return0",        REG_NONE,             REG_AX                         },
192     { "return1",        REG_NONE,             REG_AX                         },
193     { "shlax1",         REG_AX,               REG_AX | REG_TMP1              },
194     { "shlax2",         REG_AX,               REG_AX | REG_TMP1              },
195     { "shlax3",         REG_AX,               REG_AX | REG_TMP1              },
196     { "shlax4",         REG_AX,               REG_AX | REG_TMP1              },
197     { "shrax1",         REG_AX,               REG_AX | REG_TMP1              },
198     { "shrax2",         REG_AX,               REG_AX | REG_TMP1              },
199     { "shrax3",         REG_AX,               REG_AX | REG_TMP1              },
200     { "shrax4",         REG_AX,               REG_AX | REG_TMP1              },
201     { "shreax1",        REG_EAX,              REG_AX | REG_TMP1              },
202     { "shreax2",        REG_EAX,              REG_AX | REG_TMP1              },
203     { "shreax3",        REG_EAX,              REG_AX | REG_TMP1              },
204     { "shreax4",        REG_EAX,              REG_AX | REG_TMP1              },
205     { "staspidx",       REG_A | REG_Y,        REG_Y | REG_TMP1 | REG_PTR1    },
206     { "stax0sp",        REG_AX,               REG_Y                          },
207     { "staxspidx",      REG_AXY,              REG_TMP1 | REG_PTR1            },
208     { "staxysp",        REG_AXY,              REG_Y                          },
209     { "steax0sp",       REG_EAX,              REG_Y                          },
210     { "steaxysp",       REG_EAXY,             REG_Y                          },
211     { "subeq0sp",       REG_AX,               REG_AXY                        },
212     { "subeqysp",       REG_AXY,              REG_AXY                        },
213     { "tosadda0",       REG_A,                REG_AXY                        },
214     { "tosaddax",       REG_AX,               REG_AXY                        },
215     { "tosanda0",       REG_A,                REG_AXY                        },
216     { "tosandax",       REG_AX,               REG_AXY                        },
217     { "tosaslax",       REG_A,                REG_AXY | REG_TMP1             },
218     { "tosasleax",      REG_A,                REG_EAXY | REG_TMP1            },
219     { "tosasrax",       REG_A,                REG_AXY | REG_TMP1             },
220     { "tosasreax",      REG_A,                REG_EAXY | REG_TMP1            },
221     { "tosdiva0",       REG_AY,               REG_ALL                        },
222     { "tosdivax",       REG_AXY,              REG_ALL                        },
223     { "tosdiveax",      REG_EAXY,             REG_ALL                        },
224     { "toseq00",        REG_NONE,             REG_AXY | REG_SREG             },
225     { "toseqa0",        REG_A,                REG_AXY | REG_SREG             },
226     { "toseqax",        REG_AX,               REG_AXY | REG_SREG             },
227     { "toseqeax",       REG_EAX,              REG_AXY | REG_PTR1             },
228     { "tosge00",        REG_NONE,             REG_AXY | REG_SREG             },
229     { "tosgea0",        REG_A,                REG_AXY | REG_SREG             },
230     { "tosgeax",        REG_AX,               REG_AXY | REG_SREG             },
231     { "tosgeeax",       REG_EAX,              REG_AXY | REG_PTR1             },
232     { "tosgt00",        REG_NONE,             REG_AXY | REG_SREG             },
233     { "tosgta0",        REG_A,                REG_AXY | REG_SREG             },
234     { "tosgtax",        REG_AX,               REG_AXY | REG_SREG             },
235     { "tosgteax",       REG_EAX,              REG_AXY | REG_PTR1             },
236     { "tosicmp",        REG_AX,               REG_AXY | REG_SREG             },
237     { "toslcmp",        REG_EAX,              REG_A | REG_Y | REG_PTR1       },
238     { "tosle00",        REG_NONE,             REG_AXY | REG_SREG             },
239     { "toslea0",        REG_A,                REG_AXY | REG_SREG             },
240     { "tosleax",        REG_AX,               REG_AXY | REG_SREG             },
241     { "tosleeax",       REG_EAX,              REG_AXY | REG_PTR1             },
242     { "toslt00",        REG_NONE,             REG_AXY | REG_SREG             },
243     { "toslta0",        REG_A,                REG_AXY | REG_SREG             },
244     { "tosltax",        REG_AX,               REG_AXY | REG_SREG             },
245     { "toslteax",       REG_EAX,              REG_AXY | REG_PTR1             },
246     { "tosmula0",       REG_AX,               REG_ALL                        },
247     { "tosmulax",       REG_AX,               REG_ALL                        },
248     { "tosmuleax",      REG_EAX,              REG_ALL                        },
249     { "tosne00",        REG_NONE,             REG_AXY | REG_SREG             },
250     { "tosnea0",        REG_A,                REG_AXY | REG_SREG             },
251     { "tosneax",        REG_AX,               REG_AXY | REG_SREG             },
252     { "tosneeax",       REG_EAX,              REG_AXY | REG_PTR1             },
253     { "tosora0",        REG_A,                REG_AXY | REG_TMP1             },
254     { "tosorax",        REG_AX,               REG_AXY | REG_TMP1             },
255     { "tosshlax",       REG_A,                REG_AXY | REG_TMP1             },
256     { "tosshleax",      REG_A,                REG_EAXY | REG_TMP1            },
257     { "tosshrax",       REG_A,                REG_AXY | REG_TMP1             },
258     { "tosshreax",      REG_A,                REG_EAXY | REG_TMP1            },
259     { "tossuba0",       REG_A,                REG_AXY                        },
260     { "tossubax",       REG_AX,               REG_AXY                        },
261     { "tossubeax",      REG_EAX,              REG_EAXY                       },
262     { "tosuge00",       REG_NONE,             REG_AXY | REG_SREG             },
263     { "tosugea0",       REG_A,                REG_AXY | REG_SREG             },
264     { "tosugeax",       REG_AX,               REG_AXY | REG_SREG             },
265     { "tosugeeax",      REG_EAX,              REG_AXY | REG_PTR1             },
266     { "tosugt00",       REG_NONE,             REG_AXY | REG_SREG             },
267     { "tosugta0",       REG_A,                REG_AXY | REG_SREG             },
268     { "tosugtax",       REG_AX,               REG_AXY | REG_SREG             },
269     { "tosugteax",      REG_EAX,              REG_AXY | REG_PTR1             },
270     { "tosule00",       REG_NONE,             REG_AXY | REG_SREG             },
271     { "tosulea0",       REG_A,                REG_AXY | REG_SREG             },
272     { "tosuleax",       REG_AX,               REG_AXY | REG_SREG             },
273     { "tosuleeax",      REG_EAX,              REG_AXY | REG_PTR1             },
274     { "tosult00",       REG_NONE,             REG_AXY | REG_SREG             },
275     { "tosulta0",       REG_A,                REG_AXY | REG_SREG             },
276     { "tosultax",       REG_AX,               REG_AXY | REG_SREG             },
277     { "tosulteax",      REG_EAX,              REG_AXY | REG_PTR1             },
278     { "tosumula0",      REG_AX,               REG_ALL                        },
279     { "tosumulax",      REG_AX,               REG_ALL                        },
280     { "tosumuleax",     REG_EAX,              REG_ALL                        },
281     { "tsteax",         REG_EAX,              REG_Y                          },
282     { "utsteax",        REG_EAX,              REG_Y                          },
283 };
284 #define FuncInfoCount   (sizeof(FuncInfoTable) / sizeof(FuncInfoTable[0]))
285
286 /* Table with names of zero page locations used by the compiler */
287 static const ZPInfo ZPInfoTable[] = {
288     {   0, "ptr1",      REG_PTR1_LO,    REG_PTR1        },
289     {   0, "ptr1+1",    REG_PTR1_HI,    REG_PTR1        },
290     {   0, "ptr2",      REG_PTR2_LO,    REG_PTR2        },
291     {   0, "ptr2+1",    REG_PTR2_HI,    REG_PTR2        },
292     {   4, "ptr3",      REG_NONE,       REG_NONE        },
293     {   4, "ptr4",      REG_NONE,       REG_NONE        },
294     {   7, "regbank",   REG_NONE,       REG_NONE        },
295     {   0, "regsave",   REG_SAVE_LO,    REG_SAVE        },
296     {   0, "regsave+1", REG_SAVE_HI,    REG_SAVE        },
297     {   0, "sp",        REG_SP_LO,      REG_SP          },
298     {   0, "sp+1",      REG_SP_HI,      REG_SP          },
299     {   0, "sreg",      REG_SREG_LO,    REG_SREG        },
300     {   0, "sreg+1",    REG_SREG_HI,    REG_SREG        },
301     {   0, "tmp1",      REG_TMP1,       REG_TMP1        },
302     {   0, "tmp2",      REG_NONE,       REG_NONE        },
303     {   0, "tmp3",      REG_NONE,       REG_NONE        },
304     {   0, "tmp4",      REG_NONE,       REG_NONE        },
305 };
306 #define ZPInfoCount     (sizeof(ZPInfoTable) / sizeof(ZPInfoTable[0]))
307
308
309
310 /*****************************************************************************/
311 /*                                   Code                                    */
312 /*****************************************************************************/
313
314
315
316 static int CompareFuncInfo (const void* Key, const void* Info)
317 /* Compare function for bsearch */
318 {
319     return strcmp (Key, ((const FuncInfo*) Info)->Name);
320 }
321
322
323
324 void GetFuncInfo (const char* Name, unsigned short* Use, unsigned short* Chg)
325 /* For the given function, lookup register information and store it into
326  * the given variables. If the function is unknown, assume it will use and
327  * load all registers.
328  */
329 {
330     /* If the function name starts with an underline, it is an external
331      * function. Search for it in the symbol table. If the function does
332      * not start with an underline, it may be a runtime support function.
333      * Search for it in the list of builtin functions.
334      */
335     if (Name[0] == '_') {
336
337         /* Search in the symbol table, skip the leading underscore */
338         SymEntry* E = FindGlobalSym (Name+1);
339
340         /* Did we find it in the top level table? */
341         if (E && IsTypeFunc (E->Type)) {
342
343             /* A function may use the A or A/X registers if it is a fastcall
344              * function. If it is not a fastcall function but a variadic one,
345              * it will use the Y register (the parameter size is passed here).
346              * In all other cases, no registers are used. However, we assume
347              * that any function will destroy all registers.
348              */
349             FuncDesc* D = E->V.F.Func;
350             if ((D->Flags & FD_FASTCALL) != 0 && D->ParamCount > 0) {
351                 /* Will use registers depending on the last param */
352                 unsigned LastParamSize = CheckedSizeOf (D->LastParam->Type);
353                 if (LastParamSize == 1) {
354                     *Use = REG_A;
355                 } else if (LastParamSize == 2) {
356                     *Use = REG_AX;
357                 } else {
358                     *Use = REG_EAX;
359                 }
360             } else if ((D->Flags & FD_VARIADIC) != 0) {
361                 *Use = REG_Y;
362             } else {
363                 /* Will not use any registers */
364                 *Use = REG_NONE;
365             }
366
367             /* Will destroy all registers */
368             *Chg = REG_ALL;
369
370             /* Done */
371             return;
372         }
373
374     } else if (IsDigit (Name[0]) || Name[0] == '$') {
375
376         /* A call to a numeric address. Assume that anything gets used and
377          * destroyed. This is not a real problem, since numeric addresses
378          * are used mostly in inline assembly anyway.
379          */
380         *Use = REG_ALL;
381         *Chg = REG_ALL;
382         return;
383
384     } else {
385
386         /* Search for the function in the list of builtin functions */
387         const FuncInfo* Info = bsearch (Name, FuncInfoTable, FuncInfoCount,
388                                         sizeof(FuncInfo), CompareFuncInfo);
389
390         /* Do we know the function? */
391         if (Info) {
392             /* Use the information we have */
393             *Use = Info->Use;
394             *Chg = Info->Chg;
395         } else {
396             /* It's an internal function we have no information for. If in
397              * debug mode, output an additional warning, so we have a chance
398              * to fix it. Otherwise assume that the internal function will
399              * use and change all registers.
400              */
401             if (Debug) {
402                 fprintf (stderr, "No info about internal function `%s'\n", Name);
403             }
404             *Use = REG_ALL;
405             *Chg = REG_ALL;
406         }
407         return;
408     }
409
410     /* Function not found - assume that the primary register is input, and all
411      * registers are changed
412      */
413     *Use = REG_EAXY;
414     *Chg = REG_ALL;
415 }
416
417
418
419 static int CompareZPInfo (const void* Name, const void* Info)
420 /* Compare function for bsearch */
421 {
422     /* Cast the pointers to the correct data type */
423     const char* N   = (const char*) Name;
424     const ZPInfo* E = (const ZPInfo*) Info;
425
426     /* Do the compare. Be careful because of the length (Info may contain
427      * more than just the zeropage name).
428      */
429     if (E->Len == 0) {
430         /* Do a full compare */
431         return strcmp (N, E->Name);
432     } else {
433         /* Only compare the first part */
434         int Res = strncmp (N, E->Name, E->Len);
435         if (Res == 0 && (N[E->Len] != '\0' && N[E->Len] != '+')) {
436             /* Name is actually longer than Info->Name */
437             Res = -1;
438         }
439         return Res;
440     }
441 }
442
443
444
445 const ZPInfo* GetZPInfo (const char* Name)
446 /* If the given name is a zero page symbol, return a pointer to the info
447  * struct for this symbol, otherwise return NULL.
448  */
449 {
450     /* Search for the zp location in the list */
451     return bsearch (Name, ZPInfoTable, ZPInfoCount,
452                     sizeof(ZPInfo), CompareZPInfo);
453 }
454
455
456
457 static unsigned GetRegInfo2 (CodeSeg* S,
458                              CodeEntry* E,
459                              int Index,
460                              Collection* Visited,
461                              unsigned Used,
462                              unsigned Unused,
463                              unsigned Wanted)
464 /* Recursively called subfunction for GetRegInfo. */
465 {
466     /* Follow the instruction flow recording register usage. */
467     while (1) {
468
469         unsigned R;
470
471         /* Check if we have already visited the current code entry. If so,
472          * bail out.
473          */
474         if (CE_HasMark (E)) {
475             break;
476         }
477
478         /* Mark this entry as already visited */
479         CE_SetMark (E);
480         CollAppend (Visited, E);
481
482         /* Evaluate the used registers */
483         R = E->Use;
484         if (E->OPC == OP65_RTS ||
485             ((E->Info & OF_UBRA) != 0 && E->JumpTo == 0)) {
486             /* This instruction will leave the function */
487             R |= S->ExitRegs;
488         }
489         if (R != REG_NONE) {
490             /* We are not interested in the use of any register that has been
491              * used before.
492              */
493             R &= ~Unused;
494             /* Remember the remaining registers */
495             Used |= R;
496         }
497
498         /* Evaluate the changed registers */
499         if ((R = E->Chg) != REG_NONE) {
500             /* We are not interested in the use of any register that has been
501              * used before.
502              */
503             R &= ~Used;
504             /* Remember the remaining registers */
505             Unused |= R;
506         }
507
508         /* If we know about all registers now, bail out */
509         if (((Used | Unused) & Wanted) == Wanted) {
510             break;
511         }
512
513         /* If the instruction is an RTS or RTI, we're done */
514         if ((E->Info & OF_RET) != 0) {
515             break;
516         }
517
518         /* If we have an unconditional branch, follow this branch if possible,
519          * otherwise we're done.
520          */
521         if ((E->Info & OF_UBRA) != 0) {
522
523             /* Does this jump have a valid target? */
524             if (E->JumpTo) {
525
526                 /* Unconditional jump */
527                 E     = E->JumpTo->Owner;
528                 Index = -1;             /* Invalidate */
529
530             } else {
531                 /* Jump outside means we're done */
532                 break;
533             }
534
535         /* In case of conditional branches, follow the branch if possible and
536          * follow the normal flow (branch not taken) afterwards. If we cannot
537          * follow the branch, we're done.
538          */
539         } else if ((E->Info & OF_CBRA) != 0) {
540
541             /* Recursively determine register usage at the branch target */
542             unsigned U1;
543             unsigned U2;
544
545             if (E->JumpTo) {
546
547                 /* Jump to internal label */
548                 U1 = GetRegInfo2 (S, E->JumpTo->Owner, -1, Visited, Used, Unused, Wanted);
549
550             } else {
551
552                 /* Jump to external label. This will effectively exit the
553                  * function, so we use the exitregs information here.
554                  */
555                 U1 = S->ExitRegs;
556
557             }
558
559             /* Get the next entry */
560             if (Index < 0) {
561                 Index = CS_GetEntryIndex (S, E);
562             }
563             if ((E = CS_GetEntry (S, ++Index)) == 0) {
564                 Internal ("GetRegInfo2: No next entry!");
565             }
566
567             /* Follow flow if branch not taken */
568             U2 = GetRegInfo2 (S, E, Index, Visited, Used, Unused, Wanted);
569
570             /* Registers are used if they're use in any of the branches */
571             return U1 | U2;
572
573         } else {
574
575             /* Just go to the next instruction */
576             if (Index < 0) {
577                 Index = CS_GetEntryIndex (S, E);
578             }
579             E = CS_GetEntry (S, ++Index);
580             if (E == 0) {
581                 /* No next entry */
582                 Internal ("GetRegInfo2: No next entry!");
583             }
584
585         }
586
587     }
588
589     /* Return to the caller the complement of all unused registers */
590     return Used;
591 }
592
593
594
595 static unsigned GetRegInfo1 (CodeSeg* S,
596                              CodeEntry* E,
597                              int Index,
598                              Collection* Visited,
599                              unsigned Used,
600                              unsigned Unused,
601                              unsigned Wanted)
602 /* Recursively called subfunction for GetRegInfo. */
603 {
604     /* Remember the current count of the line collection */
605     unsigned Count = CollCount (Visited);
606
607     /* Call the worker routine */
608     unsigned R = GetRegInfo2 (S, E, Index, Visited, Used, Unused, Wanted);
609
610     /* Restore the old count, unmarking all new entries */
611     unsigned NewCount = CollCount (Visited);
612     while (NewCount-- > Count) {
613         CodeEntry* E = CollAt (Visited, NewCount);
614         CE_ResetMark (E);
615         CollDelete (Visited, NewCount);
616     }
617
618     /* Return the registers used */
619     return R;
620 }
621
622
623
624 unsigned GetRegInfo (struct CodeSeg* S, unsigned Index, unsigned Wanted)
625 /* Determine register usage information for the instructions starting at the
626  * given index.
627  */
628 {
629     CodeEntry*      E;
630     Collection      Visited;    /* Visited entries */
631     unsigned        R;
632
633     /* Get the code entry for the given index */
634     if (Index >= CS_GetEntryCount (S)) {
635         /* There is no such code entry */
636         return REG_NONE;
637     }
638     E = CS_GetEntry (S, Index);
639
640     /* Initialize the data structure used to collection information */
641     InitCollection (&Visited);
642
643     /* Call the recursive subfunction */
644     R = GetRegInfo1 (S, E, Index, &Visited, REG_NONE, REG_NONE, Wanted);
645
646     /* Delete the line collection */
647     DoneCollection (&Visited);
648
649     /* Return the registers used */
650     return R;
651 }
652
653
654
655 int RegAUsed (struct CodeSeg* S, unsigned Index)
656 /* Check if the value in A is used. */
657 {
658     return (GetRegInfo (S, Index, REG_A) & REG_A) != 0;
659 }
660
661
662
663 int RegXUsed (struct CodeSeg* S, unsigned Index)
664 /* Check if the value in X is used. */
665 {
666     return (GetRegInfo (S, Index, REG_X) & REG_X) != 0;
667 }
668
669
670
671 int RegYUsed (struct CodeSeg* S, unsigned Index)
672 /* Check if the value in Y is used. */
673 {
674     return (GetRegInfo (S, Index, REG_Y) & REG_Y) != 0;
675 }
676
677
678
679 int RegAXUsed (struct CodeSeg* S, unsigned Index)
680 /* Check if the value in A or(!) the value in X are used. */
681 {
682     return (GetRegInfo (S, Index, REG_AX) & REG_AX) != 0;
683 }
684
685
686
687 int RegEAXUsed (struct CodeSeg* S, unsigned Index)
688 /* Check if any of the four bytes in EAX are used. */
689 {
690     return (GetRegInfo (S, Index, REG_EAX) & REG_EAX) != 0;
691 }
692
693
694
695 unsigned GetKnownReg (unsigned Use, const RegContents* RC)
696 /* Return the register or zero page location from the set in Use, thats
697  * contents are known. If Use does not contain any register, or if the
698  * register in question does not have a known value, return REG_NONE.
699  */
700 {
701     if ((Use & REG_A) != 0) {
702         return (RC == 0 || RC->RegA >= 0)? REG_A : REG_NONE;
703     } else if ((Use & REG_X) != 0) {
704         return (RC == 0 || RC->RegX >= 0)? REG_X : REG_NONE;
705     } else if ((Use & REG_Y) != 0) {
706         return (RC == 0 || RC->RegY >= 0)? REG_Y : REG_NONE;
707     } else if ((Use & REG_TMP1) != 0) {
708         return (RC == 0 || RC->Tmp1 >= 0)? REG_TMP1 : REG_NONE;
709     } else if ((Use & REG_PTR1_LO) != 0) {
710         return (RC == 0 || RC->Ptr1Lo >= 0)? REG_PTR1_LO : REG_NONE;
711     } else if ((Use & REG_PTR1_HI) != 0) {
712         return (RC == 0 || RC->Ptr1Hi >= 0)? REG_PTR1_HI : REG_NONE;
713     } else if ((Use & REG_SREG_LO) != 0) {
714         return (RC == 0 || RC->SRegLo >= 0)? REG_SREG_LO : REG_NONE;
715     } else if ((Use & REG_SREG_HI) != 0) {
716         return (RC == 0 || RC->SRegHi >= 0)? REG_SREG_HI : REG_NONE;
717     } else {
718         return REG_NONE;
719     }
720 }
721
722
723
724 static cmp_t FindCmpCond (const char* Code, unsigned CodeLen)
725 /* Search for a compare condition by the given code using the given length */
726 {
727     unsigned I;
728
729     /* Linear search */
730     for (I = 0; I < sizeof (CmpSuffixTab) / sizeof (CmpSuffixTab [0]); ++I) {
731         if (strncmp (Code, CmpSuffixTab [I], CodeLen) == 0) {
732             /* Found */
733             return I;
734         }
735     }
736
737     /* Not found */
738     return CMP_INV;
739 }
740
741
742
743 cmp_t FindBoolCmpCond (const char* Name)
744 /* Check if the given string is the name of one of the boolean transformer
745  * subroutine, and if so, return the condition that is evaluated by this
746  * routine. Return CMP_INV if the condition is not recognised.
747  */
748 {
749     /* Check for the correct subroutine name */
750     if (strncmp (Name, "bool", 4) == 0) {
751         /* Name is ok, search for the code in the table */
752         return FindCmpCond (Name+4, strlen(Name)-4);
753     } else {
754         /* Not found */
755         return CMP_INV;
756     }
757 }
758
759
760
761 cmp_t FindTosCmpCond (const char* Name)
762 /* Check if this is a call to one of the TOS compare functions (tosgtax).
763  * Return the condition code or CMP_INV on failure.
764  */
765 {
766     unsigned Len = strlen (Name);
767
768     /* Check for the correct subroutine name */
769     if (strncmp (Name, "tos", 3) == 0 && strcmp (Name+Len-2, "ax") == 0) {
770         /* Name is ok, search for the code in the table */
771         return FindCmpCond (Name+3, Len-3-2);
772     } else {
773         /* Not found */
774         return CMP_INV;
775     }
776 }
777
778
779