1 /*****************************************************************************/
5 /* Optimize addition sequences */
9 /* (C) 2001-2002 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 /*****************************************************************************/
45 /*****************************************************************************/
46 /* Optimize additions */
47 /*****************************************************************************/
51 unsigned OptAdd1 (CodeSeg* S)
52 /* Search for the sequence
79 /* Walk over the entries */
81 while (I < CS_GetEntryCount (S)) {
86 L[0] = CS_GetEntry (S, I);
88 /* Check for the sequence */
89 if (L[0]->OPC == OP65_LDY &&
91 !CS_RangeHasLabel (S, I+1, 5) &&
92 CS_GetEntries (S, L+1, I+1, 5) &&
93 CE_IsCallTo (L[1], "ldaxysp") &&
94 CE_IsCallTo (L[2], "pushax") &&
95 L[3]->OPC == OP65_LDY &&
97 CE_IsCallTo (L[4], "ldaxysp") &&
98 CE_IsCallTo (L[5], "tosaddax")) {
103 /* Correct the stack of the first Y register load */
104 CE_SetNumArg (L[0], L[0]->Num - 1);
107 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
108 CS_InsertEntry (S, X, I+1);
111 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, L[5]->LI);
112 CS_InsertEntry (S, X, I+2);
115 Arg = MakeHexArg (L[3]->Num - 3);
116 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, L[4]->LI);
117 CS_InsertEntry (S, X, I+3);
120 X = NewCodeEntry (OP65_ADC, AM65_ZP_INDY, "sp", 0, L[5]->LI);
121 CS_InsertEntry (S, X, I+4);
124 X = NewCodeEntry (OP65_PHA, AM65_IMP, 0, 0, L[5]->LI);
125 CS_InsertEntry (S, X, I+5);
127 /* ldy #xx (beware: L[0] has changed) */
128 Arg = MakeHexArg (L[0]->Num + 1);
129 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, L[1]->LI);
130 CS_InsertEntry (S, X, I+6);
133 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
134 CS_InsertEntry (S, X, I+7);
137 Arg = MakeHexArg (L[3]->Num - 2);
138 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, L[4]->LI);
139 CS_InsertEntry (S, X, I+8);
142 X = NewCodeEntry (OP65_ADC, AM65_ZP_INDY, "sp", 0, L[5]->LI);
143 CS_InsertEntry (S, X, I+9);
146 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, L[5]->LI);
147 CS_InsertEntry (S, X, I+10);
150 X = NewCodeEntry (OP65_PLA, AM65_IMP, 0, 0, L[5]->LI);
151 CS_InsertEntry (S, X, I+11);
153 /* Delete the old code */
154 CS_DelEntries (S, I+12, 5);
156 /* Remember, we had changes */
166 /* Return the number of changes made */
172 unsigned OptAdd2 (CodeSeg* S)
173 /* Search for the sequence
194 * provided that a/x is not used later.
197 unsigned Changes = 0;
199 /* Walk over the entries */
201 while (I < CS_GetEntryCount (S)) {
206 L[0] = CS_GetEntry (S, I);
208 /* Check for the sequence */
209 if (L[0]->OPC == OP65_LDY &&
210 CE_KnownImm (L[0]) &&
211 !CS_RangeHasLabel (S, I+1, 3) &&
212 CS_GetEntries (S, L+1, I+1, 3) &&
213 CE_IsCallTo (L[1], "ldaxysp") &&
214 L[2]->OPC == OP65_LDY &&
215 CE_KnownImm (L[2]) &&
216 CE_IsCallTo (L[3], "addeqysp") &&
217 (GetRegInfo (S, I+4, REG_AX) & REG_AX) == 0) {
219 /* Insert new code behind the addeqysp */
224 Arg = MakeHexArg (L[0]->Num-1);
225 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, L[0]->LI);
226 CS_InsertEntry (S, X, I+4);
229 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
230 CS_InsertEntry (S, X, I+5);
233 X = NewCodeEntry (OP65_LDY, AM65_IMM, L[2]->Arg, 0, L[2]->LI);
234 CS_InsertEntry (S, X, I+6);
237 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, L[3]->LI);
238 CS_InsertEntry (S, X, I+7);
241 X = NewCodeEntry (OP65_ADC, AM65_ZP_INDY, "sp", 0, L[3]->LI);
242 CS_InsertEntry (S, X, I+8);
245 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "sp", 0, L[3]->LI);
246 CS_InsertEntry (S, X, I+9);
249 X = NewCodeEntry (OP65_LDY, AM65_IMM, L[0]->Arg, 0, L[0]->LI);
250 CS_InsertEntry (S, X, I+10);
253 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
254 CS_InsertEntry (S, X, I+11);
257 Arg = MakeHexArg (L[2]->Num+1);
258 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, L[2]->LI);
259 CS_InsertEntry (S, X, I+12);
262 X = NewCodeEntry (OP65_ADC, AM65_ZP_INDY, "sp", 0, L[3]->LI);
263 CS_InsertEntry (S, X, I+13);
266 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "sp", 0, L[3]->LI);
267 CS_InsertEntry (S, X, I+14);
269 /* Delete the old code */
270 CS_DelEntries (S, I, 4);
272 /* Remember, we had changes */
282 /* Return the number of changes made */
288 unsigned OptAdd3 (CodeSeg* S)
289 /* Search for the sequence
307 unsigned Changes = 0;
309 /* Walk over the entries */
311 while (I < CS_GetEntryCount (S)) {
316 L[0] = CS_GetEntry (S, I);
318 /* Check for the sequence */
319 if (CE_IsCallTo (L[0], "pushax") &&
320 CS_GetEntries (S, L+1, I+1, 3) &&
321 !CS_RangeHasLabel (S, I+1, 3) &&
322 L[1]->OPC == OP65_LDA &&
323 (L[1]->AM == AM65_ABS || L[1]->AM == AM65_ZP) &&
324 L[2]->OPC == OP65_LDX &&
325 (L[2]->AM == AM65_ABS || L[2]->AM == AM65_ZP) &&
326 CE_IsCallTo (L[3], "tosaddax")) {
330 /* Insert new code behind the sequence */
331 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, L[3]->LI);
332 CS_InsertEntry (S, X, I+4);
335 X = NewCodeEntry (OP65_ADC, L[1]->AM, L[1]->Arg, 0, L[3]->LI);
336 CS_InsertEntry (S, X, I+5);
339 X = NewCodeEntry (OP65_PHA, AM65_IMP, 0, 0, L[3]->LI);
340 CS_InsertEntry (S, X, I+6);
343 X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, L[3]->LI);
344 CS_InsertEntry (S, X, I+7);
347 X = NewCodeEntry (OP65_ADC, L[2]->AM, L[2]->Arg, 0, L[3]->LI);
348 CS_InsertEntry (S, X, I+8);
351 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, L[3]->LI);
352 CS_InsertEntry (S, X, I+9);
355 X = NewCodeEntry (OP65_PLA, AM65_IMP, 0, 0, L[3]->LI);
356 CS_InsertEntry (S, X, I+10);
358 /* Delete the old code */
359 CS_DelEntries (S, I, 4);
361 /* Remember, we had changes */
371 /* Return the number of changes made */
377 unsigned OptAdd4 (CodeSeg* S)
378 /* Search for a call to incaxn and replace it by an 8 bit add if the X register
382 unsigned Changes = 0;
384 /* Walk over the entries */
386 while (I < CS_GetEntryCount (S)) {
391 E = CS_GetEntry (S, I);
393 /* Check for the sequence */
394 if (E->OPC == OP65_JSR &&
395 strncmp (E->Arg, "incax", 5) == 0 &&
396 isdigit (E->Arg[5]) &&
398 !RegXUsed (S, I+1)) {
403 /* Insert new code behind the sequence */
404 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, E->LI);
405 CS_InsertEntry (S, X, I+1);
407 Arg = MakeHexArg (E->Arg[5] - '0');
408 X = NewCodeEntry (OP65_ADC, AM65_IMM, Arg, 0, E->LI);
409 CS_InsertEntry (S, X, I+2);
411 /* Delete the old code */
414 /* Remember, we had changes */
424 /* Return the number of changes made */
430 unsigned OptAdd5 (CodeSeg* S)
431 /* Search for the sequence
438 * and remove the handling of the high byte if X is not used later.
441 unsigned Changes = 0;
443 /* Walk over the entries */
445 while (I < CS_GetEntryCount (S)) {
450 CodeEntry* E = CS_GetEntry (S, I);
452 /* Check for the sequence */
453 if (E->OPC == OP65_ADC &&
454 CS_GetEntries (S, L, I+1, 3) &&
455 (L[0]->OPC == OP65_BCC || L[0]->OPC == OP65_JCC) &&
457 !CE_HasLabel (L[0]) &&
458 L[1]->OPC == OP65_INX &&
459 !CE_HasLabel (L[1]) &&
460 L[0]->JumpTo->Owner == L[2] &&
461 !RegXUsed (S, I+3)) {
463 /* Remove the bcs/dex */
464 CS_DelEntries (S, I+1, 2);
466 /* Remember, we had changes */
476 /* Return the number of changes made */