1 /*****************************************************************************/
5 /* 65C02 specific optimizations */
9 /* (C) 2001-2012, Ullrich von Bassewitz */
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 /* Walk over the entries */
72 while (I < CS_GetEntryCount (S)) {
75 CodeEntry* E = CS_GetEntry (S, I);
77 /* Check for addressing mode indirect indexed Y where Y is zero.
78 ** Note: All opcodes that are available as (zp),y are also available
79 ** as (zp), so we can ignore the actual opcode here.
81 if (E->AM == AM65_ZP_INDY && E->RI->In.RegY == 0) {
83 /* Replace it by indirect addressing mode */
84 CodeEntry* X = NewCodeEntry (E->OPC, AM65_ZP_IND, E->Arg, 0, E->LI);
85 CS_InsertEntry (S, X, I+1);
98 /* Return the number of changes made */
104 unsigned Opt65C02BitOps (CodeSeg* S)
105 /* Use special bit op instructions of the C02 */
107 unsigned Changes = 0;
110 /* Walk over the entries */
112 while (I < CS_GetEntryCount (S)) {
117 L[0] = CS_GetEntry (S, I);
119 /* Check for the sequence */
120 if (L[0]->OPC == OP65_LDA &&
121 (L[0]->AM == AM65_ZP || L[0]->AM == AM65_ABS) &&
122 !CS_RangeHasLabel (S, I+1, 2) &&
123 CS_GetEntries (S, L+1, I+1, 2) &&
124 (L[1]->OPC == OP65_AND || L[1]->OPC == OP65_ORA) &&
125 CE_IsConstImm (L[1]) &&
126 L[2]->OPC == OP65_STA &&
127 L[2]->AM == L[0]->AM &&
128 strcmp (L[2]->Arg, L[0]->Arg) == 0 &&
129 !RegAUsed (S, I+3)) {
134 /* Use TRB for AND and TSB for ORA */
135 if (L[1]->OPC == OP65_AND) {
138 sprintf (Buf, "$%02X", (int) ((~L[1]->Num) & 0xFF));
139 X = NewCodeEntry (OP65_LDA, AM65_IMM, Buf, 0, L[1]->LI);
140 CS_InsertEntry (S, X, I+3);
143 X = NewCodeEntry (OP65_TRB, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
144 CS_InsertEntry (S, X, I+4);
149 sprintf (Buf, "$%02X", (int) L[1]->Num);
150 X = NewCodeEntry (OP65_LDA, AM65_IMM, Buf, 0, L[1]->LI);
151 CS_InsertEntry (S, X, I+3);
154 X = NewCodeEntry (OP65_TSB, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
155 CS_InsertEntry (S, X, I+4);
158 /* Delete the old stuff */
159 CS_DelEntries (S, I, 3);
170 /* Return the number of changes made */
176 unsigned Opt65C02Stores (CodeSeg* S)
177 /* Use STZ where possible */
179 unsigned Changes = 0;
182 /* Walk over the entries */
184 while (I < CS_GetEntryCount (S)) {
187 CodeEntry* E = CS_GetEntry (S, I);
189 /* Check for a store with a register value of zero and an addressing
190 ** mode available with STZ.
192 if (((E->OPC == OP65_STA && E->RI->In.RegA == 0) ||
193 (E->OPC == OP65_STX && E->RI->In.RegX == 0) ||
194 (E->OPC == OP65_STY && E->RI->In.RegY == 0)) &&
195 (E->AM == AM65_ZP || E->AM == AM65_ABS ||
196 E->AM == AM65_ZPX || E->AM == AM65_ABSX)) {
199 CodeEntry* X = NewCodeEntry (OP65_STZ, E->AM, E->Arg, 0, E->LI);
200 CS_InsertEntry (S, X, I+1);
202 /* Delete the old stuff */
214 /* Return the number of changes made */