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