1 /*****************************************************************************/
5 /* Optimize addition sequences */
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 /*****************************************************************************/
46 /*****************************************************************************/
47 /* Optimize additions */
48 /*****************************************************************************/
52 unsigned OptAdd1 (CodeSeg* S)
53 /* Search for the sequence
73 /* Walk over the entries */
75 while (I < CS_GetEntryCount (S)) {
80 CodeEntry* E = CS_GetEntry (S, I);
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 &&
87 !CE_HasLabel (L[0]) &&
88 L[1]->OPC == OP65_LDX &&
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])) {
100 /* Remove the call to pushax */
103 /* Correct the stack offset (needed since pushax was removed) */
104 CE_SetNumArg (L[0], L[0]->Num - 2);
107 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, L[3]->LI);
108 CS_InsertEntry (S, X, I+1);
110 /* Remove the load */
111 CS_DelEntry (S, I+3); /* lda */
112 CS_DelEntry (S, I+2); /* ldx */
115 X = NewCodeEntry (OP65_ADC, AM65_ZP_INDY, "sp", 0, L[3]->LI);
116 CS_InsertEntry (S, X, I+2);
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);
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);
127 /* Delete the call to tosaddax */
128 CS_DelEntry (S, I+5);
130 /* Remember, we had changes */
140 /* Return the number of changes made */
146 unsigned OptAdd2 (CodeSeg* S)
147 /* Search for the sequence
171 * provided that a/x is not used later.
174 unsigned Changes = 0;
176 /* Walk over the entries */
178 while (I < CS_GetEntryCount (S)) {
183 L[0] = CS_GetEntry (S, I);
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) {
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);
215 X = NewCodeEntry (OP65_LDA, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
216 CS_InsertEntry (S, X, I+8);
218 X = NewCodeEntry (OP65_LDY, AM65_IMM, L[5]->Arg, 0, L[5]->LI);
219 CS_InsertEntry (S, X, I+9);
221 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, L[6]->LI);
222 CS_InsertEntry (S, X, I+10);
224 X = NewCodeEntry (OP65_ADC, AM65_ZP_INDY, "sp", 0, L[6]->LI);
225 CS_InsertEntry (S, X, I+11);
227 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "sp", 0, L[6]->LI);
228 CS_InsertEntry (S, X, I+12);
230 X = NewCodeEntry (OP65_LDY, AM65_IMM, L[0]->Arg, 0, L[0]->LI);
231 CS_InsertEntry (S, X, I+13);
233 X = NewCodeEntry (OP65_LDA, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
234 CS_InsertEntry (S, X, I+14);
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);
240 X = NewCodeEntry (OP65_ADC, AM65_ZP_INDY, "sp", 0, L[6]->LI);
241 CS_InsertEntry (S, X, I+16);
243 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "sp", 0, L[6]->LI);
244 CS_InsertEntry (S, X, I+17);
246 /* Delete the old code */
247 CS_DelEntries (S, I, 7);
249 /* Remember, we had changes */
259 /* Return the number of changes made */
265 unsigned OptAdd3 (CodeSeg* S)
266 /* Search for the sequence
273 * and remove the handling of the high byte if X is not used later.
276 unsigned Changes = 0;
278 /* Walk over the entries */
280 while (I < CS_GetEntryCount (S)) {
285 CodeEntry* E = CS_GetEntry (S, I);
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) &&
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)) {
298 /* Remove the bcs/dex */
299 CS_DelEntries (S, I+1, 2);
301 /* Remember, we had changes */
311 /* Return the number of changes made */