mirror of
https://github.com/cc65/cc65.git
synced 2024-10-17 02:26:16 +00:00
Optimizations
git-svn-id: svn://svn.cc65.org/cc65/trunk@1009 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
parent
8d6f5a2fca
commit
0c09cc7242
@ -2879,6 +2879,15 @@ void g_asr (unsigned flags, unsigned long val)
|
|||||||
|
|
||||||
case CF_CHAR:
|
case CF_CHAR:
|
||||||
case CF_INT:
|
case CF_INT:
|
||||||
|
if (val >= 8 && (flags & CF_UNSIGNED)) {
|
||||||
|
AddCodeLine ("txa");
|
||||||
|
ldxconst (0);
|
||||||
|
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 shrax%ld", val);
|
AddCodeLine ("jsr shrax%ld", val);
|
||||||
@ -2886,10 +2895,6 @@ void g_asr (unsigned flags, unsigned long val)
|
|||||||
AddCodeLine ("jsr asrax%ld", val);
|
AddCodeLine ("jsr asrax%ld", val);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
} else if (val == 8 && (flags & CF_UNSIGNED)) {
|
|
||||||
AddCodeLine ("txa");
|
|
||||||
ldxconst (0);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -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,10 +2983,6 @@ 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;
|
||||||
|
|
||||||
|
@ -161,31 +161,54 @@ 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);
|
||||||
|
|
||||||
|
/* Check the entry before the push, if it's a lda instruction with an
|
||||||
|
* addressing mode that does not use an additional index register. If
|
||||||
|
* so, we may use this location for the add and must not save the
|
||||||
|
* value in the zero page location.
|
||||||
|
*/
|
||||||
|
DirectAdd = (P->OPC == OP65_LDA &&
|
||||||
|
(P->AM == AM65_IMM || P->AM == AM65_ZP || P->AM == AM65_ABS));
|
||||||
|
|
||||||
/* Store the value into the zeropage instead of pushing it */
|
/* Store the value into the zeropage instead of pushing it */
|
||||||
|
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);
|
X = NewCodeEntry (OP65_STA, AM65_ZP, ZPLo, 0, PushEntry->LI);
|
||||||
CS_InsertEntry (S, X, Push+1);
|
CS_InsertEntry (S, X, Push+1);
|
||||||
X = NewCodeEntry (OP65_STX, AM65_ZP, ZPHi, 0, PushEntry->LI);
|
++Add; /* Correct the index */
|
||||||
CS_InsertEntry (S, X, Push+2);
|
}
|
||||||
|
|
||||||
/* Correct the index of the add and get a pointer to the entry */
|
/* Get a pointer to the add entry */
|
||||||
Add += 2;
|
|
||||||
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);
|
||||||
|
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);
|
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 */
|
||||||
@ -240,25 +263,47 @@ 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);
|
||||||
|
|
||||||
|
/* Check the entry before the push, if it's a lda instruction with an
|
||||||
|
* addressing mode that does not use an additional index register. If
|
||||||
|
* so, we may use this location for the and and must not save the
|
||||||
|
* value in the zero page location.
|
||||||
|
*/
|
||||||
|
DirectAnd = (P->OPC == OP65_LDA &&
|
||||||
|
(P->AM == AM65_IMM || P->AM == AM65_ZP || P->AM == AM65_ABS));
|
||||||
|
|
||||||
/* Store the value into the zeropage instead of pushing it */
|
/* Store the value into the zeropage instead of pushing it */
|
||||||
|
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);
|
X = NewCodeEntry (OP65_STA, AM65_ZP, ZPLo, 0, PushEntry->LI);
|
||||||
CS_InsertEntry (S, X, Push+1);
|
CS_InsertEntry (S, X, Push+1);
|
||||||
X = NewCodeEntry (OP65_STX, AM65_ZP, ZPHi, 0, PushEntry->LI);
|
++And; /* Correct the index */
|
||||||
CS_InsertEntry (S, X, Push+2);
|
}
|
||||||
|
|
||||||
/* Correct the index of the add and get a pointer to the entry */
|
/* Get a pointer to the and entry */
|
||||||
And += 2;
|
|
||||||
AndEntry = CS_GetEntry (S, And);
|
AndEntry = CS_GetEntry (S, And);
|
||||||
|
|
||||||
/* Inline the and */
|
/* Inline the and */
|
||||||
|
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);
|
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 */
|
||||||
@ -292,25 +337,46 @@ 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);
|
||||||
|
|
||||||
|
/* Check the entry before the push, if it's a lda instruction with an
|
||||||
|
* addressing mode that does not use an additional index register. If
|
||||||
|
* so, we may use this location for the or and must not save the
|
||||||
|
* value in the zero page location.
|
||||||
|
*/
|
||||||
|
DirectOr = (P->OPC == OP65_LDA &&
|
||||||
|
(P->AM == AM65_IMM || P->AM == AM65_ZP || P->AM == AM65_ABS));
|
||||||
|
|
||||||
/* Store the value into the zeropage instead of pushing it */
|
/* Store the value into the zeropage instead of pushing it */
|
||||||
|
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);
|
X = NewCodeEntry (OP65_STA, AM65_ZP, ZPLo, 0, PushEntry->LI);
|
||||||
CS_InsertEntry (S, X, Push+1);
|
CS_InsertEntry (S, X, Push+1);
|
||||||
X = NewCodeEntry (OP65_STX, AM65_ZP, ZPHi, 0, PushEntry->LI);
|
++Or; /* Correct the index */
|
||||||
CS_InsertEntry (S, X, Push+2);
|
}
|
||||||
|
|
||||||
/* Correct the index of the add and get a pointer to the entry */
|
/* Get a pointer to the or entry */
|
||||||
Or += 2;
|
|
||||||
OrEntry = CS_GetEntry (S, Or);
|
OrEntry = CS_GetEntry (S, Or);
|
||||||
|
|
||||||
/* Inline the or */
|
/* Inline the or */
|
||||||
|
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);
|
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,27 +411,49 @@ 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);
|
||||||
|
|
||||||
|
/* Check the entry before the push, if it's a lda instruction with an
|
||||||
|
* addressing mode that does not use an additional index register. If
|
||||||
|
* so, we may use this location for the xor and must not save the
|
||||||
|
* value in the zero page location.
|
||||||
|
*/
|
||||||
|
DirectXor = (P->OPC == OP65_LDA &&
|
||||||
|
(P->AM == AM65_IMM || P->AM == AM65_ZP || P->AM == AM65_ABS));
|
||||||
|
|
||||||
/* Store the value into the zeropage instead of pushing it */
|
/* Store the value into the zeropage instead of pushing it */
|
||||||
|
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);
|
X = NewCodeEntry (OP65_STA, AM65_ZP, ZPLo, 0, PushEntry->LI);
|
||||||
CS_InsertEntry (S, X, Push+1);
|
CS_InsertEntry (S, X, Push+1);
|
||||||
X = NewCodeEntry (OP65_STX, AM65_ZP, ZPHi, 0, PushEntry->LI);
|
++Xor; /* Correct the index */
|
||||||
CS_InsertEntry (S, X, Push+2);
|
}
|
||||||
|
|
||||||
/* Correct the index of the add and get a pointer to the entry */
|
/* Get a pointer to the entry */
|
||||||
Xor += 2;
|
|
||||||
XorEntry = CS_GetEntry (S, Xor);
|
XorEntry = CS_GetEntry (S, Xor);
|
||||||
|
|
||||||
/* Inline the or */
|
/* Inline the xor */
|
||||||
|
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);
|
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 */
|
||||||
|
Loading…
Reference in New Issue
Block a user