1
0
mirror of https://github.com/cc65/cc65.git synced 2024-09-07 09:54:35 +00:00

Optimizations

git-svn-id: svn://svn.cc65.org/cc65/trunk@1009 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz 2001-10-04 11:34:18 +00:00
parent 8d6f5a2fca
commit 0c09cc7242
2 changed files with 203 additions and 105 deletions

View File

@ -2813,7 +2813,7 @@ void g_and (unsigned flags, unsigned long val)
AddCodeLine ("and #$%02X", (unsigned char)(val >> 8)); AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
AddCodeLine ("tax"); AddCodeLine ("tax");
ldaconst (0); ldaconst (0);
} else { } else {
AddCodeLine ("tay"); AddCodeLine ("tay");
AddCodeLine ("txa"); AddCodeLine ("txa");
AddCodeLine ("and #$%02X", (unsigned char)(val >> 8)); AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
@ -2879,57 +2879,62 @@ void g_asr (unsigned flags, unsigned long val)
case CF_CHAR: case CF_CHAR:
case CF_INT: case CF_INT:
if (val >= 1 && val <= 4) { if (val >= 8 && (flags & CF_UNSIGNED)) {
if (flags & CF_UNSIGNED) {
AddCodeLine ("jsr shrax%ld", val);
} else {
AddCodeLine ("jsr asrax%ld", val);
}
return;
} else if (val == 8 && (flags & CF_UNSIGNED)) {
AddCodeLine ("txa"); AddCodeLine ("txa");
ldxconst (0); ldxconst (0);
return; val -= 8;
} if (val == 0) {
break; /* Done */
return;
}
}
if (val >= 1 && val <= 4) {
if (flags & CF_UNSIGNED) {
AddCodeLine ("jsr shrax%ld", val);
} else {
AddCodeLine ("jsr asrax%ld", val);
}
return;
}
break;
case CF_LONG: case CF_LONG:
if (val >= 1 && val <= 4) { if (val >= 1 && val <= 4) {
if (flags & CF_UNSIGNED) { if (flags & CF_UNSIGNED) {
AddCodeLine ("jsr shreax%ld", val); AddCodeLine ("jsr shreax%ld", val);
} else { } else {
AddCodeLine ("jsr asreax%ld", val); AddCodeLine ("jsr asreax%ld", val);
} }
return; return;
} else if (val == 8 && (flags & CF_UNSIGNED)) { } else if (val == 8 && (flags & CF_UNSIGNED)) {
AddCodeLine ("txa"); AddCodeLine ("txa");
AddCodeLine ("ldx sreg"); AddCodeLine ("ldx sreg");
AddCodeLine ("ldy sreg+1"); AddCodeLine ("ldy sreg+1");
AddCodeLine ("sty sreg"); AddCodeLine ("sty sreg");
AddCodeLine ("ldy #$00"); AddCodeLine ("ldy #$00");
AddCodeLine ("sty sreg+1"); AddCodeLine ("sty sreg+1");
return; return;
} else if (val == 16) { } else if (val == 16) {
AddCodeLine ("ldy #$00"); AddCodeLine ("ldy #$00");
AddCodeLine ("ldx sreg+1"); AddCodeLine ("ldx sreg+1");
if ((flags & CF_UNSIGNED) == 0) { if ((flags & CF_UNSIGNED) == 0) {
unsigned L = GetLocalLabel(); unsigned L = GetLocalLabel();
AddCodeLine ("bpl %s", LocalLabelName (L)); AddCodeLine ("bpl %s", LocalLabelName (L));
AddCodeLine ("dey"); AddCodeLine ("dey");
g_defcodelabel (L); g_defcodelabel (L);
} }
AddCodeLine ("lda sreg"); AddCodeLine ("lda sreg");
AddCodeLine ("sty sreg+1"); AddCodeLine ("sty sreg+1");
AddCodeLine ("sty sreg"); AddCodeLine ("sty sreg");
return; return;
} }
break; break;
default: default:
typeerror (flags); typeerror (flags);
} }
/* If we go here, we didn't emit code. Push the lhs on stack and fall /* If we go here, we didn't emit code. Push the lhs on stack and fall
* into the normal, non-optimized stuff. * into the normal, non-optimized stuff.
*/ */
g_push (flags & ~CF_CONST, 0); g_push (flags & ~CF_CONST, 0);
@ -2962,6 +2967,15 @@ void g_asl (unsigned flags, unsigned long val)
case CF_CHAR: case CF_CHAR:
case CF_INT: case CF_INT:
if (val >= 8) {
AddCodeLine ("tax");
AddCodeLine ("lda #$00");
val -= 8;
if (val == 0) {
/* Done */
return;
}
}
if (val >= 1 && val <= 4) { if (val >= 1 && val <= 4) {
if (flags & CF_UNSIGNED) { if (flags & CF_UNSIGNED) {
AddCodeLine ("jsr shlax%ld", val); AddCodeLine ("jsr shlax%ld", val);
@ -2969,11 +2983,7 @@ void g_asl (unsigned flags, unsigned long val)
AddCodeLine ("jsr aslax%ld", val); AddCodeLine ("jsr aslax%ld", val);
} }
return; return;
} else if (val == 8) { }
AddCodeLine ("tax");
AddCodeLine ("lda #$00");
return;
}
break; break;
case CF_LONG: case CF_LONG:
@ -2985,7 +2995,7 @@ void g_asl (unsigned flags, unsigned long val)
} }
return; return;
} else if (val == 8) { } else if (val == 8) {
AddCodeLine ("ldy sreg"); AddCodeLine ("ldy sreg");
AddCodeLine ("sty sreg+1"); AddCodeLine ("sty sreg+1");
AddCodeLine ("stx sreg"); AddCodeLine ("stx sreg");
AddCodeLine ("tax"); AddCodeLine ("tax");
@ -3028,7 +3038,7 @@ void g_neg (unsigned flags)
break; break;
case CF_LONG: case CF_LONG:
AddCodeLine ("jsr negeax"); AddCodeLine ("jsr negeax");
break; break;
default: default:

View File

@ -161,45 +161,68 @@ static unsigned Opt_tosaddax (CodeSeg* S, unsigned Push, unsigned Add,
const char* ZPLo, const char* ZPHi) const char* ZPLo, const char* ZPHi)
/* Optimize the tosaddax sequence if possible */ /* Optimize the tosaddax sequence if possible */
{ {
CodeEntry* P;
CodeEntry* N; CodeEntry* N;
CodeEntry* X; CodeEntry* X;
CodeEntry* PushEntry; CodeEntry* PushEntry;
CodeEntry* AddEntry; CodeEntry* AddEntry;
int DirectAdd;
/* We need the entry behind the add */ /* We need the entry behind the add */
CHECK ((N = CS_GetNextEntry (S, Add)) != 0); CHECK ((N = CS_GetNextEntry (S, Add)) != 0);
/* And the entry before the push */
CHECK ((P = CS_GetPrevEntry (S, Push)) != 0);
/* Get the push entry */ /* Get the push entry */
PushEntry = CS_GetEntry (S, Push); PushEntry = CS_GetEntry (S, Push);
/* Store the value into the zeropage instead of pushing it */ /* Check the entry before the push, if it's a lda instruction with an
X = NewCodeEntry (OP65_STA, AM65_ZP, ZPLo, 0, PushEntry->LI); * addressing mode that does not use an additional index register. If
CS_InsertEntry (S, X, Push+1); * so, we may use this location for the add and must not save the
X = NewCodeEntry (OP65_STX, AM65_ZP, ZPHi, 0, PushEntry->LI); * value in the zero page location.
CS_InsertEntry (S, X, Push+2); */
DirectAdd = (P->OPC == OP65_LDA &&
(P->AM == AM65_IMM || P->AM == AM65_ZP || P->AM == AM65_ABS));
/* Correct the index of the add and get a pointer to the entry */ /* Store the value into the zeropage instead of pushing it */
Add += 2; X = NewCodeEntry (OP65_STX, AM65_ZP, ZPHi, 0, PushEntry->LI);
CS_InsertEntry (S, X, Push+1);
++Add; /* Correct the index */
if (!DirectAdd) {
X = NewCodeEntry (OP65_STA, AM65_ZP, ZPLo, 0, PushEntry->LI);
CS_InsertEntry (S, X, Push+1);
++Add; /* Correct the index */
}
/* Get a pointer to the add entry */
AddEntry = CS_GetEntry (S, Add); AddEntry = CS_GetEntry (S, Add);
/* Inline the add */ /* Inline the add */
X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, AddEntry->LI); X = NewCodeEntry (OP65_CLC, AM65_IMP, 0, 0, AddEntry->LI);
CS_InsertEntry (S, X, Add+1); CS_InsertEntry (S, X, Add+1);
X = NewCodeEntry (OP65_ADC, AM65_ZP, ZPLo, 0, AddEntry->LI); if (DirectAdd) {
/* Add from variable location */
X = NewCodeEntry (OP65_ADC, P->AM, P->Arg, 0, AddEntry->LI);
} else {
/* Add from temp storage */
X = NewCodeEntry (OP65_ADC, AM65_ZP, ZPLo, 0, AddEntry->LI);
}
CS_InsertEntry (S, X, Add+2); CS_InsertEntry (S, X, Add+2);
if (PushEntry->RI->In.RegX == 0) { if (PushEntry->RI->In.RegX == 0) {
/* The high byte is the value in X plus the carry */ /* The high byte is the value in X plus the carry */
CodeLabel* L = CS_GenLabel (S, N); CodeLabel* L = CS_GenLabel (S, N);
X = NewCodeEntry (OP65_BCC, AM65_BRA, L->Name, L, AddEntry->LI); X = NewCodeEntry (OP65_BCC, AM65_BRA, L->Name, L, AddEntry->LI);
CS_InsertEntry (S, X, Add+3); CS_InsertEntry (S, X, Add+3);
X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, AddEntry->LI); X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, AddEntry->LI);
CS_InsertEntry (S, X, Add+4); CS_InsertEntry (S, X, Add+4);
} else if (AddEntry->RI->In.RegX == 0) { } else if (AddEntry->RI->In.RegX == 0) {
/* The high byte is that of the first operand plus carry */ /* The high byte is that of the first operand plus carry */
CodeLabel* L; CodeLabel* L;
if (PushEntry->RI->In.RegX >= 0) { if (PushEntry->RI->In.RegX >= 0) {
/* Value of first op high byte is known */ /* Value of first op high byte is known */
char Buf [16]; char Buf [16];
xsprintf (Buf, sizeof (Buf), "$%02X", PushEntry->RI->In.RegX); xsprintf (Buf, sizeof (Buf), "$%02X", PushEntry->RI->In.RegX);
X = NewCodeEntry (OP65_LDX, AM65_IMM, Buf, 0, AddEntry->LI); X = NewCodeEntry (OP65_LDX, AM65_IMM, Buf, 0, AddEntry->LI);
} else { } else {
@ -237,28 +260,50 @@ static unsigned Opt_tosaddax (CodeSeg* S, unsigned Push, unsigned Add,
static unsigned Opt_tosandax (CodeSeg* S, unsigned Push, unsigned And, static unsigned Opt_tosandax (CodeSeg* S, unsigned Push, unsigned And,
const char* ZPLo, const char* ZPHi) const char* ZPLo, const char* ZPHi)
/* Optimize the tosandax sequence if possible */ /* Optimize the tosandax sequence if possible */
{ {
CodeEntry* P;
CodeEntry* X; CodeEntry* X;
CodeEntry* PushEntry; CodeEntry* PushEntry;
CodeEntry* AndEntry; CodeEntry* AndEntry;
int DirectAnd;
/* Get the entry before the push */
CHECK ((P = CS_GetPrevEntry (S, Push)) != 0);
/* Get the push entry */ /* Get the push entry */
PushEntry = CS_GetEntry (S, Push); PushEntry = CS_GetEntry (S, Push);
/* Store the value into the zeropage instead of pushing it */ /* Check the entry before the push, if it's a lda instruction with an
X = NewCodeEntry (OP65_STA, AM65_ZP, ZPLo, 0, PushEntry->LI); * addressing mode that does not use an additional index register. If
CS_InsertEntry (S, X, Push+1); * so, we may use this location for the and and must not save the
X = NewCodeEntry (OP65_STX, AM65_ZP, ZPHi, 0, PushEntry->LI); * value in the zero page location.
CS_InsertEntry (S, X, Push+2); */
DirectAnd = (P->OPC == OP65_LDA &&
(P->AM == AM65_IMM || P->AM == AM65_ZP || P->AM == AM65_ABS));
/* Correct the index of the add and get a pointer to the entry */ /* Store the value into the zeropage instead of pushing it */
And += 2; X = NewCodeEntry (OP65_STX, AM65_ZP, ZPHi, 0, PushEntry->LI);
CS_InsertEntry (S, X, Push+1);
++And; /* Correct the index */
if (!DirectAnd) {
X = NewCodeEntry (OP65_STA, AM65_ZP, ZPLo, 0, PushEntry->LI);
CS_InsertEntry (S, X, Push+1);
++And; /* Correct the index */
}
/* Get a pointer to the and entry */
AndEntry = CS_GetEntry (S, And); AndEntry = CS_GetEntry (S, And);
/* Inline the and */ /* Inline the and */
X = NewCodeEntry (OP65_AND, AM65_ZP, ZPLo, 0, AndEntry->LI); if (DirectAnd) {
/* And with variable location */
X = NewCodeEntry (OP65_AND, P->AM, P->Arg, 0, AndEntry->LI);
} else {
/* And with temp storage */
X = NewCodeEntry (OP65_AND, AM65_ZP, ZPLo, 0, AndEntry->LI);
}
CS_InsertEntry (S, X, And+1); CS_InsertEntry (S, X, And+1);
if (PushEntry->RI->In.RegX == 0 || AndEntry->RI->In.RegX == 0) { if (PushEntry->RI->In.RegX == 0 || AndEntry->RI->In.RegX == 0) {
/* The high byte is zero */ /* The high byte is zero */
@ -289,28 +334,49 @@ static unsigned Opt_tosandax (CodeSeg* S, unsigned Push, unsigned And,
static unsigned Opt_tosorax (CodeSeg* S, unsigned Push, unsigned Or, static unsigned Opt_tosorax (CodeSeg* S, unsigned Push, unsigned Or,
const char* ZPLo, const char* ZPHi) const char* ZPLo, const char* ZPHi)
/* Optimize the tosorax sequence if possible */ /* Optimize the tosorax sequence if possible */
{ {
CodeEntry* P;
CodeEntry* X; CodeEntry* X;
CodeEntry* PushEntry; CodeEntry* PushEntry;
CodeEntry* OrEntry; CodeEntry* OrEntry;
int DirectOr;
/* Get the entry before the push */
CHECK ((P = CS_GetPrevEntry (S, Push)) != 0);
/* Get the push entry */ /* Get the push entry */
PushEntry = CS_GetEntry (S, Push); PushEntry = CS_GetEntry (S, Push);
/* Store the value into the zeropage instead of pushing it */ /* Check the entry before the push, if it's a lda instruction with an
X = NewCodeEntry (OP65_STA, AM65_ZP, ZPLo, 0, PushEntry->LI); * addressing mode that does not use an additional index register. If
CS_InsertEntry (S, X, Push+1); * so, we may use this location for the or and must not save the
X = NewCodeEntry (OP65_STX, AM65_ZP, ZPHi, 0, PushEntry->LI); * value in the zero page location.
CS_InsertEntry (S, X, Push+2); */
DirectOr = (P->OPC == OP65_LDA &&
(P->AM == AM65_IMM || P->AM == AM65_ZP || P->AM == AM65_ABS));
/* Correct the index of the add and get a pointer to the entry */ /* Store the value into the zeropage instead of pushing it */
Or += 2; X = NewCodeEntry (OP65_STX, AM65_ZP, ZPHi, 0, PushEntry->LI);
CS_InsertEntry (S, X, Push+1);
++Or; /* Correct the index */
if (DirectOr) {
X = NewCodeEntry (OP65_STA, AM65_ZP, ZPLo, 0, PushEntry->LI);
CS_InsertEntry (S, X, Push+1);
++Or; /* Correct the index */
}
/* Get a pointer to the or entry */
OrEntry = CS_GetEntry (S, Or); OrEntry = CS_GetEntry (S, Or);
/* Inline the or */ /* Inline the or */
X = NewCodeEntry (OP65_ORA, AM65_ZP, ZPLo, 0, OrEntry->LI); if (DirectOr) {
/* Or with variable location */
X = NewCodeEntry (OP65_ORA, P->AM, P->Arg, 0, OrEntry->LI);
} else {
X = NewCodeEntry (OP65_ORA, AM65_ZP, ZPLo, 0, OrEntry->LI);
}
CS_InsertEntry (S, X, Or+1); CS_InsertEntry (S, X, Or+1);
if (PushEntry->RI->In.RegX >= 0 && OrEntry->RI->In.RegX >= 0) { if (PushEntry->RI->In.RegX >= 0 && OrEntry->RI->In.RegX >= 0) {
/* Both values known, precalculate the result */ /* Both values known, precalculate the result */
@ -345,35 +411,57 @@ static unsigned Opt_tosorax (CodeSeg* S, unsigned Push, unsigned Or,
static unsigned Opt_tosxorax (CodeSeg* S, unsigned Push, unsigned Xor, static unsigned Opt_tosxorax (CodeSeg* S, unsigned Push, unsigned Xor,
const char* ZPLo, const char* ZPHi) const char* ZPLo, const char* ZPHi)
/* Optimize the tosorax sequence if possible */ /* Optimize the tosxorax sequence if possible */
{ {
CodeEntry* P;
CodeEntry* X; CodeEntry* X;
CodeEntry* PushEntry; CodeEntry* PushEntry;
CodeEntry* XorEntry; CodeEntry* XorEntry;
int DirectXor;
/* Get the entry before the push */
CHECK ((P = CS_GetPrevEntry (S, Push)) != 0);
/* Get the push entry */ /* Get the push entry */
PushEntry = CS_GetEntry (S, Push); PushEntry = CS_GetEntry (S, Push);
/* Store the value into the zeropage instead of pushing it */ /* Check the entry before the push, if it's a lda instruction with an
X = NewCodeEntry (OP65_STA, AM65_ZP, ZPLo, 0, PushEntry->LI); * addressing mode that does not use an additional index register. If
CS_InsertEntry (S, X, Push+1); * so, we may use this location for the xor and must not save the
X = NewCodeEntry (OP65_STX, AM65_ZP, ZPHi, 0, PushEntry->LI); * value in the zero page location.
CS_InsertEntry (S, X, Push+2); */
DirectXor = (P->OPC == OP65_LDA &&
(P->AM == AM65_IMM || P->AM == AM65_ZP || P->AM == AM65_ABS));
/* Correct the index of the add and get a pointer to the entry */ /* Store the value into the zeropage instead of pushing it */
Xor += 2; X = NewCodeEntry (OP65_STX, AM65_ZP, ZPHi, 0, PushEntry->LI);
CS_InsertEntry (S, X, Push+1);
++Xor; /* Correct the index */
if (DirectXor) {
X = NewCodeEntry (OP65_STA, AM65_ZP, ZPLo, 0, PushEntry->LI);
CS_InsertEntry (S, X, Push+1);
++Xor; /* Correct the index */
}
/* Get a pointer to the entry */
XorEntry = CS_GetEntry (S, Xor); XorEntry = CS_GetEntry (S, Xor);
/* Inline the or */ /* Inline the xor */
X = NewCodeEntry (OP65_EOR, AM65_ZP, ZPLo, 0, XorEntry->LI); if (DirectXor) {
/* Xor with variable location */
X = NewCodeEntry (OP65_EOR, P->AM, P->Arg, 0, XorEntry->LI);
} else {
/* Xor with temp storage */
X = NewCodeEntry (OP65_EOR, AM65_ZP, ZPLo, 0, XorEntry->LI);
}
CS_InsertEntry (S, X, Xor+1); CS_InsertEntry (S, X, Xor+1);
if (PushEntry->RI->In.RegX >= 0 && XorEntry->RI->In.RegX >= 0) { if (PushEntry->RI->In.RegX >= 0 && XorEntry->RI->In.RegX >= 0) {
/* Both values known, precalculate the result */ /* Both values known, precalculate the result */
char Buf [16]; char Buf [16];
int Val = (PushEntry->RI->In.RegX ^ XorEntry->RI->In.RegX); int Val = (PushEntry->RI->In.RegX ^ XorEntry->RI->In.RegX);
xsprintf (Buf, sizeof (Buf), "$%02X", Val); xsprintf (Buf, sizeof (Buf), "$%02X", Val);
X = NewCodeEntry (OP65_LDX, AM65_IMM, Buf, 0, XorEntry->LI); X = NewCodeEntry (OP65_LDX, AM65_IMM, Buf, 0, XorEntry->LI);
CS_InsertEntry (S, X, Xor+2); CS_InsertEntry (S, X, Xor+2);
} else if (PushEntry->RI->In.RegX != 0) { } else if (PushEntry->RI->In.RegX != 0) {
/* High byte is unknown */ /* High byte is unknown */
X = NewCodeEntry (OP65_STA, AM65_ZP, ZPLo, 0, XorEntry->LI); X = NewCodeEntry (OP65_STA, AM65_ZP, ZPLo, 0, XorEntry->LI);
@ -399,13 +487,13 @@ static unsigned Opt_tosxorax (CodeSeg* S, unsigned Push, unsigned Xor,
/*****************************************************************************/ /*****************************************************************************/
/* Code */ /* Code */
/*****************************************************************************/ /*****************************************************************************/
typedef unsigned (*OptFunc) (CodeSeg* S, unsigned Push, unsigned Store, typedef unsigned (*OptFunc) (CodeSeg* S, unsigned Push, unsigned Store,
const char* ZPLo, const char* ZPHi); const char* ZPLo, const char* ZPHi);
typedef struct OptFuncDesc OptFuncDesc; typedef struct OptFuncDesc OptFuncDesc;
struct OptFuncDesc { struct OptFuncDesc {
const char* Name; /* Name of the replaced runtime function */ const char* Name; /* Name of the replaced runtime function */