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