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++);
131 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, L[0]->LI);
132 CS_InsertEntry (S, X, IP++);
135 X = NewCodeEntry (OP65_ADC, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
136 CS_InsertEntry (S, X, IP++);
139 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[5]->LI);
140 CS_InsertEntry (S, X, IP++);
143 X = NewCodeEntry (OP65_LDY, L[1]->AM, L[1]->Arg, 0, L[1]->LI);
144 CS_InsertEntry (S, X, IP++);
147 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[8]->LI);
148 CS_InsertEntry (S, X, IP++);
151 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[8]->LI);
152 CS_InsertEntry (S, X, IP++);
154 /* Remove the old instructions */
155 CS_DelEntries (S, I, 9);
157 /* Remember, we had changes */
167 /* Return the number of changes made */
173 unsigned OptPtrLoad2 (CodeSeg* S)
174 /* Search for the sequence:
186 ** and replace it by:
199 unsigned Changes = 0;
201 /* Walk over the entries */
203 while (I < CS_GetEntryCount (S)) {
208 L[0] = CS_GetEntry (S, I);
210 /* Check for the sequence */
211 if (L[0]->OPC == OP65_ADC &&
212 CS_GetEntries (S, L+1, I+1, 8) &&
213 L[1]->OPC == OP65_PHA &&
214 L[2]->OPC == OP65_TXA &&
215 L[3]->OPC == OP65_INY &&
216 L[4]->OPC == OP65_ADC &&
217 L[5]->OPC == OP65_TAX &&
218 L[6]->OPC == OP65_PLA &&
219 L[7]->OPC == OP65_LDY &&
220 CE_IsCallTo (L[8], "ldauidx") &&
221 !CS_RangeHasLabel (S, I+1, 8)) {
225 /* Store the low byte and remove the PHA instead */
226 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
227 CS_InsertEntry (S, X, I+1);
229 /* Store the high byte */
230 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[4]->LI);
231 CS_InsertEntry (S, X, I+6);
233 /* Load high and low byte */
234 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[6]->LI);
235 CS_InsertEntry (S, X, I+10);
236 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[6]->LI);
237 CS_InsertEntry (S, X, I+11);
239 /* Delete the old code */
240 CS_DelEntry (S, I+12); /* jsr ldauidx */
241 CS_DelEntry (S, I+8); /* pla */
242 CS_DelEntry (S, I+7); /* tax */
243 CS_DelEntry (S, I+2); /* pha */
245 /* Remember, we had changes */
255 /* Return the number of changes made */
261 unsigned OptPtrLoad3 (CodeSeg* S)
262 /* Search for the sequence:
273 ** and replace it by:
280 unsigned Changes = 0;
282 /* Walk over the entries */
284 while (I < CS_GetEntryCount (S)) {
290 L[0] = CS_GetEntry (S, I);
292 /* Check for the sequence */
293 if (L[0]->OPC == OP65_LDA &&
294 L[0]->AM == AM65_IMM &&
295 CS_GetEntries (S, L+1, I+1, 7) &&
296 L[1]->OPC == OP65_LDX &&
297 L[1]->AM == AM65_IMM &&
298 L[2]->OPC == OP65_CLC &&
299 L[3]->OPC == OP65_ADC &&
300 (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
301 (L[4]->OPC == OP65_BCC || L[4]->OPC == OP65_JCC) &&
303 L[4]->JumpTo->Owner == L[6] &&
304 L[5]->OPC == OP65_INX &&
305 L[6]->OPC == OP65_LDY &&
306 CE_IsKnownImm (L[6], 0) &&
307 CE_IsCallTo (L[7], "ldauidx") &&
308 !CS_RangeHasLabel (S, I+1, 5) &&
309 !CE_HasLabel (L[7]) &&
310 /* Check the label last because this is quite costly */
311 (Len = strlen (L[0]->Arg)) > 3 &&
312 L[0]->Arg[0] == '<' &&
313 L[0]->Arg[1] == '(' &&
314 strlen (L[1]->Arg) == Len &&
315 L[1]->Arg[0] == '>' &&
316 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
321 /* We will create all the new stuff behind the current one so
322 ** we keep the line references.
324 X = NewCodeEntry (OP65_LDY, L[3]->AM, L[3]->Arg, 0, L[0]->LI);
325 CS_InsertEntry (S, X, I+8);
327 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
328 CS_InsertEntry (S, X, I+9);
330 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
332 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
333 CS_InsertEntry (S, X, I+10);
336 /* Remove the old code */
337 CS_DelEntries (S, I, 8);
339 /* Remember, we had changes */
349 /* Return the number of changes made */
355 unsigned OptPtrLoad4 (CodeSeg* S)
356 /* Search for the sequence:
368 ** and replace it by:
377 unsigned Changes = 0;
379 /* Walk over the entries */
381 while (I < CS_GetEntryCount (S)) {
387 L[0] = CS_GetEntry (S, I);
389 /* Check for the sequence */
390 if (L[0]->OPC == OP65_LDA &&
391 L[0]->AM == AM65_IMM &&
392 CS_GetEntries (S, L+1, I+1, 8) &&
393 L[1]->OPC == OP65_LDX &&
394 L[1]->AM == AM65_IMM &&
395 !CE_HasLabel (L[1]) &&
396 L[2]->OPC == OP65_LDY &&
397 CE_IsConstImm (L[2]) &&
398 !CE_HasLabel (L[2]) &&
399 L[3]->OPC == OP65_CLC &&
400 !CE_HasLabel (L[3]) &&
401 L[4]->OPC == OP65_ADC &&
402 L[4]->AM == AM65_ZP_INDY &&
403 !CE_HasLabel (L[4]) &&
404 (L[5]->OPC == OP65_BCC || L[5]->OPC == OP65_JCC) &&
406 L[5]->JumpTo->Owner == L[7] &&
407 !CE_HasLabel (L[5]) &&
408 L[6]->OPC == OP65_INX &&
409 !CE_HasLabel (L[6]) &&
410 L[7]->OPC == OP65_LDY &&
411 CE_IsKnownImm (L[7], 0) &&
412 CE_IsCallTo (L[8], "ldauidx") &&
413 !CE_HasLabel (L[8]) &&
414 /* Check the label last because this is quite costly */
415 (Len = strlen (L[0]->Arg)) > 3 &&
416 L[0]->Arg[0] == '<' &&
417 L[0]->Arg[1] == '(' &&
418 strlen (L[1]->Arg) == Len &&
419 L[1]->Arg[0] == '>' &&
420 memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
426 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[4]->Arg, 0, L[0]->LI);
427 CS_InsertEntry (S, X, I+3);
430 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, L[0]->LI);
431 CS_InsertEntry (S, X, I+4);
434 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
435 CS_InsertEntry (S, X, I+5);
438 Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
440 X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
441 CS_InsertEntry (S, X, I+6);
444 /* Remove the old code */
445 CS_DelEntries (S, I, 2);
446 CS_DelEntries (S, I+5, 6);
448 /* Remember, we had changes */
458 /* Return the number of changes made */
464 unsigned OptPtrLoad5 (CodeSeg* S)
465 /* Search for the sequence:
474 ** and replace it by:
483 unsigned Changes = 0;
485 /* Walk over the entries */
487 while (I < CS_GetEntryCount (S)) {
492 L[0] = CS_GetEntry (S, I);
494 /* Check for the sequence */
495 if (CE_IsCallTo (L[0], "pushax") &&
496 CS_GetEntries (S, L+1, I+1, 5) &&
497 L[1]->OPC == OP65_LDX &&
498 CE_IsKnownImm (L[1], 0) &&
499 L[2]->OPC == OP65_LDA &&
500 (L[2]->AM == AM65_ABS ||
501 L[2]->AM == AM65_ZP ||
502 L[2]->AM == AM65_IMM) &&
503 CE_IsCallTo (L[3], "tosaddax") &&
504 L[4]->OPC == OP65_LDY &&
505 CE_IsKnownImm (L[4], 0) &&
506 CE_IsCallTo (L[5], "ldauidx") &&
507 !CS_RangeHasLabel (S, I+1, 5)) {
512 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
513 CS_InsertEntry (S, X, I+6);
516 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
517 CS_InsertEntry (S, X, I+7);
520 X = NewCodeEntry (OP65_LDY, L[2]->AM, L[2]->Arg, 0, L[2]->LI);
521 CS_InsertEntry (S, X, I+8);
524 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[5]->LI);
525 CS_InsertEntry (S, X, I+9);
528 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[5]->LI);
529 CS_InsertEntry (S, X, I+10);
531 /* Remove the old code */
532 CS_DelEntries (S, I, 6);
534 /* Remember, we had changes */
544 /* Return the number of changes made */
550 unsigned OptPtrLoad6 (CodeSeg* S)
551 /* Search for the sequence:
561 ** and replace it by:
572 unsigned Changes = 0;
574 /* Walk over the entries */
576 while (I < CS_GetEntryCount (S)) {
581 L[0] = CS_GetEntry (S, I);
583 /* Check for the sequence */
584 if (CE_IsCallTo (L[0], "pushax") &&
585 CS_GetEntries (S, L+1, I+1, 6) &&
586 L[1]->OPC == OP65_LDY &&
587 CE_IsConstImm (L[1]) &&
589 L[2]->OPC == OP65_LDX &&
590 CE_IsKnownImm (L[2], 0) &&
591 L[3]->OPC == OP65_LDA &&
592 L[3]->AM == AM65_ZP_INDY &&
593 CE_IsCallTo (L[4], "tosaddax") &&
594 L[5]->OPC == OP65_LDY &&
595 CE_IsKnownImm (L[5], 0) &&
596 CE_IsCallTo (L[6], "ldauidx") &&
597 !CS_RangeHasLabel (S, I+1, 6) &&
598 !RegYUsed (S, I+7)) {
604 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
605 CS_InsertEntry (S, X, I+7);
608 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
609 CS_InsertEntry (S, X, I+8);
612 Arg = MakeHexArg (L[1]->Num - 2);
613 X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, L[1]->LI);
614 CS_InsertEntry (S, X, I+9);
617 X = NewCodeEntry (OP65_LDA, L[3]->AM, L[3]->Arg, 0, L[3]->LI);
618 CS_InsertEntry (S, X, I+10);
621 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, L[3]->LI);
622 CS_InsertEntry (S, X, I+11);
625 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[5]->LI);
626 CS_InsertEntry (S, X, I+12);
629 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[6]->LI);
630 CS_InsertEntry (S, X, I+13);
632 /* Remove the old code */
633 CS_DelEntries (S, I, 7);
635 /* Remember, we had changes */
645 /* Return the number of changes made */
651 unsigned OptPtrLoad7 (CodeSeg* S)
652 /* Search for the sequence:
665 ** and replace it by:
683 unsigned Changes = 0;
686 /* Walk over the entries */
688 while (I < CS_GetEntryCount (S)) {
693 L[0] = CS_GetEntry (S, I);
695 /* Check for the sequence */
696 if (L[0]->OPC == OP65_JSR &&
697 (strcmp (L[0]->Arg, "aslax1") == 0 ||
698 strcmp (L[0]->Arg, "shlax1") == 0) &&
699 CS_GetEntries (S, L+1, I+1, 9) &&
700 L[1]->OPC == OP65_CLC &&
701 L[2]->OPC == OP65_ADC &&
702 L[3]->OPC == OP65_TAY &&
703 L[4]->OPC == OP65_TXA &&
704 L[5]->OPC == OP65_ADC &&
705 L[6]->OPC == OP65_TAX &&
706 L[7]->OPC == OP65_TYA &&
707 L[8]->OPC == OP65_LDY &&
708 CE_IsCallTo (L[9], "ldaxidx") &&
709 !CS_RangeHasLabel (S, I+1, 9)) {
713 /* Track the insertion point */
714 unsigned IP = I + 10;
717 /* If X is zero on entry to aslax1, we can generate:
724 ** instead of the code above. "lda tmp1" needs to be changed
725 ** to "txa" in this case.
727 int ShortCode = (L[0]->RI->In.RegX == 0);
734 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, L[0]->LI);
735 CS_InsertEntry (S, X, IP++);
737 /* Generate clc first, since we need the label */
738 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, L[1]->LI);
739 CS_InsertEntry (S, X, IP);
742 Lab = CS_GenLabel (S, X);
745 X = NewCodeEntry (OP65_BCC, AM65_BRA, Lab->Name, Lab, L[0]->LI);
746 CS_InsertEntry (S, X, IP++);
749 X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, L[0]->LI);
750 CS_InsertEntry (S, X, IP++);
752 /* Skip the clc insn */
758 X = NewCodeEntry (OP65_STX, AM65_ZP, "tmp1", 0, L[0]->LI);
759 CS_InsertEntry (S, X, IP++);
762 X = NewCodeEntry (OP65_ASL, AM65_ACC, "a", 0, L[0]->LI);
763 CS_InsertEntry (S, X, IP++);
766 X = NewCodeEntry (OP65_ROL, AM65_ZP, "tmp1", 0, L[0]->LI);
767 CS_InsertEntry (S, X, IP++);
770 X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, L[1]->LI);
771 CS_InsertEntry (S, X, IP++);
776 X = NewCodeEntry (L[2]->OPC, L[2]->AM, L[2]->Arg, 0, L[2]->LI);
777 CS_InsertEntry (S, X, IP++);
780 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[9]->LI);
781 CS_InsertEntry (S, X, IP++);
785 X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, L[4]->LI);
788 X = NewCodeEntry (OP65_LDA, AM65_ZP, "tmp1", 0, L[4]->LI);
790 CS_InsertEntry (S, X, IP++);
793 X = NewCodeEntry (L[5]->OPC, L[5]->AM, L[5]->Arg, 0, L[5]->LI);
794 CS_InsertEntry (S, X, IP++);
797 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[9]->LI);
798 CS_InsertEntry (S, X, IP++);
801 X = NewCodeEntry (L[8]->OPC, L[8]->AM, L[8]->Arg, 0, L[8]->LI);
802 CS_InsertEntry (S, X, IP++);
805 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[9]->LI);
806 CS_InsertEntry (S, X, IP++);
809 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, L[9]->LI);
810 CS_InsertEntry (S, X, IP++);
813 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, L[9]->LI);
814 CS_InsertEntry (S, X, IP++);
817 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[9]->LI);
818 CS_InsertEntry (S, X, IP++);
820 /* Remove the old code */
821 CS_DelEntries (S, I, 10);
823 /* Remember, we had changes */
833 /* Return the number of changes made */
839 unsigned OptPtrLoad11 (CodeSeg* S)
840 /* Search for the sequence:
849 ** and replace it by:
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:
944 ** and replace it by:
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)
1158 unsigned Changes = 0;
1161 /* Walk over the entries */
1163 while (I < CS_GetEntryCount (S)) {
1168 /* Get next entry */
1169 L[0] = CS_GetEntry (S, I);
1171 /* Check for the sequence */
1172 if (L[0]->OPC == OP65_LDA && L[0]->AM == AM65_ZP &&
1173 CS_GetEntries (S, L+1, I+1, 4) &&
1174 !CS_RangeHasLabel (S, I+1, 4) &&
1175 L[1]->OPC == OP65_LDX && L[1]->AM == AM65_ZP &&
1176 (Len = strlen (L[0]->Arg)) > 0 &&
1177 strncmp (L[0]->Arg, L[1]->Arg, Len) == 0 &&
1178 strcmp (L[1]->Arg + Len, "+1") == 0 &&
1179 (L[2]->Chg & REG_AX) == 0 &&
1180 L[3]->OPC == OP65_LDY &&
1181 CE_IsCallTo (L[4], "ldauidx")) {
1186 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
1187 CS_InsertEntry (S, X, I+5);
1190 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[3]->LI);
1191 CS_InsertEntry (S, X, I+6);
1193 /* Remove the old code */
1194 CS_DelEntry (S, I+4);
1196 /* Remember, we had changes */
1206 /* Return the number of changes made */
1212 unsigned OptPtrLoad15 (CodeSeg* S)
1213 /* Search for the sequence:
1217 ** jsr pushax <- optional
1221 ** and replace it by:
1223 ** lda zp <- only if
1224 ** ldx zp+1 <- call to
1225 ** jsr pushax <- pushax present
1233 unsigned Changes = 0;
1235 /* Walk over the entries */
1237 while (I < CS_GetEntryCount (S)) {
1242 /* Check for the start of the sequence */
1243 if (CS_GetEntries (S, L, I, 3) &&
1244 L[0]->OPC == OP65_LDA && L[0]->AM == AM65_ZP &&
1245 L[1]->OPC == OP65_LDX && L[1]->AM == AM65_ZP &&
1246 !CS_RangeHasLabel (S, I+1, 2) &&
1247 (Len = strlen (L[0]->Arg)) > 0 &&
1248 strncmp (L[0]->Arg, L[1]->Arg, Len) == 0 &&
1249 strcmp (L[1]->Arg + Len, "+1") == 0) {
1251 unsigned PushAX = CE_IsCallTo (L[2], "pushax");
1253 /* Check for the remainder of the sequence */
1254 if (CS_GetEntries (S, L+3, I+3, 1 + PushAX) &&
1255 !CS_RangeHasLabel (S, I+3, 1 + PushAX) &&
1256 L[2+PushAX]->OPC == OP65_LDY &&
1257 CE_IsCallTo (L[3+PushAX], "ldaxidx")) {
1262 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[3]->LI);
1263 CS_InsertEntry (S, X, I+PushAX+4);
1266 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, L[3]->LI);
1267 CS_InsertEntry (S, X, I+PushAX+5);
1270 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, L[3]->LI);
1271 CS_InsertEntry (S, X, I+PushAX+6);
1274 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[3]->LI);
1275 CS_InsertEntry (S, X, I+PushAX+7);
1277 /* Remove the old code */
1278 CS_DelEntry (S, I+PushAX+3);
1280 CS_DelEntries (S, I, 2);
1283 /* Remember, we had changes */
1294 /* Return the number of changes made */
1300 unsigned OptPtrLoad16 (CodeSeg* S)
1301 /* Search for the sequence
1306 ** and replace it by:
1314 ** This step must be executed *after* OptPtrLoad1!
1317 unsigned Changes = 0;
1319 /* Walk over the entries */
1321 while (I < CS_GetEntryCount (S)) {
1325 /* Get next entry */
1326 L[0] = CS_GetEntry (S, I);
1328 /* Check for the sequence */
1329 if (L[0]->OPC == OP65_LDY &&
1330 CS_GetEntries (S, L+1, I+1, 1) &&
1331 CE_IsCallTo (L[1], "ldauidx") &&
1332 !CE_HasLabel (L[1])) {
1337 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[1]->LI);
1338 CS_InsertEntry (S, X, I+2);
1341 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[1]->LI);
1342 CS_InsertEntry (S, X, I+3);
1345 X = NewCodeEntry (L[0]->OPC, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
1346 CS_InsertEntry (S, X, I+4);
1349 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[1]->LI);
1350 CS_InsertEntry (S, X, I+5);
1353 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[1]->LI);
1354 CS_InsertEntry (S, X, I+6);
1356 /* Delete the old code */
1357 CS_DelEntries (S, I, 2);
1359 /* Remember, we had changes */
1369 /* Return the number of changes made */
1375 unsigned OptPtrLoad17 (CodeSeg* S)
1376 /* Search for the sequence
1381 ** and replace it by:
1391 ** This step must be executed *after* OptPtrLoad9! While code size increases
1392 ** by more than 200%, inlining will greatly improve visibility for the
1393 ** optimizer, so often part of the code gets improved later. So we will mark
1394 ** the step with less than 200% so it gets executed when -Oi is in effect.
1397 unsigned Changes = 0;
1399 /* Walk over the entries */
1401 while (I < CS_GetEntryCount (S)) {
1405 /* Get next entry */
1406 L[0] = CS_GetEntry (S, I);
1408 /* Check for the sequence */
1409 if (L[0]->OPC == OP65_LDY &&
1410 CS_GetEntries (S, L+1, I+1, 1) &&
1411 CE_IsCallTo (L[1], "ldaxidx") &&
1412 !CE_HasLabel (L[1])) {
1416 /* Store the high byte */
1417 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
1418 CS_InsertEntry (S, X, I+2);
1420 /* Store the low byte */
1421 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
1422 CS_InsertEntry (S, X, I+3);
1425 X = NewCodeEntry (L[0]->OPC, L[0]->AM, L[0]->Arg, 0, L[0]->LI);
1426 CS_InsertEntry (S, X, I+4);
1429 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[1]->LI);
1430 CS_InsertEntry (S, X, I+5);
1433 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, L[1]->LI);
1434 CS_InsertEntry (S, X, I+6);
1437 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, L[1]->LI);
1438 CS_InsertEntry (S, X, I+7);
1441 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[1]->LI);
1442 CS_InsertEntry (S, X, I+8);
1444 /* Delete original sequence */
1445 CS_DelEntries (S, I, 2);
1447 /* Remember, we had changes */
1457 /* Return the number of changes made */