+/* Data */
+/*****************************************************************************/
+
+
+
+/* Structure that holds the needed data */
+typedef struct StackOpData StackOpData;
+struct StackOpData {
+ CodeSeg* Code; /* Pointer to code segment */
+ unsigned Flags; /* Flags to remember things */
+ unsigned PushIndex; /* Index of call to pushax in codeseg */
+ unsigned OpIndex; /* Index of actual operation */
+ CodeEntry* PrevEntry; /* Entry before the call to pushax */
+ CodeEntry* PushEntry; /* Pointer to entry with call to pushax */
+ CodeEntry* OpEntry; /* Pointer to entry with op */
+ CodeEntry* NextEntry; /* Entry after the op */
+ const char* ZPLo; /* Lo byte of zero page loc to use */
+ const char* ZPHi; /* Hi byte of zero page loc to use */
+ unsigned IP; /* Insertion point used by some routines */
+};
+
+/* Flags returned by DirectOp */
+#define OP_DIRECT 0x01 /* Direct op may be used */
+#define OP_RELOAD_Y 0x02 /* Must reload index register Y */
+
+
+
+/*****************************************************************************/
+/* Helpers */
+/*****************************************************************************/
+
+
+
+static unsigned AdjustStackOffset (CodeSeg* S, unsigned Start, unsigned Stop,
+ unsigned Offs)
+/* Adjust the offset for all stack accesses in the range Start to Stop, both
+ * inclusive. The function returns the number of instructions that have been
+ * inserted.
+ */
+{
+ /* Number of inserted instructions */
+ unsigned Inserted = 0;
+
+ /* Walk over all entries */
+ unsigned I = Start;
+ while (I <= Stop) {
+
+ CodeEntry* E = CS_GetEntry (S, I);
+
+ int NeedCorrection = 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);
+
+ /* We need to correct this one */
+ NeedCorrection = 1;
+
+ } else if (CE_IsCallTo (E, "ldaxysp")) {
+
+ /* We need to correct this one */
+ NeedCorrection = 1;
+
+ }
+
+ if (NeedCorrection) {
+
+ CodeEntry* P;
+
+ /* Get the code entry before this one. If it's a LDY, adjust the
+ * value.
+ */
+ P = CS_GetPrevEntry (S, I);
+ if (P && P->OPC == OP65_LDY && CE_KnownImm (P)) {
+
+ /* The Y load is just before the stack access, adjust it */
+ CE_SetNumArg (P, P->Num - Offs);
+
+ } 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);
+ CS_InsertEntry (S, X, I);
+
+ /* One more inserted entries */
+ ++Inserted;
+ ++Stop;
+
+ /* Be sure to skip the stack access for the next round */
+ ++I;
+
+ }
+
+ }
+
+ /* Next entry */
+ ++I;
+ }
+
+ /* Return the number of inserted entries */
+ return Inserted;
+}
+
+
+
+static void InsertEntry (StackOpData* D, CodeEntry* E, unsigned Index)
+/* Insert a new entry. Depending on Index, D->PushIndex and D->OpIndex will
+ * be adjusted by this function.
+ */
+{
+ /* Insert the entry into the code segment */
+ CS_InsertEntry (D->Code, E, Index);
+
+ /* Adjust the indices if necessary */
+ if (D->PushEntry && Index <= D->PushIndex) {
+ ++D->PushIndex;
+ }
+ if (D->OpEntry && Index <= D->OpIndex) {
+ ++D->OpIndex;
+ }
+}
+
+
+
+static void DelEntry (StackOpData* D, unsigned Index)
+/* Delete an entry. Depending on Index, D->PushIndex and D->OpIndex will be
+ * adjusted by this function, and PushEntry/OpEntry may get invalidated.
+ */
+{
+ /* Delete the entry from the code segment */
+ CS_DelEntry (D->Code, Index);
+
+ /* Adjust the indices if necessary */
+ if (Index < D->PushIndex) {
+ --D->PushIndex;
+ } else if (Index == D->PushIndex) {
+ D->PushEntry = 0;
+ }
+ if (Index < D->OpIndex) {
+ --D->OpIndex;
+ } else if (Index == D->OpIndex) {
+ D->OpEntry = 0;
+ }
+}
+
+
+
+static void CheckDirectOp (StackOpData* D)
+/* Check if the given entry is a lda instruction with an addressing mode
+ * that allows us to replace it by another operation (like ora). If so, we may
+ * use this location for the or and must not save the value in the zero
+ * page location.
+ */
+{
+ /* We need the entry before the push */
+ CodeEntry* E;
+ CHECK ((E = D->PrevEntry) != 0);
+
+ if (E->OPC == OP65_LDA) {
+ if (E->AM == AM65_IMM || E->AM == AM65_ZP || E->AM == AM65_ABS) {
+ /* These insns are all ok and replaceable */
+ D->Flags |= OP_DIRECT;
+ } else if (E->AM == AM65_ZP_INDY && RegValIsKnown (E->RI->In.RegY) &&
+ strcmp (E->Arg, "sp") == 0) {
+ /* A load from the stack with known offset is also ok, but in this
+ * case we must reload the index register later. Please note that
+ * a load indirect via other zero page locations is not ok, since
+ * these locations may change between the push and the actual
+ * operation.
+ */
+ D->Flags |= (OP_DIRECT | OP_RELOAD_Y);
+ }
+ }
+}
+
+
+
+static void ReplacePushByStore (StackOpData* D)
+/* Replace the call to the push subroutine by a store into the zero page
+ * location (actually, the push is not replaced, because we need it for
+ * later, but the name is still ok since the push will get removed at the
+ * end of each routine).
+ */
+{
+ CodeEntry* X;
+
+ /* Store the value into the zeropage instead of pushing it */
+ X = NewCodeEntry (OP65_STX, AM65_ZP, D->ZPHi, 0, D->PushEntry->LI);
+ InsertEntry (D, X, D->PushIndex+1);
+ if ((D->Flags & OP_DIRECT) == 0) {
+ X = NewCodeEntry (OP65_STA, AM65_ZP, D->ZPLo, 0, D->PushEntry->LI);
+ InsertEntry (D, X, D->PushIndex+1);
+ }
+}
+
+
+
+static void AddOpLow (StackOpData* D, opc_t OPC)
+/* Add an op for the low byte of an operator. This function honours the
+ * OP_DIRECT and OP_RELOAD_Y flags and generates the necessary instructions.
+ * All code is inserted at the current insertion point.
+ */
+{
+ CodeEntry* X;
+
+ if ((D->Flags & OP_DIRECT) != 0) {
+ /* Op with a variable location. If the location is on the stack, we
+ * need to reload the Y register.
+ */
+ if ((D->Flags & OP_RELOAD_Y) != 0) {
+ const char* Arg = MakeHexArg (D->PrevEntry->RI->In.RegY);
+ X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+ }
+ X = NewCodeEntry (OPC, D->PrevEntry->AM, D->PrevEntry->Arg, 0, D->OpEntry->LI);
+ } else {
+ /* Op with temp storage */
+ X = NewCodeEntry (OPC, AM65_ZP, D->ZPLo, 0, D->OpEntry->LI);
+ }
+ InsertEntry (D, X, D->IP++);
+}
+
+
+
+static void AddOpHigh (StackOpData* D, opc_t OPC)
+/* Add an op for the high byte of an operator. Special cases (constant values
+ * or similar have to be checked separately, the function covers only the
+ * generic case. Code is inserted at the insertion point.
+ */
+{
+ CodeEntry* X;
+
+ /* High byte is unknown */
+ X = NewCodeEntry (OP65_STA, AM65_ZP, D->ZPLo, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+ X = NewCodeEntry (OP65_TXA, AM65_IMP, 0, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+ X = NewCodeEntry (OPC, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+ X = NewCodeEntry (OP65_TAX, AM65_IMP, 0, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+ X = NewCodeEntry (OP65_LDA, AM65_ZP, D->ZPLo, 0, D->OpEntry->LI);
+ InsertEntry (D, X, D->IP++);
+}
+
+
+
+static void RemovePushAndOp (StackOpData* D)
+/* Remove the call to pushax and the call to the operator subroutine */
+{
+ DelEntry (D, D->OpIndex);
+ DelEntry (D, D->PushIndex);
+}
+
+
+
+static const char* IsRegVar (const StackOpData* D)
+/* If the value pushed is that of a register variable, return the name of the
+ * entry in the register bank. Otherwise return NULL.
+ */
+{
+ CodeEntry* P;
+
+ if (D->PushIndex >= 2 &&
+ (P = D->PrevEntry) != 0 &&
+ P->OPC == OP65_LDX &&
+ P->AM == AM65_ZP &&
+ strncmp (P->Arg, "regbank+", 7) == 0 &&
+ isdigit (P->Arg[8]) &&
+ (P = CS_GetEntry (D->Code, D->PushIndex-2)) != 0 &&
+ P->OPC == OP65_LDA &&
+ P->AM == AM65_ZP &&
+ strncmp (P->Arg, "regbank+", 7) == 0 &&
+ isdigit (P->Arg[8])) {
+ /* Ok, it loads the register variable */
+ return P->Arg;
+ } else {
+ return 0;
+ }
+}
+
+
+
+/*****************************************************************************/
+/* Actual optimization functions */