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