]> git.sur5r.net Git - cc65/blob - src/cc65/codeinfo.c
a1fa65891a9eaf3bfc67550c6df8736f6c638dea
[cc65] / src / cc65 / codeinfo.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                codeinfo.c                                 */
4 /*                                                                           */
5 /*                  Additional information about 6502 code                   */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2001-2002 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 "reginfo.h"
48 #include "symtab.h"
49 #include "codeinfo.h"
50
51
52
53 /*****************************************************************************/
54 /*                                   Data                                    */
55 /*****************************************************************************/
56
57
58
59 /* Table listing the function names and code info values for known internally
60  * used functions. This table should get auto-generated in the future.
61  */
62 typedef struct FuncInfo FuncInfo;
63 struct FuncInfo {
64     const char*     Name;       /* Function name */
65     unsigned short  Use;        /* Register usage */
66     unsigned short  Chg;        /* Changed/destroyed registers */
67 };
68
69 static const FuncInfo FuncInfoTable[] = {
70     { "addysp",         REG_Y,                REG_NONE                       },
71     { "aslax1",         REG_AX,               REG_AX | REG_TMP1              },
72     { "aslax2",         REG_AX,               REG_AX | REG_TMP1              },
73     { "aslax3",         REG_AX,               REG_AX | REG_TMP1              },
74     { "aslax4",         REG_AX,               REG_AX | REG_TMP1              },
75     { "bnega",          REG_A,                REG_AX                         },
76     { "bnegax",         REG_AX,               REG_AX                         },
77     { "bnegeax",        REG_EAX,              REG_EAX                        },
78     { "booleq",         REG_NONE,             REG_AX                         },
79     { "boolge",         REG_NONE,             REG_AX                         },
80     { "boolgt",         REG_NONE,             REG_AX                         },
81     { "boolle",         REG_NONE,             REG_AX                         },
82     { "boollt",         REG_NONE,             REG_AX                         },
83     { "boolne",         REG_NONE,             REG_AX                         },
84     { "booluge",        REG_NONE,             REG_AX                         },
85     { "boolugt",        REG_NONE,             REG_AX                         },
86     { "boolule",        REG_NONE,             REG_AX                         },
87     { "boolult",        REG_NONE,             REG_AX                         },
88     { "complax",        REG_AX,               REG_AX                         },
89     { "decax1",         REG_AX,               REG_AX                         },
90     { "decax2",         REG_AX,               REG_AX                         },
91     { "decax3",         REG_AX,               REG_AX                         },
92     { "decax4",         REG_AX,               REG_AX                         },
93     { "decax5",         REG_AX,               REG_AX                         },
94     { "decax6",         REG_AX,               REG_AX                         },
95     { "decax7",         REG_AX,               REG_AX                         },
96     { "decax8",         REG_AX,               REG_AX                         },
97     { "decaxy",         REG_AXY,              REG_AX | REG_TMP1              },
98     { "deceaxy",        REG_EAXY,             REG_EAX                        },
99     { "decsp1",         REG_NONE,             REG_Y                          },
100     { "decsp2",         REG_NONE,             REG_A                          },
101     { "decsp3",         REG_NONE,             REG_A                          },
102     { "decsp4",         REG_NONE,             REG_A                          },
103     { "decsp5",         REG_NONE,             REG_A                          },
104     { "decsp6",         REG_NONE,             REG_A                          },
105     { "decsp7",         REG_NONE,             REG_A                          },
106     { "decsp8",         REG_NONE,             REG_A                          },
107     { "incax1",         REG_AX,               REG_AX                         },
108     { "incax2",         REG_AX,               REG_AX                         },
109     { "incsp1",         REG_NONE,             REG_NONE                       },
110     { "incsp2",         REG_NONE,             REG_Y                          },
111     { "incsp3",         REG_NONE,             REG_Y                          },
112     { "incsp4",         REG_NONE,             REG_Y                          },
113     { "incsp5",         REG_NONE,             REG_Y                          },
114     { "incsp6",         REG_NONE,             REG_Y                          },
115     { "incsp7",         REG_NONE,             REG_Y                          },
116     { "incsp8",         REG_NONE,             REG_Y                          },
117     { "laddeq",         REG_EAXY|REG_PTR1_LO, REG_EAXY | REG_PTR1_HI         },
118     { "laddeq1",        REG_Y | REG_PTR1_LO,  REG_EAXY | REG_PTR1_HI         },
119     { "laddeqa",        REG_AY | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI         },
120     { "ldaidx",         REG_AXY,              REG_AX | REG_PTR1              },
121     { "ldauidx",        REG_AXY,              REG_AX | REG_PTR1              },
122     { "ldax0sp",        REG_Y,                REG_AX                         },
123     { "ldaxi",          REG_AX,               REG_AXY | REG_PTR1             },
124     { "ldaxidx",        REG_AXY,              REG_AXY | REG_PTR1             },
125     { "ldaxysp",        REG_Y,                REG_AXY                        },
126     { "leaasp",         REG_A,                REG_AX                         },
127     { "lsubeq",         REG_EAXY|REG_PTR1_LO, REG_EAXY | REG_PTR1_HI         },
128     { "lsubeq0sp",      REG_EAX,              REG_EAXY                       },
129     { "lsubeq1",        REG_Y | REG_PTR1_LO,  REG_EAXY | REG_PTR1_HI         },
130     { "lsubeqa",        REG_AY | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI         },
131     { "lsubeqysp",      REG_EAXY,             REG_EAXY                       },
132     { "negax",          REG_AX,               REG_AX                         },
133     { "pusha",          REG_A,                REG_Y                          },
134     { "pusha0",         REG_A,                REG_XY                         },
135     { "pushax",         REG_AX,               REG_Y                          },
136     { "pusheax",        REG_EAX,              REG_Y                          },
137     { "pushw0sp",       REG_NONE,             REG_AXY                        },
138     { "pushwysp",       REG_Y,                REG_AXY                        },
139     { "shlax1",         REG_AX,               REG_AX | REG_TMP1              },
140     { "shlax2",         REG_AX,               REG_AX | REG_TMP1              },
141     { "shlax3",         REG_AX,               REG_AX | REG_TMP1              },
142     { "shlax4",         REG_AX,               REG_AX | REG_TMP1              },
143     { "shrax1",         REG_AX,               REG_AX | REG_TMP1              },
144     { "shrax2",         REG_AX,               REG_AX | REG_TMP1              },
145     { "shrax3",         REG_AX,               REG_AX | REG_TMP1              },
146     { "shrax4",         REG_AX,               REG_AX | REG_TMP1              },
147     { "shreax1",        REG_EAX,              REG_AX | REG_TMP1              },
148     { "shreax2",        REG_EAX,              REG_AX | REG_TMP1              },
149     { "shreax3",        REG_EAX,              REG_AX | REG_TMP1              },
150     { "shreax4",        REG_EAX,              REG_AX | REG_TMP1              },
151     { "staspidx",       REG_A | REG_Y,        REG_Y | REG_TMP1 | REG_PTR1    },
152     { "stax0sp",        REG_AX,               REG_Y                          },
153     { "staxysp",        REG_AXY,              REG_Y                          },
154     { "subeq0sp",       REG_AX,               REG_AXY                        },
155     { "subeqysp",       REG_AXY,              REG_AXY                        },
156     { "tsteax",         REG_EAX,              REG_Y                          },
157     { "tosadda0",       REG_A,                REG_AXY                        },
158     { "tosaddax",       REG_AX,               REG_AXY                        },
159     { "tosicmp",        REG_AX,               REG_AXY | REG_SREG             },
160     { "tosdiva0",       REG_AX,               REG_ALL                        },
161     { "tosdivax",       REG_AX,               REG_ALL                        },
162     { "tosdiveax",      REG_EAX,              REG_ALL                        },
163     { "toseqeax",       REG_EAX,              REG_AXY | REG_PTR1             },
164     { "tosgeeax",       REG_EAX,              REG_AXY | REG_PTR1             },
165     { "tosgteax",       REG_EAX,              REG_AXY | REG_PTR1             },
166     { "toslcmp",        REG_EAX,              REG_A | REG_Y | REG_PTR1       },
167     { "tosleeax",       REG_EAX,              REG_AXY | REG_PTR1             },
168     { "toslteax",       REG_EAX,              REG_AXY | REG_PTR1             },
169     { "tosmula0",       REG_AX,               REG_ALL                        },
170     { "tosmulax",       REG_AX,               REG_ALL                        },
171     { "tosmuleax",      REG_EAX,              REG_ALL                        },
172     { "tosneeax",       REG_EAX,              REG_AXY | REG_PTR1             },
173     { "tosshreax",      REG_EAX,              REG_EAXY | REG_PTR1 | REG_PTR2 },
174     { "tossuba0",       REG_A,                REG_AXY                        },
175     { "tossubax",       REG_AX,               REG_AXY                        },
176     { "tossubeax",      REG_EAX,              REG_EAXY                       },
177     { "tosugeeax",      REG_EAX,              REG_AXY | REG_PTR1             },
178     { "tosugteax",      REG_EAX,              REG_AXY | REG_PTR1             },
179     { "tosuleeax",      REG_EAX,              REG_AXY | REG_PTR1             },
180     { "tosulteax",      REG_EAX,              REG_AXY | REG_PTR1             },
181     { "tosumula0",      REG_AX,               REG_ALL                        },
182     { "tosumulax",      REG_AX,               REG_ALL                        },
183     { "tosumuleax",     REG_EAX,              REG_ALL                        },
184     { "utsteax",        REG_EAX,              REG_Y                          },
185 };
186 #define FuncInfoCount   (sizeof(FuncInfoTable) / sizeof(FuncInfoTable[0]))
187
188 /* Table with names of zero page locations used by the compiler */
189 static const ZPInfo ZPInfoTable[] = {
190     {   0, "ptr1",      REG_PTR1_LO,    REG_PTR1        },
191     {   0, "ptr1+1",    REG_PTR1_HI,    REG_PTR1        },
192     {   0, "ptr2",      REG_PTR2_LO,    REG_PTR2        },
193     {   0, "ptr2+1",    REG_PTR2_HI,    REG_PTR2        },
194     {   4, "ptr3",      REG_NONE,       REG_NONE        },
195     {   4, "ptr4",      REG_NONE,       REG_NONE        },
196     {   7, "regbank",   REG_NONE,       REG_NONE        },
197     {   0, "regsave",   REG_SAVE_LO,    REG_SAVE        },
198     {   0, "regsave+1", REG_SAVE_HI,    REG_SAVE        },
199     {   0, "sp",        REG_SP_LO,      REG_SP          },
200     {   0, "sp+1",      REG_SP_HI,      REG_SP          },
201     {   0, "sreg",      REG_SREG_LO,    REG_SREG        },
202     {   0, "sreg+1",    REG_SREG_HI,    REG_SREG        },
203     {   0, "tmp1",      REG_TMP1,       REG_TMP1        },
204     {   0, "tmp2",      REG_NONE,       REG_NONE        },
205     {   0, "tmp3",      REG_NONE,       REG_NONE        },
206     {   0, "tmp4",      REG_NONE,       REG_NONE        },
207 };
208 #define ZPInfoCount     (sizeof(ZPInfoTable) / sizeof(ZPInfoTable[0]))
209
210
211
212 /*****************************************************************************/
213 /*                                   Code                                    */
214 /*****************************************************************************/
215
216
217
218 static int CompareFuncInfo (const void* Key, const void* Info)
219 /* Compare function for bsearch */
220 {
221     return strcmp (Key, ((const FuncInfo*) Info)->Name);
222 }
223
224
225
226 void GetFuncInfo (const char* Name, unsigned short* Use, unsigned short* Chg)
227 /* For the given function, lookup register information and store it into
228  * the given variables. If the function is unknown, assume it will use and
229  * load all registers.
230  */
231 {
232     /* If the function name starts with an underline, it is an external
233      * function. Search for it in the symbol table. If the function does
234      * not start with an underline, it may be a runtime support function.
235      * Search for it in the list of builtin functions.
236      */
237     if (Name[0] == '_') {
238
239         /* Search in the symbol table, skip the leading underscore */
240         SymEntry* E = FindGlobalSym (Name+1);
241
242         /* Did we find it in the top level table? */
243         if (E && IsTypeFunc (E->Type)) {
244
245             /* A function may use the A or A/X registers if it is a fastcall
246              * function. If it is not a fastcall function but a variadic one,
247              * it will use the Y register (the parameter size is passed here).
248              * In all other cases, no registers are used. However, we assume
249              * that any function will destroy all registers.
250              */
251             FuncDesc* D = E->V.F.Func;
252             if ((D->Flags & FD_FASTCALL) != 0 && D->ParamCount > 0) {
253                 /* Will use registers depending on the last param */
254                 SymEntry* LastParam = D->SymTab->SymTail;
255                 unsigned LastParamSize = CheckedSizeOf (LastParam->Type);
256                 if (LastParamSize == 1) {
257                     *Use = REG_A;
258                 } else if (LastParamSize == 2) {
259                     *Use = REG_AX;
260                 } else {
261                     *Use = REG_EAX;
262                 }
263             } else if ((D->Flags & FD_VARIADIC) != 0) {
264                 *Use = REG_Y;
265             } else {
266                 /* Will not use any registers */
267                 *Use = REG_NONE;
268             }
269
270             /* Will destroy all registers */
271             *Chg = REG_ALL;
272
273             /* Done */
274             return;
275         }
276
277     } else {
278
279         /* Search for the function in the list of builtin functions */
280         const FuncInfo* Info = bsearch (Name, FuncInfoTable, FuncInfoCount,
281                                         sizeof(FuncInfo), CompareFuncInfo);
282
283         /* Do we know the function? */
284         if (Info) {
285             /* Use the information we have */
286             *Use = Info->Use;
287             *Chg = Info->Chg;
288             return;
289         }
290     }
291
292     /* Function not found - assume that the primary register is input, and all
293      * registers are changed
294      */
295     *Use = REG_EAXY;
296     *Chg = REG_ALL;
297 }
298
299
300
301 static int CompareZPInfo (const void* Name, const void* Info)
302 /* Compare function for bsearch */
303 {
304     /* Cast the pointers to the correct data type */
305     const char* N   = (const char*) Name;
306     const ZPInfo* E = (const ZPInfo*) Info;
307
308     /* Do the compare. Be careful because of the length (Info may contain
309      * more than just the zeropage name).
310      */
311     if (E->Len == 0) {
312         /* Do a full compare */
313         return strcmp (N, E->Name);
314     } else {
315         /* Only compare the first part */
316         int Res = strncmp (N, E->Name, E->Len);
317         if (Res == 0 && (N[E->Len] != '\0' && N[E->Len] != '+')) {
318             /* Name is actually longer than Info->Name */
319             Res = -1;
320         }
321         return Res;
322     }
323 }
324
325
326
327 const ZPInfo* GetZPInfo (const char* Name)
328 /* If the given name is a zero page symbol, return a pointer to the info
329  * struct for this symbol, otherwise return NULL.
330  */
331 {
332     /* Search for the zp location in the list */
333     return bsearch (Name, ZPInfoTable, ZPInfoCount,
334                     sizeof(ZPInfo), CompareZPInfo);
335 }
336
337
338
339 static unsigned GetRegInfo2 (CodeSeg* S,
340                              CodeEntry* E,
341                              int Index,
342                              Collection* Visited,
343                              unsigned Used,
344                              unsigned Unused,
345                              unsigned Wanted)
346 /* Recursively called subfunction for GetRegInfo. */
347 {
348     /* Follow the instruction flow recording register usage. */
349     while (1) {
350
351         unsigned R;
352
353         /* Check if we have already visited the current code entry. If so,
354          * bail out.
355          */
356         if (CE_HasMark (E)) {
357             break;
358         }
359
360         /* Mark this entry as already visited */
361         CE_SetMark (E);
362         CollAppend (Visited, E);
363
364         /* Evaluate the used registers */
365         R = E->Use;
366         if (E->OPC == OP65_RTS ||
367             ((E->Info & OF_BRA) != 0 && E->JumpTo == 0)) {
368             /* This instruction will leave the function */
369             R |= S->ExitRegs;
370         }
371         if (R != REG_NONE) {
372             /* We are not interested in the use of any register that has been
373              * used before.
374              */
375             R &= ~Unused;
376             /* Remember the remaining registers */
377             Used |= R;
378         }
379
380         /* Evaluate the changed registers */
381         if ((R = E->Chg) != REG_NONE) {
382             /* We are not interested in the use of any register that has been
383              * used before.
384              */
385             R &= ~Used;
386             /* Remember the remaining registers */
387             Unused |= R;
388         }
389
390         /* If we know about all registers now, bail out */
391         if (((Used | Unused) & Wanted) == Wanted) {
392             break;
393         }
394
395         /* If the instruction is an RTS or RTI, we're done */
396         if ((E->Info & OF_RET) != 0) {
397             break;
398         }
399
400         /* If we have an unconditional branch, follow this branch if possible,
401          * otherwise we're done.
402          */
403         if ((E->Info & OF_UBRA) != 0) {
404
405             /* Does this jump have a valid target? */
406             if (E->JumpTo) {
407
408                 /* Unconditional jump */
409                 E     = E->JumpTo->Owner;
410                 Index = -1;             /* Invalidate */
411
412             } else {
413                 /* Jump outside means we're done */
414                 break;
415             }
416
417         /* In case of conditional branches, follow the branch if possible and
418          * follow the normal flow (branch not taken) afterwards. If we cannot
419          * follow the branch, we're done.
420          */
421         } else if ((E->Info & OF_CBRA) != 0) {
422
423             if (E->JumpTo) {
424
425                 /* Recursively determine register usage at the branch target */
426                 unsigned U1;
427                 unsigned U2;
428
429                 U1 = GetRegInfo2 (S, E->JumpTo->Owner, -1, Visited, Used, Unused, Wanted);
430                 if (U1 == REG_ALL) {
431                     /* All registers used, no need for second call */
432                     return REG_AXY;
433                 }
434                 if (Index < 0) {
435                     Index = CS_GetEntryIndex (S, E);
436                 }
437                 if ((E = CS_GetEntry (S, ++Index)) == 0) {
438                     Internal ("GetRegInfo2: No next entry!");
439                 }
440                 U2 = GetRegInfo2 (S, E, Index, Visited, Used, Unused, Wanted);
441                 return U1 | U2;         /* Used in any of the branches */
442
443             } else {
444                 /* Jump to global symbol */
445                 break;
446             }
447
448         } else {
449
450             /* Just go to the next instruction */
451             if (Index < 0) {
452                 Index = CS_GetEntryIndex (S, E);
453             }
454             E = CS_GetEntry (S, ++Index);
455             if (E == 0) {
456                 /* No next entry */
457                 Internal ("GetRegInfo2: No next entry!");
458             }
459
460         }
461
462     }
463
464     /* Return to the caller the complement of all unused registers */
465     return Used;
466 }
467
468
469
470 static unsigned GetRegInfo1 (CodeSeg* S,
471                              CodeEntry* E,
472                              int Index,
473                              Collection* Visited,
474                              unsigned Used,
475                              unsigned Unused,
476                              unsigned Wanted)
477 /* Recursively called subfunction for GetRegInfo. */
478 {
479     /* Remember the current count of the line collection */
480     unsigned Count = CollCount (Visited);
481
482     /* Call the worker routine */
483     unsigned R = GetRegInfo2 (S, E, Index, Visited, Used, Unused, Wanted);
484
485     /* Restore the old count, unmarking all new entries */
486     unsigned NewCount = CollCount (Visited);
487     while (NewCount-- > Count) {
488         CodeEntry* E = CollAt (Visited, NewCount);
489         CE_ResetMark (E);
490         CollDelete (Visited, NewCount);
491     }
492
493     /* Return the registers used */
494     return R;
495 }
496
497
498
499 unsigned GetRegInfo (struct CodeSeg* S, unsigned Index, unsigned Wanted)
500 /* Determine register usage information for the instructions starting at the
501  * given index.
502  */
503 {
504     CodeEntry*      E;
505     Collection      Visited;    /* Visited entries */
506     unsigned        R;
507
508     /* Get the code entry for the given index */
509     if (Index >= CS_GetEntryCount (S)) {
510         /* There is no such code entry */
511         return REG_NONE;
512     }
513     E = CS_GetEntry (S, Index);
514
515     /* Initialize the data structure used to collection information */
516     InitCollection (&Visited);
517
518     /* Call the recursive subfunction */
519     R = GetRegInfo1 (S, E, Index, &Visited, REG_NONE, REG_NONE, Wanted);
520
521     /* Delete the line collection */
522     DoneCollection (&Visited);
523
524     /* Return the registers used */
525     return R;
526 }
527
528
529
530 int RegAUsed (struct CodeSeg* S, unsigned Index)
531 /* Check if the value in A is used. */
532 {
533     return (GetRegInfo (S, Index, REG_A) & REG_A) != 0;
534 }
535
536
537
538 int RegXUsed (struct CodeSeg* S, unsigned Index)
539 /* Check if the value in X is used. */
540 {
541     return (GetRegInfo (S, Index, REG_X) & REG_X) != 0;
542 }
543
544
545
546 int RegYUsed (struct CodeSeg* S, unsigned Index)
547 /* Check if the value in Y is used. */
548 {
549     return (GetRegInfo (S, Index, REG_Y) & REG_Y) != 0;
550 }
551
552
553
554 int RegAXUsed (struct CodeSeg* S, unsigned Index)
555 /* Check if the value in A or(!) the value in X are used. */
556 {
557     return (GetRegInfo (S, Index, REG_AX) & REG_AX) != 0;
558 }
559
560
561
562 unsigned GetKnownReg (unsigned Use, const RegContents* RC)
563 /* Return the register or zero page location from the set in Use, thats
564  * contents are known. If Use does not contain any register, or if the
565  * register in question does not have a known value, return REG_NONE.
566  */
567 {
568     if ((Use & REG_A) != 0) {
569         return (RC == 0 || RC->RegA >= 0)? REG_A : REG_NONE;
570     } else if ((Use & REG_X) != 0) {
571         return (RC == 0 || RC->RegX >= 0)? REG_X : REG_NONE;
572     } else if ((Use & REG_Y) != 0) {
573         return (RC == 0 || RC->RegY >= 0)? REG_Y : REG_NONE;
574     } else if ((Use & REG_TMP1) != 0) {
575         return (RC == 0 || RC->Tmp1 >= 0)? REG_TMP1 : REG_NONE;
576     } else if ((Use & REG_SREG_LO) != 0) {
577         return (RC == 0 || RC->SRegLo >= 0)? REG_SREG_LO : REG_NONE;
578     } else if ((Use & REG_SREG_HI) != 0) {
579         return (RC == 0 || RC->SRegHi >= 0)? REG_SREG_HI : REG_NONE;
580     } else {
581         return REG_NONE;
582     }
583 }
584
585
586