- *
- * and replace it by:
- *
- * sta ptr1
- * stx ptr1+1
- * lda xxx
- * ldy yyy
- * sta (ptr1),y
- */
-{
- unsigned Changes = 0;
-
- /* Walk over the entries */
- unsigned I = 0;
- while (I < CS_GetEntryCount (S)) {
-
- CodeEntry* L[4];
-
- /* Get next entry */
- L[0] = CS_GetEntry (S, I);
-
- /* Check for the sequence */
- if (CE_IsCallTo (L[0], "pushax") &&
- CS_GetEntries (S, L+1, I+1, 3) &&
- L[1]->OPC == OP65_LDA &&
- !CE_HasLabel (L[1]) &&
- L[2]->OPC == OP65_LDY &&
- !CE_HasLabel (L[2]) &&
- CE_IsCallTo (L[3], "staspidx") &&
- !CE_HasLabel (L[3])) {
-
- CodeEntry* X;
-
- /* Create and insert the stores */
- X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
- CS_InsertEntry (S, X, I+1);
-
- X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI);
- CS_InsertEntry (S, X, I+2);
-
- /* Delete the call to pushax */
- CS_DelEntry (S, I);
-
- /* Insert the store through ptr1 */
- X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
- CS_InsertEntry (S, X, I+4);
-
- /* Delete the call to staspidx */
- CS_DelEntry (S, I+5);
-
- /* Remember, we had changes */
- ++Changes;
-
- }
-
- /* Next entry */
- ++I;
-
- }
-
- /* Return the number of changes made */
- return Changes;
-}
-
-
-
-/*****************************************************************************/
-/* Optimize loads through pointers */
-/*****************************************************************************/
-
-
-
-static unsigned OptPtrLoad1 (CodeSeg* S)
-/* Search for the sequence:
- *
- * clc
- * adc xxx
- * tay
- * txa
- * adc yyy
- * tax
- * tya
- * ldy
- * jsr ldauidx
- *
- * and replace it by:
- *
- * clc
- * adc xxx
- * sta ptr1
- * txa
- * adc yyy
- * sta ptr1+1
- * ldy
- * ldx #$00
- * lda (ptr1),y
- */
-{
- unsigned Changes = 0;
-
- /* Walk over the entries */
- unsigned I = 0;
- while (I < CS_GetEntryCount (S)) {
-
- CodeEntry* L[9];
-
- /* Get next entry */
- L[0] = CS_GetEntry (S, I);
-
- /* Check for the sequence */
- if (L[0]->OPC == OP65_CLC &&
- CS_GetEntries (S, L+1, I+1, 8) &&
- L[1]->OPC == OP65_ADC &&
- !CE_HasLabel (L[1]) &&
- L[2]->OPC == OP65_TAY &&
- !CE_HasLabel (L[2]) &&
- L[3]->OPC == OP65_TXA &&
- !CE_HasLabel (L[3]) &&
- L[4]->OPC == OP65_ADC &&
- !CE_HasLabel (L[4]) &&
- L[5]->OPC == OP65_TAX &&
- !CE_HasLabel (L[5]) &&
- L[6]->OPC == OP65_TYA &&
- !CE_HasLabel (L[6]) &&
- L[7]->OPC == OP65_LDY &&
- !CE_HasLabel (L[7]) &&
- CE_IsCallTo (L[8], "ldauidx") &&
- !CE_HasLabel (L[8])) {
-
- CodeEntry* X;
- CodeEntry* P;
-
- /* Store the low byte and remove the TAY instead */
- X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[1]->LI);
- CS_InsertEntry (S, X, I+2);
- CS_DelEntry (S, I+3);
-
- /* Store the high byte */
- X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[4]->LI);
- CS_InsertEntry (S, X, I+5);
-
- /* If the instruction before the adc is a ldx, replace the
- * txa by and lda with the same location of the ldx.
- */
- if ((P = CS_GetPrevEntry (S, I)) != 0 &&
- P->OPC == OP65_LDX &&
- !CE_HasLabel (P)) {
-
- X = NewCodeEntry (OP65_LDA, P->AM, P->Arg, 0, P->LI);
- CS_InsertEntry (S, X, I+4);
- CS_DelEntry (S, I+3);
- }
-
- /* Delete more transfer insns */
- CS_DelEntry (S, I+7);
- CS_DelEntry (S, I+6);
-
- /* Delete the call to ldauidx */
- CS_DelEntry (S, I+7);
-
- /* Load high and low byte */
- X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[7]->LI);
- CS_InsertEntry (S, X, I+7);
- X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[7]->LI);
- CS_InsertEntry (S, X, I+8);
-
- /* Remember, we had changes */
- ++Changes;
-
- }
-
- /* Next entry */
- ++I;
-
- }
-
- /* Return the number of changes made */
- return Changes;
-}
-
-
-
-static unsigned OptPtrLoad2 (CodeSeg* S)
-/* Search for the sequence:
- *
- * adc xxx
- * pha
- * txa
- * iny
- * adc yyy
- * tax
- * pla
- * ldy
- * jsr ldauidx
- *
- * and replace it by:
- *
- * adc xxx
- * sta ptr1
- * txa
- * iny
- * adc yyy
- * sta ptr1+1
- * ldy
- * ldx #$00
- * lda (ptr1),y