From d790ae1b42f1a4940d7e8fc4121433716e116fe9 Mon Sep 17 00:00:00 2001 From: cuz Date: Mon, 18 Aug 2003 14:40:27 +0000 Subject: [PATCH] More optimizations git-svn-id: svn://svn.cc65.org/cc65/trunk@2363 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/cc65/codeopt.c | 108 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/src/cc65/codeopt.c b/src/cc65/codeopt.c index a99b507c6..a3b49ef48 100644 --- a/src/cc65/codeopt.c +++ b/src/cc65/codeopt.c @@ -531,6 +531,111 @@ static unsigned OptPtrStore1 (CodeSeg* S) +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 */ /*****************************************************************************/ @@ -1418,6 +1523,7 @@ static OptFunc DOptPtrLoad5 = { OptPtrLoad5, "OptPtrLoad5", 65, 0, static OptFunc DOptPtrLoad6 = { OptPtrLoad6, "OptPtrLoad6", 86, 0, 0, 0, 0, 0 }; static OptFunc DOptPtrLoad7 = { OptPtrLoad7, "OptPtrLoad7", 100, 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 }; static OptFunc DOptPush2 = { OptPush2, "OptPush2", 50, 0, 0, 0, 0, 0 }; static OptFunc DOptPushPop = { OptPushPop, "OptPushPop", 0, 0, 0, 0, 0, 0 }; @@ -1481,6 +1587,7 @@ static OptFunc* OptFuncs[] = { &DOptPtrLoad6, &DOptPtrLoad7, &DOptPtrStore1, + &DOptPtrStore2, &DOptPush1, &DOptPush2, &DOptPushPop, @@ -1738,6 +1845,7 @@ static unsigned RunOptGroup1 (CodeSeg* S) unsigned Changes = 0; Changes += RunOptFunc (S, &DOptPtrStore1, 1); + Changes += RunOptFunc (S, &DOptPtrStore2, 1); Changes += RunOptFunc (S, &DOptPtrLoad1, 1); Changes += RunOptFunc (S, &DOptPtrLoad2, 1); Changes += RunOptFunc (S, &DOptPtrLoad3, 1);