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