1 /*****************************************************************************/
5 /* Additional information about 6502 code */
9 /* (C) 2001-2002 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
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 /*****************************************************************************/
54 /*****************************************************************************/
56 /*****************************************************************************/
60 /* Table with the compare suffixes */
61 static const char CmpSuffixTab [][4] = {
62 "eq", "ne", "gt", "ge", "lt", "le", "ugt", "uge", "ult", "ule"
65 /* Table listing the function names and code info values for known internally
66 * used functions. This table should get auto-generated in the future.
68 typedef struct FuncInfo FuncInfo;
70 const char* Name; /* Function name */
71 unsigned short Use; /* Register usage */
72 unsigned short Chg; /* Changed/destroyed registers */
75 static const FuncInfo FuncInfoTable[] = {
76 { "addeq0sp", REG_AX, REG_AXY },
77 { "addeqysp", REG_AXY, REG_AXY },
78 { "addysp", REG_Y, REG_NONE },
79 { "aslax1", REG_AX, REG_AX | REG_TMP1 },
80 { "aslax2", REG_AX, REG_AX | REG_TMP1 },
81 { "aslax3", REG_AX, REG_AX | REG_TMP1 },
82 { "aslax4", REG_AX, REG_AX | REG_TMP1 },
83 { "bnega", REG_A, REG_AX },
84 { "bnegax", REG_AX, REG_AX },
85 { "bnegeax", REG_EAX, REG_EAX },
86 { "booleq", REG_NONE, REG_AX },
87 { "boolge", REG_NONE, REG_AX },
88 { "boolgt", REG_NONE, REG_AX },
89 { "boolle", REG_NONE, REG_AX },
90 { "boollt", REG_NONE, REG_AX },
91 { "boolne", REG_NONE, REG_AX },
92 { "booluge", REG_NONE, REG_AX },
93 { "boolugt", REG_NONE, REG_AX },
94 { "boolule", REG_NONE, REG_AX },
95 { "boolult", REG_NONE, REG_AX },
96 { "complax", REG_AX, REG_AX },
97 { "decax1", REG_AX, REG_AX },
98 { "decax2", REG_AX, REG_AX },
99 { "decax3", REG_AX, REG_AX },
100 { "decax4", REG_AX, REG_AX },
101 { "decax5", REG_AX, REG_AX },
102 { "decax6", REG_AX, REG_AX },
103 { "decax7", REG_AX, REG_AX },
104 { "decax8", REG_AX, REG_AX },
105 { "decaxy", REG_AXY, REG_AX | REG_TMP1 },
106 { "deceaxy", REG_EAXY, REG_EAX },
107 { "decsp1", REG_NONE, REG_Y },
108 { "decsp2", REG_NONE, REG_A },
109 { "decsp3", REG_NONE, REG_A },
110 { "decsp4", REG_NONE, REG_A },
111 { "decsp5", REG_NONE, REG_A },
112 { "decsp6", REG_NONE, REG_A },
113 { "decsp7", REG_NONE, REG_A },
114 { "decsp8", REG_NONE, REG_A },
115 { "incax1", REG_AX, REG_AX },
116 { "incax2", REG_AX, REG_AX },
117 { "incsp1", REG_NONE, REG_NONE },
118 { "incsp2", REG_NONE, REG_Y },
119 { "incsp3", REG_NONE, REG_Y },
120 { "incsp4", REG_NONE, REG_Y },
121 { "incsp5", REG_NONE, REG_Y },
122 { "incsp6", REG_NONE, REG_Y },
123 { "incsp7", REG_NONE, REG_Y },
124 { "incsp8", REG_NONE, REG_Y },
125 { "laddeq", REG_EAXY|REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
126 { "laddeq0sp", REG_EAX, REG_EAXY },
127 { "laddeq1", REG_Y | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
128 { "laddeqa", REG_AY | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
129 { "laddeqysp", REG_EAXY, REG_EAXY },
130 { "ldaidx", REG_AXY, REG_AX | REG_PTR1 },
131 { "ldauidx", REG_AXY, REG_AX | REG_PTR1 },
132 { "ldax0sp", REG_NONE, REG_AXY },
133 { "ldaxi", REG_AX, REG_AXY | REG_PTR1 },
134 { "ldaxidx", REG_AXY, REG_AXY | REG_PTR1 },
135 { "ldaxysp", REG_Y, REG_AXY },
136 { "ldeax0sp", REG_NONE, REG_EAXY },
137 { "ldeaxi", REG_AX, REG_EAXY | REG_PTR1 },
138 { "ldeaxidx", REG_AXY, REG_EAXY | REG_PTR1 },
139 { "ldeaxysp", REG_Y, REG_EAXY },
140 { "leaasp", REG_A, REG_AX },
141 { "lsubeq", REG_EAXY|REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
142 { "lsubeq0sp", REG_EAX, REG_EAXY },
143 { "lsubeq1", REG_Y | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
144 { "lsubeqa", REG_AY | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
145 { "lsubeqysp", REG_EAXY, REG_EAXY },
146 { "negax", REG_AX, REG_AX },
147 { "push0", REG_NONE, REG_AXY },
148 { "push1", REG_NONE, REG_AXY },
149 { "push2", REG_NONE, REG_AXY },
150 { "push3", REG_NONE, REG_AXY },
151 { "push4", REG_NONE, REG_AXY },
152 { "push5", REG_NONE, REG_AXY },
153 { "push6", REG_NONE, REG_AXY },
154 { "push7", REG_NONE, REG_AXY },
155 { "pusha", REG_A, REG_Y },
156 { "pusha0", REG_A, REG_XY },
157 { "pusha0sp", REG_NONE, REG_AY },
158 { "pushax", REG_AX, REG_Y },
159 { "pushaysp", REG_Y, REG_AY },
160 { "pushc0", REG_NONE, REG_A | REG_Y },
161 { "pushc1", REG_NONE, REG_A | REG_Y },
162 { "pushc2", REG_NONE, REG_A | REG_Y },
163 { "pusheax", REG_EAX, REG_Y },
164 { "pushw", REG_AX, REG_AXY | REG_PTR1 },
165 { "pushw0sp", REG_NONE, REG_AXY },
166 { "pushwidx", REG_AXY, REG_AXY | REG_PTR1 },
167 { "pushwysp", REG_Y, REG_AXY },
168 { "regswap", REG_AXY, REG_AXY | REG_TMP1 },
169 { "regswap1", REG_XY, REG_A },
170 { "regswap2", REG_XY, REG_A | REG_Y },
171 { "shlax1", REG_AX, REG_AX | REG_TMP1 },
172 { "shlax2", REG_AX, REG_AX | REG_TMP1 },
173 { "shlax3", REG_AX, REG_AX | REG_TMP1 },
174 { "shlax4", REG_AX, REG_AX | REG_TMP1 },
175 { "shrax1", REG_AX, REG_AX | REG_TMP1 },
176 { "shrax2", REG_AX, REG_AX | REG_TMP1 },
177 { "shrax3", REG_AX, REG_AX | REG_TMP1 },
178 { "shrax4", REG_AX, REG_AX | REG_TMP1 },
179 { "shreax1", REG_EAX, REG_AX | REG_TMP1 },
180 { "shreax2", REG_EAX, REG_AX | REG_TMP1 },
181 { "shreax3", REG_EAX, REG_AX | REG_TMP1 },
182 { "shreax4", REG_EAX, REG_AX | REG_TMP1 },
183 { "staspidx", REG_A | REG_Y, REG_Y | REG_TMP1 | REG_PTR1 },
184 { "stax0sp", REG_AX, REG_Y },
185 { "staxysp", REG_AXY, REG_Y },
186 { "steax0sp", REG_EAX, REG_Y },
187 { "steaxysp", REG_EAXY, REG_Y },
188 { "subeq0sp", REG_AX, REG_AXY },
189 { "subeqysp", REG_AXY, REG_AXY },
190 { "tosadda0", REG_A, REG_AXY },
191 { "tosaddax", REG_AX, REG_AXY },
192 { "tosanda0", REG_A, REG_AXY },
193 { "tosandax", REG_AX, REG_AXY },
194 { "tosaslax", REG_A, REG_AXY | REG_TMP1 },
195 { "tosasleax", REG_A, REG_EAXY | REG_TMP1 },
196 { "tosasrax", REG_A, REG_AXY | REG_TMP1 },
197 { "tosasreax", REG_A, REG_EAXY | REG_TMP1 },
198 { "tosdiva0", REG_AY, REG_ALL },
199 { "tosdivax", REG_AXY, REG_ALL },
200 { "tosdiveax", REG_EAXY, REG_ALL },
201 { "toseqeax", REG_EAX, REG_AXY | REG_PTR1 },
202 { "tosgeeax", REG_EAX, REG_AXY | REG_PTR1 },
203 { "tosgteax", REG_EAX, REG_AXY | REG_PTR1 },
204 { "tosicmp", REG_AX, REG_AXY | REG_SREG },
205 { "toslcmp", REG_EAX, REG_A | REG_Y | REG_PTR1 },
206 { "tosleeax", REG_EAX, REG_AXY | REG_PTR1 },
207 { "toslteax", REG_EAX, REG_AXY | REG_PTR1 },
208 { "tosmula0", REG_AX, REG_ALL },
209 { "tosmulax", REG_AX, REG_ALL },
210 { "tosmuleax", REG_EAX, REG_ALL },
211 { "tosneeax", REG_EAX, REG_AXY | REG_PTR1 },
212 { "tosshlax", REG_A, REG_AXY | REG_TMP1 },
213 { "tosshleax", REG_A, REG_EAXY | REG_TMP1 },
214 { "tosshrax", REG_A, REG_AXY | REG_TMP1 },
215 { "tosshreax", REG_A, REG_EAXY | REG_TMP1 },
216 { "tossuba0", REG_A, REG_AXY },
217 { "tossubax", REG_AX, REG_AXY },
218 { "tossubeax", REG_EAX, REG_EAXY },
219 { "tosugeeax", REG_EAX, REG_AXY | REG_PTR1 },
220 { "tosugteax", REG_EAX, REG_AXY | REG_PTR1 },
221 { "tosuleeax", REG_EAX, REG_AXY | REG_PTR1 },
222 { "tosulteax", REG_EAX, REG_AXY | REG_PTR1 },
223 { "tosumula0", REG_AX, REG_ALL },
224 { "tosumulax", REG_AX, REG_ALL },
225 { "tosumuleax", REG_EAX, REG_ALL },
226 { "tsteax", REG_EAX, REG_Y },
227 { "utsteax", REG_EAX, REG_Y },
229 #define FuncInfoCount (sizeof(FuncInfoTable) / sizeof(FuncInfoTable[0]))
231 /* Table with names of zero page locations used by the compiler */
232 static const ZPInfo ZPInfoTable[] = {
233 { 0, "ptr1", REG_PTR1_LO, REG_PTR1 },
234 { 0, "ptr1+1", REG_PTR1_HI, REG_PTR1 },
235 { 0, "ptr2", REG_PTR2_LO, REG_PTR2 },
236 { 0, "ptr2+1", REG_PTR2_HI, REG_PTR2 },
237 { 4, "ptr3", REG_NONE, REG_NONE },
238 { 4, "ptr4", REG_NONE, REG_NONE },
239 { 7, "regbank", REG_NONE, REG_NONE },
240 { 0, "regsave", REG_SAVE_LO, REG_SAVE },
241 { 0, "regsave+1", REG_SAVE_HI, REG_SAVE },
242 { 0, "sp", REG_SP_LO, REG_SP },
243 { 0, "sp+1", REG_SP_HI, REG_SP },
244 { 0, "sreg", REG_SREG_LO, REG_SREG },
245 { 0, "sreg+1", REG_SREG_HI, REG_SREG },
246 { 0, "tmp1", REG_TMP1, REG_TMP1 },
247 { 0, "tmp2", REG_NONE, REG_NONE },
248 { 0, "tmp3", REG_NONE, REG_NONE },
249 { 0, "tmp4", REG_NONE, REG_NONE },
251 #define ZPInfoCount (sizeof(ZPInfoTable) / sizeof(ZPInfoTable[0]))
255 /*****************************************************************************/
257 /*****************************************************************************/
261 static int CompareFuncInfo (const void* Key, const void* Info)
262 /* Compare function for bsearch */
264 return strcmp (Key, ((const FuncInfo*) Info)->Name);
269 void GetFuncInfo (const char* Name, unsigned short* Use, unsigned short* Chg)
270 /* For the given function, lookup register information and store it into
271 * the given variables. If the function is unknown, assume it will use and
272 * load all registers.
275 /* If the function name starts with an underline, it is an external
276 * function. Search for it in the symbol table. If the function does
277 * not start with an underline, it may be a runtime support function.
278 * Search for it in the list of builtin functions.
280 if (Name[0] == '_') {
282 /* Search in the symbol table, skip the leading underscore */
283 SymEntry* E = FindGlobalSym (Name+1);
285 /* Did we find it in the top level table? */
286 if (E && IsTypeFunc (E->Type)) {
288 /* A function may use the A or A/X registers if it is a fastcall
289 * function. If it is not a fastcall function but a variadic one,
290 * it will use the Y register (the parameter size is passed here).
291 * In all other cases, no registers are used. However, we assume
292 * that any function will destroy all registers.
294 FuncDesc* D = E->V.F.Func;
295 if ((D->Flags & FD_FASTCALL) != 0 && D->ParamCount > 0) {
296 /* Will use registers depending on the last param */
297 unsigned LastParamSize = CheckedSizeOf (D->LastParam->Type);
298 if (LastParamSize == 1) {
300 } else if (LastParamSize == 2) {
305 } else if ((D->Flags & FD_VARIADIC) != 0) {
308 /* Will not use any registers */
312 /* Will destroy all registers */
319 } else if (IsDigit (Name[0]) || Name[0] == '$') {
321 /* A call to a numeric address. Assume that anything gets used and
322 * destroyed. This is not a real problem, since numeric addresses
323 * are used mostly in inline assembly anyway.
331 /* Search for the function in the list of builtin functions */
332 const FuncInfo* Info = bsearch (Name, FuncInfoTable, FuncInfoCount,
333 sizeof(FuncInfo), CompareFuncInfo);
335 /* Do we know the function? */
337 /* Use the information we have */
344 /* Function not found - assume that the primary register is input, and all
345 * registers are changed
353 static int CompareZPInfo (const void* Name, const void* Info)
354 /* Compare function for bsearch */
356 /* Cast the pointers to the correct data type */
357 const char* N = (const char*) Name;
358 const ZPInfo* E = (const ZPInfo*) Info;
360 /* Do the compare. Be careful because of the length (Info may contain
361 * more than just the zeropage name).
364 /* Do a full compare */
365 return strcmp (N, E->Name);
367 /* Only compare the first part */
368 int Res = strncmp (N, E->Name, E->Len);
369 if (Res == 0 && (N[E->Len] != '\0' && N[E->Len] != '+')) {
370 /* Name is actually longer than Info->Name */
379 const ZPInfo* GetZPInfo (const char* Name)
380 /* If the given name is a zero page symbol, return a pointer to the info
381 * struct for this symbol, otherwise return NULL.
384 /* Search for the zp location in the list */
385 return bsearch (Name, ZPInfoTable, ZPInfoCount,
386 sizeof(ZPInfo), CompareZPInfo);
391 static unsigned GetRegInfo2 (CodeSeg* S,
398 /* Recursively called subfunction for GetRegInfo. */
400 /* Follow the instruction flow recording register usage. */
405 /* Check if we have already visited the current code entry. If so,
408 if (CE_HasMark (E)) {
412 /* Mark this entry as already visited */
414 CollAppend (Visited, E);
416 /* Evaluate the used registers */
418 if (E->OPC == OP65_RTS ||
419 ((E->Info & OF_UBRA) != 0 && E->JumpTo == 0)) {
420 /* This instruction will leave the function */
424 /* We are not interested in the use of any register that has been
428 /* Remember the remaining registers */
432 /* Evaluate the changed registers */
433 if ((R = E->Chg) != REG_NONE) {
434 /* We are not interested in the use of any register that has been
438 /* Remember the remaining registers */
442 /* If we know about all registers now, bail out */
443 if (((Used | Unused) & Wanted) == Wanted) {
447 /* If the instruction is an RTS or RTI, we're done */
448 if ((E->Info & OF_RET) != 0) {
452 /* If we have an unconditional branch, follow this branch if possible,
453 * otherwise we're done.
455 if ((E->Info & OF_UBRA) != 0) {
457 /* Does this jump have a valid target? */
460 /* Unconditional jump */
461 E = E->JumpTo->Owner;
462 Index = -1; /* Invalidate */
465 /* Jump outside means we're done */
469 /* In case of conditional branches, follow the branch if possible and
470 * follow the normal flow (branch not taken) afterwards. If we cannot
471 * follow the branch, we're done.
473 } else if ((E->Info & OF_CBRA) != 0) {
475 /* Recursively determine register usage at the branch target */
481 /* Jump to internal label */
482 U1 = GetRegInfo2 (S, E->JumpTo->Owner, -1, Visited, Used, Unused, Wanted);
486 /* Jump to external label. This will effectively exit the
487 * function, so we use the exitregs information here.
493 /* Get the next entry */
495 Index = CS_GetEntryIndex (S, E);
497 if ((E = CS_GetEntry (S, ++Index)) == 0) {
498 Internal ("GetRegInfo2: No next entry!");
501 /* Follow flow if branch not taken */
502 U2 = GetRegInfo2 (S, E, Index, Visited, Used, Unused, Wanted);
504 /* Registers are used if they're use in any of the branches */
509 /* Just go to the next instruction */
511 Index = CS_GetEntryIndex (S, E);
513 E = CS_GetEntry (S, ++Index);
516 Internal ("GetRegInfo2: No next entry!");
523 /* Return to the caller the complement of all unused registers */
529 static unsigned GetRegInfo1 (CodeSeg* S,
536 /* Recursively called subfunction for GetRegInfo. */
538 /* Remember the current count of the line collection */
539 unsigned Count = CollCount (Visited);
541 /* Call the worker routine */
542 unsigned R = GetRegInfo2 (S, E, Index, Visited, Used, Unused, Wanted);
544 /* Restore the old count, unmarking all new entries */
545 unsigned NewCount = CollCount (Visited);
546 while (NewCount-- > Count) {
547 CodeEntry* E = CollAt (Visited, NewCount);
549 CollDelete (Visited, NewCount);
552 /* Return the registers used */
558 unsigned GetRegInfo (struct CodeSeg* S, unsigned Index, unsigned Wanted)
559 /* Determine register usage information for the instructions starting at the
564 Collection Visited; /* Visited entries */
567 /* Get the code entry for the given index */
568 if (Index >= CS_GetEntryCount (S)) {
569 /* There is no such code entry */
572 E = CS_GetEntry (S, Index);
574 /* Initialize the data structure used to collection information */
575 InitCollection (&Visited);
577 /* Call the recursive subfunction */
578 R = GetRegInfo1 (S, E, Index, &Visited, REG_NONE, REG_NONE, Wanted);
580 /* Delete the line collection */
581 DoneCollection (&Visited);
583 /* Return the registers used */
589 int RegAUsed (struct CodeSeg* S, unsigned Index)
590 /* Check if the value in A is used. */
592 return (GetRegInfo (S, Index, REG_A) & REG_A) != 0;
597 int RegXUsed (struct CodeSeg* S, unsigned Index)
598 /* Check if the value in X is used. */
600 return (GetRegInfo (S, Index, REG_X) & REG_X) != 0;
605 int RegYUsed (struct CodeSeg* S, unsigned Index)
606 /* Check if the value in Y is used. */
608 return (GetRegInfo (S, Index, REG_Y) & REG_Y) != 0;
613 int RegAXUsed (struct CodeSeg* S, unsigned Index)
614 /* Check if the value in A or(!) the value in X are used. */
616 return (GetRegInfo (S, Index, REG_AX) & REG_AX) != 0;
621 int RegEAXUsed (struct CodeSeg* S, unsigned Index)
622 /* Check if any of the four bytes in EAX are used. */
624 return (GetRegInfo (S, Index, REG_EAX) & REG_EAX) != 0;
629 unsigned GetKnownReg (unsigned Use, const RegContents* RC)
630 /* Return the register or zero page location from the set in Use, thats
631 * contents are known. If Use does not contain any register, or if the
632 * register in question does not have a known value, return REG_NONE.
635 if ((Use & REG_A) != 0) {
636 return (RC == 0 || RC->RegA >= 0)? REG_A : REG_NONE;
637 } else if ((Use & REG_X) != 0) {
638 return (RC == 0 || RC->RegX >= 0)? REG_X : REG_NONE;
639 } else if ((Use & REG_Y) != 0) {
640 return (RC == 0 || RC->RegY >= 0)? REG_Y : REG_NONE;
641 } else if ((Use & REG_TMP1) != 0) {
642 return (RC == 0 || RC->Tmp1 >= 0)? REG_TMP1 : REG_NONE;
643 } else if ((Use & REG_PTR1_LO) != 0) {
644 return (RC == 0 || RC->Ptr1Lo >= 0)? REG_PTR1_LO : REG_NONE;
645 } else if ((Use & REG_PTR1_HI) != 0) {
646 return (RC == 0 || RC->Ptr1Hi >= 0)? REG_PTR1_HI : REG_NONE;
647 } else if ((Use & REG_SREG_LO) != 0) {
648 return (RC == 0 || RC->SRegLo >= 0)? REG_SREG_LO : REG_NONE;
649 } else if ((Use & REG_SREG_HI) != 0) {
650 return (RC == 0 || RC->SRegHi >= 0)? REG_SREG_HI : REG_NONE;
658 static cmp_t FindCmpCond (const char* Code, unsigned CodeLen)
659 /* Search for a compare condition by the given code using the given length */
664 for (I = 0; I < sizeof (CmpSuffixTab) / sizeof (CmpSuffixTab [0]); ++I) {
665 if (strncmp (Code, CmpSuffixTab [I], CodeLen) == 0) {
677 cmp_t FindBoolCmpCond (const char* Name)
678 /* Check if the given string is the name of one of the boolean transformer
679 * subroutine, and if so, return the condition that is evaluated by this
680 * routine. Return CMP_INV if the condition is not recognised.
683 /* Check for the correct subroutine name */
684 if (strncmp (Name, "bool", 4) == 0) {
685 /* Name is ok, search for the code in the table */
686 return FindCmpCond (Name+4, strlen(Name)-4);
695 cmp_t FindTosCmpCond (const char* Name)
696 /* Check if this is a call to one of the TOS compare functions (tosgtax).
697 * Return the condition code or CMP_INV on failure.
700 unsigned Len = strlen (Name);
702 /* Check for the correct subroutine name */
703 if (strncmp (Name, "tos", 3) == 0 && strcmp (Name+Len-2, "ax") == 0) {
704 /* Name is ok, search for the code in the table */
705 return FindCmpCond (Name+3, Len-3-2);