+static int IsSpLoad (const CodeEntry* E)
+/* Return true if this is the load of A from the stack */
+{
+ return E->OPC == OPC_LDA && E->AM == AM_ZP_INDY && strcmp (E->Arg, "sp") == 0;
+}
+
+
+
+static int IsLocalLoad16 (CodeSeg* S, unsigned Index,
+ CodeEntry** L, unsigned Count)
+/* Check if a 16 bit load of a local variable follows:
+ *
+ * ldy #$xx
+ * lda (sp),y
+ * tax
+ * dey
+ * lda (sp),y
+ *
+ * If so, read Count entries following the first ldy into L and return true
+ * if this is possible. Otherwise return false.
+ */
+{
+ /* Be sure we read enough entries for the check */
+ CHECK (Count >= 5);
+
+ /* Read the first entry */
+ L[0] = GetCodeEntry (S, Index);
+
+ /* Check for the sequence */
+ return (L[0]->OPC == OPC_LDY &&
+ L[0]->AM == AM_IMM &&
+ (L[0]->Flags & CEF_NUMARG) != 0 &&
+ GetCodeEntries (S, L+1, Index+1, Count-1) &&
+ IsSpLoad (L[1]) &&
+ !CodeEntryHasLabel (L[1]) &&
+ L[2]->OPC == OPC_TAX &&
+ !CodeEntryHasLabel (L[2]) &&
+ L[3]->OPC == OPC_DEY &&
+ !CodeEntryHasLabel (L[3]) &&
+ IsSpLoad (L[4]) &&
+ !CodeEntryHasLabel (L[4]));
+}
+
+
+
+static int IsImmCmp16 (CodeSeg* S, CodeEntry** L)
+/* Check if the instructions at L are an immidiate compare of a/x:
+ *
+ *
+ */
+{
+ return (L[0]->OPC == OPC_CPX &&
+ L[0]->AM == AM_IMM &&
+ (L[0]->Flags & CEF_NUMARG) != 0 &&
+ !CodeEntryHasLabel (L[0]) &&
+ (L[1]->OPC == OPC_JNE || L[1]->OPC == OPC_BNE) &&
+ L[1]->JumpTo != 0 &&
+ !CodeEntryHasLabel (L[1]) &&
+ L[2]->OPC == OPC_CMP &&
+ L[2]->AM == AM_IMM &&
+ (L[2]->Flags & CEF_NUMARG) != 0 &&
+ (L[3]->Info & OF_ZBRA) != 0 &&
+ L[3]->JumpTo != 0 &&
+ (L[1]->JumpTo->Owner == L[3] || L[1]->JumpTo == L[3]->JumpTo));
+}
+
+
+