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 char Use; /* Register usage */
65 unsigned char Chg; /* Changed/destroyed registers */
68 static const FuncInfo FuncInfoTable[] = {
69 { "addysp", REG_Y, REG_NONE },
70 { "aslax1", REG_AX, REG_AX },
71 { "aslax2", REG_AX, REG_AX },
72 { "aslax3", REG_AX, REG_AX },
73 { "aslax4", REG_AX, REG_AX },
74 { "bnega", REG_A, REG_AX },
75 { "bnegax", REG_AX, REG_AX },
76 { "bnegeax", REG_AX, REG_AX },
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 },
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 { "ldaidx", REG_AXY, REG_AX },
116 { "ldauidx", REG_AXY, REG_AX },
117 { "ldax0sp", REG_Y, REG_AX },
118 { "ldaxi", REG_AX, REG_AXY },
119 { "ldaxidx", REG_AXY, REG_AX },
120 { "ldaxysp", REG_Y, REG_AX },
121 { "leaasp", REG_A, REG_AX },
122 { "negax", REG_AX, REG_AX },
123 { "pusha", REG_A, REG_Y },
124 { "pusha0", REG_A, REG_XY },
125 { "pushax", REG_AX, REG_Y },
126 { "pusheax", REG_AX, REG_Y },
127 { "pushw0sp", REG_NONE, REG_AXY },
128 { "pushwysp", REG_Y, REG_AXY },
129 { "shlax1", REG_AX, REG_AX },
130 { "shlax2", REG_AX, REG_AX },
131 { "shlax3", REG_AX, REG_AX },
132 { "shlax4", REG_AX, REG_AX },
133 { "shrax1", REG_AX, REG_AX },
134 { "shrax2", REG_AX, REG_AX },
135 { "shrax3", REG_AX, REG_AX },
136 { "shrax4", REG_AX, REG_AX },
137 { "shreax1", REG_AX, REG_AX },
138 { "shreax2", REG_AX, REG_AX },
139 { "shreax3", REG_AX, REG_AX },
140 { "shreax4", REG_AX, REG_AX },
141 { "staspidx", REG_A | REG_Y, REG_Y },
142 { "stax0sp", REG_AX, REG_Y },
143 { "tosicmp", REG_AX, REG_AXY },
144 { "tosdiva0", REG_AX, REG_AXY },
145 { "tosdivax", REG_AX, REG_AXY },
146 { "tosdiveax", REG_AX, REG_AXY },
147 { "tosmula0", REG_AX, REG_AXY },
148 { "tosmulax", REG_AX, REG_AXY },
149 { "tosmuleax", REG_AX, REG_AXY },
150 { "tosshreax", REG_AX, REG_AXY },
151 { "tosumula0", REG_AX, REG_AXY },
152 { "tosumulax", REG_AX, REG_AXY },
153 { "tosumuleax", REG_AX, REG_AXY },
155 #define FuncInfoCount (sizeof(FuncInfoTable) / sizeof(FuncInfoTable[0]))
157 /* Table with names of zero page locations used by the compiler */
158 typedef struct ZPInfo ZPInfo;
160 unsigned char Len; /* Length of the following string */
161 char Name[11]; /* Name of zero page symbol */
163 static const ZPInfo ZPInfoTable[] = {
171 #define ZPInfoCount (sizeof(ZPInfoTable) / sizeof(ZPInfoTable[0]))
174 /*****************************************************************************/
176 /*****************************************************************************/
180 static int CompareFuncInfo (const void* Key, const void* Info)
181 /* Compare function for bsearch */
183 return strcmp (Key, ((const FuncInfo*) Info)->Name);
188 void GetFuncInfo (const char* Name, unsigned char* Use, unsigned char* Chg)
189 /* For the given function, lookup register information and store it into
190 * the given variables. If the function is unknown, assume it will use and
191 * load all registers.
194 /* If the function name starts with an underline, it is an external
195 * function. Search for it in the symbol table. If the function does
196 * not start with an underline, it may be a runtime support function.
197 * Search for it in the list of builtin functions.
199 if (Name[0] == '_') {
201 /* Search in the symbol table, skip the leading underscore */
202 SymEntry* E = FindGlobalSym (Name+1);
204 /* Did we find it in the top level table? */
205 if (E && IsTypeFunc (E->Type)) {
207 /* A function may use the A or A/X registers if it is a fastcall
208 * function. If it is not a fastcall function but a variadic one,
209 * it will use the Y register (the parameter size is passed here).
210 * In all other cases, no registers are used. However, we assume
211 * that any function will destroy all registers.
213 FuncDesc* D = E->V.F.Func;
214 if ((D->Flags & FD_FASTCALL) != 0 && D->ParamCount > 0) {
215 /* Will use registers depending on the last param */
216 SymEntry* LastParam = D->SymTab->SymTail;
217 if (SizeOf (LastParam->Type) == 1) {
222 } else if ((D->Flags & FD_VARIADIC) != 0) {
225 /* Will not use any registers */
229 /* Will destroy all registers */
238 /* Search for the function in the list of builtin functions */
239 const FuncInfo* Info = bsearch (Name, FuncInfoTable, FuncInfoCount,
240 sizeof(FuncInfo), CompareFuncInfo);
242 /* Do we know the function? */
244 /* Use the information we have */
251 /* Function not found - assume all registers used */
258 int IsZPName (const char* Name)
259 /* Return true if the given name is a zero page symbol */
264 /* Because of the low number of symbols, we do a linear search here */
265 for (I = 0, Info = ZPInfoTable; I < ZPInfoCount; ++I, ++Info) {
266 if (strncmp (Name, Info->Name, Info->Len) == 0 &&
267 (Name[Info->Len] == '\0' || Name[Info->Len] == '+')) {
279 static unsigned char GetRegInfo1 (CodeSeg* S,
284 unsigned char Unused);
285 /* Recursively called subfunction for GetRegInfo. */
289 static unsigned char GetRegInfo2 (CodeSeg* S,
294 unsigned char Unused)
295 /* Recursively called subfunction for GetRegInfo. */
297 /* Follow the instruction flow recording register usage. */
302 /* Check if we have already visited the current code entry. If so,
305 if (CE_HasMark (E)) {
309 /* Mark this entry as already visited */
311 CollAppend (Visited, E);
313 /* Evaluate the used registers */
315 if (E->OPC == OP65_RTS ||
316 ((E->Info & OF_BRA) != 0 && E->JumpTo == 0)) {
317 /* This instruction will leave the function */
321 /* We are not interested in the use of any register that has been
325 /* Remember the remaining registers */
329 /* Evaluate the changed registers */
330 if ((R = E->Chg) != REG_NONE) {
331 /* We are not interested in the use of any register that has been
335 /* Remember the remaining registers */
339 /* If we know about all registers now, bail out */
340 if ((Used | Unused) == REG_AXY) {
344 /* If the instruction is an RTS or RTI, we're done */
345 if ((E->Info & OF_RET) != 0) {
349 /* If we have an unconditional branch, follow this branch if possible,
350 * otherwise we're done.
352 if ((E->Info & OF_UBRA) != 0) {
354 /* Does this jump have a valid target? */
357 /* Unconditional jump */
358 E = E->JumpTo->Owner;
359 Index = -1; /* Invalidate */
362 /* Jump outside means we're done */
366 /* In case of conditional branches, follow the branch if possible and
367 * follow the normal flow (branch not taken) afterwards. If we cannot
368 * follow the branch, we're done.
370 } else if ((E->Info & OF_CBRA) != 0) {
374 /* Recursively determine register usage at the branch target */
378 U1 = GetRegInfo1 (S, E->JumpTo->Owner, -1, Visited, Used, Unused);
380 /* All registers used, no need for second call */
384 Index = CS_GetEntryIndex (S, E);
386 if ((E = CS_GetEntry (S, ++Index)) == 0) {
387 Internal ("GetRegInfo2: No next entry!");
389 U2 = GetRegInfo1 (S, E, Index, Visited, Used, Unused);
390 return U1 | U2; /* Used in any of the branches */
393 /* Jump to global symbol */
399 /* Just go to the next instruction */
401 Index = CS_GetEntryIndex (S, E);
403 E = CS_GetEntry (S, ++Index);
406 Internal ("GetRegInfo2: No next entry!");
413 /* Return to the caller the complement of all unused registers */
419 static unsigned char GetRegInfo1 (CodeSeg* S,
424 unsigned char Unused)
425 /* Recursively called subfunction for GetRegInfo. */
427 /* Remember the current count of the line collection */
428 unsigned Count = CollCount (Visited);
430 /* Call the worker routine */
431 unsigned char R = GetRegInfo2 (S, E, Index, Visited, Used, Unused);
433 /* Restore the old count, unmarking all new entries */
434 unsigned NewCount = CollCount (Visited);
435 while (NewCount-- > Count) {
436 CodeEntry* E = CollAt (Visited, NewCount);
438 CollDelete (Visited, NewCount);
441 /* Return the registers used */
447 unsigned char GetRegInfo (struct CodeSeg* S, unsigned Index)
448 /* Determine register usage information for the instructions starting at the
453 Collection Visited; /* Visited entries */
456 /* Get the code entry for the given index */
457 if (Index >= CS_GetEntryCount (S)) {
458 /* There is no such code entry */
461 E = CS_GetEntry (S, Index);
463 /* Initialize the data structure used to collection information */
464 InitCollection (&Visited);
466 /* Call the recursive subfunction */
467 R = GetRegInfo1 (S, E, Index, &Visited, REG_NONE, REG_NONE);
469 /* Delete the line collection */
470 DoneCollection (&Visited);
472 /* Return the registers used */
478 int RegAUsed (struct CodeSeg* S, unsigned Index)
479 /* Check if the value in A is used. */
481 return (GetRegInfo (S, Index) & REG_A) != 0;
486 int RegXUsed (struct CodeSeg* S, unsigned Index)
487 /* Check if the value in X is used. */
489 return (GetRegInfo (S, Index) & REG_X) != 0;
494 int RegYUsed (struct CodeSeg* S, unsigned Index)
495 /* Check if the value in Y is used. */
497 return (GetRegInfo (S, Index) & REG_Y) != 0;