- P[1]->Arg[Len+1] == '1' &&
- P[1]->Arg[Len+2] == '\0') {
-
- /* Ok, found. Use the name of the register bank */
- RegBank = ZPLoc = P[0]->Arg;
- }
- }
- }
-
- /* Insert the load via the zp pointer */
- X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[3]->LI);
- CS_InsertEntry (S, X, I+3);
- X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, ZPLoc, 0, L[2]->LI);
- CS_InsertEntry (S, X, I+4);
-
- /* Insert the store through the zp pointer */
- X = NewCodeEntry (OP65_STA, AM65_ZP_INDY, ZPLoc, 0, L[3]->LI);
- CS_InsertEntry (S, X, I+6+K);
-
- /* Delete the old code */
- CS_DelEntry (S, I+7+K); /* jsr spaspidx */
- CS_DelEntry (S, I+2); /* jsr ldauidx */
-
- /* Create and insert the stores into the zp pointer if needed */
- if (RegBank == 0) {
- 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 more old code. Do it here to keep a label attached to
- * entry I in place.
- */
- 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:
- *
- * lda regbank+n
- * ldx regbank+n+1
- * sta regsave
- * stx regsave+1
- * clc
- * adc #$01
- * bcc L0005
- * inx
- * L: sta regbank+n
- * stx regbank+n+1
- * lda regsave
- * ldx regsave+1
- * ldy #$00
- * jsr ldauidx
- *
- * and replace it by:
- *
- * ldy #$00
- * ldx #$00
- * lda (regbank+n),y
- * inc regbank+n
- * bne L1
- * inc regbank+n+1
- * L1: tay <- only if flags are used
- *
- * This function must execute before OptPtrLoad5!
- *
- */
-{
- unsigned Changes = 0;
-
- /* Walk over the entries */
- unsigned I = 0;
- while (I < CS_GetEntryCount (S)) {
-
- CodeEntry* L[15];
- 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_ZP &&
- strncmp (L[0]->Arg, "regbank+", 8) == 0 &&
- (Len = strlen (L[0]->Arg)) > 0 &&
- CS_GetEntries (S, L+1, I+1, 14) &&
- !CS_RangeHasLabel (S, I+1, 7) &&
- !CS_RangeHasLabel (S, I+9, 5) &&
- L[1]->OPC == OP65_LDX &&
- L[1]->AM == AM65_ZP &&
- strncmp (L[1]->Arg, L[0]->Arg, Len) == 0 &&
- strcmp (L[1]->Arg+Len, "+1") == 0 &&
- L[2]->OPC == OP65_STA &&
- L[2]->AM == AM65_ZP &&
- strcmp (L[2]->Arg, "regsave") == 0 &&
- L[3]->OPC == OP65_STX &&
- L[3]->AM == AM65_ZP &&
- strcmp (L[3]->Arg, "regsave+1") == 0 &&
- L[4]->OPC == OP65_CLC &&
- L[5]->OPC == OP65_ADC &&
- CE_KnownImm (L[5]) &&
- L[5]->Num == 1 &&
- L[6]->OPC == OP65_BCC &&
- L[6]->JumpTo != 0 &&
- L[6]->JumpTo->Owner == L[8] &&
- L[7]->OPC == OP65_INX &&
- L[8]->OPC == OP65_STA &&
- L[8]->AM == AM65_ZP &&
- strcmp (L[8]->Arg, L[0]->Arg) == 0 &&
- L[9]->OPC == OP65_STX &&
- L[9]->AM == AM65_ZP &&
- strcmp (L[9]->Arg, L[1]->Arg) == 0 &&
- L[10]->OPC == OP65_LDA &&
- L[10]->AM == AM65_ZP &&
- strcmp (L[10]->Arg, "regsave") == 0 &&
- L[11]->OPC == OP65_LDX &&
- L[11]->AM == AM65_ZP &&
- strcmp (L[11]->Arg, "regsave+1") == 0 &&
- L[12]->OPC == OP65_LDY &&
- CE_KnownImm (L[12]) &&
- CE_IsCallTo (L[13], "ldauidx")) {
-
- CodeEntry* X;
- CodeLabel* Label;
-
- /* Check if the instruction following the sequence uses the flags
- * set by the load. If so, insert a test of the value in the
- * accumulator.
- */
- if (CE_UseLoadFlags (L[14])) {
- X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, L[13]->LI);
- CS_InsertEntry (S, X, I+14);
- }
-
- /* Attach a label to L[14]. This may be either the just inserted
- * instruction, or the one following the sequence.
- */
- Label = CS_GenLabel (S, L[14]);
-
- /* ldy #$xx */
- X = NewCodeEntry (OP65_LDY, AM65_IMM, L[12]->Arg, 0, L[12]->LI);
- CS_InsertEntry (S, X, I+14);
-
- /* ldx #$xx */
- X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[13]->LI);
- CS_InsertEntry (S, X, I+15);
-
- /* lda (regbank+n),y */
- X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, L[0]->Arg, 0, L[13]->LI);
- CS_InsertEntry (S, X, I+16);
-
- /* inc regbank+n */
- X = NewCodeEntry (OP65_INC, AM65_ZP, L[0]->Arg, 0, L[5]->LI);
- CS_InsertEntry (S, X, I+17);
-
- /* bne ... */
- X = NewCodeEntry (OP65_BNE, AM65_BRA, Label->Name, Label, L[6]->LI);
- CS_InsertEntry (S, X, I+18);
-
- /* inc regbank+n+1 */
- X = NewCodeEntry (OP65_INC, AM65_ZP, L[1]->Arg, 0, L[7]->LI);
- CS_InsertEntry (S, X, I+19);
-
- /* Delete the old code */
- CS_DelEntries (S, I, 14);
-
- /* Remember, we had changes */
- ++Changes;
-
- }
-
- /* Next entry */
- ++I;
-
- }
-
- /* Return the number of changes made */
- return Changes;
-}
-
-
-
-static unsigned OptPtrLoad6 (CodeSeg* S)
-/* Search for the sequence:
- *
- * lda zp
- * ldx zp+1
- * ldy xx
- * jsr ldauidx
- *
- * and replace it by:
- *
- * ldy xx
- * ldx #$00
- * lda (zp),y
- */
-{
- unsigned Changes = 0;
-
- /* Walk over the entries */
- unsigned I = 0;
- while (I < CS_GetEntryCount (S)) {
-
- CodeEntry* L[4];
- 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_ZP &&
- CS_GetEntries (S, L+1, I+1, 3) &&
- !CS_RangeHasLabel (S, I+1, 3) &&
- L[1]->OPC == OP65_LDX && L[1]->AM == AM65_ZP &&
- (Len = strlen (L[0]->Arg)) > 0 &&
- strncmp (L[0]->Arg, L[1]->Arg, Len) == 0 &&
- strcmp (L[1]->Arg + Len, "+1") == 0 &&
- L[2]->OPC == OP65_LDY &&
- CE_IsCallTo (L[3], "ldauidx")) {