]> git.sur5r.net Git - cc65/blobdiff - src/cc65/coptind.c
Warn when structs are passed by value to a function.
[cc65] / src / cc65 / coptind.c
index 80d6c772c65a56647da04e0bc68c03f65915e487..1b666402965dbab17e5f77a04d92971e82d2801e 100644 (file)
@@ -247,50 +247,77 @@ unsigned OptRTSJumps1 (CodeSeg* S)
 
 
 unsigned OptRTSJumps2 (CodeSeg* S)
-/* Replace long conditional jumps to RTS */
+/* Replace long conditional jumps to RTS or to a final target */
 {
     unsigned Changes = 0;
 
     /* Walk over all entries minus the last one */
     unsigned I = 0;
-    while (I < CS_GetEntryCount (S)) {
-
-       CodeEntry* N;
+    while (I < CS_GetEntryCount (S) - 1) {
 
        /* Get the next entry */
        CodeEntry* E = CS_GetEntry (S, I);
 
-               /* Check if it's an unconditional branch to a local target */
+               /* 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 */
-           E->JumpTo->Owner->OPC == OP65_RTS   &&   /* Target is an RTS */
-           (N = CS_GetNextEntry (S, I)) != 0) {     /* There is a next entry */
+           E->JumpTo != 0) {                        /* Local label */
 
-           CodeEntry* X;
-           CodeLabel* LN;
-           opc_t      NewBranch;
 
-           /* We will create a jump around an RTS instead of the long branch */
-           X = NewCodeEntry (OP65_RTS, AM65_IMP, 0, 0, E->JumpTo->Owner->LI);
-           CS_InsertEntry (S, X, I+1);
+            /* Get the jump target and the next entry. There's always a next
+             * entry, because we don't cover the last entry in the loop.
+             */
+            CodeEntry* X = 0;
+            CodeEntry* T = E->JumpTo->Owner;
+            CodeEntry* N = CS_GetNextEntry (S, I);
 
-           /* Get the new branch opcode */
-           NewBranch = MakeShortBranch (GetInverseBranch (E->OPC));
+            /* Check if it's a jump to an RTS insn */
+            if (T->OPC == OP65_RTS) {
 
-           /* Get the label attached to N, create a new one if needed */
-           LN = CS_GenLabel (S, N);
+                /* It's a jump to RTS. Create a conditional branch around an
+                 * RTS insn.
+                 */
+                X = NewCodeEntry (OP65_RTS, AM65_IMP, 0, 0, T->LI);
 
-           /* Generate the branch */
-           X = NewCodeEntry (NewBranch, AM65_BRA, LN->Name, LN, E->LI);
-           CS_InsertEntry (S, X, I+1);
+            } else if (T->OPC == OP65_JMP && T->JumpTo == 0) {
 
-           /* Delete the long branch */
-           CS_DelEntry (S, I);
+                /* It's a jump to a label outside the function. Create a
+                 * conditional branch around a jump to the external label.
+                 */
+                X = NewCodeEntry (OP65_JMP, AM65_ABS, T->Arg, T->JumpTo, T->LI);
 
-           /* Remember, we had changes */
-           ++Changes;
+            }
+
+            /* If we have a replacement insn, insert it */
+            if (X) {
+
+                CodeLabel* LN;
+                opc_t      NewBranch;
+
+                /* Insert the new insn */
+                CS_InsertEntry (S, X, I+1);
+
+                /* Create a conditional branch with the inverse condition
+                 * around the replacement insn
+                 */
+
+                /* Get the new branch opcode */
+                NewBranch = MakeShortBranch (GetInverseBranch (E->OPC));
+
+                /* Get the label attached to N, create a new one if needed */
+                LN = CS_GenLabel (S, N);
+
+                /* Generate the branch */
+                X = NewCodeEntry (NewBranch, AM65_BRA, LN->Name, LN, E->LI);
+                CS_InsertEntry (S, X, I+1);
 
+                /* Delete the long branch */
+                CS_DelEntry (S, I);
+
+                /* Remember, we had changes */
+                ++Changes;
+
+            }
        }
 
        /* Next entry */
@@ -430,14 +457,24 @@ unsigned OptJumpCascades (CodeSeg* S)
        /* Get this entry */
        CodeEntry* E = CS_GetEntry (S, I);
 
-               /* Check if it's a branch, if it has a jump label, if this jump
-        * label is not attached to the instruction itself, and if the
-        * target instruction is itself a branch.
+               /* Check:
+         *   - if it's a branch,
+         *   - if it has a jump label,
+         *   - if this jump label is not attached to the instruction itself,
+         *   - if the target instruction is itself a branch,
+         *   - if either the first branch is unconditional or the target of
+         *     the second branch is internal to the function.
+         * The latter condition will avoid conditional branches to targets
+         * 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
@@ -771,13 +808,16 @@ unsigned OptJumpTarget3 (CodeSeg* S)
         CodeLabel* LN = 0;
 
        /* Get next entry */
-               CodeEntry* E = CS_GetEntry (S, I);  
+               CodeEntry* E = CS_GetEntry (S, I);
 
-        /* Check if this is a load insn with a label */
+        /* 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.
+         */
         if ((E->Info & OF_LOAD) != 0            &&
             CE_IsConstImm (E)                   &&
             CE_HasLabel (E)                     &&
-            (N = CS_GetNextEntry (S, I)) != 0)    {
+            (N = CS_GetNextEntry (S, I)) != 0   &&
+            !CE_UseLoadFlags (N)) {
 
             /* Walk over all insn that jump here */
             for (J = 0; J < CE_GetLabelCount (E); ++J) {
@@ -1901,14 +1941,16 @@ unsigned OptPushPop (CodeSeg* S)
             case FoundPop:
                 /* We're at the instruction after the PLA.
                  * Check for the following conditions:
-                 *   - If this instruction is a store of A, and A is not used
-                 *     later, we may replace the PHA by the store and remove
-                 *     pla if several other conditions are met.
+                 *   - If this instruction is a store of A, does not have a
+                 *     label, and A is not used later, we may replace the PHA
+                 *     by the store and remove pla if several other conditions
+                 *     are met.
                  *   - If this instruction is not a conditional branch, and A
                  *     is either unused later, or not changed by the code
                  *     between push and pop, we may remove PHA and PLA.
                  */
                 if (E->OPC == OP65_STA                  &&
+                    !CE_HasLabel (E)                    &&
                     !RegAUsed (S, I+1)                  &&
                     !MemAccess (S, Push+1, Pop-1, E)) {