* src/SDCCpeeph.c (operandsLiteral): new, added,
[fw/sdcc] / src / mcs51 / gen.c
index 085333afe10e2874c945b4661d04d737715253d2..84729c5f2730912f6268611f98cd5dcd8406a934 100644 (file)
@@ -458,7 +458,7 @@ getTempRegs(regs **tempRegs, int size, iCode *ic)
     }
 
   freeBitVect(freeRegs);
-  return 1;
+  return 0;
 }
 
 
@@ -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)
@@ -1350,6 +1375,50 @@ aopGet (operand * oper, int offset, bool bit16, bool dname)
           "aopget got unsupported aop->type");
   exit (1);
 }
+
+/*-----------------------------------------------------------------*/
+/* aopPutUsesAcc - indicates ahead of time whether aopPut() will   */
+/*                 clobber the accumulator                         */
+/*-----------------------------------------------------------------*/
+static bool
+aopPutUsesAcc (operand * oper, const char *s, int offset)
+{
+  asmop * aop = AOP (oper);
+
+  if (offset > (aop->size - 1))
+    return FALSE;
+
+  switch (aop->type)
+    {
+    case AOP_DUMMY:
+      return TRUE;
+    case AOP_DIR:
+      return FALSE;
+    case AOP_REG:
+      wassert(strcmp(aop->aopu.aop_reg[offset]->name, "a"));
+      return FALSE;
+    case AOP_DPTR:
+      return TRUE;
+    case AOP_R0:
+    case AOP_R1:
+      return ((aop->paged) || (*s == '@'));
+    case AOP_STK:
+      return (*s == '@');
+    case AOP_CRY:
+      return (!aop->aopu.aop_dir || strcmp(s, aop->aopu.aop_dir));
+    case AOP_STR:
+      return FALSE;
+    case AOP_IMMD:
+      return FALSE;
+    case AOP_ACC:
+      return FALSE;
+    default:
+      /* Error case --- will have been caught already */
+      wassert(0);
+      return FALSE;
+    }
+}
+
 /*-----------------------------------------------------------------*/
 /* aopPut - puts a string for a aop and indicates if acc is in use */
 /*-----------------------------------------------------------------*/
@@ -1378,16 +1447,13 @@ aopPut (operand * result, const char *s, int offset, bool bvolatile)
 
     case AOP_DIR:
       if (SPEC_SCLS (getSpec (operandType (result))) == S_SFR && offset)
-        sprintf (d, "(%s >> %d)",
-                 aop->aopu.aop_dir, offset * 8);
+        sprintf (d, "(%s >> %d)", aop->aopu.aop_dir, offset * 8);
       else if (offset)
-        sprintf (d, "(%s + %d)",
-                 aop->aopu.aop_dir, offset);
+        sprintf (d, "(%s + %d)", aop->aopu.aop_dir, offset);
       else
         sprintf (d, "%s", aop->aopu.aop_dir);
 
-      if (strcmp (d, s) ||
-          bvolatile)
+      if (strcmp (d, s) || bvolatile)
           emitcode ("mov", "%s,%s", d, s);
       if (!strcmp (d, "acc"))
           accuse = TRUE;
@@ -1461,7 +1527,6 @@ aopPut (operand * result, const char *s, int offset, bool bvolatile)
         {
           MOVA (s);
           emitcode ("movx", "@%s,a", aop->aopu.aop_ptr->name);
-
         }
       else if (*s == '@')
         {
@@ -1769,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));
 }
 
 
@@ -1803,7 +1868,6 @@ genCpl (iCode * ic)
              bit -> int -> ~int -> bit
              uchar -> int -> ~int -> bit
           */
-          werror(W_COMPLEMENT);
           emitcode ("setb", "%s", IC_RESULT (ic)->aop->aopu.aop_dir);
           goto release;
         }
@@ -1838,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));
 }
 
 /*-----------------------------------------------------------------*/
@@ -1948,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));
 }
 
 /*-----------------------------------------------------------------*/
@@ -2197,6 +2261,7 @@ assignResultValue (operand * oper, operand * func)
   int offset = 0;
   int size = AOP_SIZE (oper);
   bool accuse = FALSE;
