]> git.sur5r.net Git - cc65/blob - src/cc65/coptc02.c
Fixed a bug
[cc65] / src / cc65 / coptc02.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 coptc02.h                                 */
4 /*                                                                           */
5 /*                       65C02 specific optimizations                        */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2001-2002 Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@cc65.org                                                 */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <string.h>
37
38 /* cc65 */
39 #include "codeent.h"
40 #include "codeinfo.h"
41 #include "error.h"
42 #include "coptc02.h"
43
44
45
46 /*****************************************************************************/
47 /*                                   Data                                    */
48 /*****************************************************************************/
49
50
51
52 /*****************************************************************************/
53 /*                             Helper functions                              */
54 /*****************************************************************************/
55
56
57
58 /*****************************************************************************/
59 /*                                   Code                                    */
60 /*****************************************************************************/
61
62
63
64 unsigned Opt65C02Ind (CodeSeg* S)
65 /* Try to use the indirect addressing mode where possible */
66 {
67     unsigned Changes = 0;
68     unsigned I;
69
70     /* Generate register info for this step */
71     CS_GenRegInfo (S);
72
73     /* Walk over the entries */
74     I = 0;
75     while (I < CS_GetEntryCount (S)) {
76
77         /* Get next entry */
78         CodeEntry* E = CS_GetEntry (S, I);
79
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.
83          */
84         if (E->AM == AM65_ZP_INDY && E->RI->In.RegY == 0) {
85
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);
89             CS_DelEntry (S, I);
90
91             /* We had changes */
92             ++Changes;
93
94         }
95
96         /* Next entry */
97         ++I;
98
99     }
100
101     /* Free register info */
102     CS_FreeRegInfo (S);
103
104     /* Return the number of changes made */
105     return Changes;
106 }
107
108
109
110 unsigned Opt65C02BitOps (CodeSeg* S)
111 /* Use special bit op instructions of the C02 */
112 {
113     unsigned Changes = 0;
114     unsigned I;
115
116     /* Generate register info for this step */
117     CS_GenRegInfo (S);
118
119     /* Walk over the entries */
120     I = 0;
121     while (I < CS_GetEntryCount (S)) {
122
123         CodeEntry* L[3];
124
125         /* Get next entry */
126         L[0] = CS_GetEntry (S, I);
127
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_KnownImm (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)) {
139
140             char Buf[32];
141             CodeEntry* X;
142
143             /* Use TRB for AND and TSB for ORA */
144             if (L[1]->OPC == OP65_AND) {
145
146                 /* LDA #XX */
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);
150
151                 /* TRB */
152                 X = NewCodeEntry (OP65_TRB, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
153                 CS_InsertEntry (S, X, I+4);
154
155             } else {
156
157                 /* LDA #XX */
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);
161
162                 /* TSB */
163                 X = NewCodeEntry (OP65_TSB, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
164                 CS_InsertEntry (S, X, I+4);
165             }
166
167             /* Delete the old stuff */
168             CS_DelEntries (S, I, 3);
169
170             /* We had changes */
171             ++Changes;
172         }
173
174         /* Next entry */
175         ++I;
176
177     }
178
179     /* Free register info */
180     CS_FreeRegInfo (S);
181
182     /* Return the number of changes made */
183     return Changes;
184 }
185
186
187
188 unsigned Opt65C02Stores (CodeSeg* S)
189 /* Use STZ where possible */
190 {
191     unsigned Changes = 0;
192     unsigned I;
193
194     /* Generate register info for this step */
195     CS_GenRegInfo (S);
196
197     /* Walk over the entries */
198     I = 0;
199     while (I < CS_GetEntryCount (S)) {
200
201         /* Get next entry */
202         CodeEntry* E = CS_GetEntry (S, I);
203
204         /* Check for a store with a register value of zero and an addressing
205          * mode available with STZ.
206          */
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)) {
212
213             /* Replace by STZ */
214             CodeEntry* X = NewCodeEntry (OP65_STZ, E->AM, E->Arg, 0, E->LI);
215             CS_InsertEntry (S, X, I+1);
216
217             /* Delete the old stuff */
218             CS_DelEntry (S, I);
219
220             /* We had changes */
221             ++Changes;
222         }
223
224         /* Next entry */
225         ++I;
226
227     }
228
229     /* Free register info */
230     CS_FreeRegInfo (S);
231
232     /* Return the number of changes made */
233     return Changes;
234 }
235
236
237