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 listing the function names and code info values for known internally
60 * used functions. This table should get auto-generated in the future.
62 typedef struct FuncInfo FuncInfo;
64 const char* Name; /* Function name */
65 unsigned short Use; /* Register usage */
66 unsigned short Chg; /* Changed/destroyed registers */
69 static const FuncInfo FuncInfoTable[] = {
70 { "addysp", REG_Y, REG_NONE },
71 { "aslax1", REG_AX, REG_AX | REG_TMP1 },
72 { "aslax2", REG_AX, REG_AX | REG_TMP1 },
73 { "aslax3", REG_AX, REG_AX | REG_TMP1 },
74 { "aslax4", REG_AX, REG_AX | REG_TMP1 },
75 { "bnega", REG_A, REG_AX },
76 { "bnegax", REG_AX, REG_AX },
77 { "bnegeax", REG_EAX, REG_EAX },
78 { "booleq", REG_NONE, REG_AX },
79 { "boolge", REG_NONE, REG_AX },
80 { "boolgt", REG_NONE, REG_AX },
81 { "boolle", REG_NONE, REG_AX },
82 { "boollt", REG_NONE, REG_AX },
83 { "boolne", REG_NONE, REG_AX },
84 { "booluge", REG_NONE, REG_AX },
85 { "boolugt", REG_NONE, REG_AX },
86 { "boolule", REG_NONE, REG_AX },
87 { "boolult", REG_NONE, REG_AX },
88 { "complax", REG_AX, REG_AX },
89 { "decax1", REG_AX, REG_AX },
90 { "decax2", REG_AX, REG_AX },
91 { "decax3", REG_AX, REG_AX },
92 { "decax4", REG_AX, REG_AX },
93 { "decax5", REG_AX, REG_AX },
94 { "decax6", REG_AX, REG_AX },
95 { "decax7", REG_AX, REG_AX },
96 { "decax8", REG_AX, REG_AX },
97 { "decaxy", REG_AXY, REG_AX | REG_TMP1 },
98 { "deceaxy", REG_EAXY, REG_EAX },
99 { "decsp1", REG_NONE, REG_Y },
100 { "decsp2", REG_NONE, REG_A },
101 { "decsp3", REG_NONE, REG_A },
102 { "decsp4", REG_NONE, REG_A },
103 { "decsp5", REG_NONE, REG_A },
104 { "decsp6", REG_NONE, REG_A },
105 { "decsp7", REG_NONE, REG_A },
106 { "decsp8", REG_NONE, REG_A },
107 { "incax1", REG_AX, REG_AX },
108 { "incax2", REG_AX, REG_AX },
109 { "incsp1", REG_NONE, REG_NONE },
110 { "incsp2", REG_NONE, REG_Y },
111 { "incsp3", REG_NONE, REG_Y },
112 { "incsp4", REG_NONE, REG_Y },
113 { "incsp5", REG_NONE, REG_Y },
114 { "incsp6", REG_NONE, REG_Y },
115 { "incsp7", REG_NONE, REG_Y },
116 { "incsp8", REG_NONE, REG_Y },
117 { "laddeq", REG_EAXY|REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
118 { "laddeq1", REG_Y | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
119 { "laddeqa", REG_AY | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
120 { "ldaidx", REG_AXY, REG_AX | REG_PTR1 },
121 { "ldauidx", REG_AXY, REG_AX | REG_PTR1 },
122 { "ldax0sp", REG_Y, REG_AX },
123 { "ldaxi", REG_AX, REG_AXY | REG_PTR1 },
124 { "ldaxidx", REG_AXY, REG_AXY | REG_PTR1 },
125 { "ldaxysp", REG_Y, REG_AXY },
126 { "leaasp", REG_A, REG_AX },
127 { "lsubeq", REG_EAXY|REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
128 { "lsubeq0sp", REG_EAX, REG_EAXY },
129 { "lsubeq1", REG_Y | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
130 { "lsubeqa", REG_AY | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
131 { "lsubeqysp", REG_EAXY, REG_EAXY },
132 { "negax", REG_AX, REG_AX },
133 { "pusha", REG_A, REG_Y },
134 { "pusha0", REG_A, REG_XY },
135 { "pushax", REG_AX, REG_Y },
136 { "pusheax", REG_EAX, REG_Y },
137 { "pushw0sp", REG_NONE, REG_AXY },
138 { "pushwysp", REG_Y, REG_AXY },
139 { "shlax1", REG_AX, REG_AX | REG_TMP1 },
140 { "shlax2", REG_AX, REG_AX | REG_TMP1 },
141 { "shlax3", REG_AX, REG_AX | REG_TMP1 },
142 { "shlax4", REG_AX, REG_AX | REG_TMP1 },
143 { "shrax1", REG_AX, REG_AX | REG_TMP1 },
144 { "shrax2", REG_AX, REG_AX | REG_TMP1 },
145 { "shrax3", REG_AX, REG_AX | REG_TMP1 },
146 { "shrax4", REG_AX, REG_AX | REG_TMP1 },
147 { "shreax1", REG_EAX, REG_AX | REG_TMP1 },
148 { "shreax2", REG_EAX, REG_AX | REG_TMP1 },
149 { "shreax3", REG_EAX, REG_AX | REG_TMP1 },
150 { "shreax4", REG_EAX, REG_AX | REG_TMP1 },
151 { "staspidx", REG_A | REG_Y, REG_Y | REG_TMP1 | REG_PTR1 },
152 { "stax0sp", REG_AX, REG_Y },
153 { "staxysp", REG_AXY, REG_Y },
154 { "subeq0sp", REG_AX, REG_AXY },
155 { "subeqysp", REG_AXY, REG_AXY },
156 { "tsteax", REG_EAX, REG_Y },
157 { "tosadda0", REG_A, REG_AXY },
158 { "tosaddax", REG_AX, REG_AXY },
159 { "tosicmp", REG_AX, REG_AXY | REG_SREG },
160 { "tosdiva0", REG_AX, REG_ALL },
161 { "tosdivax", REG_AX, REG_ALL },
162 { "tosdiveax", REG_EAX, REG_ALL },
163 { "toseqeax", REG_EAX, REG_AXY | REG_PTR1 },
164 { "tosgeeax", REG_EAX, REG_AXY | REG_PTR1 },
165 { "tosgteax", REG_EAX, REG_AXY | REG_PTR1 },
166 { "toslcmp", REG_EAX, REG_A | REG_Y | REG_PTR1 },
167 { "tosleeax", REG_EAX, REG_AXY | REG_PTR1 },
168 { "toslteax", REG_EAX, REG_AXY | REG_PTR1 },
169 { "tosmula0", REG_AX, REG_ALL },
170 { "tosmulax", REG_AX, REG_ALL },
171 { "tosmuleax", REG_EAX, REG_ALL },
172 { "tosneeax", REG_EAX, REG_AXY | REG_PTR1 },
173 { "tosshreax", REG_EAX, REG_EAXY | REG_PTR1 | REG_PTR2 },
174 { "tossuba0", REG_A, REG_AXY },
175 { "tossubax", REG_AX, REG_AXY },
176 { "tossubeax", REG_EAX, REG_EAXY },
177 { "tosugeeax", REG_EAX, REG_AXY | REG_PTR1 },
178 { "tosugteax", REG_EAX, REG_AXY | REG_PTR1 },
179 { "tosuleeax", REG_EAX, REG_AXY | REG_PTR1 },
180 { "tosulteax", REG_EAX, REG_AXY | REG_PTR1 },
181 { "tosumula0", REG_AX, REG_ALL },
182 { "tosumulax", REG_AX, REG_ALL },
183 { "tosumuleax", REG_EAX, REG_ALL },
184 { "utsteax", REG_EAX, REG_Y },
186 #define FuncInfoCount (sizeof(FuncInfoTable) / sizeof(FuncInfoTable[0]))
188 /* Table with names of zero page locations used by the compiler */
189 static const ZPInfo ZPInfoTable[] = {
190 { 0, "ptr1", REG_PTR1_LO, REG_PTR1 },
191 { 0, "ptr1+1", REG_PTR1_HI, REG_PTR1 },
192 { 0, "ptr2", REG_PTR2_LO, REG_PTR2 },
193 { 0, "ptr2+1", REG_PTR2_HI, REG_PTR2 },
194 { 4, "ptr3", REG_NONE, REG_NONE },
195 { 4, "ptr4", REG_NONE, REG_NONE },
196 { 7, "regbank", REG_NONE, REG_NONE },
197 { 0, "regsave", REG_SAVE_LO, REG_SAVE },
198 { 0, "regsave+1", REG_SAVE_HI, REG_SAVE },
199 { 0, "sp", REG_SP_LO, REG_SP },
200 { 0, "sp+1", REG_SP_HI, REG_SP },
201 { 0, "sreg", REG_SREG_LO, REG_SREG },
202 { 0, "sreg+1", REG_SREG_HI, REG_SREG },
203 { 0, "tmp1", REG_TMP1, REG_TMP1 },
204 { 0, "tmp2", REG_NONE, REG_NONE },
205 { 0, "tmp3", REG_NONE, REG_NONE },
206 { 0, "tmp4", REG_NONE, REG_NONE },
208 #define ZPInfoCount (sizeof(ZPInfoTable) / sizeof(ZPInfoTable[0]))
212 /*****************************************************************************/
214 /*****************************************************************************/
218 static int CompareFuncInfo (const void* Key, const void* Info)
219 /* Compare function for bsearch */
221 return strcmp (Key, ((const FuncInfo*) Info)->Name);
226 void GetFuncInfo (const char* Name, unsigned short* Use, unsigned short* Chg)
227 /* For the given function, lookup register information and store it into
228 * the given variables. If the function is unknown, assume it will use and
229 * load all registers.
232 /* If the function name starts with an underline, it is an external
233 * function. Search for it in the symbol table. If the function does
234 * not start with an underline, it may be a runtime support function.
235 * Search for it in the list of builtin functions.
237 if (Name[0] == '_') {
239 /* Search in the symbol table, skip the leading underscore */
240 SymEntry* E = FindGlobalSym (Name+1);
242 /* Did we find it in the top level table? */
243 if (E && IsTypeFunc (E->Type)) {
245 /* A function may use the A or A/X registers if it is a fastcall
246 * function. If it is not a fastcall function but a variadic one,
247 * it will use the Y register (the parameter size is passed here).
248 * In all other cases, no registers are used. However, we assume
249 * that any function will destroy all registers.
251 FuncDesc* D = E->V.F.Func;
252 if ((D->Flags & FD_FASTCALL) != 0 && D->ParamCount > 0) {
253 /* Will use registers depending on the last param */
254 SymEntry* LastParam = D->SymTab->SymTail;
255 unsigned LastParamSize = CheckedSizeOf (LastParam->Type);
256 if (LastParamSize == 1) {
258 } else if (LastParamSize == 2) {
263 } else if ((D->Flags & FD_VARIADIC) != 0) {
266 /* Will not use any registers */
270 /* Will destroy all registers */
279 /* Search for the function in the list of builtin functions */
280 const FuncInfo* Info = bsearch (Name, FuncInfoTable, FuncInfoCount,
281 sizeof(FuncInfo), CompareFuncInfo);
283 /* Do we know the function? */
285 /* Use the information we have */
292 /* Function not found - assume that the primary register is input, and all
293 * registers are changed
301 static int CompareZPInfo (const void* Name, const void* Info)
302 /* Compare function for bsearch */
304 /* Cast the pointers to the correct data type */
305 const char* N = (const char*) Name;
306 const ZPInfo* E = (const ZPInfo*) Info;
308 /* Do the compare. Be careful because of the length (Info may contain
309 * more than just the zeropage name).
312 /* Do a full compare */
313 return strcmp (N, E->Name);
315 /* Only compare the first part */
316 int Res = strncmp (N, E->Name, E->Len);
317 if (Res == 0 && (N[E->Len] != '\0' && N[E->Len] != '+')) {
318 /* Name is actually longer than Info->Name */
327 const ZPInfo* GetZPInfo (const char* Name)
328 /* If the given name is a zero page symbol, return a pointer to the info
329 * struct for this symbol, otherwise return NULL.
332 /* Search for the zp location in the list */
333 return bsearch (Name, ZPInfoTable, ZPInfoCount,
334 sizeof(ZPInfo), CompareZPInfo);
339 static unsigned GetRegInfo2 (CodeSeg* S,
346 /* Recursively called subfunction for GetRegInfo. */
348 /* Follow the instruction flow recording register usage. */
353 /* Check if we have already visited the current code entry. If so,
356 if (CE_HasMark (E)) {
360 /* Mark this entry as already visited */
362 CollAppend (Visited, E);
364 /* Evaluate the used registers */
366 if (E->OPC == OP65_RTS ||
367 ((E->Info & OF_BRA) != 0 && E->JumpTo == 0)) {
368 /* This instruction will leave the function */
372 /* We are not interested in the use of any register that has been
376 /* Remember the remaining registers */
380 /* Evaluate the changed registers */
381 if ((R = E->Chg) != REG_NONE) {
382 /* We are not interested in the use of any register that has been
386 /* Remember the remaining registers */
390 /* If we know about all registers now, bail out */
391 if (((Used | Unused) & Wanted) == Wanted) {
395 /* If the instruction is an RTS or RTI, we're done */
396 if ((E->Info & OF_RET) != 0) {
400 /* If we have an unconditional branch, follow this branch if possible,
401 * otherwise we're done.
403 if ((E->Info & OF_UBRA) != 0) {
405 /* Does this jump have a valid target? */
408 /* Unconditional jump */
409 E = E->JumpTo->Owner;
410 Index = -1; /* Invalidate */
413 /* Jump outside means we're done */
417 /* In case of conditional branches, follow the branch if possible and
418 * follow the normal flow (branch not taken) afterwards. If we cannot
419 * follow the branch, we're done.
421 } else if ((E->Info & OF_CBRA) != 0) {
425 /* Recursively determine register usage at the branch target */
429 U1 = GetRegInfo2 (S, E->JumpTo->Owner, -1, Visited, Used, Unused, Wanted);
431 /* All registers used, no need for second call */
435 Index = CS_GetEntryIndex (S, E);
437 if ((E = CS_GetEntry (S, ++Index)) == 0) {
438 Internal ("GetRegInfo2: No next entry!");
440 U2 = GetRegInfo2 (S, E, Index, Visited, Used, Unused, Wanted);
441 return U1 | U2; /* Used in any of the branches */
444 /* Jump to global symbol */
450 /* Just go to the next instruction */
452 Index = CS_GetEntryIndex (S, E);
454 E = CS_GetEntry (S, ++Index);
457 Internal ("GetRegInfo2: No next entry!");
464 /* Return to the caller the complement of all unused registers */
470 static unsigned GetRegInfo1 (CodeSeg* S,
477 /* Recursively called subfunction for GetRegInfo. */
479 /* Remember the current count of the line collection */
480 unsigned Count = CollCount (Visited);
482 /* Call the worker routine */
483 unsigned R = GetRegInfo2 (S, E, Index, Visited, Used, Unused, Wanted);
485 /* Restore the old count, unmarking all new entries */
486 unsigned NewCount = CollCount (Visited);
487 while (NewCount-- > Count) {
488 CodeEntry* E = CollAt (Visited, NewCount);
490 CollDelete (Visited, NewCount);
493 /* Return the registers used */
499 unsigned GetRegInfo (struct CodeSeg* S, unsigned Index, unsigned Wanted)
500 /* Determine register usage information for the instructions starting at the
505 Collection Visited; /* Visited entries */
508 /* Get the code entry for the given index */
509 if (Index >= CS_GetEntryCount (S)) {
510 /* There is no such code entry */
513 E = CS_GetEntry (S, Index);
515 /* Initialize the data structure used to collection information */
516 InitCollection (&Visited);
518 /* Call the recursive subfunction */
519 R = GetRegInfo1 (S, E, Index, &Visited, REG_NONE, REG_NONE, Wanted);
521 /* Delete the line collection */
522 DoneCollection (&Visited);
524 /* Return the registers used */
530 int RegAUsed (struct CodeSeg* S, unsigned Index)
531 /* Check if the value in A is used. */
533 return (GetRegInfo (S, Index, REG_A) & REG_A) != 0;
538 int RegXUsed (struct CodeSeg* S, unsigned Index)
539 /* Check if the value in X is used. */
541 return (GetRegInfo (S, Index, REG_X) & REG_X) != 0;
546 int RegYUsed (struct CodeSeg* S, unsigned Index)
547 /* Check if the value in Y is used. */
549 return (GetRegInfo (S, Index, REG_Y) & REG_Y) != 0;
554 int RegAXUsed (struct CodeSeg* S, unsigned Index)
555 /* Check if the value in A or(!) the value in X are used. */
557 return (GetRegInfo (S, Index, REG_AX) & REG_AX) != 0;
562 unsigned GetKnownReg (unsigned Use, const RegContents* RC)
563 /* Return the register or zero page location from the set in Use, thats
564 * contents are known. If Use does not contain any register, or if the
565 * register in question does not have a known value, return REG_NONE.
568 if ((Use & REG_A) != 0) {
569 return (RC && RC->RegA >= 0)? REG_A : REG_NONE;
570 } else if ((Use & REG_X) != 0) {
571 return (RC && RC->RegX >= 0)? REG_X : REG_NONE;
572 } else if ((Use & REG_Y) != 0) {
573 return (RC && RC->RegY >= 0)? REG_Y : REG_NONE;
574 } else if ((Use & REG_TMP1) != 0) {
575 return (RC && RC->Tmp1 >= 0)? REG_TMP1 : REG_NONE;
576 } else if ((Use & REG_SREG_LO) != 0) {
577 return (RC && RC->SRegLo >= 0)? REG_SREG_LO : REG_NONE;
578 } else if ((Use & REG_SREG_HI) != 0) {
579 return (RC && RC->SRegHi >= 0)? REG_SREG_HI : REG_NONE;