]> git.sur5r.net Git - cc65/blob - src/cc65/codeinfo.c
34905af669313a35906b9ff94dc08caf672e5819
[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 "error.h"
46 #include "codeinfo.h"
47
48
49
50 /*****************************************************************************/
51 /*                                   Data                                    */
52 /*****************************************************************************/
53
54
55
56 /* Table listing the function names and code info values for known internally
57  * used functions. This table should get auto-generated in the future.
58  */
59 typedef struct FuncInfo FuncInfo;
60 struct FuncInfo {
61     const char*     Name;       /* Function name */
62     unsigned char   Use;        /* Register usage */
63     unsigned char   Chg;        /* Changed/destroyed registers */
64 };
65
66 static const FuncInfo FuncInfoTable[] = {
67     { "addysp",         REG_Y,          REG_NONE        },
68     { "booleq",         REG_NONE,       REG_AX          },
69     { "boolge",         REG_NONE,       REG_AX          },
70     { "boolgt",         REG_NONE,       REG_AX          },
71     { "boolle",         REG_NONE,       REG_AX          },
72     { "boollt",         REG_NONE,       REG_AX          },
73     { "boolne",         REG_NONE,       REG_AX          },
74     { "booluge",        REG_NONE,       REG_AX          },
75     { "boolugt",        REG_NONE,       REG_AX          },
76     { "boolule",        REG_NONE,       REG_AX          },
77     { "boolult",        REG_NONE,       REG_AX          },
78     { "decax1",         REG_AX,         REG_AX          },
79     { "decax2",         REG_AX,         REG_AX          },
80     { "decax3",         REG_AX,         REG_AX          },
81     { "decax4",         REG_AX,         REG_AX          },
82     { "decax5",         REG_AX,         REG_AX          },
83     { "decax6",         REG_AX,         REG_AX          },
84     { "decax7",         REG_AX,         REG_AX          },
85     { "decax8",         REG_AX,         REG_AX          },
86     { "decaxy",         REG_AXY,        REG_AX          },
87     { "decsp2",         REG_NONE,       REG_A           },
88     { "decsp3",         REG_NONE,       REG_A           },
89     { "decsp4",         REG_NONE,       REG_A           },
90     { "decsp5",         REG_NONE,       REG_A           },
91     { "decsp6",         REG_NONE,       REG_A           },
92     { "decsp7",         REG_NONE,       REG_A           },
93     { "decsp8",         REG_NONE,       REG_A           },
94     { "incsp1",         REG_NONE,       REG_NONE        },
95     { "incsp2",         REG_NONE,       REG_Y           },
96     { "incsp3",         REG_NONE,       REG_Y           },
97     { "incsp4",         REG_NONE,       REG_Y           },
98     { "incsp5",         REG_NONE,       REG_Y           },
99     { "incsp6",         REG_NONE,       REG_Y           },
100     { "incsp7",         REG_NONE,       REG_Y           },
101     { "incsp8",         REG_NONE,       REG_Y           },
102     { "ldax0sp",        REG_Y,          REG_AX          },
103     { "ldaxysp",        REG_Y,          REG_AX          },
104     { "pusha",          REG_A,          REG_Y           },
105     { "pusha0",         REG_A,          REG_XY          },
106     { "pushax",         REG_AX,         REG_Y           },
107     { "pushw0sp",       REG_NONE,       REG_AXY         },
108     { "pushwysp",       REG_Y,          REG_AXY         },
109     { "tosicmp",        REG_AX,         REG_AXY         },
110 };
111 #define FuncInfoCount   (sizeof(FuncInfoTable) / sizeof(FuncInfoTable[0]))
112
113
114
115 /*****************************************************************************/
116 /*                                   Code                                    */
117 /*****************************************************************************/
118
119
120
121 static int CompareFuncInfo (const void* Key, const void* Info)
122 /* Compare function for bsearch */
123 {
124     return strcmp (Key, ((const FuncInfo*) Info)->Name);
125 }
126
127
128
129 void GetFuncInfo (const char* Name, unsigned char* Use, unsigned char* Chg)
130 /* For the given function, lookup register information and combine it with
131  * the information already in place. If the function is unknown, assume it
132  * will use all registers and load all registers.
133  * See codeinfo.h for possible flags.
134  */
135 {
136     /* Search for the function */
137     const FuncInfo* Info = bsearch (Name, FuncInfoTable, FuncInfoCount,
138                                     sizeof(FuncInfo), CompareFuncInfo);
139
140     /* Do we know the function? */
141     if (Info) {
142         /* Use the information we have */
143         *Use |= Info->Use;
144         *Chg |= Info->Chg;
145     } else {
146         /* Assume all registers used */
147         *Use |= REG_AXY;
148         *Chg |= REG_AXY;
149     }
150 }
151
152
153
154 static unsigned char GetRegInfo2 (CodeSeg* S,
155                                   CodeEntry* E,
156                                   int Index,
157                                   Collection* Visited,
158                                   unsigned char Used,
159                                   unsigned char Unused)
160 /* Recursively called subfunction for GetRegInfo. */
161 {
162     /* Follow the instruction flow recording register usage. */
163     while (1) {
164
165         unsigned char R;
166
167         /* Check if we have already visited the current code entry. If so,
168          * bail out.
169          */
170         if (CodeEntryHasMark (E)) {
171             break;
172         }
173
174         /* Mark this entry as already visited */
175         CodeEntrySetMark (E);
176         CollAppend (Visited, E);
177
178         /* Evaluate the used registers */
179         R = E->Use;
180         if (E->OPC == OPC_RTS ||
181             ((E->Info & OF_BRA) != 0 && E->JumpTo == 0)) {
182             /* This instruction will leave the function */
183             R |= S->ExitRegs;
184         }
185         if (R != REG_NONE) {
186             /* We are not interested in the use of any register that has been
187              * used before.
188              */
189             R &= ~Unused;
190             /* Remember the remaining registers */
191             Used |= R;
192         }
193
194         /* Evaluate the changed registers */
195         if ((R = E->Chg) != REG_NONE) {
196             /* We are not interested in the use of any register that has been
197              * used before.
198              */
199             R &= ~Used;
200             /* Remember the remaining registers */
201             Unused |= R;
202         }
203
204         /* If we know about all registers now, bail out */
205         if ((Used | Unused) == REG_AXY) {
206             break;
207         }
208
209         /* If the instruction is an RTS or RTI, we're done */
210         if (E->OPC == OPC_RTS || E->OPC == OPC_RTI) {
211             break;
212         }
213
214         /* If we have an unconditional branch, follow this branch if possible,
215          * otherwise we're done.
216          */
217         if ((E->Info & OF_UBRA) != 0) {
218
219             /* Does this jump have a valid target? */
220             if (E->JumpTo) {
221
222                 /* Unconditional jump */
223                 E     = E->JumpTo->Owner;
224                 Index = -1;             /* Invalidate */
225
226             } else {
227                 /* Jump outside means we're done */
228                 break;
229             }
230
231         /* In case of conditional branches, follow the branch if possible and
232          * follow the normal flow (branch not taken) afterwards. If we cannot
233          * follow the branch, we're done.
234          */
235         } else if ((E->Info & OF_CBRA) != 0) {
236
237             if (E->JumpTo) {
238
239                 /* Recursively determine register usage at the branch target */
240                 unsigned char U1;
241                 unsigned char U2;
242
243                 U1 = GetRegInfo2 (S, E->JumpTo->Owner, -1, Visited, Used, Unused);
244                 if (U1 == REG_AXY) {
245                     /* All registers used, no need for second call */
246                     return REG_AXY;
247                 }
248                 if (Index < 0) {
249                     Index = GetCodeEntryIndex (S, E);
250                 }
251                 if ((E = GetCodeEntry (S, ++Index)) == 0) {
252                     Internal ("GetRegInfo2: No next entry!");
253                 }
254                 U2 = GetRegInfo2 (S, E, Index, Visited, Used, Unused);
255                 return U1 | U2;         /* Used in any of the branches */
256
257             } else {
258                 /* Jump to global symbol */
259                 break;
260             }
261
262         } else {
263
264             /* Just go to the next instruction */
265             if (Index < 0) {
266                 Index = GetCodeEntryIndex (S, E);
267             }
268             E = GetCodeEntry (S, ++Index);
269             if (E == 0) {
270                 /* No next entry */
271                 Internal ("GetRegInfo2: No next entry!");
272             }
273
274         }
275
276     }
277
278     /* Return to the caller the complement of all unused registers */
279     return Used;
280 }
281
282
283
284 static unsigned char GetRegInfo1 (CodeSeg* S,
285                                   CodeEntry* E,
286                                   int Index,
287                                   Collection* Visited,
288                                   unsigned char Used,
289                                   unsigned char Unused)
290 /* Recursively called subfunction for GetRegInfo. */
291 {
292     /* Remember the current count of the line collection */
293     unsigned Count = CollCount (Visited);
294
295     /* Call the worker routine */
296     unsigned char R = GetRegInfo2 (S, E, Index, Visited, Used, Unused);
297
298     /* Restore the old count, unmarking all new entries */
299     unsigned NewCount = CollCount (Visited);
300     while (NewCount-- > Count) {
301         CodeEntry* E = CollAt (Visited, NewCount);
302         CodeEntryResetMark (E);
303         CollDelete (Visited, NewCount);
304     }
305
306     /* Return the registers used */
307     return R;
308 }
309
310
311
312 unsigned char GetRegInfo (struct CodeSeg* S, unsigned Index)
313 /* Determine register usage information for the instructions starting at the
314  * given index.
315  */
316 {
317     CodeEntry*      E;
318     Collection      Visited;    /* Visited entries */
319     unsigned char   R;
320
321     /* Get the code entry for the given index */
322     if (Index >= GetCodeEntryCount (S)) {
323         /* There is no such code entry */
324         return REG_NONE;
325     }
326     E = GetCodeEntry (S, Index);
327
328     /* Initialize the data structure used to collection information */
329     InitCollection (&Visited);
330
331     /* Call the recursive subfunction */
332     R = GetRegInfo1 (S, E, Index, &Visited, REG_NONE, REG_NONE);
333
334     /* Delete the line collection */
335     DoneCollection (&Visited);
336
337     /* Return the registers used */
338     return R;
339 }
340
341
342
343 int RegAUsed (struct CodeSeg* S, unsigned Index)
344 /* Check if the value in A is used. */
345 {
346     return (GetRegInfo (S, Index) & REG_A) != 0;
347 }
348
349
350
351 int RegXUsed (struct CodeSeg* S, unsigned Index)
352 /* Check if the value in X is used. */
353 {
354     return (GetRegInfo (S, Index) & REG_X) != 0;
355 }
356
357
358
359 int RegYUsed (struct CodeSeg* S, unsigned Index)
360 /* Check if the value in Y is used. */
361 {
362     return (GetRegInfo (S, Index) & REG_Y) != 0;
363 }
364
365
366