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