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