- * subop
- * ldy yyy
- * sta (ptr1),y
- */
-{
- unsigned Changes = 0;
-
- /* Walk over the entries */
- unsigned I = 0;
- while (I < CS_GetEntryCount (S)) {
-
- unsigned K;
- CodeEntry* L[10];
-
- /* 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_LDY &&
- CE_KnownImm (L[1]) &&
- !CE_HasLabel (L[1]) &&
- CE_IsCallTo (L[2], "ldauidx") &&
- !CE_HasLabel (L[2]) &&
- (K = OptPtrStore1Sub (S, I+3, L+3)) > 0 &&
- CS_GetEntries (S, L+3+K, I+3+K, 2) &&
- L[3+K]->OPC == OP65_LDY &&
- CE_KnownImm (L[3+K]) &&
- !CE_HasLabel (L[3+K]) &&
- CE_IsCallTo (L[4+K], "staspidx") &&
- !CE_HasLabel (L[4+K])) {
-
- 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);
-
- /* Insert the load from ptr1 */
- X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
- CS_InsertEntry (S, X, I+5);
- X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[2]->LI);
- CS_InsertEntry (S, X, I+6);
-
- /* Insert the store through ptr1 */
- X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, "ptr1", 0, L[3]->LI);
- CS_InsertEntry (S, X, I+8+K);
-
- /* Delete the old code */
- CS_DelEntry (S, I+9+K); /* jsr spaspidx */
- CS_DelEntry (S, I+4); /* jsr ldauidx */
- CS_DelEntry (S, I); /* jsr pushax */
-
- /* Remember, we had changes */
- ++Changes;
-
- }
-
- /* Next entry */
- ++I;
-
- }
-
- /* Return the number of changes made */
- return Changes;
-}
-
-
-
-static unsigned OptPtrStore2 (CodeSeg* S)
-/* Search for the sequence:
- *
- * lda #<(label+0)
- * ldx #>(label+0)
- * clc
- * adc xxx
- * bcc L
- * inx
- * L: jsr pushax
- * ldx #$00
- * lda yyy
- * ldy #$00
- * jsr staspidx
- *
- * and replace it by:
- *
- * ldy xxx
- * ldx #$00
- * lda yyy
- * sta label,y
- */
-{
- unsigned Changes = 0;
-
- /* Walk over the entries */
- unsigned I = 0;
- while (I < CS_GetEntryCount (S)) {
-
- CodeEntry* L[11];
- unsigned Len;
-
- /* Get next entry */
- L[0] = CS_GetEntry (S, I);
-
- /* Check for the sequence */
- if (L[0]->OPC == OP65_LDA &&
- L[0]->AM == AM65_IMM &&
- CS_GetEntries (S, L+1, I+1, 10) &&
- L[1]->OPC == OP65_LDX &&
- L[1]->AM == AM65_IMM &&
- L[2]->OPC == OP65_CLC &&
- L[3]->OPC == OP65_ADC &&
- (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
- (L[4]->OPC == OP65_BCC || L[4]->OPC == OP65_JCC) &&
- L[4]->JumpTo != 0 &&
- L[4]->JumpTo->Owner == L[6] &&
- L[5]->OPC == OP65_INX &&
- CE_IsCallTo (L[6], "pushax") &&
- L[7]->OPC == OP65_LDX &&
- L[8]->OPC == OP65_LDA &&
- L[9]->OPC == OP65_LDY &&
- CE_KnownImm (L[9]) &&
- L[9]->Num == 0 &&
- CE_IsCallTo (L[10], "staspidx") &&
- !CS_RangeHasLabel (S, I+1, 5) &&
- !CS_RangeHasLabel (S, I+7, 4) &&
- /* Check the label last because this is quite costly */
- (Len = strlen (L[0]->Arg)) > 3 &&
- L[0]->Arg[0] == '<' &&
- L[0]->Arg[1] == '(' &&
- strlen (L[1]->Arg) == Len &&
- L[1]->Arg[0] == '>' &&
- memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
-
- CodeEntry* X;
- char* Label;
-
- /* We will create all the new stuff behind the current one so
- * we keep the line references.
- */
- X = NewCodeEntry (OP65_LDY, L[3]->AM, L[3]->Arg, 0, L[0]->LI);
- CS_InsertEntry (S, X, I+11);
-
- X = NewCodeEntry (OP65_LDX, L[7]->AM, L[7]->Arg, 0, L[7]->LI);
- CS_InsertEntry (S, X, I+12);
-
- X = NewCodeEntry (OP65_LDA, L[8]->AM, L[8]->Arg, 0, L[8]->LI);
- CS_InsertEntry (S, X, I+13);
-
- Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
- Label[Len-3] = '\0';
- X = NewCodeEntry (OP65_STA, AM65_ABSY, Label, 0, L[10]->LI);
- CS_InsertEntry (S, X, I+14);
- xfree (Label);
-
- /* Remove the old code */
- CS_DelEntries (S, I, 11);
-
- /* 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 &&
- L[2]->OPC == OP65_TAY &&
- L[3]->OPC == OP65_TXA &&
- L[4]->OPC == OP65_ADC &&
- L[5]->OPC == OP65_TAX &&
- L[6]->OPC == OP65_TYA &&
- L[7]->OPC == OP65_LDY &&
- CE_IsCallTo (L[8], "ldauidx") &&
- !CS_RangeHasLabel (S, I+1, 8)) {
-
- CodeEntry* X;
- CodeEntry* P;
-
- /* Track the insertion point */
- unsigned IP = I+2;
-
- /* sta ptr1 */
- X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[2]->LI);
- CS_InsertEntry (S, X, IP++);
-
- /* If the instruction before the clc is a ldx, replace the
- * txa by an lda with the same location of the ldx. Otherwise
- * transfer the value in X to A.
- */
- 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);
- } else {
- X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, L[3]->LI);
- }
- CS_InsertEntry (S, X, IP++);
-
- /* adc yyy */
- X = NewCodeEntry (OP65_ADC, L[4]->AM, L[4]->Arg, 0, L[4]->LI);
- CS_InsertEntry (S, X, IP++);
-
- /* sta ptr1+1 */
- X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[5]->LI);
- CS_InsertEntry (S, X, IP++);
-
- /* ldy ... */
- X = NewCodeEntry (OP65_LDY, L[7]->AM, L[7]->Arg, 0, L[7]->LI);
- CS_InsertEntry (S, X, IP++);
-
- /* ldx #$00 */
- X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[8]->LI);
- CS_InsertEntry (S, X, IP++);
-
- /* lda (ptr1),y */
- X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[8]->LI);
- CS_InsertEntry (S, X, IP++);
-
- /* Remove the old instructions */
- CS_DelEntries (S, IP, 7);
-
- /* 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
- */
-{
- 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_ADC &&
- CS_GetEntries (S, L+1, I+1, 8) &&
- L[1]->OPC == OP65_PHA &&
- L[2]->OPC == OP65_TXA &&
- L[3]->OPC == OP65_INY &&
- L[4]->OPC == OP65_ADC &&
- L[5]->OPC == OP65_TAX &&
- L[6]->OPC == OP65_PLA &&
- L[7]->OPC == OP65_LDY &&
- CE_IsCallTo (L[8], "ldauidx") &&
- !CS_RangeHasLabel (S, I+1, 8)) {
-
- CodeEntry* X;
-
- /* Store the low byte and remove the PHA instead */
- X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI);
- CS_InsertEntry (S, X, I+1);
-
- /* Store the high byte */
- X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[4]->LI);
- CS_InsertEntry (S, X, I+6);
-
- /* Load high and low byte */
- X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[6]->LI);
- CS_InsertEntry (S, X, I+10);
- X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[6]->LI);
- CS_InsertEntry (S, X, I+11);
-
- /* Delete the old code */
- CS_DelEntry (S, I+12); /* jsr ldauidx */
- CS_DelEntry (S, I+8); /* pla */
- CS_DelEntry (S, I+7); /* tax */
- CS_DelEntry (S, I+2); /* pha */
-
- /* Remember, we had changes */
- ++Changes;
-
- }
-
- /* Next entry */
- ++I;
-
- }
-
- /* Return the number of changes made */
- return Changes;
-}
-
-
-
-static unsigned OptPtrLoad3 (CodeSeg* S)
-/* Search for the sequence:
- *
- * lda #<(label+0)
- * ldx #>(label+0)
- * clc
- * adc xxx
- * bcc L
- * inx
- * L: ldy #$00
- * jsr ldauidx
- *
- * and replace it by:
- *
- * ldy xxx
- * ldx #$00
- * lda label,y
- */
-{
- unsigned Changes = 0;
-
- /* Walk over the entries */
- unsigned I = 0;
- while (I < CS_GetEntryCount (S)) {
-
- CodeEntry* L[8];
- unsigned Len;
-
- /* Get next entry */
- L[0] = CS_GetEntry (S, I);
-
- /* Check for the sequence */
- if (L[0]->OPC == OP65_LDA &&
- L[0]->AM == AM65_IMM &&
- CS_GetEntries (S, L+1, I+1, 7) &&
- L[1]->OPC == OP65_LDX &&
- L[1]->AM == AM65_IMM &&
- L[2]->OPC == OP65_CLC &&
- L[3]->OPC == OP65_ADC &&
- (L[3]->AM == AM65_ABS || L[3]->AM == AM65_ZP) &&
- (L[4]->OPC == OP65_BCC || L[4]->OPC == OP65_JCC) &&
- L[4]->JumpTo != 0 &&
- L[4]->JumpTo->Owner == L[6] &&
- L[5]->OPC == OP65_INX &&
- L[6]->OPC == OP65_LDY &&
- CE_KnownImm (L[6]) &&
- L[6]->Num == 0 &&
- CE_IsCallTo (L[7], "ldauidx") &&
- !CS_RangeHasLabel (S, I+1, 5) &&
- !CE_HasLabel (L[7]) &&
- /* Check the label last because this is quite costly */
- (Len = strlen (L[0]->Arg)) > 3 &&
- L[0]->Arg[0] == '<' &&
- L[0]->Arg[1] == '(' &&
- strlen (L[1]->Arg) == Len &&
- L[1]->Arg[0] == '>' &&
- memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
-
- CodeEntry* X;
- char* Label;
-
- /* We will create all the new stuff behind the current one so
- * we keep the line references.
- */
- X = NewCodeEntry (OP65_LDY, L[3]->AM, L[3]->Arg, 0, L[0]->LI);
- CS_InsertEntry (S, X, I+8);
-
- X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
- CS_InsertEntry (S, X, I+9);
-
- Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
- Label[Len-3] = '\0';
- X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
- CS_InsertEntry (S, X, I+10);
- xfree (Label);
-
- /* Remove the old code */
- CS_DelEntries (S, I, 8);
-
- /* Remember, we had changes */
- ++Changes;
-
- }
-
- /* Next entry */
- ++I;
-
- }
-
- /* Return the number of changes made */
- return Changes;
-}
-
-
-
-static unsigned OptPtrLoad4 (CodeSeg* S)
-/* Search for the sequence:
- *
- * lda #<(label+0)
- * ldx #>(label+0)
- * ldy #$xx
- * clc
- * adc (sp),y
- * bcc L
- * inx
- * L: ldy #$00
- * jsr ldauidx
- *
- * and replace it by:
- *
- * ldy #$xx
- * lda (sp),y
- * tay
- * ldx #$00
- * lda label,y
- */
-{
- unsigned Changes = 0;
-
- /* Walk over the entries */
- unsigned I = 0;
- while (I < CS_GetEntryCount (S)) {
-
- CodeEntry* L[9];
- unsigned Len;
-
- /* Get next entry */
- L[0] = CS_GetEntry (S, I);
-
- /* Check for the sequence */
- if (L[0]->OPC == OP65_LDA &&
- L[0]->AM == AM65_IMM &&
- CS_GetEntries (S, L+1, I+1, 8) &&
- L[1]->OPC == OP65_LDX &&
- L[1]->AM == AM65_IMM &&
- !CE_HasLabel (L[1]) &&
- L[2]->OPC == OP65_LDY &&
- CE_KnownImm (L[2]) &&
- !CE_HasLabel (L[2]) &&
- L[3]->OPC == OP65_CLC &&
- !CE_HasLabel (L[3]) &&
- L[4]->OPC == OP65_ADC &&
- L[4]->AM == AM65_ZP_INDY &&
- !CE_HasLabel (L[4]) &&
- (L[5]->OPC == OP65_BCC || L[5]->OPC == OP65_JCC) &&
- L[5]->JumpTo != 0 &&
- L[5]->JumpTo->Owner == L[7] &&
- !CE_HasLabel (L[5]) &&
- L[6]->OPC == OP65_INX &&
- !CE_HasLabel (L[6]) &&
- L[7]->OPC == OP65_LDY &&
- CE_KnownImm (L[7]) &&
- L[7]->Num == 0 &&
- CE_IsCallTo (L[8], "ldauidx") &&
- !CE_HasLabel (L[8]) &&
- /* Check the label last because this is quite costly */
- (Len = strlen (L[0]->Arg)) > 3 &&
- L[0]->Arg[0] == '<' &&
- L[0]->Arg[1] == '(' &&
- strlen (L[1]->Arg) == Len &&
- L[1]->Arg[0] == '>' &&
- memcmp (L[0]->Arg+1, L[1]->Arg+1, Len-1) == 0) {
-
- CodeEntry* X;
- char* Label;
-
- /* Add the lda */
- X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[4]->Arg, 0, L[0]->LI);
- CS_InsertEntry (S, X, I+3);
-
- /* Add the tay */
- X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, L[0]->LI);
- CS_InsertEntry (S, X, I+4);
-
- /* Add the ldx */
- X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[0]->LI);
- CS_InsertEntry (S, X, I+5);
-
- /* Add the lda */
- Label = memcpy (xmalloc (Len-2), L[0]->Arg+2, Len-3);
- Label[Len-3] = '\0';
- X = NewCodeEntry (OP65_LDA, AM65_ABSY, Label, 0, L[0]->LI);
- CS_InsertEntry (S, X, I+6);
- xfree (Label);
-
- /* Remove the old code */
- CS_DelEntries (S, I, 2);
- CS_DelEntries (S, I+5, 6);
-
- /* Remember, we had changes */
- ++Changes;
-
- }
-
- /* Next entry */
- ++I;
-
- }
-
- /* Return the number of changes made */
- return Changes;
-}
-
-
-
-static unsigned OptPtrLoad5 (CodeSeg* S)
-/* Search for the sequence: