/*****************************************************************************/
/* */
-/* coptind.c */
+/* coptind.c */
/* */
-/* Environment independent low level optimizations */
+/* Environment independent low level optimizations */
/* */
/* */
/* */
/*****************************************************************************/
-/* Helper functions */
+/* Helper functions */
/*****************************************************************************/
/* Walk over all code entries */
while (From <= To) {
- /* Get the next entry */
- CodeEntry* E = CS_GetEntry (S, From);
+ /* Get the next entry */
+ CodeEntry* E = CS_GetEntry (S, From);
/* Check if there is an argument and if this argument equals Arg in
* some variants.
/* Determine the branch distance */
int Distance = 0;
if (TI >= From) {
- /* Forward branch, do not count the current insn */
- unsigned J = From+1;
- while (J < TI) {
- CodeEntry* N = CS_GetEntry (S, J++);
- Distance += N->Size;
- }
+ /* Forward branch, do not count the current insn */
+ unsigned J = From+1;
+ while (J < TI) {
+ CodeEntry* N = CS_GetEntry (S, J++);
+ Distance += N->Size;
+ }
} else {
- /* Backward branch */
- unsigned J = TI;
- while (J < From) {
- CodeEntry* N = CS_GetEntry (S, J++);
- Distance -= N->Size;
- }
+ /* Backward branch */
+ unsigned J = TI;
+ while (J < From) {
+ CodeEntry* N = CS_GetEntry (S, J++);
+ Distance -= N->Size;
+ }
}
/* Return the calculated distance */
/* Return the contents of the given zeropage register */
{
if ((Use & REG_TMP1) != 0) {
- return RC->Tmp1;
+ return RC->Tmp1;
} else if ((Use & REG_PTR1_LO) != 0) {
- return RC->Ptr1Lo;
+ return RC->Ptr1Lo;
} else if ((Use & REG_PTR1_HI) != 0) {
- return RC->Ptr1Hi;
+ return RC->Ptr1Hi;
} else if ((Use & REG_SREG_LO) != 0) {
- return RC->SRegLo;
+ return RC->SRegLo;
} else if ((Use & REG_SREG_HI) != 0) {
- return RC->SRegHi;
+ return RC->SRegHi;
} else {
- return UNKNOWN_REGVAL;
+ return UNKNOWN_REGVAL;
}
}
/* Return the contents of the given register */
{
if ((Use & REG_A) != 0) {
- return RC->RegA;
+ return RC->RegA;
} else if ((Use & REG_X) != 0) {
- return RC->RegX;
+ return RC->RegX;
} else if ((Use & REG_Y) != 0) {
- return RC->RegY;
+ return RC->RegY;
} else {
return ZPRegVal (Use, RC);
}
/*****************************************************************************/
-/* Replace jumps to RTS by RTS */
+/* Replace jumps to RTS by RTS */
/*****************************************************************************/
unsigned I = 0;
while (I < CS_GetEntryCount (S)) {
- /* Get the next entry */
- CodeEntry* E = CS_GetEntry (S, I);
+ /* Get the next entry */
+ CodeEntry* E = CS_GetEntry (S, I);
- /* Check if it's an unconditional branch to a local target */
- if ((E->Info & OF_UBRA) != 0 &&
- E->JumpTo != 0 &&
- E->JumpTo->Owner->OPC == OP65_RTS) {
+ /* Check if it's an unconditional branch to a local target */
+ if ((E->Info & OF_UBRA) != 0 &&
+ E->JumpTo != 0 &&
+ E->JumpTo->Owner->OPC == OP65_RTS) {
- /* Insert an RTS instruction */
- CodeEntry* X = NewCodeEntry (OP65_RTS, AM65_IMP, 0, 0, E->LI);
- CS_InsertEntry (S, X, I+1);
+ /* Insert an RTS instruction */
+ CodeEntry* X = NewCodeEntry (OP65_RTS, AM65_IMP, 0, 0, E->LI);
+ CS_InsertEntry (S, X, I+1);
- /* Delete the jump */
- CS_DelEntry (S, I);
+ /* Delete the jump */
+ CS_DelEntry (S, I);
- /* Remember, we had changes */
- ++Changes;
+ /* Remember, we had changes */
+ ++Changes;
- }
+ }
- /* Next entry */
- ++I;
+ /* Next entry */
+ ++I;
}
unsigned I = 0;
while (I < CS_GetEntryCount (S) - 1) {
- /* Get the next entry */
- CodeEntry* E = CS_GetEntry (S, I);
+ /* Get the next entry */
+ CodeEntry* E = CS_GetEntry (S, I);
- /* Check if it's an conditional branch to a local target */
- if ((E->Info & OF_CBRA) != 0 && /* Conditional branch */
- (E->Info & OF_LBRA) != 0 && /* Long branch */
- E->JumpTo != 0) { /* Local label */
+ /* Check if it's an conditional branch to a local target */
+ if ((E->Info & OF_CBRA) != 0 && /* Conditional branch */
+ (E->Info & OF_LBRA) != 0 && /* Long branch */
+ E->JumpTo != 0) { /* Local label */
/* Get the jump target and the next entry. There's always a next
++Changes;
}
- }
+ }
- /* Next entry */
- ++I;
+ /* Next entry */
+ ++I;
}
/*****************************************************************************/
-/* Remove dead jumps */
+/* Remove dead jumps */
/*****************************************************************************/
unsigned I = 0;
while (I < CS_GetEntryCount (S)) {
- /* Get the next entry */
- CodeEntry* E = CS_GetEntry (S, I);
+ /* Get the next entry */
+ CodeEntry* E = CS_GetEntry (S, I);
- /* Check if it's a branch, if it has a local target, and if the target
- * is the next instruction.
- */
- if (E->AM == AM65_BRA &&
- E->JumpTo &&
- E->JumpTo->Owner == CS_GetNextEntry (S, I)) {
+ /* Check if it's a branch, if it has a local target, and if the target
+ * is the next instruction.
+ */
+ if (E->AM == AM65_BRA &&
+ E->JumpTo &&
+ E->JumpTo->Owner == CS_GetNextEntry (S, I)) {
- /* Delete the dead jump */
- CS_DelEntry (S, I);
+ /* Delete the dead jump */
+ CS_DelEntry (S, I);
- /* Remember, we had changes */
- ++Changes;
+ /* Remember, we had changes */
+ ++Changes;
- } else {
+ } else {
- /* Next entry */
- ++I;
+ /* Next entry */
+ ++I;
- }
+ }
}
/* Return the number of changes made */
/*****************************************************************************/
-/* Remove dead code */
+/* Remove dead code */
/*****************************************************************************/
unsigned I = 0;
while (I < CS_GetEntryCount (S)) {
- CodeEntry* N;
+ CodeEntry* N;
CodeLabel* LN;
- /* Get this entry */
- CodeEntry* E = CS_GetEntry (S, I);
+ /* Get this entry */
+ CodeEntry* E = CS_GetEntry (S, I);
- /* Check if it's an unconditional branch, and if the next entry has
- * no labels attached, or if the label is just used so that the insn
+ /* Check if it's an unconditional branch, and if the next entry has
+ * no labels attached, or if the label is just used so that the insn
* can jump to itself.
- */
- if ((E->Info & OF_DEAD) != 0 && /* Dead code follows */
- (N = CS_GetNextEntry (S, I)) != 0 && /* Has next entry */
- (!CE_HasLabel (N) || /* Don't has a label */
+ */
+ if ((E->Info & OF_DEAD) != 0 && /* Dead code follows */
+ (N = CS_GetNextEntry (S, I)) != 0 && /* Has next entry */
+ (!CE_HasLabel (N) || /* Don't has a label */
((N->Info & OF_UBRA) != 0 && /* Uncond branch */
(LN = N->JumpTo) != 0 && /* Jumps to known label */
LN->Owner == N && /* Attached to insn */
CL_GetRefCount (LN) == 1))) { /* Only reference */
- /* Delete the next entry */
- CS_DelEntry (S, I+1);
+ /* Delete the next entry */
+ CS_DelEntry (S, I+1);
- /* Remember, we had changes */
- ++Changes;
+ /* Remember, we had changes */
+ ++Changes;
- } else {
+ } else {
- /* Next entry */
- ++I;
+ /* Next entry */
+ ++I;
- }
+ }
}
/* Return the number of changes made */
/*****************************************************************************/
-/* Optimize jump cascades */
+/* Optimize jump cascades */
/*****************************************************************************/
unsigned I = 0;
while (I < CS_GetEntryCount (S)) {
- CodeEntry* N;
- CodeLabel* OldLabel;
+ CodeEntry* N;
+ CodeLabel* OldLabel;
- /* Get this entry */
- CodeEntry* E = CS_GetEntry (S, I);
+ /* Get this entry */
+ CodeEntry* E = CS_GetEntry (S, I);
- /* Check:
+ /* Check:
* - if it's a branch,
* - if it has a jump label,
* - if this jump label is not attached to the instruction itself,
* outside of the function (usually incspx), which won't simplify the
* code, since conditional far branches are emulated by a short branch
* around a jump.
- */
- if ((E->Info & OF_BRA) != 0 &&
- (OldLabel = E->JumpTo) != 0 &&
- (N = OldLabel->Owner) != E &&
- (N->Info & OF_BRA) != 0 &&
+ */
+ if ((E->Info & OF_BRA) != 0 &&
+ (OldLabel = E->JumpTo) != 0 &&
+ (N = OldLabel->Owner) != E &&
+ (N->Info & OF_BRA) != 0 &&
((E->Info & OF_CBRA) == 0 ||
N->JumpTo != 0)) {
- /* Check if we can use the final target label. This is the case,
- * if the target branch is an absolut branch, or if it is a
- * conditional branch checking the same condition as the first one.
- */
- if ((N->Info & OF_UBRA) != 0 ||
- ((E->Info & OF_CBRA) != 0 &&
- GetBranchCond (E->OPC) == GetBranchCond (N->OPC))) {
+ /* Check if we can use the final target label. This is the case,
+ * if the target branch is an absolut branch, or if it is a
+ * conditional branch checking the same condition as the first one.
+ */
+ if ((N->Info & OF_UBRA) != 0 ||
+ ((E->Info & OF_CBRA) != 0 &&
+ GetBranchCond (E->OPC) == GetBranchCond (N->OPC))) {
- /* This is a jump cascade and we may jump to the final target,
+ /* This is a jump cascade and we may jump to the final target,
* provided that the other insn does not jump to itself. If
* this is the case, we can also jump to ourselves, otherwise
* insert a jump to the new instruction and remove the old one.
- */
- CodeEntry* X;
+ */
+ CodeEntry* X;
CodeLabel* LN = N->JumpTo;
if (LN != 0 && LN->Owner == N) {
}
- /* Insert it behind E */
- CS_InsertEntry (S, X, I+1);
+ /* Insert it behind E */
+ CS_InsertEntry (S, X, I+1);
- /* Remove E */
- CS_DelEntry (S, I);
+ /* Remove E */
+ CS_DelEntry (S, I);
- /* Remember, we had changes */
- ++Changes;
+ /* Remember, we had changes */
+ ++Changes;
- /* Check if both are conditional branches, and the condition of
- * the second is the inverse of that of the first. In this case,
- * the second branch will never be taken, and we may jump directly
- * to the instruction behind this one.
- */
- } else if ((E->Info & OF_CBRA) != 0 && (N->Info & OF_CBRA) != 0) {
+ /* Check if both are conditional branches, and the condition of
+ * the second is the inverse of that of the first. In this case,
+ * the second branch will never be taken, and we may jump directly
+ * to the instruction behind this one.
+ */
+ } else if ((E->Info & OF_CBRA) != 0 && (N->Info & OF_CBRA) != 0) {
- CodeEntry* X; /* Instruction behind N */
- CodeLabel* LX; /* Label attached to X */
+ CodeEntry* X; /* Instruction behind N */
+ CodeLabel* LX; /* Label attached to X */
- /* Get the branch conditions of both branches */
- bc_t BC1 = GetBranchCond (E->OPC);
- bc_t BC2 = GetBranchCond (N->OPC);
+ /* Get the branch conditions of both branches */
+ bc_t BC1 = GetBranchCond (E->OPC);
+ bc_t BC2 = GetBranchCond (N->OPC);
- /* Check the branch conditions */
- if (BC1 != GetInverseCond (BC2)) {
- /* Condition not met */
- goto NextEntry;
- }
+ /* Check the branch conditions */
+ if (BC1 != GetInverseCond (BC2)) {
+ /* Condition not met */
+ goto NextEntry;
+ }
- /* We may jump behind this conditional branch. Get the
- * pointer to the next instruction
- */
- if ((X = CS_GetNextEntry (S, CS_GetEntryIndex (S, N))) == 0) {
- /* N is the last entry, bail out */
- goto NextEntry;
- }
+ /* We may jump behind this conditional branch. Get the
+ * pointer to the next instruction
+ */
+ if ((X = CS_GetNextEntry (S, CS_GetEntryIndex (S, N))) == 0) {
+ /* N is the last entry, bail out */
+ goto NextEntry;
+ }
- /* Get the label attached to X, create a new one if needed */
- LX = CS_GenLabel (S, X);
+ /* Get the label attached to X, create a new one if needed */
+ LX = CS_GenLabel (S, X);
- /* Move the reference from E to the new label */
- CS_MoveLabelRef (S, E, LX);
+ /* Move the reference from E to the new label */
+ CS_MoveLabelRef (S, E, LX);
- /* Remember, we had changes */
- ++Changes;
- }
- }
+ /* Remember, we had changes */
+ ++Changes;
+ }
+ }
NextEntry:
- /* Next entry */
- ++I;
+ /* Next entry */
+ ++I;
}
/*****************************************************************************/
-/* Optimize jsr/rts */
+/* Optimize jsr/rts */
/*****************************************************************************/
unsigned I = 0;
while (I < CS_GetEntryCount (S)) {
- CodeEntry* N;
+ CodeEntry* N;
- /* Get this entry */
- CodeEntry* E = CS_GetEntry (S, I);
+ /* Get this entry */
+ CodeEntry* E = CS_GetEntry (S, I);
- /* Check if it's a subroutine call and if the following insn is RTS */
- if (E->OPC == OP65_JSR &&
- (N = CS_GetNextEntry (S, I)) != 0 &&
- N->OPC == OP65_RTS) {
+ /* Check if it's a subroutine call and if the following insn is RTS */
+ if (E->OPC == OP65_JSR &&
+ (N = CS_GetNextEntry (S, I)) != 0 &&
+ N->OPC == OP65_RTS) {
- /* Change the jsr to a jmp and use the additional info for a jump */
- E->AM = AM65_BRA;
- CE_ReplaceOPC (E, OP65_JMP);
+ /* Change the jsr to a jmp and use the additional info for a jump */
+ E->AM = AM65_BRA;
+ CE_ReplaceOPC (E, OP65_JMP);
- /* Remember, we had changes */
- ++Changes;
+ /* Remember, we had changes */
+ ++Changes;
- }
+ }
- /* Next entry */
- ++I;
+ /* Next entry */
+ ++I;
}
/*****************************************************************************/
-/* Optimize jump targets */
+/* Optimize jump targets */
/*****************************************************************************/
*/
{
unsigned Changes = 0;
- CodeEntry* E1; /* Entry 1 */
- CodeEntry* E2; /* Entry 2 */
- CodeEntry* T1; /* Jump target entry 1 */
- CodeLabel* TL1; /* Target label 1 */
+ CodeEntry* E1; /* Entry 1 */
+ CodeEntry* E2; /* Entry 2 */
+ CodeEntry* T1; /* Jump target entry 1 */
+ CodeLabel* TL1; /* Target label 1 */
/* Walk over the entries */
unsigned I = 0;
while (I < CS_GetEntryCount (S)) {
- /* Get next entry */
- E2 = CS_GetNextEntry (S, I);
+ /* Get next entry */
+ E2 = CS_GetNextEntry (S, I);
- /* Check if we have a jump or branch without a label attached, and
+ /* Check if we have a jump or branch without a label attached, and
* a jump target, which is not attached to the jump itself
- */
- if (E2 != 0 &&
- (E2->Info & OF_UBRA) != 0 &&
+ */
+ if (E2 != 0 &&
+ (E2->Info & OF_UBRA) != 0 &&
!CE_HasLabel (E2) &&
- E2->JumpTo &&
- E2->JumpTo->Owner != E2) {
+ E2->JumpTo &&
+ E2->JumpTo->Owner != E2) {
- /* Get the entry preceeding the branch target */
- T1 = CS_GetPrevEntry (S, CS_GetEntryIndex (S, E2->JumpTo->Owner));
- if (T1 == 0) {
- /* There is no such entry */
- goto NextEntry;
- }
+ /* Get the entry preceeding the branch target */
+ T1 = CS_GetPrevEntry (S, CS_GetEntryIndex (S, E2->JumpTo->Owner));
+ if (T1 == 0) {
+ /* There is no such entry */
+ goto NextEntry;
+ }
/* The entry preceeding the branch target may not be the branch
* insn.
goto NextEntry;
}
- /* Get the entry preceeding the jump */
- E1 = CS_GetEntry (S, I);
+ /* Get the entry preceeding the jump */
+ E1 = CS_GetEntry (S, I);
- /* Check if both preceeding instructions are identical */
- if (!CodeEntriesAreEqual (E1, T1)) {
- /* Not equal, try next */
+ /* Check if both preceeding instructions are identical */
+ if (!CodeEntriesAreEqual (E1, T1)) {
+ /* Not equal, try next */
goto NextEntry;
- }
+ }
- /* Get the label for the instruction preceeding the jump target.
- * This routine will create a new label if the instruction does
- * not already have one.
- */
- TL1 = CS_GenLabel (S, T1);
+ /* Get the label for the instruction preceeding the jump target.
+ * This routine will create a new label if the instruction does
+ * not already have one.
+ */
+ TL1 = CS_GenLabel (S, T1);
- /* Change the jump target to point to this new label */
- CS_MoveLabelRef (S, E2, TL1);
+ /* Change the jump target to point to this new label */
+ CS_MoveLabelRef (S, E2, TL1);
- /* If the instruction preceeding the jump has labels attached,
- * move references to this label to the new label.
- */
- if (CE_HasLabel (E1)) {
- CS_MoveLabels (S, E1, T1);
- }
+ /* If the instruction preceeding the jump has labels attached,
+ * move references to this label to the new label.
+ */
+ if (CE_HasLabel (E1)) {
+ CS_MoveLabels (S, E1, T1);
+ }
- /* Remove the entry preceeding the jump */
- CS_DelEntry (S, I);
+ /* Remove the entry preceeding the jump */
+ CS_DelEntry (S, I);
- /* Remember, we had changes */
- ++Changes;
+ /* Remember, we had changes */
+ ++Changes;
- } else {
+ } else {
NextEntry:
/* Next entry */
++I;
/* New jump label */
CodeLabel* L;
- /* Get next entry */
- CodeEntry* E = CS_GetEntry (S, I);
+ /* Get next entry */
+ CodeEntry* E = CS_GetEntry (S, I);
/* Check if this is a bcc insn */
if (E->OPC == OP65_BCC || E->OPC == OP65_JCC) {
CodeEntry* N;
- /* Get next entry */
- CodeEntry* E = CS_GetEntry (S, I);
+ /* Get next entry */
+ CodeEntry* E = CS_GetEntry (S, I);
/* Check if this is a load insn with a label and the next insn is not
* a conditional branch that needs the flags from the load.
/*****************************************************************************/
-/* Optimize conditional branches */
+/* Optimize conditional branches */
/*****************************************************************************/
unsigned I = 0;
while (I < CS_GetEntryCount (S)) {
- CodeEntry* N;
- CodeLabel* L;
+ CodeEntry* N;
+ CodeLabel* L;
- /* Get next entry */
- CodeEntry* E = CS_GetEntry (S, I);
+ /* Get next entry */
+ CodeEntry* E = CS_GetEntry (S, I);
- /* Check if it's a register load */
- if ((E->Info & OF_LOAD) != 0 && /* It's a load instruction */
- E->AM == AM65_IMM && /* ..with immidiate addressing */
- (E->Flags & CEF_NUMARG) != 0 && /* ..and a numeric argument. */
- (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
- (N->Info & OF_CBRA) != 0 && /* ..which is a conditional branch */
- !CE_HasLabel (N)) { /* ..and does not have a label */
+ /* Check if it's a register load */
+ if ((E->Info & OF_LOAD) != 0 && /* It's a load instruction */
+ E->AM == AM65_IMM && /* ..with immidiate addressing */
+ (E->Flags & CEF_NUMARG) != 0 && /* ..and a numeric argument. */
+ (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
+ (N->Info & OF_CBRA) != 0 && /* ..which is a conditional branch */
+ !CE_HasLabel (N)) { /* ..and does not have a label */
- /* Get the branch condition */
- bc_t BC = GetBranchCond (N->OPC);
+ /* Get the branch condition */
+ bc_t BC = GetBranchCond (N->OPC);
- /* Check the argument against the branch condition */
- if ((BC == BC_EQ && E->Num != 0) ||
- (BC == BC_NE && E->Num == 0) ||
- (BC == BC_PL && (E->Num & 0x80) != 0) ||
- (BC == BC_MI && (E->Num & 0x80) == 0)) {
+ /* Check the argument against the branch condition */
+ if ((BC == BC_EQ && E->Num != 0) ||
+ (BC == BC_NE && E->Num == 0) ||
+ (BC == BC_PL && (E->Num & 0x80) != 0) ||
+ (BC == BC_MI && (E->Num & 0x80) == 0)) {
- /* Remove the conditional branch */
- CS_DelEntry (S, I+1);
+ /* Remove the conditional branch */
+ CS_DelEntry (S, I+1);
- /* Remember, we had changes */
- ++Changes;
+ /* Remember, we had changes */
+ ++Changes;
- } else if ((BC == BC_EQ && E->Num == 0) ||
- (BC == BC_NE && E->Num != 0) ||
- (BC == BC_PL && (E->Num & 0x80) == 0) ||
- (BC == BC_MI && (E->Num & 0x80) != 0)) {
+ } else if ((BC == BC_EQ && E->Num == 0) ||
+ (BC == BC_NE && E->Num != 0) ||
+ (BC == BC_PL && (E->Num & 0x80) == 0) ||
+ (BC == BC_MI && (E->Num & 0x80) != 0)) {
- /* The branch is always taken, replace it by a jump */
- CE_ReplaceOPC (N, OP65_JMP);
+ /* The branch is always taken, replace it by a jump */
+ CE_ReplaceOPC (N, OP65_JMP);
- /* Remember, we had changes */
- ++Changes;
- }
+ /* Remember, we had changes */
+ ++Changes;
+ }
- }
+ }
- if ((E->Info & OF_CBRA) != 0 && /* It's a conditional branch */
- (L = E->JumpTo) != 0 && /* ..referencing a local label */
- (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
- (N->Info & OF_UBRA) != 0 && /* ..which is an uncond branch, */
- !CE_HasLabel (N) && /* ..has no label attached */
- L->Owner == CS_GetNextEntry (S, I+1)) {/* ..and jump target follows */
+ if ((E->Info & OF_CBRA) != 0 && /* It's a conditional branch */
+ (L = E->JumpTo) != 0 && /* ..referencing a local label */
+ (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
+ (N->Info & OF_UBRA) != 0 && /* ..which is an uncond branch, */
+ !CE_HasLabel (N) && /* ..has no label attached */
+ L->Owner == CS_GetNextEntry (S, I+1)) {/* ..and jump target follows */
- /* Replace the jump by a conditional branch with the inverse branch
- * condition than the branch around it.
- */
- CE_ReplaceOPC (N, GetInverseBranch (E->OPC));
+ /* Replace the jump by a conditional branch with the inverse branch
+ * condition than the branch around it.
+ */
+ CE_ReplaceOPC (N, GetInverseBranch (E->OPC));
- /* Remove the conditional branch */
- CS_DelEntry (S, I);
+ /* Remove the conditional branch */
+ CS_DelEntry (S, I);
- /* Remember, we had changes */
- ++Changes;
+ /* Remember, we had changes */
+ ++Changes;
- }
+ }
- /* Next entry */
- ++I;
+ /* Next entry */
+ ++I;
}
I = 0;
while (I < CS_GetEntryCount (S)) {
- CodeEntry* N;
+ CodeEntry* N;
- /* Get next entry */
- CodeEntry* E = CS_GetEntry (S, I);
+ /* Get next entry */
+ CodeEntry* E = CS_GetEntry (S, I);
- /* Check if it's a rol insn with A in accu and a branch follows */
- if (E->OPC == OP65_ROL &&
+ /* Check if it's a rol insn with A in accu and a branch follows */
+ if (E->OPC == OP65_ROL &&
E->AM == AM65_ACC &&
E->RI->In.RegA == 0 &&
!CE_HasLabel (E) &&
(N->Info & OF_ZBRA) != 0 &&
!RegAUsed (S, I+1)) {
- /* Replace the branch condition */
- switch (GetBranchCond (N->OPC)) {
+ /* Replace the branch condition */
+ switch (GetBranchCond (N->OPC)) {
case BC_EQ: CE_ReplaceOPC (N, OP65_JCC); break;
case BC_NE: CE_ReplaceOPC (N, OP65_JCS); break;
default: Internal ("Unknown branch condition in OptCondBranches2");
/* Remember, we had changes */
++Changes;
- }
+ }
- /* Next entry */
- ++I;
+ /* Next entry */
+ ++I;
}
/* Return the number of changes made */
/*****************************************************************************/
-/* Remove unused loads and stores */
+/* Remove unused loads and stores */
/*****************************************************************************/
unsigned I = 0;
while (I < CS_GetEntryCount (S)) {
- CodeEntry* N;
-
- /* Get next entry */
- CodeEntry* E = CS_GetEntry (S, I);
-
- /* Check if it's a register load or transfer insn */
- if ((E->Info & (OF_LOAD | OF_XFR | OF_REG_INCDEC)) != 0 &&
- (N = CS_GetNextEntry (S, I)) != 0 &&
- !CE_UseLoadFlags (N)) {
-
- /* Check which sort of load or transfer it is */
- unsigned R;
- switch (E->OPC) {
- case OP65_DEA:
- case OP65_INA:
- case OP65_LDA:
- case OP65_TXA:
- case OP65_TYA: R = REG_A; break;
- case OP65_DEX:
- case OP65_INX:
- case OP65_LDX:
- case OP65_TAX: R = REG_X; break;
- case OP65_DEY:
- case OP65_INY:
- case OP65_LDY:
- case OP65_TAY: R = REG_Y; break;
- default: goto NextEntry; /* OOPS */
- }
-
- /* Get register usage and check if the register value is used later */
- if ((GetRegInfo (S, I+1, R) & R) == 0) {
-
- /* Register value is not used, remove the load */
- CS_DelEntry (S, I);
-
- /* Remember, we had changes. Account the deleted entry in I. */
- ++Changes;
- --I;
-
- }
- }
+ CodeEntry* N;
+
+ /* Get next entry */
+ CodeEntry* E = CS_GetEntry (S, I);
+
+ /* Check if it's a register load or transfer insn */
+ if ((E->Info & (OF_LOAD | OF_XFR | OF_REG_INCDEC)) != 0 &&
+ (N = CS_GetNextEntry (S, I)) != 0 &&
+ !CE_UseLoadFlags (N)) {
+
+ /* Check which sort of load or transfer it is */
+ unsigned R;
+ switch (E->OPC) {
+ case OP65_DEA:
+ case OP65_INA:
+ case OP65_LDA:
+ case OP65_TXA:
+ case OP65_TYA: R = REG_A; break;
+ case OP65_DEX:
+ case OP65_INX:
+ case OP65_LDX:
+ case OP65_TAX: R = REG_X; break;
+ case OP65_DEY:
+ case OP65_INY:
+ case OP65_LDY:
+ case OP65_TAY: R = REG_Y; break;
+ default: goto NextEntry; /* OOPS */
+ }
+
+ /* Get register usage and check if the register value is used later */
+ if ((GetRegInfo (S, I+1, R) & R) == 0) {
+
+ /* Register value is not used, remove the load */
+ CS_DelEntry (S, I);
+
+ /* Remember, we had changes. Account the deleted entry in I. */
+ ++Changes;
+ --I;
+
+ }
+ }
NextEntry:
- /* Next entry */
- ++I;
+ /* Next entry */
+ ++I;
}
unsigned I = 0;
while (I < CS_GetEntryCount (S)) {
- /* Get next entry */
- CodeEntry* E = CS_GetEntry (S, I);
+ /* Get next entry */
+ CodeEntry* E = CS_GetEntry (S, I);
- /* Check if it's a register load or transfer insn */
- if ((E->Info & OF_STORE) != 0 &&
- E->AM == AM65_ZP &&
- (E->Chg & REG_ZP) != 0) {
+ /* Check if it's a register load or transfer insn */
+ if ((E->Info & OF_STORE) != 0 &&
+ E->AM == AM65_ZP &&
+ (E->Chg & REG_ZP) != 0) {
- /* Check for the zero page location. We know that there cannot be
- * more than one zero page location involved in the store.
- */
- unsigned R = E->Chg & REG_ZP;
+ /* Check for the zero page location. We know that there cannot be
+ * more than one zero page location involved in the store.
+ */
+ unsigned R = E->Chg & REG_ZP;
- /* Get register usage and check if the register value is used later */
- if ((GetRegInfo (S, I+1, R) & R) == 0) {
+ /* Get register usage and check if the register value is used later */
+ if ((GetRegInfo (S, I+1, R) & R) == 0) {
- /* Register value is not used, remove the load */
- CS_DelEntry (S, I);
+ /* Register value is not used, remove the load */
+ CS_DelEntry (S, I);
- /* Remember, we had changes */
- ++Changes;
+ /* Remember, we had changes */
+ ++Changes;
/* Continue with next insn */
continue;
- }
- }
+ }
+ }
- /* Next entry */
- ++I;
+ /* Next entry */
+ ++I;
}
I = 0;
while (I < CS_GetEntryCount (S)) {
- CodeEntry* N;
-
- /* Get next entry */
- CodeEntry* E = CS_GetEntry (S, I);
-
- /* Assume we won't delete the entry */
- int Delete = 0;
-
- /* Get a pointer to the input registers of the insn */
- const RegContents* In = &E->RI->In;
-
- /* Handle the different instructions */
- switch (E->OPC) {
-
- case OP65_LDA:
- if (RegValIsKnown (In->RegA) && /* Value of A is known */
- CE_IsKnownImm (E, In->RegA) && /* Value to be loaded is known */
- (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
- !CE_UseLoadFlags (N)) { /* Which does not use the flags */
- Delete = 1;
- }
- break;
-
- case OP65_LDX:
- if (RegValIsKnown (In->RegX) && /* Value of X is known */
- CE_IsKnownImm (E, In->RegX) && /* Value to be loaded is known */
- (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
- !CE_UseLoadFlags (N)) { /* Which does not use the flags */
- Delete = 1;
- }
- break;
-
- case OP65_LDY:
- if (RegValIsKnown (In->RegY) && /* Value of Y is known */
- CE_IsKnownImm (E, In->RegY) && /* Value to be loaded is known */
- (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
- !CE_UseLoadFlags (N)) { /* Which does not use the flags */
- Delete = 1;
- }
- break;
-
- case OP65_STA:
- /* If we store into a known zero page location, and this
- * location does already contain the value to be stored,
- * remove the store.
- */
- if (RegValIsKnown (In->RegA) && /* Value of A is known */
- E->AM == AM65_ZP && /* Store into zp */
- In->RegA == ZPRegVal (E->Chg, In)) { /* Value identical */
-
- Delete = 1;
- }
- break;
-
- case OP65_STX:
- /* If we store into a known zero page location, and this
- * location does already contain the value to be stored,
- * remove the store.
- */
- if (RegValIsKnown (In->RegX) && /* Value of A is known */
- E->AM == AM65_ZP && /* Store into zp */
- In->RegX == ZPRegVal (E->Chg, In)) { /* Value identical */
-
- Delete = 1;
-
- /* If the value in the X register is known and the same as
- * that in the A register, replace the store by a STA. The
- * optimizer will then remove the load instruction for X
- * later. STX does support the zeropage,y addressing mode,
- * so be sure to check for that.
- */
- } else if (RegValIsKnown (In->RegX) &&
- In->RegX == In->RegA &&
- E->AM != AM65_ABSY &&
- E->AM != AM65_ZPY) {
- /* Use the A register instead */
- CE_ReplaceOPC (E, OP65_STA);
- }
- break;
-
- case OP65_STY:
- /* If we store into a known zero page location, and this
- * location does already contain the value to be stored,
- * remove the store.
- */
- if (RegValIsKnown (In->RegY) && /* Value of Y is known */
- E->AM == AM65_ZP && /* Store into zp */
- In->RegY == ZPRegVal (E->Chg, In)) { /* Value identical */
-
- Delete = 1;
-
- /* If the value in the Y register is known and the same as
- * that in the A register, replace the store by a STA. The
- * optimizer will then remove the load instruction for Y
- * later. If replacement by A is not possible try a
- * replacement by X, but check for invalid addressing modes
- * in this case.
- */
- } else if (RegValIsKnown (In->RegY)) {
- if (In->RegY == In->RegA) {
- CE_ReplaceOPC (E, OP65_STA);
- } else if (In->RegY == In->RegX &&
- E->AM != AM65_ABSX &&
- E->AM != AM65_ZPX) {
- CE_ReplaceOPC (E, OP65_STX);
- }
- }
- break;
-
- case OP65_STZ:
- /* If we store into a known zero page location, and this
- * location does already contain the value to be stored,
- * remove the store.
- */
- if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && E->AM == AM65_ZP) {
- if (ZPRegVal (E->Chg, In) == 0) {
+ CodeEntry* N;
+
+ /* Get next entry */
+ CodeEntry* E = CS_GetEntry (S, I);
+
+ /* Assume we won't delete the entry */
+ int Delete = 0;
+
+ /* Get a pointer to the input registers of the insn */
+ const RegContents* In = &E->RI->In;
+
+ /* Handle the different instructions */
+ switch (E->OPC) {
+
+ case OP65_LDA:
+ if (RegValIsKnown (In->RegA) && /* Value of A is known */
+ CE_IsKnownImm (E, In->RegA) && /* Value to be loaded is known */
+ (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
+ !CE_UseLoadFlags (N)) { /* Which does not use the flags */
+ Delete = 1;
+ }
+ break;
+
+ case OP65_LDX:
+ if (RegValIsKnown (In->RegX) && /* Value of X is known */
+ CE_IsKnownImm (E, In->RegX) && /* Value to be loaded is known */
+ (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
+ !CE_UseLoadFlags (N)) { /* Which does not use the flags */
+ Delete = 1;
+ }
+ break;
+
+ case OP65_LDY:
+ if (RegValIsKnown (In->RegY) && /* Value of Y is known */
+ CE_IsKnownImm (E, In->RegY) && /* Value to be loaded is known */
+ (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
+ !CE_UseLoadFlags (N)) { /* Which does not use the flags */
+ Delete = 1;
+ }
+ break;
+
+ case OP65_STA:
+ /* If we store into a known zero page location, and this
+ * location does already contain the value to be stored,
+ * remove the store.
+ */
+ if (RegValIsKnown (In->RegA) && /* Value of A is known */
+ E->AM == AM65_ZP && /* Store into zp */
+ In->RegA == ZPRegVal (E->Chg, In)) { /* Value identical */
+
+ Delete = 1;
+ }
+ break;
+
+ case OP65_STX:
+ /* If we store into a known zero page location, and this
+ * location does already contain the value to be stored,
+ * remove the store.
+ */
+ if (RegValIsKnown (In->RegX) && /* Value of A is known */
+ E->AM == AM65_ZP && /* Store into zp */
+ In->RegX == ZPRegVal (E->Chg, In)) { /* Value identical */
+
+ Delete = 1;
+
+ /* If the value in the X register is known and the same as
+ * that in the A register, replace the store by a STA. The
+ * optimizer will then remove the load instruction for X
+ * later. STX does support the zeropage,y addressing mode,
+ * so be sure to check for that.
+ */
+ } else if (RegValIsKnown (In->RegX) &&
+ In->RegX == In->RegA &&
+ E->AM != AM65_ABSY &&
+ E->AM != AM65_ZPY) {
+ /* Use the A register instead */
+ CE_ReplaceOPC (E, OP65_STA);
+ }
+ break;
+
+ case OP65_STY:
+ /* If we store into a known zero page location, and this
+ * location does already contain the value to be stored,
+ * remove the store.
+ */
+ if (RegValIsKnown (In->RegY) && /* Value of Y is known */
+ E->AM == AM65_ZP && /* Store into zp */
+ In->RegY == ZPRegVal (E->Chg, In)) { /* Value identical */
+
+ Delete = 1;
+
+ /* If the value in the Y register is known and the same as
+ * that in the A register, replace the store by a STA. The
+ * optimizer will then remove the load instruction for Y
+ * later. If replacement by A is not possible try a
+ * replacement by X, but check for invalid addressing modes
+ * in this case.
+ */
+ } else if (RegValIsKnown (In->RegY)) {
+ if (In->RegY == In->RegA) {
+ CE_ReplaceOPC (E, OP65_STA);
+ } else if (In->RegY == In->RegX &&
+ E->AM != AM65_ABSX &&
+ E->AM != AM65_ZPX) {
+ CE_ReplaceOPC (E, OP65_STX);
+ }
+ }
+ break;
+
+ case OP65_STZ:
+ /* If we store into a known zero page location, and this
+ * location does already contain the value to be stored,
+ * remove the store.
+ */
+ if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 && E->AM == AM65_ZP) {
+ if (ZPRegVal (E->Chg, In) == 0) {
Delete = 1;
}
- }
- break;
+ }
+ break;
- case OP65_TAX:
+ case OP65_TAX:
if (RegValIsKnown (In->RegA) &&
- In->RegA == In->RegX &&
- (N = CS_GetNextEntry (S, I)) != 0 &&
- !CE_UseLoadFlags (N)) {
- /* Value is identical and not followed by a branch */
- Delete = 1;
- }
- break;
-
- case OP65_TAY:
+ In->RegA == In->RegX &&
+ (N = CS_GetNextEntry (S, I)) != 0 &&
+ !CE_UseLoadFlags (N)) {
+ /* Value is identical and not followed by a branch */
+ Delete = 1;
+ }
+ break;
+
+ case OP65_TAY:
if (RegValIsKnown (In->RegA) &&
- In->RegA == In->RegY &&
- (N = CS_GetNextEntry (S, I)) != 0 &&
- !CE_UseLoadFlags (N)) {
- /* Value is identical and not followed by a branch */
- Delete = 1;
- }
- break;
-
- case OP65_TXA:
+ In->RegA == In->RegY &&
+ (N = CS_GetNextEntry (S, I)) != 0 &&
+ !CE_UseLoadFlags (N)) {
+ /* Value is identical and not followed by a branch */
+ Delete = 1;
+ }
+ break;
+
+ case OP65_TXA:
if (RegValIsKnown (In->RegX) &&
- In->RegX == In->RegA &&
- (N = CS_GetNextEntry (S, I)) != 0 &&
- !CE_UseLoadFlags (N)) {
- /* Value is identical and not followed by a branch */
- Delete = 1;
- }
- break;
-
- case OP65_TYA:
+ In->RegX == In->RegA &&
+ (N = CS_GetNextEntry (S, I)) != 0 &&
+ !CE_UseLoadFlags (N)) {
+ /* Value is identical and not followed by a branch */
+ Delete = 1;
+ }
+ break;
+
+ case OP65_TYA:
if (RegValIsKnown (In->RegY) &&
- In->RegY == In->RegA &&
- (N = CS_GetNextEntry (S, I)) != 0 &&
- !CE_UseLoadFlags (N)) {
- /* Value is identical and not followed by a branch */
- Delete = 1;
- }
- break;
+ In->RegY == In->RegA &&
+ (N = CS_GetNextEntry (S, I)) != 0 &&
+ !CE_UseLoadFlags (N)) {
+ /* Value is identical and not followed by a branch */
+ Delete = 1;
+ }
+ break;
- default:
- break;
+ default:
+ break;
- }
+ }
- /* Delete the entry if requested */
- if (Delete) {
+ /* Delete the entry if requested */
+ if (Delete) {
- /* Register value is not used, remove the load */
- CS_DelEntry (S, I);
+ /* Register value is not used, remove the load */
+ CS_DelEntry (S, I);
- /* Remember, we had changes */
- ++Changes;
+ /* Remember, we had changes */
+ ++Changes;
- } else {
+ } else {
- /* Next entry */
- ++I;
+ /* Next entry */
+ ++I;
- }
+ }
}
unsigned I = 0;
while (I < CS_GetEntryCount (S)) {
- CodeEntry* N;
- CodeEntry* X;
-
- /* Get next entry */
- CodeEntry* E = CS_GetEntry (S, I);
-
- /* Check if it is a store instruction followed by a load from the
- * same address which is itself not followed by a conditional branch.
- */
- if ((E->Info & OF_STORE) != 0 &&
- (N = CS_GetNextEntry (S, I)) != 0 &&
- !CE_HasLabel (N) &&
- E->AM == N->AM &&
- ((E->OPC == OP65_STA && N->OPC == OP65_LDA) ||
- (E->OPC == OP65_STX && N->OPC == OP65_LDX) ||
- (E->OPC == OP65_STY && N->OPC == OP65_LDY)) &&
- strcmp (E->Arg, N->Arg) == 0 &&
- (X = CS_GetNextEntry (S, I+1)) != 0 &&
+ CodeEntry* N;
+ CodeEntry* X;
+
+ /* Get next entry */
+ CodeEntry* E = CS_GetEntry (S, I);
+
+ /* Check if it is a store instruction followed by a load from the
+ * same address which is itself not followed by a conditional branch.
+ */
+ if ((E->Info & OF_STORE) != 0 &&
+ (N = CS_GetNextEntry (S, I)) != 0 &&
+ !CE_HasLabel (N) &&
+ E->AM == N->AM &&
+ ((E->OPC == OP65_STA && N->OPC == OP65_LDA) ||
+ (E->OPC == OP65_STX && N->OPC == OP65_LDX) ||
+ (E->OPC == OP65_STY && N->OPC == OP65_LDY)) &&
+ strcmp (E->Arg, N->Arg) == 0 &&
+ (X = CS_GetNextEntry (S, I+1)) != 0 &&
!CE_UseLoadFlags (X)) {
- /* Register has already the correct value, remove the load */
- CS_DelEntry (S, I+1);
+ /* Register has already the correct value, remove the load */
+ CS_DelEntry (S, I+1);
- /* Remember, we had changes */
- ++Changes;
+ /* Remember, we had changes */
+ ++Changes;
- }
+ }
- /* Next entry */
- ++I;
+ /* Next entry */
+ ++I;
}
unsigned I = 0;
while (I < CS_GetEntryCount (S)) {
- CodeEntry* N;
- CodeEntry* X;
- CodeEntry* P;
-
- /* Get next entry */
- CodeEntry* E = CS_GetEntry (S, I);
-
- /* Check if we have two transfer instructions */
- if ((E->Info & OF_XFR) != 0 &&
- (N = CS_GetNextEntry (S, I)) != 0 &&
- !CE_HasLabel (N) &&
- (N->Info & OF_XFR) != 0) {
-
- /* Check if it's a transfer and back */
- if ((E->OPC == OP65_TAX && N->OPC == OP65_TXA && !RegXUsed (S, I+2)) ||
- (E->OPC == OP65_TAY && N->OPC == OP65_TYA && !RegYUsed (S, I+2)) ||
- (E->OPC == OP65_TXA && N->OPC == OP65_TAX && !RegAUsed (S, I+2)) ||
- (E->OPC == OP65_TYA && N->OPC == OP65_TAY && !RegAUsed (S, I+2))) {
-
- /* If the next insn is a conditional branch, check if the insn
- * preceeding the first xfr will set the flags right, otherwise we
- * may not remove the sequence.
- */
- if ((X = CS_GetNextEntry (S, I+1)) == 0) {
- goto NextEntry;
- }
+ CodeEntry* N;
+ CodeEntry* X;
+ CodeEntry* P;
+
+ /* Get next entry */
+ CodeEntry* E = CS_GetEntry (S, I);
+
+ /* Check if we have two transfer instructions */
+ if ((E->Info & OF_XFR) != 0 &&
+ (N = CS_GetNextEntry (S, I)) != 0 &&
+ !CE_HasLabel (N) &&
+ (N->Info & OF_XFR) != 0) {
+
+ /* Check if it's a transfer and back */
+ if ((E->OPC == OP65_TAX && N->OPC == OP65_TXA && !RegXUsed (S, I+2)) ||
+ (E->OPC == OP65_TAY && N->OPC == OP65_TYA && !RegYUsed (S, I+2)) ||
+ (E->OPC == OP65_TXA && N->OPC == OP65_TAX && !RegAUsed (S, I+2)) ||
+ (E->OPC == OP65_TYA && N->OPC == OP65_TAY && !RegAUsed (S, I+2))) {
+
+ /* If the next insn is a conditional branch, check if the insn
+ * preceeding the first xfr will set the flags right, otherwise we
+ * may not remove the sequence.
+ */
+ if ((X = CS_GetNextEntry (S, I+1)) == 0) {
+ goto NextEntry;
+ }
if (CE_UseLoadFlags (X)) {
- if (I == 0) {
- /* No preceeding entry */
- goto NextEntry;
- }
- P = CS_GetEntry (S, I-1);
- if ((P->Info & OF_SETF) == 0) {
- /* Does not set the flags */
- goto NextEntry;
- }
- }
-
- /* Remove both transfers */
- CS_DelEntry (S, I+1);
- CS_DelEntry (S, I);
-
- /* Remember, we had changes */
- ++Changes;
- }
- }
+ if (I == 0) {
+ /* No preceeding entry */
+ goto NextEntry;
+ }
+ P = CS_GetEntry (S, I-1);
+ if ((P->Info & OF_SETF) == 0) {
+ /* Does not set the flags */
+ goto NextEntry;
+ }
+ }
+
+ /* Remove both transfers */
+ CS_DelEntry (S, I+1);
+ CS_DelEntry (S, I);
+
+ /* Remember, we had changes */
+ ++Changes;
+ }
+ }
NextEntry:
- /* Next entry */
- ++I;
+ /* Next entry */
+ ++I;
}
unsigned I = 0;
while (I < CS_GetEntryCount (S)) {
- CodeEntry* N;
+ CodeEntry* N;
- /* Get next entry */
- CodeEntry* E = CS_GetEntry (S, I);
+ /* Get next entry */
+ CodeEntry* E = CS_GetEntry (S, I);
- /* Check if we have a load followed by a transfer where the loaded
+ /* Check if we have a load followed by a transfer where the loaded
* register is not used later.
*/
- if ((E->Info & OF_LOAD) != 0 &&
- (N = CS_GetNextEntry (S, I)) != 0 &&
- !CE_HasLabel (N) &&
- (N->Info & OF_XFR) != 0 &&
+ if ((E->Info & OF_LOAD) != 0 &&
+ (N = CS_GetNextEntry (S, I)) != 0 &&
+ !CE_HasLabel (N) &&
+ (N->Info & OF_XFR) != 0 &&
GetRegInfo (S, I+2, E->Chg) != E->Chg) {
CodeEntry* X = 0;
++Changes;
--I; /* Correct for one entry less */
}
- }
+ }
- /* Next entry */
- ++I;
+ /* Next entry */
+ ++I;
}
/* Return the number of changes made */
unsigned I = 0;
while (I < CS_GetEntryCount (S)) {
- /* Get next entry */
- CodeEntry* E = CS_GetEntry (S, I);
+ /* Get next entry */
+ CodeEntry* E = CS_GetEntry (S, I);
switch (State) {
}
- /* Next entry */
- ++I;
+ /* Next entry */
+ ++I;
}
/* Return the number of changes made */
unsigned I = 0;
while (I < CS_GetEntryCount (S)) {
- /* Get next entry */
- CodeEntry* E = CS_GetEntry (S, I);
+ /* Get next entry */
+ CodeEntry* E = CS_GetEntry (S, I);
switch (State) {
}
- /* Next entry */
- ++I;
+ /* Next entry */
+ ++I;
}
/* Return the number of changes made */
CodeEntry* X;
- /* Get next entry */
- CodeEntry* E = CS_GetEntry (S, I);
+ /* Get next entry */
+ CodeEntry* E = CS_GetEntry (S, I);
switch (State) {
}
- /* Next entry */
- ++I;
+ /* Next entry */
+ ++I;
}
/* Return the number of changes made */
I = 0;
while (I < CS_GetEntryCount (S)) {
- /* Get next entry */
- CodeEntry* E = CS_GetEntry (S, I);
+ /* Get next entry */
+ CodeEntry* E = CS_GetEntry (S, I);
- /* Get pointers to the input and output registers of the insn */
- const RegContents* Out = &E->RI->Out;
+ /* Get pointers to the input and output registers of the insn */
+ const RegContents* Out = &E->RI->Out;
const RegContents* In = &E->RI->In;
/* Argument for LDn and flag */
++Changes;
}
- /* Next entry */
- ++I;
+ /* Next entry */
+ ++I;
}
/* Return the number of changes made */
/*****************************************************************************/
-/* Optimize branch types */
+/* Optimize branch types */
/*****************************************************************************/
unsigned I = 0;
while (I < CS_GetEntryCount (S)) {
- /* Get next entry */
- CodeEntry* E = CS_GetEntry (S, I);
+ /* Get next entry */
+ CodeEntry* E = CS_GetEntry (S, I);
- /* Check if it's a conditional branch to a local label. */
- if (E->Info & OF_CBRA) {
+ /* Check if it's a conditional branch to a local label. */
+ if (E->Info & OF_CBRA) {
- /* Is this a branch to a local symbol? */
- if (E->JumpTo != 0) {
+ /* Is this a branch to a local symbol? */
+ if (E->JumpTo != 0) {
- /* Check if the branch distance is short */
- int IsShort = IsShortDist (GetBranchDist (S, I, E->JumpTo->Owner));
+ /* Check if the branch distance is short */
+ int IsShort = IsShortDist (GetBranchDist (S, I, E->JumpTo->Owner));
- /* Make the branch short/long according to distance */
- if ((E->Info & OF_LBRA) == 0 && !IsShort) {
- /* Short branch but long distance */
- CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
- ++Changes;
- } else if ((E->Info & OF_LBRA) != 0 && IsShort) {
- /* Long branch but short distance */
- CE_ReplaceOPC (E, MakeShortBranch (E->OPC));
- ++Changes;
- }
+ /* Make the branch short/long according to distance */
+ if ((E->Info & OF_LBRA) == 0 && !IsShort) {
+ /* Short branch but long distance */
+ CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
+ ++Changes;
+ } else if ((E->Info & OF_LBRA) != 0 && IsShort) {
+ /* Long branch but short distance */
+ CE_ReplaceOPC (E, MakeShortBranch (E->OPC));
+ ++Changes;
+ }
- } else if ((E->Info & OF_LBRA) == 0) {
+ } else if ((E->Info & OF_LBRA) == 0) {
- /* Short branch to external symbol - make it long */
- CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
- ++Changes;
+ /* Short branch to external symbol - make it long */
+ CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
+ ++Changes;
- }
+ }
- } else if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 &&
- (E->Info & OF_UBRA) != 0 &&
- E->JumpTo != 0 &&
- IsShortDist (GetBranchDist (S, I, E->JumpTo->Owner))) {
+ } else if ((CPUIsets[CPU] & CPU_ISET_65SC02) != 0 &&
+ (E->Info & OF_UBRA) != 0 &&
+ E->JumpTo != 0 &&
+ IsShortDist (GetBranchDist (S, I, E->JumpTo->Owner))) {
- /* The jump is short and may be replaced by a BRA on the 65C02 CPU */
- CE_ReplaceOPC (E, OP65_BRA);
- ++Changes;
- }
+ /* The jump is short and may be replaced by a BRA on the 65C02 CPU */
+ CE_ReplaceOPC (E, OP65_BRA);
+ ++Changes;
+ }
- /* Next entry */
- ++I;
+ /* Next entry */
+ ++I;
}
I = 0;
while (I < CS_GetEntryCount (S)) {
- /* Get next entry */
- CodeEntry* E = CS_GetEntry (S, I);
+ /* Get next entry */
+ CodeEntry* E = CS_GetEntry (S, I);
- /* Check if it's what we're looking for */
- if (E->OPC == OP65_LDA &&
+ /* Check if it's what we're looking for */
+ if (E->OPC == OP65_LDA &&
E->AM == AM65_ZP_INDY &&
E->RI->In.RegY == 0 &&
E->RI->In.RegX == 0) {
- /* Replace by the same insn with other addressing mode */
+ /* Replace by the same insn with other addressing mode */
CodeEntry* X = NewCodeEntry (E->OPC, AM65_ZPX_IND, E->Arg, 0, E->LI);
CS_InsertEntry (S, X, I+1);
/* Remove the old insn */
CS_DelEntry (S, I);
++Changes;
- }
+ }
- /* Next entry */
- ++I;
+ /* Next entry */
+ ++I;
}
I = 0;
while (I < CS_GetEntryCount (S)) {
- /* Get next entry */
- CodeEntry* E = CS_GetEntry (S, I);
+ /* Get next entry */
+ CodeEntry* E = CS_GetEntry (S, I);
- /* Check if it's what we're looking for */
- if (E->OPC == OP65_LDA &&
+ /* Check if it's what we're looking for */
+ if (E->OPC == OP65_LDA &&
E->AM == AM65_ZPX_IND &&
E->RI->In.RegY == 0 &&
E->RI->In.RegX == 0) {
- /* Replace by the same insn with other addressing mode */
+ /* Replace by the same insn with other addressing mode */
CodeEntry* X = NewCodeEntry (E->OPC, AM65_ZP_INDY, E->Arg, 0, E->LI);
CS_InsertEntry (S, X, I+1);
/* Remove the old insn */
CS_DelEntry (S, I);
++Changes;
- }
+ }
- /* Next entry */
- ++I;
+ /* Next entry */
+ ++I;
}