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 { "deceaxy", REG_EAXY, REG_EAX },
98 { "decsp1", REG_NONE, REG_Y },
99 { "decsp2", REG_NONE, REG_A },
100 { "decsp3", REG_NONE, REG_A },
101 { "decsp4", REG_NONE, REG_A },
102 { "decsp5", REG_NONE, REG_A },
103 { "decsp6", REG_NONE, REG_A },
104 { "decsp7", REG_NONE, REG_A },
105 { "decsp8", REG_NONE, REG_A },
106 { "incax1", REG_AX, REG_AX },
107 { "incax2", REG_AX, REG_AX },
108 { "incsp1", REG_NONE, REG_NONE },
109 { "incsp2", REG_NONE, REG_Y },
110 { "incsp3", REG_NONE, REG_Y },
111 { "incsp4", REG_NONE, REG_Y },
112 { "incsp5", REG_NONE, REG_Y },
113 { "incsp6", REG_NONE, REG_Y },
114 { "incsp7", REG_NONE, REG_Y },
115 { "incsp8", REG_NONE, REG_Y },
116 { "laddeq", REG_EAXY|REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
117 { "laddeq1", REG_Y | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
118 { "laddeqa", REG_AY | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
119 { "ldaidx", REG_AXY, REG_AX | REG_PTR1 },
120 { "ldauidx", REG_AXY, REG_AX | REG_PTR1 },
121 { "ldax0sp", REG_Y, REG_AX },
122 { "ldaxi", REG_AX, REG_AXY | REG_PTR1 },
123 { "ldaxidx", REG_AXY, REG_AXY | REG_PTR1 },
124 { "ldaxysp", REG_Y, REG_AXY },
125 { "leaasp", REG_A, REG_AX },
126 { "lsubeq", REG_EAXY|REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
127 { "lsubeq0sp", REG_EAX, REG_EAXY },
128 { "lsubeq1", REG_Y | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
129 { "lsubeqa", REG_AY | REG_PTR1_LO, REG_EAXY | REG_PTR1_HI },
130 { "lsubeqysp", REG_EAXY, REG_EAXY },
131 { "negax", REG_AX, REG_AX },
132 { "pusha", REG_A, REG_Y },
133 { "pusha0", REG_A, REG_XY },
134 { "pushax", REG_AX, REG_Y },
135 { "pusheax", REG_EAX, REG_Y },
136 { "pushw0sp", REG_NONE, REG_AXY },
137 { "pushwysp", REG_Y, REG_AXY },
138 { "shlax1", REG_AX, REG_AX | REG_TMP1 },
139 { "shlax2", REG_AX, REG_AX | REG_TMP1 },
140 { "shlax3", REG_AX, REG_AX | REG_TMP1 },
141 { "shlax4", REG_AX, REG_AX | REG_TMP1 },
142 { "shrax1", REG_AX, REG_AX | REG_TMP1 },
143 { "shrax2", REG_AX, REG_AX | REG_TMP1 },
144 { "shrax3", REG_AX, REG_AX | REG_TMP1 },
145 { "shrax4", REG_AX, REG_AX | REG_TMP1 },
146 { "shreax1", REG_EAX, REG_AX | REG_TMP1 },
147 { "shreax2", REG_EAX, REG_AX | REG_TMP1 },
148 { "shreax3", REG_EAX, REG_AX | REG_TMP1 },
149 { "shreax4", REG_EAX, REG_AX | REG_TMP1 },
150 { "staspidx", REG_A | REG_Y, REG_Y | REG_TMP1 | REG_PTR1 },
151 { "stax0sp", REG_AX, REG_Y },
152 { "staxysp", REG_AXY, REG_Y },
153 { "subeq0sp", REG_AX, REG_AXY },
154 { "subeqysp", REG_AXY, REG_AXY },
155 { "tsteax", REG_EAX, REG_Y },
156 { "tosadda0", REG_A, REG_AXY },
157 { "tosaddax", REG_AX, REG_AXY },
158 { "tosicmp", REG_AX, REG_AXY | REG_SREG },
159 { "tosdiva0", REG_AX, REG_ALL },
160 { "tosdivax", REG_AX, REG_ALL },
161 { "tosdiveax", REG_EAX, REG_ALL },
162 { "toseqeax", REG_EAX, REG_AXY | REG_PTR1 },
163 { "tosgeeax", REG_EAX, REG_AXY | REG_PTR1 },
164 { "tosgteax", REG_EAX, REG_AXY | REG_PTR1 },
165 { "toslcmp", REG_EAX, REG_A | REG_Y | REG_PTR1 },
166 { "tosleeax", REG_EAX, REG_AXY | REG_PTR1 },
167 { "toslteax", REG_EAX, REG_AXY | REG_PTR1 },
168 { "tosmula0", REG_AX, REG_ALL },
169 { "tosmulax", REG_AX, REG_ALL },
170 { "tosmuleax", REG_EAX, REG_ALL },
171 { "tosneeax", REG_EAX, REG_AXY | REG_PTR1 },
172 { "tosshreax", REG_EAX, REG_EAXY | REG_PTR1 | REG_PTR2 },
173 { "tossuba0", REG_A, REG_AXY },
174 { "tossubax", REG_AX, REG_AXY },
175 { "tossubeax", REG_EAX, REG_EAXY },
176 { "tosugeeax", REG_EAX, REG_AXY | REG_PTR1 },
177 { "tosugteax", REG_EAX, REG_AXY | REG_PTR1 },
178 { "tosuleeax", REG_EAX, REG_AXY | REG_PTR1 },
179 { "tosulteax", REG_EAX, REG_AXY | REG_PTR1 },
180 { "tosumula0", REG_AX, REG_ALL },
181 { "tosumulax", REG_AX, REG_ALL },
182 { "tosumuleax", REG_EAX, REG_ALL },
183 { "utsteax", REG_EAX, REG_Y },
185 #define FuncInfoCount (sizeof(FuncInfoTable) / sizeof(FuncInfoTable[0]))
187 /* Table with names of zero page locations used by the compiler */
188 static const ZPInfo ZPInfoTable[] = {
189 { 0, "ptr1", REG_PTR1_LO, REG_PTR1 },
190 { 0, "ptr1+1", REG_PTR1_HI, REG_PTR1 },
191 { 0, "ptr2", REG_PTR2_LO, REG_PTR2 },
192 { 0, "ptr2+1", REG_PTR2_HI, REG_PTR2 },
193 { 4, "ptr3", REG_NONE, REG_NONE },
194 { 4, "ptr4", REG_NONE, REG_NONE },
195 { 7, "regbank", REG_NONE, REG_NONE },
196 { 0, "regsave", REG_SAVE_LO, REG_SAVE },
197 { 0, "regsave+1", REG_SAVE_HI, REG_SAVE },
198 { 0, "sp", REG_SP_LO, REG_SP },
199 { 0, "sp+1", REG_SP_HI, REG_SP },
200 { 0, "sreg", REG_SREG_LO, REG_SREG },
201 { 0, "sreg+1", REG_SREG_HI, REG_SREG },
202 { 0, "tmp1", REG_TMP1, REG_TMP1 },
203 { 0, "tmp2", REG_NONE, REG_NONE },
204 { 0, "tmp3", REG_NONE, REG_NONE },
205 { 0, "tmp4", REG_NONE, REG_NONE },
207 #define ZPInfoCount (sizeof(ZPInfoTable) / sizeof(ZPInfoTable[0]))
211 /*****************************************************************************/
213 /*****************************************************************************/
217 static int CompareFuncInfo (const void* Key, const void* Info)
218 /* Compare function for bsearch */
220 return strcmp (Key, ((const FuncInfo*) Info)->Name);
225 void GetFuncInfo (const char* Name, unsigned short* Use, unsigned short* Chg)
226 /* For the given function, lookup register information and store it into
227 * the given variables. If the function is unknown, assume it will use and
228 * load all registers.
231 /* If the function name starts with an underline, it is an external
232 * function. Search for it in the symbol table. If the function does
233 * not start with an underline, it may be a runtime support function.
234 * Search for it in the list of builtin functions.
236 if (Name[0] == '_') {
238 /* Search in the symbol table, skip the leading underscore */
239 SymEntry* E = FindGlobalSym (Name+1);
241 /* Did we find it in the top level table? */
242 if (E && IsTypeFunc (E->Type)) {
244 /* A function may use the A or A/X registers if it is a fastcall
245 * function. If it is not a fastcall function but a variadic one,
246 * it will use the Y register (the parameter size is passed here).
247 * In all other cases, no registers are used. However, we assume
248 * that any function will destroy all registers.
250 FuncDesc* D = E->V.F.Func;
251 if ((D->Flags & FD_FASTCALL) != 0 && D->ParamCount > 0) {
252 /* Will use registers depending on the last param */
253 SymEntry* LastParam = D->SymTab->SymTail;
254 if (SizeOf (LastParam->Type) == 1) {
259 } else if ((D->Flags & FD_VARIADIC) != 0) {
262 /* Will not use any registers */
266 /* Will destroy all registers */
275 /* Search for the function in the list of builtin functions */
276 const FuncInfo* Info = bsearch (Name, FuncInfoTable, FuncInfoCount,
277 sizeof(FuncInfo), CompareFuncInfo);
279 /* Do we know the function? */
281 /* Use the information we have */
288 /* Function not found - assume that the primary register is input, and all
289 * registers are changed
297 static int CompareZPInfo (const void* Name, const void* Info)
298 /* Compare function for bsearch */
300 /* Cast the pointers to the correct data type */
301 const char* N = (const char*) Name;
302 const ZPInfo* E = (const ZPInfo*) Info;
304 /* Do the compare. Be careful because of the length (Info may contain
305 * more than just the zeropage name).
308 /* Do a full compare */
309 return strcmp (N, E->Name);
311 /* Only compare the first part */
312 int Res = strncmp (N, E->Name, E->Len);
313 if (Res == 0 && (N[E->Len] != '\0' && N[E->Len] != '+')) {
314 /* Name is actually longer than Info->Name */
323 const ZPInfo* GetZPInfo (const char* Name)
324 /* If the given name is a zero page symbol, return a pointer to the info
325 * struct for this symbol, otherwise return NULL.
328 /* Search for the zp location in the list */
329 return bsearch (Name, ZPInfoTable, ZPInfoCount,
330 sizeof(ZPInfo), CompareZPInfo);
335 static unsigned GetRegInfo2 (CodeSeg* S,
342 /* Recursively called subfunction for GetRegInfo. */
344 /* Follow the instruction flow recording register usage. */
349 /* Check if we have already visited the current code entry. If so,
352 if (CE_HasMark (E)) {
356 /* Mark this entry as already visited */
358 CollAppend (Visited, E);
360 /* Evaluate the used registers */
362 if (E->OPC == OP65_RTS ||
363 ((E->Info & OF_BRA) != 0 && E->JumpTo == 0)) {
364 /* This instruction will leave the function */
368 /* We are not interested in the use of any register that has been
372 /* Remember the remaining registers */
376 /* Evaluate the changed registers */
377 if ((R = E->Chg) != REG_NONE) {
378 /* We are not interested in the use of any register that has been
382 /* Remember the remaining registers */
386 /* If we know about all registers now, bail out */
387 if (((Used | Unused) & Wanted) == Wanted) {
391 /* If the instruction is an RTS or RTI, we're done */
392 if ((E->Info & OF_RET) != 0) {
396 /* If we have an unconditional branch, follow this branch if possible,
397 * otherwise we're done.
399 if ((E->Info & OF_UBRA) != 0) {
401 /* Does this jump have a valid target? */
404 /* Unconditional jump */
405 E = E->JumpTo->Owner;
406 Index = -1; /* Invalidate */
409 /* Jump outside means we're done */
413 /* In case of conditional branches, follow the branch if possible and
414 * follow the normal flow (branch not taken) afterwards. If we cannot
415 * follow the branch, we're done.
417 } else if ((E->Info & OF_CBRA) != 0) {
421 /* Recursively determine register usage at the branch target */
425 U1 = GetRegInfo2 (S, E->JumpTo->Owner, -1, Visited, Used, Unused, Wanted);
427 /* All registers used, no need for second call */
431 Index = CS_GetEntryIndex (S, E);
433 if ((E = CS_GetEntry (S, ++Index)) == 0) {
434 Internal ("GetRegInfo2: No next entry!");
436 U2 = GetRegInfo2 (S, E, Index, Visited, Used, Unused, Wanted);
437 return U1 | U2; /* Used in any of the branches */
440 /* Jump to global symbol */
446 /* Just go to the next instruction */
448 Index = CS_GetEntryIndex (S, E);
450 E = CS_GetEntry (S, ++Index);
453 Internal ("GetRegInfo2: No next entry!");
460 /* Return to the caller the complement of all unused registers */
466 static unsigned GetRegInfo1 (CodeSeg* S,
473 /* Recursively called subfunction for GetRegInfo. */
475 /* Remember the current count of the line collection */
476 unsigned Count = CollCount (Visited);
478 /* Call the worker routine */
479 unsigned R = GetRegInfo2 (S, E, Index, Visited, Used, Unused, Wanted);
481 /* Restore the old count, unmarking all new entries */
482 unsigned NewCount = CollCount (Visited);
483 while (NewCount-- > Count) {
484 CodeEntry* E = CollAt (Visited, NewCount);
486 CollDelete (Visited, NewCount);
489 /* Return the registers used */
495 unsigned GetRegInfo (struct CodeSeg* S, unsigned Index, unsigned Wanted)
496 /* Determine register usage information for the instructions starting at the
501 Collection Visited; /* Visited entries */
504 /* Get the code entry for the given index */
505 if (Index >= CS_GetEntryCount (S)) {
506 /* There is no such code entry */
509 E = CS_GetEntry (S, Index);
511 /* Initialize the data structure used to collection information */
512 InitCollection (&Visited);
514 /* Call the recursive subfunction */
515 R = GetRegInfo1 (S, E, Index, &Visited, REG_NONE, REG_NONE, Wanted);
517 /* Delete the line collection */
518 DoneCollection (&Visited);
520 /* Return the registers used */
526 int RegAUsed (struct CodeSeg* S, unsigned Index)
527 /* Check if the value in A is used. */
529 return (GetRegInfo (S, Index, REG_A) & REG_A) != 0;
534 int RegXUsed (struct CodeSeg* S, unsigned Index)
535 /* Check if the value in X is used. */
537 return (GetRegInfo (S, Index, REG_X) & REG_X) != 0;
542 int RegYUsed (struct CodeSeg* S, unsigned Index)
543 /* Check if the value in Y is used. */
545 return (GetRegInfo (S, Index, REG_Y) & REG_Y) != 0;
550 int RegAXUsed (struct CodeSeg* S, unsigned Index)
551 /* Check if the value in A or(!) the value in X are used. */
553 return (GetRegInfo (S, Index, REG_AX) & REG_AX) != 0;