]> git.sur5r.net Git - cc65/blob - src/cc65/codeinfo.c
Added/corrected code info.
[cc65] / src / cc65 / codeinfo.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                codeinfo.c                                 */
4 /*                                                                           */
5 /*                  Additional information about 6502 code                   */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2001-2009, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
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 "chartype.h"
41 #include "coll.h"
42 #include "debugflag.h"
43
44 /* cc65 */
45 #include "codeent.h"
46 #include "codeseg.h"
47 #include "datatype.h"
48 #include "error.h"
49 #include "reginfo.h"
50 #include "symtab.h"
51 #include "codeinfo.h"
52
53
54
55 /*****************************************************************************/
56 /*                                   Data                                    */
57 /*****************************************************************************/
58
59
60
61 /* Table with the compare suffixes */
62 static const char CmpSuffixTab [][4] = {
63     "eq", "ne", "gt", "ge", "lt", "le", "ugt", "uge", "ult", "ule"
64 };
65
66 /* Table listing the function names and code info values for known internally
67  * used functions. This table should get auto-generated in the future.
68  */
69 typedef struct FuncInfo FuncInfo;
70 struct FuncInfo {
71     const char*     Name;       /* Function name */
72     unsigned short  Use;        /* Register usage */
73     unsigned short  Chg;        /* Changed/destroyed registers */
74 };
75
76 /* Note for the shift functions: Shifts are done modulo 32, so all shift
77  * routines are marked to use only the A register. The remainder is ignored
78  * anyway.
79  */                          
80 static const FuncInfo FuncInfoTable[] = {
81     { "addeq0sp",       REG_AX,               REG_AXY                        },
82     { "addeqysp",       REG_AXY,              REG_AXY                        },
83     { "addysp",         REG_Y,                REG_NONE                       },
84     { "aslax1",         REG_AX,               REG_AX | REG_TMP1              },
85     { "aslax2",         REG_AX,               REG_AX | REG_TMP1              },
86     { "aslax3",         REG_AX,               REG_AX | REG_TMP1              },
87     { "aslax4",         REG_AX,               REG_AX | REG_TMP1              },
88     { "asrax1",         REG_AX,               REG_AX | REG_TMP1              },
89     { "asrax2",         REG_AX,               REG_AX | REG_TMP1              },
90     { "asrax3",         REG_AX,               REG_AX | REG_TMP1              },
91     { "asrax4",         REG_AX,               REG_AX | REG_TMP1              },
92     { "bnega",          REG_A,                REG_AX                         },
93     { "bnegax",         REG_AX,               REG_AX                         },
94     { "bnegeax",        REG_EAX,              REG_EAX                        },
95     { "booleq",         REG_NONE,             REG_AX                         },
96     { "boolge",         REG_NONE,             REG_AX                         },
97     { "boolgt",         REG_NONE,             REG_AX                         },
98     { "boolle",         REG_NONE,             REG_AX                         },
99     { "boollt",         REG_NONE,             REG_AX                         },
100     { "boolne",         REG_NONE,             REG_AX                         },
101     { "booluge",        REG_NONE,             REG_AX                         },
102     { "boolugt",        REG_NONE,             REG_AX                         },
103     { "boolule",        REG_NONE,             REG_AX                         },
104     { "boolult",        REG_NONE,             REG_AX                         },
105     { "callax",         REG_AX,               REG_ALL                        },
106     { "complax",        REG_AX,               REG_AX                         },
107     { "decax1",         REG_AX,               REG_AX                         },
108     { "decax2",         REG_AX,               REG_AX                         },
109     { "decax3",         REG_AX,               REG_AX                         },
110     { "decax4",         REG_AX,               REG_AX                         },
111     { "decax5",         REG_AX,               REG_AX                         },
112     { "decax6",         REG_AX,               REG_AX                         },
113     { "decax7",         REG_AX,               REG_AX                         },
114     { "decax8",         REG_AX,               REG_AX                         },
115     { "decaxy",         REG_AXY,              REG_AX | REG_TMP1              },
116     { "deceaxy",        REG_EAXY,             REG_EAX                        },
117     { "decsp1",         REG_NONE,             REG_Y                          },
118     { "decsp2",         REG_NONE,             REG_A                          },
119     { "decsp3",         REG_NONE,             REG_A                          },
120     { "decsp4",         REG_NONE,             REG_A                          },
121     { "decsp5",         REG_NONE,             REG_A                          },
122     { "decsp6",         REG_NONE,             REG_A                          },
123     { "decsp7",         REG_NONE,             REG_A                          },
124     { "decsp8",         REG_NONE,             REG_A                          },
125     { "incax1",         REG_AX,               REG_AX                         },
126     { "incax2",         REG_AX,               REG_AX                         },
127     { "incax3",         REG_AX,               REG_AXY | REG_TMP1             },
128     { "incax4",         REG_AX,               REG_AXY | REG_TMP1             },
129     { "incax5",         REG_AX,               REG_AXY | REG_TMP1             },
130     { "incax6",         REG_AX,               REG_AXY | REG_TMP1             },
131     { "incax7",         REG_AX,               REG_AXY | REG_TMP1             },
132     { "incax8",         REG_AX,               REG_AXY | REG_TMP1             },
133     { "incaxy",         REG_AXY,              REG_AXY | REG_TMP1             },
134     { "incsp1",         REG_NONE,             REG_NONE                       },
135     { "incsp2",         REG_NONE,             REG_Y                          },
136     { "incsp3",         REG_NONE,             REG_Y                          },
137     { "incsp4",         REG_NONE,             REG_Y                          },
138     { "incsp5",         REG_NONE,             REG_Y                          },
139     { "incsp6",         REG_NONE,             REG_Y                          },
140     { "incsp7",         REG_NONE,             REG_Y                          },
141     { "incsp8",         REG_NONE,             REG_Y                          },
142     { "laddeq",         REG_EAXY|REG_PTR1_LO, REG_EAXY | REG_PTR1_HI         },
143     { "laddeq0sp",      REG_EAX,              REG_EAXY                       },
144     { "laddeq1",        REG_Y | REG_PTR1_LO,  REG_EAXY | REG_PTR1_HI         },
145     { "laddeqa",        REG_AY | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI         },
146     { "laddeqysp",      REG_EAXY,             REG_EAXY                       },
147     { "ldaidx",         REG_AXY,              REG_AX | REG_PTR1              },
148     { "ldauidx",        REG_AXY,              REG_AX | REG_PTR1              },
149     { "ldax0sp",        REG_NONE,             REG_AXY                        },
150     { "ldaxi",          REG_AX,               REG_AXY | REG_PTR1             },
151     { "ldaxidx",        REG_AXY,              REG_AXY | REG_PTR1             },
152     { "ldaxysp",        REG_Y,                REG_AXY                        },
153     { "ldeax0sp",       REG_NONE,             REG_EAXY                       },
154     { "ldeaxi",         REG_AX,               REG_EAXY | REG_PTR1            },
155     { "ldeaxidx",       REG_AXY,              REG_EAXY | REG_PTR1            },
156     { "ldeaxysp",       REG_Y,                REG_EAXY                       },
157     { "leaasp",         REG_A,                REG_AX                         },
158     { "lsubeq",         REG_EAXY|REG_PTR1_LO, REG_EAXY | REG_PTR1_HI         },
159     { "lsubeq0sp",      REG_EAX,              REG_EAXY                       },
160     { "lsubeq1",        REG_Y | REG_PTR1_LO,  REG_EAXY | REG_PTR1_HI         },
161     { "lsubeqa",        REG_AY | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI         },
162     { "lsubeqysp",      REG_EAXY,             REG_EAXY                       },
163     { "mulax10",        REG_AX,               REG_AX | REG_PTR1              },
164     { "mulax3",         REG_AX,               REG_AX | REG_PTR1              },
165     { "mulax5",         REG_AX,               REG_AX | REG_PTR1              },
166     { "mulax6",         REG_AX,               REG_AX | REG_PTR1              },
167     { "mulax7",         REG_AX,               REG_AX | REG_PTR1              },
168     { "mulax9",         REG_AX,               REG_AX | REG_PTR1              },
169     { "negax",          REG_AX,               REG_AX                         },
170     { "push0",          REG_NONE,             REG_AXY                        },
171     { "push0ax",        REG_AX,               REG_Y | REG_SREG               },
172     { "push1",          REG_NONE,             REG_AXY                        },
173     { "push2",          REG_NONE,             REG_AXY                        },
174     { "push3",          REG_NONE,             REG_AXY                        },
175     { "push4",          REG_NONE,             REG_AXY                        },
176     { "push5",          REG_NONE,             REG_AXY                        },
177     { "push6",          REG_NONE,             REG_AXY                        },
178     { "push7",          REG_NONE,             REG_AXY                        },
179     { "pusha",          REG_A,                REG_Y                          },
180     { "pusha0",         REG_A,                REG_XY                         },
181     { "pusha0sp",       REG_NONE,             REG_AY                         },
182     { "pushaFF",        REG_A,                REG_Y                          },
183     { "pushax",         REG_AX,               REG_Y                          },
184     { "pushaysp",       REG_Y,                REG_AY                         },
185     { "pushc0",         REG_NONE,             REG_A | REG_Y                  },
186     { "pushc1",         REG_NONE,             REG_A | REG_Y                  },
187     { "pushc2",         REG_NONE,             REG_A | REG_Y                  },
188     { "pusheax",        REG_EAX,              REG_Y                          },
189     { "pushw",          REG_AX,               REG_AXY | REG_PTR1             },
190     { "pushw0sp",       REG_NONE,             REG_AXY                        },
191     { "pushwidx",       REG_AXY,              REG_AXY | REG_PTR1             },
192     { "pushwysp",       REG_Y,                REG_AXY                        },
193     { "regswap",        REG_AXY,              REG_AXY | REG_TMP1             },
194     { "regswap1",       REG_XY,               REG_A                          },
195     { "regswap2",       REG_XY,               REG_A | REG_Y                  },
196     { "return0",        REG_NONE,             REG_AX                         },
197     { "return1",        REG_NONE,             REG_AX                         },
198     { "shlax1",         REG_AX,               REG_AX | REG_TMP1              },
199     { "shlax2",         REG_AX,               REG_AX | REG_TMP1              },
200     { "shlax3",         REG_AX,               REG_AX | REG_TMP1              },
201     { "shlax4",         REG_AX,               REG_AX | REG_TMP1              },
202     { "shrax1",         REG_AX,               REG_AX | REG_TMP1              },
203     { "shrax2",         REG_AX,               REG_AX | REG_TMP1              },
204     { "shrax3",         REG_AX,               REG_AX | REG_TMP1              },
205     { "shrax4",         REG_AX,               REG_AX | REG_TMP1              },
206     { "shreax1",        REG_EAX,              REG_AX | REG_TMP1              },
207     { "shreax2",        REG_EAX,              REG_AX | REG_TMP1              },
208     { "shreax3",        REG_EAX,              REG_AX | REG_TMP1              },
209     { "shreax4",        REG_EAX,              REG_AX | REG_TMP1              },
210     { "staspidx",       REG_A | REG_Y,        REG_Y | REG_TMP1 | REG_PTR1    },
211     { "stax0sp",        REG_AX,               REG_Y                          },
212     { "staxspidx",      REG_AXY,              REG_TMP1 | REG_PTR1            },
213     { "staxysp",        REG_AXY,              REG_Y                          },
214     { "steax0sp",       REG_EAX,              REG_Y                          },
215     { "steaxysp",       REG_EAXY,             REG_Y                          },
216     { "subeq0sp",       REG_AX,               REG_AXY                        },
217     { "subeqysp",       REG_AXY,              REG_AXY                        },
218     { "tosadd0ax",      REG_AX,               REG_EAXY | REG_TMP1            },
219     { "tosadda0",       REG_A,                REG_AXY                        },
220     { "tosaddax",       REG_AX,               REG_AXY                        },
221     { "tosaddeax",      REG_EAX,              REG_EAXY | REG_TMP1            },
222     { "tosand0ax",      REG_AX,               REG_EAX | REG_TMP1             },
223     { "tosanda0",       REG_A,                REG_AXY                        },
224     { "tosandax",       REG_AX,               REG_AXY                        },
225     { "tosandeax",      REG_EAX,              REG_EAX | REG_TMP1             },
226     { "tosaslax",       REG_A,                REG_AXY | REG_TMP1             },
227     { "tosasleax",      REG_A,                REG_EAXY | REG_TMP1            },
228     { "tosasrax",       REG_A,                REG_AXY | REG_TMP1             },
229     { "tosasreax",      REG_A,                REG_EAXY | REG_TMP1            },
230     { "tosdiv0ax",      REG_AX,               REG_ALL                        },
231     { "tosdiva0",       REG_A,                REG_ALL                        },
232     { "tosdivax",       REG_AX,               REG_ALL                        },
233     { "tosdiveax",      REG_EAX,              REG_ALL                        },
234     { "toseq00",        REG_NONE,             REG_AXY | REG_SREG             },
235     { "toseqa0",        REG_A,                REG_AXY | REG_SREG             },
236     { "toseqax",        REG_AX,               REG_AXY | REG_SREG             },
237     { "toseqeax",       REG_EAX,              REG_AXY | REG_PTR1             },
238     { "tosge00",        REG_NONE,             REG_AXY | REG_SREG             },
239     { "tosgea0",        REG_A,                REG_AXY | REG_SREG             },
240     { "tosgeax",        REG_AX,               REG_AXY | REG_SREG             },
241     { "tosgeeax",       REG_EAX,              REG_AXY | REG_PTR1             },
242     { "tosgt00",        REG_NONE,             REG_AXY | REG_SREG             },
243     { "tosgta0",        REG_A,                REG_AXY | REG_SREG             },
244     { "tosgtax",        REG_AX,               REG_AXY | REG_SREG             },
245     { "tosgteax",       REG_EAX,              REG_AXY | REG_PTR1             },
246     { "tosicmp",        REG_AX,               REG_AXY | REG_SREG             },
247     { "tosicmp0",       REG_A,                REG_AXY | REG_SREG             },
248     { "toslcmp",        REG_EAX,              REG_A | REG_Y | REG_PTR1       },
249     { "tosle00",        REG_NONE,             REG_AXY | REG_SREG             },
250     { "toslea0",        REG_A,                REG_AXY | REG_SREG             },
251     { "tosleax",        REG_AX,               REG_AXY | REG_SREG             },
252     { "tosleeax",       REG_EAX,              REG_AXY | REG_PTR1             },
253     { "toslt00",        REG_NONE,             REG_AXY | REG_SREG             },
254     { "toslta0",        REG_A,                REG_AXY | REG_SREG             },
255     { "tosltax",        REG_AX,               REG_AXY | REG_SREG             },
256     { "toslteax",       REG_EAX,              REG_AXY | REG_PTR1             },
257     { "tosmod0ax",      REG_AX,               REG_ALL                        },
258     { "tosmodeax",      REG_EAX,              REG_ALL                        },
259     { "tosmul0ax",      REG_AX,               REG_ALL                        },
260     { "tosmula0",       REG_A,                REG_ALL                        },
261     { "tosmulax",       REG_AX,               REG_ALL                        },
262     { "tosmuleax",      REG_EAX,              REG_ALL                        },
263     { "tosne00",        REG_NONE,             REG_AXY | REG_SREG             },
264     { "tosnea0",        REG_A,                REG_AXY | REG_SREG             },
265     { "tosneax",        REG_AX,               REG_AXY | REG_SREG             },
266     { "tosneeax",       REG_EAX,              REG_AXY | REG_PTR1             },
267     { "tosor0ax",       REG_AX,               REG_EAX | REG_TMP1             },
268     { "tosora0",        REG_A,                REG_AXY | REG_TMP1             },
269     { "tosorax",        REG_AX,               REG_AXY | REG_TMP1             },
270     { "tosoreax",       REG_EAX,              REG_EAX | REG_TMP1             },
271     { "tosrsub0ax",     REG_AX,               REG_EAX | REG_TMP1             },
272     { "tosrsuba0",      REG_A,                REG_AX | REG_TMP1              },
273     { "tosrsubax",      REG_AX,               REG_AX | REG_TMP1              },
274     { "tosrsubeax",     REG_EAX,              REG_EAX | REG_TMP1             },
275     { "tosshlax",       REG_A,                REG_AXY | REG_TMP1             },
276     { "tosshleax",      REG_A,                REG_EAXY | REG_TMP1            },
277     { "tosshrax",       REG_A,                REG_AXY | REG_TMP1             },
278     { "tosshreax",      REG_A,                REG_EAXY | REG_TMP1            },
279     { "tossub0ax",      REG_AX,               REG_EAXY                       },
280     { "tossuba0",       REG_A,                REG_AXY                        },
281     { "tossubax",       REG_AX,               REG_AXY                        },
282     { "tossubeax",      REG_EAX,              REG_EAXY                       },
283     { "tosudiv0ax",     REG_AX,               REG_ALL & ~REG_SAVE            },
284     { "tosudiva0",      REG_A,                REG_EAXY | REG_PTR1            }, /* also ptr4 */
285     { "tosudivax",      REG_AX,               REG_EAXY | REG_PTR1            }, /* also ptr4 */
286     { "tosudiveax",     REG_EAX,              REG_ALL & ~REG_SAVE            },
287     { "tosuge00",       REG_NONE,             REG_AXY | REG_SREG             },
288     { "tosugea0",       REG_A,                REG_AXY | REG_SREG             },
289     { "tosugeax",       REG_AX,               REG_AXY | REG_SREG             },
290     { "tosugeeax",      REG_EAX,              REG_AXY | REG_PTR1             },
291     { "tosugt00",       REG_NONE,             REG_AXY | REG_SREG             },
292     { "tosugta0",       REG_A,                REG_AXY | REG_SREG             },
293     { "tosugtax",       REG_AX,               REG_AXY | REG_SREG             },
294     { "tosugteax",      REG_EAX,              REG_AXY | REG_PTR1             },
295     { "tosule00",       REG_NONE,             REG_AXY | REG_SREG             },
296     { "tosulea0",       REG_A,                REG_AXY | REG_SREG             },
297     { "tosuleax",       REG_AX,               REG_AXY | REG_SREG             },
298     { "tosuleeax",      REG_EAX,              REG_AXY | REG_PTR1             },
299     { "tosult00",       REG_NONE,             REG_AXY | REG_SREG             },
300     { "tosulta0",       REG_A,                REG_AXY | REG_SREG             },
301     { "tosultax",       REG_AX,               REG_AXY | REG_SREG             },
302     { "tosulteax",      REG_EAX,              REG_AXY | REG_PTR1             },
303     { "tosumod0ax",     REG_AX,               REG_ALL & ~REG_SAVE            },
304     { "tosumoda0",      REG_A,                REG_EAXY | REG_PTR1            }, /* also ptr4 */
305     { "tosumodax",      REG_AX,               REG_EAXY | REG_PTR1            }, /* also ptr4 */
306     { "tosumodeax",     REG_EAX,              REG_ALL & ~REG_SAVE            },
307     { "tosumul0ax",     REG_AX,               REG_ALL                        },
308     { "tosumula0",      REG_A,                REG_ALL                        },
309     { "tosumulax",      REG_AX,               REG_ALL                        },
310     { "tosumuleax",     REG_EAX,              REG_ALL                        },
311     { "tosxor0ax",      REG_AX,               REG_EAX | REG_TMP1             },
312     { "tosxora0",       REG_A,                REG_AX | REG_TMP1              },
313     { "tosxorax",       REG_AX,               REG_AX | REG_TMP1              },
314     { "tosxoreax",      REG_EAX,              REG_EAX | REG_TMP1             },
315     { "tsteax",         REG_EAX,              REG_Y                          },
316     { "utsteax",        REG_EAX,              REG_Y                          },
317 };
318 #define FuncInfoCount   (sizeof(FuncInfoTable) / sizeof(FuncInfoTable[0]))
319
320 /* Table with names of zero page locations used by the compiler */
321 static const ZPInfo ZPInfoTable[] = {
322     {   0, "ptr1",      REG_PTR1_LO,    REG_PTR1        },
323     {   0, "ptr1+1",    REG_PTR1_HI,    REG_PTR1        },
324     {   0, "ptr2",      REG_PTR2_LO,    REG_PTR2        },
325     {   0, "ptr2+1",    REG_PTR2_HI,    REG_PTR2        },
326     {   4, "ptr3",      REG_NONE,       REG_NONE        },
327     {   4, "ptr4",      REG_NONE,       REG_NONE        },
328     {   7, "regbank",   REG_NONE,       REG_NONE        },
329     {   0, "regsave",   REG_SAVE_LO,    REG_SAVE        },
330     {   0, "regsave+1", REG_SAVE_HI,    REG_SAVE        },
331     {   0, "sp",        REG_SP_LO,      REG_SP          },
332     {   0, "sp+1",      REG_SP_HI,      REG_SP          },
333     {   0, "sreg",      REG_SREG_LO,    REG_SREG        },
334     {   0, "sreg+1",    REG_SREG_HI,    REG_SREG        },
335     {   0, "tmp1",      REG_TMP1,       REG_TMP1        },
336     {   0, "tmp2",      REG_NONE,       REG_NONE        },
337     {   0, "tmp3",      REG_NONE,       REG_NONE        },
338     {   0, "tmp4",      REG_NONE,       REG_NONE        },
339 };
340 #define ZPInfoCount     (sizeof(ZPInfoTable) / sizeof(ZPInfoTable[0]))
341
342
343
344 /*****************************************************************************/
345 /*                                   Code                                    */
346 /*****************************************************************************/
347
348
349
350 static int CompareFuncInfo (const void* Key, const void* Info)
351 /* Compare function for bsearch */
352 {
353     return strcmp (Key, ((const FuncInfo*) Info)->Name);
354 }
355
356
357
358 void GetFuncInfo (const char* Name, unsigned short* Use, unsigned short* Chg)
359 /* For the given function, lookup register information and store it into
360  * the given variables. If the function is unknown, assume it will use and
361  * load all registers.
362  */
363 {
364     /* If the function name starts with an underline, it is an external
365      * function. Search for it in the symbol table. If the function does
366      * not start with an underline, it may be a runtime support function.
367      * Search for it in the list of builtin functions.
368      */
369     if (Name[0] == '_') {
370
371         /* Search in the symbol table, skip the leading underscore */
372         SymEntry* E = FindGlobalSym (Name+1);
373
374         /* Did we find it in the top level table? */
375         if (E && IsTypeFunc (E->Type)) {
376
377             FuncDesc* D = E->V.F.Func;
378
379             /* A function may use the A or A/X registers if it is a fastcall
380              * function. If it is not a fastcall function but a variadic one,
381              * it will use the Y register (the parameter size is passed here).
382              * In all other cases, no registers are used. However, we assume
383              * that any function will destroy all registers.
384              */
385             if (IsQualFastcall (E->Type) && D->ParamCount > 0) {
386                 /* Will use registers depending on the last param */
387                 unsigned LastParamSize = CheckedSizeOf (D->LastParam->Type);
388                 if (LastParamSize == 1) {
389                     *Use = REG_A;
390                 } else if (LastParamSize == 2) {
391                     *Use = REG_AX;
392                 } else {
393                     *Use = REG_EAX;
394                 }
395             } else if ((D->Flags & FD_VARIADIC) != 0) {
396                 *Use = REG_Y;
397             } else {
398                 /* Will not use any registers */
399                 *Use = REG_NONE;
400             }
401
402             /* Will destroy all registers */
403             *Chg = REG_ALL;
404
405             /* Done */
406             return;
407         }
408
409     } else if (IsDigit (Name[0]) || Name[0] == '$') {
410
411         /* A call to a numeric address. Assume that anything gets used and
412          * destroyed. This is not a real problem, since numeric addresses
413          * are used mostly in inline assembly anyway.
414          */
415         *Use = REG_ALL;
416         *Chg = REG_ALL;
417         return;
418
419     } else {
420
421         /* Search for the function in the list of builtin functions */
422         const FuncInfo* Info = bsearch (Name, FuncInfoTable, FuncInfoCount,
423                                         sizeof(FuncInfo), CompareFuncInfo);
424
425         /* Do we know the function? */
426         if (Info) {
427             /* Use the information we have */
428             *Use = Info->Use;
429             *Chg = Info->Chg;
430         } else {
431             /* It's an internal function we have no information for. If in
432              * debug mode, output an additional warning, so we have a chance
433              * to fix it. Otherwise assume that the internal function will
434              * use and change all registers.
435              */
436             if (Debug) {
437                 fprintf (stderr, "No info about internal function `%s'\n", Name);
438             }
439             *Use = REG_ALL;
440             *Chg = REG_ALL;
441         }
442         return;
443     }
444
445     /* Function not found - assume that the primary register is input, and all
446      * registers are changed
447      */
448     *Use = REG_EAXY;
449     *Chg = REG_ALL;
450 }
451
452
453
454 static int CompareZPInfo (const void* Name, const void* Info)
455 /* Compare function for bsearch */
456 {
457     /* Cast the pointers to the correct data type */
458     const char* N   = (const char*) Name;
459     const ZPInfo* E = (const ZPInfo*) Info;
460
461     /* Do the compare. Be careful because of the length (Info may contain
462      * more than just the zeropage name).
463      */
464     if (E->Len == 0) {
465         /* Do a full compare */
466         return strcmp (N, E->Name);
467     } else {
468         /* Only compare the first part */
469         int Res = strncmp (N, E->Name, E->Len);
470         if (Res == 0 && (N[E->Len] != '\0' && N[E->Len] != '+')) {
471             /* Name is actually longer than Info->Name */
472             Res = -1;
473         }
474         return Res;
475     }
476 }
477
478
479
480 const ZPInfo* GetZPInfo (const char* Name)
481 /* If the given name is a zero page symbol, return a pointer to the info
482  * struct for this symbol, otherwise return NULL.
483  */
484 {
485     /* Search for the zp location in the list */
486     return bsearch (Name, ZPInfoTable, ZPInfoCount,
487                     sizeof(ZPInfo), CompareZPInfo);
488 }
489
490
491
492 static unsigned GetRegInfo2 (CodeSeg* S,
493                              CodeEntry* E,
494                              int Index,
495                              Collection* Visited,
496                              unsigned Used,
497                              unsigned Unused,
498                              unsigned Wanted)
499 /* Recursively called subfunction for GetRegInfo. */
500 {
501     /* Follow the instruction flow recording register usage. */
502     while (1) {
503
504         unsigned R;
505
506         /* Check if we have already visited the current code entry. If so,
507          * bail out.
508          */
509         if (CE_HasMark (E)) {
510             break;
511         }
512
513         /* Mark this entry as already visited */
514         CE_SetMark (E);
515         CollAppend (Visited, E);
516
517         /* Evaluate the used registers */
518         R = E->Use;
519         if (E->OPC == OP65_RTS ||
520             ((E->Info & OF_UBRA) != 0 && E->JumpTo == 0)) {
521             /* This instruction will leave the function */
522             R |= S->ExitRegs;
523         }
524         if (R != REG_NONE) {
525             /* We are not interested in the use of any register that has been
526              * used before.
527              */
528             R &= ~Unused;
529             /* Remember the remaining registers */
530             Used |= R;
531         }
532
533         /* Evaluate the changed registers */
534         if ((R = E->Chg) != REG_NONE) {
535             /* We are not interested in the use of any register that has been
536              * used before.
537              */
538             R &= ~Used;
539             /* Remember the remaining registers */
540             Unused |= R;
541         }
542
543         /* If we know about all registers now, bail out */
544         if (((Used | Unused) & Wanted) == Wanted) {
545             break;
546         }
547
548         /* If the instruction is an RTS or RTI, we're done */
549         if ((E->Info & OF_RET) != 0) {
550             break;
551         }
552
553         /* If we have an unconditional branch, follow this branch if possible,
554          * otherwise we're done.
555          */
556         if ((E->Info & OF_UBRA) != 0) {
557
558             /* Does this jump have a valid target? */
559             if (E->JumpTo) {
560
561                 /* Unconditional jump */
562                 E     = E->JumpTo->Owner;
563                 Index = -1;             /* Invalidate */
564
565             } else {
566                 /* Jump outside means we're done */
567                 break;
568             }
569
570         /* In case of conditional branches, follow the branch if possible and
571          * follow the normal flow (branch not taken) afterwards. If we cannot
572          * follow the branch, we're done.
573          */
574         } else if ((E->Info & OF_CBRA) != 0) {
575
576             /* Recursively determine register usage at the branch target */
577             unsigned U1;
578             unsigned U2;
579
580             if (E->JumpTo) {
581
582                 /* Jump to internal label */
583                 U1 = GetRegInfo2 (S, E->JumpTo->Owner, -1, Visited, Used, Unused, Wanted);
584
585             } else {
586
587                 /* Jump to external label. This will effectively exit the
588                  * function, so we use the exitregs information here.
589                  */
590                 U1 = S->ExitRegs;
591
592             }
593
594             /* Get the next entry */
595             if (Index < 0) {
596                 Index = CS_GetEntryIndex (S, E);
597             }
598             if ((E = CS_GetEntry (S, ++Index)) == 0) {
599                 Internal ("GetRegInfo2: No next entry!");
600             }
601
602             /* Follow flow if branch not taken */
603             U2 = GetRegInfo2 (S, E, Index, Visited, Used, Unused, Wanted);
604
605             /* Registers are used if they're use in any of the branches */
606             return U1 | U2;
607
608         } else {
609
610             /* Just go to the next instruction */
611             if (Index < 0) {
612                 Index = CS_GetEntryIndex (S, E);
613             }
614             E = CS_GetEntry (S, ++Index);
615             if (E == 0) {
616                 /* No next entry */
617                 Internal ("GetRegInfo2: No next entry!");
618             }
619
620         }
621
622     }
623
624     /* Return to the caller the complement of all unused registers */
625     return Used;
626 }
627
628
629
630 static unsigned GetRegInfo1 (CodeSeg* S,
631                              CodeEntry* E,
632                              int Index,
633                              Collection* Visited,
634                              unsigned Used,
635                              unsigned Unused,
636                              unsigned Wanted)
637 /* Recursively called subfunction for GetRegInfo. */
638 {
639     /* Remember the current count of the line collection */
640     unsigned Count = CollCount (Visited);
641
642     /* Call the worker routine */
643     unsigned R = GetRegInfo2 (S, E, Index, Visited, Used, Unused, Wanted);
644
645     /* Restore the old count, unmarking all new entries */
646     unsigned NewCount = CollCount (Visited);
647     while (NewCount-- > Count) {
648         CodeEntry* E = CollAt (Visited, NewCount);
649         CE_ResetMark (E);
650         CollDelete (Visited, NewCount);
651     }
652
653     /* Return the registers used */
654     return R;
655 }
656
657
658
659 unsigned GetRegInfo (struct CodeSeg* S, unsigned Index, unsigned Wanted)
660 /* Determine register usage information for the instructions starting at the
661  * given index.
662  */
663 {
664     CodeEntry*      E;
665     Collection      Visited;    /* Visited entries */
666     unsigned        R;
667
668     /* Get the code entry for the given index */
669     if (Index >= CS_GetEntryCount (S)) {
670         /* There is no such code entry */
671         return REG_NONE;
672     }
673     E = CS_GetEntry (S, Index);
674
675     /* Initialize the data structure used to collection information */
676     InitCollection (&Visited);
677
678     /* Call the recursive subfunction */
679     R = GetRegInfo1 (S, E, Index, &Visited, REG_NONE, REG_NONE, Wanted);
680
681     /* Delete the line collection */
682     DoneCollection (&Visited);
683
684     /* Return the registers used */
685     return R;
686 }
687
688
689
690 int RegAUsed (struct CodeSeg* S, unsigned Index)
691 /* Check if the value in A is used. */
692 {
693     return (GetRegInfo (S, Index, REG_A) & REG_A) != 0;
694 }
695
696
697
698 int RegXUsed (struct CodeSeg* S, unsigned Index)
699 /* Check if the value in X is used. */
700 {
701     return (GetRegInfo (S, Index, REG_X) & REG_X) != 0;
702 }
703
704
705
706 int RegYUsed (struct CodeSeg* S, unsigned Index)
707 /* Check if the value in Y is used. */
708 {
709     return (GetRegInfo (S, Index, REG_Y) & REG_Y) != 0;
710 }
711
712
713
714 int RegAXUsed (struct CodeSeg* S, unsigned Index)
715 /* Check if the value in A or(!) the value in X are used. */
716 {
717     return (GetRegInfo (S, Index, REG_AX) & REG_AX) != 0;
718 }
719
720
721
722 int RegEAXUsed (struct CodeSeg* S, unsigned Index)
723 /* Check if any of the four bytes in EAX are used. */
724 {
725     return (GetRegInfo (S, Index, REG_EAX) & REG_EAX) != 0;
726 }
727
728
729
730 unsigned GetKnownReg (unsigned Use, const RegContents* RC)
731 /* Return the register or zero page location from the set in Use, thats
732  * contents are known. If Use does not contain any register, or if the
733  * register in question does not have a known value, return REG_NONE.
734  */
735 {
736     if ((Use & REG_A) != 0) {
737         return (RC == 0 || RC->RegA >= 0)? REG_A : REG_NONE;
738     } else if ((Use & REG_X) != 0) {
739         return (RC == 0 || RC->RegX >= 0)? REG_X : REG_NONE;
740     } else if ((Use & REG_Y) != 0) {
741         return (RC == 0 || RC->RegY >= 0)? REG_Y : REG_NONE;
742     } else if ((Use & REG_TMP1) != 0) {
743         return (RC == 0 || RC->Tmp1 >= 0)? REG_TMP1 : REG_NONE;
744     } else if ((Use & REG_PTR1_LO) != 0) {
745         return (RC == 0 || RC->Ptr1Lo >= 0)? REG_PTR1_LO : REG_NONE;
746     } else if ((Use & REG_PTR1_HI) != 0) {
747         return (RC == 0 || RC->Ptr1Hi >= 0)? REG_PTR1_HI : REG_NONE;
748     } else if ((Use & REG_SREG_LO) != 0) {
749         return (RC == 0 || RC->SRegLo >= 0)? REG_SREG_LO : REG_NONE;
750     } else if ((Use & REG_SREG_HI) != 0) {
751         return (RC == 0 || RC->SRegHi >= 0)? REG_SREG_HI : REG_NONE;
752     } else {
753         return REG_NONE;
754     }
755 }
756
757
758
759 static cmp_t FindCmpCond (const char* Code, unsigned CodeLen)
760 /* Search for a compare condition by the given code using the given length */
761 {
762     unsigned I;
763
764     /* Linear search */
765     for (I = 0; I < sizeof (CmpSuffixTab) / sizeof (CmpSuffixTab [0]); ++I) {
766         if (strncmp (Code, CmpSuffixTab [I], CodeLen) == 0) {
767             /* Found */
768             return I;
769         }
770     }
771
772     /* Not found */
773     return CMP_INV;
774 }
775
776
777
778 cmp_t FindBoolCmpCond (const char* Name)
779 /* Check if the given string is the name of one of the boolean transformer
780  * subroutine, and if so, return the condition that is evaluated by this
781  * routine. Return CMP_INV if the condition is not recognised.
782  */
783 {
784     /* Check for the correct subroutine name */
785     if (strncmp (Name, "bool", 4) == 0) {
786         /* Name is ok, search for the code in the table */
787         return FindCmpCond (Name+4, strlen(Name)-4);
788     } else {
789         /* Not found */
790         return CMP_INV;
791     }
792 }
793
794
795
796 cmp_t FindTosCmpCond (const char* Name)
797 /* Check if this is a call to one of the TOS compare functions (tosgtax).
798  * Return the condition code or CMP_INV on failure.
799  */
800 {
801     unsigned Len = strlen (Name);
802
803     /* Check for the correct subroutine name */
804     if (strncmp (Name, "tos", 3) == 0 && strcmp (Name+Len-2, "ax") == 0) {
805         /* Name is ok, search for the code in the table */
806         return FindCmpCond (Name+3, Len-3-2);
807     } else {
808         /* Not found */
809         return CMP_INV;
810     }
811 }
812
813
814