* src/pic16/{gen.c,genarith.c}: replaced sign-extension
[fw/sdcc] / src / mcs51 / gen.c
index 83cadf84ea92165b8b94e7b586a88ca4d6af4870..e7e31c824ae56e1c3af628e5202afc105f9d7436 100644 (file)
@@ -64,12 +64,27 @@ static char *accUse[] =
 
 static unsigned short rbank = -1;
 
+#define SYM_BP(sym)   (SPEC_OCLS (sym->etype)->paged ? "_bpx" : "_bp")
+
+#define R0INB  _G.bu.bs.r0InB
+#define R1INB  _G.bu.bs.r1InB
+#define OPINB  _G.bu.bs.OpInB
+#define BINUSE _G.bu.BInUse
+
 static struct
   {
     short r0Pushed;
     short r1Pushed;
-    short r0InB;
-    short r1InB;
+    union
+      {
+        struct
+          {
+            short r0InB : 2;//2 so we can see it overflow
+            short r1InB : 2;//2 so we can see it overflow
+            short OpInB : 2;//2 so we can see it overflow
+          } bs;
+        short BInUse;
+      } bu;
     short accInUse;
     short inLine;
     short debugLine;
@@ -88,13 +103,14 @@ extern int mcs51_ptrRegReq;
 extern int mcs51_nRegs;
 extern FILE *codeOutFile;
 static void saveRBank (int, iCode *, bool);
+
 #define RESULTONSTACK(x) \
                          (IC_RESULT(x) && IC_RESULT(x)->aop && \
                          IC_RESULT(x)->aop->type == AOP_STK )
 
-#define MOVA(x) mova(x)  /* use function to avoid multiple eval */
-#define CLRC    emitcode("clr","c")
-#define SETC    emitcode("setb","c")
+#define MOVA(x)  mova(x)  /* use function to avoid multiple eval */
+#define CLRC     emitcode("clr","c")
+#define SETC     emitcode("setb","c")
 
 static lineNode *lineHead = NULL;
 static lineNode *lineCurr = NULL;
@@ -126,23 +142,21 @@ emitcode (char *inst, const char *fmt,...)
   if (inst && *inst)
     {
       if (fmt && *fmt)
-        SNPRINTF (lb, sizeof(lb), "%s\t", inst);
+          SNPRINTF (lb, sizeof(lb), "%s\t", inst);
       else
-        SNPRINTF (lb, sizeof(lb), "%s", inst);
+          SNPRINTF (lb, sizeof(lb), "%s", inst);
       tvsprintf (lb + strlen(lb), sizeof(lb) - strlen(lb), fmt, ap);
     }
   else
-    tvsprintf (lb, sizeof(lb), fmt, ap);
+      tvsprintf (lb, sizeof(lb), fmt, ap);
 
   while (isspace (*lbp))
-    lbp++;
-
-  //printf ("%s\n", lb);
+      lbp++;
 
   if (lbp && *lbp)
-    lineCurr = (lineCurr ?
-                connectLine (lineCurr, newLineNode (lb)) :
-                (lineHead = newLineNode (lb)));
+      lineCurr = (lineCurr ?
+                  connectLine (lineCurr, newLineNode (lb)) :
+                  (lineHead = newLineNode (lb)));
   lineCurr->isInline = _G.inLine;
   lineCurr->isDebug = _G.debugLine;
   lineCurr->ic = _G.current_iCode;
@@ -169,20 +183,57 @@ static void
 mova (const char *x)
 {
   /* do some early peephole optimization */
-  if (!strcmp(x, "a") || !strcmp(x, "acc"))
+  if (!strncmp(x, "a", 2) || !strncmp(x, "acc", 4))
     return;
 
   emitcode("mov","a,%s", x);
 }
 
+/*-----------------------------------------------------------------*/
+/* pushB - saves register B if necessary                           */
+/*-----------------------------------------------------------------*/
+static bool
+pushB (void)
+{
+  bool pushedB = FALSE;
+
+  if (BINUSE)
+    {
+      emitcode ("push", "b");
+//    printf("B was in use !\n");
+      pushedB = TRUE;
+    }
+  else
+    {
+      OPINB++;
+    }
+  return pushedB;
+}
+
+/*-----------------------------------------------------------------*/
+/* popB - restores value of register B if necessary                */
+/*-----------------------------------------------------------------*/
+static void
+popB (bool pushedB)
+{
+  if (pushedB)
+    {
+      emitcode ("pop", "b");
+    }
+  else
+    {
+      OPINB--;
+    }
+}
+
 /*-----------------------------------------------------------------*/
 /* getFreePtr - returns r0 or r1 whichever is free or can be pushed */
 /*-----------------------------------------------------------------*/
 static regs *
 getFreePtr (iCode * ic, asmop ** aopp, bool result)
 {
-  bool r0iu = FALSE, r1iu = FALSE;
-  bool r0ou = FALSE, r1ou = FALSE;
+  bool r0iu, r1iu;
+  bool r0ou, r1ou;
 
   /* the logic: if r0 & r1 used in the instruction
      then we are in trouble otherwise */
@@ -225,7 +276,7 @@ getFreePtr (iCode * ic, asmop ** aopp, bool result)
         {
           emitcode ("mov", "b,%s",
                     mcs51_regWithIdx (R0_IDX)->dname);
-          _G.r0InB++;
+          R0INB++;
         }
       else if (!_G.r0Pushed)
         {
@@ -249,7 +300,7 @@ getFreePtr (iCode * ic, asmop ** aopp, bool result)
         {
           emitcode ("mov", "b,%s",
                     mcs51_regWithIdx (R1_IDX)->dname);
-          _G.r1InB++;
+          R1INB++;
         }
       else if (!_G.r1Pushed)
         {
@@ -365,7 +416,6 @@ pointerCode (sym_link * etype)
 
 }
 
-
 /*-----------------------------------------------------------------*/
 /* leftRightUseAcc - returns size of accumulator use by operands   */
 /*-----------------------------------------------------------------*/
@@ -432,7 +482,6 @@ leftRightUseAcc(iCode *ic)
     return accuse;
 }
 
-
 /*-----------------------------------------------------------------*/
 /* aopForSym - for a true symbol                                   */
 /*-----------------------------------------------------------------*/
@@ -467,17 +516,34 @@ aopForSym (iCode * ic, symbol * sym, bool result)
 
           if (sym->onStack)
             {
+              char offset = ((sym->stack < 0) ?
+                         ((char) (sym->stack - _G.nRegsSaved)) :
+                         ((char) sym->stack)) & 0xff;
               if (_G.accInUse || leftRightUseAcc (ic))
                 emitcode ("push", "acc");
 
-              emitcode ("mov", "a,_bp");
-              emitcode ("add", "a,#0x%02x",
-                        ((sym->stack < 0) ?
-                         ((char) (sym->stack - _G.nRegsSaved)) :
-                         ((char) sym->stack)) & 0xff);
-              emitcode ("mov", "%s,a",
-                        aop->aopu.aop_ptr->name);
-
+              if ((offset >= -3) && (offset <= 3))
+                {
+                  emitcode ("mov", "%s,%s",
+                            aop->aopu.aop_ptr->name, SYM_BP (sym));
+                  while (offset < 0)
+                    {
+                      emitcode ("dec", aop->aopu.aop_ptr->name);
+                      offset++;
+                    }
+                  while (offset > 0)
+                    {
+                      emitcode ("inc", aop->aopu.aop_ptr->name);
+                      offset--;
+                    }
+                }
+              else
+                {
+                  emitcode ("mov", "a,%s", SYM_BP (sym));
+                  emitcode ("add", "a,#0x%02x", offset);
+                  emitcode ("mov", "%s,a",
+                            aop->aopu.aop_ptr->name);
+                }
               if (_G.accInUse || leftRightUseAcc (ic))
                 emitcode ("pop", "acc");
             }
@@ -543,7 +609,7 @@ aopForRemat (symbol * sym)
 {
   iCode *ic = sym->rematiCode;
   asmop *aop = newAsmop (AOP_IMMD);
-  int ptr_type=0;
+  int ptr_type = 0;
   int val = 0;
 
   for (;;)
@@ -633,7 +699,7 @@ operandsEqu (operand * op1, operand * op2)
 {
   symbol *sym1, *sym2;
 
-  /* if they not symbols */
+  /* if they're not symbols */
   if (!IS_SYMOP (op1) || !IS_SYMOP (op2))
     return FALSE;
 
@@ -650,6 +716,7 @@ operandsEqu (operand * op1, operand * op2)
   if (sym1 == sym2)
     return TRUE;
 
+  /* if they have the same rname */
   if (sym1->rname[0] && sym2->rname[0]
       && strcmp (sym1->rname, sym2->rname) == 0)
     return TRUE;
@@ -738,7 +805,7 @@ aopOp (operand * op, iCode * ic, bool result)
     }
 
   /* this is a temporary : this has
-     only four choices :
+     only five choices :
      a) register
      b) spillocation
      c) rematerialize
@@ -843,10 +910,10 @@ freeAsmop (operand * op, asmop * aaop, iCode * ic, bool pop)
   switch (aop->type)
     {
     case AOP_R0:
-      if (_G.r0InB)
+      if (R0INB)
         {
           emitcode ("mov", "r0,b");
-          _G.r0InB--;
+          R0INB--;
         }
       else if (_G.r0Pushed)
         {
@@ -860,10 +927,10 @@ freeAsmop (operand * op, asmop * aaop, iCode * ic, bool pop)
       break;
 
     case AOP_R1:
-      if (_G.r1InB)
+      if (R1INB)
         {
           emitcode ("mov", "r1,b");
-          _G.r1InB--;
+          R1INB--;
         }
       if (_G.r1Pushed)
         {
@@ -958,7 +1025,7 @@ freeForBranchAsmop (operand * op)
   switch (aop->type)
     {
     case AOP_R0:
-      if (_G.r0InB)
+      if (R0INB)
         {
           emitcode ("mov", "r0,b");
         }
@@ -969,7 +1036,7 @@ freeForBranchAsmop (operand * op)
       break;
 
     case AOP_R1:
-      if (_G.r1InB)
+      if (R1INB)
         {
           emitcode ("mov", "r1,b");
         }
@@ -1040,6 +1107,8 @@ aopGetUsesAcc (asmop *aop, int offset)
     case AOP_CRY:
       return TRUE;
     case AOP_ACC:
+      if (offset)
+        return FALSE;
       return TRUE;
     case AOP_LIT:
       return FALSE;
@@ -1197,12 +1266,13 @@ aopGet (asmop * aop, int offset, bool bit16, bool dname)
   exit (1);
 }
 /*-----------------------------------------------------------------*/
-/* aopPut - puts a string for a aop                                */
+/* aopPut - puts a string for a aop and indicates if acc is in use */
 /*-----------------------------------------------------------------*/
-static void
+static bool
 aopPut (asmop * aop, const char *s, int offset, bool bvolatile)
 {
   char *d = buffer;
+  bool accuse = FALSE;
 
   if (aop->size && offset > (aop->size - 1))
     {
@@ -1217,6 +1287,7 @@ aopPut (asmop * aop, const char *s, int offset, bool bvolatile)
     {
     case AOP_DUMMY:
       MOVA (s);         /* read s in case it was volatile */
+      accuse = TRUE;
       break;
 
     case AOP_DIR:
@@ -1228,7 +1299,9 @@ aopPut (asmop * aop, const char *s, int offset, bool bvolatile)
 
       if (strcmp (d, s) ||
           bvolatile)
-        emitcode ("mov", "%s,%s", d, s);
+          emitcode ("mov", "%s,%s", d, s);
+      if (!strcmp (d, "acc"))
+          accuse = TRUE;
 
       break;
 
@@ -1275,7 +1348,7 @@ aopPut (asmop * aop, const char *s, int offset, bool bvolatile)
 
       aop->coff = offset;
 
-      /* if not in accumulater */
+      /* if not in accumulator */
       MOVA (s);
 
       emitcode ("movx", "@dptr,a");
@@ -1339,11 +1412,13 @@ aopPut (asmop * aop, const char *s, int offset, bool bvolatile)
       break;
 
     case AOP_CRY:
-      /* if bit variable */
+      /* if not bit variable */
       if (!aop->aopu.aop_dir)
         {
+          /* inefficient: move carry into A and use jz/jnz */
           emitcode ("clr", "a");
           emitcode ("rlc", "a");
+          accuse = TRUE;
         }
       else
         {
@@ -1355,15 +1430,10 @@ aopPut (asmop * aop, const char *s, int offset, bool bvolatile)
             emitcode ("mov", "%s,c", aop->aopu.aop_dir);
           else
             {
-              if (strcmp (s, "a"))
-                {
-                  MOVA (s);
-                }
-              {
-                /* set C, if a >= 1 */
-                emitcode ("add", "a,#0xff");
-                emitcode ("mov", "%s,c", aop->aopu.aop_dir);
-              }
+              MOVA (s);
+              /* set C, if a >= 1 */
+              emitcode ("add", "a,#0xff");
+              emitcode ("mov", "%s,c", aop->aopu.aop_dir);
             }
         }
       break;
@@ -1376,6 +1446,7 @@ aopPut (asmop * aop, const char *s, int offset, bool bvolatile)
       break;
 
     case AOP_ACC:
+      accuse = TRUE;
       aop->coff = offset;
       if (!offset && (strcmp (s, "acc") == 0) &&
           !bvolatile)
@@ -1392,6 +1463,7 @@ aopPut (asmop * aop, const char *s, int offset, bool bvolatile)
       exit (1);
     }
 
+    return accuse;
 }
 
 
@@ -1547,9 +1619,37 @@ toBoolean (operand * oper)
 {
   int size = AOP_SIZE (oper) - 1;
   int offset = 1;
+  bool AccUsed = FALSE;
+  bool pushedB;
+
+  while (!AccUsed && size--)
+    {
+      AccUsed |= aopGetUsesAcc(AOP (oper), offset++);
+    }
+
+  size = AOP_SIZE (oper) - 1;
+  offset = 1;
   MOVA (aopGet (AOP (oper), 0, FALSE, FALSE));
-  while (size--)
-    emitcode ("orl", "a,%s", aopGet (AOP (oper), offset++, FALSE, FALSE));
+  if (size && AccUsed && (AOP (oper)->type != AOP_ACC))
+    {
+      pushedB = pushB ();
+      emitcode("mov", "b,a");
+      while (--size)
+        {
+          MOVA (aopGet (AOP (oper), offset++, FALSE, FALSE));
+          emitcode ("orl", "b,a");
+        }
+      MOVA (aopGet (AOP (oper), offset++, FALSE, FALSE));
+      emitcode ("orl", "a,b");
+      popB (pushedB);
+    }
+  else
+    {
+      while (size--)
+        {
+          emitcode ("orl", "a,%s", aopGet (AOP (oper), offset++, FALSE, FALSE));
+        }
+    }
 }
 
 
@@ -1599,8 +1699,9 @@ genCpl (iCode * ic)
   int offset = 0;
   int size;
   symbol *tlbl;
+  sym_link *letype = getSpec (operandType (IC_LEFT (ic)));
 
-  D(emitcode (";     genCpl",""));
+  D(emitcode (";", "genCpl"));
 
   /* assign asmOps to operand & result */
   aopOp (IC_LEFT (ic), ic, FALSE);
@@ -1609,27 +1710,32 @@ genCpl (iCode * ic)
   /* special case if in bit space */
   if (AOP_TYPE (IC_RESULT (ic)) == AOP_CRY)
     {
-      if (AOP_TYPE (IC_LEFT (ic)) == AOP_CRY)
+      char *l;
+
+      if (AOP_TYPE (IC_LEFT (ic)) == AOP_CRY ||
+          (SPEC_USIGN (letype) && IS_CHAR (letype)))
         {
-          /* promotion rules are responsible for this strange result: */
+          /* promotion rules are responsible for this strange result:
+             bit -> int -> ~int -> bit
+             uchar -> int -> ~int -> bit
+          */
+          werror(W_COMPLEMENT);
           emitcode ("setb", "%s", IC_RESULT (ic)->aop->aopu.aop_dir);
           goto release;
         }
 
       tlbl=newiTempLabel(NULL);
-      if (AOP_TYPE (IC_LEFT (ic)) == AOP_ACC ||
+      l = aopGet (AOP (IC_LEFT (ic)), offset++, FALSE, FALSE);
+      if ((AOP_TYPE (IC_LEFT (ic)) == AOP_ACC && offset == 0) ||
           AOP_TYPE (IC_LEFT (ic)) == AOP_REG ||
           IS_AOP_PREG (IC_LEFT (ic)))
         {
-          emitcode ("cjne", "%s,#0x01,%05d$",
-                    aopGet (AOP (IC_LEFT (ic)), 0, FALSE, FALSE),
-                    tlbl->key + 100);
+          emitcode ("cjne", "%s,#0xFF,%05d$", l, tlbl->key + 100);
         }
       else
         {
-          char *l = aopGet (AOP (IC_LEFT (ic)), 0, FALSE, FALSE);
           MOVA (l);
-          emitcode ("cjne", "a,#0x01,%05d$", tlbl->key + 100);
+          emitcode ("cjne", "a,#0xFF,%05d$", tlbl->key + 100);
         }
       emitcode ("", "%05d$:", tlbl->key + 100);
       outBitC (IC_RESULT(ic));
@@ -1792,7 +1898,7 @@ saveRegisters (iCode * lic)
        IFFUNC_ISNAKED(OP_SYM_TYPE(IC_LEFT (ic)))))
     return;
 
-  /* safe the registers in use at this time but skip the
+  /* save the registers in use at this time but skip the
      ones for the result */
   rsave = bitVectCplAnd (bitVectCopy (ic->rMask),
                          mcs51_rUmaskForOp (IC_RESULT(ic)));
@@ -1800,24 +1906,53 @@ saveRegisters (iCode * lic)
   ic->regsSaved = 1;
   if (options.useXstack)
     {
-      if (bitVectBitValue (rsave, R0_IDX))
-        emitcode ("mov", "b,r0");
-      emitcode ("mov", "r0,%s", spname);
-      for (i = 0; i < mcs51_nRegs; i++)
+      int count = bitVectnBitsOn (rsave);
+
+      if (count == 1)
         {
-          if (bitVectBitValue (rsave, i))
+          i = bitVectFirstBit (rsave);
+          emitcode ("mov", "a,%s", mcs51_regWithIdx (i)->name);
+          emitcode ("mov", "r0,%s", spname);
+          emitcode ("inc", "%s", spname);// allocate before use
+          emitcode ("movx", "@r0,a");
+          if (bitVectBitValue (rsave, R0_IDX))
+            emitcode ("mov", "r0,a");
+        }
+      else if (count != 0)
+        {
+          if (bitVectBitValue (rsave, R0_IDX))
             {
-              if (i == R0_IDX)
-                emitcode ("mov", "a,b");
-              else
-                emitcode ("mov", "a,%s", mcs51_regWithIdx (i)->name);
-              emitcode ("movx", "@r0,a");
-              emitcode ("inc", "r0");
+              emitcode ("push", "%s", mcs51_regWithIdx (R0_IDX)->dname);
+            }
+          emitcode ("mov", "r0,%s", spname);
+          MOVA ("r0");
+          emitcode ("add", "a,#%d", count);
+          emitcode ("mov", "%s,a", spname);
+          for (i = 0; i < mcs51_nRegs; i++)
+            {
+              if (bitVectBitValue (rsave, i))
+                {
+                  if (i == R0_IDX)
+                    {
+                      emitcode ("pop", "acc");
+                      emitcode ("push", "acc");
+                    }
+                  else
+                    {
+                      emitcode ("mov", "a,%s", mcs51_regWithIdx (i)->name);
+                    }
+                  emitcode ("movx", "@r0,a");
+                  if (--count)
+                    {
+                      emitcode ("inc", "r0");
+                    }
+                }
+            }
+          if (bitVectBitValue (rsave, R0_IDX))
+            {
+              emitcode ("pop", "%s", mcs51_regWithIdx (R0_IDX)->dname);
             }
         }
-      emitcode ("mov", "%s,r0", spname);
-      if (bitVectBitValue (rsave, R0_IDX))
-        emitcode ("mov", "r0,b");
     }
   else
     for (i = 0; i < mcs51_nRegs; i++)
@@ -1843,23 +1978,38 @@ unsaveRegisters (iCode * ic)
 
   if (options.useXstack)
     {
-      emitcode ("mov", "r0,%s", spname);
-      for (i = mcs51_nRegs; i >= 0; i--)
+      int count = bitVectnBitsOn (rsave);
+
+      if (count == 1)
         {
-          if (bitVectBitValue (rsave, i))
+          emitcode ("mov", "r0,%s", spname);
+          emitcode ("dec", "r0");
+          emitcode ("movx", "a,@r0");
+          i = bitVectFirstBit (rsave);
+          emitcode ("mov", "%s,a", mcs51_regWithIdx (i)->name);
+          emitcode ("dec", "%s", spname);
+        }
+      else if (count != 0)
+        {
+          emitcode ("mov", "r0,%s", spname);
+          for (i = mcs51_nRegs; i >= 0; i--)
             {
-              emitcode ("dec", "r0");
-              emitcode ("movx", "a,@r0");
-              if (i == R0_IDX)
-                emitcode ("mov", "b,a");
-              else
-                emitcode ("mov", "%s,a", mcs51_regWithIdx (i)->name);
+              if (bitVectBitValue (rsave, i))
+                {
+                  emitcode ("dec", "r0");
+                  emitcode ("movx", "a,@r0");
+                  if (i != R0_IDX)
+                    emitcode ("mov", "%s,a", mcs51_regWithIdx (i)->name);
+                  else
+                    emitcode ("push", "acc");
+                }
+            }
+          emitcode ("mov", "%s,r0", spname);
+          if (bitVectBitValue (rsave, R0_IDX))
+            {
+              emitcode ("pop", "ar0");
             }
-
         }
-      emitcode ("mov", "%s,r0", spname);
-      if (bitVectBitValue (rsave, R0_IDX))
-        emitcode ("mov", "r0,b");
     }
   else
     for (i = mcs51_nRegs; i >= 0; i--)
@@ -1867,7 +2017,6 @@ unsaveRegisters (iCode * ic)
         if (bitVectBitValue (rsave, i))
           emitcode ("pop", "%s", mcs51_regWithIdx (i)->dname);
       }
-
 }
 
 
@@ -1889,23 +2038,26 @@ pushSide (operand * oper, int size)
           emitcode ("push", "acc");
         }
       else
-        emitcode ("push", "%s", l);
+          emitcode ("push", "%s", l);
+        }
     }
