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