]> git.sur5r.net Git - cc65/blob - src/cc65/codeinfo.c
Adding functionality to StrBuf
[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     { "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 char* Use, unsigned char* 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 char GetRegInfo2 (CodeSeg* S,
280                                   CodeEntry* E,
281                                   int Index,
282                                   Collection* Visited,
283                                   unsigned char Used,
284                                   unsigned char Unused)
285 /* Recursively called subfunction for GetRegInfo. */
286 {
287     /* Follow the instruction flow recording register usage. */
288     while (1) {
289
290         unsigned char R;
291
292         /* Check if we have already visited the current code entry. If so,
293          * bail out.
294          */
295         if (CE_HasMark (E)) {
296             break;
297         }
298
299         /* Mark this entry as already visited */
300         CE_SetMark (E);
301         CollAppend (Visited, E);
302
303         /* Evaluate the used registers */
304         R = E->Use;
305         if (E->OPC == OP65_RTS ||
306             ((E->Info & OF_BRA) != 0 && E->JumpTo == 0)) {
307             /* This instruction will leave the function */
308             R |= S->ExitRegs;
309         }
310         if (R != REG_NONE) {
311             /* We are not interested in the use of any register that has been
312              * used before.
313              */
314             R &= ~Unused;
315             /* Remember the remaining registers */
316             Used |= R;
317         }
318
319         /* Evaluate the changed registers */
320         if ((R = E->Chg) != REG_NONE) {
321             /* We are not interested in the use of any register that has been
322              * used before.
323              */
324             R &= ~Used;
325             /* Remember the remaining registers */
326             Unused |= R;
327         }
328
329         /* If we know about all registers now, bail out */
330         if ((Used | Unused) == REG_AXY) {
331             break;
332         }
333
334         /* If the instruction is an RTS or RTI, we're done */
335         if ((E->Info & OF_RET) != 0) {
336             break;
337         }
338
339         /* If we have an unconditional branch, follow this branch if possible,
340          * otherwise we're done.
341          */
342         if ((E->Info & OF_UBRA) != 0) {
343
344             /* Does this jump have a valid target? */
345             if (E->JumpTo) {
346
347                 /* Unconditional jump */
348                 E     = E->JumpTo->Owner;
349                 Index = -1;             /* Invalidate */
350
351             } else {
352                 /* Jump outside means we're done */
353                 break;
354             }
355
356         /* In case of conditional branches, follow the branch if possible and
357          * follow the normal flow (branch not taken) afterwards. If we cannot
358          * follow the branch, we're done.
359          */
360         } else if ((E->Info & OF_CBRA) != 0) {
361
362             if (E->JumpTo) {
363
364                 /* Recursively determine register usage at the branch target */
365                 unsigned char U1;
366                 unsigned char U2;
367
368                 U1 = GetRegInfo2 (S, E->JumpTo->Owner, -1, Visited, Used, Unused);
369                 if (U1 == REG_AXY) {
370                     /* All registers used, no need for second call */
371                     return REG_AXY;
372                 }
373                 if (Index < 0) {
374                     Index = CS_GetEntryIndex (S, E);
375                 }
376                 if ((E = CS_GetEntry (S, ++Index)) == 0) {
377                     Internal ("GetRegInfo2: No next entry!");
378                 }
379                 U2 = GetRegInfo2 (S, E, Index, Visited, Used, Unused);
380                 return U1 | U2;         /* Used in any of the branches */
381
382             } else {
383                 /* Jump to global symbol */
384                 break;
385             }
386
387         } else {
388
389             /* Just go to the next instruction */
390             if (Index < 0) {
391                 Index = CS_GetEntryIndex (S, E);
392             }
393             E = CS_GetEntry (S, ++Index);
394             if (E == 0) {
395                 /* No next entry */
396                 Internal ("GetRegInfo2: No next entry!");
397             }
398
399         }
400
401     }
402
403     /* Return to the caller the complement of all unused registers */
404     return Used;
405 }
406
407
408
409 static unsigned char GetRegInfo1 (CodeSeg* S,
410                                   CodeEntry* E,
411                                   int Index,
412                                   Collection* Visited,
413                                   unsigned char Used,
414                                   unsigned char Unused)
415 /* Recursively called subfunction for GetRegInfo. */
416 {
417     /* Remember the current count of the line collection */
418     unsigned Count = CollCount (Visited);
419
420     /* Call the worker routine */
421     unsigned char R = GetRegInfo2 (S, E, Index, Visited, Used, Unused);
422
423     /* Restore the old count, unmarking all new entries */
424     unsigned NewCount = CollCount (Visited);
425     while (NewCount-- > Count) {
426         CodeEntry* E = CollAt (Visited, NewCount);
427         CE_ResetMark (E);
428         CollDelete (Visited, NewCount);
429     }
430
431     /* Return the registers used */
432     return R;
433 }
434
435
436
437 unsigned char GetRegInfo (struct CodeSeg* S, unsigned Index)
438 /* Determine register usage information for the instructions starting at the
439  * given index.
440  */
441 {
442     CodeEntry*      E;
443     Collection      Visited;    /* Visited entries */
444     unsigned char   R;
445
446     /* Get the code entry for the given index */
447     if (Index >= CS_GetEntryCount (S)) {
448         /* There is no such code entry */
449         return REG_NONE;
450     }
451     E = CS_GetEntry (S, Index);
452
453     /* Initialize the data structure used to collection information */
454     InitCollection (&Visited);
455
456     /* Call the recursive subfunction */
457     R = GetRegInfo1 (S, E, Index, &Visited, REG_NONE, REG_NONE);
458
459     /* Delete the line collection */
460     DoneCollection (&Visited);
461
462     /* Return the registers used */
463     return R;
464 }
465
466
467
468 int RegAUsed (struct CodeSeg* S, unsigned Index)
469 /* Check if the value in A is used. */
470 {
471     return (GetRegInfo (S, Index) & REG_A) != 0;
472 }
473
474
475
476 int RegXUsed (struct CodeSeg* S, unsigned Index)
477 /* Check if the value in X is used. */
478 {
479     return (GetRegInfo (S, Index) & REG_X) != 0;
480 }
481
482
483
484 int RegYUsed (struct CodeSeg* S, unsigned Index)
485 /* Check if the value in Y is used. */
486 {
487     return (GetRegInfo (S, Index) & REG_Y) != 0;
488 }
489
490
491
492