+  bool pushedA = FALSE;
 
   if (func && IS_BIT (OP_SYM_ETYPE (func)))
     {
@@ -2204,8 +2269,15 @@ assignResultValue (operand * oper, operand * func)
       return FALSE;
     }
 
+  if ((size > 3) && aopPutUsesAcc (oper, fReturn[offset], offset))
+    {
+      emitcode ("push", "acc");
+      pushedA = TRUE;
+    }
   while (size--)
     {
+      if ((offset == 3) && pushedA)
+        emitcode ("pop", "acc");
       accuse |= aopPut (oper, fReturn[offset], offset, isOperandVolatile (oper, FALSE));
       offset++;
     }
@@ -2365,7 +2437,7 @@ static void
 saveRBank (int bank, iCode * ic, bool pushPsw)
 {
   int i;
-  int count = mcs51_nRegs + (pushPsw ? 1 : 0);
+  int count = 8 + ((mcs51_nRegs > 8) ? 1 : 0) + (pushPsw ? 1 : 0);
   asmop *aop = NULL;
   regs *r = NULL;
 
@@ -2388,7 +2460,7 @@ saveRBank (int bank, iCode * ic, bool pushPsw)
       emitcode ("mov", "%s,a", spname);
     }
 
-  for (i = 0; i < mcs51_nRegs; i++)
+  for (i = 0; i < 8; i++)
     {
       if (options.useXstack)
         {
@@ -2403,6 +2475,22 @@ saveRBank (int bank, iCode * ic, bool pushPsw)
                   regs8051[i].base, 8 * bank + regs8051[i].offset);
     }
 
+  if (mcs51_nRegs > 8)
+    {
+      if (options.useXstack)
+        {
+          emitcode ("mov", "a,bits");
+          emitcode ("movx", "@%s,a", r->name);
+          if (--count)
+            emitcode ("inc", "%s", r->name);
+        }
+      else
+        {
+          emitcode ("push", "bits");
+        }
+      BitBankUsed = 1;
+    }
+
   if (pushPsw)
     {
       if (options.useXstack)
@@ -2469,7 +2557,21 @@ unsaveRBank (int bank, iCode * ic, bool popPsw)
         }
     }
 
-  for (i = (mcs51_nRegs - 1); i >= 0; i--)
+  if (mcs51_nRegs > 8)
+    {
+      if (options.useXstack)
+        {
+          emitcode ("dec", "%s", r->name);
+          emitcode ("movx", "a,@%s", r->name);
+          emitcode ("mov", "bits,a");
+        }
+      else
+        {
+          emitcode ("pop", "bits");
+        }
+    }
+
+  for (i = 7; i >= 0; i--)
     {
       if (options.useXstack)
         {
@@ -2508,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)
@@ -2540,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)
@@ -2554,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);
     }
 }
 
