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 { "addeq0sp", REG_AX, REG_AXY },
76 { "addeqysp", REG_AXY, REG_AXY },
77 { "addysp", REG_Y, REG_NONE },
78 { "aslax1", REG_AX, REG_AX | REG_TMP1 },
79 { "aslax2", REG_AX, REG_AX | REG_TMP1 },
80 { "aslax3", REG_AX, REG_AX | REG_TMP1 },
81 { "aslax4", REG_AX, REG_AX | REG_TMP1 },
82 { "bnega", REG_A, REG_AX },
83 { "bnegax", REG_AX, REG_AX },
84 { "bnegeax", REG_EAX, REG_EAX },
85 { "booleq", REG_NONE, REG_AX },
86 { "boolge", REG_NONE, REG_AX },
87 { "boolgt", REG_NONE, REG_AX },
88 { "boolle", REG_NONE, REG_AX },
89 { "boollt", REG_NONE, REG_AX },
90 { "boolne", REG_NONE, REG_AX },
91 { "booluge", REG_NONE, REG_AX },
92 { "boolugt", REG_NONE, REG_AX },
93 { "boolule", REG_NONE, REG_AX },
94 { "boolult", REG_NONE, REG_AX },
95 { "complax", REG_AX, REG_AX },
96 { "decax1", REG_AX, REG_AX },
97 { "decax2", REG_AX, REG_AX },
98 { "decax3", REG_AX, REG_AX },
99 { "decax4", REG_AX, REG_AX },
100 { "decax5", REG_AX, REG_AX },
101 { "decax6", REG_AX, REG_AX },
102 { "decax7", REG_AX, REG_AX },
103 { "decax8", REG_AX, REG_AX },
104 { "decaxy", REG_AXY, REG_AX | REG_TMP1 },
105 { "deceaxy", REG_EAXY, REG_EAX },
106 { "decsp1", REG_NONE, REG_Y },
107 { "decsp2", REG_NONE, REG_A },
108 { "decsp3", REG_NONE, REG_A },
109 { "decsp4", REG_NONE, REG_A },
110 { "decsp5", REG_NONE, REG_A },
111 { "decsp6", REG_NONE, REG_A },
112 { "decsp7", REG_NONE, REG_A },
113 { "decsp8", REG_NONE, REG_A },
114 { "incax1", REG_AX, REG_AX },
115 { "incax2", REG_AX, REG_AX },
116 { "incsp1", REG_NONE, REG_NONE },
117 { "incsp2", REG_NONE, REG_Y },
118 { "incsp3", REG_NONE, REG_Y },
119 { "incsp4", REG_NONE, REG_Y },
120 { "incsp5", REG_NONE, REG_Y },
121 { "incsp6", REG_NONE, REG_Y },
122 { "incsp7", REG_NONE, REG_Y },
123 { "incsp8", REG_NONE, REG_Y },
124 { "laddeq", REG_EAXY|REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
125 { "laddeq1", REG_Y | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
126 { "laddeqa", REG_AY | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
127 { "ldaidx", REG_AXY, REG_AX | REG_PTR1 },
128 { "ldauidx", REG_AXY, REG_AX | REG_PTR1 },
129 { "ldax0sp", REG_NONE, REG_AXY },
130 { "ldaxi", REG_AX, REG_AXY | REG_PTR1 },
131 { "ldaxidx", REG_AXY, REG_AXY | REG_PTR1 },
132 { "ldaxysp", REG_Y, REG_AXY },
133 { "leaasp", REG_A, REG_AX },
134 { "lsubeq", REG_EAXY|REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
135 { "lsubeq0sp", REG_EAX, REG_EAXY },
136 { "lsubeq1", REG_Y | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
137 { "lsubeqa", REG_AY | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
138 { "lsubeqysp", REG_EAXY, REG_EAXY },
139 { "negax", REG_AX, REG_AX },
140 { "push0", REG_NONE, REG_AXY },
141 { "push1", REG_NONE, REG_AXY },
142 { "push2", REG_NONE, REG_AXY },
143 { "push3", REG_NONE, REG_AXY },
144 { "push4", REG_NONE, REG_AXY },
145 { "push5", REG_NONE, REG_AXY },
146 { "push6", REG_NONE, REG_AXY },
147 { "push7", REG_NONE, REG_AXY },
148 { "pusha", REG_A, REG_Y },
149 { "pusha0", REG_A, REG_XY },
150 { "pushax", REG_AX, REG_Y },
151 { "pushc0", REG_NONE, REG_A | REG_Y },
152 { "pushc1", REG_NONE, REG_A | REG_Y },
153 { "pushc2", REG_NONE, REG_A | REG_Y },
154 { "pusheax", REG_EAX, REG_Y },
155 { "pushw0sp", REG_NONE, REG_AXY },
156 { "pushwysp", REG_Y, REG_AXY },
157 { "shlax1", REG_AX, REG_AX | REG_TMP1 },
158 { "shlax2", REG_AX, REG_AX | REG_TMP1 },
159 { "shlax3", REG_AX, REG_AX | REG_TMP1 },
160 { "shlax4", REG_AX, REG_AX | REG_TMP1 },
161 { "shrax1", REG_AX, REG_AX | REG_TMP1 },
162 { "shrax2", REG_AX, REG_AX | REG_TMP1 },
163 { "shrax3", REG_AX, REG_AX | REG_TMP1 },
164 { "shrax4", REG_AX, REG_AX | REG_TMP1 },
165 { "shreax1", REG_EAX, REG_AX | REG_TMP1 },
166 { "shreax2", REG_EAX, REG_AX | REG_TMP1 },
167 { "shreax3", REG_EAX, REG_AX | REG_TMP1 },
168 { "shreax4", REG_EAX, REG_AX | REG_TMP1 },
169 { "staspidx", REG_A | REG_Y, REG_Y | REG_TMP1 | REG_PTR1 },
170 { "stax0sp", REG_AX, REG_Y },
171 { "staxysp", REG_AXY, REG_Y },
172 { "subeq0sp", REG_AX, REG_AXY },
173 { "subeqysp", REG_AXY, REG_AXY },
174 { "tosadda0", REG_A, REG_AXY },
175 { "tosaddax", REG_AX, REG_AXY },
176 { "tosdiva0", REG_AX, REG_ALL },
177 { "tosdivax", REG_AX, REG_ALL },
178 { "tosdiveax", REG_EAX, REG_ALL },
179 { "toseqeax", REG_EAX, REG_AXY | REG_PTR1 },
180 { "tosgeeax", REG_EAX, REG_AXY | REG_PTR1 },
181 { "tosgteax", REG_EAX, REG_AXY | REG_PTR1 },
182 { "tosicmp", REG_AX, REG_AXY | REG_SREG },
183 { "toslcmp", REG_EAX, REG_A | REG_Y | REG_PTR1 },
184 { "tosleeax", REG_EAX, REG_AXY | REG_PTR1 },
185 { "toslteax", REG_EAX, REG_AXY | REG_PTR1 },
186 { "tosmula0", REG_AX, REG_ALL },
187 { "tosmulax", REG_AX, REG_ALL },
188 { "tosmuleax", REG_EAX, REG_ALL },
189 { "tosneeax", REG_EAX, REG_AXY | REG_PTR1 },
190 { "tosshreax", REG_EAX, REG_EAXY | REG_PTR1 | REG_PTR2 },
191 { "tossuba0", REG_A, REG_AXY },
192 { "tossubax", REG_AX, REG_AXY },
193 { "tossubeax", REG_EAX, REG_EAXY },
194 { "tosugeeax", REG_EAX, REG_AXY | REG_PTR1 },
195 { "tosugteax", REG_EAX, REG_AXY | REG_PTR1 },
196 { "tosuleeax", REG_EAX, REG_AXY | REG_PTR1 },
197 { "tosulteax", REG_EAX, REG_AXY | REG_PTR1 },
198 { "tosumula0", REG_AX, REG_ALL },
199 { "tosumulax", REG_AX, REG_ALL },
200 { "tosumuleax", REG_EAX, REG_ALL },
201 { "tsteax", REG_EAX, REG_Y },
202 { "utsteax", REG_EAX, REG_Y },
204 #define FuncInfoCount (sizeof(FuncInfoTable) / sizeof(FuncInfoTable[0]))
206 /* Table with names of zero page locations used by the compiler */
207 static const ZPInfo ZPInfoTable[] = {
208 { 0, "ptr1", REG_PTR1_LO, REG_PTR1 },
209 { 0, "ptr1+1", REG_PTR1_HI, REG_PTR1 },
210 { 0, "ptr2", REG_PTR2_LO, REG_PTR2 },
211 { 0, "ptr2+1", REG_PTR2_HI, REG_PTR2 },
212 { 4, "ptr3", REG_NONE, REG_NONE },
213 { 4, "ptr4", REG_NONE, REG_NONE },
214 { 7, "regbank", REG_NONE, REG_NONE },
215 { 0, "regsave", REG_SAVE_LO, REG_SAVE },
216 { 0, "regsave+1", REG_SAVE_HI, REG_SAVE },
217 { 0, "sp", REG_SP_LO, REG_SP },
218 { 0, "sp+1", REG_SP_HI, REG_SP },
219 { 0, "sreg", REG_SREG_LO, REG_SREG },
220 { 0, "sreg+1", REG_SREG_HI, REG_SREG },
221 { 0, "tmp1", REG_TMP1, REG_TMP1 },
222 { 0, "tmp2", REG_NONE, REG_NONE },
223 { 0, "tmp3", REG_NONE, REG_NONE },
224 { 0, "tmp4", REG_NONE, REG_NONE },
226 #define ZPInfoCount (sizeof(ZPInfoTable) / sizeof(ZPInfoTable[0]))
230 /*****************************************************************************/
232 /*****************************************************************************/
236 static int CompareFuncInfo (const void* Key, const void* Info)
237 /* Compare function for bsearch */
239 return strcmp (Key, ((const FuncInfo*) Info)->Name);
244 void GetFuncInfo (const char* Name, unsigned short* Use, unsigned short* Chg)
245 /* For the given function, lookup register information and store it into
246 * the given variables. If the function is unknown, assume it will use and
247 * load all registers.
250 /* If the function name starts with an underline, it is an external
251 * function. Search for it in the symbol table. If the function does
252 * not start with an underline, it may be a runtime support function.
253 * Search for it in the list of builtin functions.
255 if (Name[0] == '_') {
257 /* Search in the symbol table, skip the leading underscore */
258 SymEntry* E = FindGlobalSym (Name+1);
260 /* Did we find it in the top level table? */
261 if (E && IsTypeFunc (E->Type)) {
263 /* A function may use the A or A/X registers if it is a fastcall
264 * function. If it is not a fastcall function but a variadic one,
265 * it will use the Y register (the parameter size is passed here).
266 * In all other cases, no registers are used. However, we assume
267 * that any function will destroy all registers.
269 FuncDesc* D = E->V.F.Func;
270 if ((D->Flags & FD_FASTCALL) != 0 && D->ParamCount > 0) {
271 /* Will use registers depending on the last param */
272 SymEntry* LastParam = D->SymTab->SymTail;
273 unsigned LastParamSize = CheckedSizeOf (LastParam->Type);
274 if (LastParamSize == 1) {
276 } else if (LastParamSize == 2) {
281 } else if ((D->Flags & FD_VARIADIC) != 0) {
284 /* Will not use any registers */
288 /* Will destroy all registers */
297 /* Search for the function in the list of builtin functions */
298 const FuncInfo* Info = bsearch (Name, FuncInfoTable, FuncInfoCount,
299 sizeof(FuncInfo), CompareFuncInfo);
301 /* Do we know the function? */
303 /* Use the information we have */
310 /* Function not found - assume that the primary register is input, and all
311 * registers are changed
319 static int CompareZPInfo (const void* Name, const void* Info)
320 /* Compare function for bsearch */
322 /* Cast the pointers to the correct data type */
323 const char* N = (const char*) Name;
324 const ZPInfo* E = (const ZPInfo*) Info;
326 /* Do the compare. Be careful because of the length (Info may contain
327 * more than just the zeropage name).
330 /* Do a full compare */
331 return strcmp (N, E->Name);
333 /* Only compare the first part */
334 int Res = strncmp (N, E->Name, E->Len);
335 if (Res == 0 && (N[E->Len] != '\0' && N[E->Len] != '+')) {
336 /* Name is actually longer than Info->Name */
345 const ZPInfo* GetZPInfo (const char* Name)
346 /* If the given name is a zero page symbol, return a pointer to the info
347 * struct for this symbol, otherwise return NULL.
350 /* Search for the zp location in the list */
351 return bsearch (Name, ZPInfoTable, ZPInfoCount,
352 sizeof(ZPInfo), CompareZPInfo);
357 static unsigned GetRegInfo2 (CodeSeg* S,
364 /* Recursively called subfunction for GetRegInfo. */
366 /* Follow the instruction flow recording register usage. */
371 /* Check if we have already visited the current code entry. If so,
374 if (CE_HasMark (E)) {
378 /* Mark this entry as already visited */
380 CollAppend (Visited, E);
382 /* Evaluate the used registers */
384 if (E->OPC == OP65_RTS ||
385 ((E->Info & OF_BRA) != 0 && E->JumpTo == 0)) {
386 /* This instruction will leave the function */
390 /* We are not interested in the use of any register that has been
394 /* Remember the remaining registers */
398 /* Evaluate the changed registers */
399 if ((R = E->Chg) != REG_NONE) {
400 /* We are not interested in the use of any register that has been
404 /* Remember the remaining registers */
408 /* If we know about all registers now, bail out */
409 if (((Used | Unused) & Wanted) == Wanted) {
413 /* If the instruction is an RTS or RTI, we're done */
414 if ((E->Info & OF_RET) != 0) {
418 /* If we have an unconditional branch, follow this branch if possible,
419 * otherwise we're done.
421 if ((E->Info & OF_UBRA) != 0) {
423 /* Does this jump have a valid target? */
426 /* Unconditional jump */
427 E = E->JumpTo->Owner;
428 Index = -1; /* Invalidate */
431 /* Jump outside means we're done */
435 /* In case of conditional branches, follow the branch if possible and
436 * follow the normal flow (branch not taken) afterwards. If we cannot
437 * follow the branch, we're done.
439 } else if ((E->Info & OF_CBRA) != 0) {
443 /* Recursively determine register usage at the branch target */
447 U1 = GetRegInfo2 (S, E->JumpTo->Owner, -1, Visited, Used, Unused, Wanted);
449 /* All registers used, no need for second call */
453 Index = CS_GetEntryIndex (S, E);
455 if ((E = CS_GetEntry (S, ++Index)) == 0) {
456 Internal ("GetRegInfo2: No next entry!");
458 U2 = GetRegInfo2 (S, E, Index, Visited, Used, Unused, Wanted);
459 return U1 | U2; /* Used in any of the branches */
462 /* Jump to global symbol */
468 /* Just go to the next instruction */
470 Index = CS_GetEntryIndex (S, E);
472 E = CS_GetEntry (S, ++Index);
475 Internal ("GetRegInfo2: No next entry!");
482 /* Return to the caller the complement of all unused registers */
488 static unsigned GetRegInfo1 (CodeSeg* S,
495 /* Recursively called subfunction for GetRegInfo. */
497 /* Remember the current count of the line collection */
498 unsigned Count = CollCount (Visited);
500 /* Call the worker routine */
501 unsigned R = GetRegInfo2 (S, E, Index, Visited, Used, Unused, Wanted);
503 /* Restore the old count, unmarking all new entries */
504 unsigned NewCount = CollCount (Visited);
505 while (NewCount-- > Count) {
506 CodeEntry* E = CollAt (Visited, NewCount);
508 CollDelete (Visited, NewCount);
511 /* Return the registers used */
517 unsigned GetRegInfo (struct CodeSeg* S, unsigned Index, unsigned Wanted)
518 /* Determine register usage information for the instructions starting at the
523 Collection Visited; /* Visited entries */
526 /* Get the code entry for the given index */
527 if (Index >= CS_GetEntryCount (S)) {
528 /* There is no such code entry */
531 E = CS_GetEntry (S, Index);
533 /* Initialize the data structure used to collection information */
534 InitCollection (&Visited);
536 /* Call the recursive subfunction */
537 R = GetRegInfo1 (S, E, Index, &Visited, REG_NONE, REG_NONE, Wanted);
539 /* Delete the line collection */
540 DoneCollection (&Visited);
542 /* Return the registers used */
548 int RegAUsed (struct CodeSeg* S, unsigned Index)
549 /* Check if the value in A is used. */
551 return (GetRegInfo (S, Index, REG_A) & REG_A) != 0;
556 int RegXUsed (struct CodeSeg* S, unsigned Index)
557 /* Check if the value in X is used. */
559 return (GetRegInfo (S, Index, REG_X) & REG_X) != 0;
564 int RegYUsed (struct CodeSeg* S, unsigned Index)
565 /* Check if the value in Y is used. */
567 return (GetRegInfo (S, Index, REG_Y) & REG_Y) != 0;
572 int RegAXUsed (struct CodeSeg* S, unsigned Index)
573 /* Check if the value in A or(!) the value in X are used. */
575 return (GetRegInfo (S, Index, REG_AX) & REG_AX) != 0;
580 unsigned GetKnownReg (unsigned Use, const RegContents* RC)
581 /* Return the register or zero page location from the set in Use, thats
582 * contents are known. If Use does not contain any register, or if the
583 * register in question does not have a known value, return REG_NONE.
586 if ((Use & REG_A) != 0) {
587 return (RC == 0 || RC->RegA >= 0)? REG_A : REG_NONE;
588 } else if ((Use & REG_X) != 0) {
589 return (RC == 0 || RC->RegX >= 0)? REG_X : REG_NONE;
590 } else if ((Use & REG_Y) != 0) {
591 return (RC == 0 || RC->RegY >= 0)? REG_Y : REG_NONE;
592 } else if ((Use & REG_TMP1) != 0) {
593 return (RC == 0 || RC->Tmp1 >= 0)? REG_TMP1 : REG_NONE;
594 } else if ((Use & REG_SREG_LO) != 0) {
595 return (RC == 0 || RC->SRegLo >= 0)? REG_SREG_LO : REG_NONE;
596 } else if ((Use & REG_SREG_HI) != 0) {
597 return (RC == 0 || RC->SRegHi >= 0)? REG_SREG_HI : REG_NONE;
605 static cmp_t FindCmpCond (const char* Code, unsigned CodeLen)
606 /* Search for a compare condition by the given code using the given length */
611 for (I = 0; I < sizeof (CmpSuffixTab) / sizeof (CmpSuffixTab [0]); ++I) {
612 if (strncmp (Code, CmpSuffixTab [I], CodeLen) == 0) {
624 cmp_t FindBoolCmpCond (const char* Name)
625 /* Check if the given string is the name of one of the boolean transformer
626 * subroutine, and if so, return the condition that is evaluated by this
627 * routine. Return CMP_INV if the condition is not recognised.
630 /* Check for the correct subroutine name */
631 if (strncmp (Name, "bool", 4) == 0) {
632 /* Name is ok, search for the code in the table */
633 return FindCmpCond (Name+4, strlen(Name)-4);
642 cmp_t FindTosCmpCond (const char* Name)
643 /* Check if this is a call to one of the TOS compare functions (tosgtax).
644 * Return the condition code or CMP_INV on failure.
647 unsigned Len = strlen (Name);
649 /* Check for the correct subroutine name */
650 if (strncmp (Name, "tos", 3) == 0 && strcmp (Name+Len-2, "ax") == 0) {
651 /* Name is ok, search for the code in the table */
652 return FindCmpCond (Name+3, Len-3-2);