1 /*****************************************************************************/
5 /* Additional information about 6502 code */
9 /* (C) 2001 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 /*****************************************************************************/
52 /*****************************************************************************/
54 /*****************************************************************************/
58 /* Table listing the function names and code info values for known internally
59 * used functions. This table should get auto-generated in the future.
61 typedef struct FuncInfo FuncInfo;
63 const char* Name; /* Function name */
64 unsigned short Use; /* Register usage */
65 unsigned short Chg; /* Changed/destroyed registers */
68 static const FuncInfo FuncInfoTable[] = {
69 { "addysp", REG_Y, REG_NONE },
70 { "aslax1", REG_AX, REG_AX | REG_TMP1 },
71 { "aslax2", REG_AX, REG_AX | REG_TMP1 },
72 { "aslax3", REG_AX, REG_AX | REG_TMP1 },
73 { "aslax4", REG_AX, REG_AX | REG_TMP1 },
74 { "bnega", REG_A, REG_AX },
75 { "bnegax", REG_AX, REG_AX },
76 { "bnegeax", REG_EAX, REG_EAX },
77 { "booleq", REG_NONE, REG_AX },
78 { "boolge", REG_NONE, REG_AX },
79 { "boolgt", REG_NONE, REG_AX },
80 { "boolle", REG_NONE, REG_AX },
81 { "boollt", REG_NONE, REG_AX },
82 { "boolne", REG_NONE, REG_AX },
83 { "booluge", REG_NONE, REG_AX },
84 { "boolugt", REG_NONE, REG_AX },
85 { "boolule", REG_NONE, REG_AX },
86 { "boolult", REG_NONE, REG_AX },
87 { "complax", REG_AX, REG_AX },
88 { "decax1", REG_AX, REG_AX },
89 { "decax2", REG_AX, REG_AX },
90 { "decax3", REG_AX, REG_AX },
91 { "decax4", REG_AX, REG_AX },
92 { "decax5", REG_AX, REG_AX },
93 { "decax6", REG_AX, REG_AX },
94 { "decax7", REG_AX, REG_AX },
95 { "decax8", REG_AX, REG_AX },
96 { "decaxy", REG_AXY, REG_AX | REG_TMP1 },
97 { "decsp1", REG_NONE, REG_Y },
98 { "decsp2", REG_NONE, REG_A },
99 { "decsp3", REG_NONE, REG_A },
100 { "decsp4", REG_NONE, REG_A },
101 { "decsp5", REG_NONE, REG_A },
102 { "decsp6", REG_NONE, REG_A },
103 { "decsp7", REG_NONE, REG_A },
104 { "decsp8", REG_NONE, REG_A },
105 { "incax1", REG_AX, REG_AX },
106 { "incax2", REG_AX, REG_AX },
107 { "incsp1", REG_NONE, REG_NONE },
108 { "incsp2", REG_NONE, REG_Y },
109 { "incsp3", REG_NONE, REG_Y },
110 { "incsp4", REG_NONE, REG_Y },
111 { "incsp5", REG_NONE, REG_Y },
112 { "incsp6", REG_NONE, REG_Y },
113 { "incsp7", REG_NONE, REG_Y },
114 { "incsp8", REG_NONE, REG_Y },
115 { "laddeq", REG_EAXY|REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
116 { "laddeq1", REG_Y | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
117 { "laddeqa", REG_AY | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
118 { "ldaidx", REG_AXY, REG_AX | REG_PTR1 },
119 { "ldauidx", REG_AXY, REG_AX | REG_PTR1 },
120 { "ldax0sp", REG_Y, REG_AX },
121 { "ldaxi", REG_AX, REG_AXY | REG_PTR1 },
122 { "ldaxidx", REG_AXY, REG_AXY | REG_PTR1 },
123 { "ldaxysp", REG_Y, REG_AXY },
124 { "leaasp", REG_A, REG_AX },
125 { "negax", REG_AX, REG_AX },
126 { "pusha", REG_A, REG_Y },
127 { "pusha0", REG_A, REG_XY },
128 { "pushax", REG_AX, REG_Y },
129 { "pusheax", REG_EAX, REG_Y },
130 { "pushw0sp", REG_NONE, REG_AXY },
131 { "pushwysp", REG_Y, REG_AXY },
132 { "shlax1", REG_AX, REG_AX | REG_TMP1 },
133 { "shlax2", REG_AX, REG_AX | REG_TMP1 },
134 { "shlax3", REG_AX, REG_AX | REG_TMP1 },
135 { "shlax4", REG_AX, REG_AX | REG_TMP1 },
136 { "shrax1", REG_AX, REG_AX | REG_TMP1 },
137 { "shrax2", REG_AX, REG_AX | REG_TMP1 },
138 { "shrax3", REG_AX, REG_AX | REG_TMP1 },
139 { "shrax4", REG_AX, REG_AX | REG_TMP1 },
140 { "shreax1", REG_EAX, REG_AX | REG_TMP1 },
141 { "shreax2", REG_EAX, REG_AX | REG_TMP1 },
142 { "shreax3", REG_EAX, REG_AX | REG_TMP1 },
143 { "shreax4", REG_EAX, REG_AX | REG_TMP1 },
144 { "staspidx", REG_A | REG_Y, REG_Y | REG_TMP1 | REG_PTR1 },
145 { "stax0sp", REG_AX, REG_Y },
146 { "staxysp", REG_AXY, REG_Y },
147 { "tsteax", REG_EAX, REG_Y },
148 { "tosadda0", REG_A, REG_AXY },
149 { "tosaddax", REG_AX, REG_AXY },
150 { "tosicmp", REG_AX, REG_AXY | REG_SREG },
151 { "tosdiva0", REG_AX, REG_ALL },
152 { "tosdivax", REG_AX, REG_ALL },
153 { "tosdiveax", REG_EAX, REG_ALL },
154 { "toseqeax", REG_EAX, REG_AXY | REG_PTR1 },
155 { "tosgeeax", REG_EAX, REG_AXY | REG_PTR1 },
156 { "tosgteax", REG_EAX, REG_AXY | REG_PTR1 },
157 { "toslcmp", REG_EAX, REG_A | REG_Y | REG_PTR1 },
158 { "tosleeax", REG_EAX, REG_AXY | REG_PTR1 },
159 { "toslteax", REG_EAX, REG_AXY | REG_PTR1 },
160 { "tosmula0", REG_AX, REG_ALL },
161 { "tosmulax", REG_AX, REG_ALL },
162 { "tosmuleax", REG_EAX, REG_ALL },
163 { "tosneeax", REG_EAX, REG_AXY | REG_PTR1 },
164 { "tosshreax", REG_EAX, REG_EAXY | REG_PTR1 | REG_PTR2 },
165 { "tosugeeax", REG_EAX, REG_AXY | REG_PTR1 },
166 { "tosugteax", REG_EAX, REG_AXY | REG_PTR1 },
167 { "tosuleeax", REG_EAX, REG_AXY | REG_PTR1 },
168 { "tosulteax", REG_EAX, REG_AXY | REG_PTR1 },
169 { "tosumula0", REG_AX, REG_ALL },
170 { "tosumulax", REG_AX, REG_ALL },
171 { "tosumuleax", REG_EAX, REG_ALL },
172 { "utsteax", REG_EAX, REG_Y },
174 #define FuncInfoCount (sizeof(FuncInfoTable) / sizeof(FuncInfoTable[0]))
176 /* Table with names of zero page locations used by the compiler */
177 static const ZPInfo ZPInfoTable[] = {
178 { 0, "ptr1", REG_PTR1_LO, REG_PTR1 },
179 { 0, "ptr1+1", REG_PTR1_HI, REG_PTR1 },
180 { 0, "ptr2", REG_PTR2_LO, REG_PTR2 },
181 { 0, "ptr2+1", REG_PTR2_HI, REG_PTR2 },
182 { 4, "ptr3", REG_NONE, REG_NONE },
183 { 4, "ptr4", REG_NONE, REG_NONE },
184 { 7, "regbank", REG_NONE, REG_NONE },
185 { 0, "regsave", REG_SAVE_LO, REG_SAVE },
186 { 0, "regsave+1", REG_SAVE_HI, REG_SAVE },
187 { 0, "sp", REG_SP_LO, REG_SP },
188 { 0, "sp+1", REG_SP_HI, REG_SP },
189 { 0, "sreg", REG_SREG_LO, REG_SREG },
190 { 0, "sreg+1", REG_SREG_HI, REG_SREG },
191 { 0, "tmp1", REG_TMP1, REG_TMP1 },
192 { 0, "tmp2", REG_NONE, REG_NONE },
193 { 0, "tmp3", REG_NONE, REG_NONE },
194 { 0, "tmp4", REG_NONE, REG_NONE },
196 #define ZPInfoCount (sizeof(ZPInfoTable) / sizeof(ZPInfoTable[0]))
200 /*****************************************************************************/
202 /*****************************************************************************/
206 static int CompareFuncInfo (const void* Key, const void* Info)
207 /* Compare function for bsearch */
209 return strcmp (Key, ((const FuncInfo*) Info)->Name);
214 void GetFuncInfo (const char* Name, unsigned short* Use, unsigned short* Chg)
215 /* For the given function, lookup register information and store it into
216 * the given variables. If the function is unknown, assume it will use and
217 * load all registers.
220 /* If the function name starts with an underline, it is an external
221 * function. Search for it in the symbol table. If the function does
222 * not start with an underline, it may be a runtime support function.
223 * Search for it in the list of builtin functions.
225 if (Name[0] == '_') {
227 /* Search in the symbol table, skip the leading underscore */
228 SymEntry* E = FindGlobalSym (Name+1);
230 /* Did we find it in the top level table? */
231 if (E && IsTypeFunc (E->Type)) {
233 /* A function may use the A or A/X registers if it is a fastcall
234 * function. If it is not a fastcall function but a variadic one,
235 * it will use the Y register (the parameter size is passed here).
236 * In all other cases, no registers are used. However, we assume
237 * that any function will destroy all registers.
239 FuncDesc* D = E->V.F.Func;
240 if ((D->Flags & FD_FASTCALL) != 0 && D->ParamCount > 0) {
241 /* Will use registers depending on the last param */
242 SymEntry* LastParam = D->SymTab->SymTail;
243 if (SizeOf (LastParam->Type) == 1) {
248 } else if ((D->Flags & FD_VARIADIC) != 0) {
251 /* Will not use any registers */
255 /* Will destroy all registers */
264 /* Search for the function in the list of builtin functions */
265 const FuncInfo* Info = bsearch (Name, FuncInfoTable, FuncInfoCount,
266 sizeof(FuncInfo), CompareFuncInfo);
268 /* Do we know the function? */
270 /* Use the information we have */
277 /* Function not found - assume that the primary register is input, and all
278 * registers are changed
286 static int CompareZPInfo (const void* Name, const void* Info)
287 /* Compare function for bsearch */
289 /* Cast the pointers to the correct data type */
290 const char* N = (const char*) Name;
291 const ZPInfo* E = (const ZPInfo*) Info;
293 /* Do the compare. Be careful because of the length (Info may contain
294 * more than just the zeropage name).
297 /* Do a full compare */
298 return strcmp (N, E->Name);
300 /* Only compare the first part */
301 int Res = strncmp (N, E->Name, E->Len);
302 if (Res == 0 && (N[E->Len] != '\0' && N[E->Len] != '+')) {
303 /* Name is actually longer than Info->Name */
312 const ZPInfo* GetZPInfo (const char* Name)
313 /* If the given name is a zero page symbol, return a pointer to the info
314 * struct for this symbol, otherwise return NULL.
317 /* Search for the zp location in the list */
318 return bsearch (Name, ZPInfoTable, ZPInfoCount,
319 sizeof(ZPInfo), CompareZPInfo);
324 static unsigned GetRegInfo2 (CodeSeg* S,
331 /* Recursively called subfunction for GetRegInfo. */
333 /* Follow the instruction flow recording register usage. */
338 /* Check if we have already visited the current code entry. If so,
341 if (CE_HasMark (E)) {
345 /* Mark this entry as already visited */
347 CollAppend (Visited, E);
349 /* Evaluate the used registers */
351 if (E->OPC == OP65_RTS ||
352 ((E->Info & OF_BRA) != 0 && E->JumpTo == 0)) {
353 /* This instruction will leave the function */
357 /* We are not interested in the use of any register that has been
361 /* Remember the remaining registers */
365 /* Evaluate the changed registers */
366 if ((R = E->Chg) != REG_NONE) {
367 /* We are not interested in the use of any register that has been
371 /* Remember the remaining registers */
375 /* If we know about all registers now, bail out */
376 if (((Used | Unused) & Wanted) == Wanted) {
380 /* If the instruction is an RTS or RTI, we're done */
381 if ((E->Info & OF_RET) != 0) {
385 /* If we have an unconditional branch, follow this branch if possible,
386 * otherwise we're done.
388 if ((E->Info & OF_UBRA) != 0) {
390 /* Does this jump have a valid target? */
393 /* Unconditional jump */
394 E = E->JumpTo->Owner;
395 Index = -1; /* Invalidate */
398 /* Jump outside means we're done */
402 /* In case of conditional branches, follow the branch if possible and
403 * follow the normal flow (branch not taken) afterwards. If we cannot
404 * follow the branch, we're done.
406 } else if ((E->Info & OF_CBRA) != 0) {
410 /* Recursively determine register usage at the branch target */
414 U1 = GetRegInfo2 (S, E->JumpTo->Owner, -1, Visited, Used, Unused, Wanted);
416 /* All registers used, no need for second call */
420 Index = CS_GetEntryIndex (S, E);
422 if ((E = CS_GetEntry (S, ++Index)) == 0) {
423 Internal ("GetRegInfo2: No next entry!");
425 U2 = GetRegInfo2 (S, E, Index, Visited, Used, Unused, Wanted);
426 return U1 | U2; /* Used in any of the branches */
429 /* Jump to global symbol */
435 /* Just go to the next instruction */
437 Index = CS_GetEntryIndex (S, E);
439 E = CS_GetEntry (S, ++Index);
442 Internal ("GetRegInfo2: No next entry!");
449 /* Return to the caller the complement of all unused registers */
455 static unsigned GetRegInfo1 (CodeSeg* S,
462 /* Recursively called subfunction for GetRegInfo. */
464 /* Remember the current count of the line collection */
465 unsigned Count = CollCount (Visited);
467 /* Call the worker routine */
468 unsigned R = GetRegInfo2 (S, E, Index, Visited, Used, Unused, Wanted);
470 /* Restore the old count, unmarking all new entries */
471 unsigned NewCount = CollCount (Visited);
472 while (NewCount-- > Count) {
473 CodeEntry* E = CollAt (Visited, NewCount);
475 CollDelete (Visited, NewCount);
478 /* Return the registers used */
484 unsigned GetRegInfo (struct CodeSeg* S, unsigned Index, unsigned Wanted)
485 /* Determine register usage information for the instructions starting at the
490 Collection Visited; /* Visited entries */
493 /* Get the code entry for the given index */
494 if (Index >= CS_GetEntryCount (S)) {
495 /* There is no such code entry */
498 E = CS_GetEntry (S, Index);
500 /* Initialize the data structure used to collection information */
501 InitCollection (&Visited);
503 /* Call the recursive subfunction */
504 R = GetRegInfo1 (S, E, Index, &Visited, REG_NONE, REG_NONE, Wanted);
506 /* Delete the line collection */
507 DoneCollection (&Visited);
509 /* Return the registers used */
515 int RegAUsed (struct CodeSeg* S, unsigned Index)
516 /* Check if the value in A is used. */
518 return (GetRegInfo (S, Index, REG_A) & REG_A) != 0;
523 int RegXUsed (struct CodeSeg* S, unsigned Index)
524 /* Check if the value in X is used. */
526 return (GetRegInfo (S, Index, REG_X) & REG_X) != 0;
531 int RegYUsed (struct CodeSeg* S, unsigned Index)
532 /* Check if the value in Y is used. */
534 return (GetRegInfo (S, Index, REG_Y) & REG_Y) != 0;
539 int RegAXUsed (struct CodeSeg* S, unsigned Index)
540 /* Check if the value in A or(!) the value in X are used. */
542 return (GetRegInfo (S, Index, REG_AX) & REG_AX) != 0;