* src/SDCCpeeph.c (labelIsReturnOnly): fixed bug 1464657
[fw/sdcc] / src / mcs51 / gen.c
index afce9d527460cb594b5553b8b965cc18b0e50fc1..e8b5afaba5abf93112addd9f882e2f8953a2e29e 100644 (file)
@@ -472,6 +472,7 @@ newAsmop (short type)
 
   aop = Safe_calloc (1, sizeof (asmop));
   aop->type = type;
+  aop->allocated = 1;
   return aop;
 }
 
@@ -568,7 +569,10 @@ aopForSym (iCode * ic, symbol * sym, bool result)
 
   /* if already has one */
   if (sym->aop)
-    return sym->aop;
+    {
+         sym->aop->allocated++;
+      return sym->aop;
+    }
 
   /* assign depending on the storage class */
   /* if it is on the stack or indirectly addressable */
@@ -805,6 +809,24 @@ operandsEqu (operand * op1, operand * op2)
   return FALSE;
 }
 
+/*-----------------------------------------------------------------*/
+/* sameReg - two asmops have the same register at given offsets    */
+/*-----------------------------------------------------------------*/
+static bool
+sameReg (asmop * aop1, int off1, asmop * aop2, int off2)
+{
+  if (aop1->type != AOP_REG && aop1->type != AOP_CRY)
+    return FALSE;
+
+  if (aop1->type != aop2->type)
+    return FALSE;
+
+  if (aop1->aopu.aop_reg[off1] != aop2->aopu.aop_reg[off2])
+    return FALSE;
+
+  return TRUE;
+}
+
 /*-----------------------------------------------------------------*/
 /* sameRegs - two asmops have the same registers                   */
 /*-----------------------------------------------------------------*/
@@ -826,8 +848,7 @@ sameRegs (asmop * aop1, asmop * aop2)
     return FALSE;
 
   for (i = 0; i < aop1->size; i++)
-    if (aop1->aopu.aop_reg[i] !=
-        aop2->aopu.aop_reg[i])
+    if (aop1->aopu.aop_reg[i] != aop2->aopu.aop_reg[i])
       return FALSE;
 
   return TRUE;
@@ -856,13 +877,17 @@ aopOp (operand * op, iCode * ic, bool result)
     }
 
   /* if already has a asmop then continue */
-  if (op->aop )
-    return;
+  if (op->aop)
+    {
+         op->aop->allocated++;
+      return;
+    }
 
   /* if the underlying symbol has a aop */
   if (IS_SYMOP (op) && OP_SYMBOL (op)->aop)
     {
       op->aop = OP_SYMBOL (op)->aop;
+         op->aop->allocated++;
       return;
     }
 
@@ -979,13 +1004,13 @@ freeAsmop (operand * op, asmop * aaop, iCode * ic, bool pop)
   if (!aop)
     return;
 
-  if (aop->freed)
-    goto dealloc;
+  aop->allocated--;
 
-  aop->freed = 1;
+  if (aop->allocated)
+    goto dealloc;
 
-  /* depending on the asmop type only three cases need work AOP_RO
-     , AOP_R1 && AOP_STK */
+  /* depending on the asmop type only three cases need work
+     AOP_R0, AOP_R1 & AOP_STK */
   switch (aop->type)
     {
     case AOP_R0:
@@ -1098,7 +1123,7 @@ freeForBranchAsmop (operand * op)
   if (!aop)
     return;
 
-  if (aop->freed)
+  if (!aop->allocated)
     return;
 
   switch (aop->type)
@@ -1809,8 +1834,8 @@ genNot (iCode * ic)
 
 release:
   /* release the aops */
-  freeAsmop (IC_LEFT (ic), NULL, ic, (RESULTONSTACK (ic) ? 0 : 1));
   freeAsmop (IC_RESULT (ic), NULL, ic, TRUE);
+  freeAsmop (IC_LEFT (ic), NULL, ic, (RESULTONSTACK (ic) ? 0 : 1));
 }
 
 