@@ -3507,7 +3613,7 @@ genEndFunction (iCode * ic)
   /* restore the register bank  */
   if ( /* FUNC_REGBANK (sym->type) || */ IFFUNC_ISISR (sym->type))
   {
-    if (/* !FUNC_REGBANK (sym->type) || */ !IFFUNC_ISISR (sym->type)
+    if (!FUNC_REGBANK (sym->type) || !IFFUNC_ISISR (sym->type)
      || !options.useXstack)
     {
         /* Special case of ISR using non-zero bank with useXstack
@@ -4152,6 +4258,7 @@ genPlus (iCode * ic)
   int size, offset = 0;
   int skip_bytes = 0;
   char *add = "add";
+  bool swappedLR = FALSE;
   operand *leftOp, *rightOp;
   operand * op;
 
@@ -4173,6 +4280,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
@@ -4217,7 +4325,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 */
@@ -4308,9 +4416,17 @@ genPlus (iCode * ic)
   adjustArithmeticResult (ic);
 
 release:
-  freeAsmop (IC_LEFT (ic), NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
-  freeAsmop (IC_RIGHT (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));
+    }
 }
 
 /*-----------------------------------------------------------------*/
@@ -4424,9 +4540,23 @@ genMinusDec (iCode * ic)
      same */
   if (sameRegs (AOP (IC_LEFT (ic)), AOP (IC_RESULT (ic))))
     {
+      char *l;
+
+      if (aopGetUsesAcc (IC_LEFT (ic), 0))
+        {
+          MOVA (aopGet (IC_RESULT (ic), 0, FALSE, FALSE));
+          l = "a";
+        }
+      else
+        {
+          l = aopGet (IC_RESULT (ic), 0, FALSE, FALSE);
+        }
 
       while (icount--)
-        emitcode ("dec", "%s", aopGet (IC_RESULT (ic), 0, FALSE, FALSE));
+        emitcode ("dec", "%s", l);
+
+      if (AOP_NEEDSACC (IC_RESULT (ic)))
+        aopPut (IC_RESULT (ic), "a", 0, isOperandVolatile (IC_RESULT (ic), FALSE));
 
       return TRUE;
     }
@@ -4582,13 +4712,17 @@ genMinus (iCode * ic)
               emitcode ("subb", "a,b");
               popB (pushedB);
             } else {
+              /* reverse subtraction with 2's complement */
+              if (offset == 0)
+                emitcode( "setb", "c");
+               else
+                emitcode( "cpl", "c");
               wassertl(!aopGetUsesAcc(leftOp, offset), "accumulator clash");
               MOVA (aopGet(rightOp, offset, FALSE, TRUE));
-              if (offset == 0) {
-                emitcode( "setb", "c");
-              }
               emitcode("subb", "a,%s", aopGet(leftOp, offset, FALSE, TRUE));
               emitcode("cpl", "a");
+              if (size) /* skip if last byte */
+                emitcode( "cpl", "c");
             }
           } else {
             MOVA (aopGet (leftOp, offset, FALSE, FALSE));
@@ -4606,9 +4740,9 @@ genMinus (iCode * ic)
   adjustArithmeticResult (ic);
 
 release:
-  freeAsmop (IC_LEFT (ic), NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
-  freeAsmop (IC_RIGHT (ic), NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
   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));
 }
 
 
@@ -4842,7 +4976,7 @@ genMult (iCode * ic)
 
   D(emitcode (";     genMult",""));
 
-  /* assign the amsops */
+  /* assign the asmops */
   aopOp (left, ic, FALSE);
   aopOp (right, ic, FALSE);
   aopOp (result, ic, TRUE);
@@ -4918,6 +5052,8 @@ genDivOneByte (operand * left,
 {
   bool lUnsigned, rUnsigned, pushedB;
   bool runtimeSign, compiletimeSign;
+  bool accuse = FALSE;
+  bool pushedA = FALSE;
   symbol *lbl;
   int size, offset;
 
@@ -5081,20 +5217,32 @@ genDivOneByte (operand * left,
       emitcode ("inc", "a");
       emitcode ("", "%05d$:", (lbl->key + 100));
 
-      aopPut (result, "a", 0, isOperandVolatile (result, FALSE));
+      accuse = aopPut (result, "a", 0, isOperandVolatile (result, FALSE));
       if (size > 0)
         {
           /* msb is 0x00 or 0xff depending on the sign */
           if (runtimeSign)
             {
+              if (accuse)
+                {
+                  emitcode ("push", "acc");
+                  pushedA = TRUE;
+                }
               emitcode ("mov", "c,F0");
               emitcode ("subb", "a,acc");
               while (size--)
                 aopPut (result, "a", offset++, isOperandVolatile (result, FALSE));
             }
           else /* compiletimeSign */
-            while (size--)
-              aopPut (result, "#0xff", offset++, isOperandVolatile (result, FALSE));
+            {
+              if (aopPutUsesAcc (result, "#0xFF", offset))
+                {
+                  emitcode ("push", "acc");
+                  pushedA = TRUE;
+                }
+              while (size--)
+                aopPut (result, "#0xff", offset++, isOperandVolatile (result, FALSE));
+            }
         }
     }
   else
@@ -5104,6 +5252,8 @@ genDivOneByte (operand * left,
         aopPut (result, zero, offset++, isOperandVolatile (result, FALSE));
     }
 
+  if (pushedA)
+    emitcode ("pop", "acc");
   popB (pushedB);
 }
 
@@ -5144,9 +5294,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);
 }
 
 /*-----------------------------------------------------------------*/
@@ -5415,9 +5565,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);
 }
 
 /*-----------------------------------------------------------------*/
