- CodeEntry* X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
- InsertEntry (D, X, I+1);
+ if (Correction == 2 && (N = CS_GetNextEntry(D->Code, I)) != 0 &&
+ ((N->Info & OF_ZBRA) != 0) && N->JumpTo != 0) {
+ /* The Y register is used but the load instruction loads A
+ ** and is followed by a branch that evaluates the zero flag.
+ ** This means that we cannot just insert the load insn
+ ** for the Y register at this place, because it would
+ ** destroy the Z flag. Instead place load insns at the
+ ** target of the branch and after it.
+ ** Note: There is a chance that this code won't work. The
+ ** jump may be a backwards jump (in which case the stack
+ ** offset has already been adjusted) or there may be other
+ ** instructions between the load and the conditional jump.
+ ** Currently the compiler does not generate such code, but
+ ** it is possible to force the optimizer into something
+ ** invalid by use of inline assembler.
+ */
+
+ /* Add load insn after the branch */
+ CodeEntry* X = NewCodeEntry (OP65_LDY, AM65_IMM, Arg, 0, E->LI);
+ InsertEntry (D, X, I+2);