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