/*****************************************************************************/
/* */
-/* coptstop.c */
+/* coptstop.c */
/* */
-/* Optimize operations that take operands via the stack */
+/* Optimize operations that take operands via the stack */
/* */
/* */
/* */
/*****************************************************************************/
-/* Helpers */
+/* Helpers */
/*****************************************************************************/
int I = D->PushIndex + 1;
while (I < D->OpIndex) {
- CodeEntry* E = CS_GetEntry (D->Code, I);
+ CodeEntry* E = CS_GetEntry (D->Code, I);
int NeedCorrection = 0;
- if ((E->Use & REG_SP) != 0) {
+ if ((E->Use & REG_SP) != 0) {
- /* Check for some things that should not happen */
- CHECK (E->AM == AM65_ZP_INDY || E->RI->In.RegY >= (short) Offs);
- CHECK (strcmp (E->Arg, "sp") == 0);
+ /* Check for some things that should not happen */
+ CHECK (E->AM == AM65_ZP_INDY || E->RI->In.RegY >= (short) Offs);
+ CHECK (strcmp (E->Arg, "sp") == 0);
/* We need to correct this one */
NeedCorrection = 1;
if (NeedCorrection) {
- /* Get the code entry before this one. If it's a LDY, adjust the
- * value.
- */
- CodeEntry* P = CS_GetPrevEntry (D->Code, I);
- if (P && P->OPC == OP65_LDY && CE_IsConstImm (P)) {
+ /* Get the code entry before this one. If it's a LDY, adjust the
+ * value.
+ */
+ CodeEntry* P = CS_GetPrevEntry (D->Code, I);
+ if (P && P->OPC == OP65_LDY && CE_IsConstImm (P)) {
- /* The Y load is just before the stack access, adjust it */
- CE_SetNumArg (P, P->Num - Offs);
+ /* The Y load is just before the stack access, adjust it */
+ CE_SetNumArg (P, P->Num - Offs);
- } else {
+ } else {
- /* Insert a new load instruction before the stack access */
- const char* Arg = MakeHexArg (E->RI->In.RegY - Offs);
- CodeEntry* X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
- InsertEntry (D, X, I++);
+ /* Insert a new load instruction before the stack access */
+ const char* Arg = MakeHexArg (E->RI->In.RegY - Offs);
+ CodeEntry* X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
+ InsertEntry (D, X, I++);
- }
+ }
/* If we need the value of Y later, be sure to reload it */
if (RegYUsed (D->Code, I+1)) {
- const char* Arg = MakeHexArg (E->RI->In.RegY);
- CodeEntry* X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
- InsertEntry (D, X, I+1);
+ const char* Arg = MakeHexArg (E->RI->In.RegY);
+ CodeEntry* X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
+ InsertEntry (D, X, I+1);
- /* Skip this instruction in the next round */
- ++I;
+ /* Skip this instruction in the next round */
+ ++I;
}
- }
+ }
- /* Next entry */
- ++I;
+ /* Next entry */
+ ++I;
}
/* If we have rhs load insns that load from stack, we'll have to adjust
CodeEntry* X;
if ((LI->A.Flags & LI_DIRECT) != 0) {
- /* Op with a variable location. If the location is on the stack, we
+ /* Op with a variable location. If the location is on the stack, we
* need to reload the Y register.
*/
if ((LI->A.Flags & LI_RELOAD_Y) == 0) {
} else {
- /* Op with temp storage */
- X = NewCodeEntry (OPC, AM65_ZP, D->ZPLo, 0, D->OpEntry->LI);
+ /* Op with temp storage */
+ X = NewCodeEntry (OPC, AM65_ZP, D->ZPLo, 0, D->OpEntry->LI);
InsertEntry (D, X, D->IP++);
}
CodeEntry* X;
if (KeepResult) {
- /* pha */
- X = NewCodeEntry (OP65_PHA, AM65_IMP, 0, 0, D->OpEntry->LI);
- InsertEntry (D, X, D->IP++);
+ /* pha */
+ X = NewCodeEntry (OP65_PHA, AM65_IMP, 0, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
}
/* txa */
/* opc xxx */
CodeEntry* LoadX = LI->X.LoadEntry;
- X = NewCodeEntry (OPC, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
+ X = NewCodeEntry (OPC, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
InsertEntry (D, X, D->IP++);
} else {
}
if (KeepResult) {
- /* tax */
- X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, D->OpEntry->LI);
- InsertEntry (D, X, D->IP++);
+ /* tax */
+ X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
- /* pla */
- X = NewCodeEntry (OP65_PLA, AM65_IMP, 0, 0, D->OpEntry->LI);
- InsertEntry (D, X, D->IP++);
+ /* pla */
+ X = NewCodeEntry (OP65_PLA, AM65_IMP, 0, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
}
}
/*****************************************************************************/
-/* Actual optimization functions */
+/* Actual optimization functions */
/*****************************************************************************/
D->IP = D->OpIndex+1;
- /* Add operand for low byte */
- AddOpLow (D, OP65_CMP, &D->Rhs);
+ /* Add operand for low byte */
+ AddOpLow (D, OP65_CMP, &D->Rhs);
/* bne L */
X = NewCodeEntry (OP65_BNE, AM65_BRA, L->Name, L, D->OpEntry->LI);
InsertEntry (D, X, D->IP++);
- /* Add operand for high byte */
- AddOpHigh (D, OP65_CMP, &D->Rhs, 0);
+ /* Add operand for high byte */
+ AddOpHigh (D, OP65_CMP, &D->Rhs, 0);
} else {
+static unsigned Opt_tosshift (StackOpData* D, const char* Name)
+/* Optimize shift sequences. */
+{
+ CodeEntry* X;
+
+ /* Store the value into the zeropage instead of pushing it */
+ ReplacePushByStore (D);
+
+ /* If the lhs is direct (but not stack relative), we can just reload the
+ * data later.
+ */
+ if ((D->Lhs.A.Flags & (LI_DIRECT | LI_RELOAD_Y)) == LI_DIRECT &&
+ (D->Lhs.X.Flags & (LI_DIRECT | LI_RELOAD_Y)) == LI_DIRECT) {
+
+ CodeEntry* LoadX = D->Lhs.X.LoadEntry;
+ CodeEntry* LoadA = D->Lhs.A.LoadEntry;
+
+ /* Inline the shift */
+ D->IP = D->OpIndex+1;
+
+ /* tay */
+ X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+
+ /* lda */
+ X = NewCodeEntry (OP65_LDA, LoadA->AM, LoadA->Arg, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+
+ /* ldx */
+ X = NewCodeEntry (OP65_LDX, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+
+ /* Lhs load entries can be removed */
+ D->Lhs.X.Flags |= LI_REMOVE;
+ D->Lhs.A.Flags |= LI_REMOVE;
+
+ } else {
+
+ /* Save lhs into zeropage and reload later */
+ AddStoreX (D);
+ AddStoreA (D);
+
+ /* Be sure to setup IP after adding the stores, otherwise it will get
+ * messed up.
+ */
+ D->IP = D->OpIndex+1;
+
+ /* tay */
+ X = NewCodeEntry (OP65_TAY, AM65_IMP, 0, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+
+ /* lda zp */
+ X = NewCodeEntry (OP65_LDA, AM65_ZP, D->ZPLo, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+
+ /* ldx zp+1 */
+ X = NewCodeEntry (OP65_LDX, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+
+ }
+
+ /* jsr shlaxy/aslaxy/whatever */
+ X = NewCodeEntry (OP65_JSR, AM65_ABS, Name, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+
+ /* Remove the push and the call to the shift function */
+ RemoveRemainders (D);
+
+ /* We changed the sequence */
+ return 1;
+}
+
+
+
static unsigned Opt___bzero (StackOpData* D)
/* Optimize the __bzero sequence */
{
/* Loop using the sign bit */
/* ldy #count-1 */
- Arg = MakeHexArg (D->OpEntry->RI->In.RegA - 1);
+ Arg = MakeHexArg (D->OpEntry->RI->In.RegA - 1);
X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
InsertEntry (D, X, D->OpIndex+2);
InsertEntry (D, X, D->OpIndex+4);
/* cpy #count */
- Arg = MakeHexArg (D->OpEntry->RI->In.RegA);
+ Arg = MakeHexArg (D->OpEntry->RI->In.RegA);
X = NewCodeEntry (OP65_CPY, AM65_IMM, Arg, 0, D->OpEntry->LI);
InsertEntry (D, X, D->OpIndex+5);
if (RegValIsKnown (D->OpEntry->RI->In.RegY)) {
/* Value of Y is known */
- const char* Arg = MakeHexArg (D->OpEntry->RI->In.RegY + 1);
- X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
+ const char* Arg = MakeHexArg (D->OpEntry->RI->In.RegY + 1);
+ X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
} else {
X = NewCodeEntry (OP65_INY, AM65_IMP, 0, 0, D->OpEntry->LI);
}
InsertEntry (D, X, D->OpIndex+2);
if (RegValIsKnown (D->OpEntry->RI->In.RegX)) {
- /* Value of X is known */
- const char* Arg = MakeHexArg (D->OpEntry->RI->In.RegX);
- X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, D->OpEntry->LI);
+ /* Value of X is known */
+ const char* Arg = MakeHexArg (D->OpEntry->RI->In.RegX);
+ X = NewCodeEntry (OP65_LDA, AM65_IMM, Arg, 0, D->OpEntry->LI);
} else {
- /* Value unknown */
- X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, D->OpEntry->LI);
+ /* Value unknown */
+ X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, D->OpEntry->LI);
}
InsertEntry (D, X, D->OpIndex+3);
X = NewCodeEntry (OP65_INX, AM65_IMP, 0, 0, D->OpEntry->LI);
InsertEntry (D, X, D->IP++);
- } else if (D->OpEntry->RI->In.RegX == 0 &&
- (RegValIsKnown (D->PushEntry->RI->In.RegX) ||
- (D->Lhs.X.Flags & LI_RELOAD_Y) == 0)) {
+ } else if (D->OpEntry->RI->In.RegX == 0 &&
+ (RegValIsKnown (D->PushEntry->RI->In.RegX) ||
+ (D->Lhs.X.Flags & LI_RELOAD_Y) == 0)) {
/* The high byte is that of the first operand plus carry */
CodeLabel* L;
X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, D->OpEntry->LI);
} else {
/* Value of first op high byte is unknown. Load from ZP or
- * original storage.
- */
- if (D->Lhs.X.Flags & LI_DIRECT) {
- CodeEntry* LoadX = D->Lhs.X.LoadEntry;
- X = NewCodeEntry (OP65_LDX, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
- } else {
+ * original storage.
+ */
+ if (D->Lhs.X.Flags & LI_DIRECT) {
+ CodeEntry* LoadX = D->Lhs.X.LoadEntry;
+ X = NewCodeEntry (OP65_LDX, LoadX->AM, LoadX->Arg, 0, D->OpEntry->LI);
+ } else {
X = NewCodeEntry (OP65_LDX, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI);
- }
+ }
}
InsertEntry (D, X, D->IP++);
+static unsigned Opt_tosaslax (StackOpData* D)
+/* Optimize the tosaslax sequence */
+{
+ return Opt_tosshift (D, "aslaxy");
+}
+
+
+
+static unsigned Opt_tosasrax (StackOpData* D)
+/* Optimize the tosasrax sequence */
+{
+ return Opt_tosshift (D, "asraxy");
+}
+
+
+
static unsigned Opt_toseqax (StackOpData* D)
/* Optimize the toseqax sequence */
{
CodeLabel* L;
- /* Inline the sbc */
+ /* Inline the compare */
D->IP = D->OpIndex+1;
/* Must be true because of OP_RHS_LOAD */
+static unsigned Opt_tosshlax (StackOpData* D)
+/* Optimize the tosshlax sequence */
+{
+ return Opt_tosshift (D, "shlaxy");
+}
+
+
+
+static unsigned Opt_tosshrax (StackOpData* D)
+/* Optimize the tosshrax sequence */
+{
+ return Opt_tosshift (D, "shraxy");
+}
+
+
+
static unsigned Opt_tossubax (StackOpData* D)
/* Optimize the tossubax sequence. Note: subtraction is not commutative! */
{
/* High byte */
if (RegValIsKnown (D->PushEntry->RI->In.RegX) &&
RegValIsKnown (D->OpEntry->RI->In.RegX)) {
- /* Both values known, precalculate the result */
- const char* Arg = MakeHexArg (D->PushEntry->RI->In.RegX ^ D->OpEntry->RI->In.RegX);
- X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, D->OpEntry->LI);
- InsertEntry (D, X, D->IP++);
+ /* Both values known, precalculate the result */
+ const char* Arg = MakeHexArg (D->PushEntry->RI->In.RegX ^ D->OpEntry->RI->In.RegX);
+ X = NewCodeEntry (OP65_LDX, AM65_IMM, Arg, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
} else if (D->PushEntry->RI->In.RegX != 0) {
- /* High byte is unknown */
+ /* High byte is unknown */
AddOpHigh (D, OP65_EOR, &D->Lhs, 1);
}
/*****************************************************************************/
-/* Code */
+/* Code */
/*****************************************************************************/
{ "staxspidx", Opt_staxspidx, REG_AX, OP_NONE },
{ "tosaddax", Opt_tosaddax, REG_NONE, OP_NONE },
{ "tosandax", Opt_tosandax, REG_NONE, OP_NONE },
+ { "tosaslax", Opt_tosaslax, REG_NONE, OP_NONE },
+ { "tosasrax", Opt_tosasrax, REG_NONE, OP_NONE },
{ "toseqax", Opt_toseqax, REG_NONE, OP_NONE },
{ "tosgeax", Opt_tosgeax, REG_NONE, OP_RHS_LOAD_DIRECT },
{ "tosltax", Opt_tosltax, REG_NONE, OP_RHS_LOAD_DIRECT },
{ "tosneax", Opt_tosneax, REG_NONE, OP_NONE },
{ "tosorax", Opt_tosorax, REG_NONE, OP_NONE },
+ { "tosshlax", Opt_tosshlax, REG_NONE, OP_NONE },
+ { "tosshrax", Opt_tosshrax, REG_NONE, OP_NONE },
{ "tossubax", Opt_tossubax, REG_NONE, OP_RHS_LOAD_DIRECT },
{ "tosugeax", Opt_tosugeax, REG_NONE, OP_RHS_LOAD_DIRECT },
{ "tosugtax", Opt_tosugtax, REG_NONE, OP_RHS_LOAD_DIRECT },
static int CmpFunc (const void* Key, const void* Func)
/* Compare function for bsearch */
{
- return strcmp (Key, ((const OptFuncDesc*) Func)->Name);
+ return strcmp (Key, ((const OptFuncDesc*) Func)->Name);
}
"aslax2",
"aslax3",
"aslax4",
+ "aslaxy",
"asrax1",
"asrax2",
"asrax3",
"asrax4",
+ "asraxy",
"bnegax",
+ "complax",
+ "decax1",
+ "decax2",
+ "decax3",
+ "decax4",
+ "decax5",
+ "decax6",
+ "decax7",
+ "decax8",
+ "decaxy",
+ "incax1",
+ "incax2",
+ "incax3",
+ "incax4",
+ "incax5",
+ "incax6",
+ "incax7",
+ "incax8",
+ "incaxy",
"ldaxidx",
"ldaxysp",
"negax",
"shlax2",
"shlax3",
"shlax4",
+ "shlaxy",
"shrax1",
"shrax2",
"shrax3",
"shrax4",
+ "shraxy",
};
void* R = bsearch (Name,
/*****************************************************************************/
-/* Code */
+/* Code */
/*****************************************************************************/
{
unsigned Changes = 0; /* Number of changes in one run */
StackOpData Data;
- unsigned I;
+ int I;
+ int OldEntryCount; /* Old number of entries */
+ unsigned UsedRegs = 0; /* Registers used */
+ unsigned ChangedRegs = 0;/* Registers changed */
+
enum {
Initialize,
} State = Initialize;
- /* Generate register info */
- CS_GenRegInfo (S);
-
/* Remember the code segment in the info struct */
Data.Code = S;
* intermediate code for zero page use.
*/
I = 0;
- while (I < CS_GetEntryCount (S)) {
+ while (I < (int)CS_GetEntryCount (S)) {
- /* Get the next entry */
- CodeEntry* E = CS_GetEntry (S, I);
+ /* Get the next entry */
+ CodeEntry* E = CS_GetEntry (S, I);
/* Actions depend on state */
switch (State) {
case Initialize:
ResetStackOpData (&Data);
+ UsedRegs = ChangedRegs = REG_NONE;
State = Search;
/* FALLTHROUGH */
Data.UsedRegs |= (E->Use | E->Chg);
TrackLoads (&Data.Rhs, E, I);
}
+ /* If the registers from the push (A/X) are used before they're
+ * changed, we cannot change the sequence, because this would
+ * with a high probability change the register contents.
+ */
+ UsedRegs |= E->Use;
+ if ((UsedRegs & ~ChangedRegs) & REG_AX) {
+ I = Data.PushIndex;
+ State = Initialize;
+ break;
+ }
+ ChangedRegs |= E->Chg;
break;
case FoundOp:
Data.OpEntry = CS_GetEntry (S, Data.OpIndex);
Data.NextEntry = CS_GetNextEntry (S, Data.OpIndex);
+ /* Remember the current number of code lines */
+ OldEntryCount = CS_GetEntryCount (S);
+
/* Adjust stack offsets to account for the upcoming removal */
AdjustStackOffset (&Data, 2);
/* Call the optimizer function */
Changes += Data.OptFunc->Func (&Data);
+ /* Since the function may have added or deleted entries,
+ * correct the index.
+ */
+ I += CS_GetEntryCount (S) - OldEntryCount;
+
/* Regenerate register info */
CS_GenRegInfo (S);
/* Done */
State = Initialize;
- break;
+ continue;
- }
+ }
- /* Next entry */
- ++I;
+ /* Next entry */
+ ++I;
}
- /* Free the register info */
- CS_FreeRegInfo (S);
-
/* Return the number of changes made */
return Changes;
}