@@ -5616,9 +5766,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);
 
@@ -5646,9 +5796,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);
 
@@ -5758,6 +5908,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",""));
@@ -5775,6 +5926,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))
@@ -5926,9 +6078,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));
+    }
 }
 
 /*-----------------------------------------------------------------*/
@@ -6026,9 +6186,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);
 }
 
 
@@ -6068,9 +6228,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);
 }
 
 /*-----------------------------------------------------------------*/
@@ -6378,10 +6538,25 @@ genAnd (iCode * ic, iCode * ifx)
             }
           else
             {
-              if (AOP_TYPE (left) == AOP_ACC && offset == 0)
+              if (AOP_TYPE (left) == AOP_ACC)
                 {
+                  if (offset)
+                    emitcode("mov", "a,b");
                   emitcode ("anl", "a,%s", aopGet (right, offset, FALSE, FALSE));
                 }
+              else if (aopGetUsesAcc (left, offset) && aopGetUsesAcc (right, offset))
+                {
+                  emitcode ("mov", "b,%s", aopGet (left, offset, FALSE, FALSE));
+                  MOVA (aopGet (right, offset, FALSE, FALSE));
+                  emitcode ("anl", "a,b");
+                  aopPut (result, "a", offset, isOperandVolatile (result, FALSE));
+                }
+              else if (aopGetUsesAcc (left, offset))
+                {
+                  MOVA (aopGet (left, offset, FALSE, FALSE));
+                  emitcode ("anl", "a,%s", aopGet (right, offset, FALSE, FALSE));
+                  aopPut (result, "a", offset, isOperandVolatile (result, FALSE));
+                }
               else
                 {
                   MOVA (aopGet (right, offset, FALSE, FALSE));
@@ -6503,11 +6678,21 @@ genAnd (iCode * ic, iCode * ifx)
                     emitcode("mov", "a,b");
                   emitcode ("anl", "a,%s", aopGet (right, offset, FALSE, FALSE));
                 }
+              else if (aopGetUsesAcc (left, offset) && aopGetUsesAcc (right, offset))
+                {
+                  emitcode ("mov", "b,%s", aopGet (left, offset, FALSE, FALSE));
+                  MOVA (aopGet (right, offset, FALSE, FALSE));
+                  emitcode ("anl", "a,b");
+                }
+              else if (aopGetUsesAcc (left, offset))
+                {
+                  MOVA (aopGet (left, offset, FALSE, FALSE));
+                  emitcode ("anl", "a,%s", aopGet (right, offset, FALSE, FALSE));
+                }
               else
                 {
                   MOVA (aopGet (right, offset, FALSE, FALSE));
-                  emitcode ("anl", "a,%s",
-                            aopGet (left, offset, FALSE, FALSE));
+                  emitcode ("anl", "a,%s", aopGet (left, offset, FALSE, FALSE));
                 }
               aopPut (result, "a", offset, isOperandVolatile (result, FALSE));
             }
@@ -6515,9 +6700,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);
 }
 
 /*-----------------------------------------------------------------*/
@@ -6723,6 +6908,19 @@ genOr (iCode * ic, iCode * ifx)
                     emitcode("mov", "a,b");
                   emitcode ("orl", "a,%s", aopGet (right, offset, FALSE, FALSE));
                 }
+              else if (aopGetUsesAcc (left, offset) && aopGetUsesAcc (right, offset))
+                {
+                  emitcode ("mov", "b,%s", aopGet (left, offset, FALSE, FALSE));
+                  MOVA (aopGet (right, offset, FALSE, FALSE));
+                  emitcode ("orl", "a,b");
+                  aopPut (result, "a", offset, isOperandVolatile (result, FALSE));
+                }
+              else if (aopGetUsesAcc (left, offset))
+                {
+                  MOVA (aopGet (left, offset, FALSE, FALSE));
+                  emitcode ("orl", "a,%s", aopGet (right, offset, FALSE, FALSE));
+                  aopPut (result, "a", offset, isOperandVolatile (result, FALSE));
+                }
               else
                 {
                   MOVA (aopGet (right, offset, FALSE, FALSE));
@@ -6812,11 +7010,21 @@ genOr (iCode * ic, iCode * ifx)
                     emitcode("mov", "a,b");
                   emitcode ("orl", "a,%s", aopGet (right, offset, FALSE, FALSE));
                 }
+              else if (aopGetUsesAcc (left, offset) && aopGetUsesAcc (right, offset))
+                {
+                  emitcode ("mov", "b,%s", aopGet (left, offset, FALSE, FALSE));
+                  MOVA (aopGet (right, offset, FALSE, FALSE));
+                  emitcode ("orl", "a,b");
+                }
+              else if (aopGetUsesAcc (left, offset))
+                {
+                  MOVA (aopGet (left, offset, FALSE, FALSE));
+                  emitcode ("orl", "a,%s", aopGet (right, offset, FALSE, FALSE));
+                }
               else
                 {
                   MOVA (aopGet (right, offset, FALSE, FALSE));
-                  emitcode ("orl", "a,%s",
-                            aopGet (left, offset, FALSE, FALSE));
+                  emitcode ("orl", "a,%s", aopGet (left, offset, FALSE, FALSE));
                 }
               aopPut (result, "a", offset, isOperandVolatile (result, FALSE));
             }
@@ -6824,9 +7032,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);
 }
 
 /*-----------------------------------------------------------------*/
@@ -7011,6 +7219,19 @@ genXor (iCode * ic, iCode * ifx)
                     emitcode("mov", "a,b");
                   emitcode ("xrl", "a,%s", aopGet (right, offset, FALSE, FALSE));
                 }
+              else if (aopGetUsesAcc (left, offset) && aopGetUsesAcc (right, offset))
+                {
+                  emitcode ("mov", "b,%s", aopGet (left, offset, FALSE, FALSE));
+                  MOVA (aopGet (right, offset, FALSE, FALSE));
+                  emitcode ("xrl", "a,b");
+                  aopPut (result, "a", offset, isOperandVolatile (result, FALSE));
+                }
+              else if (aopGetUsesAcc (left, offset))
+                {
+                  MOVA (aopGet (left, offset, FALSE, FALSE));
+                  emitcode ("xrl", "a,%s", aopGet (right, offset, FALSE, FALSE));
+                  aopPut (result, "a", offset, isOperandVolatile (result, FALSE));
+                }
               else
                 {
                   MOVA (aopGet (right, offset, FALSE, FALSE));
@@ -7096,11 +7317,21 @@ genXor (iCode * ic, iCode * ifx)
                     emitcode("mov", "a,b");
                   emitcode ("xrl", "a,%s", aopGet (right, offset, FALSE, FALSE));
                 }
+              else if (aopGetUsesAcc (left, offset) && aopGetUsesAcc (right, offset))
+                {
+                  emitcode ("mov", "b,%s", aopGet (left, offset, FALSE, FALSE));
+                  MOVA (aopGet (right, offset, FALSE, FALSE));
+                  emitcode ("xrl", "a,b");
+                }
+              else if (aopGetUsesAcc (left, offset))
+                {
+                  MOVA (aopGet (left, offset, FALSE, FALSE));
+                  emitcode ("xrl", "a,%s", aopGet (right, offset, FALSE, FALSE));
+                }
               else
                 {
                   MOVA (aopGet (right, offset, FALSE, FALSE));
-                  emitcode ("xrl", "a,%s",
-                            aopGet (left, offset, FALSE, TRUE));
+                  emitcode ("xrl", "a,%s", aopGet (left, offset, FALSE, TRUE));
                 }
               aopPut (result, "a", offset, isOperandVolatile (result, FALSE));
             }
@@ -7108,9 +7339,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);
 }
 
 /*-----------------------------------------------------------------*/
@@ -7204,8 +7435,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);
 }
 
 /*-----------------------------------------------------------------*/
@@ -7259,8 +7490,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);
 }
 
 /*-----------------------------------------------------------------*/
@@ -7292,9 +7523,8 @@ genGetHbit (iCode * ic)
       outAcc (result);
     }
 
-
-  freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
+  freeAsmop (left, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -7364,9 +7594,9 @@ genGetAbit (iCode * ic)
       outAcc (result);
     }
 