@@ -1877,8 +1902,8 @@ genCpl (iCode * ic)
 
 release:
   /* release the aops */
-  freeAsmop (IC_LEFT (ic), NULL, ic, (RESULTONSTACK (ic) ? 0 : 1));
   freeAsmop (IC_RESULT (ic), NULL, ic, TRUE);
+  freeAsmop (IC_LEFT (ic), NULL, ic, (RESULTONSTACK (ic) ? 0 : 1));
 }
 
 /*-----------------------------------------------------------------*/
@@ -1987,8 +2012,8 @@ genUminus (iCode * ic)
 
 release:
   /* release the aops */
-  freeAsmop (IC_LEFT (ic), NULL, ic, (RESULTONSTACK (ic) ? 0 : 1));
   freeAsmop (IC_RESULT (ic), NULL, ic, TRUE);
+  freeAsmop (IC_LEFT (ic), NULL, ic, (RESULTONSTACK (ic) ? 0 : 1));
 }
 
 /*-----------------------------------------------------------------*/
@@ -2585,12 +2610,12 @@ static void genSend(set *sendSet)
   for (sic = setFirstItem (sendSet); sic;
        sic = setNextItem (sendSet))
     {
-      aopOp (IC_LEFT (sic), sic, FALSE);
-
       if (sic->argreg > 12)
         {
           int bit = sic->argreg-13;
 
+          aopOp (IC_LEFT (sic), sic, FALSE);
+
           /* if left is a literal then
              we know what the value is */
           if (AOP_TYPE (IC_LEFT (sic)) == AOP_LIT)
@@ -2617,8 +2642,9 @@ static void genSend(set *sendSet)
             }
           bit_count++;
           BitBankUsed = 1;
+
+          freeAsmop (IC_LEFT (sic), NULL, sic, TRUE);
         }
-      freeAsmop (IC_LEFT (sic), NULL, sic, TRUE);
     }
 
   if (bit_count)
@@ -2631,30 +2657,33 @@ static void genSend(set *sendSet)
   for (sic = setFirstItem (sendSet); sic;
        sic = setNextItem (sendSet))
     {
-      int size, offset = 0;
-      aopOp (IC_LEFT (sic), sic, FALSE);
-      size = AOP_SIZE (IC_LEFT (sic));
-
-      if (sic->argreg == 1)
+      if (sic->argreg <= 12)
         {
-          while (size--)
+          int size, offset = 0;
+          aopOp (IC_LEFT (sic), sic, FALSE);
+          size = AOP_SIZE (IC_LEFT (sic));
+
+          if (sic->argreg == 1)
             {
-              char *l = aopGet (IC_LEFT (sic), offset, FALSE, FALSE);
-              if (strcmp (l, fReturn[offset]))
-                  emitcode ("mov", "%s,%s", fReturn[offset], l);
-              offset++;
+              while (size--)
+                {
+                  char *l = aopGet (IC_LEFT (sic), offset, FALSE, FALSE);
+                  if (strcmp (l, fReturn[offset]))
+                      emitcode ("mov", "%s,%s", fReturn[offset], l);
+                  offset++;
+                }
             }
-        }
-      else if (sic->argreg <= 12)
-        {
-          while (size--)
+          else
             {
-              emitcode ("mov","%s,%s", rb1regs[sic->argreg+offset-5],
-                        aopGet (IC_LEFT (sic), offset,FALSE, FALSE));
-              offset++;
+              while (size--)
+                {
+                  emitcode ("mov","%s,%s", rb1regs[sic->argreg+offset-5],
+                            aopGet (IC_LEFT (sic), offset,FALSE, FALSE));
+                  offset++;
+                }
             }
+          freeAsmop (IC_LEFT (sic), NULL, sic, TRUE);
         }
-      freeAsmop (IC_LEFT (sic), NULL, sic, TRUE);
     }
 }
 
