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