-  freeAsmop (left, NULL, ic, TRUE);
-  freeAsmop (right, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
+  freeAsmop (right, NULL, ic, TRUE);
+  freeAsmop (left, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -7393,9 +7623,9 @@ genGetByte (iCode * ic)
           0,
           isOperandVolatile (result, FALSE));
 
-  freeAsmop (left, NULL, ic, TRUE);
-  freeAsmop (right, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
+  freeAsmop (right, NULL, ic, TRUE);
+  freeAsmop (left, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -7426,9 +7656,9 @@ genGetWord (iCode * ic)
           1,
           isOperandVolatile (result, FALSE));
 
-  freeAsmop (left, NULL, ic, TRUE);
-  freeAsmop (right, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
+  freeAsmop (right, NULL, ic, TRUE);
+  freeAsmop (left, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -7493,8 +7723,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);
 }
 
 
@@ -7680,7 +7910,7 @@ movLeft2Result (operand * left, int offl,
               /* MSB sign in acc.7 ! */
               if (getDataSize (left) == offl + 1)
                 {
-                  emitcode ("mov", "a,%s", l);
+                  MOVA (l);
                   aopPut (result, "a", offr, isOperandVolatile (result, FALSE));
                 }
             }
@@ -7995,21 +8225,47 @@ static void
 shiftL2Left2Result (operand * left, int offl,
                     operand * result, int offr, int shCount)
 {
+  char * x;
+  bool pushedB = FALSE;
+  bool usedB = FALSE;
+
   if (sameRegs (AOP (result), AOP (left)) &&
       ((offl + MSB16) == offr))
     {
       /* don't crash result[offr] */
       MOVA (aopGet (left, offl, FALSE, FALSE));
       emitcode ("xch", "a,%s", aopGet (left, offl + MSB16, FALSE, FALSE));
+      x = aopGet (result, offr, FALSE, FALSE);
+    }
+  else if (aopGetUsesAcc (result, offr))
+    {
+      movLeft2Result (left, offl, result, offr, 0);
+      pushedB = pushB ();
+      usedB = TRUE;
+      emitcode ("mov", "b,%s", aopGet (left, offl + MSB16, FALSE, FALSE));
+      MOVA (aopGet (result, offr, FALSE, FALSE));
+      emitcode ("xch", "a,b");
+      x = "b";
     }
   else
     {
       movLeft2Result (left, offl, result, offr, 0);
       MOVA (aopGet (left, offl + MSB16, FALSE, FALSE));
+      x = aopGet (result, offr, FALSE, FALSE);
     }
   /* ax << shCount (x = lsb(result)) */
-  AccAXLsh (aopGet (result, offr, FALSE, FALSE), shCount);
-  aopPut (result, "a", offr + MSB16, isOperandVolatile (result, FALSE));
+  AccAXLsh (x, shCount);
+  if (usedB)
+    {
+      emitcode ("xch", "a,b");
+      aopPut (result, "a", offr, isOperandVolatile (result, FALSE));
+      aopPut (result, "b", offr + MSB16, isOperandVolatile (result, FALSE));
+      popB (pushedB);
+    }
+  else
+    {
+      aopPut (result, "a", offr + MSB16, isOperandVolatile (result, FALSE));
+    }
 }
 
 
@@ -8021,23 +8277,45 @@ shiftR2Left2Result (operand * left, int offl,
                     operand * result, int offr,
                     int shCount, int sign)
 {
+  char * x;
+  bool pushedB = FALSE;
+  bool usedB = FALSE;
+
   if (sameRegs (AOP (result), AOP (left)) &&
       ((offl + MSB16) == offr))
     {
       /* don't crash result[offr] */
       MOVA (aopGet (left, offl, FALSE, FALSE));
       emitcode ("xch", "a,%s", aopGet (left, offl + MSB16, FALSE, FALSE));
+      x = aopGet (result, offr, FALSE, FALSE);
+    }
+  else if (aopGetUsesAcc (result, offr))
+    {
+      movLeft2Result (left, offl, result, offr, 0);
+      pushedB = pushB ();
+      usedB = TRUE;
+      emitcode ("mov", "b,%s", aopGet (result, offr, FALSE, FALSE));
+      MOVA (aopGet (left, offl + MSB16, FALSE, FALSE));
+      x = "b";
     }
   else
     {
       movLeft2Result (left, offl, result, offr, 0);
       MOVA (aopGet (left, offl + MSB16, FALSE, FALSE));
+      x = aopGet (result, offr, FALSE, FALSE);
     }
   /* a:x >> shCount (x = lsb(result)) */
   if (sign)
-    AccAXRshS (aopGet (result, offr, FALSE, FALSE), shCount);
+    AccAXRshS (x, shCount);
   else
-    AccAXRsh (aopGet (result, offr, FALSE, FALSE), shCount);
+    AccAXRsh (x, shCount);
+  if (usedB)
+    {
+      emitcode ("xch", "a,b");
+      aopPut (result, "a", offr, isOperandVolatile (result, FALSE));
+      emitcode ("xch", "a,b");
+      popB (pushedB);
+    }
   if (getDataSize (result) > 1)
     aopPut (result, "a", offr + MSB16, isOperandVolatile (result, FALSE));
 }
@@ -8053,7 +8331,16 @@ shiftLLeftOrResult (operand * left, int offl,
   /* shift left accumulator */
   AccLsh (shCount);
   /* or with result */
-  emitcode ("orl", "a,%s", aopGet (result, offr, FALSE, FALSE));
+  if (aopGetUsesAcc (result, offr))
+    {
+      emitcode ("xch", "a,b");
+      MOVA (aopGet (result, offr, FALSE, FALSE));
+      emitcode ("orl", "a,b");
+    }
+  else
+    {
+      emitcode ("orl", "a,%s", aopGet (result, offr, FALSE, FALSE));
+    }
   /* back to result */
   aopPut (result, "a", offr, isOperandVolatile (result, FALSE));
 }
@@ -8345,8 +8632,8 @@ genLeftShiftLiteral (operand * left,
           break;
         }
     }
-  freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
+  freeAsmop (left, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -8455,8 +8742,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);
 }
 
 /*-----------------------------------------------------------------*/
