1 /*****************************************************************************/
5 /* Additional information about 6502 code */
9 /* (C) 2001-2006, Ullrich von Bassewitz */
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 static const FuncInfo FuncInfoTable[] = {
77 { "addeq0sp", REG_AX, REG_AXY },
78 { "addeqysp", REG_AXY, REG_AXY },
79 { "addysp", REG_Y, REG_NONE },
80 { "aslax1", REG_AX, REG_AX | REG_TMP1 },
81 { "aslax2", REG_AX, REG_AX | REG_TMP1 },
82 { "aslax3", REG_AX, REG_AX | REG_TMP1 },
83 { "aslax4", REG_AX, REG_AX | REG_TMP1 },
84 { "asrax1", REG_AX, REG_AX | REG_TMP1 },
85 { "asrax2", REG_AX, REG_AX | REG_TMP1 },
86 { "asrax3", REG_AX, REG_AX | REG_TMP1 },
87 { "asrax4", REG_AX, REG_AX | REG_TMP1 },
88 { "bnega", REG_A, REG_AX },
89 { "bnegax", REG_AX, REG_AX },
90 { "bnegeax", REG_EAX, REG_EAX },
91 { "booleq", REG_NONE, REG_AX },
92 { "boolge", REG_NONE, REG_AX },
93 { "boolgt", REG_NONE, REG_AX },
94 { "boolle", REG_NONE, REG_AX },
95 { "boollt", REG_NONE, REG_AX },
96 { "boolne", REG_NONE, REG_AX },
97 { "booluge", REG_NONE, REG_AX },
98 { "boolugt", REG_NONE, REG_AX },
99 { "boolule", REG_NONE, REG_AX },
100 { "boolult", REG_NONE, REG_AX },
101 { "callax", REG_AX, REG_ALL },
102 { "complax", REG_AX, REG_AX },
103 { "decax1", REG_AX, REG_AX },
104 { "decax2", REG_AX, REG_AX },
105 { "decax3", REG_AX, REG_AX },
106 { "decax4", REG_AX, REG_AX },
107 { "decax5", REG_AX, REG_AX },
108 { "decax6", REG_AX, REG_AX },
109 { "decax7", REG_AX, REG_AX },
110 { "decax8", REG_AX, REG_AX },
111 { "decaxy", REG_AXY, REG_AX | REG_TMP1 },
112 { "deceaxy", REG_EAXY, REG_EAX },
113 { "decsp1", REG_NONE, REG_Y },
114 { "decsp2", REG_NONE, REG_A },
115 { "decsp3", REG_NONE, REG_A },
116 { "decsp4", REG_NONE, REG_A },
117 { "decsp5", REG_NONE, REG_A },
118 { "decsp6", REG_NONE, REG_A },
119 { "decsp7", REG_NONE, REG_A },
120 { "decsp8", REG_NONE, REG_A },
121 { "incax1", REG_AX, REG_AX },
122 { "incax2", REG_AX, REG_AX },
123 { "incax3", REG_AX, REG_AXY | REG_TMP1 },
124 { "incax4", REG_AX, REG_AXY | REG_TMP1 },
125 { "incax5", REG_AX, REG_AXY | REG_TMP1 },
126 { "incax6", REG_AX, REG_AXY | REG_TMP1 },
127 { "incax7", REG_AX, REG_AXY | REG_TMP1 },
128 { "incax8", REG_AX, REG_AXY | REG_TMP1 },
129 { "incaxy", REG_AXY, REG_AXY | REG_TMP1 },
130 { "incsp1", REG_NONE, REG_NONE },
131 { "incsp2", REG_NONE, REG_Y },
132 { "incsp3", REG_NONE, REG_Y },
133 { "incsp4", REG_NONE, REG_Y },
134 { "incsp5", REG_NONE, REG_Y },
135 { "incsp6", REG_NONE, REG_Y },
136 { "incsp7", REG_NONE, REG_Y },
137 { "incsp8", REG_NONE, REG_Y },
138 { "laddeq", REG_EAXY|REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
139 { "laddeq0sp", REG_EAX, REG_EAXY },
140 { "laddeq1", REG_Y | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
141 { "laddeqa", REG_AY | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
142 { "laddeqysp", REG_EAXY, REG_EAXY },
143 { "ldaidx", REG_AXY, REG_AX | REG_PTR1 },
144 { "ldauidx", REG_AXY, REG_AX | REG_PTR1 },
145 { "ldax0sp", REG_NONE, REG_AXY },
146 { "ldaxi", REG_AX, REG_AXY | REG_PTR1 },
147 { "ldaxidx", REG_AXY, REG_AXY | REG_PTR1 },
148 { "ldaxysp", REG_Y, REG_AXY },
149 { "ldeax0sp", REG_NONE, REG_EAXY },
150 { "ldeaxi", REG_AX, REG_EAXY | REG_PTR1 },
151 { "ldeaxidx", REG_AXY, REG_EAXY | REG_PTR1 },
152 { "ldeaxysp", REG_Y, REG_EAXY },
153 { "leaasp", REG_A, REG_AX },
154 { "lsubeq", REG_EAXY|REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
155 { "lsubeq0sp", REG_EAX, REG_EAXY },
156 { "lsubeq1", REG_Y | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
157 { "lsubeqa", REG_AY | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
158 { "lsubeqysp", REG_EAXY, REG_EAXY },
159 { "mulax10", REG_AX, REG_AX | REG_PTR1 },
160 { "mulax3", REG_AX, REG_AX | REG_PTR1 },
161 { "mulax5", REG_AX, REG_AX | REG_PTR1 },
162 { "mulax6", REG_AX, REG_AX | REG_PTR1 },
163 { "mulax7", REG_AX, REG_AX | REG_PTR1 },
164 { "mulax9", REG_AX, REG_AX | REG_PTR1 },
165 { "negax", REG_AX, REG_AX },
166 { "push0", REG_NONE, REG_AXY },
167 { "push1", REG_NONE, REG_AXY },
168 { "push2", REG_NONE, REG_AXY },
169 { "push3", REG_NONE, REG_AXY },
170 { "push4", REG_NONE, REG_AXY },
171 { "push5", REG_NONE, REG_AXY },
172 { "push6", REG_NONE, REG_AXY },
173 { "push7", REG_NONE, REG_AXY },
174 { "pusha", REG_A, REG_Y },
175 { "pusha0", REG_A, REG_XY },
176 { "pusha0sp", REG_NONE, REG_AY },
177 { "pushaFF", REG_A, REG_Y },
178 { "pushax", REG_AX, REG_Y },
179 { "pushaysp", REG_Y, REG_AY },
180 { "pushc0", REG_NONE, REG_A | REG_Y },
181 { "pushc1", REG_NONE, REG_A | REG_Y },
182 { "pushc2", REG_NONE, REG_A | REG_Y },
183 { "pusheax", REG_EAX, REG_Y },
184 { "pushw", REG_AX, REG_AXY | REG_PTR1 },
185 { "pushw0sp", REG_NONE, REG_AXY },
186 { "pushwidx", REG_AXY, REG_AXY | REG_PTR1 },
187 { "pushwysp", REG_Y, REG_AXY },
188 { "regswap", REG_AXY, REG_AXY | REG_TMP1 },
189 { "regswap1", REG_XY, REG_A },
190 { "regswap2", REG_XY, REG_A | REG_Y },
191 { "return0", REG_NONE, REG_AX },
192 { "return1", REG_NONE, REG_AX },
193 { "shlax1", REG_AX, REG_AX | REG_TMP1 },
194 { "shlax2", REG_AX, REG_AX | REG_TMP1 },
195 { "shlax3", REG_AX, REG_AX | REG_TMP1 },
196 { "shlax4", REG_AX, REG_AX | REG_TMP1 },
197 { "shrax1", REG_AX, REG_AX | REG_TMP1 },
198 { "shrax2", REG_AX, REG_AX | REG_TMP1 },
199 { "shrax3", REG_AX, REG_AX | REG_TMP1 },
200 { "shrax4", REG_AX, REG_AX | REG_TMP1 },
201 { "shreax1", REG_EAX, REG_AX | REG_TMP1 },
202 { "shreax2", REG_EAX, REG_AX | REG_TMP1 },
203 { "shreax3", REG_EAX, REG_AX | REG_TMP1 },
204 { "shreax4", REG_EAX, REG_AX | REG_TMP1 },
205 { "staspidx", REG_A | REG_Y, REG_Y | REG_TMP1 | REG_PTR1 },
206 { "stax0sp", REG_AX, REG_Y },
207 { "staxspidx", REG_AXY, REG_TMP1 | REG_PTR1 },
208 { "staxysp", REG_AXY, REG_Y },
209 { "steax0sp", REG_EAX, REG_Y },
210 { "steaxysp", REG_EAXY, REG_Y },
211 { "subeq0sp", REG_AX, REG_AXY },
212 { "subeqysp", REG_AXY, REG_AXY },
213 { "tosadda0", REG_A, REG_AXY },
214 { "tosaddax", REG_AX, REG_AXY },
215 { "tosanda0", REG_A, REG_AXY },
216 { "tosandax", REG_AX, REG_AXY },
217 { "tosaslax", REG_A, REG_AXY | REG_TMP1 },
218 { "tosasleax", REG_A, REG_EAXY | REG_TMP1 },
219 { "tosasrax", REG_A, REG_AXY | REG_TMP1 },
220 { "tosasreax", REG_A, REG_EAXY | REG_TMP1 },
221 { "tosdiva0", REG_AY, REG_ALL },
222 { "tosdivax", REG_AXY, REG_ALL },
223 { "tosdiveax", REG_EAXY, REG_ALL },
224 { "toseq00", REG_NONE, REG_AXY | REG_SREG },
225 { "toseqa0", REG_A, REG_AXY | REG_SREG },
226 { "toseqax", REG_AX, REG_AXY | REG_SREG },
227 { "toseqeax", REG_EAX, REG_AXY | REG_PTR1 },
228 { "tosge00", REG_NONE, REG_AXY | REG_SREG },
229 { "tosgea0", REG_A, REG_AXY | REG_SREG },
230 { "tosgeax", REG_AX, REG_AXY | REG_SREG },
231 { "tosgeeax", REG_EAX, REG_AXY | REG_PTR1 },
232 { "tosgt00", REG_NONE, REG_AXY | REG_SREG },
233 { "tosgta0", REG_A, REG_AXY | REG_SREG },
234 { "tosgtax", REG_AX, REG_AXY | REG_SREG },
235 { "tosgteax", REG_EAX, REG_AXY | REG_PTR1 },
236 { "tosicmp", REG_AX, REG_AXY | REG_SREG },
237 { "toslcmp", REG_EAX, REG_A | REG_Y | REG_PTR1 },
238 { "tosle00", REG_NONE, REG_AXY | REG_SREG },
239 { "toslea0", REG_A, REG_AXY | REG_SREG },
240 { "tosleax", REG_AX, REG_AXY | REG_SREG },
241 { "tosleeax", REG_EAX, REG_AXY | REG_PTR1 },
242 { "toslt00", REG_NONE, REG_AXY | REG_SREG },
243 { "toslta0", REG_A, REG_AXY | REG_SREG },
244 { "tosltax", REG_AX, REG_AXY | REG_SREG },
245 { "toslteax", REG_EAX, REG_AXY | REG_PTR1 },
246 { "tosmula0", REG_AX, REG_ALL },
247 { "tosmulax", REG_AX, REG_ALL },
248 { "tosmuleax", REG_EAX, REG_ALL },
249 { "tosne00", REG_NONE, REG_AXY | REG_SREG },
250 { "tosnea0", REG_A, REG_AXY | REG_SREG },
251 { "tosneax", REG_AX, REG_AXY | REG_SREG },
252 { "tosneeax", REG_EAX, REG_AXY | REG_PTR1 },
253 { "tosora0", REG_A, REG_AXY | REG_TMP1 },
254 { "tosorax", REG_AX, REG_AXY | REG_TMP1 },
255 { "tosshlax", REG_A, REG_AXY | REG_TMP1 },
256 { "tosshleax", REG_A, REG_EAXY | REG_TMP1 },
257 { "tosshrax", REG_A, REG_AXY | REG_TMP1 },
258 { "tosshreax", REG_A, REG_EAXY | REG_TMP1 },
259 { "tossuba0", REG_A, REG_AXY },
260 { "tossubax", REG_AX, REG_AXY },
261 { "tossubeax", REG_EAX, REG_EAXY },
262 { "tosuge00", REG_NONE, REG_AXY | REG_SREG },
263 { "tosugea0", REG_A, REG_AXY | REG_SREG },
264 { "tosugeax", REG_AX, REG_AXY | REG_SREG },
265 { "tosugeeax", REG_EAX, REG_AXY | REG_PTR1 },
266 { "tosugt00", REG_NONE, REG_AXY | REG_SREG },
267 { "tosugta0", REG_A, REG_AXY | REG_SREG },
268 { "tosugtax", REG_AX, REG_AXY | REG_SREG },
269 { "tosugteax", REG_EAX, REG_AXY | REG_PTR1 },
270 { "tosule00", REG_NONE, REG_AXY | REG_SREG },
271 { "tosulea0", REG_A, REG_AXY | REG_SREG },
272 { "tosuleax", REG_AX, REG_AXY | REG_SREG },
273 { "tosuleeax", REG_EAX, REG_AXY | REG_PTR1 },
274 { "tosult00", REG_NONE, REG_AXY | REG_SREG },
275 { "tosulta0", REG_A, REG_AXY | REG_SREG },
276 { "tosultax", REG_AX, REG_AXY | REG_SREG },
277 { "tosulteax", REG_EAX, REG_AXY | REG_PTR1 },
278 { "tosumula0", REG_AX, REG_ALL },
279 { "tosumulax", REG_AX, REG_ALL },
280 { "tosumuleax", REG_EAX, REG_ALL },
281 { "tsteax", REG_EAX, REG_Y },
282 { "utsteax", REG_EAX, REG_Y },
284 #define FuncInfoCount (sizeof(FuncInfoTable) / sizeof(FuncInfoTable[0]))
286 /* Table with names of zero page locations used by the compiler */
287 static const ZPInfo ZPInfoTable[] = {
288 { 0, "ptr1", REG_PTR1_LO, REG_PTR1 },
289 { 0, "ptr1+1", REG_PTR1_HI, REG_PTR1 },
290 { 0, "ptr2", REG_PTR2_LO, REG_PTR2 },
291 { 0, "ptr2+1", REG_PTR2_HI, REG_PTR2 },
292 { 4, "ptr3", REG_NONE, REG_NONE },
293 { 4, "ptr4", REG_NONE, REG_NONE },
294 { 7, "regbank", REG_NONE, REG_NONE },
295 { 0, "regsave", REG_SAVE_LO, REG_SAVE },
296 { 0, "regsave+1", REG_SAVE_HI, REG_SAVE },
297 { 0, "sp", REG_SP_LO, REG_SP },
298 { 0, "sp+1", REG_SP_HI, REG_SP },
299 { 0, "sreg", REG_SREG_LO, REG_SREG },
300 { 0, "sreg+1", REG_SREG_HI, REG_SREG },
301 { 0, "tmp1", REG_TMP1, REG_TMP1 },
302 { 0, "tmp2", REG_NONE, REG_NONE },
303 { 0, "tmp3", REG_NONE, REG_NONE },
304 { 0, "tmp4", REG_NONE, REG_NONE },
306 #define ZPInfoCount (sizeof(ZPInfoTable) / sizeof(ZPInfoTable[0]))
310 /*****************************************************************************/
312 /*****************************************************************************/
316 static int CompareFuncInfo (const void* Key, const void* Info)
317 /* Compare function for bsearch */
319 return strcmp (Key, ((const FuncInfo*) Info)->Name);
324 void GetFuncInfo (const char* Name, unsigned short* Use, unsigned short* Chg)
325 /* For the given function, lookup register information and store it into
326 * the given variables. If the function is unknown, assume it will use and
327 * load all registers.
330 /* If the function name starts with an underline, it is an external
331 * function. Search for it in the symbol table. If the function does
332 * not start with an underline, it may be a runtime support function.
333 * Search for it in the list of builtin functions.
335 if (Name[0] == '_') {
337 /* Search in the symbol table, skip the leading underscore */
338 SymEntry* E = FindGlobalSym (Name+1);
340 /* Did we find it in the top level table? */
341 if (E && IsTypeFunc (E->Type)) {
343 /* A function may use the A or A/X registers if it is a fastcall
344 * function. If it is not a fastcall function but a variadic one,
345 * it will use the Y register (the parameter size is passed here).
346 * In all other cases, no registers are used. However, we assume
347 * that any function will destroy all registers.
349 FuncDesc* D = E->V.F.Func;
350 if ((D->Flags & FD_FASTCALL) != 0 && D->ParamCount > 0) {
351 /* Will use registers depending on the last param */
352 unsigned LastParamSize = CheckedSizeOf (D->LastParam->Type);
353 if (LastParamSize == 1) {
355 } else if (LastParamSize == 2) {
360 } else if ((D->Flags & FD_VARIADIC) != 0) {
363 /* Will not use any registers */
367 /* Will destroy all registers */
374 } else if (IsDigit (Name[0]) || Name[0] == '$') {
376 /* A call to a numeric address. Assume that anything gets used and
377 * destroyed. This is not a real problem, since numeric addresses
378 * are used mostly in inline assembly anyway.
386 /* Search for the function in the list of builtin functions */
387 const FuncInfo* Info = bsearch (Name, FuncInfoTable, FuncInfoCount,
388 sizeof(FuncInfo), CompareFuncInfo);
390 /* Do we know the function? */
392 /* Use the information we have */
396 /* It's an internal function we have no information for. If in
397 * debug mode, output an additional warning, so we have a chance
398 * to fix it. Otherwise assume that the internal function will
399 * use and change all registers.
402 fprintf (stderr, "No info about internal function `%s'\n", Name);
410 /* Function not found - assume that the primary register is input, and all
411 * registers are changed
419 static int CompareZPInfo (const void* Name, const void* Info)
420 /* Compare function for bsearch */
422 /* Cast the pointers to the correct data type */
423 const char* N = (const char*) Name;
424 const ZPInfo* E = (const ZPInfo*) Info;
426 /* Do the compare. Be careful because of the length (Info may contain
427 * more than just the zeropage name).
430 /* Do a full compare */
431 return strcmp (N, E->Name);
433 /* Only compare the first part */
434 int Res = strncmp (N, E->Name, E->Len);
435 if (Res == 0 && (N[E->Len] != '\0' && N[E->Len] != '+')) {
436 /* Name is actually longer than Info->Name */
445 const ZPInfo* GetZPInfo (const char* Name)
446 /* If the given name is a zero page symbol, return a pointer to the info
447 * struct for this symbol, otherwise return NULL.
450 /* Search for the zp location in the list */
451 return bsearch (Name, ZPInfoTable, ZPInfoCount,
452 sizeof(ZPInfo), CompareZPInfo);
457 static unsigned GetRegInfo2 (CodeSeg* S,
464 /* Recursively called subfunction for GetRegInfo. */
466 /* Follow the instruction flow recording register usage. */
471 /* Check if we have already visited the current code entry. If so,
474 if (CE_HasMark (E)) {
478 /* Mark this entry as already visited */
480 CollAppend (Visited, E);
482 /* Evaluate the used registers */
484 if (E->OPC == OP65_RTS ||
485 ((E->Info & OF_UBRA) != 0 && E->JumpTo == 0)) {
486 /* This instruction will leave the function */
490 /* We are not interested in the use of any register that has been
494 /* Remember the remaining registers */
498 /* Evaluate the changed registers */
499 if ((R = E->Chg) != REG_NONE) {
500 /* We are not interested in the use of any register that has been
504 /* Remember the remaining registers */
508 /* If we know about all registers now, bail out */
509 if (((Used | Unused) & Wanted) == Wanted) {
513 /* If the instruction is an RTS or RTI, we're done */
514 if ((E->Info & OF_RET) != 0) {
518 /* If we have an unconditional branch, follow this branch if possible,
519 * otherwise we're done.
521 if ((E->Info & OF_UBRA) != 0) {
523 /* Does this jump have a valid target? */
526 /* Unconditional jump */
527 E = E->JumpTo->Owner;
528 Index = -1; /* Invalidate */
531 /* Jump outside means we're done */
535 /* In case of conditional branches, follow the branch if possible and
536 * follow the normal flow (branch not taken) afterwards. If we cannot
537 * follow the branch, we're done.
539 } else if ((E->Info & OF_CBRA) != 0) {
541 /* Recursively determine register usage at the branch target */
547 /* Jump to internal label */
548 U1 = GetRegInfo2 (S, E->JumpTo->Owner, -1, Visited, Used, Unused, Wanted);
552 /* Jump to external label. This will effectively exit the
553 * function, so we use the exitregs information here.
559 /* Get the next entry */
561 Index = CS_GetEntryIndex (S, E);
563 if ((E = CS_GetEntry (S, ++Index)) == 0) {
564 Internal ("GetRegInfo2: No next entry!");
567 /* Follow flow if branch not taken */
568 U2 = GetRegInfo2 (S, E, Index, Visited, Used, Unused, Wanted);
570 /* Registers are used if they're use in any of the branches */
575 /* Just go to the next instruction */
577 Index = CS_GetEntryIndex (S, E);
579 E = CS_GetEntry (S, ++Index);
582 Internal ("GetRegInfo2: No next entry!");
589 /* Return to the caller the complement of all unused registers */
595 static unsigned GetRegInfo1 (CodeSeg* S,
602 /* Recursively called subfunction for GetRegInfo. */
604 /* Remember the current count of the line collection */
605 unsigned Count = CollCount (Visited);
607 /* Call the worker routine */
608 unsigned R = GetRegInfo2 (S, E, Index, Visited, Used, Unused, Wanted);
610 /* Restore the old count, unmarking all new entries */
611 unsigned NewCount = CollCount (Visited);
612 while (NewCount-- > Count) {
613 CodeEntry* E = CollAt (Visited, NewCount);
615 CollDelete (Visited, NewCount);
618 /* Return the registers used */
624 unsigned GetRegInfo (struct CodeSeg* S, unsigned Index, unsigned Wanted)
625 /* Determine register usage information for the instructions starting at the
630 Collection Visited; /* Visited entries */
633 /* Get the code entry for the given index */
634 if (Index >= CS_GetEntryCount (S)) {
635 /* There is no such code entry */
638 E = CS_GetEntry (S, Index);
640 /* Initialize the data structure used to collection information */
641 InitCollection (&Visited);
643 /* Call the recursive subfunction */
644 R = GetRegInfo1 (S, E, Index, &Visited, REG_NONE, REG_NONE, Wanted);
646 /* Delete the line collection */
647 DoneCollection (&Visited);
649 /* Return the registers used */
655 int RegAUsed (struct CodeSeg* S, unsigned Index)
656 /* Check if the value in A is used. */
658 return (GetRegInfo (S, Index, REG_A) & REG_A) != 0;
663 int RegXUsed (struct CodeSeg* S, unsigned Index)
664 /* Check if the value in X is used. */
666 return (GetRegInfo (S, Index, REG_X) & REG_X) != 0;
671 int RegYUsed (struct CodeSeg* S, unsigned Index)
672 /* Check if the value in Y is used. */
674 return (GetRegInfo (S, Index, REG_Y) & REG_Y) != 0;
679 int RegAXUsed (struct CodeSeg* S, unsigned Index)
680 /* Check if the value in A or(!) the value in X are used. */
682 return (GetRegInfo (S, Index, REG_AX) & REG_AX) != 0;
687 int RegEAXUsed (struct CodeSeg* S, unsigned Index)
688 /* Check if any of the four bytes in EAX are used. */
690 return (GetRegInfo (S, Index, REG_EAX) & REG_EAX) != 0;
695 unsigned GetKnownReg (unsigned Use, const RegContents* RC)
696 /* Return the register or zero page location from the set in Use, thats
697 * contents are known. If Use does not contain any register, or if the
698 * register in question does not have a known value, return REG_NONE.
701 if ((Use & REG_A) != 0) {
702 return (RC == 0 || RC->RegA >= 0)? REG_A : REG_NONE;
703 } else if ((Use & REG_X) != 0) {
704 return (RC == 0 || RC->RegX >= 0)? REG_X : REG_NONE;
705 } else if ((Use & REG_Y) != 0) {
706 return (RC == 0 || RC->RegY >= 0)? REG_Y : REG_NONE;
707 } else if ((Use & REG_TMP1) != 0) {
708 return (RC == 0 || RC->Tmp1 >= 0)? REG_TMP1 : REG_NONE;
709 } else if ((Use & REG_PTR1_LO) != 0) {
710 return (RC == 0 || RC->Ptr1Lo >= 0)? REG_PTR1_LO : REG_NONE;
711 } else if ((Use & REG_PTR1_HI) != 0) {
712 return (RC == 0 || RC->Ptr1Hi >= 0)? REG_PTR1_HI : REG_NONE;
713 } else if ((Use & REG_SREG_LO) != 0) {
714 return (RC == 0 || RC->SRegLo >= 0)? REG_SREG_LO : REG_NONE;
715 } else if ((Use & REG_SREG_HI) != 0) {
716 return (RC == 0 || RC->SRegHi >= 0)? REG_SREG_HI : REG_NONE;
724 static cmp_t FindCmpCond (const char* Code, unsigned CodeLen)
725 /* Search for a compare condition by the given code using the given length */
730 for (I = 0; I < sizeof (CmpSuffixTab) / sizeof (CmpSuffixTab [0]); ++I) {
731 if (strncmp (Code, CmpSuffixTab [I], CodeLen) == 0) {
743 cmp_t FindBoolCmpCond (const char* Name)
744 /* Check if the given string is the name of one of the boolean transformer
745 * subroutine, and if so, return the condition that is evaluated by this
746 * routine. Return CMP_INV if the condition is not recognised.
749 /* Check for the correct subroutine name */
750 if (strncmp (Name, "bool", 4) == 0) {
751 /* Name is ok, search for the code in the table */
752 return FindCmpCond (Name+4, strlen(Name)-4);
761 cmp_t FindTosCmpCond (const char* Name)
762 /* Check if this is a call to one of the TOS compare functions (tosgtax).
763 * Return the condition code or CMP_INV on failure.
766 unsigned Len = strlen (Name);
768 /* Check for the correct subroutine name */
769 if (strncmp (Name, "tos", 3) == 0 && strcmp (Name+Len-2, "ax") == 0) {
770 /* Name is ok, search for the code in the table */
771 return FindCmpCond (Name+3, Len-3-2);