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