@@ -8505,59 +8792,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));
     }
@@ -8695,8 +9012,8 @@ genRightShiftLiteral (operand * left,
           break;
         }
     }
-  freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
+  freeAsmop (left, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -8806,8 +9123,8 @@ genSignedRightShift (iCode * ic)
   popB (pushedB);
 
 release:
-  freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
+  freeAsmop (left, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -8931,8 +9248,8 @@ genRightShift (iCode * ic)
   popB (pushedB);
 
 release:
-  freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
+  freeAsmop (left, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -9066,8 +9383,17 @@ genUnpackBits (operand * result, char *rname, int ptype, iCode *ifx)
   if (blen < 8)
     {
       emitPtrByteGet (rname, ptype, FALSE);
-      AccRsh (bstr);
+      AccRol (8 - bstr);
       emitcode ("anl", "a,#0x%02x", ((unsigned char) -1) >> (8 - blen));
+      if (!SPEC_USIGN (etype))
+        {
+          /* signed bitfield */
+          symbol *tlbl = newiTempLabel (NULL);
+
+          emitcode ("jnb", "acc.%d,%05d$", blen - 1, tlbl->key + 100);
+          emitcode ("orl", "a,#0x%02x", (unsigned char) (0xff << blen));
+          emitcode ("", "%05d$:", tlbl->key + 100);
+        }
       aopPut (result, "a", offset++, isOperandVolatile (result, FALSE));
       goto finish;
     }
@@ -9087,15 +9413,36 @@ genUnpackBits (operand * result, char *rname, int ptype, iCode *ifx)
     {
       emitPtrByteGet (rname, ptype, FALSE);
       emitcode ("anl", "a,#0x%02x", ((unsigned char) -1) >> (8-rlen));
+      if (!SPEC_USIGN (etype))
+        {
+          /* signed bitfield */
+          symbol *tlbl = newiTempLabel (NULL);
+
+          emitcode ("jnb", "acc.%d,%05d$", rlen - 1, tlbl->key + 100);
+          emitcode ("orl", "a,#0x%02x", (unsigned char) (0xff << rlen));
+          emitcode ("", "%05d$:", tlbl->key + 100);
+        }
       aopPut (result, "a", offset++, isOperandVolatile (result, FALSE));
     }
 
 finish:
   if (offset < rsize)
     {
+      char *source;
+
+      if (SPEC_USIGN (etype))
+        source = zero;
+      else
+        {
+          /* signed bitfield: sign extension with 0x00 or 0xff */
+          emitcode ("rlc", "a");
+          emitcode ("subb", "a,acc");
+
+          source = "a";
+        }
       rsize -= offset;
       while (rsize--)
-        aopPut (result, zero, offset++, isOperandVolatile (result, FALSE));
+        aopPut (result, source, offset++, isOperandVolatile (result, FALSE));
     }
 }
 
@@ -9128,8 +9475,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);
 }
 
 /*-----------------------------------------------------------------*/
