1 /*****************************************************************************/
5 /* 65C02 specific optimizations */
9 /* (C) 2001-2005, Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
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 /*****************************************************************************/
46 /*****************************************************************************/
48 /*****************************************************************************/
52 /*****************************************************************************/
53 /* Helper functions */
54 /*****************************************************************************/
58 /*****************************************************************************/
60 /*****************************************************************************/
64 unsigned Opt65C02Ind (CodeSeg* S)
65 /* Try to use the indirect addressing mode where possible */
70 /* Generate register info for this step */
73 /* Walk over the entries */
75 while (I < CS_GetEntryCount (S)) {
78 CodeEntry* E = CS_GetEntry (S, I);
80 /* Check for addressing mode indirect indexed Y where Y is zero.
81 * Note: All opcodes that are available as (zp),y are also available
82 * as (zp), so we can ignore the actual opcode here.
84 if (E->AM == AM65_ZP_INDY && E->RI->In.RegY == 0) {
86 /* Replace it by indirect addressing mode */
87 CodeEntry* X = NewCodeEntry (E->OPC, AM65_ZP_IND, E->Arg, 0, E->LI);
88 CS_InsertEntry (S, X, I+1);
101 /* Free register info */
104 /* Return the number of changes made */
110 unsigned Opt65C02BitOps (CodeSeg* S)
111 /* Use special bit op instructions of the C02 */
113 unsigned Changes = 0;
116 /* Generate register info for this step */
119 /* Walk over the entries */
121 while (I < CS_GetEntryCount (S)) {
126 L[0] = CS_GetEntry (S, I);
128 /* Check for the sequence */
129 if (L[0]->OPC == OP65_LDA &&
130 (L[0]->AM == AM65_ZP || L[0]->AM == AM65_ABS) &&
131 !CS_RangeHasLabel (S, I+1, 2) &&
132 CS_GetEntries (S, L+1, I+1, 2) &&
133 (L[1]->OPC == OP65_AND || L[1]->OPC == OP65_ORA) &&
134 CE_IsConstImm (L[1]) &&
135 L[2]->OPC == OP65_STA &&
136 L[2]->AM == L[0]->AM &&
137 strcmp (L[2]->Arg, L[0]->Arg) == 0 &&
138 !RegAUsed (S, I+3)) {
143 /* Use TRB for AND and TSB for ORA */
144 if (L[1]->OPC == OP65_AND) {
147 sprintf (Buf, "$%02X", (int) ((~L[1]->Num) & 0xFF));
148 X = NewCodeEntry (OP65_LDA, AM65_IMM, Buf, 0, L[1]->LI);
149 CS_InsertEntry (S, X, I+3);
152 X = NewCodeEntry (OP65_TRB, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
153 CS_InsertEntry (S, X, I+4);
158 sprintf (Buf, "$%02X", (int) L[1]->Num);
159 X = NewCodeEntry (OP65_LDA, AM65_IMM, Buf, 0, L[1]->LI);
160 CS_InsertEntry (S, X, I+3);
163 X = NewCodeEntry (OP65_TSB, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
164 CS_InsertEntry (S, X, I+4);
167 /* Delete the old stuff */
168 CS_DelEntries (S, I, 3);
179 /* Free register info */
182 /* Return the number of changes made */
188 unsigned Opt65C02Stores (CodeSeg* S)
189 /* Use STZ where possible */
191 unsigned Changes = 0;
194 /* Generate register info for this step */
197 /* Walk over the entries */
199 while (I < CS_GetEntryCount (S)) {
202 CodeEntry* E = CS_GetEntry (S, I);
204 /* Check for a store with a register value of zero and an addressing
205 * mode available with STZ.
207 if (((E->OPC == OP65_STA && E->RI->In.RegA == 0) ||
208 (E->OPC == OP65_STX && E->RI->In.RegX == 0) ||
209 (E->OPC == OP65_STY && E->RI->In.RegY == 0)) &&
210 (E->AM == AM65_ZP || E->AM == AM65_ABS ||
211 E->AM == AM65_ZPX || E->AM == AM65_ABSX)) {
214 CodeEntry* X = NewCodeEntry (OP65_STZ, E->AM, E->Arg, 0, E->LI);
215 CS_InsertEntry (S, X, I+1);
217 /* Delete the old stuff */
229 /* Free register info */
232 /* Return the number of changes made */