+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;
+ }
+}
+
+
+