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