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 /*****************************************************************************/
53 /*****************************************************************************/
55 /*****************************************************************************/
59 /* Table with the compare suffixes */
60 static const char CmpSuffixTab [][4] = {
61 "eq", "ne", "gt", "ge", "lt", "le", "ugt", "uge", "ult", "ule"
64 /* Table listing the function names and code info values for known internally
65 * used functions. This table should get auto-generated in the future.
67 typedef struct FuncInfo FuncInfo;
69 const char* Name; /* Function name */
70 unsigned short Use; /* Register usage */
71 unsigned short Chg; /* Changed/destroyed registers */
74 static const FuncInfo FuncInfoTable[] = {
75 { "addysp", REG_Y, REG_NONE },
76 { "aslax1", REG_AX, REG_AX | REG_TMP1 },
77 { "aslax2", REG_AX, REG_AX | REG_TMP1 },
78 { "aslax3", REG_AX, REG_AX | REG_TMP1 },
79 { "aslax4", REG_AX, REG_AX | REG_TMP1 },
80 { "bnega", REG_A, REG_AX },
81 { "bnegax", REG_AX, REG_AX },
82 { "bnegeax", REG_EAX, REG_EAX },
83 { "booleq", REG_NONE, REG_AX },
84 { "boolge", REG_NONE, REG_AX },
85 { "boolgt", REG_NONE, REG_AX },
86 { "boolle", REG_NONE, REG_AX },
87 { "boollt", REG_NONE, REG_AX },
88 { "boolne", REG_NONE, REG_AX },
89 { "booluge", REG_NONE, REG_AX },
90 { "boolugt", REG_NONE, REG_AX },
91 { "boolule", REG_NONE, REG_AX },
92 { "boolult", REG_NONE, REG_AX },
93 { "complax", REG_AX, REG_AX },
94 { "decax1", REG_AX, REG_AX },
95 { "decax2", REG_AX, REG_AX },
96 { "decax3", REG_AX, REG_AX },
97 { "decax4", REG_AX, REG_AX },
98 { "decax5", REG_AX, REG_AX },
99 { "decax6", REG_AX, REG_AX },
100 { "decax7", REG_AX, REG_AX },
101 { "decax8", REG_AX, REG_AX },
102 { "decaxy", REG_AXY, REG_AX | REG_TMP1 },
103 { "deceaxy", REG_EAXY, REG_EAX },
104 { "decsp1", REG_NONE, REG_Y },
105 { "decsp2", REG_NONE, REG_A },
106 { "decsp3", REG_NONE, REG_A },
107 { "decsp4", REG_NONE, REG_A },
108 { "decsp5", REG_NONE, REG_A },
109 { "decsp6", REG_NONE, REG_A },
110 { "decsp7", REG_NONE, REG_A },
111 { "decsp8", REG_NONE, REG_A },
112 { "incax1", REG_AX, REG_AX },
113 { "incax2", REG_AX, REG_AX },
114 { "incsp1", REG_NONE, REG_NONE },
115 { "incsp2", REG_NONE, REG_Y },
116 { "incsp3", REG_NONE, REG_Y },
117 { "incsp4", REG_NONE, REG_Y },
118 { "incsp5", REG_NONE, REG_Y },
119 { "incsp6", REG_NONE, REG_Y },
120 { "incsp7", REG_NONE, REG_Y },
121 { "incsp8", REG_NONE, REG_Y },
122 { "laddeq", REG_EAXY|REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
123 { "laddeq1", REG_Y | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
124 { "laddeqa", REG_AY | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
125 { "ldaidx", REG_AXY, REG_AX | REG_PTR1 },
126 { "ldauidx", REG_AXY, REG_AX | REG_PTR1 },
127 { "ldax0sp", REG_Y, REG_AX },
128 { "ldaxi", REG_AX, REG_AXY | REG_PTR1 },
129 { "ldaxidx", REG_AXY, REG_AXY | REG_PTR1 },
130 { "ldaxysp", REG_Y, REG_AXY },
131 { "leaasp", REG_A, REG_AX },
132 { "lsubeq", REG_EAXY|REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
133 { "lsubeq0sp", REG_EAX, REG_EAXY },
134 { "lsubeq1", REG_Y | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
135 { "lsubeqa", REG_AY | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
136 { "lsubeqysp", REG_EAXY, REG_EAXY },
137 { "negax", REG_AX, REG_AX },
138 { "pusha", REG_A, REG_Y },
139 { "pusha0", REG_A, REG_XY },
140 { "pushax", REG_AX, REG_Y },
141 { "pusheax", REG_EAX, REG_Y },
142 { "pushw0sp", REG_NONE, REG_AXY },
143 { "pushwysp", REG_Y, REG_AXY },
144 { "shlax1", REG_AX, REG_AX | REG_TMP1 },
145 { "shlax2", REG_AX, REG_AX | REG_TMP1 },
146 { "shlax3", REG_AX, REG_AX | REG_TMP1 },
147 { "shlax4", REG_AX, REG_AX | REG_TMP1 },
148 { "shrax1", REG_AX, REG_AX | REG_TMP1 },
149 { "shrax2", REG_AX, REG_AX | REG_TMP1 },
150 { "shrax3", REG_AX, REG_AX | REG_TMP1 },
151 { "shrax4", REG_AX, REG_AX | REG_TMP1 },
152 { "shreax1", REG_EAX, REG_AX | REG_TMP1 },
153 { "shreax2", REG_EAX, REG_AX | REG_TMP1 },
154 { "shreax3", REG_EAX, REG_AX | REG_TMP1 },
155 { "shreax4", REG_EAX, REG_AX | REG_TMP1 },
156 { "staspidx", REG_A | REG_Y, REG_Y | REG_TMP1 | REG_PTR1 },
157 { "stax0sp", REG_AX, REG_Y },
158 { "staxysp", REG_AXY, REG_Y },
159 { "subeq0sp", REG_AX, REG_AXY },
160 { "subeqysp", REG_AXY, REG_AXY },
161 { "tsteax", REG_EAX, REG_Y },
162 { "tosadda0", REG_A, REG_AXY },
163 { "tosaddax", REG_AX, REG_AXY },
164 { "tosicmp", REG_AX, REG_AXY | REG_SREG },
165 { "tosdiva0", REG_AX, REG_ALL },
166 { "tosdivax", REG_AX, REG_ALL },
167 { "tosdiveax", REG_EAX, REG_ALL },
168 { "toseqeax", REG_EAX, REG_AXY | REG_PTR1 },
169 { "tosgeeax", REG_EAX, REG_AXY | REG_PTR1 },
170 { "tosgteax", REG_EAX, REG_AXY | REG_PTR1 },
171 { "toslcmp", REG_EAX, REG_A | REG_Y | REG_PTR1 },
172 { "tosleeax", REG_EAX, REG_AXY | REG_PTR1 },
173 { "toslteax", REG_EAX, REG_AXY | REG_PTR1 },
174 { "tosmula0", REG_AX, REG_ALL },
175 { "tosmulax", REG_AX, REG_ALL },
176 { "tosmuleax", REG_EAX, REG_ALL },
177 { "tosneeax", REG_EAX, REG_AXY | REG_PTR1 },
178 { "tosshreax", REG_EAX, REG_EAXY | REG_PTR1 | REG_PTR2 },
179 { "tossuba0", REG_A, REG_AXY },
180 { "tossubax", REG_AX, REG_AXY },
181 { "tossubeax", REG_EAX, REG_EAXY },
182 { "tosugeeax", REG_EAX, REG_AXY | REG_PTR1 },
183 { "tosugteax", REG_EAX, REG_AXY | REG_PTR1 },
184 { "tosuleeax", REG_EAX, REG_AXY | REG_PTR1 },
185 { "tosulteax", REG_EAX, REG_AXY | REG_PTR1 },
186 { "tosumula0", REG_AX, REG_ALL },
187 { "tosumulax", REG_AX, REG_ALL },
188 { "tosumuleax", REG_EAX, REG_ALL },
189 { "utsteax", REG_EAX, REG_Y },
191 #define FuncInfoCount (sizeof(FuncInfoTable) / sizeof(FuncInfoTable[0]))
193 /* Table with names of zero page locations used by the compiler */
194 static const ZPInfo ZPInfoTable[] = {
195 { 0, "ptr1", REG_PTR1_LO, REG_PTR1 },
196 { 0, "ptr1+1", REG_PTR1_HI, REG_PTR1 },
197 { 0, "ptr2", REG_PTR2_LO, REG_PTR2 },
198 { 0, "ptr2+1", REG_PTR2_HI, REG_PTR2 },
199 { 4, "ptr3", REG_NONE, REG_NONE },
200 { 4, "ptr4", REG_NONE, REG_NONE },
201 { 7, "regbank", REG_NONE, REG_NONE },
202 { 0, "regsave", REG_SAVE_LO, REG_SAVE },
203 { 0, "regsave+1", REG_SAVE_HI, REG_SAVE },
204 { 0, "sp", REG_SP_LO, REG_SP },
205 { 0, "sp+1", REG_SP_HI, REG_SP },
206 { 0, "sreg", REG_SREG_LO, REG_SREG },
207 { 0, "sreg+1", REG_SREG_HI, REG_SREG },
208 { 0, "tmp1", REG_TMP1, REG_TMP1 },
209 { 0, "tmp2", REG_NONE, REG_NONE },
210 { 0, "tmp3", REG_NONE, REG_NONE },
211 { 0, "tmp4", REG_NONE, REG_NONE },
213 #define ZPInfoCount (sizeof(ZPInfoTable) / sizeof(ZPInfoTable[0]))
217 /*****************************************************************************/
219 /*****************************************************************************/
223 static int CompareFuncInfo (const void* Key, const void* Info)
224 /* Compare function for bsearch */
226 return strcmp (Key, ((const FuncInfo*) Info)->Name);
231 void GetFuncInfo (const char* Name, unsigned short* Use, unsigned short* Chg)
232 /* For the given function, lookup register information and store it into
233 * the given variables. If the function is unknown, assume it will use and
234 * load all registers.
237 /* If the function name starts with an underline, it is an external
238 * function. Search for it in the symbol table. If the function does
239 * not start with an underline, it may be a runtime support function.
240 * Search for it in the list of builtin functions.
242 if (Name[0] == '_') {
244 /* Search in the symbol table, skip the leading underscore */
245 SymEntry* E = FindGlobalSym (Name+1);
247 /* Did we find it in the top level table? */
248 if (E && IsTypeFunc (E->Type)) {
250 /* A function may use the A or A/X registers if it is a fastcall
251 * function. If it is not a fastcall function but a variadic one,
252 * it will use the Y register (the parameter size is passed here).
253 * In all other cases, no registers are used. However, we assume
254 * that any function will destroy all registers.
256 FuncDesc* D = E->V.F.Func;
257 if ((D->Flags & FD_FASTCALL) != 0 && D->ParamCount > 0) {
258 /* Will use registers depending on the last param */
259 SymEntry* LastParam = D->SymTab->SymTail;
260 unsigned LastParamSize = CheckedSizeOf (LastParam->Type);
261 if (LastParamSize == 1) {
263 } else if (LastParamSize == 2) {
268 } else if ((D->Flags & FD_VARIADIC) != 0) {
271 /* Will not use any registers */
275 /* Will destroy all registers */
284 /* Search for the function in the list of builtin functions */
285 const FuncInfo* Info = bsearch (Name, FuncInfoTable, FuncInfoCount,
286 sizeof(FuncInfo), CompareFuncInfo);
288 /* Do we know the function? */
290 /* Use the information we have */
297 /* Function not found - assume that the primary register is input, and all
298 * registers are changed
306 static int CompareZPInfo (const void* Name, const void* Info)
307 /* Compare function for bsearch */
309 /* Cast the pointers to the correct data type */
310 const char* N = (const char*) Name;
311 const ZPInfo* E = (const ZPInfo*) Info;
313 /* Do the compare. Be careful because of the length (Info may contain
314 * more than just the zeropage name).
317 /* Do a full compare */
318 return strcmp (N, E->Name);
320 /* Only compare the first part */
321 int Res = strncmp (N, E->Name, E->Len);
322 if (Res == 0 && (N[E->Len] != '\0' && N[E->Len] != '+')) {
323 /* Name is actually longer than Info->Name */
332 const ZPInfo* GetZPInfo (const char* Name)
333 /* If the given name is a zero page symbol, return a pointer to the info
334 * struct for this symbol, otherwise return NULL.
337 /* Search for the zp location in the list */
338 return bsearch (Name, ZPInfoTable, ZPInfoCount,
339 sizeof(ZPInfo), CompareZPInfo);
344 static unsigned GetRegInfo2 (CodeSeg* S,
351 /* Recursively called subfunction for GetRegInfo. */
353 /* Follow the instruction flow recording register usage. */
358 /* Check if we have already visited the current code entry. If so,
361 if (CE_HasMark (E)) {
365 /* Mark this entry as already visited */
367 CollAppend (Visited, E);
369 /* Evaluate the used registers */
371 if (E->OPC == OP65_RTS ||
372 ((E->Info & OF_BRA) != 0 && E->JumpTo == 0)) {
373 /* This instruction will leave the function */
377 /* We are not interested in the use of any register that has been
381 /* Remember the remaining registers */
385 /* Evaluate the changed registers */
386 if ((R = E->Chg) != REG_NONE) {
387 /* We are not interested in the use of any register that has been
391 /* Remember the remaining registers */
395 /* If we know about all registers now, bail out */
396 if (((Used | Unused) & Wanted) == Wanted) {
400 /* If the instruction is an RTS or RTI, we're done */
401 if ((E->Info & OF_RET) != 0) {
405 /* If we have an unconditional branch, follow this branch if possible,
406 * otherwise we're done.
408 if ((E->Info & OF_UBRA) != 0) {
410 /* Does this jump have a valid target? */
413 /* Unconditional jump */
414 E = E->JumpTo->Owner;
415 Index = -1; /* Invalidate */
418 /* Jump outside means we're done */
422 /* In case of conditional branches, follow the branch if possible and
423 * follow the normal flow (branch not taken) afterwards. If we cannot
424 * follow the branch, we're done.
426 } else if ((E->Info & OF_CBRA) != 0) {
430 /* Recursively determine register usage at the branch target */
434 U1 = GetRegInfo2 (S, E->JumpTo->Owner, -1, Visited, Used, Unused, Wanted);
436 /* All registers used, no need for second call */
440 Index = CS_GetEntryIndex (S, E);
442 if ((E = CS_GetEntry (S, ++Index)) == 0) {
443 Internal ("GetRegInfo2: No next entry!");
445 U2 = GetRegInfo2 (S, E, Index, Visited, Used, Unused, Wanted);
446 return U1 | U2; /* Used in any of the branches */
449 /* Jump to global symbol */
455 /* Just go to the next instruction */
457 Index = CS_GetEntryIndex (S, E);
459 E = CS_GetEntry (S, ++Index);
462 Internal ("GetRegInfo2: No next entry!");
469 /* Return to the caller the complement of all unused registers */
475 static unsigned GetRegInfo1 (CodeSeg* S,
482 /* Recursively called subfunction for GetRegInfo. */
484 /* Remember the current count of the line collection */
485 unsigned Count = CollCount (Visited);
487 /* Call the worker routine */
488 unsigned R = GetRegInfo2 (S, E, Index, Visited, Used, Unused, Wanted);
490 /* Restore the old count, unmarking all new entries */
491 unsigned NewCount = CollCount (Visited);
492 while (NewCount-- > Count) {
493 CodeEntry* E = CollAt (Visited, NewCount);
495 CollDelete (Visited, NewCount);
498 /* Return the registers used */
504 unsigned GetRegInfo (struct CodeSeg* S, unsigned Index, unsigned Wanted)
505 /* Determine register usage information for the instructions starting at the
510 Collection Visited; /* Visited entries */
513 /* Get the code entry for the given index */
514 if (Index >= CS_GetEntryCount (S)) {
515 /* There is no such code entry */
518 E = CS_GetEntry (S, Index);
520 /* Initialize the data structure used to collection information */
521 InitCollection (&Visited);
523 /* Call the recursive subfunction */
524 R = GetRegInfo1 (S, E, Index, &Visited, REG_NONE, REG_NONE, Wanted);
526 /* Delete the line collection */
527 DoneCollection (&Visited);
529 /* Return the registers used */
535 int RegAUsed (struct CodeSeg* S, unsigned Index)
536 /* Check if the value in A is used. */
538 return (GetRegInfo (S, Index, REG_A) & REG_A) != 0;
543 int RegXUsed (struct CodeSeg* S, unsigned Index)
544 /* Check if the value in X is used. */
546 return (GetRegInfo (S, Index, REG_X) & REG_X) != 0;
551 int RegYUsed (struct CodeSeg* S, unsigned Index)
552 /* Check if the value in Y is used. */
554 return (GetRegInfo (S, Index, REG_Y) & REG_Y) != 0;
559 int RegAXUsed (struct CodeSeg* S, unsigned Index)
560 /* Check if the value in A or(!) the value in X are used. */
562 return (GetRegInfo (S, Index, REG_AX) & REG_AX) != 0;
567 unsigned GetKnownReg (unsigned Use, const RegContents* RC)
568 /* Return the register or zero page location from the set in Use, thats
569 * contents are known. If Use does not contain any register, or if the
570 * register in question does not have a known value, return REG_NONE.
573 if ((Use & REG_A) != 0) {
574 return (RC == 0 || RC->RegA >= 0)? REG_A : REG_NONE;
575 } else if ((Use & REG_X) != 0) {
576 return (RC == 0 || RC->RegX >= 0)? REG_X : REG_NONE;
577 } else if ((Use & REG_Y) != 0) {
578 return (RC == 0 || RC->RegY >= 0)? REG_Y : REG_NONE;
579 } else if ((Use & REG_TMP1) != 0) {
580 return (RC == 0 || RC->Tmp1 >= 0)? REG_TMP1 : REG_NONE;
581 } else if ((Use & REG_SREG_LO) != 0) {
582 return (RC == 0 || RC->SRegLo >= 0)? REG_SREG_LO : REG_NONE;
583 } else if ((Use & REG_SREG_HI) != 0) {
584 return (RC == 0 || RC->SRegHi >= 0)? REG_SREG_HI : REG_NONE;
592 static cmp_t FindCmpCond (const char* Code, unsigned CodeLen)
593 /* Search for a compare condition by the given code using the given length */
598 for (I = 0; I < sizeof (CmpSuffixTab) / sizeof (CmpSuffixTab [0]); ++I) {
599 if (strncmp (Code, CmpSuffixTab [I], CodeLen) == 0) {
611 cmp_t FindBoolCmpCond (const char* Name)
612 /* Check if the given string is the name of one of the boolean transformer
613 * subroutine, and if so, return the condition that is evaluated by this
614 * routine. Return CMP_INV if the condition is not recognised.
617 /* Check for the correct subroutine name */
618 if (strncmp (Name, "bool", 4) == 0) {
619 /* Name is ok, search for the code in the table */
620 return FindCmpCond (Name+4, strlen(Name)-4);
629 cmp_t FindTosCmpCond (const char* Name)
630 /* Check if this is a call to one of the TOS compare functions (tosgtax).
631 * Return the condition code or CMP_INV on failure.
634 unsigned Len = strlen (Name);
636 /* Check for the correct subroutine name */
637 if (strncmp (Name, "tos", 3) == 0 && strcmp (Name+Len-2, "ax") == 0) {
638 /* Name is ok, search for the code in the table */
639 return FindCmpCond (Name+3, Len-3-2);