1 /*****************************************************************************/
5 /* Optimize addition sequences */
9 /* (C) 2001-2005, Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
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 /*****************************************************************************/
47 /* Optimize additions */
48 /*****************************************************************************/
52 unsigned OptAdd1 (CodeSeg* S)
53 /* Search for the sequence
80 /* Walk over the entries */
82 while (I < CS_GetEntryCount (S)) {
87 L[0] = CS_GetEntry (S, I);
89 /* Check for the sequence */
90 if (L[0]->OPC == OP65_LDY &&
91 CE_IsConstImm (L[0]) &&
92 !CS_RangeHasLabel (S, I+1, 5) &&
93 CS_GetEntries (S, L+1, I+1, 5) &&
94 CE_IsCallTo (L[1], "ldaxysp") &&
95 CE_IsCallTo (L[2], "pushax") &&
96 L[3]->OPC == OP65_LDY &&
97 CE_IsConstImm (L[3]) &&
98 CE_IsCallTo (L[4], "ldaxysp") &&
99 CE_IsCallTo (L[5], "tosaddax")) {
104 /* Correct the stack of the first Y register load */
105 CE_SetNumArg (L[0], L[0]->Num - 1);
108 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
109 CS_InsertEntry (S, X, I+1);
112 Arg = MakeHexArg (L[3]->Num - 3);
113 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, L[4]->LI);
114 CS_InsertEntry (S, X, I+2);
117 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, L[5]->LI);
118 CS_InsertEntry (S, X, I+3);
121 X = NewCodeEntry (OP65_ADC, AM65_ZP_INDY, "sp", 0, L[5]->LI);
122 CS_InsertEntry (S, X, I+4);
125 X = NewCodeEntry (OP65_PHA, AM65_IMP, 0, 0, L[5]->LI);
126 CS_InsertEntry (S, X, I+5);
128 /* ldy #xx (beware: L[0] has changed) */
129 Arg = MakeHexArg (L[0]->Num + 1);
130 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, L[1]->LI);
131 CS_InsertEntry (S, X, I+6);
134 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
135 CS_InsertEntry (S, X, I+7);
138 Arg = MakeHexArg (L[3]->Num - 2);
139 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, L[4]->LI);
140 CS_InsertEntry (S, X, I+8);
143 X = NewCodeEntry (OP65_ADC, AM65_ZP_INDY, "sp", 0, L[5]->LI);
144 CS_InsertEntry (S, X, I+9);
147 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, L[5]->LI);
148 CS_InsertEntry (S, X, I+10);
151 X = NewCodeEntry (OP65_PLA, AM65_IMP, 0, 0, L[5]->LI);
152 CS_InsertEntry (S, X, I+11);
154 /* Delete the old code */
155 CS_DelEntries (S, I+12, 5);
157 /* Remember, we had changes */
167 /* Return the number of changes made */
173 unsigned OptAdd2 (CodeSeg* S)
174 /* Search for the sequence
195 * provided that a/x is not used later.
198 unsigned Changes = 0;
200 /* Walk over the entries */
202 while (I < CS_GetEntryCount (S)) {
207 L[0] = CS_GetEntry (S, I);
209 /* Check for the sequence */
210 if (L[0]->OPC == OP65_LDY &&
211 CE_IsConstImm (L[0]) &&
212 !CS_RangeHasLabel (S, I+1, 3) &&
213 CS_GetEntries (S, L+1, I+1, 3) &&
214 CE_IsCallTo (L[1], "ldaxysp") &&
215 L[2]->OPC == OP65_LDY &&
216 CE_IsConstImm (L[2]) &&
217 CE_IsCallTo (L[3], "addeqysp") &&
218 (GetRegInfo (S, I+4, REG_AX) & REG_AX) == 0) {
220 /* Insert new code behind the addeqysp */
225 Arg = MakeHexArg (L[0]->Num-1);
226 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, L[0]->LI);
227 CS_InsertEntry (S, X, I+4);
230 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
231 CS_InsertEntry (S, X, I+5);
234 X = NewCodeEntry (OP65_LDY, AM65_IMM, L[2]->Arg, 0, L[2]->LI);
235 CS_InsertEntry (S, X, I+6);
238 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, L[3]->LI);
239 CS_InsertEntry (S, X, I+7);
242 X = NewCodeEntry (OP65_ADC, AM65_ZP_INDY, "sp", 0, L[3]->LI);
243 CS_InsertEntry (S, X, I+8);
246 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "sp", 0, L[3]->LI);
247 CS_InsertEntry (S, X, I+9);
250 X = NewCodeEntry (OP65_LDY, AM65_IMM, L[0]->Arg, 0, L[0]->LI);
251 CS_InsertEntry (S, X, I+10);
254 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "sp", 0, L[1]->LI);
255 CS_InsertEntry (S, X, I+11);
258 Arg = MakeHexArg (L[2]->Num+1);
259 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, L[2]->LI);
260 CS_InsertEntry (S, X, I+12);
263 X = NewCodeEntry (OP65_ADC, AM65_ZP_INDY, "sp", 0, L[3]->LI);
264 CS_InsertEntry (S, X, I+13);
267 X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "sp", 0, L[3]->LI);
268 CS_InsertEntry (S, X, I+14);
270 /* Delete the old code */
271 CS_DelEntries (S, I, 4);
273 /* Remember, we had changes */
283 /* Return the number of changes made */
289 unsigned OptAdd3 (CodeSeg* S)
290 /* Search for the sequence
306 unsigned Changes = 0;
308 /* Walk over the entries */
310 while (I < CS_GetEntryCount (S)) {
315 L[0] = CS_GetEntry (S, I);
317 /* Check for the sequence */
318 if (CE_IsCallTo (L[0], "pushax") &&
319 CS_GetEntries (S, L+1, I+1, 4) &&
320 !CS_RangeHasLabel (S, I+1, 3) &&
321 L[1]->OPC == OP65_LDX &&
322 CE_IsKnownImm (L[1], 0) &&
323 L[2]->OPC == OP65_LDA &&
324 CE_IsCallTo (L[3], "tosaddax")) {
329 /* Insert new code behind the sequence */
330 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, L[3]->LI);
331 CS_InsertEntry (S, X, I+4);
334 X = NewCodeEntry (OP65_ADC, L[2]->AM, L[2]->Arg, 0, L[3]->LI);
335 CS_InsertEntry (S, X, I+5);
338 Label = CS_GenLabel (S, L[4]);
339 X = NewCodeEntry (OP65_BCC, AM65_BRA, Label->Name, Label, L[3]->LI);
340 CS_InsertEntry (S, X, I+6);
343 X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, L[3]->LI);
344 CS_InsertEntry (S, X, I+7);
346 /* Delete the old code */
347 CS_DelEntries (S, I, 4);
349 /* Remember, we had changes */
359 /* Return the number of changes made */
365 unsigned OptAdd4 (CodeSeg* S)
366 /* Search for the sequence
384 unsigned Changes = 0;
386 /* Walk over the entries */
388 while (I < CS_GetEntryCount (S)) {
393 L[0] = CS_GetEntry (S, I);
395 /* Check for the sequence */
396 if (CE_IsCallTo (L[0], "pushax") &&
397 CS_GetEntries (S, L+1, I+1, 3) &&
398 !CS_RangeHasLabel (S, I+1, 3) &&
399 L[1]->OPC == OP65_LDA &&
400 (L[1]->AM == AM65_ABS || L[1]->AM == AM65_ZP) &&
401 L[2]->OPC == OP65_LDX &&
402 (L[2]->AM == AM65_ABS || L[2]->AM == AM65_ZP) &&
403 CE_IsCallTo (L[3], "tosaddax")) {
407 /* Insert new code behind the sequence */
408 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, L[3]->LI);
409 CS_InsertEntry (S, X, I+4);
412 X = NewCodeEntry (OP65_ADC, L[1]->AM, L[1]->Arg, 0, L[3]->LI);
413 CS_InsertEntry (S, X, I+5);
416 X = NewCodeEntry (OP65_PHA, AM65_IMP, 0, 0, L[3]->LI);
417 CS_InsertEntry (S, X, I+6);
420 X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, L[3]->LI);
421 CS_InsertEntry (S, X, I+7);
424 X = NewCodeEntry (OP65_ADC, L[2]->AM, L[2]->Arg, 0, L[3]->LI);
425 CS_InsertEntry (S, X, I+8);
428 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, L[3]->LI);
429 CS_InsertEntry (S, X, I+9);
432 X = NewCodeEntry (OP65_PLA, AM65_IMP, 0, 0, L[3]->LI);
433 CS_InsertEntry (S, X, I+10);
435 /* Delete the old code */
436 CS_DelEntries (S, I, 4);
438 /* Remember, we had changes */
448 /* Return the number of changes made */
454 unsigned OptAdd5 (CodeSeg* S)
455 /* Search for a call to incaxn and replace it by an 8 bit add if the X register
459 unsigned Changes = 0;
461 /* Walk over the entries */
463 while (I < CS_GetEntryCount (S)) {
468 E = CS_GetEntry (S, I);
470 /* Check for the sequence */
471 if (E->OPC == OP65_JSR &&
472 strncmp (E->Arg, "incax", 5) == 0 &&
473 IsDigit (E->Arg[5]) &&
475 !RegXUsed (S, I+1)) {
480 /* Insert new code behind the sequence */
481 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, E->LI);
482 CS_InsertEntry (S, X, I+1);
484 Arg = MakeHexArg (E->Arg[5] - '0');
485 X = NewCodeEntry (OP65_ADC, AM65_IMM, Arg, 0, E->LI);
486 CS_InsertEntry (S, X, I+2);
488 /* Delete the old code */
491 /* Remember, we had changes */
501 /* Return the number of changes made */
507 unsigned OptAdd6 (CodeSeg* S)
508 /* Search for the sequence
515 * and remove the handling of the high byte if X is not used later.
518 unsigned Changes = 0;
520 /* Walk over the entries */
522 while (I < CS_GetEntryCount (S)) {
527 CodeEntry* E = CS_GetEntry (S, I);
529 /* Check for the sequence */
530 if (E->OPC == OP65_ADC &&
531 CS_GetEntries (S, L, I+1, 3) &&
532 (L[0]->OPC == OP65_BCC || L[0]->OPC == OP65_JCC) &&
534 !CE_HasLabel (L[0]) &&
535 L[1]->OPC == OP65_INX &&
536 !CE_HasLabel (L[1]) &&
537 L[0]->JumpTo->Owner == L[2] &&
538 !RegXUsed (S, I+3)) {
540 /* Remove the bcs/dex */
541 CS_DelEntries (S, I+1, 2);
543 /* Remember, we had changes */
553 /* Return the number of changes made */