@@ -9365,10 +9712,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;
-
 }
 
 /*--------------------------------------------------------------------*/
@@ -9479,8 +9825,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);
 }
 
 /*-----------------------------------------------------------------*/
@@ -9541,8 +9887,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);
 }
 
 /*-----------------------------------------------------------------*/
@@ -9593,9 +9939,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);
 }
 
 /*-----------------------------------------------------------------*/
@@ -9829,8 +10174,8 @@ genDataPointerSet (operand * right,
                 aopGet (right, offset++, FALSE, FALSE));
     }
 
-  freeAsmop (right, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
+  freeAsmop (right, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -10053,8 +10398,6 @@ genPagedPointerSet (operand * right,
   if (pi) pi->generated = 1;
   freeAsmop (result, NULL, ic, TRUE);
   freeAsmop (right, NULL, ic, TRUE);
-
-
 }
 
 /*-----------------------------------------------------------------*/
@@ -10353,7 +10696,6 @@ genFarFarAssign (operand * result, operand * right, iCode * ic)
       aopPut (result, "a", --offset, isOperandVolatile (result, FALSE));
     }
   freeAsmop (result, NULL, ic, FALSE);
-
 }
 
 /*-----------------------------------------------------------------*/
@@ -10468,8 +10810,8 @@ genAssign (iCode * ic)
     }
 
 release:
-  freeAsmop (right, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
+  freeAsmop (right, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -10597,8 +10939,8 @@ genCast (iCode * ic)
 
   /* if the result is a bit (and not a bitfield) */
   // if (AOP_TYPE (result) == AOP_CRY)
-  if (IS_BITVAR (OP_SYMBOL (result)->type)
-      && !IS_BITFIELD (OP_SYMBOL (result)->type) )
+  if (IS_BIT (OP_SYMBOL (result)->type))
+    /* not for bitfields */
     {
       /* if the right size is a literal then
          we know what the value is */
@@ -10753,9 +11095,8 @@ genCast (iCode * ic)
   /* we are done hurray !!!! */
 
 release:
-  freeAsmop (right, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
-
+  freeAsmop (right, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -10853,7 +11194,8 @@ genReceive (iCode * ic)
 
   if (ic->argreg == 1)
     { /* first parameter */
-      if (isOperandInFarSpace (IC_RESULT (ic)) &&
+      if ((isOperandInFarSpace (IC_RESULT (ic)) ||
+           isOperandInPagedSpace (IC_RESULT (ic))) &&
           (OP_SYMBOL (IC_RESULT (ic))->isspilt ||
            IS_TRUE_SYMOP (IC_RESULT (ic))))
         {
@@ -11121,11 +11463,21 @@ gen51Code (iCode * lic)
         char regsInUse[80];
         int i;
 
+        #if 0
         for (i=0; i<8; i++) {
           sprintf (&regsInUse[i],
-                   "%c", ic->riu & (1<<i) ? i+'0' : '-');
-        }
+                   "%c", ic->riu & (1<<i) ? i+'0' : '-'); /* show riu */
         regsInUse[i]=0;
+        #else
+        strcpy (regsInUse, "--------");
+        for (i=0; i < 8; i++) {
+          if (bitVectBitValue (ic->rMask, i))
+            {
+              int offset = regs8051[i].offset;
+              regsInUse[offset] = offset + '0'; /* show rMask */
+            }
+        #endif
+        }
         emitcode("", "; [%s] ic:%d: %s", regsInUse, ic->seq, printILine(ic));
       }
       /* if the result is marked as