@@ -2690,6 +2719,7 @@ genCall (iCode * ic)
   bool accuse = FALSE;
   bool accPushed = FALSE;
   bool resultInF0 = FALSE;
+  bool assignResultGenerated = FALSE;
 
   D(emitcode(";     genCall",""));
 
@@ -2772,6 +2802,7 @@ genCall (iCode * ic)
       _G.accInUse--;
 
       accuse = assignResultValue (IC_RESULT (ic), IC_LEFT (ic));
+      assignResultGenerated = TRUE;
 
       freeAsmop (IC_RESULT (ic), NULL, ic, TRUE);
     }
@@ -2788,7 +2819,8 @@ genCall (iCode * ic)
               accPushed = TRUE;
             }
           if (IS_BIT (OP_SYM_ETYPE (IC_LEFT (ic))) &&
-              IS_BIT (OP_SYM_ETYPE (IC_RESULT (ic))))
+              IS_BIT (OP_SYM_ETYPE (IC_RESULT (ic))) &&
+              !assignResultGenerated)
             {
               emitcode ("mov", "F0,c");
               resultInF0 = TRUE;
@@ -2827,7 +2859,7 @@ genCall (iCode * ic)
 //  if (restoreBank)
 //    unsaveRBank (FUNC_REGBANK (dtype), ic, FALSE);
 
-  if (IS_BIT (OP_SYM_ETYPE (IC_RESULT (ic))))
+  if (IS_BIT (OP_SYM_ETYPE (IC_RESULT (ic))) && !assignResultGenerated)
     {
       if (resultInF0)
           emitcode ("mov", "c,F0");
@@ -4229,6 +4261,7 @@ genPlus (iCode * ic)
   int size, offset = 0;
   int skip_bytes = 0;
   char *add = "add";
+  bool swappedLR = FALSE;
   operand *leftOp, *rightOp;
   operand * op;
 
@@ -4250,6 +4283,7 @@ genPlus (iCode * ic)
       operand *t = IC_RIGHT (ic);
       IC_RIGHT (ic) = IC_LEFT (ic);
       IC_LEFT (ic) = t;
+         swappedLR = TRUE;
     }
 
   /* if both left & right are in bit
@@ -4294,7 +4328,7 @@ genPlus (iCode * ic)
   size = getDataSize (IC_RESULT (ic));
   leftOp = IC_LEFT(ic);
   rightOp = IC_RIGHT(ic);
-  op=IC_LEFT(ic);
+  op = IC_LEFT(ic);
 
   /* if this is an add for an array access
      at a 256 byte boundary */
@@ -4385,9 +4419,17 @@ genPlus (iCode * ic)
   adjustArithmeticResult (ic);
 
 release:
-  freeAsmop (IC_RIGHT (ic), NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
-  freeAsmop (IC_LEFT (ic), NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
   freeAsmop (IC_RESULT (ic), NULL, ic, TRUE);
+  if (!swappedLR)
+    {
+      freeAsmop (IC_RIGHT (ic), NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
+      freeAsmop (IC_LEFT (ic), NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
+    }
+  else
+    {
+      freeAsmop (IC_LEFT (ic), NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
+      freeAsmop (IC_RIGHT (ic), NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
+    }
 }
 
 /*-----------------------------------------------------------------*/
@@ -4701,9 +4743,9 @@ genMinus (iCode * ic)
   adjustArithmeticResult (ic);
 
 release:
+  freeAsmop (IC_RESULT (ic), NULL, ic, TRUE);
   freeAsmop (IC_RIGHT (ic), NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
   freeAsmop (IC_LEFT (ic), NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
-  freeAsmop (IC_RESULT (ic), NULL, ic, TRUE);
 }
 
 
@@ -4969,9 +5011,9 @@ genMult (iCode * ic)
   assert (0);
 
 release:
+  freeAsmop (result, NULL, ic, TRUE);
   freeAsmop (right, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
   freeAsmop (left, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
-  freeAsmop (result, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -5255,9 +5297,9 @@ genDiv (iCode * ic)
   /* should have been converted to function call */
   assert (0);
 release:
+  freeAsmop (result, NULL, ic, TRUE);
   freeAsmop (right, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
   freeAsmop (left, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
-  freeAsmop (result, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -5526,9 +5568,9 @@ genMod (iCode * ic)
   assert (0);
 
 release:
+  freeAsmop (result, NULL, ic, TRUE);
   freeAsmop (right, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
   freeAsmop (left, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
-  freeAsmop (result, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -5727,9 +5769,9 @@ genCmpGt (iCode * ic, iCode * ifx)
   sign = !((SPEC_USIGN (letype) && !(IS_CHAR (letype) && IS_LITERAL (letype))) ||
            (SPEC_USIGN (retype) && !(IS_CHAR (retype) && IS_LITERAL (retype))));
   /* assign the amsops */
+  aopOp (result, ic, TRUE);
   aopOp (left, ic, FALSE);
   aopOp (right, ic, FALSE);
-  aopOp (result, ic, TRUE);
 
   genCmp (right, left, result, ifx, sign, ic);
 
@@ -5757,9 +5799,9 @@ genCmpLt (iCode * ic, iCode * ifx)
   sign = !((SPEC_USIGN (letype) && !(IS_CHAR (letype) && IS_LITERAL (letype))) ||
            (SPEC_USIGN (retype) && !(IS_CHAR (retype) && IS_LITERAL (retype))));
   /* assign the amsops */
+  aopOp (result, ic, TRUE);
   aopOp (left, ic, FALSE);
   aopOp (right, ic, FALSE);
-  aopOp (result, ic, TRUE);
 
   genCmp (left, right, result, ifx, sign, ic);
 
@@ -5869,6 +5911,7 @@ gencjne (operand * left, operand * right, symbol * lbl)
 static void
 genCmpEq (iCode * ic, iCode * ifx)
 {
+  bool swappedLR = FALSE;
   operand *left, *right, *result;
 
   D(emitcode (";     genCmpEq",""));
@@ -5886,6 +5929,7 @@ genCmpEq (iCode * ic, iCode * ifx)
       operand *t = IC_RIGHT (ic);
       IC_RIGHT (ic) = IC_LEFT (ic);
       IC_LEFT (ic) = t;
+         swappedLR = TRUE;
     }
 
   if (ifx && !AOP_SIZE (result))
@@ -6037,9 +6081,17 @@ genCmpEq (iCode * ic, iCode * ifx)
     }
 
 release:
-  freeAsmop (right, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
-  freeAsmop (left, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
   freeAsmop (result, NULL, ic, TRUE);
+  if (!swappedLR)
+    {
+      freeAsmop (right, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
+      freeAsmop (left, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
+    }
+  else
+    {
+      freeAsmop (left, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
+      freeAsmop (right, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
+    }
 }
 
 /*-----------------------------------------------------------------*/
@@ -6137,9 +6189,9 @@ genAndOp (iCode * ic)
       outBitAcc (result);
     }
 
+  freeAsmop (result, NULL, ic, TRUE);
   freeAsmop (right, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
   freeAsmop (left, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
-  freeAsmop (result, NULL, ic, TRUE);
 }
 
 
@@ -6179,9 +6231,9 @@ genOrOp (iCode * ic)
       outBitAcc (result);
     }
 
+  freeAsmop (result, NULL, ic, TRUE);
   freeAsmop (right, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
   freeAsmop (left, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
-  freeAsmop (result, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -6651,9 +6703,9 @@ genAnd (iCode * ic, iCode * ifx)
     }
 
 release:
+  freeAsmop (result, NULL, ic, TRUE);
   freeAsmop (right, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
   freeAsmop (left, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
-  freeAsmop (result, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -6983,9 +7035,9 @@ genOr (iCode * ic, iCode * ifx)
     }
 
 release:
+  freeAsmop (result, NULL, ic, TRUE);
   freeAsmop (right, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
   freeAsmop (left, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
-  freeAsmop (result, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -7290,9 +7342,9 @@ genXor (iCode * ic, iCode * ifx)
     }
 
 release:
+  freeAsmop (result, NULL, ic, TRUE);
   freeAsmop (right, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
   freeAsmop (left, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
-  freeAsmop (result, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -7386,8 +7438,8 @@ genRRC (iCode * ic)
   emitcode ("mov", "acc.7,c");
  release:
   aopPut (result, "a", AOP_SIZE (result) - 1, isOperandVolatile (result, FALSE));
-  freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
+  freeAsmop (left, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -7441,8 +7493,8 @@ genRLC (iCode * ic)
   emitcode ("mov", "acc.0,c");
  release:
   aopPut (result, "a", 0, isOperandVolatile (result, FALSE));
-  freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
+  freeAsmop (left, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -7474,8 +7526,8 @@ genGetHbit (iCode * ic)
       outAcc (result);
     }
 
-  freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
+  freeAsmop (left, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -7545,9 +7597,9 @@ genGetAbit (iCode * ic)
       outAcc (result);
     }
 
+  freeAsmop (result, NULL, ic, TRUE);
   freeAsmop (right, NULL, ic, TRUE);
   freeAsmop (left, NULL, ic, TRUE);
-  freeAsmop (result, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -7574,9 +7626,9 @@ genGetByte (iCode * ic)
           0,
           isOperandVolatile (result, FALSE));
 
+  freeAsmop (result, NULL, ic, TRUE);
   freeAsmop (right, NULL, ic, TRUE);
   freeAsmop (left, NULL, ic, TRUE);
-  freeAsmop (result, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -7607,9 +7659,9 @@ genGetWord (iCode * ic)
           1,
           isOperandVolatile (result, FALSE));
 
+  freeAsmop (result, NULL, ic, TRUE);
   freeAsmop (right, NULL, ic, TRUE);
   freeAsmop (left, NULL, ic, TRUE);
-  freeAsmop (result, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -7674,8 +7726,8 @@ genSwap (iCode * ic)
       wassertl(FALSE, "unsupported SWAP operand size");
     }
 
-  freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
+  freeAsmop (left, NULL, ic, TRUE);
 }
 
 
@@ -8583,8 +8635,8 @@ genLeftShiftLiteral (operand * left,
           break;
         }
     }
-  freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
+  freeAsmop (left, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -8693,8 +8745,8 @@ genLeftShift (iCode * ic)
   emitcode ("djnz", "b,%05d$", tlbl->key + 100);
   popB (pushedB);
 release:
-  freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
+  freeAsmop (left, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -8743,59 +8795,89 @@ static void
 shiftRLong (operand * left, int offl,
             operand * result, int sign)
 {
-  int isSameRegs = sameRegs (AOP (left), AOP (result));
+  bool useSameRegs = regsInCommon (left, result);
 
-  if (isSameRegs && offl>1) {
-    // we are in big trouble, but this shouldn't happen
-    werror(E_INTERNAL_ERROR, __FILE__, __LINE__);
-  }
+  if (useSameRegs && offl>1)
+    {
+      // we are in big trouble, but this shouldn't happen
+      werror(E_INTERNAL_ERROR, __FILE__, __LINE__);
+    }
 
   MOVA (aopGet (left, MSB32, FALSE, FALSE));
 
-  if (offl==MSB16) {
-    // shift is > 8
-    if (sign) {
-      emitcode ("rlc", "a");
-      emitcode ("subb", "a,acc");
-      if (isSameRegs)
-        emitcode ("xch", "a,%s", aopGet (left, MSB32, FALSE, FALSE));
-      else {
-        aopPut (result, "a", MSB32, isOperandVolatile (result, FALSE));
-        MOVA (aopGet (left, MSB32, FALSE, FALSE));
-      }
-    } else {
-      aopPut (result, zero, MSB32, isOperandVolatile (result, FALSE));
+  if (offl==MSB16)
+    {
+      // shift is > 8
+      if (sign)
+           {
+          emitcode ("rlc", "a");
+          emitcode ("subb", "a,acc");
+          if (useSameRegs && sameReg (AOP (left), MSB32, AOP (result), MSB32))
+                   {
+              emitcode ("xch", "a,%s", aopGet (left, MSB32, FALSE, FALSE));
+                   }
+          else
+                   {
+              aopPut (result, "a", MSB32, isOperandVolatile (result, FALSE));
+              MOVA (aopGet (left, MSB32, FALSE, FALSE));
+                   }
+           }
+         else
+           {
+          aopPut (result, zero, MSB32, isOperandVolatile (result, FALSE));
+           }
     }
-  }
 
-  if (!sign) {
-    emitcode ("clr", "c");
-  } else {
-    emitcode ("mov", "c,acc.7");
-  }
+  if (!sign)
+    {
+      emitcode ("clr", "c");
+    }
+  else
+    {
+      emitcode ("mov", "c,acc.7");
+    }
 
   emitcode ("rrc", "a");
 
-  if (isSameRegs && offl==MSB16) {
-    emitcode ("xch", "a,%s",aopGet (left, MSB24, FALSE, FALSE));
-  } else {
-    aopPut (result, "a", MSB32-offl, isOperandVolatile (result, FALSE));
-    MOVA (aopGet (left, MSB24, FALSE, FALSE));
-  }
+  if (useSameRegs && offl==MSB16 &&
+      sameReg (AOP (left), MSB24, AOP (result), MSB32-offl))
+    {
+      emitcode ("xch", "a,%s",aopGet (left, MSB24, FALSE, FALSE));
+    }
+  else
+    {
+      aopPut (result, "a", MSB32-offl, isOperandVolatile (result, FALSE));
+      MOVA (aopGet (left, MSB24, FALSE, FALSE));
+    }
 
   emitcode ("rrc", "a");
-  if (isSameRegs && offl==1) {
-    emitcode ("xch", "a,%s",aopGet (left, MSB16, FALSE, FALSE));
-  } else {
-    aopPut (result, "a", MSB24-offl, isOperandVolatile (result, FALSE));
-    MOVA (aopGet (left, MSB16, FALSE, FALSE));
-  }
+  if (useSameRegs && offl==1 &&
+      sameReg (AOP (left), MSB16, AOP (result), MSB24-offl))
+    {
+      emitcode ("xch", "a,%s",aopGet (left, MSB16, FALSE, FALSE));
+    }
+  else
+    {
+      aopPut (result, "a", MSB24-offl, isOperandVolatile (result, FALSE));
+      MOVA (aopGet (left, MSB16, FALSE, FALSE));
+    }
   emitcode ("rrc", "a");
-  aopPut (result, "a", MSB16 - offl, isOperandVolatile (result, FALSE));
-
-  if (offl == LSB)
+  if (offl != LSB)
+    {
+      aopPut (result, "a", MSB16 - offl, isOperandVolatile (result, FALSE));
+    }
+  else
     {
-      MOVA (aopGet (left, LSB, FALSE, FALSE));
+      if (useSameRegs &&
+          sameReg (AOP (left), LSB, AOP (result), MSB16-offl))
+        {
+          emitcode ("xch", "a,%s",aopGet (left, LSB, FALSE, FALSE));
+        }
+      else
+        {
+          aopPut (result, "a", MSB16 - offl, isOperandVolatile (result, FALSE));
+          MOVA (aopGet (left, LSB, FALSE, FALSE));
+           }
       emitcode ("rrc", "a");
       aopPut (result, "a", LSB, isOperandVolatile (result, FALSE));
     }
@@ -8933,8 +9015,8 @@ genRightShiftLiteral (operand * left,
           break;
         }
     }
-  freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
+  freeAsmop (left, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -9044,8 +9126,8 @@ genSignedRightShift (iCode * ic)
   popB (pushedB);
 
 release:
-  freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
+  freeAsmop (left, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -9169,8 +9251,8 @@ genRightShift (iCode * ic)
   popB (pushedB);
 
 release:
-  freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
+  freeAsmop (left, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -9396,8 +9478,8 @@ genDataPointerGet (operand * left,
       aopPut (result, buffer, offset++, isOperandVolatile (result, FALSE));
     }
 
-  freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
+  freeAsmop (left, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -9633,10 +9715,9 @@ genPagedPointerGet (operand * left,
     }
 
   /* done */
-  freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
+  freeAsmop (left, NULL, ic, TRUE);
   if (pi) pi->generated = 1;
-
 }
 
 /*--------------------------------------------------------------------*/
@@ -9747,8 +9828,8 @@ genFarPointerGet (operand * left,
       genIfxJump (ifx, "a", left, NULL, result);
     }
 
-  freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
+  freeAsmop (left, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -9809,8 +9890,8 @@ genCodePointerGet (operand * left,
       genIfxJump (ifx, "a", left, NULL, result);
     }
 
-  freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
+  freeAsmop (left, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -9861,9 +9942,8 @@ genGenPointerGet (operand * left,
       genIfxJump (ifx, "a", left, NULL, result);
     }
 
-
-  freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
+  freeAsmop (left, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -10097,8 +10177,8 @@ genDataPointerSet (operand * right,
                 aopGet (right, offset++, FALSE, FALSE));
     }
 
-  freeAsmop (right, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
+  freeAsmop (right, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -10321,8 +10401,6 @@ genPagedPointerSet (operand * right,
   if (pi) pi->generated = 1;
   freeAsmop (result, NULL, ic, TRUE);
   freeAsmop (right, NULL, ic, TRUE);
-
-
 }
 
 /*-----------------------------------------------------------------*/
@@ -10621,7 +10699,6 @@ genFarFarAssign (operand * result, operand * right, iCode * ic)
       aopPut (result, "a", --offset, isOperandVolatile (result, FALSE));
     }
   freeAsmop (result, NULL, ic, FALSE);
-
 }
 
 /*-----------------------------------------------------------------*/
@@ -10736,8 +10813,8 @@ genAssign (iCode * ic)
     }
 
 release:
-  freeAsmop (right, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
+  freeAsmop (right, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -10759,15 +10836,24 @@ genJumpTab (iCode * ic)
       /* this algorithm needs 9 cycles and 7 + 3*n bytes
          if the switch argument is in a register.
          (8 cycles and 6+2*n bytes if peepholes can change ljmp to sjmp) */
-      /* (MB) What if peephole converts ljmp to sjmp or ret ???
-         How will multiply by three be updated ???*/
+      /* Peephole may not convert ljmp to sjmp or ret
+         labelIsReturnOnly & labelInRange must check
+         currPl->ic->op != JUMPTABLE */
       aopOp (IC_JTCOND (ic), ic, FALSE);
       /* get the condition into accumulator */
       l = aopGet (IC_JTCOND (ic), 0, FALSE, FALSE);
       MOVA (l);
       /* multiply by three */
-      emitcode ("add", "a,acc");
-      emitcode ("add", "a,%s", aopGet (IC_JTCOND (ic), 0, FALSE, FALSE));
+      if (aopGetUsesAcc (IC_JTCOND (ic), 0))
+        {
+          emitcode ("mov", "b,#3");
+          emitcode ("mul", "ab");
+        }
+      else
+        {
+          emitcode ("add", "a,acc");
+          emitcode ("add", "a,%s", aopGet (IC_JTCOND (ic), 0, FALSE, FALSE));
+        }
       freeAsmop (IC_JTCOND (ic), NULL, ic, TRUE);
 
       jtab = newiTempLabel (NULL);
@@ -11021,9 +11107,8 @@ genCast (iCode * ic)
   /* we are done hurray !!!! */
 
 release:
-  freeAsmop (right, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
-
+  freeAsmop (right, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/