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