-}
 
 /*-----------------------------------------------------------------*/
-/* assignResultValue -               */
+/* assignResultValue - also indicates if acc is in use afterwards  */
 /*-----------------------------------------------------------------*/
-static void
+static bool
 assignResultValue (operand * oper)
 {
   int offset = 0;
   int size = AOP_SIZE (oper);
+  bool accuse = FALSE;
+
   while (size--)
     {
-      aopPut (AOP (oper), fReturn[offset], offset, isOperandVolatile (oper, FALSE));
+      accuse |= aopPut (AOP (oper), fReturn[offset], offset, isOperandVolatile (oper, FALSE));
       offset++;
     }
+  return accuse;
 }
 
 
@@ -1924,30 +2076,37 @@ genXpush (iCode * ic)
   aopOp (IC_LEFT (ic), ic, FALSE);
   r = getFreePtr (ic, &aop, FALSE);
 
-
-  emitcode ("mov", "%s,_spx", r->name);
-
   size = AOP_SIZE (IC_LEFT (ic));
-  while (size--)
-    {
 
-      char *l = aopGet (AOP (IC_LEFT (ic)),
-                        offset++, FALSE, FALSE);
-      MOVA (l);
+  if (size == 1)
+    {
+      MOVA (aopGet (AOP (IC_LEFT (ic)), 0, FALSE, FALSE));
+      emitcode ("mov", "%s,%s", r->name, spname);
+      emitcode ("inc", "%s", spname); // allocate space first
       emitcode ("movx", "@%s,a", r->name);
-      emitcode ("inc", "%s", r->name);
-
     }
+  else
+    {
+      // allocate space first
+      emitcode ("mov", "%s,%s", r->name, spname);
+      MOVA (r->name);
+      emitcode ("add", "a,#%d", size);
+      emitcode ("mov", "%s,a", spname);
 
-
-  emitcode ("mov", "_spx,%s", r->name);
+      while (size--)
+        {
+          MOVA (aopGet (AOP (IC_LEFT (ic)), offset++, FALSE, FALSE));
+          emitcode ("movx", "@%s,a", r->name);
+          emitcode ("inc", "%s", r->name);
+        }
+    }
 
   freeAsmop (NULL, aop, ic, TRUE);
   freeAsmop (IC_LEFT (ic), NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
-/* genIpush - genrate code for pushing this gets a little complex  */
+/* genIpush - generate code for pushing this gets a little complex */
 /*-----------------------------------------------------------------*/
 static void
 genIpush (iCode * ic)
@@ -1998,7 +2157,6 @@ genIpush (iCode * ic)
   /* then do the push */
   aopOp (IC_LEFT (ic), ic, FALSE);
 
-
   // pushSide(IC_LEFT(ic), AOP_SIZE(IC_LEFT(ic)));
   size = AOP_SIZE (IC_LEFT (ic));
 
@@ -2013,7 +2171,7 @@ genIpush (iCode * ic)
           emitcode ("push", "acc");
         }
       else
-        emitcode ("push", "%s", l);
+          emitcode ("push", "%s", l);
     }
 
   freeAsmop (IC_LEFT (ic), NULL, ic, TRUE);
