]> git.sur5r.net Git - cc65/blob - src/cc65/codeinfo.c
84309174b54dad0ca41e2c578792c5d740e05912
[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         if ((R = E->Use) != REG_NONE) {
180             /* We are not interested in the use of any register that has been
181              * used before.
182              */
183             R &= ~Unused;
184             /* Remember the remaining registers */
185             Used |= R;
186         }
187
188         /* Evaluate the changed registers */
189         if ((R = E->Chg) != REG_NONE) {
190             /* We are not interested in the use of any register that has been
191              * used before.
192              */
193             R &= ~Used;
194             /* Remember the remaining registers */
195             Unused |= R;
196         }
197
198         /* If we know about all registers now, bail out */
199         if ((Used | Unused) == REG_AXY) {
200             break;
201         }
202
203         /* If the instruction is an RTS or RTI, we're done */
204         if (E->OPC == OPC_RTS || E->OPC == OPC_RTI) {
205             break;
206         }
207
208         /* If we have an unconditional branch, follow this branch if possible,
209          * otherwise we're done.
210          */
211         if ((E->Info & OF_UBRA) != 0) {
212
213             /* Does this jump have a valid target? */
214             if (E->JumpTo) {
215
216                 /* Unconditional jump */
217                 E     = E->JumpTo->Owner;
218                 Index = -1;             /* Invalidate */
219
220             } else {
221                 /* Jump outside means we're done */
222                 break;
223             }
224
225         /* In case of conditional branches, follow the branch if possible and
226          * follow the normal flow (branch not taken) afterwards. If we cannot
227          * follow the branch, we're done.
228          */
229         } else if ((E->Info & OF_CBRA) != 0) {
230
231             if (E->JumpTo) {
232
233                 /* Recursively determine register usage at the branch target */
234                 unsigned char U1;
235                 unsigned char U2;
236
237                 U1 = GetRegInfo2 (S, E->JumpTo->Owner, -1, Visited, Used, Unused);
238                 if (U1 == REG_AXY) {
239                     /* All registers used, no need for second call */
240                     return REG_AXY;
241                 }
242                 if (Index < 0) {
243                     Index = GetCodeEntryIndex (S, E);
244                 }
245                 if ((E = GetCodeEntry (S, ++Index)) == 0) {
246                     Internal ("GetRegInfo2: No next entry!");
247                 }
248                 U2 = GetRegInfo2 (S, E, Index, Visited, Used, Unused);
249                 return U1 | U2;         /* Used in any of the branches */
250
251             } else {
252                 /* Jump to global symbol */
253                 break;
254             }
255
256         } else {
257
258             /* Just go to the next instruction */
259             if (Index < 0) {
260                 Index = GetCodeEntryIndex (S, E);
261             }
262             E = GetCodeEntry (S, ++Index);
263             if (E == 0) {
264                 /* No next entry */
265                 Internal ("GetRegInfo2: No next entry!");
266             }
267
268         }
269
270     }
271
272     /* Return to the caller the complement of all unused registers */
273     return Used;
274 }
275
276
277
278 static unsigned char GetRegInfo1 (CodeSeg* S,
279                                   CodeEntry* E,
280                                   int Index,
281                                   Collection* Visited,
282                                   unsigned char Used,
283                                   unsigned char Unused)
284 /* Recursively called subfunction for GetRegInfo. */
285 {
286     /* Remember the current count of the line collection */
287     unsigned Count = CollCount (Visited);
288
289     /* Call the worker routine */
290     unsigned char R = GetRegInfo2 (S, E, Index, Visited, Used, Unused);
291
292     /* Restore the old count, unmarking all new entries */
293     unsigned NewCount = CollCount (Visited);
294     while (NewCount-- > Count) {
295         CodeEntry* E = CollAt (Visited, NewCount);
296         CodeEntryResetMark (E);
297         CollDelete (Visited, NewCount);
298     }
299
300     /* Return the registers used */
301     return R;
302 }
303
304
305
306 unsigned char GetRegInfo (struct CodeSeg* S, unsigned Index)
307 /* Determine register usage information for the instructions starting at the
308  * given index.
309  */
310 {
311     CodeEntry*      E;
312     Collection      Visited;    /* Visited entries */
313     unsigned char   R;
314
315     /* Get the code entry for the given index */
316     if (Index >= GetCodeEntryCount (S)) {
317         /* There is no such code entry */
318         return REG_NONE;
319     }
320     E = GetCodeEntry (S, Index);
321
322     /* Initialize the data structure used to collection information */
323     InitCollection (&Visited);
324
325     /* Call the recursive subfunction */
326     R = GetRegInfo1 (S, E, Index, &Visited, REG_NONE, REG_NONE);
327
328     /* Delete the line collection */
329     DoneCollection (&Visited);
330
331     /* Return the registers used */
332     return R;
333 }
334
335
336
337 int RegAUsed (struct CodeSeg* S, unsigned Index)
338 /* Check if the value in A is used. */
339 {
340     return (GetRegInfo (S, Index) & REG_A) != 0;
341 }
342
343
344
345 int RegXUsed (struct CodeSeg* S, unsigned Index)
346 /* Check if the value in X is used. */
347 {
348     return (GetRegInfo (S, Index) & REG_X) != 0;
349 }
350
351
352
353 int RegYUsed (struct CodeSeg* S, unsigned Index)
354 /* Check if the value in Y is used. */
355 {
356     return (GetRegInfo (S, Index) & REG_Y) != 0;
357 }
358
359
360