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 &&
219 E->JumpTo->Owner == CS_GetNextEntry (S, I)) {
221 /* Delete the dead jump */
224 /* Remember, we had changes */
235 /* Return the number of changes made */
241 /*****************************************************************************/
242 /* Remove dead code */
243 /*****************************************************************************/
247 unsigned OptDeadCode (CodeSeg* S)
248 /* Remove dead code (code that follows an unconditional jump or an rts/rti
252 unsigned Changes = 0;
254 /* Walk over all entries */
256 while (I < CS_GetEntryCount (S)) {
261 CodeEntry* E = CS_GetEntry (S, I);
263 /* Check if it's an unconditional branch, and if the next entry has
266 if ((E->Info & OF_DEAD) != 0 &&
267 (N = CS_GetNextEntry (S, I)) != 0 &&
270 /* Delete the next entry */
271 CS_DelEntry (S, I+1);
273 /* Remember, we had changes */
284 /* Return the number of changes made */
290 /*****************************************************************************/
291 /* Optimize jump cascades */
292 /*****************************************************************************/
296 unsigned OptJumpCascades (CodeSeg* S)
297 /* Optimize jump cascades (jumps to jumps). In such a case, the jump is
298 * replaced by a jump to the final location. This will in some cases produce
299 * worse code, because some jump targets are no longer reachable by short
300 * branches, but this is quite rare, so there are more advantages than
304 unsigned Changes = 0;
306 /* Walk over all entries */
308 while (I < CS_GetEntryCount (S)) {
314 CodeEntry* E = CS_GetEntry (S, I);
316 /* Check if it's a branch, if it has a jump label, if this jump
317 * label is not attached to the instruction itself, and if the
318 * target instruction is itself a branch.
320 if ((E->Info & OF_BRA) != 0 &&
321 (OldLabel = E->JumpTo) != 0 &&
322 (N = OldLabel->Owner) != E &&
323 (N->Info & OF_BRA) != 0) {
325 /* Check if we can use the final target label. This is the case,
326 * if the target branch is an absolut branch, or if it is a
327 * conditional branch checking the same condition as the first one.
329 if ((N->Info & OF_UBRA) != 0 ||
330 ((E->Info & OF_CBRA) != 0 &&
331 GetBranchCond (E->OPC) == GetBranchCond (N->OPC))) {
333 /* This is a jump cascade and we may jump to the final target.
334 * Insert a new instruction, then remove the old one
336 CodeEntry* X = NewCodeEntry (E->OPC, E->AM, N->Arg, N->JumpTo, E->LI);
338 /* Insert it behind E */
339 CS_InsertEntry (S, X, I+1);
344 /* Remember, we had changes */
352 /* Check if both are conditional branches, and the condition of
353 * the second is the inverse of that of the first. In this case,
354 * the second branch will never be taken, and we may jump directly
355 * to the instruction behind this one.
357 if ((E->Info & OF_CBRA) != 0 && (N->Info & OF_CBRA) != 0) {
359 CodeEntry* X; /* Instruction behind N */
360 CodeLabel* LX; /* Label attached to X */
362 /* Get the branch conditions of both branches */
363 bc_t BC1 = GetBranchCond (E->OPC);
364 bc_t BC2 = GetBranchCond (N->OPC);
366 /* Check the branch conditions */
367 if (BC1 != GetInverseCond (BC2)) {
368 /* Condition not met */
372 /* We may jump behind this conditional branch. Get the
373 * pointer to the next instruction
375 if ((X = CS_GetNextEntry (S, CS_GetEntryIndex (S, N))) == 0) {
376 /* N is the last entry, bail out */
380 /* Get the label attached to X, create a new one if needed */
381 LX = CS_GenLabel (S, X);
383 /* Move the reference from E to the new label */
384 CS_MoveLabelRef (S, E, LX);
386 /* Remember, we had changes */
401 /* Return the number of changes made */
407 /*****************************************************************************/
408 /* Optimize jsr/rts */
409 /*****************************************************************************/
413 unsigned OptRTS (CodeSeg* S)
414 /* Optimize subroutine calls followed by an RTS. The subroutine call will get
415 * replaced by a jump. Don't bother to delete the RTS if it does not have a
416 * label, the dead code elimination should take care of it.
419 unsigned Changes = 0;
421 /* Walk over all entries minus the last one */
423 while (I < CS_GetEntryCount (S)) {
428 CodeEntry* E = CS_GetEntry (S, I);
430 /* Check if it's a subroutine call and if the following insn is RTS */
431 if (E->OPC == OP65_JSR &&
432 (N = CS_GetNextEntry (S, I)) != 0 &&
433 N->OPC == OP65_RTS) {
435 /* Change the jsr to a jmp and use the additional info for a jump */
437 CE_ReplaceOPC (E, OP65_JMP);
439 /* Remember, we had changes */
449 /* Return the number of changes made */
455 /*****************************************************************************/
456 /* Optimize jump targets */
457 /*****************************************************************************/
461 unsigned OptJumpTarget (CodeSeg* S)
462 /* If the instruction preceeding an unconditional branch is the same as the
463 * instruction preceeding the jump target, the jump target may be moved
464 * one entry back. This is a size optimization, since the instruction before
465 * the branch gets removed.
468 unsigned Changes = 0;
469 CodeEntry* E1; /* Entry 1 */
470 CodeEntry* E2; /* Entry 2 */
471 CodeEntry* T1; /* Jump target entry 1 */
472 CodeLabel* TL1; /* Target label 1 */
474 /* Walk over the entries */
476 while (I < CS_GetEntryCount (S)) {
479 E2 = CS_GetNextEntry (S, I);
481 /* Check if we have a jump or branch, and a matching label */
482 if (E2 && (E2->Info & OF_UBRA) != 0 && E2->JumpTo) {
484 /* Get the entry preceeding the branch target */
485 T1 = CS_GetPrevEntry (S, CS_GetEntryIndex (S, E2->JumpTo->Owner));
487 /* There is no such entry */
491 /* Get the entry preceeding the jump */
492 E1 = CS_GetEntry (S, I);
494 /* Check if both preceeding instructions are identical */
495 if (!CodeEntriesAreEqual (E1, T1)) {
496 /* Not equal, try next */
500 /* Get the label for the instruction preceeding the jump target.
501 * This routine will create a new label if the instruction does
502 * not already have one.
504 TL1 = CS_GenLabel (S, T1);
506 /* Change the jump target to point to this new label */
507 CS_MoveLabelRef (S, E2, TL1);
509 /* If the instruction preceeding the jump has labels attached,
510 * move references to this label to the new label.
512 if (CE_HasLabel (E1)) {
513 CS_MoveLabels (S, E1, T1);
516 /* Remove the entry preceeding the jump */
519 /* Remember, we had changes */
530 /* Return the number of changes made */
536 /*****************************************************************************/
537 /* Optimize conditional branches */
538 /*****************************************************************************/
542 unsigned OptCondBranches (CodeSeg* S)
543 /* Performs several optimization steps:
545 * - If an immidiate load of a register is followed by a conditional jump that
546 * is never taken because the load of the register sets the flags in such a
547 * manner, remove the conditional branch.
548 * - If the conditional branch is always taken because of the register load,
549 * replace it by a jmp.
550 * - If a conditional branch jumps around an unconditional branch, remove the
551 * conditional branch and make the jump a conditional branch with the
552 * inverse condition of the first one.
555 unsigned Changes = 0;
557 /* Walk over the entries */
559 while (I < CS_GetEntryCount (S)) {
565 CodeEntry* E = CS_GetEntry (S, I);
567 /* Check if it's a register load */
568 if ((E->Info & OF_LOAD) != 0 && /* It's a load instruction */
569 E->AM == AM65_IMM && /* ..with immidiate addressing */
570 (E->Flags & CEF_NUMARG) != 0 && /* ..and a numeric argument. */
571 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
572 (N->Info & OF_CBRA) != 0 && /* ..which is a conditional branch */
573 !CE_HasLabel (N)) { /* ..and does not have a label */
575 /* Get the branch condition */
576 bc_t BC = GetBranchCond (N->OPC);
578 /* Check the argument against the branch condition */
579 if ((BC == BC_EQ && E->Num != 0) ||
580 (BC == BC_NE && E->Num == 0) ||
581 (BC == BC_PL && (E->Num & 0x80) != 0) ||
582 (BC == BC_MI && (E->Num & 0x80) == 0)) {
584 /* Remove the conditional branch */
585 CS_DelEntry (S, I+1);
587 /* Remember, we had changes */
590 } else if ((BC == BC_EQ && E->Num == 0) ||
591 (BC == BC_NE && E->Num != 0) ||
592 (BC == BC_PL && (E->Num & 0x80) == 0) ||
593 (BC == BC_MI && (E->Num & 0x80) != 0)) {
595 /* The branch is always taken, replace it by a jump */
596 CE_ReplaceOPC (N, OP65_JMP);
598 /* Remember, we had changes */
604 if ((E->Info & OF_CBRA) != 0 && /* It's a conditional branch */
605 (L = E->JumpTo) != 0 && /* ..referencing a local label */
606 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a following entry */
607 (N->Info & OF_UBRA) != 0 && /* ..which is an uncond branch, */
608 !CE_HasLabel (N) && /* ..has no label attached */
609 L->Owner == CS_GetNextEntry (S, I+1)) {/* ..and jump target follows */
611 /* Replace the jump by a conditional branch with the inverse branch
612 * condition than the branch around it.
614 CE_ReplaceOPC (N, GetInverseBranch (E->OPC));
616 /* Remove the conditional branch */
619 /* Remember, we had changes */
629 /* Return the number of changes made */
635 /*****************************************************************************/
636 /* Remove unused loads and stores */
637 /*****************************************************************************/
641 unsigned OptUnusedLoads (CodeSeg* S)
642 /* Remove loads of registers where the value loaded is not used later. */
644 unsigned Changes = 0;
646 /* Walk over the entries */
648 while (I < CS_GetEntryCount (S)) {
653 CodeEntry* E = CS_GetEntry (S, I);
655 /* Check if it's a register load or transfer insn */
656 if ((E->Info & (OF_LOAD | OF_XFR | OF_REG_INCDEC)) != 0 &&
657 (N = CS_GetNextEntry (S, I)) != 0 &&
658 (N->Info & OF_FBRA) == 0) {
660 /* Check which sort of load or transfer it is */
667 case OP65_TYA: R = REG_A; break;
671 case OP65_TAX: R = REG_X; break;
675 case OP65_TAY: R = REG_Y; break;
676 default: goto NextEntry; /* OOPS */
679 /* Get register usage and check if the register value is used later */
680 if ((GetRegInfo (S, I+1, R) & R) == 0) {
682 /* Register value is not used, remove the load */
685 /* Remember, we had changes */
697 /* Return the number of changes made */
703 unsigned OptUnusedStores (CodeSeg* S)
704 /* Remove stores into zero page registers that aren't used later */
706 unsigned Changes = 0;
708 /* Walk over the entries */
710 while (I < CS_GetEntryCount (S)) {
713 CodeEntry* E = CS_GetEntry (S, I);
715 /* Check if it's a register load or transfer insn */
716 if ((E->Info & OF_STORE) != 0 &&
718 (E->Chg & REG_ZP) != 0) {
720 /* Check for the zero page location. We know that there cannot be
721 * more than one zero page location involved in the store.
723 unsigned R = E->Chg & REG_ZP;
725 /* Get register usage and check if the register value is used later */
726 if ((GetRegInfo (S, I+1, R) & R) == 0) {
728 /* Register value is not used, remove the load */
731 /* Remember, we had changes */
742 /* Return the number of changes made */
748 unsigned OptDupLoads (CodeSeg* S)
749 /* Remove loads of registers where the value loaded is already in the register. */
751 unsigned Changes = 0;
754 /* Generate register info for this step */
757 /* Walk over the entries */
759 while (I < CS_GetEntryCount (S)) {
764 CodeEntry* E = CS_GetEntry (S, I);
766 /* Assume we won't delete the entry */
769 /* Get a pointer to the input registers of the insn */
770 const RegContents* In = &E->RI->In;
772 /* Handle the different instructions */
776 if (In->RegA >= 0 && /* Value of A is known */
777 CE_KnownImm (E) && /* Value to be loaded is known */
778 In->RegA == (long) E->Num && /* Both are equal */
779 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
780 (N->Info & OF_FBRA) == 0) { /* Which is not a cond branch */
786 if (In->RegX >= 0 && /* Value of X is known */
787 CE_KnownImm (E) && /* Value to be loaded is known */
788 In->RegX == (long) E->Num && /* Both are equal */
789 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
790 (N->Info & OF_FBRA) == 0) { /* Which is not a cond branch */
796 if (In->RegY >= 0 && /* Value of Y is known */
797 CE_KnownImm (E) && /* Value to be loaded is known */
798 In->RegY == (long) E->Num && /* Both are equal */
799 (N = CS_GetNextEntry (S, I)) != 0 && /* There is a next entry */
800 (N->Info & OF_FBRA) == 0) { /* Which is not a cond branch */
806 /* If we store into a known zero page location, and this
807 * location does already contain the value to be stored,
810 if (In->RegA >= 0 && /* Value of A is known */
811 E->AM == AM65_ZP && /* Store into zp */
812 (((E->Chg & REG_SREG_LO) != 0 && /* Store into sreg */
813 In->RegA == In->SRegLo) || /* Value identical */
814 ((E->Chg & REG_SREG_HI) != 0 && /* Store into sreg+1 */
815 In->RegA == In->SRegHi))) { /* Value identical */
821 /* If we store into a known zero page location, and this
822 * location does already contain the value to be stored,
825 if (In->RegX >= 0 && /* Value of A is known */
826 E->AM == AM65_ZP && /* Store into zp */
827 (((E->Chg & REG_SREG_LO) != 0 && /* Store into sreg */
828 In->RegX == In->SRegLo) || /* Value identical */
829 ((E->Chg & REG_SREG_HI) != 0 && /* Store into sreg+1 */
830 In->RegX == In->SRegHi))) { /* Value identical */
833 /* If the value in the X register is known and the same as
834 * that in the A register, replace the store by a STA. The
835 * optimizer will then remove the load instruction for X
836 * later. STX does support the zeropage,y addressing mode,
837 * so be sure to check for that.
839 } else if (In->RegX >= 0 &&
840 In->RegX == In->RegA &&
841 E->AM != AM65_ABSY &&
843 /* Use the A register instead */
844 CE_ReplaceOPC (E, OP65_STA);
849 /* If we store into a known zero page location, and this
850 * location does already contain the value to be stored,
853 if (In->RegX >= 0 && /* Value of A is known */
854 E->AM == AM65_ZP && /* Store into zp */
855 (((E->Chg & REG_SREG_LO) != 0 && /* Store into sreg */
856 In->RegX == In->SRegLo) || /* Value identical */
857 ((E->Chg & REG_SREG_HI) != 0 && /* Store into sreg+1 */
858 In->RegX == In->SRegHi))) { /* Value identical */
860 /* If the value in the Y register is known and the same as
861 * that in the A register, replace the store by a STA. The
862 * optimizer will then remove the load instruction for Y
863 * later. If replacement by A is not possible try a
864 * replacement by X, but check for invalid addressing modes
867 } else if (In->RegY >= 0) {
868 if (In->RegY == In->RegA) {
869 CE_ReplaceOPC (E, OP65_STA);
870 } else if (In->RegY == In->RegX &&
871 E->AM != AM65_ABSX &&
873 CE_ReplaceOPC (E, OP65_STX);
880 In->RegA == In->RegX &&
881 (N = CS_GetNextEntry (S, I)) != 0 &&
882 (N->Info & OF_FBRA) == 0) {
883 /* Value is identical and not followed by a branch */
890 In->RegA == In->RegY &&
891 (N = CS_GetNextEntry (S, I)) != 0 &&
892 (N->Info & OF_FBRA) == 0) {
893 /* Value is identical and not followed by a branch */
900 In->RegX == In->RegA &&
901 (N = CS_GetNextEntry (S, I)) != 0 &&
902 (N->Info & OF_FBRA) == 0) {
903 /* Value is identical and not followed by a branch */
910 In->RegY == In->RegA &&
911 (N = CS_GetNextEntry (S, I)) != 0 &&
912 (N->Info & OF_FBRA) == 0) {
913 /* Value is identical and not followed by a branch */
923 /* Delete the entry if requested */
926 /* Register value is not used, remove the load */
929 /* Remember, we had changes */
941 /* Free register info */
944 /* Return the number of changes made */
950 unsigned OptStoreLoad (CodeSeg* S)
951 /* Remove a store followed by a load from the same location. */
953 unsigned Changes = 0;
955 /* Walk over the entries */
957 while (I < CS_GetEntryCount (S)) {
963 CodeEntry* E = CS_GetEntry (S, I);
965 /* Check if it is a store instruction followed by a load from the
966 * same address which is itself not followed by a conditional branch.
968 if ((E->Info & OF_STORE) != 0 &&
969 (N = CS_GetNextEntry (S, I)) != 0 &&
972 ((E->OPC == OP65_STA && N->OPC == OP65_LDA) ||
973 (E->OPC == OP65_STX && N->OPC == OP65_LDX) ||
974 (E->OPC == OP65_STY && N->OPC == OP65_LDY)) &&
975 strcmp (E->Arg, N->Arg) == 0 &&
976 (X = CS_GetNextEntry (S, I+1)) != 0 &&
977 (X->Info & OF_FBRA) == 0) {
979 /* Register has already the correct value, remove the load */
980 CS_DelEntry (S, I+1);
982 /* Remember, we had changes */
992 /* Return the number of changes made */
998 unsigned OptTransfers (CodeSeg* S)
999 /* Remove transfers from one register to another and back */
1001 unsigned Changes = 0;
1003 /* Walk over the entries */
1005 while (I < CS_GetEntryCount (S)) {
1011 /* Get next entry */
1012 CodeEntry* E = CS_GetEntry (S, I);
1014 /* Check if it is a store instruction followed by a load from the
1015 * same address which is itself not followed by a conditional branch.
1017 if ((E->Info & OF_XFR) != 0 &&
1018 (N = CS_GetNextEntry (S, I)) != 0 &&
1020 (N->Info & OF_XFR) != 0) {
1022 /* Check if it's a transfer and back */
1023 if ((E->OPC == OP65_TAX && N->OPC == OP65_TXA && !RegXUsed (S, I+2)) ||
1024 (E->OPC == OP65_TAY && N->OPC == OP65_TYA && !RegYUsed (S, I+2)) ||
1025 (E->OPC == OP65_TXA && N->OPC == OP65_TAX && !RegAUsed (S, I+2)) ||
1026 (E->OPC == OP65_TYA && N->OPC == OP65_TAY && !RegAUsed (S, I+1))) {
1028 /* If the next insn is a conditional branch, check if the insn
1029 * preceeding the first xfr will set the flags right, otherwise we
1030 * may not remove the sequence.
1032 if ((X = CS_GetNextEntry (S, I+1)) == 0) {
1035 if ((X->Info & OF_FBRA) != 0) {
1037 /* No preceeding entry */
1040 P = CS_GetEntry (S, I-1);
1041 if ((P->Info & OF_SETF) == 0) {
1042 /* Does not set the flags */
1047 /* Remove both transfers */
1048 CS_DelEntry (S, I+1);
1051 /* Remember, we had changes */
1062 /* Return the number of changes made */
1068 /*****************************************************************************/
1069 /* Optimize branch types */
1070 /*****************************************************************************/
1074 unsigned OptBranchDist (CodeSeg* S)
1075 /* Change branches for the distance needed. */
1077 unsigned Changes = 0;
1080 /* Get the number of entries, bail out if we have not enough */
1081 unsigned Count = CS_GetEntryCount (S);
1083 /* Walk over the entries */
1087 /* Get next entry */
1088 CodeEntry* E = CS_GetEntry (S, I);
1090 /* Check if it's a conditional branch to a local label. */
1091 if (E->Info & OF_CBRA) {
1093 /* Is this a branch to a local symbol? */
1094 if (E->JumpTo != 0) {
1096 /* Check if the branch distance is short */
1097 int IsShort = IsShortDist (GetBranchDist (S, I, E->JumpTo->Owner));
1099 /* Make the branch short/long according to distance */
1100 if ((E->Info & OF_LBRA) == 0 && !IsShort) {
1101 /* Short branch but long distance */
1102 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
1104 } else if ((E->Info & OF_LBRA) != 0 && IsShort) {
1105 /* Long branch but short distance */
1106 CE_ReplaceOPC (E, MakeShortBranch (E->OPC));
1110 } else if ((E->Info & OF_LBRA) == 0) {
1112 /* Short branch to external symbol - make it long */
1113 CE_ReplaceOPC (E, MakeLongBranch (E->OPC));
1118 } else if (CPU == CPU_65C02 &&
1119 (E->Info & OF_UBRA) != 0 &&
1121 IsShortDist (GetBranchDist (S, I, E->JumpTo->Owner))) {
1123 /* The jump is short and may be replaced by a BRA on the 65C02 CPU */
1124 CE_ReplaceOPC (E, OP65_BRA);
1133 /* Return the number of changes made */