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