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 */
180 if (E->OPC == OPC_RTS ||
181 ((E->Info & OF_BRA) != 0 && E->JumpTo == 0)) {
182 /* This instruction will leave the function */
186 /* We are not interested in the use of any register that has been
190 /* Remember the remaining registers */
194 /* Evaluate the changed registers */
195 if ((R = E->Chg) != REG_NONE) {
196 /* We are not interested in the use of any register that has been
200 /* Remember the remaining registers */
204 /* If we know about all registers now, bail out */
205 if ((Used | Unused) == REG_AXY) {
209 /* If the instruction is an RTS or RTI, we're done */
210 if (E->OPC == OPC_RTS || E->OPC == OPC_RTI) {
214 /* If we have an unconditional branch, follow this branch if possible,
215 * otherwise we're done.
217 if ((E->Info & OF_UBRA) != 0) {
219 /* Does this jump have a valid target? */
222 /* Unconditional jump */
223 E = E->JumpTo->Owner;
224 Index = -1; /* Invalidate */
227 /* Jump outside means we're done */
231 /* In case of conditional branches, follow the branch if possible and
232 * follow the normal flow (branch not taken) afterwards. If we cannot
233 * follow the branch, we're done.
235 } else if ((E->Info & OF_CBRA) != 0) {
239 /* Recursively determine register usage at the branch target */
243 U1 = GetRegInfo2 (S, E->JumpTo->Owner, -1, Visited, Used, Unused);
245 /* All registers used, no need for second call */
249 Index = GetCodeEntryIndex (S, E);
251 if ((E = GetCodeEntry (S, ++Index)) == 0) {
252 Internal ("GetRegInfo2: No next entry!");
254 U2 = GetRegInfo2 (S, E, Index, Visited, Used, Unused);
255 return U1 | U2; /* Used in any of the branches */
258 /* Jump to global symbol */
264 /* Just go to the next instruction */
266 Index = GetCodeEntryIndex (S, E);
268 E = GetCodeEntry (S, ++Index);
271 Internal ("GetRegInfo2: No next entry!");
278 /* Return to the caller the complement of all unused registers */
284 static unsigned char GetRegInfo1 (CodeSeg* S,
289 unsigned char Unused)
290 /* Recursively called subfunction for GetRegInfo. */
292 /* Remember the current count of the line collection */
293 unsigned Count = CollCount (Visited);
295 /* Call the worker routine */
296 unsigned char R = GetRegInfo2 (S, E, Index, Visited, Used, Unused);
298 /* Restore the old count, unmarking all new entries */
299 unsigned NewCount = CollCount (Visited);
300 while (NewCount-- > Count) {
301 CodeEntry* E = CollAt (Visited, NewCount);
302 CodeEntryResetMark (E);
303 CollDelete (Visited, NewCount);
306 /* Return the registers used */
312 unsigned char GetRegInfo (struct CodeSeg* S, unsigned Index)
313 /* Determine register usage information for the instructions starting at the
318 Collection Visited; /* Visited entries */
321 /* Get the code entry for the given index */
322 if (Index >= GetCodeEntryCount (S)) {
323 /* There is no such code entry */
326 E = GetCodeEntry (S, Index);
328 /* Initialize the data structure used to collection information */
329 InitCollection (&Visited);
331 /* Call the recursive subfunction */
332 R = GetRegInfo1 (S, E, Index, &Visited, REG_NONE, REG_NONE);
334 /* Delete the line collection */
335 DoneCollection (&Visited);
337 /* Return the registers used */
343 int RegAUsed (struct CodeSeg* S, unsigned Index)
344 /* Check if the value in A is used. */
346 return (GetRegInfo (S, Index) & REG_A) != 0;
351 int RegXUsed (struct CodeSeg* S, unsigned Index)
352 /* Check if the value in X is used. */
354 return (GetRegInfo (S, Index) & REG_X) != 0;
359 int RegYUsed (struct CodeSeg* S, unsigned Index)
360 /* Check if the value in Y is used. */
362 return (GetRegInfo (S, Index) & REG_Y) != 0;