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 /*****************************************************************************/
50 /*****************************************************************************/
52 /*****************************************************************************/
56 /* Table listing the function names and code info values for known internally
57 * used functions. This table should get auto-generated in the future.
59 typedef struct FuncInfo FuncInfo;
61 const char* Name; /* Function name */
62 unsigned char Use; /* Register usage */
63 unsigned char Chg; /* Changed/destroyed registers */
66 static const FuncInfo FuncInfoTable[] = {
67 { "addysp", REG_Y, REG_NONE },
68 { "booleq", REG_NONE, REG_AX },
69 { "boolge", REG_NONE, REG_AX },
70 { "boolgt", REG_NONE, REG_AX },
71 { "boolle", REG_NONE, REG_AX },
72 { "boollt", REG_NONE, REG_AX },
73 { "boolne", REG_NONE, REG_AX },
74 { "booluge", REG_NONE, REG_AX },
75 { "boolugt", REG_NONE, REG_AX },
76 { "boolule", REG_NONE, REG_AX },
77 { "boolult", REG_NONE, REG_AX },
78 { "decax1", REG_AX, REG_AX },
79 { "decax2", REG_AX, REG_AX },
80 { "decax3", REG_AX, REG_AX },
81 { "decax4", REG_AX, REG_AX },
82 { "decax5", REG_AX, REG_AX },
83 { "decax6", REG_AX, REG_AX },
84 { "decax7", REG_AX, REG_AX },
85 { "decax8", REG_AX, REG_AX },
86 { "decaxy", REG_AXY, REG_AX },
87 { "decsp2", REG_NONE, REG_A },
88 { "decsp3", REG_NONE, REG_A },
89 { "decsp4", REG_NONE, REG_A },
90 { "decsp5", REG_NONE, REG_A },
91 { "decsp6", REG_NONE, REG_A },
92 { "decsp7", REG_NONE, REG_A },
93 { "decsp8", REG_NONE, REG_A },
94 { "incsp1", REG_NONE, REG_NONE },
95 { "incsp2", REG_NONE, REG_Y },
96 { "incsp3", REG_NONE, REG_Y },
97 { "incsp4", REG_NONE, REG_Y },
98 { "incsp5", REG_NONE, REG_Y },
99 { "incsp6", REG_NONE, REG_Y },
100 { "incsp7", REG_NONE, REG_Y },
101 { "incsp8", REG_NONE, REG_Y },
102 { "ldax0sp", REG_Y, REG_AX },
103 { "ldaxysp", REG_Y, REG_AX },
104 { "pusha", REG_A, REG_Y },
105 { "pusha0", REG_A, REG_XY },
106 { "pushax", REG_AX, REG_Y },
107 { "pushw0sp", REG_NONE, REG_AXY },
108 { "pushwysp", REG_Y, REG_AXY },
109 { "tosicmp", REG_AX, REG_AXY },
111 #define FuncInfoCount (sizeof(FuncInfoTable) / sizeof(FuncInfoTable[0]))
115 /*****************************************************************************/
117 /*****************************************************************************/
121 static int CompareFuncInfo (const void* Key, const void* Info)
122 /* Compare function for bsearch */
124 return strcmp (Key, ((const FuncInfo*) Info)->Name);
129 void GetFuncInfo (const char* Name, unsigned char* Use, unsigned char* Chg)
130 /* For the given function, lookup register information and combine it with
131 * the information already in place. If the function is unknown, assume it
132 * will use all registers and load all registers.
133 * See codeinfo.h for possible flags.
136 /* Search for the function */
137 const FuncInfo* Info = bsearch (Name, FuncInfoTable, FuncInfoCount,
138 sizeof(FuncInfo), CompareFuncInfo);
140 /* Do we know the function? */
142 /* Use the information we have */
146 /* Assume all registers used */
154 static unsigned char GetRegInfo2 (CodeSeg* S,
159 unsigned char Unused)
160 /* Recursively called subfunction for GetRegInfo. */
162 /* Follow the instruction flow recording register usage. */
167 /* Check if we have already visited the current code entry. If so,
170 if (CodeEntryHasMark (E)) {
174 /* Mark this entry as already visited */
175 CodeEntrySetMark (E);
176 CollAppend (Visited, E);
178 /* Evaluate the used registers */
179 if ((R = E->Use) != REG_NONE) {
180 /* We are not interested in the use of any register that has been
184 /* Remember the remaining registers */
188 /* Evaluate the changed registers */
189 if ((R = E->Chg) != REG_NONE) {
190 /* We are not interested in the use of any register that has been
194 /* Remember the remaining registers */
198 /* If we know about all registers now, bail out */
199 if ((Used | Unused) == REG_AXY) {
203 /* If the instruction is an RTS or RTI, we're done */
204 if (E->OPC == OPC_RTS || E->OPC == OPC_RTI) {
208 /* If we have an unconditional branch, follow this branch if possible,
209 * otherwise we're done.
211 if ((E->Info & OF_UBRA) != 0) {
213 /* Does this jump have a valid target? */
216 /* Unconditional jump */
217 E = E->JumpTo->Owner;
218 Index = -1; /* Invalidate */
221 /* Jump outside means we're done */
225 /* In case of conditional branches, follow the branch if possible and
226 * follow the normal flow (branch not taken) afterwards. If we cannot
227 * follow the branch, we're done.
229 } else if ((E->Info & OF_CBRA) != 0) {
233 /* Recursively determine register usage at the branch target */
237 U1 = GetRegInfo2 (S, E->JumpTo->Owner, -1, Visited, Used, Unused);
239 /* All registers used, no need for second call */
243 Index = GetCodeEntryIndex (S, E);
245 if ((E = GetCodeEntry (S, ++Index)) == 0) {
246 Internal ("GetRegInfo2: No next entry!");
248 U2 = GetRegInfo2 (S, E, Index, Visited, Used, Unused);
249 return U1 | U2; /* Used in any of the branches */
252 /* Jump to global symbol */
258 /* Just go to the next instruction */
260 Index = GetCodeEntryIndex (S, E);
262 E = GetCodeEntry (S, ++Index);
265 Internal ("GetRegInfo2: No next entry!");
272 /* Return to the caller the complement of all unused registers */
278 static unsigned char GetRegInfo1 (CodeSeg* S,
283 unsigned char Unused)
284 /* Recursively called subfunction for GetRegInfo. */
286 /* Remember the current count of the line collection */
287 unsigned Count = CollCount (Visited);
289 /* Call the worker routine */
290 unsigned char R = GetRegInfo2 (S, E, Index, Visited, Used, Unused);
292 /* Restore the old count, unmarking all new entries */
293 unsigned NewCount = CollCount (Visited);
294 while (NewCount-- > Count) {
295 CodeEntry* E = CollAt (Visited, NewCount);
296 CodeEntryResetMark (E);
297 CollDelete (Visited, NewCount);
300 /* Return the registers used */
306 unsigned char GetRegInfo (struct CodeSeg* S, unsigned Index)
307 /* Determine register usage information for the instructions starting at the
312 Collection Visited; /* Visited entries */
315 /* Get the code entry for the given index */
316 if (Index >= GetCodeEntryCount (S)) {
317 /* There is no such code entry */
320 E = GetCodeEntry (S, Index);
322 /* Initialize the data structure used to collection information */
323 InitCollection (&Visited);
325 /* Call the recursive subfunction */
326 R = GetRegInfo1 (S, E, Index, &Visited, REG_NONE, REG_NONE);
328 /* Delete the line collection */
329 DoneCollection (&Visited);
331 /* Return the registers used */
337 int RegAUsed (struct CodeSeg* S, unsigned Index)
338 /* Check if the value in A is used. */
340 return (GetRegInfo (S, Index) & REG_A) != 0;
345 int RegXUsed (struct CodeSeg* S, unsigned Index)
346 /* Check if the value in X is used. */
348 return (GetRegInfo (S, Index) & REG_X) != 0;
353 int RegYUsed (struct CodeSeg* S, unsigned Index)
354 /* Check if the value in Y is used. */
356 return (GetRegInfo (S, Index) & REG_Y) != 0;