1 /*****************************************************************************/
5 /* Additional information about 6502 code */
9 /* (C) 2001-2012, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
42 #include "debugflag.h"
55 /*****************************************************************************/
57 /*****************************************************************************/
61 /* Table with the compare suffixes */
62 static const char CmpSuffixTab [][4] = {
63 "eq", "ne", "gt", "ge", "lt", "le", "ugt", "uge", "ult", "ule"
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.
69 typedef struct FuncInfo FuncInfo;
71 const char* Name; /* Function name */
72 unsigned short Use; /* Register usage */
73 unsigned short Chg; /* Changed/destroyed registers */
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
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 },
337 #define FuncInfoCount (sizeof(FuncInfoTable) / sizeof(FuncInfoTable[0]))
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 },
359 #define ZPInfoCount (sizeof(ZPInfoTable) / sizeof(ZPInfoTable[0]))
363 /*****************************************************************************/
365 /*****************************************************************************/
369 static int CompareFuncInfo (const void* Key, const void* Info)
370 /* Compare function for bsearch */
372 return strcmp (Key, ((const FuncInfo*) Info)->Name);
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.
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.
388 if (Name[0] == '_') {
390 /* Search in the symbol table, skip the leading underscore */
391 SymEntry* E = FindGlobalSym (Name+1);
393 /* Did we find it in the top level table? */
394 if (E && IsTypeFunc (E->Type)) {
396 FuncDesc* D = E->V.F.Func;
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.
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) {
409 } else if (LastParamSize == 2) {
414 } else if ((D->Flags & FD_VARIADIC) != 0) {
417 /* Will not use any registers */
421 /* Will destroy all registers */
428 } else if (IsDigit (Name[0]) || Name[0] == '$') {
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.
440 /* Search for the function in the list of builtin functions */
441 const FuncInfo* Info = bsearch (Name, FuncInfoTable, FuncInfoCount,
442 sizeof(FuncInfo), CompareFuncInfo);
444 /* Do we know the function? */
446 /* Use the information we have */
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.
456 fprintf (stderr, "No info about internal function `%s'\n", Name);
464 /* Function not found - assume that the primary register is input, and all
465 * registers are changed
473 static int CompareZPInfo (const void* Name, const void* Info)
474 /* Compare function for bsearch */
476 /* Cast the pointers to the correct data type */
477 const char* N = (const char*) Name;
478 const ZPInfo* E = (const ZPInfo*) Info;
480 /* Do the compare. Be careful because of the length (Info may contain
481 * more than just the zeropage name).
484 /* Do a full compare */
485 return strcmp (N, E->Name);
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 */
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.
504 /* Search for the zp location in the list */
505 return bsearch (Name, ZPInfoTable, ZPInfoCount,
506 sizeof(ZPInfo), CompareZPInfo);
511 static unsigned GetRegInfo2 (CodeSeg* S,
518 /* Recursively called subfunction for GetRegInfo. */
520 /* Follow the instruction flow recording register usage. */
525 /* Check if we have already visited the current code entry. If so,
528 if (CE_HasMark (E)) {
532 /* Mark this entry as already visited */
534 CollAppend (Visited, E);
536 /* Evaluate the used registers */
538 if (E->OPC == OP65_RTS ||
539 ((E->Info & OF_UBRA) != 0 && E->JumpTo == 0)) {
540 /* This instruction will leave the function */
544 /* We are not interested in the use of any register that has been
548 /* Remember the remaining registers */
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
558 /* Remember the remaining registers */
562 /* If we know about all registers now, bail out */
563 if (((Used | Unused) & Wanted) == Wanted) {
567 /* If the instruction is an RTS or RTI, we're done */
568 if ((E->Info & OF_RET) != 0) {
572 /* If we have an unconditional branch, follow this branch if possible,
573 * otherwise we're done.
575 if ((E->Info & OF_UBRA) != 0) {
577 /* Does this jump have a valid target? */
580 /* Unconditional jump */
581 E = E->JumpTo->Owner;
582 Index = -1; /* Invalidate */
585 /* Jump outside means we're done */
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.
593 } else if ((E->Info & OF_CBRA) != 0) {
595 /* Recursively determine register usage at the branch target */
601 /* Jump to internal label */
602 U1 = GetRegInfo2 (S, E->JumpTo->Owner, -1, Visited, Used, Unused, Wanted);
606 /* Jump to external label. This will effectively exit the
607 * function, so we use the exitregs information here.
613 /* Get the next entry */
615 Index = CS_GetEntryIndex (S, E);
617 if ((E = CS_GetEntry (S, ++Index)) == 0) {
618 Internal ("GetRegInfo2: No next entry!");
621 /* Follow flow if branch not taken */
622 U2 = GetRegInfo2 (S, E, Index, Visited, Used, Unused, Wanted);
624 /* Registers are used if they're use in any of the branches */
629 /* Just go to the next instruction */
631 Index = CS_GetEntryIndex (S, E);
633 E = CS_GetEntry (S, ++Index);
636 Internal ("GetRegInfo2: No next entry!");
643 /* Return to the caller the complement of all unused registers */
649 static unsigned GetRegInfo1 (CodeSeg* S,
656 /* Recursively called subfunction for GetRegInfo. */
658 /* Remember the current count of the line collection */
659 unsigned Count = CollCount (Visited);
661 /* Call the worker routine */
662 unsigned R = GetRegInfo2 (S, E, Index, Visited, Used, Unused, Wanted);
664 /* Restore the old count, unmarking all new entries */
665 unsigned NewCount = CollCount (Visited);
666 while (NewCount-- > Count) {
667 CodeEntry* E = CollAt (Visited, NewCount);
669 CollDelete (Visited, NewCount);
672 /* Return the registers used */
678 unsigned GetRegInfo (struct CodeSeg* S, unsigned Index, unsigned Wanted)
679 /* Determine register usage information for the instructions starting at the
684 Collection Visited; /* Visited entries */
687 /* Get the code entry for the given index */
688 if (Index >= CS_GetEntryCount (S)) {
689 /* There is no such code entry */
692 E = CS_GetEntry (S, Index);
694 /* Initialize the data structure used to collection information */
695 InitCollection (&Visited);
697 /* Call the recursive subfunction */
698 R = GetRegInfo1 (S, E, Index, &Visited, REG_NONE, REG_NONE, Wanted);
700 /* Delete the line collection */
701 DoneCollection (&Visited);
703 /* Return the registers used */
709 int RegAUsed (struct CodeSeg* S, unsigned Index)
710 /* Check if the value in A is used. */
712 return (GetRegInfo (S, Index, REG_A) & REG_A) != 0;
717 int RegXUsed (struct CodeSeg* S, unsigned Index)
718 /* Check if the value in X is used. */
720 return (GetRegInfo (S, Index, REG_X) & REG_X) != 0;
725 int RegYUsed (struct CodeSeg* S, unsigned Index)
726 /* Check if the value in Y is used. */
728 return (GetRegInfo (S, Index, REG_Y) & REG_Y) != 0;
733 int RegAXUsed (struct CodeSeg* S, unsigned Index)
734 /* Check if the value in A or(!) the value in X are used. */
736 return (GetRegInfo (S, Index, REG_AX) & REG_AX) != 0;
741 int RegEAXUsed (struct CodeSeg* S, unsigned Index)
742 /* Check if any of the four bytes in EAX are used. */
744 return (GetRegInfo (S, Index, REG_EAX) & REG_EAX) != 0;
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.
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;
778 static cmp_t FindCmpCond (const char* Code, unsigned CodeLen)
779 /* Search for a compare condition by the given code using the given length */
784 for (I = 0; I < sizeof (CmpSuffixTab) / sizeof (CmpSuffixTab [0]); ++I) {
785 if (strncmp (Code, CmpSuffixTab [I], CodeLen) == 0) {
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.
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);
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.
820 unsigned Len = strlen (Name);
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);