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 (L[0]->OPC == OP65_CLC &&
495 CS_GetEntries (S, L+1, I+1, 5) &&
496 L[1]->OPC == OP65_ADC &&
497 (L[1]->AM == AM65_ABS || L[1]->AM == AM65_ZP || L[1]->AM == AM65_IMM) &&
498 (L[2]->OPC == OP65_BCC || L[2]->OPC == OP65_JCC) &&
500 L[2]->JumpTo->Owner == L[4] &&
501 L[3]->OPC == OP65_INX &&
502 L[4]->OPC == OP65_LDY &&
503 CE_IsKnownImm (L[4], 0) &&
504 CE_IsCallTo (L[5], "ldauidx") &&
505 !CS_RangeHasLabel (S, I+1, 3) &&
506 !CE_HasLabel (L[5])) {
510 /* We will create all the new stuff behind the current one so
511 * we keep the line references.
513 X = NewCodeEntry (OP65_LDY, L[1]->AM, L[1]->Arg, 0, L[0]->LI);
514 CS_InsertEntry (S, X, I+6);
517 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
518 CS_InsertEntry (S, X, I+7);
521 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
522 CS_InsertEntry (S, X, I+8);
525 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
526 CS_InsertEntry (S, X, I+9);
528 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[0]->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:
576 * L1: tay <- only if flags are used
578 * This function must execute before OptPtrLoad7!
582 unsigned Changes = 0;
584 /* Walk over the entries */
586 while (I < CS_GetEntryCount (S)) {
592 L[0] = CS_GetEntry (S, I);
594 /* Check for the sequence */
595 if (L[0]->OPC == OP65_LDA &&
596 L[0]->AM == AM65_ZP &&
597 strncmp (L[0]->Arg, "regbank+", 8) == 0 &&
598 (Len = strlen (L[0]->Arg)) > 0 &&
599 CS_GetEntries (S, L+1, I+1, 14) &&
600 !CS_RangeHasLabel (S, I+1, 7) &&
601 !CS_RangeHasLabel (S, I+9, 5) &&
602 L[1]->OPC == OP65_LDX &&
603 L[1]->AM == AM65_ZP &&
604 strncmp (L[1]->Arg, L[0]->Arg, Len) == 0 &&
605 strcmp (L[1]->Arg+Len, "+1") == 0 &&
606 L[2]->OPC == OP65_STA &&
607 L[2]->AM == AM65_ZP &&
608 strcmp (L[2]->Arg, "regsave") == 0 &&
609 L[3]->OPC == OP65_STX &&
610 L[3]->AM == AM65_ZP &&
611 strcmp (L[3]->Arg, "regsave+1") == 0 &&
612 L[4]->OPC == OP65_CLC &&
613 L[5]->OPC == OP65_ADC &&
614 CE_IsKnownImm (L[5], 1) &&
615 L[6]->OPC == OP65_BCC &&
617 L[6]->JumpTo->Owner == L[8] &&
618 L[7]->OPC == OP65_INX &&
619 L[8]->OPC == OP65_STA &&
620 L[8]->AM == AM65_ZP &&
621 strcmp (L[8]->Arg, L[0]->Arg) == 0 &&
622 L[9]->OPC == OP65_STX &&
623 L[9]->AM == AM65_ZP &&
624 strcmp (L[9]->Arg, L[1]->Arg) == 0 &&
625 L[10]->OPC == OP65_LDA &&
626 L[10]->AM == AM65_ZP &&
627 strcmp (L[10]->Arg, "regsave") == 0 &&
628 L[11]->OPC == OP65_LDX &&
629 L[11]->AM == AM65_ZP &&
630 strcmp (L[11]->Arg, "regsave+1") == 0 &&
631 L[12]->OPC == OP65_LDY &&
632 CE_IsConstImm (L[12]) &&
633 CE_IsCallTo (L[13], "ldauidx")) {
638 /* Check if the instruction following the sequence uses the flags
639 * set by the load. If so, insert a test of the value in the
642 if (CE_UseLoadFlags (L[14])) {
643 X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, L[13]->LI);
644 CS_InsertEntry (S, X, I+14);
647 /* Attach a label to L[14]. This may be either the just inserted
648 * instruction, or the one following the sequence.
650 Label = CS_GenLabel (S, L[14]);
653 X = NewCodeEntry (OP65_LDY, AM65_IMM, L[12]->Arg, 0, L[12]->LI);
654 CS_InsertEntry (S, X, I+14);
657 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[13]->LI);
658 CS_InsertEntry (S, X, I+15);
660 /* lda (regbank+n),y */
661 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[13]->LI);
662 CS_InsertEntry (S, X, I+16);
665 X = NewCodeEntry (OP65_INC, AM65_ZP, L[0]->Arg, 0, L[5]->LI);
666 CS_InsertEntry (S, X, I+17);
669 X = NewCodeEntry (OP65_BNE, AM65_BRA, Label->Name, Label, L[6]->LI);
670 CS_InsertEntry (S, X, I+18);
672 /* inc regbank+n+1 */
673 X = NewCodeEntry (OP65_INC, AM65_ZP, L[1]->Arg, 0, L[7]->LI);
674 CS_InsertEntry (S, X, I+19);
676 /* Delete the old code */
677 CS_DelEntries (S, I, 14);
679 /* Remember, we had changes */
689 /* Return the number of changes made */
695 unsigned OptPtrLoad7 (CodeSeg* S)
696 /* Search for the sequence:
710 unsigned Changes = 0;
712 /* Walk over the entries */
714 while (I < CS_GetEntryCount (S)) {
720 L[0] = CS_GetEntry (S, I);
722 /* Check for the sequence */
723 if (L[0]->OPC == OP65_LDA && L[0]->AM == AM65_ZP &&
724 CS_GetEntries (S, L+1, I+1, 3) &&
725 !CS_RangeHasLabel (S, I+1, 3) &&
726 L[1]->OPC == OP65_LDX && L[1]->AM == AM65_ZP &&
727 (Len = strlen (L[0]->Arg)) > 0 &&
728 strncmp (L[0]->Arg, L[1]->Arg, Len) == 0 &&
729 strcmp (L[1]->Arg + Len, "+1") == 0 &&
730 L[2]->OPC == OP65_LDY &&
731 CE_IsCallTo (L[3], "ldauidx")) {
736 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
737 CS_InsertEntry (S, X, I+3);
740 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[3]->LI);
741 CS_InsertEntry (S, X, I+4);
743 /* Remove the old code */
744 CS_DelEntry (S, I+5);
745 CS_DelEntries (S, I, 2);
747 /* Remember, we had changes */
757 /* Return the number of changes made */
763 unsigned OptPtrLoad8 (CodeSeg* S)
764 /* Search for the sequence:
768 * (anything that doesn't change a/x)
776 * (anything that doesn't change a/x)
781 * Must execute before OptPtrLoad10!
784 unsigned Changes = 0;
787 /* Generate register info */
790 /* Walk over the entries */
792 while (I < CS_GetEntryCount (S)) {
798 L[0] = CS_GetEntry (S, I);
800 /* Check for the sequence */
801 if (L[0]->OPC == OP65_LDA && L[0]->AM == AM65_ZP &&
802 CS_GetEntries (S, L+1, I+1, 4) &&
803 !CS_RangeHasLabel (S, I+1, 4) &&
804 L[1]->OPC == OP65_LDX && L[1]->AM == AM65_ZP &&
805 (Len = strlen (L[0]->Arg)) > 0 &&
806 strncmp (L[0]->Arg, L[1]->Arg, Len) == 0 &&
807 strcmp (L[1]->Arg + Len, "+1") == 0 &&
808 (L[2]->Chg & REG_AX) == 0 &&
809 L[3]->OPC == OP65_LDY &&
810 CE_IsCallTo (L[4], "ldauidx")) {
815 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
816 CS_InsertEntry (S, X, I+5);
819 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[3]->LI);
820 CS_InsertEntry (S, X, I+6);
822 /* Remove the old code */
823 CS_DelEntry (S, I+4);
825 /* Remember, we had changes */
835 /* Free the register info */
838 /* Return the number of changes made */
844 unsigned OptPtrLoad9 (CodeSeg* S)
845 /* Search for the sequence:
861 unsigned Changes = 0;
863 /* Walk over the entries */
865 while (I < CS_GetEntryCount (S)) {
871 L[0] = CS_GetEntry (S, I);
873 /* Check for the sequence */
874 if (L[0]->OPC == OP65_LDA && L[0]->AM == AM65_ZP &&
875 CS_GetEntries (S, L+1, I+1, 3) &&
876 !CS_RangeHasLabel (S, I+1, 3) &&
877 L[1]->OPC == OP65_LDX && L[1]->AM == AM65_ZP &&
878 (Len = strlen (L[0]->Arg)) > 0 &&
879 strncmp (L[0]->Arg, L[1]->Arg, Len) == 0 &&
880 strcmp (L[1]->Arg + Len, "+1") == 0 &&
881 L[2]->OPC == OP65_LDY &&
882 CE_IsCallTo (L[3], "ldaxidx")) {
887 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[3]->LI);
888 CS_InsertEntry (S, X, I+4);
891 X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, L[3]->LI);
892 CS_InsertEntry (S, X, I+5);
895 X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, L[3]->LI);
896 CS_InsertEntry (S, X, I+6);
899 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[3]->LI);
900 CS_InsertEntry (S, X, I+7);
902 /* Remove the old code */
903 CS_DelEntry (S, I+3);
904 CS_DelEntries (S, I, 2);
906 /* Remember, we had changes */
916 /* Return the number of changes made */
922 unsigned OptPtrLoad10 (CodeSeg* S)
923 /* Search for the sequence
936 * This step must be executed *after* OptPtrLoad1!
939 unsigned Changes = 0;
941 /* Walk over the entries */
943 while (I < CS_GetEntryCount (S)) {
948 L[0] = CS_GetEntry (S, I);
950 /* Check for the sequence */
951 if (L[0]->OPC == OP65_LDY &&
952 CS_GetEntries (S, L+1, I+1, 1) &&
953 CE_IsCallTo (L[1], "ldauidx") &&
954 !CE_HasLabel (L[1])) {
958 /* Store the high byte */
959 X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
960 CS_InsertEntry (S, X, I);
962 /* Store the low byte */
963 X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
964 CS_InsertEntry (S, X, I+1);
966 /* Delete the call to ldauidx */
967 CS_DelEntry (S, I+3);
969 /* Load the high and low byte */
970 X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
971 CS_InsertEntry (S, X, I+3);
972 X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[0]->LI);
973 CS_InsertEntry (S, X, I+4);
975 /* Remember, we had changes */
985 /* Return the number of changes made */