]> git.sur5r.net Git - cc65/blob - src/cc65/codeinfo.c
4fcfd0f91de80de17bf6b66226838c734134549f
[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                 unsigned LastParamSize = CheckedSizeOf (LastParam->Type);
255                 if (LastParamSize == 1) {
256                     *Use = REG_A;
257                 } else if (LastParamSize == 2) {
258                     *Use = REG_AX;
259                 } else {
260                     *Use = REG_EAX;
261                 }
262             } else if ((D->Flags & FD_VARIADIC) != 0) {
263                 *Use = REG_Y;
264             } else {
265                 /* Will not use any registers */
266                 *Use = REG_NONE;
267             }
268
269             /* Will destroy all registers */
270             *Chg = REG_ALL;
271
272             /* Done */
273             return;
274         }
275
276     } else {
277
278         /* Search for the function in the list of builtin functions */
279         const FuncInfo* Info = bsearch (Name, FuncInfoTable, FuncInfoCount,
280                                         sizeof(FuncInfo), CompareFuncInfo);
281
282         /* Do we know the function? */
283         if (Info) {
284             /* Use the information we have */
285             *Use = Info->Use;
286             *Chg = Info->Chg;
287             return;
288         }
289     }
290
291     /* Function not found - assume that the primary register is input, and all
292      * registers are changed
293      */
294     *Use = REG_EAXY;
295     *Chg = REG_ALL;
296 }
297
298
299
300 static int CompareZPInfo (const void* Name, const void* Info)
301 /* Compare function for bsearch */
302 {
303     /* Cast the pointers to the correct data type */
304     const char* N   = (const char*) Name;
305     const ZPInfo* E = (const ZPInfo*) Info;
306
307     /* Do the compare. Be careful because of the length (Info may contain
308      * more than just the zeropage name).
309      */
310     if (E->Len == 0) {
311         /* Do a full compare */
312         return strcmp (N, E->Name);
313     } else {
314         /* Only compare the first part */
315         int Res = strncmp (N, E->Name, E->Len);
316         if (Res == 0 && (N[E->Len] != '\0' && N[E->Len] != '+')) {
317             /* Name is actually longer than Info->Name */
318             Res = -1;
319         }
320         return Res;
321     }
322 }
323
324
325
326 const ZPInfo* GetZPInfo (const char* Name)
327 /* If the given name is a zero page symbol, return a pointer to the info
328  * struct for this symbol, otherwise return NULL.
329  */
330 {
331     /* Search for the zp location in the list */
332     return bsearch (Name, ZPInfoTable, ZPInfoCount,
333                     sizeof(ZPInfo), CompareZPInfo);
334 }
335
336
337
338 static unsigned GetRegInfo2 (CodeSeg* S,
339                              CodeEntry* E,
340                              int Index,
341                              Collection* Visited,
342                              unsigned Used,
343                              unsigned Unused,
344                              unsigned Wanted)
345 /* Recursively called subfunction for GetRegInfo. */
346 {
347     /* Follow the instruction flow recording register usage. */
348     while (1) {
349
350         unsigned R;
351
352         /* Check if we have already visited the current code entry. If so,
353          * bail out.
354          */
355         if (CE_HasMark (E)) {
356             break;
357         }
358
359         /* Mark this entry as already visited */
360         CE_SetMark (E);
361         CollAppend (Visited, E);
362
363         /* Evaluate the used registers */
364         R = E->Use;
365         if (E->OPC == OP65_RTS ||
366             ((E->Info & OF_BRA) != 0 && E->JumpTo == 0)) {
367             /* This instruction will leave the function */
368             R |= S->ExitRegs;
369         }
370         if (R != REG_NONE) {
371             /* We are not interested in the use of any register that has been
372              * used before.
373              */
374             R &= ~Unused;
375             /* Remember the remaining registers */
376             Used |= R;
377         }
378
379         /* Evaluate the changed registers */
380         if ((R = E->Chg) != REG_NONE) {
381             /* We are not interested in the use of any register that has been
382              * used before.
383              */
384             R &= ~Used;
385             /* Remember the remaining registers */
386             Unused |= R;
387         }
388
389         /* If we know about all registers now, bail out */
390         if (((Used | Unused) & Wanted) == Wanted) {
391             break;
392         }
393
394         /* If the instruction is an RTS or RTI, we're done */
395         if ((E->Info & OF_RET) != 0) {
396             break;
397         }
398
399         /* If we have an unconditional branch, follow this branch if possible,
400          * otherwise we're done.
401          */
402         if ((E->Info & OF_UBRA) != 0) {
403
404             /* Does this jump have a valid target? */
405             if (E->JumpTo) {
406
407                 /* Unconditional jump */
408                 E     = E->JumpTo->Owner;
409                 Index = -1;             /* Invalidate */
410
411             } else {
412                 /* Jump outside means we're done */
413                 break;
414             }
415
416         /* In case of conditional branches, follow the branch if possible and
417          * follow the normal flow (branch not taken) afterwards. If we cannot
418          * follow the branch, we're done.
419          */
420         } else if ((E->Info & OF_CBRA) != 0) {
421
422             if (E->JumpTo) {
423
424                 /* Recursively determine register usage at the branch target */
425                 unsigned U1;
426                 unsigned U2;
427
428                 U1 = GetRegInfo2 (S, E->JumpTo->Owner, -1, Visited, Used, Unused, Wanted);
429                 if (U1 == REG_ALL) {
430                     /* All registers used, no need for second call */
431                     return REG_AXY;
432                 }
433                 if (Index < 0) {
434                     Index = CS_GetEntryIndex (S, E);
435                 }
436                 if ((E = CS_GetEntry (S, ++Index)) == 0) {
437                     Internal ("GetRegInfo2: No next entry!");
438                 }
439                 U2 = GetRegInfo2 (S, E, Index, Visited, Used, Unused, Wanted);
440                 return U1 | U2;         /* Used in any of the branches */
441
442             } else {
443                 /* Jump to global symbol */
444                 break;
445             }
446
447         } else {
448
449             /* Just go to the next instruction */
450             if (Index < 0) {
451                 Index = CS_GetEntryIndex (S, E);
452             }
453             E = CS_GetEntry (S, ++Index);
454             if (E == 0) {
455                 /* No next entry */
456                 Internal ("GetRegInfo2: No next entry!");
457             }
458
459         }
460
461     }
462
463     /* Return to the caller the complement of all unused registers */
464     return Used;
465 }
466
467
468
469 static unsigned GetRegInfo1 (CodeSeg* S,
470                              CodeEntry* E,
471                              int Index,
472                              Collection* Visited,
473                              unsigned Used,
474                              unsigned Unused,
475                              unsigned Wanted)
476 /* Recursively called subfunction for GetRegInfo. */
477 {
478     /* Remember the current count of the line collection */
479     unsigned Count = CollCount (Visited);
480
481     /* Call the worker routine */
482     unsigned R = GetRegInfo2 (S, E, Index, Visited, Used, Unused, Wanted);
483
484     /* Restore the old count, unmarking all new entries */
485     unsigned NewCount = CollCount (Visited);
486     while (NewCount-- > Count) {
487         CodeEntry* E = CollAt (Visited, NewCount);
488         CE_ResetMark (E);
489         CollDelete (Visited, NewCount);
490     }
491
492     /* Return the registers used */
493     return R;
494 }
495
496
497
498 unsigned GetRegInfo (struct CodeSeg* S, unsigned Index, unsigned Wanted)
499 /* Determine register usage information for the instructions starting at the
500  * given index.
501  */
502 {
503     CodeEntry*      E;
504     Collection      Visited;    /* Visited entries */
505     unsigned        R;
506
507     /* Get the code entry for the given index */
508     if (Index >= CS_GetEntryCount (S)) {
509         /* There is no such code entry */
510         return REG_NONE;
511     }
512     E = CS_GetEntry (S, Index);
513
514     /* Initialize the data structure used to collection information */
515     InitCollection (&Visited);
516
517     /* Call the recursive subfunction */
518     R = GetRegInfo1 (S, E, Index, &Visited, REG_NONE, REG_NONE, Wanted);
519
520     /* Delete the line collection */
521     DoneCollection (&Visited);
522
523     /* Return the registers used */
524     return R;
525 }
526
527
528
529 int RegAUsed (struct CodeSeg* S, unsigned Index)
530 /* Check if the value in A is used. */
531 {
532     return (GetRegInfo (S, Index, REG_A) & REG_A) != 0;
533 }
534
535
536
537 int RegXUsed (struct CodeSeg* S, unsigned Index)
538 /* Check if the value in X is used. */
539 {
540     return (GetRegInfo (S, Index, REG_X) & REG_X) != 0;
541 }
542
543
544
545 int RegYUsed (struct CodeSeg* S, unsigned Index)
546 /* Check if the value in Y is used. */
547 {
548     return (GetRegInfo (S, Index, REG_Y) & REG_Y) != 0;
549 }
550
551
552
553 int RegAXUsed (struct CodeSeg* S, unsigned Index)
554 /* Check if the value in A or(!) the value in X are used. */
555 {
556     return (GetRegInfo (S, Index, REG_AX) & REG_AX) != 0;
557 }
558
559
560