From cbf4884f1361ffadd348558a3460ae026a702a4e Mon Sep 17 00:00:00 2001 From: uz Date: Sun, 16 Aug 2009 20:56:33 +0000 Subject: [PATCH] Some more pattern replacements. git-svn-id: svn://svn.cc65.org/cc65/trunk@4029 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/cc65/codeopt.c | 35 ++-- src/cc65/coptptrload.c | 389 ++++++++++++++++++++++++++++++++++++++++- src/cc65/coptptrload.h | 85 ++++++++- 3 files changed, 482 insertions(+), 27 deletions(-) diff --git a/src/cc65/codeopt.c b/src/cc65/codeopt.c index f8425f852..d94aa8be7 100644 --- a/src/cc65/codeopt.c +++ b/src/cc65/codeopt.c @@ -1033,13 +1033,16 @@ static OptFunc DOptPtrLoad1 = { OptPtrLoad1, "OptPtrLoad1", 100, 0, static OptFunc DOptPtrLoad2 = { OptPtrLoad2, "OptPtrLoad2", 100, 0, 0, 0, 0, 0 }; static OptFunc DOptPtrLoad3 = { OptPtrLoad3, "OptPtrLoad3", 100, 0, 0, 0, 0, 0 }; static OptFunc DOptPtrLoad4 = { OptPtrLoad4, "OptPtrLoad4", 100, 0, 0, 0, 0, 0 }; -static OptFunc DOptPtrLoad5 = { OptPtrLoad5, "OptPtrLoad5", 92, 0, 0, 0, 0, 0 }; -static OptFunc DOptPtrLoad6 = { OptPtrLoad6, "OptPtrLoad6", 50, 0, 0, 0, 0, 0 }; -static OptFunc DOptPtrLoad7 = { OptPtrLoad7, "OptPtrLoad7", 65, 0, 0, 0, 0, 0 }; -static OptFunc DOptPtrLoad8 = { OptPtrLoad8, "OptPtrLoad8", 108, 0, 0, 0, 0, 0 }; -static OptFunc DOptPtrLoad9 = { OptPtrLoad9, "OptPtrLoad9", 86, 0, 0, 0, 0, 0 }; -static OptFunc DOptPtrLoad10 = { OptPtrLoad10, "OptPtrLoad10", 100, 0, 0, 0, 0, 0 }; -static OptFunc DOptPtrLoad11 = { OptPtrLoad11, "OptPtrLoad11", 190, 0, 0, 0, 0, 0 }; +static OptFunc DOptPtrLoad5 = { OptPtrLoad5, "OptPtrLoad5", 50, 0, 0, 0, 0, 0 }; +static OptFunc DOptPtrLoad6 = { OptPtrLoad6, "OptPtrLoad6", 60, 0, 0, 0, 0, 0 }; +static OptFunc DOptPtrLoad7 = { OptPtrLoad7, "OptPtrLoad7", 140, 0, 0, 0, 0, 0 }; +static OptFunc DOptPtrLoad11 = { OptPtrLoad11, "OptPtrLoad11", 92, 0, 0, 0, 0, 0 }; +static OptFunc DOptPtrLoad12 = { OptPtrLoad12, "OptPtrLoad12", 50, 0, 0, 0, 0, 0 }; +static OptFunc DOptPtrLoad13 = { OptPtrLoad13, "OptPtrLoad13", 65, 0, 0, 0, 0, 0 }; +static OptFunc DOptPtrLoad14 = { OptPtrLoad14, "OptPtrLoad14", 108, 0, 0, 0, 0, 0 }; +static OptFunc DOptPtrLoad15 = { OptPtrLoad15, "OptPtrLoad15", 86, 0, 0, 0, 0, 0 }; +static OptFunc DOptPtrLoad16 = { OptPtrLoad16, "OptPtrLoad16", 100, 0, 0, 0, 0, 0 }; +static OptFunc DOptPtrLoad17 = { OptPtrLoad17, "OptPtrLoad17", 190, 0, 0, 0, 0, 0 }; static OptFunc DOptPtrStore1 = { OptPtrStore1, "OptPtrStore1", 100, 0, 0, 0, 0, 0 }; static OptFunc DOptPtrStore2 = { OptPtrStore2, "OptPtrStore2", 40, 0, 0, 0, 0, 0 }; static OptFunc DOptPush1 = { OptPush1, "OptPush1", 65, 0, 0, 0, 0, 0 }; @@ -1107,16 +1110,19 @@ static OptFunc* OptFuncs[] = { &DOptNegAX4, &DOptPrecalc, &DOptPtrLoad1, - &DOptPtrLoad10, &DOptPtrLoad11, + &DOptPtrLoad12, + &DOptPtrLoad13, + &DOptPtrLoad14, + &DOptPtrLoad15, + &DOptPtrLoad16, + &DOptPtrLoad17, &DOptPtrLoad2, &DOptPtrLoad3, &DOptPtrLoad4, &DOptPtrLoad5, &DOptPtrLoad6, &DOptPtrLoad7, - &DOptPtrLoad8, - &DOptPtrLoad9, &DOptPtrStore1, &DOptPtrStore2, &DOptPush1, @@ -1390,10 +1396,13 @@ static unsigned RunOptGroup1 (CodeSeg* S) Changes += RunOptFunc (S, &DOptPtrLoad5, 1); Changes += RunOptFunc (S, &DOptPtrLoad6, 1); Changes += RunOptFunc (S, &DOptPtrLoad7, 1); - Changes += RunOptFunc (S, &DOptPtrLoad8, 1); - Changes += RunOptFunc (S, &DOptPtrLoad9, 1); - Changes += RunOptFunc (S, &DOptPtrLoad10, 1); Changes += RunOptFunc (S, &DOptPtrLoad11, 1); + Changes += RunOptFunc (S, &DOptPtrLoad12, 1); + Changes += RunOptFunc (S, &DOptPtrLoad13, 1); + Changes += RunOptFunc (S, &DOptPtrLoad14, 1); + Changes += RunOptFunc (S, &DOptPtrLoad15, 1); + Changes += RunOptFunc (S, &DOptPtrLoad16, 1); + Changes += RunOptFunc (S, &DOptPtrLoad17, 1); Changes += RunOptFunc (S, &DOptNegAX1, 1); Changes += RunOptFunc (S, &DOptNegAX2, 1); Changes += RunOptFunc (S, &DOptNegAX3, 1); diff --git a/src/cc65/coptptrload.c b/src/cc65/coptptrload.c index 556e4e209..2bde19cea 100644 --- a/src/cc65/coptptrload.c +++ b/src/cc65/coptptrload.c @@ -461,6 +461,382 @@ unsigned OptPtrLoad4 (CodeSeg* S) unsigned OptPtrLoad5 (CodeSeg* S) +/* Search for the sequence: + * + * jsr pushax + * ldx #$00 + * lda yyy + * jsr tosaddax + * ldy #$00 + * jsr ldauidx + * + * and replace it by: + * + * sta ptr1 + * stx ptr1+1 + * ldy yyy + * ldx #$00 + * lda (ptr1),y + */ +{ + unsigned Changes = 0; + + /* Walk over the entries */ + unsigned I = 0; + while (I < CS_GetEntryCount (S)) { + + CodeEntry* L[6]; + + /* 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, 5) && + L[1]->OPC == OP65_LDX && + CE_IsKnownImm (L[1], 0) && + L[2]->OPC == OP65_LDA && + (L[2]->AM == AM65_ABS || + L[2]->AM == AM65_ZP || + L[2]->AM == AM65_IMM) && + CE_IsCallTo (L[3], "tosaddax") && + L[4]->OPC == OP65_LDY && + CE_IsKnownImm (L[4], 0) && + CE_IsCallTo (L[5], "ldauidx") && + !CS_RangeHasLabel (S, I+1, 5)) { + + CodeEntry* X; + + /* sta ptr1 */ + X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI); + CS_InsertEntry (S, X, I+6); + + /* stx ptr1+1 */ + X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI); + CS_InsertEntry (S, X, I+7); + + /* ldy yyy */ + X = NewCodeEntry (OP65_LDY, L[2]->AM, L[2]->Arg, 0, L[2]->LI); + CS_InsertEntry (S, X, I+8); + + /* ldx #$00 */ + X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[5]->LI); + CS_InsertEntry (S, X, I+9); + + /* lda (ptr1),y */ + X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[5]->LI); + CS_InsertEntry (S, X, I+10); + + /* Remove the old code */ + CS_DelEntries (S, I, 6); + + /* Remember, we had changes */ + ++Changes; + + } + + /* Next entry */ + ++I; + + } + + /* Return the number of changes made */ + return Changes; +} + + + +unsigned OptPtrLoad6 (CodeSeg* S) +/* Search for the sequence: + * + * jsr pushax + * ldy xxx + * ldx #$00 + * lda (sp),y + * jsr tosaddax + * ldy #$00 + * jsr ldauidx + * + * and replace it by: + * + * sta ptr1 + * stx ptr1+1 + * ldy xxx + * lda (sp),y + * tay + * ldx #$00 + * lda (ptr1),y + */ +{ + unsigned Changes = 0; + + /* Walk over the entries */ + unsigned I = 0; + while (I < CS_GetEntryCount (S)) { + + CodeEntry* L[7]; + + /* 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, 6) && + L[1]->OPC == OP65_LDY && + L[2]->OPC == OP65_LDX && + CE_IsKnownImm (L[2], 0) && + L[3]->OPC == OP65_LDA && + L[3]->AM == AM65_ZP_INDY && + CE_IsCallTo (L[4], "tosaddax") && + L[5]->OPC == OP65_LDY && + CE_IsKnownImm (L[5], 0) && + CE_IsCallTo (L[6], "ldauidx") && + !CS_RangeHasLabel (S, I+1, 6)) { + + CodeEntry* X; + + /* sta ptr1 */ + X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[0]->LI); + CS_InsertEntry (S, X, I+7); + + /* stx ptr1+1 */ + X = NewCodeEntry (OP65_STX, AM65_ZP, "ptr1+1", 0, L[0]->LI); + CS_InsertEntry (S, X, I+8); + + /* ldy yyy */ + X = NewCodeEntry (OP65_LDY, L[1]->AM, L[1]->Arg, 0, L[1]->LI); + CS_InsertEntry (S, X, I+9); + + /* lda (sp),y */ + X = NewCodeEntry (OP65_LDA, L[3]->AM, L[3]->Arg, 0, L[3]->LI); + CS_InsertEntry (S, X, I+10); + + /* tay */ + X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, L[3]->LI); + CS_InsertEntry (S, X, I+11); + + /* ldx #$00 */ + X = NewCodeEntry (OP65_LDX, AM65_IMM, "$00", 0, L[5]->LI); + CS_InsertEntry (S, X, I+12); + + /* lda (ptr1),y */ + X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[6]->LI); + CS_InsertEntry (S, X, I+13); + + /* Remove the old code */ + CS_DelEntries (S, I, 7); + + /* Remember, we had changes */ + ++Changes; + + } + + /* Next entry */ + ++I; + + } + + /* Return the number of changes made */ + return Changes; +} + + + +unsigned OptPtrLoad7 (CodeSeg* S) +/* Search for the sequence: + * + * jsr aslax1/shlax1 + * clc + * adc xxx + * tay + * txa + * adc yyy + * tax + * tya + * ldy zzz + * jsr ldaxidx + * + * and replace it by: + * + * stx tmp1 + * asl a + * rol tmp1 + * clc + * adc xxx + * sta ptr1 + * lda tmp1 + * adc yyy + * sta ptr1+1 + * ldy zzz + * lda (ptr1),y + * tax + * dey + * lda (ptr1),y + */ +{ + unsigned Changes = 0; + unsigned I; + + /* Generate register info */ + CS_GenRegInfo (S); + + /* Walk over the entries */ + I = 0; + while (I < CS_GetEntryCount (S)) { + + CodeEntry* L[10]; + + /* Get next entry */ + L[0] = CS_GetEntry (S, I); + + /* Check for the sequence */ + if (L[0]->OPC == OP65_JSR && + (strcmp (L[0]->Arg, "aslax1") == 0 || + strcmp (L[0]->Arg, "shlax1") == 0) && + CS_GetEntries (S, L+1, I+1, 9) && + L[1]->OPC == OP65_CLC && + L[2]->OPC == OP65_ADC && + L[3]->OPC == OP65_TAY && + L[4]->OPC == OP65_TXA && + L[5]->OPC == OP65_ADC && + L[6]->OPC == OP65_TAX && + L[7]->OPC == OP65_TYA && + L[8]->OPC == OP65_LDY && + CE_IsCallTo (L[9], "ldaxidx") && + !CS_RangeHasLabel (S, I+1, 9)) { + + CodeEntry* X; + + /* Track the insertion point */ + unsigned IP = I + 10; + + + /* If X is zero on entry to aslax1, we can generate: + * + * asl a + * bcc L1 + * inx + * L1: clc + * + * instead of the code above. "lda tmp1" needs to be changed + * to "txa" in this case. + */ + int ShortCode = (L[0]->RI->In.RegX == 0); + + if (ShortCode) { + + CodeLabel* Lab; + + /* asl a */ + X = NewCodeEntry (OP65_ASL, AM65_IMP, "a", 0, L[0]->LI); + CS_InsertEntry (S, X, IP++); + + /* Generate clc first, since we need the label */ + X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, L[1]->LI); + CS_InsertEntry (S, X, IP); + + /* Get the label */ + Lab = CS_GenLabel (S, X); + + /* bcc Lab */ + X = NewCodeEntry (OP65_BCC, AM65_BRA, Lab->Name, Lab, L[0]->LI); + CS_InsertEntry (S, X, IP++); + + /* inx */ + X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, L[0]->LI); + CS_InsertEntry (S, X, IP++); + + /* Skip the clc insn */ + ++IP; + + } else { + + /* stx tmp1 */ + X = NewCodeEntry (OP65_STX, AM65_ZP, "tmp1", 0, L[0]->LI); + CS_InsertEntry (S, X, IP++); + + /* asl a */ + X = NewCodeEntry (OP65_ASL, AM65_IMP, "a", 0, L[0]->LI); + CS_InsertEntry (S, X, IP++); + + /* rol tmp1 */ + X = NewCodeEntry (OP65_ROL, AM65_ZP, "tmp1", 0, L[0]->LI); + CS_InsertEntry (S, X, IP++); + + /* clc */ + X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, L[1]->LI); + CS_InsertEntry (S, X, IP++); + + } + + /* adc xxx */ + X = NewCodeEntry (L[2]->OPC, L[2]->AM, L[2]->Arg, 0, L[2]->LI); + CS_InsertEntry (S, X, IP++); + + /* sta ptr1 */ + X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1", 0, L[9]->LI); + CS_InsertEntry (S, X, IP++); + + if (ShortCode) { + /* txa */ + X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, L[4]->LI); + } else { + /* lda tmp1 */ + X = NewCodeEntry (OP65_LDA, AM65_ZP, "tmp1", 0, L[4]->LI); + } + CS_InsertEntry (S, X, IP++); + + /* adc xxx */ + X = NewCodeEntry (L[5]->OPC, L[5]->AM, L[5]->Arg, 0, L[5]->LI); + CS_InsertEntry (S, X, IP++); + + /* sta ptr1+1 */ + X = NewCodeEntry (OP65_STA, AM65_ZP, "ptr1+1", 0, L[9]->LI); + CS_InsertEntry (S, X, IP++); + + /* ldy zzz */ + X = NewCodeEntry (L[8]->OPC, L[8]->AM, L[8]->Arg, 0, L[8]->LI); + CS_InsertEntry (S, X, IP++); + + /* lda (ptr1),y */ + X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[9]->LI); + CS_InsertEntry (S, X, IP++); + + /* tax */ + X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, L[9]->LI); + CS_InsertEntry (S, X, IP++); + + /* dey */ + X = NewCodeEntry (OP65_DEY, AM65_IMP, 0, 0, L[9]->LI); + CS_InsertEntry (S, X, IP++); + + /* lda (ptr1),y */ + X = NewCodeEntry (OP65_LDA, AM65_ZP_INDY, "ptr1", 0, L[9]->LI); + CS_InsertEntry (S, X, IP++); + + /* Remove the old code */ + CS_DelEntries (S, I, 10); + + /* Remember, we had changes */ + ++Changes; + + } + + /* Next entry */ + ++I; + + } + + /* Free the register info */ + CS_FreeRegInfo (S); + + /* Return the number of changes made */ + return Changes; +} + + + +unsigned OptPtrLoad11 (CodeSeg* S) /* Search for the sequence: * * clc @@ -547,7 +923,7 @@ unsigned OptPtrLoad5 (CodeSeg* S) -unsigned OptPtrLoad6 (CodeSeg* S) +unsigned OptPtrLoad12 (CodeSeg* S) /* Search for the sequence: * * lda regbank+n @@ -692,7 +1068,7 @@ unsigned OptPtrLoad6 (CodeSeg* S) -unsigned OptPtrLoad7 (CodeSeg* S) +unsigned OptPtrLoad13 (CodeSeg* S) /* Search for the sequence: * * lda zp @@ -760,7 +1136,7 @@ unsigned OptPtrLoad7 (CodeSeg* S) -unsigned OptPtrLoad8 (CodeSeg* S) +unsigned OptPtrLoad14 (CodeSeg* S) /* Search for the sequence: * * lda zp @@ -778,7 +1154,6 @@ unsigned OptPtrLoad8 (CodeSeg* S) * ldx #$00 * lda (zp),y * - * Must execute before OptPtrLoad10! */ { unsigned Changes = 0; @@ -841,7 +1216,7 @@ unsigned OptPtrLoad8 (CodeSeg* S) -unsigned OptPtrLoad9 (CodeSeg* S) +unsigned OptPtrLoad15 (CodeSeg* S) /* Search for the sequence: * * lda zp @@ -919,7 +1294,7 @@ unsigned OptPtrLoad9 (CodeSeg* S) -unsigned OptPtrLoad10 (CodeSeg* S) +unsigned OptPtrLoad16 (CodeSeg* S) /* Search for the sequence * * ldy ... @@ -988,7 +1363,7 @@ unsigned OptPtrLoad10 (CodeSeg* S) -unsigned OptPtrLoad11 (CodeSeg* S) +unsigned OptPtrLoad17 (CodeSeg* S) /* Search for the sequence * * ldy ... diff --git a/src/cc65/coptptrload.h b/src/cc65/coptptrload.h index 155ca0c88..06790aee5 100644 --- a/src/cc65/coptptrload.h +++ b/src/cc65/coptptrload.h @@ -143,6 +143,77 @@ unsigned OptPtrLoad4 (CodeSeg* S); */ unsigned OptPtrLoad5 (CodeSeg* S); +/* Search for the sequence: + * + * jsr pushax + * ldx #$00 + * lda yyy + * jsr tosaddax + * ldy #$00 + * jsr ldauidx + * + * and replace it by: + * + * sta ptr1 + * stx ptr1+1 + * ldy yyy + * lda (ptr1),y + */ + +unsigned OptPtrLoad6 (CodeSeg* S); +/* Search for the sequence: + * + * jsr pushax + * ldy xxx + * ldx #$00 + * lda (sp),y + * jsr tosaddax + * ldy #$00 + * jsr ldauidx + * + * and replace it by: + * + * sta ptr1 + * stx ptr1+1 + * ldy xxx + * lda (sp),y + * tay + * lda (ptr1),y + */ + +unsigned OptPtrLoad7 (CodeSeg* S); +/* Search for the sequence: + * + * jsr aslax1/shlax1 + * clc + * adc xxx + * tay + * txa + * adc yyy + * tax + * tya + * ldy zzz + * jsr ldaxidx + * + * and replace it by: + * + * stx tmp1 + * asl a + * rol tmp1 + * clc + * adc xxx + * sta ptr1 + * lda tmp1 + * adc yyy + * sta ptr1+1 + * ldy zzz + * lda (ptr1),y + * tax + * dey + * lda (ptr1),y + */ + +unsigned OptPtrLoad11 (CodeSeg* S); /* Search for the sequence: * * clc @@ -161,8 +232,8 @@ unsigned OptPtrLoad5 (CodeSeg* S); * lda (ptr1),y */ -unsigned OptPtrLoad6 (CodeSeg* S); -/* Search for the sequence: +unsigned OptPtrLoad12 (CodeSeg* S); +/* Search for the sequence: * * lda regbank+n * ldx regbank+n+1 @@ -193,7 +264,7 @@ unsigned OptPtrLoad6 (CodeSeg* S); * */ -unsigned OptPtrLoad7 (CodeSeg* S); +unsigned OptPtrLoad13 (CodeSeg* S); /* Search for the sequence: * * lda zp @@ -208,7 +279,7 @@ unsigned OptPtrLoad7 (CodeSeg* S); * lda (zp),y */ -unsigned OptPtrLoad8 (CodeSeg* S); +unsigned OptPtrLoad14 (CodeSeg* S); /* Search for the sequence: * * lda zp @@ -229,7 +300,7 @@ unsigned OptPtrLoad8 (CodeSeg* S); * Must execute before OptPtrLoad10! */ -unsigned OptPtrLoad9 (CodeSeg* S); +unsigned OptPtrLoad15 (CodeSeg* S); /* Search for the sequence: * * lda zp @@ -246,7 +317,7 @@ unsigned OptPtrLoad9 (CodeSeg* S); * lda (zp),y */ -unsigned OptPtrLoad10 (CodeSeg* S); +unsigned OptPtrLoad16 (CodeSeg* S); /* Search for the sequence * * ldy ... @@ -263,7 +334,7 @@ unsigned OptPtrLoad10 (CodeSeg* S); * This step must be executed *after* OptPtrLoad1! */ -unsigned OptPtrLoad11 (CodeSeg* S); +unsigned OptPtrLoad17 (CodeSeg* S); /* Search for the sequence * * ldy ... -- 2.39.5