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