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