1 /*****************************************************************************/
5 /* Optimize loads through pointers */
9 /* (C) 2001-2009 Ullrich von Bassewitz */
10 /* Roemerstrasse 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 /*****************************************************************************/
44 #include "coptptrload.h"
48 /*****************************************************************************/
50 /*****************************************************************************/
54 unsigned OptPtrLoad1 (CodeSeg* S)
55 /* Search for the sequence:
81 /* Walk over the entries */
83 while (I < CS_GetEntryCount (S)) {
88 L[0] = CS_GetEntry (S, I);
90 /* Check for the sequence */
91 if (L[0]->OPC == OP65_CLC &&
92 CS_GetEntries (S, L+1, I+1, 8) &&
93 L[1]->OPC == OP65_ADC &&
94 (L[1]->AM == AM65_ABS ||
95 L[1]->AM == AM65_ZP ||
96 L[1]->AM == AM65_IMM) &&
97 L[2]->OPC == OP65_TAY &&
98 L[3]->OPC == OP65_TXA &&
99 L[4]->OPC == OP65_ADC &&
100 L[5]->OPC == OP65_TAX &&
101 L[6]->OPC == OP65_TYA &&
102 L[7]->OPC == OP65_LDY &&
103 CE_IsKnownImm (L[7], 0) &&
104 CE_IsCallTo (L[8], "ldauidx") &&
105 !CS_RangeHasLabel (S, I+1, 8)) {
110 /* Track the insertion point */
114 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[2]->LI);
115 CS_InsertEntry (S, X, IP++);
117 /* If the instruction before the clc is a ldx, replace the
118 * txa by an lda with the same location of the ldx. Otherwise
119 * transfer the value in X to A.
121 if ((P = CS_GetPrevEntry (S, I)) != 0 &&
122 P->OPC == OP65_LDX &&
124 X = NewCodeEntry (OP65_LDA, P->AM, P->Arg, 0, P->LI);
126 X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, L[3]->LI);
128 CS_InsertEntry (S, X, IP++);
130 /* clc is now in the right place */
134 X = NewCodeEntry (OP65_ADC, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
135 CS_InsertEntry (S, X, IP++);
138 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[5]->LI);
139 CS_InsertEntry (S, X, IP++);
142 X = NewCodeEntry (OP65_LDY, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
143 CS_InsertEntry (S, X, IP++);
146 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[8]->LI);
147 CS_InsertEntry (S, X, IP++);
150 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[8]->LI);
151 CS_InsertEntry (S, X, IP++);
153 /* Remove the old instructions */
154 CS_DelEntries (S, IP, 8);
156 /* Remember, we had changes */
166 /* Return the number of changes made */
172 unsigned OptPtrLoad2 (CodeSeg* S)
173 /* Search for the sequence:
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_ADC &&
211 CS_GetEntries (S, L+1, I+1, 8) &&
212 L[1]->OPC == OP65_PHA &&
213 L[2]->OPC == OP65_TXA &&
214 L[3]->OPC == OP65_INY &&
215 L[4]->OPC == OP65_ADC &&
216 L[5]->OPC == OP65_TAX &&
217 L[6]->OPC == OP65_PLA &&
218 L[7]->OPC == OP65_LDY &&
219 CE_IsCallTo (L[8], "ldauidx") &&
220 !CS_RangeHasLabel (S, I+1, 8)) {
224 /* Store the low byte and remove the PHA instead */
225 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
226 CS_InsertEntry (S, X, I+1);
228 /* Store the high byte */
229 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[4]->LI);
230 CS_InsertEntry (S, X, I+6);
232 /* Load high and low byte */
233 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[6]->LI);
234 CS_InsertEntry (S, X, I+10);
235 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[6]->LI);
236 CS_InsertEntry (S, X, I+11);
238 /* Delete the old code */
239 CS_DelEntry (S, I+12); /* jsr ldauidx */
240 CS_DelEntry (S, I+8); /* pla */
241 CS_DelEntry (S, I+7); /* tax */
242 CS_DelEntry (S, I+2); /* pha */
244 /* Remember, we had changes */
254 /* Return the number of changes made */
260 unsigned OptPtrLoad3 (CodeSeg* S)
261 /* Search for the sequence:
279 unsigned Changes = 0;
281 /* Walk over the entries */
283 while (I < CS_GetEntryCount (S)) {
289 L[0] = CS_GetEntry (S, I);
291 /* Check for the sequence */
292 if (L[0]->OPC == OP65_LDA &&
293 L[0]->AM == AM65_IMM &&
294 CS_GetEntries (S, L+1, I+1, 7) &&
295 L[1]->OPC == OP65_LDX &&
296 L[1]->AM == AM65_IMM &&
297 L[2]->OPC == OP65_CLC &&
298 L[3]->OPC == OP65_ADC &&
299 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
300 (L[4]->OPC == OP65_BCC || L[4]->OPC == OP65_JCC) &&
302 L[4]->JumpTo->Owner == L[6] &&
303 L[5]->OPC == OP65_INX &&
304 L[6]->OPC == OP65_LDY &&
305 CE_IsKnownImm (L[6], 0) &&
306 CE_IsCallTo (L[7], "ldauidx") &&
307 !CS_RangeHasLabel (S, I+1, 5) &&
308 !CE_HasLabel (L[7]) &&
309 /* Check the label last because this is quite costly */
310 (Len = strlen (L[0]->Arg)) > 3 &&
311 L[0]->Arg[0] == '<' &&
312 L[0]->Arg[1] == '(' &&
313 strlen (L[1]->Arg) == Len &&
314 L[1]->Arg[0] == '>' &&
315 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
320 /* We will create all the new stuff behind the current one so
321 * we keep the line references.
323 X = NewCodeEntry (OP65_LDY, L[3]->AM, L[3]->Arg, 0, L[0]->LI);
324 CS_InsertEntry (S, X, I+8);
326 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
327 CS_InsertEntry (S, X, I+9);
329 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
331 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
332 CS_InsertEntry (S, X, I+10);
335 /* Remove the old code */
336 CS_DelEntries (S, I, 8);
338 /* Remember, we had changes */
348 /* Return the number of changes made */
354 unsigned OptPtrLoad4 (CodeSeg* S)
355 /* Search for the sequence:
376 unsigned Changes = 0;
378 /* Walk over the entries */
380 while (I < CS_GetEntryCount (S)) {
386 L[0] = CS_GetEntry (S, I);
388 /* Check for the sequence */
389 if (L[0]->OPC == OP65_LDA &&
390 L[0]->AM == AM65_IMM &&
391 CS_GetEntries (S, L+1, I+1, 8) &&
392 L[1]->OPC == OP65_LDX &&
393 L[1]->AM == AM65_IMM &&
394 !CE_HasLabel (L[1]) &&
395 L[2]->OPC == OP65_LDY &&
396 CE_IsConstImm (L[2]) &&
397 !CE_HasLabel (L[2]) &&
398 L[3]->OPC == OP65_CLC &&
399 !CE_HasLabel (L[3]) &&
400 L[4]->OPC == OP65_ADC &&
401 L[4]->AM == AM65_ZP_INDY &&
402 !CE_HasLabel (L[4]) &&
403 (L[5]->OPC == OP65_BCC || L[5]->OPC == OP65_JCC) &&
405 L[5]->JumpTo->Owner == L[7] &&
406 !CE_HasLabel (L[5]) &&
407 L[6]->OPC == OP65_INX &&
408 !CE_HasLabel (L[6]) &&
409 L[7]->OPC == OP65_LDY &&
410 CE_IsKnownImm (L[7], 0) &&
411 CE_IsCallTo (L[8], "ldauidx") &&
412 !CE_HasLabel (L[8]) &&
413 /* Check the label last because this is quite costly */
414 (Len = strlen (L[0]->Arg)) > 3 &&
415 L[0]->Arg[0] == '<' &&
416 L[0]->Arg[1] == '(' &&
417 strlen (L[1]->Arg) == Len &&
418 L[1]->Arg[0] == '>' &&
419 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
425 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[4]->Arg, 0, L[0]->LI);
426 CS_InsertEntry (S, X, I+3);
429 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, L[0]->LI);
430 CS_InsertEntry (S, X, I+4);
433 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
434 CS_InsertEntry (S, X, I+5);
437 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
439 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
440 CS_InsertEntry (S, X, I+6);
443 /* Remove the old code */
444 CS_DelEntries (S, I, 2);
445 CS_DelEntries (S, I+5, 6);
447 /* Remember, we had changes */
457 /* Return the number of changes made */
463 unsigned OptPtrLoad5 (CodeSeg* S)
464 /* Search for the sequence:
482 unsigned Changes = 0;
484 /* Walk over the entries */
486 while (I < CS_GetEntryCount (S)) {
491 L[0] = CS_GetEntry (S, I);
493 /* Check for the sequence */
494 if (CE_IsCallTo (L[0], "pushax") &&
495 CS_GetEntries (S, L+1, I+1, 5) &&
496 L[1]->OPC == OP65_LDX &&
497 CE_IsKnownImm (L[1], 0) &&
498 L[2]->OPC == OP65_LDA &&
499 (L[2]->AM == AM65_ABS ||
500 L[2]->AM == AM65_ZP ||
501 L[2]->AM == AM65_IMM) &&
502 CE_IsCallTo (L[3], "tosaddax") &&
503 L[4]->OPC == OP65_LDY &&
504 CE_IsKnownImm (L[4], 0) &&
505 CE_IsCallTo (L[5], "ldauidx") &&
506 !CS_RangeHasLabel (S, I+1, 5)) {
511 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
512 CS_InsertEntry (S, X, I+6);
515 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
516 CS_InsertEntry (S, X, I+7);
519 X = NewCodeEntry (OP65_LDY, L[2]->AM, L[2]->Arg, 0, L[2]->LI);
520 CS_InsertEntry (S, X, I+8);
523 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[5]->LI);
524 CS_InsertEntry (S, X, I+9);
527 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[5]->LI);
528 CS_InsertEntry (S, X, I+10);
530 /* Remove the old code */
531 CS_DelEntries (S, I, 6);
533 /* Remember, we had changes */
543 /* Return the number of changes made */
549 unsigned OptPtrLoad6 (CodeSeg* S)
550 /* Search for the sequence:
571 unsigned Changes = 0;
573 /* Walk over the entries */
575 while (I < CS_GetEntryCount (S)) {
580 L[0] = CS_GetEntry (S, I);
582 /* Check for the sequence */
583 if (CE_IsCallTo (L[0], "pushax") &&
584 CS_GetEntries (S, L+1, I+1, 6) &&
585 L[1]->OPC == OP65_LDY &&
586 L[2]->OPC == OP65_LDX &&
587 CE_IsKnownImm (L[2], 0) &&
588 L[3]->OPC == OP65_LDA &&
589 L[3]->AM == AM65_ZP_INDY &&
590 CE_IsCallTo (L[4], "tosaddax") &&
591 L[5]->OPC == OP65_LDY &&
592 CE_IsKnownImm (L[5], 0) &&
593 CE_IsCallTo (L[6], "ldauidx") &&
594 !CS_RangeHasLabel (S, I+1, 6)) {
599 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
600 CS_InsertEntry (S, X, I+7);
603 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
604 CS_InsertEntry (S, X, I+8);
607 X = NewCodeEntry (OP65_LDY, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
608 CS_InsertEntry (S, X, I+9);
611 X = NewCodeEntry (OP65_LDA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
612 CS_InsertEntry (S, X, I+10);
615 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, L[3]->LI);
616 CS_InsertEntry (S, X, I+11);
619 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[5]->LI);
620 CS_InsertEntry (S, X, I+12);
623 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[6]->LI);
624 CS_InsertEntry (S, X, I+13);
626 /* Remove the old code */
627 CS_DelEntries (S, I, 7);
629 /* Remember, we had changes */
639 /* Return the number of changes made */
645 unsigned OptPtrLoad7 (CodeSeg* S)
646 /* Search for the sequence:
677 unsigned Changes = 0;
680 /* Generate register info */
683 /* Walk over the entries */
685 while (I < CS_GetEntryCount (S)) {
690 L[0] = CS_GetEntry (S, I);
692 /* Check for the sequence */
693 if (L[0]->OPC == OP65_JSR &&
694 (strcmp (L[0]->Arg, "aslax1") == 0 ||
695 strcmp (L[0]->Arg, "shlax1") == 0) &&
696 CS_GetEntries (S, L+1, I+1, 9) &&
697 L[1]->OPC == OP65_CLC &&
698 L[2]->OPC == OP65_ADC &&
699 L[3]->OPC == OP65_TAY &&
700 L[4]->OPC == OP65_TXA &&
701 L[5]->OPC == OP65_ADC &&
702 L[6]->OPC == OP65_TAX &&
703 L[7]->OPC == OP65_TYA &&
704 L[8]->OPC == OP65_LDY &&
705 CE_IsCallTo (L[9], "ldaxidx") &&
706 !CS_RangeHasLabel (S, I+1, 9)) {
710 /* Track the insertion point */
711 unsigned IP = I + 10;
714 /* If X is zero on entry to aslax1, we can generate:
721 * instead of the code above. "lda tmp1" needs to be changed
722 * to "txa" in this case.
724 int ShortCode = (L[0]->RI->In.RegX == 0);
731 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, L[0]->LI);
732 CS_InsertEntry (S, X, IP++);
734 /* Generate clc first, since we need the label */
735 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, L[1]->LI);
736 CS_InsertEntry (S, X, IP);
739 Lab = CS_GenLabel (S, X);
742 X = NewCodeEntry (OP65_BCC, AM65_BRA, Lab->Name, Lab, L[0]->LI);
743 CS_InsertEntry (S, X, IP++);
746 X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, L[0]->LI);
747 CS_InsertEntry (S, X, IP++);
749 /* Skip the clc insn */
755 X = NewCodeEntry (OP65_STX, AM65_ZP, "tmp1", 0, L[0]->LI);
756 CS_InsertEntry (S, X, IP++);
759 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, L[0]->LI);
760 CS_InsertEntry (S, X, IP++);
763 X = NewCodeEntry (OP65_ROL, AM65_ZP, "tmp1", 0, L[0]->LI);
764 CS_InsertEntry (S, X, IP++);
767 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, L[1]->LI);
768 CS_InsertEntry (S, X, IP++);
773 X = NewCodeEntry (L[2]->OPC, L[2]->AM, L[2]->Arg, 0, L[2]->LI);
774 CS_InsertEntry (S, X, IP++);
777 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[9]->LI);
778 CS_InsertEntry (S, X, IP++);
782 X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, L[4]->LI);
785 X = NewCodeEntry (OP65_LDA, AM65_ZP, "tmp1", 0, L[4]->LI);
787 CS_InsertEntry (S, X, IP++);
790 X = NewCodeEntry (L[5]->OPC, L[5]->AM, L[5]->Arg, 0, L[5]->LI);
791 CS_InsertEntry (S, X, IP++);
794 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[9]->LI);
795 CS_InsertEntry (S, X, IP++);
798 X = NewCodeEntry (L[8]->OPC, L[8]->AM, L[8]->Arg, 0, L[8]->LI);
799 CS_InsertEntry (S, X, IP++);
802 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[9]->LI);
803 CS_InsertEntry (S, X, IP++);
806 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, L[9]->LI);
807 CS_InsertEntry (S, X, IP++);
810 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, L[9]->LI);
811 CS_InsertEntry (S, X, IP++);
814 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[9]->LI);
815 CS_InsertEntry (S, X, IP++);
817 /* Remove the old code */
818 CS_DelEntries (S, I, 10);
820 /* Remember, we had changes */
830 /* Free the register info */
833 /* Return the number of changes made */
839 unsigned OptPtrLoad11 (CodeSeg* S)
840 /* Search for the sequence:
858 unsigned Changes = 0;
860 /* Walk over the entries */
862 while (I < CS_GetEntryCount (S)) {
867 L[0] = CS_GetEntry (S, I);
869 /* Check for the sequence */
870 if (L[0]->OPC == OP65_CLC &&
871 CS_GetEntries (S, L+1, I+1, 5) &&
872 L[1]->OPC == OP65_ADC &&
873 (L[1]->AM == AM65_ABS || L[1]->AM == AM65_ZP || L[1]->AM == AM65_IMM) &&
874 (L[2]->OPC == OP65_BCC || L[2]->OPC == OP65_JCC) &&
876 L[2]->JumpTo->Owner == L[4] &&
877 L[3]->OPC == OP65_INX &&
878 L[4]->OPC == OP65_LDY &&
879 CE_IsKnownImm (L[4], 0) &&
880 CE_IsCallTo (L[5], "ldauidx") &&
881 !CS_RangeHasLabel (S, I+1, 3) &&
882 !CE_HasLabel (L[5])) {
886 /* We will create all the new stuff behind the current one so
887 * we keep the line references.
889 X = NewCodeEntry (OP65_LDY, L[1]->AM, L[1]->Arg, 0, L[0]->LI);
890 CS_InsertEntry (S, X, I+6);
893 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
894 CS_InsertEntry (S, X, I+7);
897 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
898 CS_InsertEntry (S, X, I+8);
901 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
902 CS_InsertEntry (S, X, I+9);
904 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[0]->LI);
905 CS_InsertEntry (S, X, I+10);
907 /* Remove the old code */
908 CS_DelEntries (S, I, 6);
910 /* Remember, we had changes */
920 /* Return the number of changes made */
926 unsigned OptPtrLoad12 (CodeSeg* S)
927 /* Search for the sequence:
952 * L1: tay <- only if flags are used
954 * This function must execute before OptPtrLoad7!
958 unsigned Changes = 0;
960 /* Walk over the entries */
962 while (I < CS_GetEntryCount (S)) {
968 L[0] = CS_GetEntry (S, I);
970 /* Check for the sequence */
971 if (L[0]->OPC == OP65_LDA &&
972 L[0]->AM == AM65_ZP &&
973 strncmp (L[0]->Arg, "regbank+", 8) == 0 &&
974 (Len = strlen (L[0]->Arg)) > 0 &&
975 CS_GetEntries (S, L+1, I+1, 14) &&
976 !CS_RangeHasLabel (S, I+1, 7) &&
977 !CS_RangeHasLabel (S, I+9, 5) &&
978 L[1]->OPC == OP65_LDX &&
979 L[1]->AM == AM65_ZP &&
980 strncmp (L[1]->Arg, L[0]->Arg, Len) == 0 &&
981 strcmp (L[1]->Arg+Len, "+1") == 0 &&
982 L[2]->OPC == OP65_STA &&
983 L[2]->AM == AM65_ZP &&
984 strcmp (L[2]->Arg, "regsave") == 0 &&
985 L[3]->OPC == OP65_STX &&
986 L[3]->AM == AM65_ZP &&
987 strcmp (L[3]->Arg, "regsave+1") == 0 &&
988 L[4]->OPC == OP65_CLC &&
989 L[5]->OPC == OP65_ADC &&
990 CE_IsKnownImm (L[5], 1) &&
991 L[6]->OPC == OP65_BCC &&
993 L[6]->JumpTo->Owner == L[8] &&
994 L[7]->OPC == OP65_INX &&
995 L[8]->OPC == OP65_STA &&
996 L[8]->AM == AM65_ZP &&
997 strcmp (L[8]->Arg, L[0]->Arg) == 0 &&
998 L[9]->OPC == OP65_STX &&
999 L[9]->AM == AM65_ZP &&
1000 strcmp (L[9]->Arg, L[1]->Arg) == 0 &&
1001 L[10]->OPC == OP65_LDA &&
1002 L[10]->AM == AM65_ZP &&
1003 strcmp (L[10]->Arg, "regsave") == 0 &&
1004 L[11]->OPC == OP65_LDX &&
1005 L[11]->AM == AM65_ZP &&
1006 strcmp (L[11]->Arg, "regsave+1") == 0 &&
1007 L[12]->OPC == OP65_LDY &&
1008 CE_IsConstImm (L[12]) &&
1009 CE_IsCallTo (L[13], "ldauidx")) {
1014 /* Check if the instruction following the sequence uses the flags
1015 * set by the load. If so, insert a test of the value in the
1018 if (CE_UseLoadFlags (L[14])) {
1019 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, L[13]->LI);
1020 CS_InsertEntry (S, X, I+14);
1023 /* Attach a label to L[14]. This may be either the just inserted
1024 * instruction, or the one following the sequence.
1026 Label = CS_GenLabel (S, L[14]);
1029 X = NewCodeEntry (OP65_LDY, AM65_IMM, L[12]->Arg, 0, L[12]->LI);
1030 CS_InsertEntry (S, X, I+14);
1033 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[13]->LI);
1034 CS_InsertEntry (S, X, I+15);
1036 /* lda (regbank+n),y */
1037 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[13]->LI);
1038 CS_InsertEntry (S, X, I+16);
1041 X = NewCodeEntry (OP65_INC, AM65_ZP, L[0]->Arg, 0, L[5]->LI);
1042 CS_InsertEntry (S, X, I+17);
1045 X = NewCodeEntry (OP65_BNE, AM65_BRA, Label->Name, Label, L[6]->LI);
1046 CS_InsertEntry (S, X, I+18);
1048 /* inc regbank+n+1 */
1049 X = NewCodeEntry (OP65_INC, AM65_ZP, L[1]->Arg, 0, L[7]->LI);
1050 CS_InsertEntry (S, X, I+19);
1052 /* Delete the old code */
1053 CS_DelEntries (S, I, 14);
1055 /* Remember, we had changes */
1065 /* Return the number of changes made */
1071 unsigned OptPtrLoad13 (CodeSeg* S)
1072 /* Search for the sequence:
1079 * and replace it by:
1086 unsigned Changes = 0;
1088 /* Walk over the entries */
1090 while (I < CS_GetEntryCount (S)) {
1095 /* Get next entry */
1096 L[0] = CS_GetEntry (S, I);
1098 /* Check for the sequence */
1099 if (L[0]->OPC == OP65_LDA && L[0]->AM == AM65_ZP &&
1100 CS_GetEntries (S, L+1, I+1, 3) &&
1101 !CS_RangeHasLabel (S, I+1, 3) &&
1102 L[1]->OPC == OP65_LDX && L[1]->AM == AM65_ZP &&
1103 (Len = strlen (L[0]->Arg)) > 0 &&
1104 strncmp (L[0]->Arg, L[1]->Arg, Len) == 0 &&
1105 strcmp (L[1]->Arg + Len, "+1") == 0 &&
1106 L[2]->OPC == OP65_LDY &&
1107 CE_IsCallTo (L[3], "ldauidx")) {
1112 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
1113 CS_InsertEntry (S, X, I+3);
1116 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[3]->LI);
1117 CS_InsertEntry (S, X, I+4);
1119 /* Remove the old code */
1120 CS_DelEntry (S, I+5);
1121 CS_DelEntries (S, I, 2);
1123 /* Remember, we had changes */
1133 /* Return the number of changes made */
1139 unsigned OptPtrLoad14 (CodeSeg* S)
1140 /* Search for the sequence:
1144 * (anything that doesn't change a/x)
1148 * and replace it by:
1152 * (anything that doesn't change a/x)
1159 unsigned Changes = 0;
1162 /* Generate register info */
1165 /* Walk over the entries */
1167 while (I < CS_GetEntryCount (S)) {
1172 /* Get next entry */
1173 L[0] = CS_GetEntry (S, I);
1175 /* Check for the sequence */
1176 if (L[0]->OPC == OP65_LDA && L[0]->AM == AM65_ZP &&
1177 CS_GetEntries (S, L+1, I+1, 4) &&
1178 !CS_RangeHasLabel (S, I+1, 4) &&
1179 L[1]->OPC == OP65_LDX && L[1]->AM == AM65_ZP &&
1180 (Len = strlen (L[0]->Arg)) > 0 &&
1181 strncmp (L[0]->Arg, L[1]->Arg, Len) == 0 &&
1182 strcmp (L[1]->Arg + Len, "+1") == 0 &&
1183 (L[2]->Chg & REG_AX) == 0 &&
1184 L[3]->OPC == OP65_LDY &&
1185 CE_IsCallTo (L[4], "ldauidx")) {
1190 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
1191 CS_InsertEntry (S, X, I+5);
1194 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[3]->LI);
1195 CS_InsertEntry (S, X, I+6);
1197 /* Remove the old code */
1198 CS_DelEntry (S, I+4);
1200 /* Remember, we had changes */
1210 /* Free the register info */
1213 /* Return the number of changes made */
1219 unsigned OptPtrLoad15 (CodeSeg* S)
1220 /* Search for the sequence:
1227 * and replace it by:
1236 unsigned Changes = 0;
1238 /* Walk over the entries */
1240 while (I < CS_GetEntryCount (S)) {
1245 /* Get next entry */
1246 L[0] = CS_GetEntry (S, I);
1248 /* Check for the sequence */
1249 if (L[0]->OPC == OP65_LDA && L[0]->AM == AM65_ZP &&
1250 CS_GetEntries (S, L+1, I+1, 3) &&
1251 !CS_RangeHasLabel (S, I+1, 3) &&
1252 L[1]->OPC == OP65_LDX && L[1]->AM == AM65_ZP &&
1253 (Len = strlen (L[0]->Arg)) > 0 &&
1254 strncmp (L[0]->Arg, L[1]->Arg, Len) == 0 &&
1255 strcmp (L[1]->Arg + Len, "+1") == 0 &&
1256 L[2]->OPC == OP65_LDY &&
1257 CE_IsCallTo (L[3], "ldaxidx")) {
1262 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[3]->LI);
1263 CS_InsertEntry (S, X, I+4);
1266 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, L[3]->LI);
1267 CS_InsertEntry (S, X, I+5);
1270 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, L[3]->LI);
1271 CS_InsertEntry (S, X, I+6);
1274 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[3]->LI);
1275 CS_InsertEntry (S, X, I+7);
1277 /* Remove the old code */
1278 CS_DelEntry (S, I+3);
1279 CS_DelEntries (S, I, 2);
1281 /* Remember, we had changes */
1291 /* Return the number of changes made */
1297 unsigned OptPtrLoad16 (CodeSeg* S)
1298 /* Search for the sequence
1303 * and replace it by:
1311 * This step must be executed *after* OptPtrLoad1!
1314 unsigned Changes = 0;
1316 /* Walk over the entries */
1318 while (I < CS_GetEntryCount (S)) {
1322 /* Get next entry */
1323 L[0] = CS_GetEntry (S, I);
1325 /* Check for the sequence */
1326 if (L[0]->OPC == OP65_LDY &&
1327 CS_GetEntries (S, L+1, I+1, 1) &&
1328 CE_IsCallTo (L[1], "ldauidx") &&
1329 !CE_HasLabel (L[1])) {
1333 /* Store the high byte */
1334 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
1335 CS_InsertEntry (S, X, I);
1337 /* Store the low byte */
1338 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
1339 CS_InsertEntry (S, X, I+1);
1341 /* Delete the call to ldauidx */
1342 CS_DelEntry (S, I+3);
1344 /* Load the high and low byte */
1345 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
1346 CS_InsertEntry (S, X, I+3);
1347 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[0]->LI);
1348 CS_InsertEntry (S, X, I+4);
1350 /* Remember, we had changes */
1360 /* Return the number of changes made */
1366 unsigned OptPtrLoad17 (CodeSeg* S)
1367 /* Search for the sequence
1372 * and replace it by:
1382 * This step must be executed *after* OptPtrLoad9! While code size increases
1383 * by more than 200%, inlining will greatly improve visibility for the
1384 * optimizer, so often part of the code gets improved later. So we will mark
1385 * the step with less than 200% so it gets executed when -Oi is in effect.
1388 unsigned Changes = 0;
1390 /* Walk over the entries */
1392 while (I < CS_GetEntryCount (S)) {
1396 /* Get next entry */
1397 L[0] = CS_GetEntry (S, I);
1399 /* Check for the sequence */
1400 if (L[0]->OPC == OP65_LDY &&
1401 CS_GetEntries (S, L+1, I+1, 1) &&
1402 CE_IsCallTo (L[1], "ldaxidx") &&
1403 !CE_HasLabel (L[1])) {
1407 /* Store the high byte */
1408 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
1409 CS_InsertEntry (S, X, I+2);
1411 /* Store the low byte */
1412 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
1413 CS_InsertEntry (S, X, I+3);
1416 X = NewCodeEntry (L[0]->OPC, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
1417 CS_InsertEntry (S, X, I+4);
1420 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[1]->LI);
1421 CS_InsertEntry (S, X, I+5);
1424 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, L[1]->LI);
1425 CS_InsertEntry (S, X, I+6);
1428 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, L[1]->LI);
1429 CS_InsertEntry (S, X, I+7);
1432 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[1]->LI);
1433 CS_InsertEntry (S, X, I+8);
1435 /* Delete original sequence */
1436 CS_DelEntries (S, I, 2);
1438 /* Remember, we had changes */
1448 /* Return the number of changes made */