* src/mcs51/gen.c (genEndFunction): removed unused variable to fix
[fw/sdcc] / src / mcs51 / gen.c
index 5cea9722c0c00d082e9a0eaec04ce094afcbee9e..2a20db2bd9a4010b44860e8817f2379101b85e77 100644 (file)
@@ -64,6 +64,13 @@ 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;
@@ -75,9 +82,9 @@ static 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;
@@ -190,7 +197,7 @@ pushB (void)
 {
   bool pushedB = FALSE;
 
-  if (_G.BInUse)
+  if (BINUSE)
     {
       emitcode ("push", "b");
 //    printf("B was in use !\n");
@@ -198,7 +205,7 @@ pushB (void)
     }
   else
     {
-      _G.OpInB++;
+      OPINB++;
     }
   return pushedB;
 }
@@ -215,7 +222,7 @@ popB (bool pushedB)
     }
   else
     {
-      _G.OpInB--;
+      OPINB--;
     }
 }
 
@@ -269,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)
         {
@@ -293,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)
         {
@@ -509,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");
             }
@@ -781,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
@@ -886,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)
         {
@@ -903,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)
         {
@@ -1001,7 +1025,7 @@ freeForBranchAsmop (operand * op)
   switch (aop->type)
     {
     case AOP_R0:
-      if (_G.r0InB)
+      if (R0INB)
         {
           emitcode ("mov", "r0,b");
         }
@@ -1012,7 +1036,7 @@ freeForBranchAsmop (operand * op)
       break;
 
     case AOP_R1:
-      if (_G.r1InB)
+      if (R1INB)
         {
           emitcode ("mov", "r1,b");
         }
@@ -1240,12 +1264,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))
     {
@@ -1260,6 +1285,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:
@@ -1272,6 +1298,8 @@ aopPut (asmop * aop, const char *s, int offset, bool bvolatile)
       if (strcmp (d, s) ||
           bvolatile)
           emitcode ("mov", "%s,%s", d, s);
+      if (!strcmp (d, "acc"))
+          accuse = TRUE;
 
       break;
 
@@ -1318,7 +1346,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");
@@ -1382,11 +1410,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
         {
@@ -1398,15 +1428,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;
@@ -1419,6 +1444,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)
@@ -1435,6 +1461,7 @@ aopPut (asmop * aop, const char *s, int offset, bool bvolatile)
       exit (1);
     }
 
+    return accuse;
 }
 
 
@@ -1590,28 +1617,32 @@ toBoolean (operand * oper)
 {
   int size = AOP_SIZE (oper) - 1;
   int offset = 1;
-  char *l = aopGet (AOP (oper), 0, FALSE, FALSE);
+  bool AccUsed = FALSE;
   bool pushedB;
 
-  if (!strncmp (l, "a", 2) || !strncmp (l, "acc", 4))
+  while (!AccUsed && size--)
     {
-      if (size--)
+      AccUsed |= aopGetUsesAcc(AOP (oper), offset++);
+    }
+
+  size = AOP_SIZE (oper) - 1;
+  offset = 1;
+  MOVA (aopGet (AOP (oper), 0, FALSE, FALSE));
+  if (size && AccUsed && (AOP (oper)->type != AOP_ACC))
+    {
+      pushedB = pushB ();
+      emitcode("mov", "b,a");
+      while (--size)
         {
-          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);
+          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
     {
-      MOVA (l);
       while (size--)
         {
           emitcode ("orl", "a,%s", aopGet (AOP (oper), offset++, FALSE, FALSE));
@@ -1666,8 +1697,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);
@@ -1676,27 +1708,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);
+      l = aopGet (AOP (IC_LEFT (ic)), offset++, FALSE, FALSE);
       if (AOP_TYPE (IC_LEFT (ic)) == AOP_ACC ||
           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));
@@ -1867,26 +1904,52 @@ saveRegisters (iCode * lic)
   ic->regsSaved = 1;
   if (options.useXstack)
     {
-      if (bitVectBitValue (rsave, R0_IDX))
+      int count = bitVectnBitsOn (rsave);
+
+      if (count == 1)
         {
-          emitcode ("mov", "a,r0");
-          emitcode ("push", "%s", mcs51_regWithIdx (R0_IDX)->dname);
+          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");
         }
-      emitcode ("mov", "r0,%s", spname);
-      for (i = 0; i < mcs51_nRegs; i++)
+      else if (count != 0)
         {
-          if (bitVectBitValue (rsave, i))
+          if (bitVectBitValue (rsave, R0_IDX))
             {
-              if (i != R0_IDX)
-                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 ("pop", "%s", mcs51_regWithIdx (R0_IDX)->dname);
         }
     }
   else
@@ -1913,22 +1976,37 @@ unsaveRegisters (iCode * ic)
 
   if (options.useXstack)
     {
-      emitcode ("mov", "r0,%s", spname);
-      for (i = mcs51_nRegs; i >= 0; i--)
-        {
-          if (bitVectBitValue (rsave, i))
-            {
-              emitcode ("dec", "r0");
-              emitcode ("movx", "a,@r0");
-              if (i != R0_IDX)
-                emitcode ("mov", "%s,a", mcs51_regWithIdx (i)->name);
-            }
+      int count = bitVectnBitsOn (rsave);
 
+      if (count == 1)
+        {
+          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);
         }
-      emitcode ("mov", "%s,r0", spname);
-      if (bitVectBitValue (rsave, R0_IDX))
+      else if (count != 0)
         {
-          emitcode ("mov", "r0,a");
+          emitcode ("mov", "r0,%s", spname);
+          for (i = mcs51_nRegs; i >= 0; i--)
+            {
+              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");
+            }
         }
     }
   else
@@ -1937,7 +2015,6 @@ unsaveRegisters (iCode * ic)
         if (bitVectBitValue (rsave, i))
           emitcode ("pop", "%s", mcs51_regWithIdx (i)->dname);
       }
-
 }
 
 
