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 { "bnega", REG_A, REG_AX },
85 { "bnegax", REG_AX, REG_AX },
86 { "bnegeax", REG_EAX, REG_EAX },
87 { "booleq", REG_NONE, REG_AX },
88 { "boolge", REG_NONE, REG_AX },
89 { "boolgt", REG_NONE, REG_AX },
90 { "boolle", REG_NONE, REG_AX },
91 { "boollt", REG_NONE, REG_AX },
92 { "boolne", REG_NONE, REG_AX },
93 { "booluge", REG_NONE, REG_AX },
94 { "boolugt", REG_NONE, REG_AX },
95 { "boolule", REG_NONE, REG_AX },
96 { "boolult", REG_NONE, REG_AX },
97 { "complax", REG_AX, REG_AX },
98 { "decax1", REG_AX, REG_AX },
99 { "decax2", REG_AX, REG_AX },
100 { "decax3", REG_AX, REG_AX },
101 { "decax4", REG_AX, REG_AX },
102 { "decax5", REG_AX, REG_AX },
103 { "decax6", REG_AX, REG_AX },
104 { "decax7", REG_AX, REG_AX },
105 { "decax8", REG_AX, REG_AX },
106 { "decaxy", REG_AXY, REG_AX | REG_TMP1 },
107 { "deceaxy", REG_EAXY, REG_EAX },
108 { "decsp1", REG_NONE, REG_Y },
109 { "decsp2", REG_NONE, REG_A },
110 { "decsp3", REG_NONE, REG_A },
111 { "decsp4", REG_NONE, REG_A },
112 { "decsp5", REG_NONE, REG_A },
113 { "decsp6", REG_NONE, REG_A },
114 { "decsp7", REG_NONE, REG_A },
115 { "decsp8", REG_NONE, REG_A },
116 { "incax1", REG_AX, REG_AX },
117 { "incax2", REG_AX, REG_AX },
118 { "incax3", REG_AX, REG_AX },
119 { "incax4", REG_AX, REG_AX },
120 { "incax5", REG_AX, REG_AX },
121 { "incax6", REG_AX, REG_AX },
122 { "incax7", REG_AX, REG_AX },
123 { "incax8", REG_AX, REG_AX },
124 { "incsp1", REG_NONE, REG_NONE },
125 { "incsp2", REG_NONE, REG_Y },
126 { "incsp3", REG_NONE, REG_Y },
127 { "incsp4", REG_NONE, REG_Y },
128 { "incsp5", REG_NONE, REG_Y },
129 { "incsp6", REG_NONE, REG_Y },
130 { "incsp7", REG_NONE, REG_Y },
131 { "incsp8", REG_NONE, REG_Y },
132 { "laddeq", REG_EAXY|REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
133 { "laddeq0sp", REG_EAX, REG_EAXY },
134 { "laddeq1", REG_Y | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
135 { "laddeqa", REG_AY | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
136 { "laddeqysp", REG_EAXY, REG_EAXY },
137 { "ldaidx", REG_AXY, REG_AX | REG_PTR1 },
138 { "ldauidx", REG_AXY, REG_AX | REG_PTR1 },
139 { "ldax0sp", REG_NONE, REG_AXY },
140 { "ldaxi", REG_AX, REG_AXY | REG_PTR1 },
141 { "ldaxidx", REG_AXY, REG_AXY | REG_PTR1 },
142 { "ldaxysp", REG_Y, REG_AXY },
143 { "ldeax0sp", REG_NONE, REG_EAXY },
144 { "ldeaxi", REG_AX, REG_EAXY | REG_PTR1 },
145 { "ldeaxidx", REG_AXY, REG_EAXY | REG_PTR1 },
146 { "ldeaxysp", REG_Y, REG_EAXY },
147 { "leaasp", REG_A, REG_AX },
148 { "lsubeq", REG_EAXY|REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
149 { "lsubeq0sp", REG_EAX, REG_EAXY },
150 { "lsubeq1", REG_Y | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
151 { "lsubeqa", REG_AY | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
152 { "lsubeqysp", REG_EAXY, REG_EAXY },
153 { "negax", REG_AX, REG_AX },
154 { "push0", REG_NONE, REG_AXY },
155 { "push1", REG_NONE, REG_AXY },
156 { "push2", REG_NONE, REG_AXY },
157 { "push3", REG_NONE, REG_AXY },
158 { "push4", REG_NONE, REG_AXY },
159 { "push5", REG_NONE, REG_AXY },
160 { "push6", REG_NONE, REG_AXY },
161 { "push7", REG_NONE, REG_AXY },
162 { "pusha", REG_A, REG_Y },
163 { "pusha0", REG_A, REG_XY },
164 { "pusha0sp", REG_NONE, REG_AY },
165 { "pushax", REG_AX, REG_Y },
166 { "pushaysp", REG_Y, REG_AY },
167 { "pushc0", REG_NONE, REG_A | REG_Y },
168 { "pushc1", REG_NONE, REG_A | REG_Y },
169 { "pushc2", REG_NONE, REG_A | REG_Y },
170 { "pusheax", REG_EAX, REG_Y },
171 { "pushw", REG_AX, REG_AXY | REG_PTR1 },
172 { "pushw0sp", REG_NONE, REG_AXY },
173 { "pushwidx", REG_AXY, REG_AXY | REG_PTR1 },
174 { "pushwysp", REG_Y, REG_AXY },
175 { "regswap", REG_AXY, REG_AXY | REG_TMP1 },
176 { "regswap1", REG_XY, REG_A },
177 { "regswap2", REG_XY, REG_A | REG_Y },
178 { "shlax1", REG_AX, REG_AX | REG_TMP1 },
179 { "shlax2", REG_AX, REG_AX | REG_TMP1 },
180 { "shlax3", REG_AX, REG_AX | REG_TMP1 },
181 { "shlax4", REG_AX, REG_AX | REG_TMP1 },
182 { "shrax1", REG_AX, REG_AX | REG_TMP1 },
183 { "shrax2", REG_AX, REG_AX | REG_TMP1 },
184 { "shrax3", REG_AX, REG_AX | REG_TMP1 },
185 { "shrax4", REG_AX, REG_AX | REG_TMP1 },
186 { "shreax1", REG_EAX, REG_AX | REG_TMP1 },
187 { "shreax2", REG_EAX, REG_AX | REG_TMP1 },
188 { "shreax3", REG_EAX, REG_AX | REG_TMP1 },
189 { "shreax4", REG_EAX, REG_AX | REG_TMP1 },
190 { "staspidx", REG_A | REG_Y, REG_Y | REG_TMP1 | REG_PTR1 },
191 { "stax0sp", REG_AX, REG_Y },
192 { "staxspidx", REG_AXY, REG_TMP1 | REG_PTR1 },
193 { "staxysp", REG_AXY, REG_Y },
194 { "steax0sp", REG_EAX, REG_Y },
195 { "steaxysp", REG_EAXY, REG_Y },
196 { "subeq0sp", REG_AX, REG_AXY },
197 { "subeqysp", REG_AXY, REG_AXY },
198 { "tosadda0", REG_A, REG_AXY },
199 { "tosaddax", REG_AX, REG_AXY },
200 { "tosanda0", REG_A, REG_AXY },
201 { "tosandax", REG_AX, REG_AXY },
202 { "tosaslax", REG_A, REG_AXY | REG_TMP1 },
203 { "tosasleax", REG_A, REG_EAXY | REG_TMP1 },
204 { "tosasrax", REG_A, REG_AXY | REG_TMP1 },
205 { "tosasreax", REG_A, REG_EAXY | REG_TMP1 },
206 { "tosdiva0", REG_AY, REG_ALL },
207 { "tosdivax", REG_AXY, REG_ALL },
208 { "tosdiveax", REG_EAXY, REG_ALL },
209 { "toseqeax", REG_EAX, REG_AXY | REG_PTR1 },
210 { "tosgeeax", REG_EAX, REG_AXY | REG_PTR1 },
211 { "tosgteax", REG_EAX, REG_AXY | REG_PTR1 },
212 { "tosicmp", REG_AX, REG_AXY | REG_SREG },
213 { "toslcmp", REG_EAX, REG_A | REG_Y | REG_PTR1 },
214 { "tosleeax", REG_EAX, REG_AXY | REG_PTR1 },
215 { "toslteax", REG_EAX, REG_AXY | REG_PTR1 },
216 { "tosmula0", REG_AX, REG_ALL },
217 { "tosmulax", REG_AX, REG_ALL },
218 { "tosmuleax", REG_EAX, REG_ALL },
219 { "tosneeax", REG_EAX, REG_AXY | REG_PTR1 },
220 { "tosshlax", REG_A, REG_AXY | REG_TMP1 },
221 { "tosshleax", REG_A, REG_EAXY | REG_TMP1 },
222 { "tosshrax", REG_A, REG_AXY | REG_TMP1 },
223 { "tosshreax", REG_A, REG_EAXY | REG_TMP1 },
224 { "tossuba0", REG_A, REG_AXY },
225 { "tossubax", REG_AX, REG_AXY },
226 { "tossubeax", REG_EAX, REG_EAXY },
227 { "tosugeeax", REG_EAX, REG_AXY | REG_PTR1 },
228 { "tosugteax", REG_EAX, REG_AXY | REG_PTR1 },
229 { "tosuleeax", REG_EAX, REG_AXY | REG_PTR1 },
230 { "tosulteax", REG_EAX, REG_AXY | REG_PTR1 },
231 { "tosumula0", REG_AX, REG_ALL },
232 { "tosumulax", REG_AX, REG_ALL },
233 { "tosumuleax", REG_EAX, REG_ALL },
234 { "tsteax", REG_EAX, REG_Y },
235 { "utsteax", REG_EAX, REG_Y },
237 #define FuncInfoCount (sizeof(FuncInfoTable) / sizeof(FuncInfoTable[0]))
239 /* Table with names of zero page locations used by the compiler */
240 static const ZPInfo ZPInfoTable[] = {
241 { 0, "ptr1", REG_PTR1_LO, REG_PTR1 },
242 { 0, "ptr1+1", REG_PTR1_HI, REG_PTR1 },
243 { 0, "ptr2", REG_PTR2_LO, REG_PTR2 },
244 { 0, "ptr2+1", REG_PTR2_HI, REG_PTR2 },
245 { 4, "ptr3", REG_NONE, REG_NONE },
246 { 4, "ptr4", REG_NONE, REG_NONE },
247 { 7, "regbank", REG_NONE, REG_NONE },
248 { 0, "regsave", REG_SAVE_LO, REG_SAVE },
249 { 0, "regsave+1", REG_SAVE_HI, REG_SAVE },
250 { 0, "sp", REG_SP_LO, REG_SP },
251 { 0, "sp+1", REG_SP_HI, REG_SP },
252 { 0, "sreg", REG_SREG_LO, REG_SREG },
253 { 0, "sreg+1", REG_SREG_HI, REG_SREG },
254 { 0, "tmp1", REG_TMP1, REG_TMP1 },
255 { 0, "tmp2", REG_NONE, REG_NONE },
256 { 0, "tmp3", REG_NONE, REG_NONE },
257 { 0, "tmp4", REG_NONE, REG_NONE },
259 #define ZPInfoCount (sizeof(ZPInfoTable) / sizeof(ZPInfoTable[0]))
263 /*****************************************************************************/
265 /*****************************************************************************/
269 static int CompareFuncInfo (const void* Key, const void* Info)
270 /* Compare function for bsearch */
272 return strcmp (Key, ((const FuncInfo*) Info)->Name);
277 void GetFuncInfo (const char* Name, unsigned short* Use, unsigned short* Chg)
278 /* For the given function, lookup register information and store it into
279 * the given variables. If the function is unknown, assume it will use and
280 * load all registers.
283 /* If the function name starts with an underline, it is an external
284 * function. Search for it in the symbol table. If the function does
285 * not start with an underline, it may be a runtime support function.
286 * Search for it in the list of builtin functions.
288 if (Name[0] == '_') {
290 /* Search in the symbol table, skip the leading underscore */
291 SymEntry* E = FindGlobalSym (Name+1);
293 /* Did we find it in the top level table? */
294 if (E && IsTypeFunc (E->Type)) {
296 /* A function may use the A or A/X registers if it is a fastcall
297 * function. If it is not a fastcall function but a variadic one,
298 * it will use the Y register (the parameter size is passed here).
299 * In all other cases, no registers are used. However, we assume
300 * that any function will destroy all registers.
302 FuncDesc* D = E->V.F.Func;
303 if ((D->Flags & FD_FASTCALL) != 0 && D->ParamCount > 0) {
304 /* Will use registers depending on the last param */
305 unsigned LastParamSize = CheckedSizeOf (D->LastParam->Type);
306 if (LastParamSize == 1) {
308 } else if (LastParamSize == 2) {
313 } else if ((D->Flags & FD_VARIADIC) != 0) {
316 /* Will not use any registers */
320 /* Will destroy all registers */
327 } else if (IsDigit (Name[0]) || Name[0] == '$') {
329 /* A call to a numeric address. Assume that anything gets used and
330 * destroyed. This is not a real problem, since numeric addresses
331 * are used mostly in inline assembly anyway.
339 /* Search for the function in the list of builtin functions */
340 const FuncInfo* Info = bsearch (Name, FuncInfoTable, FuncInfoCount,
341 sizeof(FuncInfo), CompareFuncInfo);
343 /* Do we know the function? */
345 /* Use the information we have */
349 /* It's an internal function we have no information for. If in
350 * debug mode, output an additional warning, so we have a chance
351 * to fix it. Otherwise assume that the internal function will
352 * use and change all registers.
355 fprintf (stderr, "No info about internal function `%s'", Name);
363 /* Function not found - assume that the primary register is input, and all
364 * registers are changed
372 static int CompareZPInfo (const void* Name, const void* Info)
373 /* Compare function for bsearch */
375 /* Cast the pointers to the correct data type */
376 const char* N = (const char*) Name;
377 const ZPInfo* E = (const ZPInfo*) Info;
379 /* Do the compare. Be careful because of the length (Info may contain
380 * more than just the zeropage name).
383 /* Do a full compare */
384 return strcmp (N, E->Name);
386 /* Only compare the first part */
387 int Res = strncmp (N, E->Name, E->Len);
388 if (Res == 0 && (N[E->Len] != '\0' && N[E->Len] != '+')) {
389 /* Name is actually longer than Info->Name */
398 const ZPInfo* GetZPInfo (const char* Name)
399 /* If the given name is a zero page symbol, return a pointer to the info
400 * struct for this symbol, otherwise return NULL.
403 /* Search for the zp location in the list */
404 return bsearch (Name, ZPInfoTable, ZPInfoCount,
405 sizeof(ZPInfo), CompareZPInfo);
410 static unsigned GetRegInfo2 (CodeSeg* S,
417 /* Recursively called subfunction for GetRegInfo. */
419 /* Follow the instruction flow recording register usage. */
424 /* Check if we have already visited the current code entry. If so,
427 if (CE_HasMark (E)) {
431 /* Mark this entry as already visited */
433 CollAppend (Visited, E);
435 /* Evaluate the used registers */
437 if (E->OPC == OP65_RTS ||
438 ((E->Info & OF_UBRA) != 0 && E->JumpTo == 0)) {
439 /* This instruction will leave the function */
443 /* We are not interested in the use of any register that has been
447 /* Remember the remaining registers */
451 /* Evaluate the changed registers */
452 if ((R = E->Chg) != REG_NONE) {
453 /* We are not interested in the use of any register that has been
457 /* Remember the remaining registers */
461 /* If we know about all registers now, bail out */
462 if (((Used | Unused) & Wanted) == Wanted) {
466 /* If the instruction is an RTS or RTI, we're done */
467 if ((E->Info & OF_RET) != 0) {
471 /* If we have an unconditional branch, follow this branch if possible,
472 * otherwise we're done.
474 if ((E->Info & OF_UBRA) != 0) {
476 /* Does this jump have a valid target? */
479 /* Unconditional jump */
480 E = E->JumpTo->Owner;
481 Index = -1; /* Invalidate */
484 /* Jump outside means we're done */
488 /* In case of conditional branches, follow the branch if possible and
489 * follow the normal flow (branch not taken) afterwards. If we cannot
490 * follow the branch, we're done.
492 } else if ((E->Info & OF_CBRA) != 0) {
494 /* Recursively determine register usage at the branch target */
500 /* Jump to internal label */
501 U1 = GetRegInfo2 (S, E->JumpTo->Owner, -1, Visited, Used, Unused, Wanted);
505 /* Jump to external label. This will effectively exit the
506 * function, so we use the exitregs information here.
512 /* Get the next entry */
514 Index = CS_GetEntryIndex (S, E);
516 if ((E = CS_GetEntry (S, ++Index)) == 0) {
517 Internal ("GetRegInfo2: No next entry!");
520 /* Follow flow if branch not taken */
521 U2 = GetRegInfo2 (S, E, Index, Visited, Used, Unused, Wanted);
523 /* Registers are used if they're use in any of the branches */
528 /* Just go to the next instruction */
530 Index = CS_GetEntryIndex (S, E);
532 E = CS_GetEntry (S, ++Index);
535 Internal ("GetRegInfo2: No next entry!");
542 /* Return to the caller the complement of all unused registers */
548 static unsigned GetRegInfo1 (CodeSeg* S,
555 /* Recursively called subfunction for GetRegInfo. */
557 /* Remember the current count of the line collection */
558 unsigned Count = CollCount (Visited);
560 /* Call the worker routine */
561 unsigned R = GetRegInfo2 (S, E, Index, Visited, Used, Unused, Wanted);
563 /* Restore the old count, unmarking all new entries */
564 unsigned NewCount = CollCount (Visited);
565 while (NewCount-- > Count) {
566 CodeEntry* E = CollAt (Visited, NewCount);
568 CollDelete (Visited, NewCount);
571 /* Return the registers used */
577 unsigned GetRegInfo (struct CodeSeg* S, unsigned Index, unsigned Wanted)
578 /* Determine register usage information for the instructions starting at the
583 Collection Visited; /* Visited entries */
586 /* Get the code entry for the given index */
587 if (Index >= CS_GetEntryCount (S)) {
588 /* There is no such code entry */
591 E = CS_GetEntry (S, Index);
593 /* Initialize the data structure used to collection information */
594 InitCollection (&Visited);
596 /* Call the recursive subfunction */
597 R = GetRegInfo1 (S, E, Index, &Visited, REG_NONE, REG_NONE, Wanted);
599 /* Delete the line collection */
600 DoneCollection (&Visited);
602 /* Return the registers used */
608 int RegAUsed (struct CodeSeg* S, unsigned Index)
609 /* Check if the value in A is used. */
611 return (GetRegInfo (S, Index, REG_A) & REG_A) != 0;
616 int RegXUsed (struct CodeSeg* S, unsigned Index)
617 /* Check if the value in X is used. */
619 return (GetRegInfo (S, Index, REG_X) & REG_X) != 0;
624 int RegYUsed (struct CodeSeg* S, unsigned Index)
625 /* Check if the value in Y is used. */
627 return (GetRegInfo (S, Index, REG_Y) & REG_Y) != 0;
632 int RegAXUsed (struct CodeSeg* S, unsigned Index)
633 /* Check if the value in A or(!) the value in X are used. */
635 return (GetRegInfo (S, Index, REG_AX) & REG_AX) != 0;
640 int RegEAXUsed (struct CodeSeg* S, unsigned Index)
641 /* Check if any of the four bytes in EAX are used. */
643 return (GetRegInfo (S, Index, REG_EAX) & REG_EAX) != 0;
648 unsigned GetKnownReg (unsigned Use, const RegContents* RC)
649 /* Return the register or zero page location from the set in Use, thats
650 * contents are known. If Use does not contain any register, or if the
651 * register in question does not have a known value, return REG_NONE.
654 if ((Use & REG_A) != 0) {
655 return (RC == 0 || RC->RegA >= 0)? REG_A : REG_NONE;
656 } else if ((Use & REG_X) != 0) {
657 return (RC == 0 || RC->RegX >= 0)? REG_X : REG_NONE;
658 } else if ((Use & REG_Y) != 0) {
659 return (RC == 0 || RC->RegY >= 0)? REG_Y : REG_NONE;
660 } else if ((Use & REG_TMP1) != 0) {
661 return (RC == 0 || RC->Tmp1 >= 0)? REG_TMP1 : REG_NONE;
662 } else if ((Use & REG_PTR1_LO) != 0) {
663 return (RC == 0 || RC->Ptr1Lo >= 0)? REG_PTR1_LO : REG_NONE;
664 } else if ((Use & REG_PTR1_HI) != 0) {
665 return (RC == 0 || RC->Ptr1Hi >= 0)? REG_PTR1_HI : REG_NONE;
666 } else if ((Use & REG_SREG_LO) != 0) {
667 return (RC == 0 || RC->SRegLo >= 0)? REG_SREG_LO : REG_NONE;
668 } else if ((Use & REG_SREG_HI) != 0) {
669 return (RC == 0 || RC->SRegHi >= 0)? REG_SREG_HI : REG_NONE;
677 static cmp_t FindCmpCond (const char* Code, unsigned CodeLen)
678 /* Search for a compare condition by the given code using the given length */
683 for (I = 0; I < sizeof (CmpSuffixTab) / sizeof (CmpSuffixTab [0]); ++I) {
684 if (strncmp (Code, CmpSuffixTab [I], CodeLen) == 0) {
696 cmp_t FindBoolCmpCond (const char* Name)
697 /* Check if the given string is the name of one of the boolean transformer
698 * subroutine, and if so, return the condition that is evaluated by this
699 * routine. Return CMP_INV if the condition is not recognised.
702 /* Check for the correct subroutine name */
703 if (strncmp (Name, "bool", 4) == 0) {
704 /* Name is ok, search for the code in the table */
705 return FindCmpCond (Name+4, strlen(Name)-4);
714 cmp_t FindTosCmpCond (const char* Name)
715 /* Check if this is a call to one of the TOS compare functions (tosgtax).
716 * Return the condition code or CMP_INV on failure.
719 unsigned Len = strlen (Name);
721 /* Check for the correct subroutine name */
722 if (strncmp (Name, "tos", 3) == 0 && strcmp (Name+Len-2, "ax") == 0) {
723 /* Name is ok, search for the code in the table */
724 return FindCmpCond (Name+3, Len-3-2);