1 /*****************************************************************************/
5 /* Environment independent low level optimizations */
9 /* (C) 2001 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
46 /*****************************************************************************/
47 /* Helper functions */
48 /*****************************************************************************/
52 static int GetBranchDist (CodeSeg* S, unsigned From, CodeEntry* To)
53 /* Get the branch distance between the two entries and return it. The distance
54 * will be negative for backward jumps and positive for forward jumps.
57 /* Get the index of the branch target */
58 unsigned TI = CS_GetEntryIndex (S, To);
60 /* Determine the branch distance */
63 /* Forward branch, do not count the current insn */
66 CodeEntry* N = CS_GetEntry (S, J++);
73 CodeEntry* N = CS_GetEntry (S, J++);
78 /* Return the calculated distance */
84 static int IsShortDist (int Distance)
85 /* Return true if the given distance is a short branch distance */
87 return (Distance >= -125 && Distance <= 125);
92 /*****************************************************************************/
93 /* Replace jumps to RTS by RTS */
94 /*****************************************************************************/
98 unsigned OptRTSJumps1 (CodeSeg* S)
99 /* Replace jumps to RTS by RTS */
101 unsigned Changes = 0;
103 /* Walk over all entries minus the last one */
105 while (I < CS_GetEntryCount (S)) {
107 /* Get the next entry */
108 CodeEntry* E = CS_GetEntry (S, I);
110 /* Check if it's an unconditional branch to a local target */
111 if ((E->Info & OF_UBRA) != 0 &&
113 E->JumpTo->Owner->OPC == OP65_RTS) {
115 /* Insert an RTS instruction */
116 CodeEntry* X = NewCodeEntry (OP65_RTS, AM65_IMP, 0, 0, E->LI);
117 CS_InsertEntry (S, X, I+1);
119 /* Delete the jump */
122 /* Remember, we had changes */
132 /* Return the number of changes made */
138 unsigned OptRTSJumps2 (CodeSeg* S)
139 /* Replace long conditional jumps to RTS */
141 unsigned Changes = 0;
143 /* Walk over all entries minus the last one */
145 while (I < CS_GetEntryCount (S)) {
149 /* Get the next entry */
150 CodeEntry* E = CS_GetEntry (S, I);
152 /* Check if it's an unconditional branch to a local target */
153 if ((E->Info & OF_CBRA) != 0 && /* Conditional branch */
154 (E->Info & OF_LBRA) != 0 && /* Long branch */
155 E->JumpTo != 0 && /* Local label */
156 E->JumpTo->Owner->OPC == OP65_RTS && /* Target is an RTS */
157 (N = CS_GetNextEntry (S, I)) != 0) { /* There is a next entry */
163 /* We will create a jump around an RTS instead of the long branch */
164 X = NewCodeEntry (OP65_RTS, AM65_IMP, 0, 0, E->JumpTo->Owner->LI);
165 CS_InsertEntry (S, X, I+1);
167 /* Get the new branch opcode */
168 NewBranch = MakeShortBranch (GetInverseBranch (E->OPC));
170 /* Get the label attached to N, create a new one if needed */
171 LN = CS_GenLabel (S, N);
173 /* Generate the branch */
174 X = NewCodeEntry (NewBranch, AM65_BRA, LN->Name, LN, E->LI);
175 CS_InsertEntry (S, X, I+1);
177 /* Delete the long branch */
180 /* Remember, we had changes */
190 /* Return the number of changes made */
196 /*****************************************************************************/
197 /* Remove dead jumps */
198 /*****************************************************************************/
202 unsigned OptDeadJumps (CodeSeg* S)
203 /* Remove dead jumps (jumps to the next instruction) */
205 unsigned Changes = 0;
207 /* Walk over all entries minus the last one */
209 while (I < CS_GetEntryCount (S)) {
211 /* Get the next entry */
212 CodeEntry* E = CS_GetEntry (S, I);
214 /* Check if it's a branch, if it has a local target, and if the target
215 * is the next instruction.
217 if (E->AM == AM65_BRA && E->JumpTo && E->JumpTo->Owner == CS_GetEntry (S, I+1)) {
219 /* Delete the dead jump */
222 /* Remember, we had changes */
233 /* Return the number of changes made */
239 /*****************************************************************************/
240 /* Remove dead code */
241 /*****************************************************************************/
245 unsigned OptDeadCode (CodeSeg* S)
246 /* Remove dead code (code that follows an unconditional jump or an rts/rti
250 unsigned Changes = 0;
252 /* Walk over all entries */
254 while (I < CS_GetEntryCount (S)) {
259 CodeEntry* E = CS_GetEntry (S, I);
261 /* Check if it's an unconditional branch, and if the next entry has
264 if ((E->Info & OF_DEAD) != 0 &&
265 (N = CS_GetNextEntry (S, I)) != 0 &&
268 /* Delete the next entry */
269 CS_DelEntry (S, I+1);
271 /* Remember, we had changes */
282 /* Return the number of changes made */
288 /*****************************************************************************/
289 /* Optimize jump cascades */
290 /*****************************************************************************/
294 unsigned OptJumpCascades (CodeSeg* S)
295 /* Optimize jump cascades (jumps to jumps). In such a case, the jump is
296 * replaced by a jump to the final location. This will in some cases produce
297 * worse code, because some jump targets are no longer reachable by short
298 * branches, but this is quite rare, so there are more advantages than
302 unsigned Changes = 0;
304 /* Walk over all entries */
306 while (I < CS_GetEntryCount (S)) {
312 CodeEntry* E = CS_GetEntry (S, I);
314 /* Check if it's a branch, if it has a jump label, if this jump
315 * label is not attached to the instruction itself, and if the
316 * target instruction is itself a branch.
318 if ((E->Info & OF_BRA) != 0 &&
319 (OldLabel = E->JumpTo) != 0 &&
320 (N = OldLabel->Owner) != E &&
321 (N->Info & OF_BRA) != 0) {
323 /* Check if we can use the final target label. This is the case,
324 * if the target branch is an absolut branch, or if it is a
325 * conditional branch checking the same condition as the first one.
327 if ((N->Info & OF_UBRA) != 0 ||
328 ((E->Info & OF_CBRA) != 0 &&
329 GetBranchCond (E->OPC) == GetBranchCond (N->OPC))) {
331 /* This is a jump cascade and we may jump to the final target.
332 * Insert a new instruction, then remove the old one
334 CodeEntry* X = NewCodeEntry (E->OPC, E->AM, N->Arg, N->JumpTo, E->LI);
336 /* Insert it behind E */
337 CS_InsertEntry (S, X, I+1);
342 /* Remember, we had changes */
350 /* Check if both are conditional branches, and the condition of
351 * the second is the inverse of that of the first. In this case,
352 * the second branch will never be taken, and we may jump directly
353 * to the instruction behind this one.
355 if ((E->Info & OF_CBRA) != 0 && (N->Info & OF_CBRA) != 0) {
357 CodeEntry* X; /* Instruction behind N */
358 CodeLabel* LX; /* Label attached to X */
360 /* Get the branch conditions of both branches */
361 bc_t BC1 = GetBranchCond (E->OPC);
362 bc_t BC2 = GetBranchCond (N->OPC);
364 /* Check the branch conditions */
365 if (BC1 != GetInverseCond (BC2)) {
366 /* Condition not met */
370 /* We may jump behind this conditional branch. Get the
371 * pointer to the next instruction
373 if ((X = CS_GetNextEntry (S, CS_GetEntryIndex (S, N))) == 0) {
374 /* N is the last entry, bail out */
378 /* Get the label attached to X, create a new one if needed */
379 LX = CS_GenLabel (S, X);
381 /* Move the reference from E to the new label */
382 CS_MoveLabelRef (S, E, LX);
384 /* Remember, we had changes */
399 /* Return the number of changes made */
405 /*****************************************************************************/
406 /* Optimize jsr/rts */
407 /*****************************************************************************/
411 unsigned OptRTS (CodeSeg* S)
412 /* Optimize subroutine calls followed by an RTS. The subroutine call will get
413 * replaced by a jump. Don't bother to delete the RTS if it does not have a
414 * label, the dead code elimination should take care of it.
417 unsigned Changes = 0;
419 /* Walk over all entries minus the last one */
421 while (I < CS_GetEntryCount (S)) {
426 CodeEntry* E = CS_GetEntry (S, I);
428 /* Check if it's a subroutine call and if the following insn is RTS */
429 if (E->OPC == OP65_JSR &&
430 (N = CS_GetNextEntry (S, I)) != 0 &&
431 N->OPC == OP65_RTS) {
433 /* Change the jsr to a jmp and use the additional info for a jump */
435 CE_ReplaceOPC (E, OP65_JMP);
437 /* Remember, we had changes */
447 /* Return the number of changes made */
453 /*****************************************************************************/
454 /* Optimize jump targets */
455 /*****************************************************************************/
459 unsigned OptJumpTarget (CodeSeg* S)
460 /* If the instruction preceeding an unconditional branch is the same as the
461 * instruction preceeding the jump target, the jump target may be moved
462 * one entry back. This is a size optimization, since the instruction before
463 * the branch gets removed.
466 unsigned Changes = 0;
467 CodeEntry* E1; /* Entry 1 */
468 CodeEntry* E2; /* Entry 2 */
469 CodeEntry* T1; /* Jump target entry 1 */
470 CodeEntry* T2; /* Jump target entry 2 */
471 CodeLabel* TL1; /* Target label 1 */
472 unsigned TI; /* Target index */
474 /* Walk over the entries */
476 while (I < CS_GetEntryCount (S)) {
479 E2 = CS_GetEntry (S, I+1);
481 /* Check if we have a jump or branch, and a matching label */
482 if ((E2->Info & OF_UBRA) != 0 && E2->JumpTo) {
484 /* Get the target instruction for the label */
485 T2 = E2->JumpTo->Owner;
487 /* Get the entry preceeding this one (if possible) */
488 TI = CS_GetEntryIndex (S, T2);
490 /* There is no entry before this one */
493 T1 = CS_GetEntry (S, TI-1);
495 /* Get the entry preceeding the jump */
496 E1 = CS_GetEntry (S, I);
498 /* Check if both preceeding instructions are identical */
499 if (!CodeEntriesAreEqual (E1, T1)) {
500 /* Not equal, try next */
504 /* Get the label for the instruction preceeding the jump target.
505 * This routine will create a new label if the instruction does
506 * not already have one.
508 TL1 = CS_GenLabel (S, T1);
510 /* Change the jump target to point to this new label */
511 CS_MoveLabelRef (S, E2, TL1);
513 /* If the instruction preceeding the jump has labels attached,
514 * move references to this label to the new label.
516 if (CE_HasLabel (E1)) {
517 CS_MoveLabels (S, E1, T1);
520 /* Remove the entry preceeding the jump */
523 /* Remember, we had changes */
534 /* Return the number of changes made */
540 /*****************************************************************************/
541 /* Optimize conditional branches */
542 /*****************************************************************************/
546 unsigned OptCondBranches (CodeSeg* S)
547 /* Performs several optimization steps:
549 * - If an immidiate load of a register is followed by a conditional jump that
550 * is never taken because the load of the register sets the flags in such a
551 * manner, remove the conditional branch.
552 * - If the conditional branch is always taken because of the register load,
553 * replace it by a jmp.
554 * - If a conditional branch jumps around an unconditional branch, remove the
555 * conditional branch and make the jump a conditional branch with the
556 * inverse condition of the first one.
559 unsigned Changes = 0;
561 /* Walk over the entries */
563 while (I < CS_GetEntryCount (S)) {
569 CodeEntry* E = CS_GetEntry (S, I);
571 /* Check if it's a register load */
572 if ((E->Info & OF_LOAD) != 0 && /* It's a load instruction */
573 E->AM == AM65_IMM && /* ..with immidiate addressing */
574 (E->Flags & CEF_NUMARG) != 0 && /* ..and a numeric argument. */
575 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
576 (N->Info & OF_CBRA) != 0 && /* ..which is a conditional branch */
577 !CE_HasLabel (N)) { /* ..and does not have a label */
579 /* Get the branch condition */
580 bc_t BC = GetBranchCond (N->OPC);
582 /* Check the argument against the branch condition */
583 if ((BC == BC_EQ && E->Num != 0) ||
584 (BC == BC_NE && E->Num == 0) ||
585 (BC == BC_PL && (E->Num & 0x80) != 0) ||
586 (BC == BC_MI && (E->Num & 0x80) == 0)) {
588 /* Remove the conditional branch */
589 CS_DelEntry (S, I+1);
591 /* Remember, we had changes */
594 } else if ((BC == BC_EQ && E->Num == 0) ||
595 (BC == BC_NE && E->Num != 0) ||
596 (BC == BC_PL && (E->Num & 0x80) == 0) ||
597 (BC == BC_MI && (E->Num & 0x80) != 0)) {
599 /* The branch is always taken, replace it by a jump */
600 CE_ReplaceOPC (N, OP65_JMP);
602 /* Remember, we had changes */
608 if ((E->Info & OF_CBRA) != 0 && /* It's a conditional branch */
609 (L = E->JumpTo) != 0 && /* ..referencing a local label */
610 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
611 (N->Info & OF_UBRA) != 0 && /* ..which is an uncond branch, */
612 !CE_HasLabel (N) && /* ..has no label attached */
613 L->Owner == CS_GetNextEntry (S, I+1)) {/* ..and jump target follows */
615 /* Replace the jump by a conditional branch with the inverse branch
616 * condition than the branch around it.
618 CE_ReplaceOPC (N, GetInverseBranch (E->OPC));
620 /* Remove the conditional branch */
623 /* Remember, we had changes */
633 /* Return the number of changes made */
639 /*****************************************************************************/
640 /* Remove unused loads and stores */
641 /*****************************************************************************/
645 unsigned OptUnusedLoads (CodeSeg* S)
646 /* Remove loads of registers where the value loaded is not used later. */
648 unsigned Changes = 0;
650 /* Walk over the entries */
652 while (I < CS_GetEntryCount (S)) {
657 CodeEntry* E = CS_GetEntry (S, I);
659 /* Check if it's a register load or transfer insn */
660 if ((E->Info & (OF_LOAD | OF_XFR | OF_REG_INCDEC)) != 0 &&
661 (N = CS_GetNextEntry (S, I)) != 0 &&
662 (N->Info & OF_FBRA) == 0) {
664 /* Check which sort of load or transfer it is */
671 case OP65_TYA: R = REG_A; break;
675 case OP65_TAX: R = REG_X; break;
679 case OP65_TAY: R = REG_Y; break;
680 default: goto NextEntry; /* OOPS */
683 /* Get register usage and check if the register value is used later */
684 if ((GetRegInfo (S, I+1, R) & R) == 0) {
686 /* Register value is not used, remove the load */
689 /* Remember, we had changes */
701 /* Return the number of changes made */
707 unsigned OptUnusedStores (CodeSeg* S)
708 /* Remove stores into zero page registers that aren't used later */
710 unsigned Changes = 0;
712 /* Walk over the entries */
714 while (I < CS_GetEntryCount (S)) {
717 CodeEntry* E = CS_GetEntry (S, I);
719 /* Check if it's a register load or transfer insn */
720 if ((E->Info & OF_STORE) != 0 &&
722 (E->Chg & REG_ZP) != 0) {
724 /* Check for the zero page location. We know that there cannot be
725 * more than one zero page location involved in the store.
727 unsigned R = E->Chg & REG_ZP;
729 /* Get register usage and check if the register value is used later */
730 if ((GetRegInfo (S, I+1, R) & R) == 0) {
732 /* Register value is not used, remove the load */
735 /* Remember, we had changes */
746 /* Return the number of changes made */
752 unsigned OptDupLoads (CodeSeg* S)
753 /* Remove loads of registers where the value loaded is already in the register. */
755 unsigned Changes = 0;
758 /* Generate register info for this step */
761 /* Walk over the entries */
763 while (I < CS_GetEntryCount (S)) {
768 CodeEntry* E = CS_GetEntry (S, I);
770 /* Assume we won't delete the entry */
773 /* Get a pointer to the input registers of the insn */
774 const RegContents* In = &E->RI->In;
776 /* Handle the different instructions */
780 if (In->RegA >= 0 && /* Value of A is known */
781 CE_KnownImm (E) && /* Value to be loaded is known */
782 In->RegA == (long) E->Num && /* Both are equal */
783 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
784 (N->Info & OF_FBRA) == 0) { /* Which is not a cond branch */
790 if (In->RegX >= 0 && /* Value of X is known */
791 CE_KnownImm (E) && /* Value to be loaded is known */
792 In->RegX == (long) E->Num && /* Both are equal */
793 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
794 (N->Info & OF_FBRA) == 0) { /* Which is not a cond branch */
800 if (In->RegY >= 0 && /* Value of Y is known */
801 CE_KnownImm (E) && /* Value to be loaded is known */
802 In->RegY == (long) E->Num && /* Both are equal */
803 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
804 (N->Info & OF_FBRA) == 0) { /* Which is not a cond branch */
810 /* If we store into a known zero page location, and this
811 * location does already contain the value to be stored,
814 if (In->RegA >= 0 && /* Value of A is known */
815 E->AM == AM65_ZP && /* Store into zp */
816 (((E->Chg & REG_SREG_LO) != 0 && /* Store into sreg */
817 In->RegA == In->SRegLo) || /* Value identical */
818 ((E->Chg & REG_SREG_HI) != 0 && /* Store into sreg+1 */
819 In->RegA == In->SRegHi))) { /* Value identical */
825 /* If we store into a known zero page location, and this
826 * location does already contain the value to be stored,
829 if (In->RegX >= 0 && /* Value of A is known */
830 E->AM == AM65_ZP && /* Store into zp */
831 (((E->Chg & REG_SREG_LO) != 0 && /* Store into sreg */
832 In->RegX == In->SRegLo) || /* Value identical */
833 ((E->Chg & REG_SREG_HI) != 0 && /* Store into sreg+1 */
834 In->RegX == In->SRegHi))) { /* Value identical */
837 /* If the value in the X register is known and the same as
838 * that in the A register, replace the store by a STA. The
839 * optimizer will then remove the load instruction for X
840 * later. STX does support the zeropage,y addressing mode,
841 * so be sure to check for that.
843 } else if (In->RegX >= 0 &&
844 In->RegX == In->RegA &&
845 E->AM != AM65_ABSY &&
847 /* Use the A register instead */
848 CE_ReplaceOPC (E, OP65_STA);
853 /* If we store into a known zero page location, and this
854 * location does already contain the value to be stored,
857 if (In->RegX >= 0 && /* Value of A is known */
858 E->AM == AM65_ZP && /* Store into zp */
859 (((E->Chg & REG_SREG_LO) != 0 && /* Store into sreg */
860 In->RegX == In->SRegLo) || /* Value identical */
861 ((E->Chg & REG_SREG_HI) != 0 && /* Store into sreg+1 */
862 In->RegX == In->SRegHi))) { /* Value identical */
864 /* If the value in the Y register is known and the same as
865 * that in the A register, replace the store by a STA. The
866 * optimizer will then remove the load instruction for Y
867 * later. If replacement by A is not possible try a
868 * replacement by X, but check for invalid addressing modes
871 } else if (In->RegY >= 0) {
872 if (In->RegY == In->RegA) {
873 CE_ReplaceOPC (E, OP65_STA);
874 } else if (In->RegY == In->RegX &&
875 E->AM != AM65_ABSX &&
877 CE_ReplaceOPC (E, OP65_STX);
884 In->RegA == In->RegX &&
885 (N = CS_GetNextEntry (S, I)) != 0 &&
886 (N->Info & OF_FBRA) == 0) {
887 /* Value is identical and not followed by a branch */
894 In->RegA == In->RegY &&
895 (N = CS_GetNextEntry (S, I)) != 0 &&
896 (N->Info & OF_FBRA) == 0) {
897 /* Value is identical and not followed by a branch */
904 In->RegX == In->RegA &&
905 (N = CS_GetNextEntry (S, I)) != 0 &&
906 (N->Info & OF_FBRA) == 0) {
907 /* Value is identical and not followed by a branch */
914 In->RegY == In->RegA &&
915 (N = CS_GetNextEntry (S, I)) != 0 &&
916 (N->Info & OF_FBRA) == 0) {
917 /* Value is identical and not followed by a branch */
927 /* Delete the entry if requested */
930 /* Register value is not used, remove the load */
933 /* Remember, we had changes */
945 /* Free register info */
948 /* Return the number of changes made */
954 unsigned OptStoreLoad (CodeSeg* S)
955 /* Remove a store followed by a load from the same location. */
957 unsigned Changes = 0;
959 /* Walk over the entries */
961 while (I < CS_GetEntryCount (S)) {
967 CodeEntry* E = CS_GetEntry (S, I);
969 /* Check if it is a store instruction followed by a load from the
970 * same address which is itself not followed by a conditional branch.
972 if ((E->Info & OF_STORE) != 0 &&
973 (N = CS_GetNextEntry (S, I)) != 0 &&
976 ((E->OPC == OP65_STA && N->OPC == OP65_LDA) ||
977 (E->OPC == OP65_STX && N->OPC == OP65_LDX) ||
978 (E->OPC == OP65_STY && N->OPC == OP65_LDY)) &&
979 strcmp (E->Arg, N->Arg) == 0 &&
980 (X = CS_GetNextEntry (S, I+1)) != 0 &&
981 (X->Info & OF_FBRA) == 0) {
983 /* Register has already the correct value, remove the load */
984 CS_DelEntry (S, I+1);
986 /* Remember, we had changes */
996 /* Return the number of changes made */
1002 unsigned OptTransfers (CodeSeg* S)
1003 /* Remove transfers from one register to another and back */
1005 unsigned Changes = 0;
1007 /* Walk over the entries */
1009 while (I < CS_GetEntryCount (S)) {
1015 /* Get next entry */
1016 CodeEntry* E = CS_GetEntry (S, I);
1018 /* Check if it is a store instruction followed by a load from the
1019 * same address which is itself not followed by a conditional branch.
1021 if ((E->Info & OF_XFR) != 0 &&
1022 (N = CS_GetNextEntry (S, I)) != 0 &&
1024 (N->Info & OF_XFR) != 0) {
1026 /* Check if it's a transfer and back */
1027 if ((E->OPC == OP65_TAX && N->OPC == OP65_TXA && !RegXUsed (S, I+2)) ||
1028 (E->OPC == OP65_TAY && N->OPC == OP65_TYA && !RegYUsed (S, I+2)) ||
1029 (E->OPC == OP65_TXA && N->OPC == OP65_TAX && !RegAUsed (S, I+2)) ||
1030 (E->OPC == OP65_TYA && N->OPC == OP65_TAY && !RegAUsed (S, I+1))) {
1032 /* If the next insn is a conditional branch, check if the insn
1033 * preceeding the first xfr will set the flags right, otherwise we
1034 * may not remove the sequence.
1036 if ((X = CS_GetNextEntry (S, I+1)) == 0) {
1039 if ((X->Info & OF_FBRA) != 0) {
1041 /* No preceeding entry */
1044 P = CS_GetEntry (S, I-1);
1045 if ((P->Info & OF_SETF) == 0) {
1046 /* Does not set the flags */
1051 /* Remove both transfers */
1052 CS_DelEntry (S, I+1);
1055 /* Remember, we had changes */
1066 /* Return the number of changes made */
1072 /*****************************************************************************/
1073 /* Optimize branch types */
1074 /*****************************************************************************/
1078 unsigned OptBranchDist (CodeSeg* S)
1079 /* Change branches for the distance needed. */
1081 unsigned Changes = 0;
1084 /* Get the number of entries, bail out if we have not enough */
1085 unsigned Count = CS_GetEntryCount (S);
1087 /* Walk over the entries */
1091 /* Get next entry */
1092 CodeEntry* E = CS_GetEntry (S, I);
1094 /* Check if it's a conditional branch to a local label. */
1095 if (E->Info & OF_CBRA) {
1097 /* Is this a branch to a local symbol? */
1098 if (E->JumpTo != 0) {
1100 /* Check if the branch distance is short */
1101 int IsShort = IsShortDist (GetBranchDist (S, I, E->JumpTo->Owner));
1103 /* Make the branch short/long according to distance */
1104 if ((E->Info & OF_LBRA) == 0 && !IsShort) {
1105 /* Short branch but long distance */
1106 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
1108 } else if ((E->Info & OF_LBRA) != 0 && IsShort) {
1109 /* Long branch but short distance */
1110 CE_ReplaceOPC (E, MakeShortBranch (E->OPC));
1114 } else if ((E->Info & OF_LBRA) == 0) {
1116 /* Short branch to external symbol - make it long */
1117 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
1122 } else if (CPU == CPU_65C02 &&
1123 (E->Info & OF_UBRA) != 0 &&
1125 IsShortDist (GetBranchDist (S, I, E->JumpTo->Owner))) {
1127 /* The jump is short and may be replaced by a BRA on the 65C02 CPU */
1128 CE_ReplaceOPC (E, OP65_BRA);
1137 /* Return the number of changes made */