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