]> git.sur5r.net Git - cc65/blob - src/cc65/coptadd.c
Fixed some bugs
[cc65] / src / cc65 / coptadd.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 coptadd.c                                 */
4 /*                                                                           */
5 /*                        Optimize addition sequences                        */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2001      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 /* common */
37 #include "xsprintf.h"
38
39 /* cc65 */
40 #include "codeent.h"
41 #include "codeinfo.h"
42 #include "coptadd.h"
43
44
45
46 /*****************************************************************************/
47 /*                            Optimize additions                             */
48 /*****************************************************************************/
49
50
51
52 unsigned OptAdd1 (CodeSeg* S)
53 /* Search for the sequence
54  *
55  *      jsr     pushax
56  *      ldy     xxx
57  *      ldx     #$00
58  *      lda     (sp),y
59  *      jsr     tosaddax
60  *
61  * and replace it by:
62  *
63  *      ldy     xxx-2
64  *      clc
65  *      adc     (sp),y
66  *      bcc     L
67  *      inx
68  * L:
69  */
70 {
71     unsigned Changes = 0;
72
73     /* Walk over the entries */
74     unsigned I = 0;
75     while (I < CS_GetEntryCount (S)) {
76
77         CodeEntry* L[5];
78
79         /* Get next entry */
80         CodeEntry* E = CS_GetEntry (S, I);
81
82         /* Check for the sequence */
83         if (CE_IsCall (E, "pushax")          &&
84             CS_GetEntries (S, L, I+1, 5)     &&
85             L[0]->OPC == OP65_LDY            &&
86             CE_KnownImm (L[0])               &&
87             !CE_HasLabel (L[0])              &&
88             L[1]->OPC == OP65_LDX            &&
89             CE_KnownImm (L[1])               &&
90             L[1]->Num == 0                   &&
91             !CE_HasLabel (L[1])              &&
92             L[2]->OPC == OP65_LDA            &&
93             !CE_HasLabel (L[2])              &&
94             CE_IsCall (L[3], "tosaddax")     &&
95             !CE_HasLabel (L[3])) {
96
97             CodeEntry* X;
98             CodeLabel* Label;
99
100             /* Remove the call to pushax */
101             CS_DelEntry (S, I);
102
103             /* Correct the stack offset (needed since pushax was removed) */
104             CE_SetNumArg (L[0], L[0]->Num - 2);
105
106             /* Add the clc . */
107             X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, L[3]->LI);
108             CS_InsertEntry (S, X, I+1);
109
110             /* Remove the load */
111             CS_DelEntry (S, I+3);      /* lda */
112             CS_DelEntry (S, I+2);      /* ldx */
113
114             /* Add the adc */
115             X = NewCodeEntry (OP65_ADC, AM65_ZP_INDY, "sp", 0, L[3]->LI);
116             CS_InsertEntry (S, X, I+2);
117
118             /* Generate the branch label and the branch */
119             Label = CS_GenLabel (S, L[4]);
120             X = NewCodeEntry (OP65_BCC, AM65_BRA, Label->Name, Label, L[3]->LI);
121             CS_InsertEntry (S, X, I+3);
122
123             /* Generate the increment of the high byte */
124             X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, L[3]->LI);
125             CS_InsertEntry (S, X, I+4);
126
127             /* Delete the call to tosaddax */
128             CS_DelEntry (S, I+5);
129
130             /* Remember, we had changes */
131             ++Changes;
132
133         }
134
135         /* Next entry */
136         ++I;
137
138     }
139
140     /* Return the number of changes made */
141     return Changes;
142 }
143
144
145
146 unsigned OptAdd2 (CodeSeg* S)
147 /* Search for the sequence
148  *
149  *      ldy     #yy
150  *      lda     (sp),y
151  *      tax
152  *      ldy     #xx
153  *      lda     (sp),y
154  *      ldy     #zz
155  *      jsr     addeqysp
156  *
157  * and replace it by:
158  *
159  *      ldy     #xx
160  *      lda     (sp),y
161  *      ldy     #zz
162  *      clc
163  *      adc     (sp),y
164  *      sta     (sp),y
165  *      ldy     #yy
166  *      lda     (sp),y
167  *      ldy     #zz+1
168  *      adc     (sp),y
169  *      sta     (sp),y
170  *
171  * provided that a/x is not used later.
172  */
173 {
174     unsigned Changes = 0;
175
176     /* Walk over the entries */
177     unsigned I = 0;
178     while (I < CS_GetEntryCount (S)) {
179
180         CodeEntry* L[7];
181
182         /* Get next entry */
183         L[0] = CS_GetEntry (S, I);
184
185         /* Check for the sequence */
186         if (L[0]->OPC == OP65_LDY               &&
187             CE_KnownImm (L[0])                  &&
188             CS_GetEntries (S, L+1, I+1, 6)      &&
189             L[1]->OPC == OP65_LDA               &&
190             L[1]->AM == AM65_ZP_INDY            &&
191             !CE_HasLabel (L[1])                 &&
192             L[2]->OPC == OP65_TAX               &&
193             !CE_HasLabel (L[2])                 &&
194             L[3]->OPC == OP65_LDY               &&
195             CE_KnownImm (L[3])                  &&
196             !CE_HasLabel (L[3])                 &&
197             L[4]->OPC == OP65_LDA               &&
198             L[4]->AM == AM65_ZP_INDY            &&
199             !CE_HasLabel (L[4])                 &&
200             L[5]->OPC == OP65_LDY               &&
201             CE_KnownImm (L[5])                  &&
202             !CE_HasLabel (L[5])                 &&
203             CE_IsCall (L[6], "addeqysp")        &&
204             !CE_HasLabel (L[6])                 &&
205             (GetRegInfo (S, I+7, REG_AX) & REG_AX) == 0) {
206
207             char Buf [20];
208             CodeEntry* X;
209
210
211             /* Insert new code behind the addeqysp */
212             X = NewCodeEntry (OP65_LDY, AM65_IMM, L[3]->Arg, 0, L[3]->LI);
213             CS_InsertEntry (S, X, I+7);
214
215             X = NewCodeEntry (OP65_LDA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
216             CS_InsertEntry (S, X, I+8);
217
218             X = NewCodeEntry (OP65_LDY, AM65_IMM, L[5]->Arg, 0, L[5]->LI);
219             CS_InsertEntry (S, X, I+9);
220
221             X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, L[6]->LI);
222             CS_InsertEntry (S, X, I+10);
223
224             X = NewCodeEntry (OP65_ADC, AM65_ZP_INDY, "sp", 0, L[6]->LI);
225             CS_InsertEntry (S, X, I+11);
226
227             X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "sp", 0, L[6]->LI);
228             CS_InsertEntry (S, X, I+12);
229
230             X = NewCodeEntry (OP65_LDY, AM65_IMM, L[0]->Arg, 0, L[0]->LI);
231             CS_InsertEntry (S, X, I+13);
232
233             X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
234             CS_InsertEntry (S, X, I+14);
235
236             xsprintf (Buf, sizeof (Buf), "$%02X", (int)(L[5]->Num+1));
237             X = NewCodeEntry (OP65_LDY, AM65_IMM, Buf, 0, L[5]->LI);
238             CS_InsertEntry (S, X, I+15);
239
240             X = NewCodeEntry (OP65_ADC, AM65_ZP_INDY, "sp", 0, L[6]->LI);
241             CS_InsertEntry (S, X, I+16);
242
243             X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "sp", 0, L[6]->LI);
244             CS_InsertEntry (S, X, I+17);
245
246             /* Delete the old code */
247             CS_DelEntries (S, I, 7);
248
249             /* Remember, we had changes */
250             ++Changes;
251
252         }
253
254         /* Next entry */
255         ++I;
256
257     }
258
259     /* Return the number of changes made */
260     return Changes;
261 }
262
263
264
265 unsigned OptAdd3 (CodeSeg* S)
266 /* Search for the sequence
267  *
268  *      adc     ...
269  *      bcc     L
270  *      inx
271  * L:
272  *
273  * and remove the handling of the high byte if X is not used later.
274  */
275 {
276     unsigned Changes = 0;
277
278     /* Walk over the entries */
279     unsigned I = 0;
280     while (I < CS_GetEntryCount (S)) {
281
282         CodeEntry* L[3];
283
284         /* Get next entry */
285         CodeEntry* E = CS_GetEntry (S, I);
286
287         /* Check for the sequence */
288         if (E->OPC == OP65_ADC                               &&
289             CS_GetEntries (S, L, I+1, 3)                     &&
290             (L[0]->OPC == OP65_BCC || L[0]->OPC == OP65_JCC) &&
291             L[0]->JumpTo != 0                                &&
292             !CE_HasLabel (L[0])                              &&
293             L[1]->OPC == OP65_INX                            &&
294             !CE_HasLabel (L[1])                              &&
295             L[0]->JumpTo->Owner == L[2]                      &&
296             !RegXUsed (S, I+3)) {
297
298             /* Remove the bcs/dex */
299             CS_DelEntries (S, I+1, 2);
300
301             /* Remember, we had changes */
302             ++Changes;
303
304         }
305
306         /* Next entry */
307         ++I;
308
309     }
310
311     /* Return the number of changes made */
312     return Changes;
313 }
314
315
316
317