@@ -2044,17 +2202,18 @@ genIpop (iCode * ic)
 }
 
 /*-----------------------------------------------------------------*/
-/* unsaveRBank - restores the resgister bank from stack            */
+/* saveRBank - saves an entire register bank on the stack          */
 /*-----------------------------------------------------------------*/
 static void
-unsaveRBank (int bank, iCode * ic, bool popPsw)
+saveRBank (int bank, iCode * ic, bool pushPsw)
 {
   int i;
+  int count = mcs51_nRegs + (pushPsw ? 1 : 0);
   asmop *aop = NULL;
   regs *r = NULL;
 
   if (options.useXstack)
-  {
+    {
       if (!ic)
       {
           /* Assume r0 is available for use. */
@@ -2065,54 +2224,60 @@ unsaveRBank (int bank, iCode * ic, bool popPsw)
           aop = newAsmop (0);
           r = getFreePtr (ic, &aop, FALSE);
       }
-      emitcode ("mov", "%s,_spx", r->name);
-  }
+      // allocate space first
+      emitcode ("mov", "%s,%s", r->name, spname);
+      MOVA (r->name);
+      emitcode ("add", "a,#%d", count);
+      emitcode ("mov", "%s,a", spname);
+    }
 
-  if (popPsw)
+  for (i = 0; i < mcs51_nRegs; i++)
     {
       if (options.useXstack)
-      {
-          emitcode ("movx", "a,@%s", r->name);
-          emitcode ("mov", "psw,a");
-          emitcode ("dec", "%s", r->name);
+        {
+          emitcode ("mov", "a,(%s+%d)",
+                    regs8051[i].base, 8 * bank + regs8051[i].offset);
+          emitcode ("movx", "@%s,a", r->name);
+          if (--count)
+            emitcode ("inc", "%s", r->name);
         }
       else
-      {
-        emitcode ("pop", "psw");
-      }
+        emitcode ("push", "(%s+%d)",
+                  regs8051[i].base, 8 * bank + regs8051[i].offset);
     }
 
-  for (i = (mcs51_nRegs - 1); i >= 0; i--)
+  if (pushPsw)
     {
       if (options.useXstack)
         {
-          emitcode ("movx", "a,@%s", r->name);
-          emitcode ("mov", "(%s+%d),a",
-                    regs8051[i].base, 8 * bank + regs8051[i].offset);
-          emitcode ("dec", "%s", r->name);
+          emitcode ("mov", "a,psw");
+          emitcode ("movx", "@%s,a", r->name);
 
         }
       else
-        emitcode ("pop", "(%s+%d)",
-                  regs8051[i].base, 8 * bank + regs8051[i].offset);
+        {
+          emitcode ("push", "psw");
+        }
+
+      emitcode ("mov", "psw,#0x%02x", (bank << 3) & 0x00ff);
     }
 
-  if (options.useXstack)
+  if (aop)
     {
-      emitcode ("mov", "_spx,%s", r->name);
+      freeAsmop (NULL, aop, ic, TRUE);
     }
 
-  if (aop)
+  if (ic)
   {
-      freeAsmop (NULL, aop, ic, TRUE);
+    ic->bankSaved = 1;
   }
 }
 
 /*-----------------------------------------------------------------*/
-/* saveRBank - saves an entire register bank on the stack          */
+/* unsaveRBank - restores the register bank from stack             */
 /*-----------------------------------------------------------------*/
 static void
-saveRBank (int bank, iCode * ic, bool pushPsw)
+unsaveRBank (int bank, iCode * ic, bool popPsw)
 {
   int i;
   asmop *aop = NULL;
@@ -2121,59 +2286,57 @@ saveRBank (int bank, iCode * ic, bool pushPsw)
   if (options.useXstack)
     {
       if (!ic)
-      {
+        {
           /* Assume r0 is available for use. */
           r = mcs51_regWithIdx (R0_IDX);;
-      }
+        }
       else
-      {
+        {
           aop = newAsmop (0);
           r = getFreePtr (ic, &aop, FALSE);
-      }
-      emitcode ("mov", "%s,_spx", r->name);
+        }
+      emitcode ("mov", "%s,%s", r->name, spname);
     }
 
-  for (i = 0; i < mcs51_nRegs; i++)
+  if (popPsw)
     {
       if (options.useXstack)
         {
-          emitcode ("inc", "%s", r->name);
-          emitcode ("mov", "a,(%s+%d)",
-                    regs8051[i].base, 8 * bank + regs8051[i].offset);
-          emitcode ("movx", "@%s,a", r->name);
+          emitcode ("dec", "%s", r->name);
+          emitcode ("movx", "a,@%s", r->name);
+          emitcode ("mov", "psw,a");
         }
       else
-        emitcode ("push", "(%s+%d)",
-                  regs8051[i].base, 8 * bank + regs8051[i].offset);
+        {
+          emitcode ("pop", "psw");
+        }
     }
 
-  if (pushPsw)
+  for (i = (mcs51_nRegs - 1); i >= 0; i--)
     {
       if (options.useXstack)
         {
-          emitcode ("mov", "a,psw");
-          emitcode ("movx", "@%s,a", r->name);
-          emitcode ("inc", "%s", r->name);
-          emitcode ("mov", "_spx,%s", r->name);
-
+          emitcode ("dec", "%s", r->name);
+          emitcode ("movx", "a,@%s", r->name);
+          emitcode ("mov", "(%s+%d),a",
+                    regs8051[i].base, 8 * bank + regs8051[i].offset);
         }
       else
-      {
-        emitcode ("push", "psw");
-      }
-
-      emitcode ("mov", "psw,#0x%02x", (bank << 3) & 0x00ff);
+        {
+          emitcode ("pop", "(%s+%d)",
+                  regs8051[i].base, 8 * bank + regs8051[i].offset);
+        }
     }
 
-    if (aop)
+  if (options.useXstack)
     {
-        freeAsmop (NULL, aop, ic, TRUE);
+      emitcode ("mov", "%s,%s", spname, r->name);
     }
 
-  if (ic)
-  {
-      ic->bankSaved = 1;
-  }
+  if (aop)
+    {
+      freeAsmop (NULL, aop, ic, TRUE);
+    }
 }
 
 /*-----------------------------------------------------------------*/
@@ -2218,11 +2381,13 @@ genCall (iCode * ic)
   sym_link *dtype;
 //  bool restoreBank = FALSE;
   bool swapBanks = FALSE;
+  bool accuse = FALSE;
+  bool accPushed = FALSE;
 
   D(emitcode(";     genCall",""));
 
   dtype = operandType (IC_LEFT (ic));
-  /* if send set is not empty the assign */
+  /* if send set is not empty then assign */
   if (_G.sendSet)
     {
         if (IFFUNC_ISREENT(dtype)) { /* need to reverse the send set */
@@ -2278,21 +2443,34 @@ genCall (iCode * ic)
       aopOp (IC_RESULT (ic), ic, FALSE);
       _G.accInUse--;
 
-      assignResultValue (IC_RESULT (ic));
+      accuse = assignResultValue (IC_RESULT (ic));
 
       freeAsmop (IC_RESULT (ic), NULL, ic, TRUE);
     }
 
-  /* adjust the stack for parameters if
-     required */
+  /* adjust the stack for parameters if required */
   if (ic->parmBytes)
     {
       int i;
       if (ic->parmBytes > 3)
         {
+          if (accuse)
+            {
+              emitcode ("push", "acc");
+              accPushed = TRUE;
+            }
+
           emitcode ("mov", "a,%s", spname);
           emitcode ("add", "a,#0x%02x", (-ic->parmBytes) & 0xff);
           emitcode ("mov", "%s,a", spname);
+
+          /* unsaveRegisters from xstack needs acc, but */
+          /* unsaveRegisters from stack needs this popped */
+          if (accPushed && !options.useXstack)
+            {
+              emitcode ("pop", "acc");
+              accPushed = FALSE;
+            }
         }
       else
         for (i = 0; i < ic->parmBytes; i++)
@@ -2301,11 +2479,22 @@ genCall (iCode * ic)
 
   /* if we hade saved some registers then unsave them */
   if (ic->regsSaved && !IFFUNC_CALLEESAVES(dtype))
-    unsaveRegisters (ic);
+    {
+      if (accuse && !accPushed && options.useXstack)
+        {
+          /* xstack needs acc, but doesn't touch normal stack */
+          emitcode ("push", "acc");
+          accPushed = TRUE;
+        }
+      unsaveRegisters (ic);
+    }
 
 //  /* if register bank was saved then pop them */
 //  if (restoreBank)
 //    unsaveRBank (FUNC_REGBANK (dtype), ic, FALSE);
+
+  if (accPushed)
+    emitcode ("pop", "acc");
 }
 
 /*-----------------------------------------------------------------*/
@@ -2471,13 +2660,14 @@ inExcludeList (char *s)
 static void
 genFunction (iCode * ic)
 {
-  symbol *sym = OP_SYMBOL (IC_LEFT (ic));
+  symbol   *sym = OP_SYMBOL (IC_LEFT (ic));
   sym_link *ftype;
-  bool   switchedPSW = FALSE;
-  int calleesaves_saved_register = -1;
-  int stackAdjust = sym->stack;
-  int accIsFree = sym->recvSize < 4;
-  iCode * ric = (ic->next && ic->next->op == RECEIVE) ? ic->next : NULL;
+  bool     switchedPSW = FALSE;
+  int      calleesaves_saved_register = -1;
+  int      stackAdjust = sym->stack;
+  int      accIsFree = sym->recvSize < 4;
+  iCode    *ric = (ic->next && ic->next->op == RECEIVE) ? ic->next : NULL;
+  bool     fReentrant = (IFFUNC_ISREENT (sym->type) || options.stackAuto);
 
   _G.nRegsSaved = 0;
   /* create the function header */
@@ -2698,26 +2888,26 @@ genFunction (iCode * ic)
     }
 
 
-  if (IFFUNC_ISREENT (sym->type) || options.stackAuto)
+  if (fReentrant)
     {
-
       if (options.useXstack)
         {
-          if (!accIsFree)
-            emitcode ("push", "acc");
           emitcode ("mov", "r0,%s", spname);
-          emitcode ("mov", "a,_bp");
-          emitcode ("movx", "@r0,a");
           emitcode ("inc", "%s", spname);
-          if (!accIsFree)
-            emitcode ("pop", "acc");
+          emitcode ("xch", "a,_bpx");
+          emitcode ("movx", "@r0,a");
+          emitcode ("inc", "r0");
+          emitcode ("mov", "a,r0");
+          emitcode ("xch", "a,_bpx");
+          emitcode ("push", "_bp");     /* save the callers stack  */
+          emitcode ("mov", "_bp,sp");
         }
       else
         {
           /* set up the stack */
           emitcode ("push", "_bp");     /* save the callers stack  */
+          emitcode ("mov", "_bp,sp");
         }
-      emitcode ("mov", "_bp,%s", spname);
     }
 
   /* For some cases it is worthwhile to perform a RECEIVE iCode */
@@ -2783,18 +2973,15 @@ genFunction (iCode * ic)
   /* adjust the stack for the function */
   if (stackAdjust)
     {
-
       int i = stackAdjust;
       if (i > 256)
         werror (W_STACK_OVERFLOW, sym->name);
 
       if (i > 3 && accIsFree)
         {
-
           emitcode ("mov", "a,sp");
           emitcode ("add", "a,#0x%02x", ((char) sym->stack & 0xff));
           emitcode ("mov", "sp,a");
-
         }
       else if (i > 5)
         {
@@ -2836,14 +3023,27 @@ genFunction (iCode * ic)
 
   if (sym->xstack)
     {
+      char i = ((char) sym->xstack & 0xff);
 
-      if (!accIsFree)
-        emitcode ("push", "acc");
-      emitcode ("mov", "a,_spx");
-      emitcode ("add", "a,#0x%02x", ((char) sym->xstack & 0xff));
-      emitcode ("mov", "_spx,a");
-      if (!accIsFree)
-        emitcode ("pop", "acc");
+      if (i > 3 && accIsFree)
+        {
+          emitcode ("mov", "a,_spx");
+          emitcode ("add", "a,#0x%02x", i);
+          emitcode ("mov", "_spx,a");
+        }
+      else if (i > 5)
+        {
+          emitcode ("push", "acc");
+          emitcode ("mov", "a,_spx");
+          emitcode ("add", "a,#0x%02x", i);
+          emitcode ("mov", "_spx,a");
+          emitcode ("pop", "acc");
+        }
+      else
+        {
+          while (i--)
+            emitcode ("inc", "_spx");
+        }
     }
 
   /* if critical function then turn interrupts off */
@@ -2864,12 +3064,12 @@ genFunction (iCode * ic)
 static void
 genEndFunction (iCode * ic)
 {
-  symbol *sym = OP_SYMBOL (IC_LEFT (ic));
+  symbol   *sym = OP_SYMBOL (IC_LEFT (ic));
   lineNode *lnp = lineCurr;
-  bitVect *regsUsed;
-  bitVect *regsUsedPrologue;
-  bitVect *regsUnneeded;
-  int idx;
+  bitVect  *regsUsed;
+  bitVect  *regsUsedPrologue;
+  bitVect  *regsUnneeded;
+  int      idx;
 
   _G.currentFunc = NULL;
   if (IFFUNC_ISNAKED(sym->type))
@@ -2886,34 +3086,19 @@ genEndFunction (iCode * ic)
       emitcode ("mov", "ea,c");
     }
 
-  if (IFFUNC_ISREENT (sym->type) || options.stackAuto)
-    {
-      emitcode ("mov", "%s,_bp", spname);
-    }
-
-  /* if use external stack but some variables were
-     added to the local stack then decrement the
-     local stack */
-  if (options.useXstack && sym->stack)
-    {
-      emitcode ("mov", "a,sp");
-      emitcode ("add", "a,#0x%02x", ((char) -sym->stack) & 0xff);
-      emitcode ("mov", "sp,a");
-    }
-
-
   if ((IFFUNC_ISREENT (sym->type) || options.stackAuto))
     {
+      emitcode ("mov", "sp,_bp");
+      emitcode ("pop", "_bp");
+
       if (options.useXstack)
         {
-          emitcode ("mov", "r0,%s", spname);
+          emitcode ("xch", "a,_bpx");
+          emitcode ("mov", "r0,a");
+          emitcode ("dec", "r0");
           emitcode ("movx", "a,@r0");
-          emitcode ("mov", "_bp,a");
-          emitcode ("dec", "%s", spname);
-        }
-      else
-        {
-          emitcode ("pop", "_bp");
+          emitcode ("xch", "a,_bpx");
+          emitcode ("mov", "%s,r0", spname); //read before freeing stack space (interrupts)
         }
     }
 
@@ -3644,11 +3829,13 @@ genPlus (iCode * ic)
         {
           if (aopGetUsesAcc (leftOp, offset) && aopGetUsesAcc (rightOp, offset))
             {
-              emitcode("mov", "b,a");
+              bool pushedB;
               MOVA (aopGet (leftOp,  offset, FALSE, TRUE));
+              pushedB = pushB ();
               emitcode("xch", "a,b");
               MOVA (aopGet (rightOp, offset, FALSE, TRUE));
               emitcode (add, "a,b");
+              popB (pushedB);
             }
           else if (aopGetUsesAcc (leftOp, offset))
             {
@@ -3925,13 +4112,26 @@ genMinus (iCode * ic)
       while (size--)
         {
           if (aopGetUsesAcc(rightOp, offset)) {
-            wassertl(!aopGetUsesAcc(leftOp, offset), "accumulator clash");
-            MOVA (aopGet(rightOp, offset, FALSE, TRUE));
-            if (offset == 0) {
-              emitcode( "setb", "c");
+            if (aopGetUsesAcc(leftOp, offset)) {
+              bool pushedB;
+
+              MOVA (aopGet (rightOp, offset, FALSE, FALSE));
+              pushedB = pushB ();
+              emitcode ("mov", "b,a");
+              if (offset == 0)
+                CLRC;
+              MOVA (aopGet (leftOp, offset, FALSE, FALSE));
+              emitcode ("subb", "a,b");
+              popB (pushedB);
+            } else {
+              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");
             }
-            emitcode("subb", "a,%s", aopGet(leftOp, offset, FALSE, TRUE));
-            emitcode("cpl", "a");
           } else {
             MOVA (aopGet (leftOp, offset, FALSE, FALSE));
             if (offset == 0)
@@ -3980,7 +4180,7 @@ genMultOneByte (operand * left,
   symbol *lbl;
   int size = AOP_SIZE (result);
   bool runtimeSign, compiletimeSign;
-  bool lUnsigned, rUnsigned;
+  bool lUnsigned, rUnsigned, pushedB;
 
   D(emitcode (";     genMultOneByte",""));
 
@@ -4013,6 +4213,8 @@ genMultOneByte (operand * left,
   lUnsigned = SPEC_USIGN (getSpec (operandType (left)));
   rUnsigned = SPEC_USIGN (getSpec (operandType (right)));
 
+  pushedB = pushB ();
+
   if (size == 1 /* no, this is not a bug; with a 1 byte result there's
                    no need to take care about the signedness! */
       || (lUnsigned && rUnsigned))
@@ -4038,6 +4240,8 @@ genMultOneByte (operand * left,
       aopPut (AOP (result), "a", 0, isOperandVolatile (result, FALSE));
       if (size == 2)
         aopPut (AOP (result), "b", 1, isOperandVolatile (result, FALSE));
+
+      popB (pushedB);
       return;
     }
 
@@ -4164,6 +4368,8 @@ genMultOneByte (operand * left,
   aopPut (AOP (result), "a", 0, isOperandVolatile (result, FALSE));
   if (size == 2)
     aopPut (AOP (result), "b", 1, isOperandVolatile (result, FALSE));
+
+  popB (pushedB);
 }
 
 /*-----------------------------------------------------------------*/
@@ -4223,11 +4429,13 @@ genDivbits (operand * left,
             operand * right,
             operand * result)
 {
-
   char *l;
+  bool pushedB;
 
   D(emitcode (";     genDivbits",""));
 
+  pushedB = pushB ();
+
   /* the result must be bit */
   emitcode ("mov", "b,%s", aopGet (AOP (right), 0, FALSE, FALSE));
   l = aopGet (AOP (left), 0, FALSE, FALSE);
@@ -4236,6 +4444,9 @@ genDivbits (operand * left,
 
   emitcode ("div", "ab");
   emitcode ("rrc", "a");
+
+  popB (pushedB);
+
   aopPut (AOP (result), "c", 0, isOperandVolatile (result, FALSE));
 }
 
@@ -4247,7 +4458,7 @@ genDivOneByte (operand * left,
                operand * right,
                operand * result)
 {
-  bool lUnsigned, rUnsigned;
+  bool lUnsigned, rUnsigned, pushedB;
   bool runtimeSign, compiletimeSign;
   symbol *lbl;
   int size, offset;
@@ -4285,6 +4496,8 @@ genDivOneByte (operand * left,
   lUnsigned = SPEC_USIGN (getSpec (operandType (left)));
   rUnsigned = SPEC_USIGN (getSpec (operandType (right)));
 
+  pushedB = pushB ();
+
   /* signed or unsigned */
   if (lUnsigned && rUnsigned)
     {
@@ -4295,6 +4508,8 @@ genDivOneByte (operand * left,
       aopPut (AOP (result), "a", 0, isOperandVolatile (result, FALSE));
       while (size--)
         aopPut (AOP (result), zero, offset++, isOperandVolatile (result, FALSE));
+
+      popB (pushedB);
       return;
     }
 
@@ -4430,6 +4645,8 @@ genDivOneByte (operand * left,
       while (size--)
         aopPut (AOP (result), zero, offset++, isOperandVolatile (result, FALSE));
     }
+
+  popB (pushedB);
 }
 
 /*-----------------------------------------------------------------*/
@@ -4482,11 +4699,13 @@ genModbits (operand * left,
             operand * right,
             operand * result)
 {
-
   char *l;
+  bool pushedB;
 
   D(emitcode (";     genModbits",""));
 
+  pushedB = pushB ();
+
   /* the result must be bit */
   emitcode ("mov", "b,%s", aopGet (AOP (right), 0, FALSE, FALSE));
   l = aopGet (AOP (left), 0, FALSE, FALSE);
@@ -4496,6 +4715,9 @@ genModbits (operand * left,
   emitcode ("div", "ab");
   emitcode ("mov", "a,b");
   emitcode ("rrc", "a");
+
+  popB (pushedB);
+
   aopPut (AOP (result), "c", 0, isOperandVolatile (result, FALSE));
 }
 
@@ -4507,7 +4729,7 @@ genModOneByte (operand * left,
                operand * right,
                operand * result)
 {
-  bool lUnsigned, rUnsigned;
+  bool lUnsigned, rUnsigned, pushedB;
   bool runtimeSign, compiletimeSign;
   symbol *lbl;
   int size, offset;
@@ -4519,6 +4741,63 @@ genModOneByte (operand * left,
   lUnsigned = SPEC_USIGN (getSpec (operandType (left)));
   rUnsigned = SPEC_USIGN (getSpec (operandType (right)));
 
+  /* if right is a literal, check it for 2^n */
+  if (AOP_TYPE(right) == AOP_LIT)
+    {
+      unsigned char val = abs((int) operandLitValue(right));
+      symbol *lbl2 = NULL;
+
+      switch (val)
+        {
+          case 1: /* sometimes it makes sense (on tricky code and hardware)... */
+          case 2:
+          case 4:
+          case 8:
+          case 16:
+          case 32:
+          case 64:
+          case 128:
+            if (lUnsigned)
+              werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
+                      "modulus of unsigned char by 2^n literal shouldn't be processed here");
+              /* because iCode should have been changed to genAnd  */
+              /* see file "SDCCopt.c", function "convertToFcall()" */
+
+            MOVA (aopGet (AOP (left), 0, FALSE, FALSE));
+            emitcode ("mov", "c,acc.7");
+            emitcode ("anl", "a,#0x%02x", val - 1);
+            lbl = newiTempLabel (NULL);
+            emitcode ("jz", "%05d$", (lbl->key + 100));
+            emitcode ("jnc", "%05d$", (lbl->key + 100));
+            emitcode ("orl", "a,#0x%02x", 0xff ^ (val - 1));
+            if (size)
+              {
+                int size2 = size;
+                int offs2 = offset;
+
+                aopPut (AOP (result), "a", 0, isOperandVolatile (result, FALSE));
+                while (size2--)
+                  aopPut (AOP (result), "#0xff", offs2++, isOperandVolatile (result, FALSE));
+                lbl2 = newiTempLabel (NULL);
+                emitcode ("sjmp", "%05d$", (lbl2->key + 100));
+              }
+            emitcode ("", "%05d$:", (lbl->key + 100));
+            aopPut (AOP (result), "a", 0, isOperandVolatile (result, FALSE));
+            while (size--)
+              aopPut (AOP (result), zero, offset++, isOperandVolatile (result, FALSE));
+            if (lbl2)
+              {
+                emitcode ("", "%05d$:", (lbl2->key + 100));
+              }
+            return;
+
+          default:
+            break;
+        }
+    }
+
+  pushedB = pushB ();
+
   /* signed or unsigned */
   if (lUnsigned && rUnsigned)
     {
@@ -4529,6 +4808,8 @@ genModOneByte (operand * left,
       aopPut (AOP (result), "b", 0, isOperandVolatile (result, FALSE));
       while (size--)
         aopPut (AOP (result), zero, offset++, isOperandVolatile (result, FALSE));
+
+      popB (pushedB);
       return;
     }
 
@@ -4539,7 +4820,7 @@ genModOneByte (operand * left,
   /* modulus: sign of the right operand has no influence on the result! */
   if (AOP_TYPE(right) == AOP_LIT)
     {
-      signed char val = (char) floatFromVal (AOP (right)->aopu.aop_lit);
+      signed char val = (char) operandLitValue(right);
 
       if (!rUnsigned && val < 0)
         emitcode ("mov", "b,#0x%02x", -val);
@@ -4634,6 +4915,8 @@ genModOneByte (operand * left,
       while (size--)
         aopPut (AOP (result), zero, offset++, isOperandVolatile (result, FALSE));
     }
+
+  popB (pushedB);
 }
 
 /*-----------------------------------------------------------------*/
@@ -4648,7 +4931,7 @@ genMod (iCode * ic)
 
   D(emitcode (";     genMod",""));
 
-  /* assign the amsops */
+  /* assign the asmops */
   aopOp (left, ic, FALSE);
   aopOp (right, ic, FALSE);
   aopOp (result, ic, TRUE);
@@ -4790,9 +5073,13 @@ genCmp (operand * left, operand * right,
           CLRC;
           while (size--)
             {
+              bool pushedB = FALSE;
               rightInB = aopGetUsesAcc(AOP (right), offset);
               if (rightInB)
-                emitcode ("mov", "b,%s", aopGet (AOP (right), offset, FALSE, FALSE));
+                {
+                  pushedB = pushB ();
+                  emitcode ("mov", "b,%s", aopGet (AOP (right), offset, FALSE, FALSE));
+                }
               MOVA (aopGet (AOP (left), offset, FALSE, FALSE));
               if (sign && size == 0)
                 {
@@ -4807,7 +5094,11 @@ genCmp (operand * left, operand * right,
                   else
                     {
                       if (!rightInB)
-                        emitcode ("mov", "b,%s", aopGet (AOP (right), offset, FALSE, FALSE));
+                        {
+                          pushedB = pushB ();
+                          rightInB++;
+                          emitcode ("mov", "b,%s", aopGet (AOP (right), offset, FALSE, FALSE));
+                        }
                       emitcode ("xrl", "b,#0x80");
                       emitcode ("subb", "a,b");
                     }
@@ -4819,6 +5110,8 @@ genCmp (operand * left, operand * right,
                   else
                     emitcode ("subb", "a,%s", aopGet (AOP (right), offset, FALSE, FALSE));
                 }
+              if (rightInB)
+                popB (pushedB);
               offset++;
             }
         }
@@ -4869,7 +5162,7 @@ genCmpGt (iCode * ic, iCode * ifx)
   aopOp (right, ic, FALSE);
   aopOp (result, ic, TRUE);
 
-  genCmp (right, left, result, ifx, sign,ic);
+  genCmp (right, left, result, ifx, sign, ic);
 
   freeAsmop (result, NULL, ic, TRUE);
 }
@@ -4971,7 +5264,10 @@ gencjneshort (operand * left, operand * right, symbol * lbl)
       /* right is a pointer reg need both a & b */
       while (size--)
         {
-          char *l = aopGet (AOP (left), offset, FALSE, FALSE);
+          char *l;
+          //if B in use: push B; mov B,left; mov A,right; clrc; subb A,B; pop B; jnz
+          wassertl(!BINUSE, "B was in use");
+          l = aopGet (AOP (left), offset, FALSE, FALSE);
           if (strcmp (l, "b"))
             emitcode ("mov", "b,%s", l);
           MOVA (aopGet (AOP (right), offset, FALSE, FALSE));
@@ -5226,7 +5522,7 @@ hasInc (operand *op, iCode *ic,int osize)
       return lic;
     }
     /* if the operand used or deffed */
-    if (bitVectBitValue(OP_USES(op),lic->key) || (unsigned) lic->defKey == op->key) {
+    if (bitVectBitValue(OP_USES(op),lic->key) || lic->defKey == op->key) {
       return NULL;
     }
     /* if GOTO or IFX */
@@ -5520,7 +5816,17 @@ genAnd (iCode * ic, iCode * ifx)
           MOVA (aopGet (AOP (left), posbit >> 3, FALSE, FALSE));
           // bit = left & 2^n
           if (size)
-            emitcode ("mov", "c,acc.%d", posbit & 0x07);
+            {
+              switch (posbit & 0x07)
+                {
+                  case 0: emitcode ("rrc", "a");
+                          break;
+                  case 7: emitcode ("rlc", "a");
+                          break;
+                  default: emitcode ("mov", "c,acc.%d", posbit & 0x07);
+                          break;
+                }
+            }
           // if(left &  2^n)
           else
             {
@@ -5530,7 +5836,7 @@ genAnd (iCode * ic, iCode * ifx)
                             "acc.%d", posbit & 0x07);
                   genIfxJump (ifx, buffer, left, right, result);
                 }
-              else 
+              else
                 {// what is this case? just found it in ds390/gen.c
                    emitcode ("anl","a,#!constbyte",1 << (posbit & 0x07));
                 }
@@ -5595,7 +5901,7 @@ genAnd (iCode * ic, iCode * ifx)
                   if (isOperandVolatile (left, FALSE))
                     MOVA (aopGet (AOP (left), offset, FALSE, FALSE));
                   else
-                continue;
+                    continue;
                 }
               else if (bytelit == 0)
                 {
@@ -5603,8 +5909,8 @@ genAnd (iCode * ic, iCode * ifx)
                 }
               else if (IS_AOP_PREG (result))
                 {
-                  MOVA (aopGet (AOP (right), offset, FALSE, FALSE));
-                  emitcode ("anl", "a,%s", aopGet (AOP (left), offset, FALSE, TRUE));
+                  MOVA (aopGet (AOP (left), offset, FALSE, TRUE));
+                  emitcode ("anl", "a,%s", aopGet (AOP (right), offset, FALSE, FALSE));
                   aopPut (AOP (result), "a", offset, isOperandVolatile (result, FALSE));
                 }
               else
@@ -5614,8 +5920,10 @@ genAnd (iCode * ic, iCode * ifx)
             }
           else
             {
-              if (AOP_TYPE (left) == AOP_ACC)
-                emitcode ("anl", "a,%s", aopGet (AOP (right), offset, FALSE, FALSE));
+              if (AOP_TYPE (left) == AOP_ACC && offset == 0)
+                {
+                  emitcode ("anl", "a,%s", aopGet (AOP (right), offset, FALSE, FALSE));
+                }
               else
                 {
                   MOVA (aopGet (AOP (right), offset, FALSE, FALSE));
@@ -5645,15 +5953,30 @@ genAnd (iCode * ic, iCode * ifx)
             emitcode ("setb", "c");
           while (sizer--)
             {
-              if (AOP_TYPE(right)==AOP_REG && AOP_TYPE(left)==AOP_ACC) {
-                emitcode ("anl", "a,%s",
-                          aopGet (AOP (right), offset, FALSE, FALSE));
-              } else {
-                if (AOP_TYPE(left)==AOP_ACC) {
-                  emitcode("mov", "b,a");
-                  MOVA (aopGet (AOP (right), offset, FALSE, FALSE));
-                  emitcode("anl", "a,b");
-                }else {
+              if ((AOP_TYPE(right)==AOP_REG  || IS_AOP_PREG(right) || AOP_TYPE(right)==AOP_DIR)
+                  && AOP_TYPE(left)==AOP_ACC)
+                {
+                  if (offset)
+                    emitcode("mov", "a,b");
+                  emitcode ("anl", "a,%s",
+                            aopGet (AOP (right), offset, FALSE, FALSE));
+                } else {
+                  if (AOP_TYPE(left)==AOP_ACC)
+                    {
+                      if (!offset)
+                        {
+                          bool pushedB = pushB ();
+                          emitcode("mov", "b,a");
+                          MOVA (aopGet (AOP (right), offset, FALSE, FALSE));
+                          emitcode("anl", "a,b");
+                          popB (pushedB);
+                        }
+                      else
+                        {
+                          MOVA (aopGet (AOP (right), offset, FALSE, FALSE));
+                          emitcode("anl", "a,b");
+                        }
+                } else {
                   MOVA (aopGet (AOP (right), offset, FALSE, FALSE));
                   emitcode ("anl", "a,%s",
                             aopGet (AOP (left), offset, FALSE, FALSE));
@@ -5698,11 +6021,30 @@ genAnd (iCode * ic, iCode * ifx)
                       aopPut (AOP (result), zero, offset, isOperandVolatile (result, FALSE));
                       continue;
                     }
+                  else if (AOP_TYPE (left) == AOP_ACC)
+                    {
+                      if (!offset)
+                        {
+                          emitcode ("anl", "a,%s", aopGet (AOP (right), offset, FALSE, FALSE));
+                          aopPut (AOP (result), "a", offset, isOperandVolatile (result, FALSE));
+                          continue;
+                        }
+                      else
+                        {
+                          emitcode ("anl", "b,%s", aopGet (AOP (right), offset, FALSE, FALSE));
+                          aopPut (AOP (result), "b", offset, isOperandVolatile (result, FALSE));
+                          continue;
+                        }
+                    }
                 }
               // faster than result <- left, anl result,right
               // and better if result is SFR
               if (AOP_TYPE (left) == AOP_ACC)
-                emitcode ("anl", "a,%s", aopGet (AOP (right), offset, FALSE, FALSE));
+                {
+                  if (offset)
+                    emitcode("mov", "a,b");
+                  emitcode ("anl", "a,%s", aopGet (AOP (right), offset, FALSE, FALSE));
+                }
               else
                 {
                   MOVA (aopGet (AOP (right), offset, FALSE, FALSE));
@@ -5904,8 +6246,8 @@ genOr (iCode * ic, iCode * ifx)
                 }
               else if (IS_AOP_PREG (left))
                 {
-                  MOVA (aopGet (AOP (right), offset, FALSE, FALSE));
-                  emitcode ("orl", "a,%s", aopGet (AOP (left), offset, FALSE, TRUE));
+                  MOVA (aopGet (AOP (left), offset, FALSE, TRUE));
+                  emitcode ("orl", "a,%s", aopGet (AOP (right), offset, FALSE, FALSE));
                   aopPut (AOP (result), "a", offset, isOperandVolatile (result, FALSE));
                 }
               else
@@ -5918,7 +6260,11 @@ genOr (iCode * ic, iCode * ifx)
           else
             {
               if (AOP_TYPE (left) == AOP_ACC)
-                emitcode ("orl", "a,%s", aopGet (AOP (right), offset, FALSE, FALSE));
+                {
+                  if (offset)
+                    emitcode("mov", "a,b");
+                  emitcode ("orl", "a,%s", aopGet (AOP (right), offset, FALSE, FALSE));
+                }
               else
                 {
                   MOVA (aopGet (AOP (right), offset, FALSE, FALSE));
@@ -5951,6 +6297,8 @@ genOr (iCode * ic, iCode * ifx)
           while (sizer--)
             {
               if (AOP_TYPE(right)==AOP_REG && AOP_TYPE(left)==AOP_ACC) {
+                if (offset)
+                  emitcode("mov", "a,b");
                 emitcode ("orl", "a,%s",
                           aopGet (AOP (right), offset, FALSE, FALSE));
               } else {
@@ -6001,7 +6349,11 @@ genOr (iCode * ic, iCode * ifx)
               // faster than result <- left, anl result,right
               // and better if result is SFR
               if (AOP_TYPE (left) == AOP_ACC)
-                emitcode ("orl", "a,%s", aopGet (AOP (right), offset, FALSE, FALSE));
+                {
+                  if (offset)
+                    emitcode("mov", "a,b");
+                  emitcode ("orl", "a,%s", aopGet (AOP (right), offset, FALSE, FALSE));
+                }
               else
                 {
                   MOVA (aopGet (AOP (right), offset, FALSE, FALSE));
@@ -6178,12 +6530,12 @@ genXor (iCode * ic, iCode * ifx)
                   if (isOperandVolatile (left, FALSE))
                     MOVA (aopGet (AOP (left), offset, FALSE, FALSE));
                   else
-                continue;
+                    continue;
                 }
               else if (IS_AOP_PREG (left))
                 {
-                  MOVA (aopGet (AOP (right), offset, FALSE, FALSE));
-                  emitcode ("xrl", "a,%s", aopGet (AOP (left), offset, FALSE, TRUE));
+                  MOVA (aopGet (AOP (left), offset, FALSE, TRUE));
+                  emitcode ("xrl", "a,%s", aopGet (AOP (right), offset, FALSE, FALSE));
                   aopPut (AOP (result), "a", offset, isOperandVolatile (result, FALSE));
                 }
               else
@@ -6196,7 +6548,11 @@ genXor (iCode * ic, iCode * ifx)
           else
             {
               if (AOP_TYPE (left) == AOP_ACC)
-                emitcode ("xrl", "a,%s", aopGet (AOP (right), offset, FALSE, FALSE));
+                {
+                  if (offset)
+                    emitcode("mov", "a,b");
+                  emitcode ("xrl", "a,%s", aopGet (AOP (right), offset, FALSE, FALSE));
+                }
               else
                 {
                   MOVA (aopGet (AOP (right), offset, FALSE, FALSE));
@@ -6234,6 +6590,8 @@ genXor (iCode * ic, iCode * ifx)
               else
                 {
                   if (AOP_TYPE(right)==AOP_REG && AOP_TYPE(left)==AOP_ACC) {
+                    if (offset)
+                      emitcode("mov", "a,b");
                     emitcode ("xrl", "a,%s",
                               aopGet (AOP (right), offset, FALSE, FALSE));
                   } else {
@@ -6275,7 +6633,11 @@ genXor (iCode * ic, iCode * ifx)
               // faster than result <- left, anl result,right
               // and better if result is SFR
               if (AOP_TYPE (left) == AOP_ACC)
-                emitcode ("xrl", "a,%s", aopGet (AOP (right), offset, FALSE, FALSE));
+                {
+                  if (offset)
+                    emitcode("mov", "a,b");
+                  emitcode ("xrl", "a,%s", aopGet (AOP (right), offset, FALSE, FALSE));
+                }
               else
                 {
                   MOVA (aopGet (AOP (right), offset, FALSE, FALSE));
@@ -6365,7 +6727,7 @@ genRRC (iCode * ic)
       emitcode ("rr", "a");
       goto release;
   }
-  CLRC;
+  /* no need to clear carry, bit7 will be written later */
   while (size--)
     {
       l = aopGet (AOP (left), offset, FALSE, FALSE);
@@ -6417,7 +6779,7 @@ genRLC (iCode * ic)
               emitcode("rl","a");
               goto release;
       }
-      emitcode ("add", "a,acc");
+      emitcode("rlc","a"); /* bit0 will be written later */
       if (AOP_SIZE (result) > 1)
         aopPut (AOP (result), "a", offset++, isOperandVolatile (result, FALSE));
       while (size--)
@@ -6510,15 +6872,22 @@ genSwap (iCode * ic)
       else if (operandsEqu (left, result))
         {
           char * reg = "a";
+          bool pushedB = FALSE, leftInB = FALSE;
+
           MOVA (aopGet (AOP (left), 0, FALSE, FALSE));
           if (aopGetUsesAcc(AOP (left), 1) || aopGetUsesAcc(AOP (result), 0))
             {
+              pushedB = pushB ();
               emitcode ("mov", "b,a");
               reg = "b";
+              leftInB = TRUE;
             }
           aopPut (AOP (result), aopGet (AOP (left), 1, FALSE, FALSE),
                   0, isOperandVolatile (result, FALSE));
           aopPut (AOP (result), reg, 1, isOperandVolatile (result, FALSE));
+
+          if (leftInB)
+            popB (pushedB);
         }
       else
         {
@@ -7202,7 +7571,7 @@ shiftLLong (operand * left, operand * result, int offr)
 
   if (size >= MSB24 + offr)
     {
-      if (!(sameRegs (AOP (left), AOP (left)) && size >= MSB24 + offr && offr != LSB))
+      if (!(sameRegs (AOP (result), AOP (left)) && size >= MSB24 + offr && offr != LSB))
         {
           l = aopGet (AOP (left), MSB24, FALSE, FALSE);
           MOVA (l);
@@ -7398,6 +7767,7 @@ genLeftShift (iCode * ic)
   int size, offset;
   char *l;
   symbol *tlbl, *tlbl1;
+  bool pushedB;
 
   D(emitcode (";     genLeftShift",""));
 
@@ -7421,14 +7791,14 @@ genLeftShift (iCode * ic)
      more that 32 bits make no sense anyway, ( the
      largest size of an object can be only 32 bits ) */
 
+  pushedB = pushB ();
   emitcode ("mov", "b,%s", aopGet (AOP (right), 0, FALSE, FALSE));
   emitcode ("inc", "b");
   freeAsmop (right, NULL, ic, TRUE);
   aopOp (left, ic, FALSE);
   aopOp (result, ic, FALSE);
 
-  /* now move the left to the result if they are not the
-     same */
+  /* now move the left to the result if they are not the same */
   if (!sameRegs (AOP (left), AOP (result)) &&
       AOP_SIZE (result) > 1)
     {
@@ -7467,6 +7837,7 @@ genLeftShift (iCode * ic)
       emitcode ("add", "a,acc");
       emitcode ("", "%05d$:", tlbl1->key + 100);
       emitcode ("djnz", "b,%05d$", tlbl->key + 100);
+      popB (pushedB);
       aopPut (AOP (result), "a", 0, isOperandVolatile (result, FALSE));
       goto release;
     }
@@ -7490,6 +7861,7 @@ genLeftShift (iCode * ic)
 
   emitcode ("", "%05d$:", tlbl1->key + 100);
   emitcode ("djnz", "b,%05d$", tlbl->key + 100);
+  popB (pushedB);
 release:
   freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
@@ -7745,6 +8117,7 @@ genSignedRightShift (iCode * ic)
   int size, offset;
   char *l;
   symbol *tlbl, *tlbl1;
+  bool pushedB;
 
   D(emitcode (";     genSignedRightShift",""));
 
@@ -7769,6 +8142,7 @@ genSignedRightShift (iCode * ic)
      more that 32 bits make no sense anyway, ( the
      largest size of an object can be only 32 bits ) */
 
+  pushedB = pushB ();
   emitcode ("mov", "b,%s", aopGet (AOP (right), 0, FALSE, FALSE));
   emitcode ("inc", "b");
   freeAsmop (right, NULL, ic, TRUE);
@@ -7818,6 +8192,7 @@ genSignedRightShift (iCode * ic)
       emitcode ("rrc", "a");
       emitcode ("", "%05d$:", tlbl1->key + 100);
       emitcode ("djnz", "b,%05d$", tlbl->key + 100);
+      popB (pushedB);
       aopPut (AOP (result), "a", 0, isOperandVolatile (result, FALSE));
       goto release;
     }
@@ -7836,6 +8211,7 @@ genSignedRightShift (iCode * ic)
   reAdjustPreg (AOP (result));
   emitcode ("", "%05d$:", tlbl1->key + 100);
   emitcode ("djnz", "b,%05d$", tlbl->key + 100);
+  popB (pushedB);
 
 release:
   freeAsmop (left, NULL, ic, TRUE);
@@ -7853,6 +8229,7 @@ genRightShift (iCode * ic)
   int size, offset;
   char *l;
   symbol *tlbl, *tlbl1;
+  bool pushedB;
 
   D(emitcode (";     genRightShift",""));
 
@@ -7893,6 +8270,7 @@ genRightShift (iCode * ic)
      more that 32 bits make no sense anyway, ( the
      largest size of an object can be only 32 bits ) */
 
+  pushedB = pushB ();
   emitcode ("mov", "b,%s", aopGet (AOP (right), 0, FALSE, FALSE));
   emitcode ("inc", "b");
   freeAsmop (right, NULL, ic, TRUE);
@@ -7938,6 +8316,7 @@ genRightShift (iCode * ic)
       emitcode ("rrc", "a");
       emitcode ("", "%05d$:", tlbl1->key + 100);
       emitcode ("djnz", "b,%05d$", tlbl->key + 100);
+      popB (pushedB);
       aopPut (AOP (result), "a", 0, isOperandVolatile (result, FALSE));
       goto release;
     }
@@ -7957,6 +8336,7 @@ genRightShift (iCode * ic)
 
   emitcode ("", "%05d$:", tlbl1->key + 100);
   emitcode ("djnz", "b,%05d$", tlbl->key + 100);
+  popB (pushedB);
 
 release:
   freeAsmop (left, NULL, ic, TRUE);
@@ -8078,18 +8458,18 @@ genUnpackBits (operand * result, char *rname, int ptype, iCode *ifx)
           SNPRINTF (buffer, sizeof(buffer),
                     "acc.%d", bstr);
           genIfxJump (ifx, buffer, NULL, NULL, NULL);
-       }
+        }
       else
         {
           if (blen < 8)
             emitcode ("anl", "a,#0x%02x",
-                     (((unsigned char) -1) >> (8 - blen)) << bstr);
+                      (((unsigned char) -1) >> (8 - blen)) << bstr);
           genIfxJump (ifx, "a", NULL, NULL, NULL);
-       }
+        }
       return;
     }
   wassert (!ifx);
-  
+
   /* If the bitfield length is less than a byte */
   if (blen < 8)
     {
@@ -8168,7 +8548,7 @@ genNearPointerGet (operand * left,
                    operand * result,
                    iCode * ic,
                    iCode * pi,
-                  iCode * ifx)
+                   iCode * ifx)
 {
   asmop *aop = NULL;
   regs *preg = NULL;
@@ -8249,7 +8629,7 @@ genNearPointerGet (operand * left,
 
               emitcode ("mov", "a,@%s", rname);
               if (!ifx)
-                aopPut (AOP (result), "a", offset, isOperandVolatile (result, FALSE));
+              aopPut (AOP (result), "a", offset, isOperandVolatile (result, FALSE));
             }
           else
             {
@@ -8288,7 +8668,7 @@ genNearPointerGet (operand * left,
             emitcode ("dec", "%s", rname);
         }
     }
-  
+
   if (ifx && !ifx->generated)
     {
       genIfxJump (ifx, "a", left, NULL, result);
@@ -8308,7 +8688,7 @@ genPagedPointerGet (operand * left,
                     operand * result,
                     iCode * ic,
                     iCode *pi,
-                   iCode *ifx)
+                    iCode *ifx)
 {
   asmop *aop = NULL;
   regs *preg = NULL;
@@ -8353,7 +8733,7 @@ genPagedPointerGet (operand * left,
 
           emitcode ("movx", "a,@%s", rname);
           if (!ifx)
-            aopPut (AOP (result), "a", offset, isOperandVolatile (result, FALSE));
+          aopPut (AOP (result), "a", offset, isOperandVolatile (result, FALSE));
 
           offset++;
 
@@ -8407,7 +8787,7 @@ loadDptrFromOperand (operand *op, bool loadBToo)
 {
   if (AOP_TYPE (op) != AOP_STR)
     {
-      /* if this is remateriazable */
+      /* if this is rematerializable */
       if (AOP_TYPE (op) == AOP_IMMD)
         {
           emitcode ("mov", "dptr,%s", aopGet (AOP (op), 0, TRUE, FALSE));
@@ -8488,20 +8868,19 @@ genFarPointerGet (operand * left,
       while (size--)
         {
           emitcode ("movx", "a,@dptr");
-         if (!ifx)
+          if (!ifx)
             aopPut (AOP (result), "a", offset++, isOperandVolatile (result, FALSE));
           if (size || pi)
             emitcode ("inc", "dptr");
         }
-
     }
-  
+
   if (pi && AOP_TYPE (left) != AOP_IMMD && AOP_TYPE (left) != AOP_STR)
     {
-      aopPut ( AOP (left), "dpl", 0, isOperandVolatile (left, FALSE));
-      aopPut ( AOP (left), "dph", 1, isOperandVolatile (left, FALSE));
-      pi->generated = 1;
-    }
+    aopPut ( AOP (left), "dpl", 0, isOperandVolatile (left, FALSE));
+    aopPut ( AOP (left), "dph", 1, isOperandVolatile (left, FALSE));
+    pi->generated = 1;
+  }
 
   if (ifx && !ifx->generated)
     {
@@ -8545,7 +8924,7 @@ genCodePointerGet (operand * left,
               emitcode ("clr", "a");
               emitcode ("movc", "a,@a+dptr");
               if (!ifx)
-                aopPut (AOP (result), "a", offset++, isOperandVolatile (result, FALSE));
+              aopPut (AOP (result), "a", offset++, isOperandVolatile (result, FALSE));
               emitcode ("inc", "dptr");
             }
           else
@@ -8553,17 +8932,17 @@ genCodePointerGet (operand * left,
               emitcode ("mov", "a,#0x%02x", offset);
               emitcode ("movc", "a,@a+dptr");
               if (!ifx)
-                aopPut (AOP (result), "a", offset++, isOperandVolatile (result, FALSE));
+              aopPut (AOP (result), "a", offset++, isOperandVolatile (result, FALSE));
             }
         }
     }
 
   if (pi && AOP_TYPE (left) != AOP_IMMD && AOP_TYPE (left) != AOP_STR)
     {
-      aopPut ( AOP (left), "dpl", 0, isOperandVolatile (left, FALSE));
-      aopPut ( AOP (left), "dph", 1, isOperandVolatile (left, FALSE));
-      pi->generated = 1;
-    }
+    aopPut ( AOP (left), "dpl", 0, isOperandVolatile (left, FALSE));
+    aopPut ( AOP (left), "dph", 1, isOperandVolatile (left, FALSE));
+    pi->generated = 1;
+  }
 
   if (ifx && !ifx->generated)
     {
@@ -8604,7 +8983,7 @@ genGenPointerGet (operand * left,
         {
           emitcode ("lcall", "__gptrget");
           if (!ifx)
-            aopPut (AOP (result), "a", offset++, isOperandVolatile (result, FALSE));
+          aopPut (AOP (result), "a", offset++, isOperandVolatile (result, FALSE));
           if (size || pi)
             emitcode ("inc", "dptr");
         }
@@ -8612,17 +8991,17 @@ genGenPointerGet (operand * left,
 
   if (pi && AOP_TYPE (left) != AOP_IMMD && AOP_TYPE (left) != AOP_STR)
     {
-      aopPut ( AOP (left), "dpl", 0, isOperandVolatile (left, FALSE));
-      aopPut ( AOP (left), "dph", 1, isOperandVolatile (left, FALSE));
-      pi->generated = 1;
-    }
+    aopPut ( AOP (left), "dpl", 0, isOperandVolatile (left, FALSE));
+    aopPut ( AOP (left), "dph", 1, isOperandVolatile (left, FALSE));
+    pi->generated = 1;
+  }
 
   if (ifx && !ifx->generated)
     {
       genIfxJump (ifx, "a", left, NULL, result);
     }
 
-    
+
   freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
 }
@@ -8641,7 +9020,7 @@ genPointerGet (iCode * ic, iCode *pi, iCode *ifx)
 
   left = IC_LEFT (ic);
   result = IC_RESULT (ic);
-  
+
   if (getSize (operandType (result))>1)
     ifx = NULL;
 
@@ -8753,6 +9132,7 @@ genPackBits (sym_link * etype,
             }
           else
             {
+              bool pushedB;
               /* Case with a bitfield length < 8 and arbitrary source
               */
               MOVA (aopGet (AOP (right), 0, FALSE, FALSE));
@@ -8760,6 +9140,7 @@ genPackBits (sym_link * etype,
               AccLsh (bstr);
               emitcode ("anl", "a,#0x%02x", (~mask) & 0xff);
 
+              pushedB = pushB ();
               /* transfer A to B and get next byte */
               emitPtrByteGet (rname, p_type, TRUE);
 
@@ -8767,6 +9148,8 @@ genPackBits (sym_link * etype,
               emitcode ("orl", "a,b");
               if (p_type == GPOINTER)
                 emitcode ("pop", "b");
+
+              popB (pushedB);
            }
         }
 
@@ -8804,11 +9187,13 @@ genPackBits (sym_link * etype,
         }
       else
         {
+          bool pushedB;
           /* Case with partial byte and arbitrary source
           */
           MOVA (aopGet (AOP (right), offset++, FALSE, FALSE));
           emitcode ("anl", "a,#0x%02x", (~mask) & 0xff);
 
+          pushedB = pushB ();
           /* transfer A to B and get next byte */
           emitPtrByteGet (rname, p_type, TRUE);
 
@@ -8816,6 +9201,8 @@ genPackBits (sym_link * etype,
           emitcode ("orl", "a,b");
           if (p_type == GPOINTER)
             emitcode ("pop", "b");
+
+          popB (pushedB);
         }
       emitPtrByteSet (rname, p_type, "a");
     }
@@ -9260,9 +9647,8 @@ genIfx (iCode * ic, iCode * popIc)
   if (popIc)
     genIpop (popIc);
 
-  /* if the condition is  a bit variable */
-  if (isbit && IS_ITEMP (cond) &&
-      SPIL_LOC (cond))
+  /* if the condition is a bit variable */
+  if (isbit && IS_ITEMP (cond) && SPIL_LOC (cond))
     genIfxJump (ic, SPIL_LOC (cond)->rname, NULL, NULL, NULL);
   else if (isbit && !IS_ITEMP (cond))
     genIfxJump (ic, OP_SYMBOL (cond)->rname, NULL, NULL, NULL);
@@ -9294,7 +9680,7 @@ genAddrOf (iCode * ic)
          it */
       if (sym->stack)
         {
-          emitcode ("mov", "a,_bp");
+          emitcode ("mov", "a,%s", SYM_BP (sym));
           emitcode ("add", "a,#0x%02x", ((sym->stack < 0) ?
                                          ((char) (sym->stack - _G.nRegsSaved)) :
                                          ((char) sym->stack)) & 0xff);
@@ -9303,7 +9689,7 @@ genAddrOf (iCode * ic)
       else
         {
           /* we can just move _bp */
-          aopPut (AOP (IC_RESULT (ic)), "_bp", 0, isOperandVolatile (IC_RESULT (ic), FALSE));
+          aopPut (AOP (IC_RESULT (ic)), SYM_BP (sym), 0, isOperandVolatile (IC_RESULT (ic), FALSE));
         }
       /* fill the result with zero */
       size = AOP_SIZE (IC_RESULT (ic)) - 1;
@@ -9453,16 +9839,21 @@ genAssign (iCode * ic)
       !IS_FLOAT (operandType (right)) &&
       (lit < 256L))
     {
+      while ((size) && (lit))
+        {
+          aopPut (AOP (result),
+                  aopGet (AOP (right), offset, FALSE, FALSE),
+                  offset,
+                  isOperandVolatile (result, FALSE));
+          lit >>= 8;
+          offset++;
+          size--;
+        }
       emitcode ("clr", "a");
       while (size--)
         {
-          if ((unsigned int) ((lit >> (size * 8)) & 0x0FFL) == 0)
-            aopPut (AOP (result), "a", size, isOperandVolatile (result, FALSE));
-          else
-            aopPut (AOP (result),
-                    aopGet (AOP (right), size, FALSE, FALSE),
-                    size,
-                    isOperandVolatile (result, FALSE));
+          aopPut (AOP (result), "a", offset, isOperandVolatile (result, FALSE));
+          offset++;
         }
     }
   else
@@ -9483,7 +9874,7 @@ release:
 }
 
 /*-----------------------------------------------------------------*/
-/* genJumpTab - genrates code for jump table                       */
+/* genJumpTab - generates code for jump table                      */
 /*-----------------------------------------------------------------*/
 static void
 genJumpTab (iCode * ic)
@@ -9498,9 +9889,11 @@ genJumpTab (iCode * ic)
 
   if( count <= 16 )
     {
-      /* this algorithm needs 9 cycles and 7 + 3*n bytes 
-         if the switch argument is in an register. 
-         (8 cycles and 6+2*n bytes if peepholes can change ljmp to sjmp) */ 
+      /* 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 ???*/
       aopOp (IC_JTCOND (ic), ic, FALSE);
       /* get the condition into accumulator */
       l = aopGet (AOP (IC_JTCOND (ic)), 0, FALSE, FALSE);
@@ -9521,24 +9914,26 @@ genJumpTab (iCode * ic)
     }
   else
     {
-      /* this algorithm needs 14 cycles and 13 + 2*n bytes 
-         if the switch argument is in an register. 
-         For n>6 this algorithm may be more compact */ 
+      /* this algorithm needs 14 cycles and 13 + 2*n bytes
+         if the switch argument is in a register.
+         For n>6 this algorithm may be more compact */
       jtablo = newiTempLabel (NULL);
       jtabhi = newiTempLabel (NULL);
 
       /* get the condition into accumulator.
-         Using b as temporary storage, if register push/pop is needed */ 
+         Using b as temporary storage, if register push/pop is needed */
       aopOp (IC_JTCOND (ic), ic, FALSE);
       l = aopGet (AOP (IC_JTCOND (ic)), 0, FALSE, FALSE);
       if ((AOP_TYPE (IC_JTCOND (ic)) == AOP_R0 && _G.r0Pushed) ||
           (AOP_TYPE (IC_JTCOND (ic)) == AOP_R1 && _G.r1Pushed))
         {
+          // (MB) what if B is in use???
+          wassertl(!BINUSE, "B was in use");
           emitcode ("mov", "b,%s", l);
           l = "b";
         }
-      freeAsmop (IC_JTCOND (ic), NULL, ic, TRUE);            
-      MOVA (l);      
+      freeAsmop (IC_JTCOND (ic), NULL, ic, TRUE);
+      MOVA (l);
       if( count <= 112 )
         {
           emitcode ("add", "a,#(%05d$-3-.)", jtablo->key + 100);
@@ -9550,7 +9945,7 @@ genJumpTab (iCode * ic)
           emitcode ("movc", "a,@a+pc");
           emitcode ("push", "acc");
         }
-      else     
+      else
         {
           /* this scales up to n<=255, but needs two more bytes
              and changes dptr */
@@ -9577,9 +9972,7 @@ genJumpTab (iCode * ic)
       for (jtab = setFirstItem (IC_JTLABELS (ic)); jtab;
            jtab = setNextItem (IC_JTLABELS (ic)))
          emitcode (".db", "%05d$>>8", jtab->key + 100);
-    
-    }  
-
+    }
 }
 
 /*-----------------------------------------------------------------*/
@@ -10007,20 +10400,22 @@ genCritical (iCode *ic)
   D(emitcode(";     genCritical",""));
 
   if (IC_RESULT (ic))
-    aopOp (IC_RESULT (ic), ic, TRUE);
-
-  emitcode ("setb", "c");
-  emitcode ("jbc", "ea,%05d$", (tlbl->key + 100)); /* atomic test & clear */
-  emitcode ("clr", "c");
-  emitcode ("", "%05d$:", (tlbl->key + 100));
-
-  if (IC_RESULT (ic))
-    outBitC (IC_RESULT (ic)); /* save old ea in an operand */
+    {
+      aopOp (IC_RESULT (ic), ic, TRUE);
+      aopPut (AOP (IC_RESULT (ic)), one, 0, 0);
+      emitcode ("jbc", "ea,%05d$", (tlbl->key + 100)); /* atomic test & clear */
+      aopPut (AOP (IC_RESULT (ic)), zero, 0, 0);
+      emitcode ("", "%05d$:", (tlbl->key + 100));
+      freeAsmop (IC_RESULT (ic), NULL, ic, TRUE);
+    }
   else
-    emitcode ("push", "psw"); /* save old ea via c in psw on top of stack*/
-
-  if (IC_RESULT (ic))
-    freeAsmop (IC_RESULT (ic), NULL, ic, TRUE);
+    {
+      emitcode ("setb", "c");
+      emitcode ("jbc", "ea,%05d$", (tlbl->key + 100)); /* atomic test & clear */
+      emitcode ("clr", "c");
+      emitcode ("", "%05d$:", (tlbl->key + 100));
+      emitcode ("push", "psw"); /* save old ea via c in psw on top of stack*/
+    }
 }
 
 /*-----------------------------------------------------------------*/
@@ -10041,7 +10436,8 @@ genEndCritical (iCode *ic)
         }
       else
         {
-          MOVA (aopGet (AOP (IC_RIGHT (ic)), 0, FALSE, FALSE));
+          if (AOP_TYPE (IC_RIGHT (ic)) != AOP_DUMMY)
+            MOVA (aopGet (AOP (IC_RIGHT (ic)), 0, FALSE, FALSE));
           emitcode ("rrc", "a");
           emitcode ("mov", "ea,c");
         }
@@ -10090,7 +10486,7 @@ gen51Code (iCode * lic)
         {
           if (options.debug)
             {
-              debugFile->writeCLine(ic);
+              debugFile->writeCLine (ic);
             }
           if (!options.noCcodeInAsm) {
             emitcode ("", ";%s:%d: %s", ic->filename, ic->lineno,
@@ -10275,7 +10671,7 @@ gen51Code (iCode * lic)
         case GET_VALUE_AT_ADDRESS:
           genPointerGet (ic,
                          hasInc (IC_LEFT (ic), ic,
-                                getSize (operandType (IC_RESULT (ic)))),
+                                 getSize (operandType (IC_RESULT (ic)))),
                          ifxForOp (IC_RESULT (ic), ic) );
           break;