@@ -1964,18 +2041,21 @@ pushSide (operand * oper, int size)
     }
 
 /*-----------------------------------------------------------------*/
-/* 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;
 }
 
 
@@ -1994,30 +2074,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)
@@ -2068,7 +2155,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));
 
@@ -2084,7 +2170,7 @@ genIpush (iCode * ic)
         }
       else
           emitcode ("push", "%s", l);
-        }
+    }
 
   freeAsmop (IC_LEFT (ic), NULL, ic, TRUE);
 }
@@ -2114,17 +2200,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. */
@@ -2135,54 +2222,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;
@@ -2191,59 +2284,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 ("pop", "(%s+%d)",
+                  regs8051[i].base, 8 * bank + regs8051[i].offset);
+        }
+    }
 
-      emitcode ("mov", "psw,#0x%02x", (bank << 3) & 0x00ff);
+  if (options.useXstack)
+    {
+      emitcode ("mov", "%s,%s", spname, r->name);
     }
 
   if (aop)
     {
       freeAsmop (NULL, aop, ic, TRUE);
     }
-
-  if (ic)
-  {
-    ic->bankSaved = 1;
-  }
 }
 
 /*-----------------------------------------------------------------*/
@@ -2288,6 +2379,8 @@ genCall (iCode * ic)
   sym_link *dtype;
 //  bool restoreBank = FALSE;
   bool swapBanks = FALSE;
+  bool accuse = FALSE;
+  bool accPushed = FALSE;
 
   D(emitcode(";     genCall",""));
 
@@ -2348,7 +2441,7 @@ 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);
     }
@@ -2360,6 +2453,11 @@ genCall (iCode * ic)
       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);
@@ -2371,11 +2469,21 @@ 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)
+        {
+          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");
 }
 
 /*-----------------------------------------------------------------*/
@@ -2541,13 +2649,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 */
@@ -2768,26 +2877,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 */
@@ -2853,18 +2962,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)
         {
@@ -2906,14 +3012,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 */
@@ -2934,12 +3053,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))
@@ -2956,30 +3075,21 @@ genEndFunction (iCode * ic)
       emitcode ("mov", "ea,c");
     }
 
-  if (IFFUNC_ISREENT (sym->type) || options.stackAuto)
+  if ((IFFUNC_ISREENT (sym->type) || options.stackAuto))// && !options.useXstack)
     {
-      emitcode ("mov", "%s,_bp", spname);
+      emitcode ("mov", "sp,_bp");
     }
-
-  /* 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))
     {
       if (options.useXstack)
         {
-          emitcode ("mov", "r0,%s", spname);
+          emitcode ("pop", "_bp");
+          emitcode ("xch", "a,_bpx");
+          emitcode ("mov", "r0,a");
+          emitcode ("dec", "r0");
           emitcode ("movx", "a,@r0");
-          emitcode ("mov", "_bp,a");
-          emitcode ("dec", "%s", spname);
+          emitcode ("xch", "a,_bpx");
+          emitcode ("mov", "%s,r0", spname); //read before freeing stack space (interrupts)
         }
       else
         {
@@ -4626,6 +4736,61 @@ 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 */
@@ -4650,7 +4815,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);
@@ -4761,7 +4926,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);
@@ -4992,7 +5157,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);
 }
@@ -5096,7 +5261,7 @@ gencjneshort (operand * left, operand * right, symbol * lbl)
         {
           char *l;
           //if B in use: push B; mov B,left; mov A,right; clrc; subb A,B; pop B; jnz
-          wassertl(!_G.BInUse, "B was in use");
+          wassertl(!BINUSE, "B was in use");
           l = aopGet (AOP (left), offset, FALSE, FALSE);
           if (strcmp (l, "b"))
             emitcode ("mov", "b,%s", l);
@@ -5646,7 +5811,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
             {
@@ -5729,8 +5904,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
@@ -6032,8 +6207,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
@@ -6310,8 +6485,8 @@ genXor (iCode * ic, iCode * ifx)
                 }
               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
@@ -6493,7 +6668,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);
@@ -6545,7 +6720,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--)
@@ -7337,7 +7512,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);
@@ -9414,8 +9589,7 @@ genIfx (iCode * ic, iCode * popIc)
     genIpop (popIc);
 
   /* if the condition is a bit variable */
-  if (isbit && IS_ITEMP (cond) &&
-      SPIL_LOC (cond))
+  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);
@@ -9447,7 +9621,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);
@@ -9456,7 +9630,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;
@@ -9606,16 +9780,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
@@ -9636,7 +9815,7 @@ release:
 }
 
 /*-----------------------------------------------------------------*/
-/* genJumpTab - genrates code for jump table                       */
+/* genJumpTab - generates code for jump table                      */
 /*-----------------------------------------------------------------*/
 static void
 genJumpTab (iCode * ic)
@@ -9690,7 +9869,7 @@ genJumpTab (iCode * ic)
           (AOP_TYPE (IC_JTCOND (ic)) == AOP_R1 && _G.r1Pushed))
         {
           // (MB) what if B is in use???
-          wassertl(!_G.BInUse, "B was in use");
+          wassertl(!BINUSE, "B was in use");
           emitcode ("mov", "b,%s", l);
           l = "b";
         }
@@ -10162,20 +10341,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*/
+    }
 }
 
 /*-----------------------------------------------------------------*/
@@ -10196,7 +10377,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");
         }