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