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