* doc/sdccman.lyx: inserted footnotes about inline assembler labels,
[fw/sdcc] / src / mcs51 / gen.c
index 6ababda9db752bc272f82730220deb370ac6ff52..39a01cbe694c2e0d65af6fe2f0e3d582abbb0e1a 100644 (file)
@@ -1,5 +1,5 @@
 /*-------------------------------------------------------------------------
-  SDCCgen51.c - source file for code generation for 8051
+  gen.c - source file for code generation for 8051
 
   Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1998)
          and -  Jean-Louis VERN.jlvern@writeme.com (1999)
@@ -28,8 +28,7 @@
       Made everything static
 -------------------------------------------------------------------------*/
 
-//#define D(x)
-#define D(x) x
+#define D(x) do if (options.verboseAsm) {x;} while(0)
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -41,7 +40,9 @@
 #include "common.h"
 #include "SDCCpeeph.h"
 #include "ralloc.h"
+#include "rtrack.h"
 #include "gen.h"
+#include "dbuf_string.h"
 
 char *aopLiteral (value * val, int offset);
 char *aopLiteralLong (value * val, int offset, int size);
@@ -65,19 +66,23 @@ static char *accUse[] =
 
 static unsigned short rbank = -1;
 
+#define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
+
+#define REG_WITH_INDEX   mcs51_regWithIdx
+
 #define AOP(op) op->aop
 #define AOP_TYPE(op) AOP(op)->type
 #define AOP_SIZE(op) AOP(op)->size
 #define IS_AOP_PREG(x) (AOP(x) && (AOP_TYPE(x) == AOP_R1 || \
-                       AOP_TYPE(x) == AOP_R0))
+                        AOP_TYPE(x) == AOP_R0))
 
 #define AOP_NEEDSACC(x) (AOP(x) && (AOP_TYPE(x) == AOP_CRY ||  \
-                        AOP_TYPE(x) == AOP_DPTR || \
-                        AOP(x)->paged))
+                         AOP_TYPE(x) == AOP_DPTR || \
+                         AOP(x)->paged))
 
 #define AOP_INPREG(x) (x && (x->type == AOP_REG &&                        \
-                      (x->aopu.aop_reg[0] == mcs51_regWithIdx(R0_IDX) || \
-                      x->aopu.aop_reg[0] == mcs51_regWithIdx(R1_IDX) )))
+                       (x->aopu.aop_reg[0] == REG_WITH_INDEX(R0_IDX) || \
+                        x->aopu.aop_reg[0] == REG_WITH_INDEX(R1_IDX) )))
 
 #define SYM_BP(sym)   (SPEC_OCLS (sym->etype)->paged ? "_bpx" : "_bp")
 
@@ -115,14 +120,15 @@ static char *rb1regs[] = {
     "b0",  "b1",  "b2",  "b3",  "b4",  "b5",  "b6",  "b7"
 };
 
-extern FILE *codeOutFile;
-static void saveRBank (int, iCode *, bool);
+extern struct dbuf_s *codeOutBuf;
 
 #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 MOVB(x)  movb(x)
+
 #define CLRC     emitcode("clr","c")
 #define SETC     emitcode("setb","c")
 
@@ -144,50 +150,63 @@ static unsigned char SRMask[] =
 /*-----------------------------------------------------------------*/
 /* emitcode - writes the code into a file : for now it is simple    */
 /*-----------------------------------------------------------------*/
-static void
-emitcode (char *inst, const char *fmt,...)
+void
+emitcode (const char *inst, const char *fmt,...)
 {
   va_list ap;
-  char lb[INITIAL_INLINEASM];
-  char *lbp = lb;
+  struct dbuf_s dbuf;
+  const char *lbp, *lb;
+
+  dbuf_init (&dbuf, INITIAL_INLINEASM);
 
   va_start (ap, fmt);
 
   if (inst && *inst)
     {
+      dbuf_append_str (&dbuf, inst);
+
       if (fmt && *fmt)
         {
-          SNPRINTF (lb, sizeof(lb), "%s\t", inst);
-        }
-      else
-        {
-          SNPRINTF (lb, sizeof(lb), "%s", inst);
+          dbuf_append_char (&dbuf, '\t');
+          dbuf_tvprintf (&dbuf, fmt, ap);
         }
-
-      tvsprintf (lb + strlen(lb), sizeof(lb) - strlen(lb), fmt, ap);
     }
   else
     {
-      tvsprintf (lb, sizeof(lb), fmt, ap);
+      dbuf_tvprintf (&dbuf, fmt, ap);
     }
 
+  lbp = lb = dbuf_c_str(&dbuf);
+
   while (isspace ((unsigned char)*lbp))
     {
       lbp++;
     }
 
-  if (lbp && *lbp)
+  if (lbp)
     {
+      rtrackUpdate (lbp);
+
       lineCurr = (lineCurr ?
                   connectLine (lineCurr, newLineNode (lb)) :
                   (lineHead = newLineNode (lb)));
+
+      lineCurr->isInline = _G.inLine;
+      lineCurr->isDebug = _G.debugLine;
+      lineCurr->ic = _G.current_iCode;
+      lineCurr->isComment = (*lbp==';');
     }
 
-  lineCurr->isInline = _G.inLine;
-  lineCurr->isDebug = _G.debugLine;
-  lineCurr->ic = _G.current_iCode;
-  lineCurr->isComment = (*lbp==';');
   va_end (ap);
+
+  dbuf_destroy(&dbuf);
+}
+
+static void
+emitLabel (symbol *tlbl)
+{
+  emitcode ("", "%05d$:", tlbl->key + 100);
+  lineCurr->isLabel = 1;
 }
 
 /*-----------------------------------------------------------------*/
@@ -212,25 +231,32 @@ mova (const char *x)
   if (!strncmp(x, "a", 2) || !strncmp(x, "acc", 4))
     return;
 
+  /* if it is a literal mov try to get it cheaper */
+  if (*x == '#' &&
+      rtrackMoveALit(x))
+    return;
+
   emitcode("mov", "a,%s", x);
 }
 
 /*-----------------------------------------------------------------*/
-/* movc - moves specified value into the carry                     */
+/* movb - moves specified value into register b                    */
 /*-----------------------------------------------------------------*/
 static void
-movc (const char *s)
+movb (const char *x)
 {
-  if (s == zero)
-    CLRC;
-  else if (s == one)
-    SETC;
-  else if (strcmp (s, "c"))
-    {/* it's not in carry already */
-      MOVA (s);
-      /* set C, if a >= 1 */
-      emitcode ("add", "a,#0xff");
+  /* do some early peephole optimization */
+  if (!strncmp(x, "b", 2))
+    return;
+
+  /* if it is a literal mov try to get it cheaper */
+  if (*x == '#')
+    {
+      emitcode("mov","b,%s", rtrackGetLit(x));
+      return;
     }
+
+  emitcode("mov","b,%s", x);
 }
 
 /*-----------------------------------------------------------------*/
@@ -276,7 +302,7 @@ popB (bool pushedB)
 static bool
 pushReg (int index, bool bits_pushed)
 {
-  regs * reg = mcs51_regWithIdx (index);
+  regs * reg = REG_WITH_INDEX (index);
   if (reg->type == REG_BIT)
     {
       if (!bits_pushed)
@@ -294,7 +320,7 @@ pushReg (int index, bool bits_pushed)
 static bool
 popReg (int index, bool bits_popped)
 {
-  regs * reg = mcs51_regWithIdx (index);
+  regs * reg = REG_WITH_INDEX (index);
   if (reg->type == REG_BIT)
     {
       if (!bits_popped)
@@ -335,7 +361,7 @@ getFreePtr (iCode * ic, asmop ** aopp, bool result)
       ic->rUsed = bitVectSetBit (ic->rUsed, R0_IDX);
       (*aopp)->type = AOP_R0;
 
-      return (*aopp)->aopu.aop_ptr = mcs51_regWithIdx (R0_IDX);
+      return (*aopp)->aopu.aop_ptr = REG_WITH_INDEX (R0_IDX);
     }
 
   /* if no usage of r1 then return it */
@@ -344,7 +370,7 @@ getFreePtr (iCode * ic, asmop ** aopp, bool result)
       ic->rUsed = bitVectSetBit (ic->rUsed, R1_IDX);
       (*aopp)->type = AOP_R1;
 
-      return (*aopp)->aopu.aop_ptr = mcs51_regWithIdx (R1_IDX);
+      return (*aopp)->aopu.aop_ptr = REG_WITH_INDEX (R1_IDX);
     }
 
   /* now we know they both have usage */
@@ -354,21 +380,20 @@ getFreePtr (iCode * ic, asmop ** aopp, bool result)
       /* push it if not already pushed */
       if (ic->op == IPUSH)
         {
-          emitcode ("mov", "b,%s",
-                    mcs51_regWithIdx (R0_IDX)->dname);
+          MOVB (REG_WITH_INDEX (R0_IDX)->dname);
           R0INB++;
         }
       else if (!_G.r0Pushed)
         {
           emitcode ("push", "%s",
-                    mcs51_regWithIdx (R0_IDX)->dname);
+                    REG_WITH_INDEX (R0_IDX)->dname);
           _G.r0Pushed++;
         }
 
       ic->rUsed = bitVectSetBit (ic->rUsed, R0_IDX);
       (*aopp)->type = AOP_R0;
 
-      return (*aopp)->aopu.aop_ptr = mcs51_regWithIdx (R0_IDX);
+      return (*aopp)->aopu.aop_ptr = REG_WITH_INDEX (R0_IDX);
     }
 
   /* if r1 not used then */
@@ -378,39 +403,41 @@ getFreePtr (iCode * ic, asmop ** aopp, bool result)
       /* push it if not already pushed */
       if (ic->op == IPUSH)
         {
-          emitcode ("mov", "b,%s",
-                    mcs51_regWithIdx (R1_IDX)->dname);
+          MOVB (REG_WITH_INDEX (R1_IDX)->dname);
           R1INB++;
         }
       else if (!_G.r1Pushed)
         {
           emitcode ("push", "%s",
-                    mcs51_regWithIdx (R1_IDX)->dname);
+                    REG_WITH_INDEX (R1_IDX)->dname);
           _G.r1Pushed++;
         }
 
       ic->rUsed = bitVectSetBit (ic->rUsed, R1_IDX);
       (*aopp)->type = AOP_R1;
-      return mcs51_regWithIdx (R1_IDX);
+      return REG_WITH_INDEX (R1_IDX);
     }
+
 endOfWorld:
   /* I said end of world, but not quite end of world yet */
-  if (result) {
-    /* we can push it on the stack */
-    (*aopp)->type = AOP_STK;
-    return NULL;
-  } else {
-    /* in the case that result AND left AND right needs a pointer reg
-       we can safely use the result's */
-    if (bitVectBitValue (mcs51_rUmaskForOp(IC_RESULT(ic)), R0_IDX)) {
+  /* if this is a result then we can push it on the stack */
+  if (result)
+    {
+      (*aopp)->type = AOP_STK;
+      return NULL;
+    }
+  /* in the case that result AND left AND right needs a pointer reg
+     we can safely use the result's */
+  if (bitVectBitValue (mcs51_rUmaskForOp(IC_RESULT(ic)), R0_IDX))
+    {
       (*aopp)->type = AOP_R0;
-      return mcs51_regWithIdx (R0_IDX);
+      return REG_WITH_INDEX (R0_IDX);
     }
-    if (bitVectBitValue (mcs51_rUmaskForOp(IC_RESULT(ic)), R1_IDX)) {
+  if (bitVectBitValue (mcs51_rUmaskForOp(IC_RESULT(ic)), R1_IDX))
+    {
       (*aopp)->type = AOP_R1;
-      return mcs51_regWithIdx (R1_IDX);
+      return REG_WITH_INDEX (R1_IDX);
     }
-  }
 
   /* now this is REALLY the end of the world */
   werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
@@ -459,7 +486,7 @@ getTempRegs(regs **tempRegs, int size, iCode *ic)
   for (i=0; i<freeRegs->size; i++)
     {
       if (bitVectBitValue(freeRegs,i))
-        tempRegs[offset++] = mcs51_regWithIdx(i);
+        tempRegs[offset++] = REG_WITH_INDEX(i);
       if (offset>=size)
         {
           freeBitVect(freeRegs);
@@ -600,9 +627,9 @@ aopForSym (iCode * ic, symbol * sym, bool result)
         {
           if (sym->onStack)
             {
-              char offset = ((sym->stack < 0) ?
-                         ((char) (sym->stack - _G.nRegsSaved)) :
-                         ((char) sym->stack)) & 0xff;
+              signed char offset = ((sym->stack < 0) ?
+                         ((signed char) (sym->stack - _G.nRegsSaved)) :
+                         ((signed char) sym->stack)) & 0xff;
 
               if ((abs(offset) <= 3) ||
                   (accuse && (abs(offset) <= 7)))
@@ -625,7 +652,7 @@ aopForSym (iCode * ic, symbol * sym, bool result)
                   if (accuse)
                     emitcode ("push", "acc");
                   emitcode ("mov", "a,%s", SYM_BP (sym));
-                  emitcode ("add", "a,#0x%02x", offset);
+                  emitcode ("add", "a,#0x%02x", offset & 0xff);
                   emitcode ("mov", "%s,a", aop->aopu.aop_ptr->name);
                   if (accuse)
                     emitcode ("pop", "acc");
@@ -731,7 +758,7 @@ aopForRemat (symbol * sym)
   /* set immd2 field if required */
   if (aop->aopu.aop_immd.from_cast_remat)
     {
-      sprintf(buffer,"#0x%02x",ptr_type);
+      SNPRINTF (buffer, sizeof(buffer), "#0x%02x", ptr_type);
       aop->aopu.aop_immd.aop_immd2 = Safe_strdup(buffer);
     }
 
@@ -825,11 +852,14 @@ operandsEqu (operand * op1, operand * op2)
 }
 
 /*-----------------------------------------------------------------*/
-/* sameReg - two asmops have the same register at given offsets    */
+/* sameByte - two asmops have the same address at given offsets    */
 /*-----------------------------------------------------------------*/
 static bool
-sameReg (asmop * aop1, int off1, asmop * aop2, int off2)
+sameByte (asmop * aop1, int off1, asmop * aop2, int off2)
 {
+  if (aop1 == aop2 && off1 == off2)
+    return TRUE;
+
   if (aop1->type != AOP_REG && aop1->type != AOP_CRY)
     return FALSE;
 
@@ -926,8 +956,8 @@ aopOp (operand * op, iCode * ic, bool result)
   /* if the type is a conditional */
   if (sym->regType == REG_CND)
     {
-      aop = op->aop = sym->aop = newAsmop (AOP_CRY);
-      aop->size = 0;
+      sym->aop = op->aop = aop = newAsmop (AOP_CRY);
+      aop->size = sym->ruonly ? 1 : 0;
       return;
     }
 
@@ -940,8 +970,7 @@ aopOp (operand * op, iCode * ic, bool result)
       /* rematerialize it NOW */
       if (sym->remat)
         {
-          sym->aop = op->aop = aop =
-            aopForRemat (sym);
+          sym->aop = op->aop = aop = aopForRemat (sym);
           aop->size = getSize (sym->type);
           return;
         }
@@ -949,7 +978,7 @@ aopOp (operand * op, iCode * ic, bool result)
       if (sym->accuse)
         {
           int i;
-          aop = op->aop = sym->aop = newAsmop (AOP_ACC);
+          sym->aop = op->aop = aop = newAsmop (AOP_ACC);
           aop->size = getSize (sym->type);
           for (i = 0; i < 2; i++)
             aop->aopu.aop_str[i] = accUse[i];
@@ -960,7 +989,7 @@ aopOp (operand * op, iCode * ic, bool result)
         {
           unsigned i;
 
-          aop = op->aop = sym->aop = newAsmop (AOP_STR);
+          sym->aop = op->aop = aop = newAsmop (AOP_STR);
           aop->size = getSize (sym->type);
           for (i = 0; i < fReturnSizeMCS51; i++)
             aop->aopu.aop_str[i] = fReturn[i];
@@ -1059,7 +1088,7 @@ freeAsmop (operand * op, asmop * aaop, iCode * ic, bool pop)
           emitcode ("mov", "r1,b");
           R1INB--;
         }
-      if (_G.r1Pushed)
+      else if (_G.r1Pushed)
         {
           if (pop)
             {
@@ -1105,7 +1134,6 @@ freeAsmop (operand * op, asmop * aaop, iCode * ic, bool pop)
             emitcode ("pop", "ar1");
             _G.r1Pushed--;
           }
-
         if (_G.r0Pushed)
           {
             emitcode ("pop", "ar0");
@@ -1299,7 +1327,6 @@ aopGet (operand * oper, int offset, bool bit16, bool dname)
       SNPRINTF (buffer, sizeof(buffer), "@%s", aop->aopu.aop_ptr->name);
       return Safe_strdup(buffer);
 
-
     case AOP_DPTR:
       if (aop->code && aop->coff==0 && offset>=1) {
         emitcode ("mov", "a,#0x%02x", offset);
@@ -1352,7 +1379,8 @@ aopGet (operand * oper, int offset, bool bit16, bool dname)
       else
         {
           SNPRINTF (buffer, sizeof(buffer),
-                    "#%s", aop->aopu.aop_immd.aop_immd1);
+                    "#%s",
+                    aop->aopu.aop_immd.aop_immd1);
         }
       return Safe_strdup(buffer);
 
@@ -1367,13 +1395,14 @@ aopGet (operand * oper, int offset, bool bit16, bool dname)
         {
           SNPRINTF (buffer, sizeof(buffer),
                     "(%s + %d)",
-                   aop->aopu.aop_dir,
-                   offset);
+                    aop->aopu.aop_dir,
+                    offset);
         }
       else
         {
           SNPRINTF (buffer, sizeof(buffer),
-                   "%s", aop->aopu.aop_dir);
+                    "%s",
+                    aop->aopu.aop_dir);
         }
 
       return Safe_strdup(buffer);
@@ -1385,8 +1414,8 @@ aopGet (operand * oper, int offset, bool bit16, bool dname)
         return aop->aopu.aop_reg[offset]->name;
 
     case AOP_CRY:
-      emitcode ("clr", "a");
       emitcode ("mov", "c,%s", aop->aopu.aop_dir);
+      emitcode ("clr", "a");
       emitcode ("rlc", "a");
       return (dname ? "acc" : "a");
 
@@ -1465,6 +1494,7 @@ aopPut (operand * result, const char *s, int offset)
   bool bvolatile = isOperandVolatile (result, FALSE);
   bool accuse = FALSE;
   asmop * aop = AOP (result);
+  const char *d = NULL;
 
   if (aop->size && offset > (aop->size - 1))
     {
@@ -1491,19 +1521,20 @@ aopPut (operand * result, const char *s, int offset)
         }
       else if (offset)
         {
-            SNPRINTF (buffer, sizeof(buffer),
-                      "(%s + %d)",
-                      aop->aopu.aop_dir, offset);
+          SNPRINTF (buffer, sizeof(buffer),
+                    "(%s + %d)",
+                    aop->aopu.aop_dir, offset);
         }
       else
         {
-            SNPRINTF (buffer, sizeof(buffer),
-                     "%s", aop->aopu.aop_dir);
+          SNPRINTF (buffer, sizeof(buffer),
+                    "%s",
+                    aop->aopu.aop_dir);
         }
 
       if (strcmp (buffer, s) || bvolatile)
         {
-            emitcode ("mov", "%s,%s", buffer, s);
+          emitcode ("mov", "%s,%s", buffer, s);
         }
       if (!strcmp (buffer, "acc"))
         {
@@ -1610,42 +1641,69 @@ aopPut (operand * result, const char *s, int offset)
 
     case AOP_STK:
       if (strcmp (s, "a") == 0)
-        emitcode ("push", "acc");
-      else
-        if (*s=='@') {
+        {
+          emitcode ("push", "acc");
+        }
+      else if (*s=='@')
+        {
           MOVA(s);
           emitcode ("push", "acc");
-        } else {
+        }
+      else if (strcmp (s, "r0") == 0 ||
+               strcmp (s, "r1") == 0 ||
+               strcmp (s, "r2") == 0 ||
+               strcmp (s, "r3") == 0 ||
+               strcmp (s, "r4") == 0 ||
+               strcmp (s, "r5") == 0 ||
+               strcmp (s, "r6") == 0 ||
+               strcmp (s, "r7") == 0)
+        {
+          char buffer[10];
+          SNPRINTF (buffer, sizeof(buffer), "a%s", s);
+          emitcode ("push", buffer);
+        }
+      else
+        {
           emitcode ("push", s);
         }
 
       break;
 
     case AOP_CRY:
-      /* if not bit variable */
-      if (!aop->aopu.aop_dir)
+      // destination is carry for return-use-only
+      d = (IS_OP_RUONLY (result)) ? "c" : aop->aopu.aop_dir;
+      // source is no literal and not in carry
+      if ((s != zero) && (s != one) && strcmp (s, "c"))
         {
-          /* inefficient: move carry into A and use jz/jnz */
-          emitcode ("clr", "a");
-          emitcode ("rlc", "a");
-          accuse = TRUE;
+          MOVA (s);
+          /* set C, if a >= 1 */
+          emitcode ("add", "a,#0xff");
+          s = "c";
         }
-      else
+      // now source is zero, one or carry
+
+      /* if result no bit variable */
+      if (!d)
         {
-          if (s == zero)
-            emitcode ("clr", "%s", aop->aopu.aop_dir);
-          else if (s == one)
-            emitcode ("setb", "%s", aop->aopu.aop_dir);
-          else if (!strcmp (s, "c"))
-            emitcode ("mov", "%s,c", aop->aopu.aop_dir);
-          else if (strcmp (s, aop->aopu.aop_dir))
+          if (!strcmp (s, "c"))
+            {
+              /* inefficient: move carry into A and use jz/jnz */
+              emitcode ("clr", "a");
+              emitcode ("rlc", "a");
+              accuse = TRUE;
+            }
+          else
             {
               MOVA (s);
-              /* set C, if a >= 1 */
-              emitcode ("add", "a,#0xff");
-              emitcode ("mov", "%s,c", aop->aopu.aop_dir);
+              accuse = TRUE;
             }
         }
+      else if (s == zero)
+          emitcode ("clr", "%s", d);
+      else if (s == one)
+          emitcode ("setb", "%s", d);
+      else if (strcmp (s, d))
+          emitcode ("mov", "%s,c", d);
       break;
 
     case AOP_STR:
@@ -1796,7 +1854,8 @@ outBitC (operand * result)
   /* if the result is bit */
   if (AOP_TYPE (result) == AOP_CRY)
     {
-      aopPut (result, "c", 0);
+      if (!OP_SYMBOL (result)->ruonly)
+        aopPut (result, "c", 0);
     }
   else
     {
@@ -1842,11 +1901,88 @@ toBoolean (operand * oper)
     {
       while (size--)
         {
-          emitcode ("orl", "a,%s", aopGet (oper, offset++, FALSE, FALSE));
+          emitcode ("orl", "a,%s",
+                    aopGet (oper, offset++, FALSE, FALSE));
         }
     }
 }
 
+/*-----------------------------------------------------------------*/
+/* toCarry - make boolean and move into carry                      */
+/*-----------------------------------------------------------------*/
+static void
+toCarry (operand * oper)
+{
+  /* if the operand is a literal then
+     we know what the value is */
+  if (AOP_TYPE (oper) == AOP_LIT)
+    {
+      if ((int) operandLitValue (oper))
+        SETC;
+      else
+        CLRC;
+    }
+  else if (AOP_TYPE (oper) == AOP_CRY)
+    {
+      emitcode ("mov", "c,%s", oper->aop->aopu.aop_dir);
+    }
+  else
+    {
+      /* or the operand into a */
+      toBoolean (oper);
+      /* set C, if a >= 1 */
+      emitcode ("add", "a,#0xff");
+    }
+}
+
+/*-----------------------------------------------------------------*/
+/* assignBit - assign operand to bit operand                       */
+/*-----------------------------------------------------------------*/
+static void
+assignBit (operand * result, operand * right)
+{
+  /* if the right side is a literal then
+     we know what the value is */
+  if (AOP_TYPE (right) == AOP_LIT)
+    {
+      if ((int) operandLitValue (right))
+        aopPut (result, one, 0);
+      else
+        aopPut (result, zero, 0);
+    }
+  else
+    {
+      toCarry (right);
+      aopPut (result, "c", 0);
+    }
+}
+
+
+/*-------------------------------------------------------------------*/
+/* xch_a_aopGet - for exchanging acc with value of the aop           */
+/*-------------------------------------------------------------------*/
+static char *
+xch_a_aopGet (operand * oper, int offset, bool bit16, bool dname)
+{
+  char * l;
+
+  if (aopGetUsesAcc (oper, offset))
+    {
+      emitcode("mov", "b,a");
+      MOVA (aopGet (oper, offset, bit16, dname));
+      emitcode("xch", "a,b");
+      aopPut (oper, "a", offset);
+      emitcode("xch", "a,b");
+      l = "b";
+    }
+  else
+    {
+      l = aopGet (oper, offset, bit16, dname);
+      emitcode("xch", "a,%s", l);
+    }
+  return l;
+}
+
 
 /*-----------------------------------------------------------------*/
 /* genNot - generate code for ! operation                          */
@@ -1884,7 +2020,7 @@ genNot (iCode * ic)
   /* set C, if a == 0 */
   tlbl = newiTempLabel (NULL);
   emitcode ("cjne", "a,#0x01,%05d$", tlbl->key + 100);
-  emitcode ("", "%05d$:", tlbl->key + 100);
+  emitLabel (tlbl);
   outBitC (IC_RESULT (ic));
 
 release:
@@ -1940,7 +2076,7 @@ genCpl (iCode * ic)
           MOVA (l);
           emitcode ("cjne", "a,#0xFF,%05d$", tlbl->key + 100);
         }
-      emitcode ("", "%05d$:", tlbl->key + 100);
+      emitLabel (tlbl);
       outBitC (IC_RESULT(ic));
       goto release;
     }
@@ -2092,8 +2228,8 @@ saveRegisters (iCode * lic)
   if (ic->regsSaved)
     return;
   if (IS_SYMOP(IC_LEFT(ic)) &&
-      (IFFUNC_CALLEESAVES(OP_SYMBOL(IC_LEFT(ic))->type) ||
-       IFFUNC_ISNAKED(OP_SYM_TYPE(IC_LEFT (ic)))))
+      (IFFUNC_CALLEESAVES (OP_SYMBOL (IC_LEFT (ic))->type) ||
+       IFFUNC_ISNAKED (OP_SYM_TYPE (IC_LEFT (ic)))))
     return;
 
   /* save the registers in use at this time but skip the
@@ -2115,10 +2251,11 @@ saveRegisters (iCode * lic)
           rsave = bitVectCplAnd (rsave, rsavebits);
           rsave = bitVectSetBit (rsave, bitVectFirstBit (rsavebits));
         }
+      freeBitVect (rsavebits);
 
       if (count == 1)
         {
-          regs * reg = mcs51_regWithIdx (bitVectFirstBit (rsave));
+          regs * reg = REG_WITH_INDEX (bitVectFirstBit (rsave));
           if (reg->type == REG_BIT)
             {
               emitcode ("mov", "a,%s", reg->base);
@@ -2137,7 +2274,7 @@ saveRegisters (iCode * lic)
         {
           if (bitVectBitValue (rsave, R0_IDX))
             {
-              emitcode ("push", "%s", mcs51_regWithIdx (R0_IDX)->dname);
+              emitcode ("push", "%s", REG_WITH_INDEX (R0_IDX)->dname);
             }
           emitcode ("mov", "r0,%s", spname);
           MOVA ("r0");
@@ -2147,7 +2284,7 @@ saveRegisters (iCode * lic)
             {
               if (bitVectBitValue (rsave, i))
                 {
-                  regs * reg = mcs51_regWithIdx (i);
+                  regs * reg = REG_WITH_INDEX (i);
                   if (i == R0_IDX)
                     {
                       emitcode ("pop", "acc");
@@ -2170,7 +2307,7 @@ saveRegisters (iCode * lic)
             }
           if (bitVectBitValue (rsave, R0_IDX))
             {
-              emitcode ("pop", "%s", mcs51_regWithIdx (R0_IDX)->dname);
+              emitcode ("pop", "%s", REG_WITH_INDEX (R0_IDX)->dname);
             }
         }
     }
@@ -2185,6 +2322,7 @@ saveRegisters (iCode * lic)
             }
         }
     }
+  freeBitVect (rsave);
 }
 
 /*-----------------------------------------------------------------*/
@@ -2214,10 +2352,11 @@ unsaveRegisters (iCode * ic)
           rsave = bitVectCplAnd (rsave, rsavebits);
           rsave = bitVectSetBit (rsave, bitVectFirstBit (rsavebits));
         }
+      freeBitVect (rsavebits);
 
       if (count == 1)
         {
-          regs * reg = mcs51_regWithIdx (bitVectFirstBit (rsave));
+          regs * reg = REG_WITH_INDEX (bitVectFirstBit (rsave));
           emitcode ("mov", "r0,%s", spname);
           emitcode ("dec", "r0");
           emitcode ("movx", "a,@r0");
@@ -2238,7 +2377,7 @@ unsaveRegisters (iCode * ic)
             {
               if (bitVectBitValue (rsave, i))
                 {
-                  regs * reg = mcs51_regWithIdx (i);
+                  regs * reg = REG_WITH_INDEX (i);
                   emitcode ("dec", "r0");
                   emitcode ("movx", "a,@r0");
                   if (i == R0_IDX)
@@ -2273,6 +2412,7 @@ unsaveRegisters (iCode * ic)
             }
         }
     }
+  freeBitVect (rsave);
 }
 
 
@@ -2486,22 +2626,22 @@ static void
 saveRBank (int bank, iCode * ic, bool pushPsw)
 {
   int i;
-  int count = 8 + ((mcs51_nRegs > 8) ? 1 : 0) + (pushPsw ? 1 : 0);
+  int count = 8 + (pushPsw ? 1 : 0);
   asmop *aop = NULL;
   regs *r = NULL;
 
   if (options.useXstack)
     {
       if (!ic)
-      {
+        {
           /* Assume r0 is available for use. */
-          r = mcs51_regWithIdx (R0_IDX);;
-      }
+          r = REG_WITH_INDEX (R0_IDX);
+        }
       else
-      {
+        {
           aop = newAsmop (0);
           r = getFreePtr (ic, &aop, FALSE);
-      }
+        }
       // allocate space first
       emitcode ("mov", "%s,%s", r->name, spname);
       MOVA (r->name);
@@ -2524,22 +2664,6 @@ saveRBank (int bank, iCode * ic, bool pushPsw)
                   regs8051[i].base, 8 * bank + regs8051[i].offset);
     }
 
-  if (mcs51_nRegs > 8)
-    {
-      if (options.useXstack)
-        {
-          emitcode ("mov", "a,bits");
-          emitcode ("movx", "@%s,a", r->name);
-          if (--count)
-            emitcode ("inc", "%s", r->name);
-        }
-      else
-        {
-          emitcode ("push", "bits");
-        }
-      BitBankUsed = 1;
-    }
-
   if (pushPsw)
     {
       if (options.useXstack)
@@ -2561,9 +2685,9 @@ saveRBank (int bank, iCode * ic, bool pushPsw)
     }
 
   if (ic)
-  {
-    ic->bankSaved = 1;
-  }
+    {
+      ic->bankSaved = 1;
+    }
 }
 
 /*-----------------------------------------------------------------*/
@@ -2581,7 +2705,7 @@ unsaveRBank (int bank, iCode * ic, bool popPsw)
       if (!ic)
         {
           /* Assume r0 is available for use. */
-          r = mcs51_regWithIdx (R0_IDX);;
+          r = REG_WITH_INDEX (R0_IDX);;
         }
       else
         {
@@ -2605,20 +2729,6 @@ unsaveRBank (int bank, iCode * ic, bool popPsw)
         }
     }
 
-  if (mcs51_nRegs > 8)
-    {
-      if (options.useXstack)
-        {
-          emitcode ("dec", "%s", r->name);
-          emitcode ("movx", "a,@%s", r->name);
-          emitcode ("mov", "bits,a");
-        }
-      else
-        {
-          emitcode ("pop", "bits");
-        }
-    }
-
   for (i = 7; i >= 0; i--)
     {
       if (options.useXstack)
@@ -2673,19 +2783,10 @@ static void genSend(set *sendSet)
               else
                   emitcode ("clr", "b[%d]", bit);
             }
-          else if (AOP_TYPE (IC_LEFT (sic)) == AOP_CRY)
-            {
-              char *l = AOP (IC_LEFT (sic))->aopu.aop_dir;
-                if (strcmp (l, "c"))
-                    emitcode ("mov", "c,%s", l);
-                emitcode ("mov", "b[%d],c", bit);
-            }
           else
             {
               /* we need to or */
-              toBoolean (IC_LEFT (sic));
-              /* set C, if a >= 1 */
-              emitcode ("add", "a,#0xff");
+              toCarry (IC_LEFT (sic));
               emitcode ("mov", "b[%d],c", bit);
             }
           bit_count++;
@@ -2717,7 +2818,9 @@ static void genSend(set *sendSet)
                 {
                   char *l = aopGet (IC_LEFT (sic), offset, FALSE, FALSE);
                   if (strcmp (l, fReturn[offset]))
+                    {
                       emitcode ("mov", "%s,%s", fReturn[offset], l);
+                    }
                   offset++;
                 }
             }
@@ -2944,7 +3047,7 @@ genPcall (iCode * ic)
   /* if we are calling a not _naked function that is not using
      the same register bank then we need to save the
      destination registers on the stack */
-  if (currFunc && dtype && !IFFUNC_ISNAKED(dtype) &&
+  if (currFunc && dtype && !IFFUNC_ISNAKED (dtype) &&
       (FUNC_REGBANK (currFunc->type) != FUNC_REGBANK (dtype)) &&
       !IFFUNC_ISISR (dtype))
     {
@@ -2954,7 +3057,7 @@ genPcall (iCode * ic)
       // need caution message to user here
     }
 
-  if (IS_LITERAL(etype))
+  if (IS_LITERAL (etype))
     {
       /* if send set is not empty then assign */
       if (_G.sendSet)
@@ -2966,12 +3069,12 @@ genPcall (iCode * ic)
       if (swapBanks)
         {
           emitcode ("mov", "psw,#0x%02x",
-           ((FUNC_REGBANK(dtype)) << 3) & 0xff);
+           ((FUNC_REGBANK (dtype)) << 3) & 0xff);
         }
 
-      if (IFFUNC_ISBANKEDCALL (dtype) && !SPEC_STAT(getSpec(dtype)))
+      if (IFFUNC_ISBANKEDCALL (dtype) && !SPEC_STAT (getSpec(dtype)))
         {
-          if (IFFUNC_CALLEESAVES(dtype))
+          if (IFFUNC_CALLEESAVES (dtype))
             {
               werror (E_BANKED_WITH_CALLEESAVES);
             }
@@ -2992,9 +3095,9 @@ genPcall (iCode * ic)
     }
   else
     {
-      if (IFFUNC_ISBANKEDCALL (dtype) && !SPEC_STAT(getSpec(dtype)))
+      if (IFFUNC_ISBANKEDCALL (dtype) && !SPEC_STAT (getSpec(dtype)))
         {
-          if (IFFUNC_CALLEESAVES(dtype))
+          if (IFFUNC_CALLEESAVES (dtype))
             {
               werror (E_BANKED_WITH_CALLEESAVES);
             }
@@ -3004,6 +3107,7 @@ genPcall (iCode * ic)
 
               if (!swapBanks)
                 {
+                  /* what if aopGet needs r0 or r1 ??? */
                   emitcode ("mov", "ar0,%s", aopGet(IC_LEFT (ic), 0, FALSE, FALSE));
                   emitcode ("mov", "ar1,%s", aopGet(IC_LEFT (ic), 1, FALSE, FALSE));
                   emitcode ("mov", "ar2,%s", aopGet(IC_LEFT (ic), 2, FALSE, FALSE));
@@ -3028,14 +3132,14 @@ genPcall (iCode * ic)
               if (swapBanks)
                 {
                   emitcode ("mov", "psw,#0x%02x",
-                   ((FUNC_REGBANK(dtype)) << 3) & 0xff);
+                   ((FUNC_REGBANK (dtype)) << 3) & 0xff);
                 }
 
               /* make the call */
               emitcode ("lcall", "__sdcc_banked_call");
             }
         }
-      else
+      else if (_G.sendSet)
         {
           /* push the return address on to the stack */
           emitcode ("mov", "a,#%05d$", (rlbl->key + 100));
@@ -3060,17 +3164,49 @@ genPcall (iCode * ic)
           if (swapBanks)
             {
               emitcode ("mov", "psw,#0x%02x",
-               ((FUNC_REGBANK(dtype)) << 3) & 0xff);
+               ((FUNC_REGBANK (dtype)) << 3) & 0xff);
             }
 
           /* make the call */
           emitcode ("ret", "");
-          emitcode ("", "%05d$:", (rlbl->key + 100));
+          emitLabel (rlbl);
+        }
+      else /* the send set is empty */
+        {
+          char *l;
+          /* now get the calling address into dptr */
+          aopOp (IC_LEFT (ic), ic, FALSE);
+
+          l = aopGet (IC_LEFT (ic), 0, FALSE, FALSE);
+          if (AOP_TYPE (IC_LEFT (ic)) == AOP_DPTR)
+            {
+              emitcode ("mov", "r0,%s", l);
+              l = aopGet (IC_LEFT (ic), 1, FALSE, FALSE);
+              emitcode ("mov", "dph,%s", l);
+              emitcode ("mov", "dpl,r0");
+            }
+          else
+            {
+              emitcode ("mov", "dpl,%s", l);
+              l = aopGet (IC_LEFT (ic), 1, FALSE, FALSE);
+              emitcode ("mov", "dph,%s", l);
+            }
+
+          freeAsmop (IC_LEFT (ic), NULL, ic, TRUE);
+
+          if (swapBanks)
+            {
+              emitcode ("mov", "psw,#0x%02x",
+               ((FUNC_REGBANK (dtype)) << 3) & 0xff);
+            }
+
+          /* make the call */
+          emitcode ("lcall", "__sdcc_call_dptr");
         }
     }
   if (swapBanks)
     {
-      selectRegBank (FUNC_REGBANK(currFunc->type), IS_BIT (etype));
+      selectRegBank (FUNC_REGBANK (currFunc->type), IS_BIT (etype));
     }
 
   /* if we need assign a result value */
@@ -3110,7 +3246,6 @@ genPcall (iCode * ic)
       else
         for (i = 0; i < ic->parmBytes; i++)
           emitcode ("dec", "%s", spname);
-
     }
 
 //  /* if register bank was saved then unsave them */
@@ -3118,7 +3253,7 @@ genPcall (iCode * ic)
 //    unsaveRBank (FUNC_REGBANK (dtype), ic, TRUE);
 
   /* if we had saved some registers then unsave them */
-  if (ic->regsSaved && !IFFUNC_CALLEESAVES(dtype))
+  if (ic->regsSaved && !IFFUNC_CALLEESAVES (dtype))
     unsaveRegisters (ic);
 
   if (IS_BIT (OP_SYM_ETYPE (IC_RESULT (ic))))
@@ -3151,12 +3286,6 @@ resultRemat (iCode * ic)
   return 0;
 }
 
-#if defined(__BORLANDC__) || defined(_MSC_VER)
-#define STRCASECMP stricmp
-#else
-#define STRCASECMP strcasecmp
-#endif
-
 /*-----------------------------------------------------------------*/
 /* inExcludeList - return 1 if the string is in exclude Reg list   */
 /*-----------------------------------------------------------------*/
@@ -3200,6 +3329,7 @@ genFunction (iCode * ic)
   emitcode (";", "-----------------------------------------");
 
   emitcode ("", "%s:", sym->rname);
+  lineCurr->isLabel = 1;
   ftype = operandType (IC_LEFT (ic));
   _G.currentFunc = sym;
 
@@ -3237,6 +3367,16 @@ genFunction (iCode * ic)
      save acc, b, dpl, dph  */
   if (IFFUNC_ISISR (sym->type))
     {
+      bitVect *rsavebits;
+
+      rsavebits = bitVectIntersect (bitVectCopy (mcs51_allBitregs ()), sym->regsUsed);
+      if (IFFUNC_HASFCALL(sym->type) || !bitVectIsZero (rsavebits))
+        {
+          emitcode ("push", "bits");
+          BitBankUsed = 1;
+        }
+      freeBitVect (rsavebits);
+
       if (!inExcludeList ("acc"))
         emitcode ("push", "acc");
       if (!inExcludeList ("b"))
@@ -3260,12 +3400,11 @@ genFunction (iCode * ic)
               /* if any registers used */
               if (sym->regsUsed)
                 {
-                  bool bits_pushed = FALSE;
                   /* save the registers used */
                   for (i = 0; i < sym->regsUsed->size; i++)
                     {
                       if (bitVectBitValue (sym->regsUsed, i))
-                        bits_pushed = pushReg (i, bits_pushed);
+                        pushReg (i, TRUE);
                     }
                 }
             }
@@ -3282,7 +3421,7 @@ genFunction (iCode * ic)
                 }
             }
         }
-        else
+      else
         {
             /* This ISR uses a non-zero bank.
              *
@@ -3412,7 +3551,6 @@ genFunction (iCode * ic)
         }
     }
 
-
   if (fReentrant)
     {
       if (options.useXstack)
@@ -3468,7 +3606,7 @@ genFunction (iCode * ic)
           int ofs;
 
           _G.current_iCode = ric;
-          D(emitcode (";     genReceive",""));
+          D(emitcode (";", "genReceive"));
           for (ofs=0; ofs < sym->recvSize; ofs++)
             {
               if (!strcmp (fReturn[ofs], "a"))
@@ -3493,7 +3631,7 @@ genFunction (iCode * ic)
           int ofs;
 
           _G.current_iCode = ric;
-          D(emitcode (";     genReceive",""));
+          D(emitcode (";", "genReceive"));
           for (ofs=0; ofs < sym->recvSize; ofs++)
             {
               emitcode ("mov", "%s,%s", rsym->regs[ofs]->name, fReturn[ofs]);
@@ -3529,11 +3667,11 @@ genFunction (iCode * ic)
               /* if it's a callee-saves function we need a saved register */
               if (calleesaves_saved_register >= 0)
                 {
-                  emitcode ("mov", "%s,a", mcs51_regWithIdx (calleesaves_saved_register)->dname);
+                  emitcode ("mov", "%s,a", REG_WITH_INDEX (calleesaves_saved_register)->dname);
                   emitcode ("mov", "a,sp");
                   emitcode ("add", "a,#0x%02x", ((char) sym->stack & 0xff));
                   emitcode ("mov", "sp,a");
-                  emitcode ("mov", "a,%s", mcs51_regWithIdx (calleesaves_saved_register)->dname);
+                  emitcode ("mov", "a,%s", REG_WITH_INDEX (calleesaves_saved_register)->dname);
                 }
               else
                 /* do it the hard way */
@@ -3562,14 +3700,14 @@ genFunction (iCode * ic)
       if (i > 3 && accIsFree)
         {
           emitcode ("mov", "a,_spx");
-          emitcode ("add", "a,#0x%02x", i);
+          emitcode ("add", "a,#0x%02x", i & 0xff);
           emitcode ("mov", "_spx,a");
         }
       else if (i > 5)
         {
           emitcode ("push", "acc");
           emitcode ("mov", "a,_spx");
-          emitcode ("add", "a,#0x%02x", i);
+          emitcode ("add", "a,#0x%02x", i & 0xff);
           emitcode ("mov", "_spx,a");
           emitcode ("pop", "acc");
         }
@@ -3587,7 +3725,7 @@ genFunction (iCode * ic)
       emitcode ("setb", "c");
       emitcode ("jbc", "ea,%05d$", (tlbl->key + 100)); /* atomic test & clear */
       emitcode ("clr", "c");
-      emitcode ("", "%05d$:", (tlbl->key + 100));
+      emitLabel (tlbl);
       emitcode ("push", "psw"); /* save old ea via c in psw */
     }
 }
@@ -3651,7 +3789,8 @@ genEndFunction (iCode * ic)
         }
       else if (sym->stack || FUNC_HASSTACKPARM(sym->type))
         {
-          emitcode ("mov", "sp,_bp");
+          if (sym->stack)
+            emitcode ("mov", "sp,_bp");
           emitcode ("pop", "_bp");
         }
     }
@@ -3671,6 +3810,7 @@ genEndFunction (iCode * ic)
 
   if (IFFUNC_ISISR (sym->type))
     {
+      bitVect *rsavebits;
 
       /* now we need to restore the registers */
       /* if this isr has no bank i.e. is going to
@@ -3687,12 +3827,11 @@ genEndFunction (iCode * ic)
               /* if any registers used */
               if (sym->regsUsed)
                 {
-                  bool bits_popped = FALSE;
                   /* save the registers used */
                   for (i = sym->regsUsed->size; i >= 0; i--)
                     {
                       if (bitVectBitValue (sym->regsUsed, i))
-                        bits_popped = popReg (i, bits_popped);
+                        popReg (i, TRUE);
                     }
                 }
             }
@@ -3703,8 +3842,8 @@ genEndFunction (iCode * ic)
                       emitcode ("pop","%s",rb1regs[i]);
                   }
               }
-              /* this function has  a function call cannot
-                 determines register usage so we will have to pop the
+              /* this function has a function call. We cannot
+                 determine register usage so we will have to pop the
                  entire bank */
               unsaveRBank (0, ic, FALSE);
             }
@@ -3745,6 +3884,11 @@ genEndFunction (iCode * ic)
       if (!inExcludeList ("acc"))
         emitcode ("pop", "acc");
 
+      rsavebits = bitVectIntersect (bitVectCopy (mcs51_allBitregs ()), sym->regsUsed);
+      if (IFFUNC_HASFCALL(sym->type) || !bitVectIsZero (rsavebits))
+        emitcode ("pop", "bits");
+      freeBitVect (rsavebits);
+
       /* if debug then send end of function */
       if (options.debug && currFunc)
         {
@@ -3767,13 +3911,13 @@ genEndFunction (iCode * ic)
                 {
                   if (bitVectBitValue (sym->regsUsed, i) ||
                       (mcs51_ptrRegReq && (i == R0_IDX || i == R1_IDX)))
-                    emitcode ("pop", "%s", mcs51_regWithIdx (i)->dname);
+                    emitcode ("pop", "%s", REG_WITH_INDEX (i)->dname);
                 }
             }
           else if (mcs51_ptrRegReq)
             {
-              emitcode ("pop", "%s", mcs51_regWithIdx (R1_IDX)->dname);
-              emitcode ("pop", "%s", mcs51_regWithIdx (R0_IDX)->dname);
+              emitcode ("pop", "%s", REG_WITH_INDEX (R1_IDX)->dname);
+              emitcode ("pop", "%s", REG_WITH_INDEX (R0_IDX)->dname);
             }
 
         }
@@ -3882,7 +4026,7 @@ genEndFunction (iCode * ic)
 
   for (idx = 0; idx < regsUnneeded->size; idx++)
     if (bitVectBitValue (regsUnneeded, idx))
-      emitcode ("", ";\teliminated unneeded push/pop %s", mcs51_regWithIdx (idx)->dname);
+      emitcode (";", "eliminated unneeded push/pop %s", REG_WITH_INDEX (idx)->dname);
 
   freeBitVect (regsUnneeded);
   freeBitVect (regsUsed);
@@ -3911,38 +4055,38 @@ genRet (iCode * ic)
 
   if (IS_BIT(_G.currentFunc->etype))
     {
-      movc (aopGet (IC_LEFT (ic), 0, FALSE, FALSE));
-      size = 0;
+      if (!(IS_SYMOP (IC_LEFT (ic)) && OP_SYMBOL (IC_LEFT (ic))->ruonly))
+        toCarry (IC_LEFT (ic));
     }
-
-  while (size--)
+  else
     {
-      char *l;
-      if (AOP_TYPE (IC_LEFT (ic)) == AOP_DPTR)
+      while (size--)
         {
-          /* #NOCHANGE */
-          l = aopGet (IC_LEFT (ic), offset++,
-                      FALSE, TRUE);
-          emitcode ("push", "%s", l);
-          pushed++;
+          char *l;
+          if (AOP_TYPE (IC_LEFT (ic)) == AOP_DPTR)
+            {
+              /* #NOCHANGE */
+              l = aopGet (IC_LEFT (ic), offset++, FALSE, TRUE);
+              emitcode ("push", "%s", l);
+              pushed++;
+            }
+          else
+            {
+              l = aopGet (IC_LEFT (ic), offset, FALSE, FALSE);
+              if (strcmp (fReturn[offset], l))
+                emitcode ("mov", "%s,%s", fReturn[offset++], l);
+            }
         }
-      else
+
+      while (pushed)
         {
-          l = aopGet (IC_LEFT (ic), offset,
-                      FALSE, FALSE);
-          if (strcmp (fReturn[offset], l))
-            emitcode ("mov", "%s,%s", fReturn[offset++], l);
+          pushed--;
+          if (strcmp (fReturn[pushed], "a"))
+            emitcode ("pop", fReturn[pushed]);
+          else
+            emitcode ("pop", "acc");
         }
     }
-
-  while (pushed)
-    {
-      pushed--;
-      if (strcmp (fReturn[pushed], "a"))
-        emitcode ("pop", fReturn[pushed]);
-      else
-        emitcode ("pop", "acc");
-    }
   freeAsmop (IC_LEFT (ic), NULL, ic, TRUE);
 
 jumpret:
@@ -3965,7 +4109,7 @@ genLabel (iCode * ic)
   if (IC_LABEL (ic) == entryLabel)
     return;
 
-  emitcode ("", "%05d$:", (IC_LABEL (ic)->key + 100));
+  emitLabel (IC_LABEL (ic));
 }
 
 /*-----------------------------------------------------------------*/
@@ -4026,10 +4170,12 @@ genPlusIncr (iCode * ic)
 
   icount = (unsigned int) floatFromVal (AOP (IC_RIGHT (ic))->aopu.aop_lit);
 
-  D(emitcode (";     genPlusIncr",""));
+  D(emitcode (";","genPlusIncr"));
 
   /* if increment >=16 bits in register or direct space */
-  if ((AOP_TYPE(IC_LEFT(ic)) == AOP_REG || AOP_TYPE(IC_LEFT(ic)) == AOP_DIR ) &&
+  if (( AOP_TYPE(IC_LEFT(ic)) == AOP_REG || 
+        AOP_TYPE(IC_LEFT(ic)) == AOP_DIR ||
+        (IS_AOP_PREG (IC_LEFT(ic)) && !AOP_NEEDSACC (IC_LEFT(ic))) ) &&
       sameRegs (AOP (IC_LEFT (ic)), AOP (IC_RESULT (ic))) &&
       !isOperandVolatile (IC_RESULT (ic), FALSE) &&
       (size > 1) &&
@@ -4047,7 +4193,7 @@ genPlusIncr (iCode * ic)
           && (labelRange = findLabelBackwards (ic, IC_LABEL (ic->next)->key)) != 0
           && labelRange <= 10)
         {
-          emitcode (";", "tail increment optimized");
+          D (emitcode (";", "tail increment optimized (range %d)", labelRange));
           tlbl = IC_LABEL (ic->next);
           emitTlbl = 0;
         }
@@ -4103,7 +4249,7 @@ genPlusIncr (iCode * ic)
 
       if (emitTlbl)
         {
-          emitcode ("", "%05d$:", tlbl->key + 100);
+          emitLabel (tlbl);
         }
       return TRUE;
     }
@@ -4146,7 +4292,6 @@ genPlusIncr (iCode * ic)
      same */
   if (sameRegs (AOP (IC_LEFT (ic)), AOP (IC_RESULT (ic))))
     {
-
       if (icount > 3)
         {
           MOVA (aopGet (IC_LEFT (ic), 0, FALSE, FALSE));
@@ -4155,14 +4300,23 @@ genPlusIncr (iCode * ic)
         }
       else
         {
-
           while (icount--)
-            emitcode ("inc", "%s", aopGet (IC_LEFT (ic), 0, FALSE, FALSE));
+            {
+              emitcode ("inc", "%s", aopGet (IC_LEFT (ic), 0, FALSE, FALSE));
+            }
         }
 
       return TRUE;
     }
 
+  if (icount == 1)
+    {
+      MOVA (aopGet (IC_LEFT (ic), 0, FALSE, FALSE));
+      emitcode ("inc", "a");
+      aopPut (IC_RESULT (ic), "a", 0);
+      return TRUE;
+    }
+
   return FALSE;
 }
 
@@ -4182,7 +4336,7 @@ outBitAcc (operand * result)
     {
       emitcode ("jz", "%05d$", tlbl->key + 100);
       emitcode ("mov", "a,%s", one);
-      emitcode ("", "%05d$:", tlbl->key + 100);
+      emitLabel (tlbl);
       outAcc (result);
     }
 }
@@ -4193,24 +4347,23 @@ outBitAcc (operand * result)
 static void
 genPlusBits (iCode * ic)
 {
-  D(emitcode (";     genPlusBits",""));
+  D (emitcode (";", "genPlusBits"));
 
+  emitcode ("mov", "c,%s", AOP (IC_LEFT (ic))->aopu.aop_dir);
   if (AOP_TYPE (IC_RESULT (ic)) == AOP_CRY)
     {
       symbol *lbl = newiTempLabel (NULL);
-      emitcode ("mov", "c,%s", AOP (IC_LEFT (ic))->aopu.aop_dir);
       emitcode ("jnb", "%s,%05d$", AOP (IC_RIGHT (ic))->aopu.aop_dir, (lbl->key + 100));
       emitcode ("cpl", "c");
-      emitcode ("", "%05d$:", (lbl->key + 100));
+      emitLabel (lbl);
       outBitC (IC_RESULT (ic));
     }
   else
     {
       emitcode ("clr", "a");
-      emitcode ("mov", "c,%s", AOP (IC_LEFT (ic))->aopu.aop_dir);
       emitcode ("rlc", "a");
       emitcode ("mov", "c,%s", AOP (IC_RIGHT (ic))->aopu.aop_dir);
-      emitcode ("addc", "a,#0x00");
+      emitcode ("addc", "a,%s", zero);
       outAcc (IC_RESULT (ic));
     }
 }
@@ -4282,7 +4435,8 @@ adjustArithmeticResult (iCode * ic)
       !sameRegs (AOP (IC_RESULT (ic)), AOP (IC_RIGHT (ic))))
     {
       char buffer[5];
-      sprintf (buffer, "#%d", pointerTypeToGPByte (pointerCode (getSpec (operandType (IC_LEFT (ic)))), NULL, NULL));
+      SNPRINTF (buffer, sizeof(buffer),
+                "#%d", pointerTypeToGPByte (pointerCode (getSpec (operandType (IC_LEFT (ic)))), NULL, NULL));
       aopPut (IC_RESULT (ic), buffer, GPTRSIZE - 1);
     }
 }
@@ -4301,9 +4455,9 @@ genPlus (iCode * ic)
   operand *leftOp, *rightOp;
   operand * op;
 
-  /* special cases :- */
+  D (emitcode (";", "genPlus"));
 
-  D(emitcode (";     genPlus",""));
+  /* special cases :- */
 
   aopOp (IC_LEFT (ic), ic, FALSE);
   aopOp (IC_RIGHT (ic), ic, FALSE);
@@ -4349,7 +4503,7 @@ genPlus (iCode * ic)
           while (size--)
             {
               MOVA (aopGet (IC_RIGHT (ic), offset, FALSE, FALSE));
-              emitcode ("addc", "a,#00");
+              emitcode ("addc", "a,%s", zero);
               aopPut (IC_RESULT (ic), "a", offset++);
             }
         }
@@ -4376,7 +4530,7 @@ genPlus (iCode * ic)
        && (SPEC_ADDR (OP_SYM_ETYPE (op)) & 0xff) == 0
      )
     {
-      D(emitcode (";     genPlus aligned array",""));
+      D(emitcode (";", "genPlus aligned array"));
       aopPut (IC_RESULT (ic),
               aopGet (rightOp, 0, FALSE, FALSE),
               0);
@@ -4405,7 +4559,7 @@ genPlus (iCode * ic)
            skip_bytes++;
          }
        if (skip_bytes)
-         D(emitcode (";     genPlus shortcut",""));
+         D(emitcode (";", "genPlus shortcut"));
     }
 
   while (size--)
@@ -4485,10 +4639,12 @@ genMinusDec (iCode * ic)
   if ((icount = (unsigned int) floatFromVal (AOP (IC_RIGHT (ic))->aopu.aop_lit)) > 4)
     return FALSE;
 
-  D(emitcode (";     genMinusDec",""));
+  D (emitcode (";", "genMinusDec"));
 
   /* if decrement >=16 bits in register or direct space */
-  if ((AOP_TYPE(IC_LEFT(ic)) == AOP_REG || AOP_TYPE(IC_LEFT(ic)) == AOP_DIR) &&
+  if (( AOP_TYPE(IC_LEFT(ic)) == AOP_REG || 
+        AOP_TYPE(IC_LEFT(ic)) == AOP_DIR ||
+        (IS_AOP_PREG (IC_LEFT(ic)) && !AOP_NEEDSACC (IC_LEFT(ic))) ) &&
       sameRegs (AOP (IC_LEFT (ic)), AOP (IC_RESULT (ic))) &&
       (size > 1) &&
       (icount == 1))
@@ -4505,7 +4661,7 @@ genMinusDec (iCode * ic)
           && (labelRange = findLabelBackwards (ic, IC_LABEL (ic->next)->key)) != 0
           && labelRange <= 10)
         {
-          emitcode (";", "tail decrement optimized");
+          D (emitcode (";", "tail decrement optimized (range %d)", labelRange));
           tlbl = IC_LABEL (ic->next);
           emitTlbl = 0;
         }
@@ -4561,7 +4717,7 @@ genMinusDec (iCode * ic)
         }
       if (emitTlbl)
         {
-          emitcode ("", "%05d$:", tlbl->key + 100);
+          emitLabel (tlbl);
         }
       return TRUE;
     }
@@ -4589,7 +4745,9 @@ genMinusDec (iCode * ic)
         }
 
       while (icount--)
-        emitcode ("dec", "%s", l);
+        {
+          emitcode ("dec", "%s", l);
+        }
 
       if (AOP_NEEDSACC (IC_RESULT (ic)))
         aopPut (IC_RESULT (ic), "a", 0);
@@ -4597,6 +4755,14 @@ genMinusDec (iCode * ic)
       return TRUE;
     }
 
+  if (icount == 1)
+    {
+      MOVA (aopGet (IC_LEFT (ic), 0, FALSE, FALSE));
+      emitcode ("dec", "a");
+      aopPut (IC_RESULT (ic), "a", 0);
+      return TRUE;
+    }
+
   return FALSE;
 }
 
@@ -4614,11 +4780,17 @@ addSign (operand * result, int offset, int sign)
           emitcode ("rlc", "a");
           emitcode ("subb", "a,acc");
           while (size--)
-            aopPut (result, "a", offset++);
+            {
+              aopPut (result, "a", offset++);
+            }
         }
       else
-        while (size--)
-          aopPut (result, zero, offset++);
+        {
+          while (size--)
+            {
+              aopPut (result, zero, offset++);
+            }
+        }
     }
 }
 
@@ -4630,14 +4802,14 @@ genMinusBits (iCode * ic)
 {
   symbol *lbl = newiTempLabel (NULL);
 
-  D(emitcode (";     genMinusBits",""));
+  D (emitcode (";", "genMinusBits"));
 
   if (AOP_TYPE (IC_RESULT (ic)) == AOP_CRY)
     {
       emitcode ("mov", "c,%s", AOP (IC_LEFT (ic))->aopu.aop_dir);
       emitcode ("jnb", "%s,%05d$", AOP (IC_RIGHT (ic))->aopu.aop_dir, (lbl->key + 100));
       emitcode ("cpl", "c");
-      emitcode ("", "%05d$:", (lbl->key + 100));
+      emitLabel (lbl);
       outBitC (IC_RESULT (ic));
     }
   else
@@ -4646,7 +4818,7 @@ genMinusBits (iCode * ic)
       emitcode ("subb", "a,acc");
       emitcode ("jnb", "%s,%05d$", AOP (IC_LEFT (ic))->aopu.aop_dir, (lbl->key + 100));
       emitcode ("inc", "a");
-      emitcode ("", "%05d$:", (lbl->key + 100));
+      emitLabel (lbl);
       aopPut (IC_RESULT (ic), "a", 0);
       addSign (IC_RESULT (ic), MSB16, SPEC_USIGN (getSpec (operandType (IC_RESULT (ic)))));
     }
@@ -4660,7 +4832,7 @@ genMinus (iCode * ic)
 {
   int size, offset = 0;
 
-  D(emitcode (";     genMinus",""));
+  D (emitcode (";", "genMinus"));
 
   aopOp (IC_LEFT (ic), ic, FALSE);
   aopOp (IC_RIGHT (ic), ic, FALSE);
@@ -4695,7 +4867,7 @@ genMinus (iCode * ic)
         {
           if (useCarry || ((lit >> (offset * 8)) & 0x0FFL))
             {
-            MOVA (aopGet (IC_LEFT (ic), offset, FALSE, FALSE));
+              MOVA (aopGet (IC_LEFT (ic), offset, FALSE, FALSE));
               if (!offset && !size && lit== (unsigned long) -1)
                 {
                   emitcode ("dec", "a");
@@ -4751,7 +4923,7 @@ genMinus (iCode * ic)
               /* reverse subtraction with 2's complement */
               if (offset == 0)
                 emitcode( "setb", "c");
-               else
+              else
                 emitcode( "cpl", "c");
               wassertl(!aopGetUsesAcc(leftOp, offset), "accumulator clash");
               MOVA (aopGet(rightOp, offset, FALSE, TRUE));
@@ -4789,7 +4961,7 @@ genMultbits (operand * left,
              operand * right,
              operand * result)
 {
-  D(emitcode (";     genMultbits",""));
+  D (emitcode (";", "genMultbits"));
 
   emitcode ("mov", "c,%s", AOP (left)->aopu.aop_dir);
   emitcode ("anl", "c,%s", AOP (right)->aopu.aop_dir);
@@ -4809,7 +4981,7 @@ genMultOneByte (operand * left,
   bool runtimeSign, compiletimeSign;
   bool lUnsigned, rUnsigned, pushedB;
 
-  D(emitcode (";     genMultOneByte",""));
+  D (emitcode (";", "genMultOneByte"));
 
   if (size < 1 || size > 2)
     {
@@ -4855,7 +5027,7 @@ genMultOneByte (operand * left,
         {
           /* moving to accumulator first helps peepholes */
           MOVA (aopGet (left, 0, FALSE, FALSE));
-          emitcode ("mov", "b,%s", aopGet (right, 0, FALSE, FALSE));
+          MOVB (aopGet (right, 0, FALSE, FALSE));
         }
       else
         {
@@ -4942,7 +5114,7 @@ genMultOneByte (operand * left,
           emitcode ("cpl", "F0"); /* complement sign flag */
           emitcode ("cpl", "a");  /* 2's complement */
           emitcode ("inc", "a");
-          emitcode ("", "%05d$:", (lbl->key + 100));
+          emitLabel (lbl);
           emitcode ("mov", "b,a");
         }
     }
@@ -4967,7 +5139,7 @@ genMultOneByte (operand * left,
           emitcode ("cpl", "F0"); /* complement sign flag */
           emitcode ("cpl", "a"); /* 2's complement */
           emitcode ("inc", "a");
-          emitcode ("", "%05d$:", (lbl->key + 100));
+          emitLabel (lbl);
         }
     }
 
@@ -4989,7 +5161,7 @@ genMultOneByte (operand * left,
           emitcode ("addc", "a,#0");
           emitcode ("xch", "a,b");
         }
-      emitcode ("", "%05d$:", (lbl->key + 100));
+      emitLabel (lbl);
     }
   aopPut (result, "a", 0);
   if (size == 2)
@@ -5008,7 +5180,7 @@ genMult (iCode * ic)
   operand *right = IC_RIGHT (ic);
   operand *result = IC_RESULT (ic);
 
-  D(emitcode (";     genMult",""));
+  D (emitcode (";", "genMult"));
 
   /* assign the asmops */
   aopOp (left, ic, FALSE);
@@ -5058,7 +5230,7 @@ genDivbits (operand * left,
   char *l;
   bool pushedB;
 
-  D(emitcode (";     genDivbits",""));
+  D(emitcode (";", "genDivbits"));
 
   pushedB = pushB ();
 
@@ -5091,7 +5263,7 @@ genDivOneByte (operand * left,
   symbol *lbl;
   int size, offset;
 
-  D(emitcode (";     genDivOneByte",""));
+  D(emitcode (";", "genDivOneByte"));
 
   /* Why is it necessary that genDivOneByte() can return an int result?
      Have a look at:
@@ -5130,7 +5302,7 @@ genDivOneByte (operand * left,
   if (lUnsigned && rUnsigned)
     {
       /* unsigned is easy */
-      emitcode ("mov", "b,%s", aopGet (right, 0, FALSE, FALSE));
+      MOVB (aopGet (right, 0, FALSE, FALSE));
       MOVA (aopGet (left, 0, FALSE, FALSE));
       emitcode ("div", "ab");
       aopPut (result, "a", 0);
@@ -5210,7 +5382,7 @@ genDivOneByte (operand * left,
           emitcode ("cpl", "F0"); /* complement sign flag */
           emitcode ("cpl", "a");  /* 2's complement */
           emitcode ("inc", "a");
-          emitcode ("", "%05d$:", (lbl->key + 100));
+          emitLabel (lbl);
           emitcode ("mov", "b,a");
         }
     }
@@ -5235,7 +5407,7 @@ genDivOneByte (operand * left,
           emitcode ("cpl", "F0"); /* complement sign flag */
           emitcode ("cpl", "a");  /* 2's complement */
           emitcode ("inc", "a");
-          emitcode ("", "%05d$:", (lbl->key + 100));
+          emitLabel (lbl);
         }
     }
 
@@ -5249,7 +5421,7 @@ genDivOneByte (operand * left,
         emitcode ("jnb", "F0,%05d$", (lbl->key + 100));
       emitcode ("cpl", "a"); /* lsb 2's complement */
       emitcode ("inc", "a");
-      emitcode ("", "%05d$:", (lbl->key + 100));
+      emitLabel (lbl);
 
       accuse = aopPut (result, "a", 0);
       if (size > 0)
@@ -5301,9 +5473,9 @@ genDiv (iCode * ic)
   operand *right = IC_RIGHT (ic);
   operand *result = IC_RESULT (ic);
 
-  D(emitcode (";     genDiv",""));
+  D (emitcode (";", "genDiv"));
 
-  /* assign the amsops */
+  /* assign the asmops */
   aopOp (left, ic, FALSE);
   aopOp (right, ic, FALSE);
   aopOp (result, ic, TRUE);
@@ -5344,7 +5516,7 @@ genModbits (operand * left,
   char *l;
   bool pushedB;
 
-  D(emitcode (";     genModbits",""));
+  D (emitcode (";", "genModbits"));
 
   pushedB = pushB ();
 
@@ -5376,7 +5548,7 @@ genModOneByte (operand * left,
   symbol *lbl;
   int size, offset;
 
-  D(emitcode (";     genModOneByte",""));
+  D (emitcode (";", "genModOneByte"));
 
   size = AOP_SIZE (result) - 1;
   offset = 1;
@@ -5423,13 +5595,13 @@ genModOneByte (operand * left,
                 lbl2 = newiTempLabel (NULL);
                 emitcode ("sjmp", "%05d$", (lbl2->key + 100));
               }
-            emitcode ("", "%05d$:", (lbl->key + 100));
+            emitLabel (lbl);
             aopPut (result, "a", 0);
             while (size--)
               aopPut (result, zero, offset++);
             if (lbl2)
               {
-                emitcode ("", "%05d$:", (lbl2->key + 100));
+                emitLabel (lbl2);
               }
             return;
 
@@ -5444,7 +5616,7 @@ genModOneByte (operand * left,
   if (lUnsigned && rUnsigned)
     {
       /* unsigned is easy */
-      emitcode ("mov", "b,%s", aopGet (right, 0, FALSE, FALSE));
+      MOVB (aopGet (right, 0, FALSE, FALSE));
       MOVA (aopGet (left, 0, FALSE, FALSE));
       emitcode ("div", "ab");
       aopPut (result, "b", 0);
@@ -5480,7 +5652,7 @@ genModOneByte (operand * left,
           emitcode ("jnb", "acc.7,%05d$", (lbl->key + 100));
           emitcode ("cpl", "a"); /* 2's complement */
           emitcode ("inc", "a");
-          emitcode ("", "%05d$:", (lbl->key + 100));
+          emitLabel (lbl);
           emitcode ("mov", "b,a");
         }
     }
@@ -5518,7 +5690,7 @@ genModOneByte (operand * left,
           emitcode ("setb", "F0"); /* set sign flag */
           emitcode ("cpl", "a");   /* 2's complement */
           emitcode ("inc", "a");
-          emitcode ("", "%05d$:", (lbl->key + 100));
+          emitLabel (lbl);
         }
     }
 
@@ -5533,7 +5705,7 @@ genModOneByte (operand * left,
         emitcode ("jnb", "F0,%05d$", (lbl->key + 100));
       emitcode ("cpl", "a"); /* 2's complement */
       emitcode ("inc", "a");
-      emitcode ("", "%05d$:", (lbl->key + 100));
+      emitLabel (lbl);
 
       aopPut (result, "a", 0);
       if (size > 0)
@@ -5571,7 +5743,7 @@ genMod (iCode * ic)
   operand *right = IC_RIGHT (ic);
   operand *result = IC_RESULT (ic);
 
-  D(emitcode (";     genMod",""));
+  D (emitcode (";", "genMod"));
 
   /* assign the asmops */
   aopOp (left, ic, FALSE);
@@ -5614,7 +5786,7 @@ genIfxJump (iCode * ic, char *jval, operand *left, operand *right, operand *resu
   symbol *tlbl = newiTempLabel (NULL);
   char *inst;
 
-  D(emitcode (";     genIfxJump",""));
+  D (emitcode (";", "genIfxJump"));
 
   /* if true label then we jump if condition
      supplied is true */
@@ -5639,7 +5811,7 @@ genIfxJump (iCode * ic, char *jval, operand *left, operand *right, operand *resu
   freeForBranchAsmop (right);
   freeForBranchAsmop (left);
   emitcode ("ljmp", "%05d$", jlbl->key + 100);
-  emitcode ("", "%05d$:", tlbl->key + 100);
+  emitLabel (tlbl);
 
   /* mark the icode as generated */
   ic->generated = 1;
@@ -5656,7 +5828,7 @@ genCmp (operand * left, operand * right,
   unsigned long lit = 0L;
   bool rightInB;
 
-  D(emitcode (";     genCmp",""));
+  D (emitcode (";", "genCmp"));
 
   /* if left & right are bit variables */
   if (AOP_TYPE (left) == AOP_CRY &&
@@ -5681,7 +5853,7 @@ genCmp (operand * left, operand * right,
                     aopGet (left, offset, FALSE, FALSE),
                     aopGet (right, offset, FALSE, FALSE),
                     lbl->key + 100);
-          emitcode ("", "%05d$:", lbl->key + 100);
+          emitLabel (lbl);
         }
       else
         {
@@ -5707,7 +5879,36 @@ genCmp (operand * left, operand * right,
                           return;
                         }
                       else
-                        emitcode ("rlc", "a");
+                        {
+                          emitcode ("rlc", "a");
+                        }
+                    }
+                  goto release;
+                }
+              else
+                {//nonzero literal
+                  int bytelit = ((lit >> (offset * 8)) & 0x0FFL);
+                  while (size && (bytelit == 0))
+                    {
+                      offset++;
+                      bytelit = ((lit >> (offset * 8)) & 0x0FFL);
+                      size--;
+                    }
+                  CLRC;
+                  while (size--)
+                    {
+                      MOVA (aopGet (left, offset, FALSE, FALSE));
+                      if (sign && size == 0)
+                        {
+                          emitcode ("xrl", "a,#0x80");
+                          emitcode ("subb", "a,#0x%02x",
+                                    0x80 ^ (unsigned int) ((lit >> (offset * 8)) & 0x0FFL));
+                        }
+                      else
+                        {
+                          emitcode ("subb", "a,%s", aopGet (right, offset, FALSE, FALSE));
+                        }
+                      offset++;
                     }
                   goto release;
                 }
@@ -5726,24 +5927,14 @@ genCmp (operand * left, operand * right,
               if (sign && size == 0)
                 {
                   emitcode ("xrl", "a,#0x80");
-                  if (AOP_TYPE (right) == AOP_LIT)
+                  if (!rightInB)
                     {
-                      unsigned long lit = (unsigned long)
-                      floatFromVal (AOP (right)->aopu.aop_lit);
-                      emitcode ("subb", "a,#0x%02x",
-                                0x80 ^ (unsigned int) ((lit >> (offset * 8)) & 0x0FFL));
-                    }
-                  else
-                    {
-                      if (!rightInB)
-                        {
-                          pushedB = pushB ();
-                          rightInB++;
-                          emitcode ("mov", "b,%s", aopGet (right, offset, FALSE, FALSE));
-                        }
-                      emitcode ("xrl", "b,#0x80");
-                      emitcode ("subb", "a,b");
+                      pushedB = pushB ();
+                      rightInB++;
+                      MOVB (aopGet (right, offset, FALSE, FALSE));
                     }
+                  emitcode ("xrl", "b,#0x80");
+                  emitcode ("subb", "a,b");
                 }
               else
                 {
@@ -5772,9 +5963,13 @@ release:
          ifx conditional branch then generate
          code a little differently */
       if (ifx)
-        genIfxJump (ifx, "c", NULL, NULL, result);
+        {
+          genIfxJump (ifx, "c", NULL, NULL, result);
+        }
       else
-        outBitC (result);
+        {
+          outBitC (result);
+        }
       /* leave the result in acc */
     }
 }
@@ -5789,7 +5984,7 @@ genCmpGt (iCode * ic, iCode * ifx)
   sym_link *letype, *retype;
   int sign;
 
-  D(emitcode (";     genCmpGt",""));
+  D (emitcode (";", "genCmpGt"));
 
   left = IC_LEFT (ic);
   right = IC_RIGHT (ic);
@@ -5799,7 +5994,7 @@ genCmpGt (iCode * ic, iCode * ifx)
   retype = getSpec (operandType (right));
   sign = !((SPEC_USIGN (letype) && !(IS_CHAR (letype) && IS_LITERAL (letype))) ||
            (SPEC_USIGN (retype) && !(IS_CHAR (retype) && IS_LITERAL (retype))));
-  /* assign the amsops */
+  /* assign the asmops */
   aopOp (result, ic, TRUE);
   aopOp (left, ic, FALSE);
   aopOp (right, ic, FALSE);
@@ -5819,7 +6014,7 @@ genCmpLt (iCode * ic, iCode * ifx)
   sym_link *letype, *retype;
   int sign;
 
-  D(emitcode (";     genCmpLt",""));
+  D (emitcode (";", "genCmpLt"));
 
   left = IC_LEFT (ic);
   right = IC_RIGHT (ic);
@@ -5829,7 +6024,7 @@ genCmpLt (iCode * ic, iCode * ifx)
   retype = getSpec (operandType (right));
   sign = !((SPEC_USIGN (letype) && !(IS_CHAR (letype) && IS_LITERAL (letype))) ||
            (SPEC_USIGN (retype) && !(IS_CHAR (retype) && IS_LITERAL (retype))));
-  /* assign the amsops */
+  /* assign the asmops */
   aopOp (result, ic, TRUE);
   aopOp (left, ic, FALSE);
   aopOp (right, ic, FALSE);
@@ -5849,11 +6044,14 @@ gencjneshort (operand * left, operand * right, symbol * lbl)
   int offset = 0;
   unsigned long lit = 0L;
 
+  D (emitcode (";", "gencjneshort"));
+
   /* if the left side is a literal or
      if the right is in a pointer register and left
      is not */
-  if ((AOP_TYPE (left) == AOP_LIT) ||
+  if ((AOP_TYPE (left) == AOP_LIT)  ||
       (AOP_TYPE (left) == AOP_IMMD) ||
+      (AOP_TYPE (left) == AOP_DIR)  ||
       (IS_AOP_PREG (right) && !IS_AOP_PREG (left)))
     {
       operand *t = right;
@@ -5906,12 +6104,9 @@ gencjneshort (operand * left, operand * right, symbol * lbl)
       /* right is a pointer reg need both a & b */
       while (size--)
         {
-          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 (left, offset, FALSE, FALSE);
-          if (strcmp (l, "b"))
-            emitcode ("mov", "b,%s", l);
+          MOVB (aopGet (left, offset, FALSE, FALSE));
           MOVA (aopGet (right, offset, FALSE, FALSE));
           emitcode ("cjne", "a,b,%05d$", lbl->key + 100);
           offset++;
@@ -5927,13 +6122,15 @@ gencjne (operand * left, operand * right, symbol * lbl)
 {
   symbol *tlbl = newiTempLabel (NULL);
 
+  D (emitcode (";", "gencjne"));
+
   gencjneshort (left, right, lbl);
 
   emitcode ("mov", "a,%s", one);
   emitcode ("sjmp", "%05d$", tlbl->key + 100);
-  emitcode ("", "%05d$:", lbl->key + 100);
+  emitLabel (lbl);
   emitcode ("clr", "a");
-  emitcode ("", "%05d$:", tlbl->key + 100);
+  emitLabel (tlbl);
 }
 
 /*-----------------------------------------------------------------*/
@@ -5945,7 +6142,7 @@ genCmpEq (iCode * ic, iCode * ifx)
   bool swappedLR = FALSE;
   operand *left, *right, *result;
 
-  D(emitcode (";     genCmpEq",""));
+  D (emitcode (";", "genCmpEq"));
 
   aopOp ((left = IC_LEFT (ic)), ic, FALSE);
   aopOp ((right = IC_RIGHT (ic)), ic, FALSE);
@@ -5994,7 +6191,7 @@ genCmpEq (iCode * ic, iCode * ifx)
               emitcode ("mov", "c,%s", AOP (left)->aopu.aop_dir);
               emitcode ("jb", "%s,%05d$", AOP (right)->aopu.aop_dir, (lbl->key + 100));
               emitcode ("cpl", "c");
-              emitcode ("", "%05d$:", (lbl->key + 100));
+              emitLabel (lbl);
             }
           /* if true label then we jump if condition
              supplied is true */
@@ -6015,7 +6212,7 @@ genCmpEq (iCode * ic, iCode * ifx)
               freeForBranchAsmop (left);
               emitcode ("ljmp", "%05d$", IC_FALSE (ifx)->key + 100);
             }
-          emitcode ("", "%05d$:", tlbl->key + 100);
+          emitLabel (tlbl);
         }
       else
         {
@@ -6027,18 +6224,18 @@ genCmpEq (iCode * ic, iCode * ifx)
               freeForBranchAsmop (right);
               freeForBranchAsmop (left);
               emitcode ("ljmp", "%05d$", IC_TRUE (ifx)->key + 100);
-              emitcode ("", "%05d$:", tlbl->key + 100);
+              emitLabel (tlbl);
             }
           else
             {
               symbol *lbl = newiTempLabel (NULL);
               emitcode ("sjmp", "%05d$", lbl->key + 100);
-              emitcode ("", "%05d$:", tlbl->key + 100);
+              emitLabel (tlbl);
               freeForBranchAsmop (result);
               freeForBranchAsmop (right);
               freeForBranchAsmop (left);
               emitcode ("ljmp", "%05d$", IC_FALSE (ifx)->key + 100);
-              emitcode ("", "%05d$:", lbl->key + 100);
+              emitLabel (lbl);
             }
         }
       /* mark the icode as generated */
@@ -6074,7 +6271,7 @@ genCmpEq (iCode * ic, iCode * ifx)
           emitcode ("mov", "c,%s", AOP (left)->aopu.aop_dir);
           emitcode ("jb", "%s,%05d$", AOP (right)->aopu.aop_dir, (lbl->key + 100));
           emitcode ("cpl", "c");
-          emitcode ("", "%05d$:", (lbl->key + 100));
+          emitLabel (lbl);
         }
       /* c = 1 if egal */
       if (AOP_TYPE (result) == AOP_CRY && AOP_SIZE (result))
@@ -6151,7 +6348,7 @@ ifxForOp (operand * op, iCode * ic)
 /* hasInc - operand is incremented before any other use            */
 /*-----------------------------------------------------------------*/
 static iCode *
-hasInc (operand *op, iCode *ic,int osize)
+hasInc (operand *op, iCode *ic, int osize)
 {
   sym_link *type = operandType(op);
   sym_link *retype = getSpec (type);
@@ -6193,7 +6390,7 @@ genAndOp (iCode * ic)
   operand *left, *right, *result;
   symbol *tlbl;
 
-  D(emitcode (";     genAndOp",""));
+  D (emitcode (";", "genAndOp"));
 
   /* note here that && operations that are in an
      if statement are taken away by backPatchLabels
@@ -6216,7 +6413,7 @@ genAndOp (iCode * ic)
       toBoolean (left);
       emitcode ("jz", "%05d$", tlbl->key + 100);
       toBoolean (right);
-      emitcode ("", "%05d$:", tlbl->key + 100);
+      emitLabel (tlbl);
       outBitAcc (result);
     }
 
@@ -6235,7 +6432,7 @@ genOrOp (iCode * ic)
   operand *left, *right, *result;
   symbol *tlbl;
 
-  D(emitcode (";     genOrOp",""));
+  D (emitcode (";", "genOrOp"));
 
   /* note here that || operations that are in an
      if statement are taken away by backPatchLabels
@@ -6258,7 +6455,7 @@ genOrOp (iCode * ic)
       toBoolean (left);
       emitcode ("jnz", "%05d$", tlbl->key + 100);
       toBoolean (right);
-      emitcode ("", "%05d$:", tlbl->key + 100);
+      emitLabel (tlbl);
       outBitAcc (result);
     }
 
@@ -6322,12 +6519,12 @@ jmpTrueOrFalse (iCode * ic, symbol * tlbl, operand *left, operand *right, operan
     {
       symbol *nlbl = newiTempLabel (NULL);
       emitcode ("sjmp", "%05d$", nlbl->key + 100);
-      emitcode ("", "%05d$:", tlbl->key + 100);
+      emitLabel (tlbl);
       freeForBranchAsmop (result);
       freeForBranchAsmop (right);
       freeForBranchAsmop (left);
       emitcode ("ljmp", "%05d$", IC_TRUE (ic)->key + 100);
-      emitcode ("", "%05d$:", nlbl->key + 100);
+      emitLabel (nlbl);
     }
   else
     {
@@ -6335,7 +6532,7 @@ jmpTrueOrFalse (iCode * ic, symbol * tlbl, operand *left, operand *right, operan
       freeForBranchAsmop (right);
       freeForBranchAsmop (left);
       emitcode ("ljmp", "%05d$", IC_FALSE (ic)->key + 100);
-      emitcode ("", "%05d$:", tlbl->key + 100);
+      emitLabel (tlbl);
     }
   ic->generated = 1;
 }
@@ -6352,17 +6549,17 @@ genAnd (iCode * ic, iCode * ifx)
   int bytelit = 0;
   char buffer[10];
 
-  D(emitcode (";     genAnd",""));
+  D (emitcode (";", "genAnd"));
 
   aopOp ((left = IC_LEFT (ic)), ic, FALSE);
   aopOp ((right = IC_RIGHT (ic)), ic, FALSE);
   aopOp ((result = IC_RESULT (ic)), ic, TRUE);
 
 #ifdef DEBUG_TYPE
-  emitcode ("", "; Type res[%d] = l[%d]&r[%d]",
+  emitcode (";", "Type res[%d] = l[%d]&r[%d]",
             AOP_TYPE (result),
             AOP_TYPE (left), AOP_TYPE (right));
-  emitcode ("", "; Size res[%d] = l[%d]&r[%d]",
+  emitcode (";", "Size res[%d] = l[%d]&r[%d]",
             AOP_SIZE (result),
             AOP_SIZE (left), AOP_SIZE (right));
 #endif
@@ -6490,7 +6687,7 @@ genAnd (iCode * ic, iCode * ifx)
                 }
               else
                 {// what is this case? just found it in ds390/gen.c
-                   emitcode ("anl","a,#!constbyte",1 << (posbit & 0x07));
+                  emitcode ("anl","a,#!constbyte",1 << (posbit & 0x07));
                 }
               goto release;
             }
@@ -6523,7 +6720,7 @@ genAnd (iCode * ic, iCode * ifx)
           if (size)
             {
               emitcode ("clr", "c");
-              emitcode ("", "%05d$:", tlbl->key + 100);
+              emitLabel (tlbl);
             }
           // if(left & literal)
           else
@@ -6531,7 +6728,7 @@ genAnd (iCode * ic, iCode * ifx)
               if (ifx)
                 jmpTrueOrFalse (ifx, tlbl, left, right, result);
               else
-                emitcode ("", "%05d$:", tlbl->key + 100);
+                emitLabel (tlbl);
               goto release;
             }
         }
@@ -6580,7 +6777,7 @@ genAnd (iCode * ic, iCode * ifx)
                 }
               else if (aopGetUsesAcc (left, offset) && aopGetUsesAcc (right, offset))
                 {
-                  emitcode ("mov", "b,%s", aopGet (left, offset, FALSE, FALSE));
+                  MOVB (aopGet (left, offset, FALSE, FALSE));
                   MOVA (aopGet (right, offset, FALSE, FALSE));
                   emitcode ("anl", "a,b");
                   aopPut (result, "a", offset);
@@ -6600,8 +6797,7 @@ genAnd (iCode * ic, iCode * ifx)
                       aopPut (result, "a", offset);
                     }
                   else
-                    emitcode ("anl", "%s,a",
-                              aopGet (left, offset, FALSE, TRUE));
+                    emitcode ("anl", "%s,a", aopGet (left, offset, FALSE, TRUE));
                 }
             }
         }
@@ -6625,43 +6821,54 @@ genAnd (iCode * ic, iCode * ifx)
                 {
                   if (offset)
                     emitcode("mov", "a,b");
-                  emitcode ("anl", "a,%s",
-                            aopGet (right, offset, FALSE, FALSE));
-                } else {
-                  if (AOP_TYPE(left)==AOP_ACC)
+                  emitcode ("anl", "a,%s", aopGet (right, offset, FALSE, FALSE));
+                }
+              else if (AOP_TYPE(left)==AOP_ACC)
+                {
+                  if (!offset)
+                    {
+                      bool pushedB = pushB ();
+                      emitcode("mov", "b,a");
+                      MOVA (aopGet (right, offset, FALSE, FALSE));
+                      emitcode("anl", "a,b");
+                      popB (pushedB);
+                    }
+                  else
                     {
-                      if (!offset)
-                        {
-                          bool pushedB = pushB ();
-                          emitcode("mov", "b,a");
-                          MOVA (aopGet (right, offset, FALSE, FALSE));
-                          emitcode("anl", "a,b");
-                          popB (pushedB);
-                        }
-                      else
-                        {
-                          MOVA (aopGet (right, offset, FALSE, FALSE));
-                          emitcode("anl", "a,b");
-                        }
-                    } else {
                       MOVA (aopGet (right, offset, FALSE, FALSE));
-                      emitcode ("anl", "a,%s",
-                                aopGet (left, offset, FALSE, FALSE));
+                      emitcode("anl", "a,b");
+                    }
+                }
+              else if (aopGetUsesAcc (left, offset) && aopGetUsesAcc (right, offset))
+                {
+                  MOVB (aopGet (left, offset, FALSE, FALSE));
+                  MOVA (aopGet (right, offset, FALSE, FALSE));
+                  emitcode ("anl", "a,b");
+                }
+              else if (aopGetUsesAcc (left, offset))
+                {
+                  MOVA (aopGet (left, offset, FALSE, FALSE));
+                  emitcode ("anl", "a,%s", aopGet (right, offset, FALSE, FALSE));
                     }
+              else
+                {
+                  MOVA (aopGet (right, offset, FALSE, FALSE));
+                  emitcode ("anl", "a,%s", aopGet (left, offset, FALSE, FALSE));
                 }
+
               emitcode ("jnz", "%05d$", tlbl->key + 100);
               offset++;
             }
           if (size)
             {
               CLRC;
-              emitcode ("", "%05d$:", tlbl->key + 100);
+              emitLabel (tlbl);
               outBitC (result);
             }
           else if (ifx)
             jmpTrueOrFalse (ifx, tlbl, left, right, result);
           else
-            emitcode ("", "%05d$:", tlbl->key + 100);
+            emitLabel (tlbl);
         }
       else
         {
@@ -6705,15 +6912,32 @@ genAnd (iCode * ic, iCode * ifx)
                 }
               // faster than result <- left, anl result,right
               // and better if result is SFR
-              if (AOP_TYPE (left) == AOP_ACC)
+              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 (right, offset, FALSE, FALSE));
                 }
+              else if (AOP_TYPE(left)==AOP_ACC)
+                {
+                  if (!offset)
+                    {
+                      bool pushedB = pushB ();
+                      emitcode("mov", "b,a");
+                      MOVA (aopGet (right, offset, FALSE, FALSE));
+                      emitcode("anl", "a,b");
+                      popB (pushedB);
+                    }
+                  else
+                    {
+                      MOVA (aopGet (right, offset, FALSE, FALSE));
+                      emitcode("anl", "a,b");
+                    }
+                }
               else if (aopGetUsesAcc (left, offset) && aopGetUsesAcc (right, offset))
                 {
-                  emitcode ("mov", "b,%s", aopGet (left, offset, FALSE, FALSE));
+                  MOVB (aopGet (left, offset, FALSE, FALSE));
                   MOVA (aopGet (right, offset, FALSE, FALSE));
                   emitcode ("anl", "a,b");
                 }
@@ -6749,17 +6973,17 @@ genOr (iCode * ic, iCode * ifx)
   unsigned long lit = 0L;
   int bytelit = 0;
 
-  D(emitcode (";     genOr",""));
+  D (emitcode (";", "genOr"));
 
   aopOp ((left = IC_LEFT (ic)), ic, FALSE);
   aopOp ((right = IC_RIGHT (ic)), ic, FALSE);
   aopOp ((result = IC_RESULT (ic)), ic, TRUE);
 
 #ifdef DEBUG_TYPE
-  emitcode ("", "; Type res[%d] = l[%d]&r[%d]",
+  emitcode (";", "Type res[%d] = l[%d]&r[%d]",
             AOP_TYPE (result),
             AOP_TYPE (left), AOP_TYPE (right));
-  emitcode ("", "; Size res[%d] = l[%d]&r[%d]",
+  emitcode (";", "Size res[%d] = l[%d]&r[%d]",
             AOP_SIZE (result),
             AOP_SIZE (left), AOP_SIZE (right));
 #endif
@@ -6848,7 +7072,7 @@ genOr (iCode * ic, iCode * ifx)
               else
                 {
                   CLRC;
-                  emitcode ("", "%05d$:", tlbl->key + 100);
+                  emitLabel (tlbl);
                 }
             }
         }
@@ -6888,7 +7112,7 @@ genOr (iCode * ic, iCode * ifx)
               symbol *tlbl = newiTempLabel (NULL);
               emitcode ("jnz", "%05d$", tlbl->key + 100);
               CLRC;
-              emitcode ("", "%05d$:", tlbl->key + 100);
+              emitLabel (tlbl);
             }
           else
             {
@@ -6943,7 +7167,7 @@ genOr (iCode * ic, iCode * ifx)
                 }
               else if (aopGetUsesAcc (left, offset) && aopGetUsesAcc (right, offset))
                 {
-                  emitcode ("mov", "b,%s", aopGet (left, offset, FALSE, FALSE));
+                  MOVB (aopGet (left, offset, FALSE, FALSE));
                   MOVA (aopGet (right, offset, FALSE, FALSE));
                   emitcode ("orl", "a,b");
                   aopPut (result, "a", offset);
@@ -6964,8 +7188,7 @@ genOr (iCode * ic, iCode * ifx)
                     }
                   else
                     {
-                      emitcode ("orl", "%s,a",
-                                aopGet (left, offset, FALSE, TRUE));
+                      emitcode ("orl", "%s,a", aopGet (left, offset, FALSE, TRUE));
                     }
                 }
             }
@@ -6985,29 +7208,59 @@ genOr (iCode * ic, iCode * ifx)
             emitcode ("setb", "c");
           while (sizer--)
             {
-              if (AOP_TYPE(right)==AOP_REG && AOP_TYPE(left)==AOP_ACC) {
-                if (offset)
-                  emitcode("mov", "a,b");
-                emitcode ("orl", "a,%s",
-                          aopGet (right, offset, FALSE, FALSE));
-              } else {
-                MOVA (aopGet (right, offset, FALSE, FALSE));
-                emitcode ("orl", "a,%s",
-                          aopGet (left, offset, FALSE, FALSE));
+              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 ("orl", "a,%s", aopGet (right, offset, FALSE, FALSE));
+                }
+              else if (AOP_TYPE(left)==AOP_ACC)
+                {
+                  if (!offset)
+                    {
+                      bool pushedB = pushB ();
+                      emitcode("mov", "b,a");
+                      MOVA (aopGet (right, offset, FALSE, FALSE));
+                      emitcode("orl", "a,b");
+                      popB (pushedB);
+                    }
+                  else
+                    {
+                      MOVA (aopGet (right, offset, FALSE, FALSE));
+                      emitcode("orl", "a,b");
+                    }
+                }
+              else if (aopGetUsesAcc (left, offset) && aopGetUsesAcc (right, offset))
+                {
+                  MOVB (aopGet (left, offset, FALSE, FALSE));
+                  MOVA (aopGet (right, offset, FALSE, FALSE));
+                  emitcode ("orl", "a,b");
+                }
+              else if (aopGetUsesAcc (left, offset))
+                {
+                  MOVA (aopGet (left, offset, FALSE, FALSE));
+                  emitcode ("orl", "a,%s", aopGet (right, offset, FALSE, FALSE));
+                }
+              else
+                {
+                  MOVA (aopGet (right, offset, FALSE, FALSE));
+                  emitcode ("orl", "a,%s", aopGet (left, offset, FALSE, FALSE));
               }
+
               emitcode ("jnz", "%05d$", tlbl->key + 100);
               offset++;
             }
           if (size)
             {
               CLRC;
-              emitcode ("", "%05d$:", tlbl->key + 100);
+              emitLabel (tlbl);
               outBitC (result);
             }
           else if (ifx)
             jmpTrueOrFalse (ifx, tlbl, left, right, result);
           else
-            emitcode ("", "%05d$:", tlbl->key + 100);
+            emitLabel (tlbl);
         }
       else
         {
@@ -7034,17 +7287,34 @@ genOr (iCode * ic, iCode * ifx)
                       continue;
                     }
                 }
-              // faster than result <- left, anl result,right
+              // faster than result <- left, orl result,right
               // and better if result is SFR
-              if (AOP_TYPE (left) == AOP_ACC)
+              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 ("orl", "a,%s", aopGet (right, offset, FALSE, FALSE));
                 }
+              else if (AOP_TYPE(left)==AOP_ACC)
+                {
+                  if (!offset)
+                    {
+                      bool pushedB = pushB ();
+                      emitcode("mov", "b,a");
+                      MOVA (aopGet (right, offset, FALSE, FALSE));
+                      emitcode("orl", "a,b");
+                      popB (pushedB);
+                    }
+                  else
+                    {
+                      MOVA (aopGet (right, offset, FALSE, FALSE));
+                      emitcode("orl", "a,b");
+                    }
+                }
               else if (aopGetUsesAcc (left, offset) && aopGetUsesAcc (right, offset))
                 {
-                  emitcode ("mov", "b,%s", aopGet (left, offset, FALSE, FALSE));
+                  MOVB (aopGet (left, offset, FALSE, FALSE));
                   MOVA (aopGet (right, offset, FALSE, FALSE));
                   emitcode ("orl", "a,b");
                 }
@@ -7080,17 +7350,17 @@ genXor (iCode * ic, iCode * ifx)
   unsigned long lit = 0L;
   int bytelit = 0;
 
-  D(emitcode (";     genXor",""));
+  D (emitcode (";", "genXor"));
 
   aopOp ((left = IC_LEFT (ic)), ic, FALSE);
   aopOp ((right = IC_RIGHT (ic)), ic, FALSE);
   aopOp ((result = IC_RESULT (ic)), ic, TRUE);
 
 #ifdef DEBUG_TYPE
-  emitcode ("", "; Type res[%d] = l[%d]&r[%d]",
+  emitcode (";", "Type res[%d] = l[%d]&r[%d]",
             AOP_TYPE (result),
             AOP_TYPE (left), AOP_TYPE (right));
-  emitcode ("", "; Size res[%d] = l[%d]&r[%d]",
+  emitcode (";", "Size res[%d] = l[%d]&r[%d]",
             AOP_SIZE (result),
             AOP_SIZE (left), AOP_SIZE (right));
 #endif
@@ -7121,6 +7391,7 @@ genXor (iCode * ic, iCode * ifx)
       right = left;
       left = tmp;
     }
+
   if (AOP_TYPE (right) == AOP_LIT)
     lit = (unsigned long) floatFromVal (AOP (right)->aopu.aop_lit);
 
@@ -7171,7 +7442,6 @@ genXor (iCode * ic, iCode * ifx)
                     }
                 }
             }
-
         }
       else
         {
@@ -7202,13 +7472,13 @@ genXor (iCode * ic, iCode * ifx)
             }
           emitcode ("jnb", "%s,%05d$", AOP (left)->aopu.aop_dir, (tlbl->key + 100));
           emitcode ("cpl", "c");
-          emitcode ("", "%05d$:", (tlbl->key + 100));
+          emitLabel (tlbl);
         }
       // bit = c
       // val = c
       if (size)
         outBitC (result);
-      // if(bit | ...)
+      // if(bit ^ ...)
       else if ((AOP_TYPE (result) == AOP_CRY) && ifx)
         genIfxJump (ifx, "c", left, right, result);
       goto release;
@@ -7253,7 +7523,7 @@ genXor (iCode * ic, iCode * ifx)
                 }
               else if (aopGetUsesAcc (left, offset) && aopGetUsesAcc (right, offset))
                 {
-                  emitcode ("mov", "b,%s", aopGet (left, offset, FALSE, FALSE));
+                  MOVB (aopGet (left, offset, FALSE, FALSE));
                   MOVA (aopGet (right, offset, FALSE, FALSE));
                   emitcode ("xrl", "a,b");
                   aopPut (result, "a", offset);
@@ -7273,8 +7543,7 @@ genXor (iCode * ic, iCode * ifx)
                       aopPut (result, "a", offset);
                     }
                   else
-                    emitcode ("xrl", "%s,a",
-                              aopGet (left, offset, FALSE, TRUE));
+                    emitcode ("xrl", "%s,a", aopGet (left, offset, FALSE, TRUE));
                 }
             }
         }
@@ -7289,6 +7558,7 @@ genXor (iCode * ic, iCode * ifx)
           // if(!size && ifx), conditional oper: if(left ^ right)
           symbol *tlbl = newiTempLabel (NULL);
           int sizer = max (AOP_SIZE (left), AOP_SIZE (right));
+
           if (size)
             emitcode ("setb", "c");
           while (sizer--)
@@ -7298,26 +7568,53 @@ genXor (iCode * ic, iCode * ifx)
                 {
                   MOVA (aopGet (left, offset, FALSE, FALSE));
                 }
+              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 ("xrl", "a,%s", aopGet (right, offset, FALSE, FALSE));
+                }
+              else if (AOP_TYPE(left)==AOP_ACC)
+                {
+                  if (!offset)
+                    {
+                      bool pushedB = pushB ();
+                      emitcode("mov", "b,a");
+                      MOVA (aopGet (right, offset, FALSE, FALSE));
+                      emitcode("xrl", "a,b");
+                      popB (pushedB);
+                    }
+                  else
+                    {
+                      MOVA (aopGet (right, offset, FALSE, FALSE));
+                      emitcode("xrl", "a,b");
+                    }
+                }
+              else if (aopGetUsesAcc (left, offset) && aopGetUsesAcc (right, offset))
+                {
+                  MOVB (aopGet (left, offset, FALSE, FALSE));
+                  MOVA (aopGet (right, offset, FALSE, FALSE));
+                  emitcode ("xrl", "a,b");
+                }
+              else if (aopGetUsesAcc (left, offset))
+                {
+                  MOVA (aopGet (left, offset, FALSE, FALSE));
+                  emitcode ("xrl", "a,%s", aopGet (right, offset, FALSE, FALSE));
+                }
               else
                 {
-                  if (AOP_TYPE(right)==AOP_REG && AOP_TYPE(left)==AOP_ACC) {
-                    if (offset)
-                      emitcode("mov", "a,b");
-                    emitcode ("xrl", "a,%s",
-                              aopGet (right, offset, FALSE, FALSE));
-                  } else {
-                    MOVA (aopGet (right, offset, FALSE, FALSE));
-                    emitcode ("xrl", "a,%s",
-                              aopGet (left, offset, FALSE, FALSE));
-                  }
+                  MOVA (aopGet (right, offset, FALSE, FALSE));
+                  emitcode ("xrl", "a,%s", aopGet (left, offset, FALSE, TRUE));
                 }
+
               emitcode ("jnz", "%05d$", tlbl->key + 100);
               offset++;
             }
           if (size)
             {
               CLRC;
-              emitcode ("", "%05d$:", tlbl->key + 100);
+              emitLabel (tlbl);
               outBitC (result);
             }
           else if (ifx)
@@ -7328,7 +7625,7 @@ genXor (iCode * ic, iCode * ifx)
           for (; (size--); offset++)
             {
               // normal case
-              // result = left & right
+              // result = left ^ right
               if (AOP_TYPE (right) == AOP_LIT)
                 {
                   bytelit = (int) ((lit >> (offset * 8)) & 0x0FFL);
@@ -7340,17 +7637,34 @@ genXor (iCode * ic, iCode * ifx)
                       continue;
                     }
                 }
-              // faster than result <- left, anl result,right
+              // faster than result <- left, xrl result,right
               // and better if result is SFR
-              if (AOP_TYPE (left) == AOP_ACC)
+              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 ("xrl", "a,%s", aopGet (right, offset, FALSE, FALSE));
                 }
+              else if (AOP_TYPE(left)==AOP_ACC)
+                {
+                  if (!offset)
+                    {
+                      bool pushedB = pushB ();
+                      emitcode("mov", "b,a");
+                      MOVA (aopGet (right, offset, FALSE, FALSE));
+                      emitcode("xrl", "a,b");
+                      popB (pushedB);
+                    }
+                  else
+                    {
+                      MOVA (aopGet (right, offset, FALSE, FALSE));
+                      emitcode("xrl", "a,b");
+                    }
+                }
               else if (aopGetUsesAcc (left, offset) && aopGetUsesAcc (right, offset))
                 {
-                  emitcode ("mov", "b,%s", aopGet (left, offset, FALSE, FALSE));
+                  MOVB (aopGet (left, offset, FALSE, FALSE));
                   MOVA (aopGet (right, offset, FALSE, FALSE));
                   emitcode ("xrl", "a,b");
                 }
@@ -7426,10 +7740,10 @@ static void
 genRRC (iCode * ic)
 {
   operand *left, *result;
-  int size, offset = 0;
+  int size, offset;
   char *l;
 
-  D(emitcode (";     genRRC",""));
+  D (emitcode (";", "genRRC"));
 
   /* rotate right with carry */
   left = IC_LEFT (ic);
@@ -7476,10 +7790,10 @@ static void
 genRLC (iCode * ic)
 {
   operand *left, *result;
-  int size, offset = 0;
+  int size, offset;
   char *l;
 
-  D(emitcode (";     genRLC",""));
+  D (emitcode (";", "genRLC"));
 
   /* rotate right with carry */
   left = IC_LEFT (ic);
@@ -7500,7 +7814,10 @@ genRLC (iCode * ic)
       }
       emitcode("rlc","a"); /* bit0 will be written later */
       if (AOP_SIZE (result) > 1)
-        aopPut (result, "a", offset++);
+        {
+          aopPut (result, "a", offset++);
+        }
+
       while (size--)
         {
           l = aopGet (left, offset, FALSE, FALSE);
@@ -7532,7 +7849,7 @@ genGetHbit (iCode * ic)
 {
   operand *left, *result;
 
-  D(emitcode (";     genGetHbit",""));
+  D (emitcode (";", "genGetHbit"));
 
   left = IC_LEFT (ic);
   result = IC_RESULT (ic);
@@ -7566,7 +7883,7 @@ genGetAbit (iCode * ic)
   operand *left, *right, *result;
   int shCount;
 
-  D(emitcode (";     genGetAbit",""));
+  D (emitcode (";", "genGetAbit"));
 
   left = IC_LEFT (ic);
   right = IC_RIGHT (ic);
@@ -7638,7 +7955,7 @@ genGetByte (iCode * ic)
   operand *left, *right, *result;
   int offset;
 
-  D(emitcode (";     genGetByte",""));
+  D (emitcode (";", "genGetByte"));
 
   left = IC_LEFT (ic);
   right = IC_RIGHT (ic);
@@ -7666,7 +7983,7 @@ genGetWord (iCode * ic)
   operand *left, *right, *result;
   int offset;
 
-  D(emitcode (";     genGetWord",""));
+  D (emitcode (";", "genGetWord"));
 
   left = IC_LEFT (ic);
   right = IC_RIGHT (ic);
@@ -7696,7 +8013,7 @@ genSwap (iCode * ic)
 {
   operand *left, *result;
 
-  D(emitcode (";     genSwap",""));
+  D(emitcode (";", "genSwap"));
 
   left = IC_LEFT (ic);
   result = IC_RESULT (ic);
@@ -7869,7 +8186,7 @@ AccSRsh (int shCount)
           emitcode ("jnb", "acc.%d,%05d$", 7 - shCount, tlbl->key + 100);
           emitcode ("orl", "a,#0x%02x",
                     (unsigned char) ~SRMask[shCount]);
-          emitcode ("", "%05d$:", tlbl->key + 100);
+          emitLabel (tlbl);
         }
     }
 }
@@ -7926,7 +8243,9 @@ movLeft2Result (operand * left, int offl,
       else
         {
           if (!sign)
-            aopPut (result, l, offr);
+            {
+              aopPut (result, l, offr);
+            }
           else
             {
               /* MSB sign in acc.7 ! */
@@ -8194,7 +8513,7 @@ AccAXRshS (char *x, int shCount)
       emitcode ("orl", "a,#0x%02x",
                 (unsigned char) ~SRMask[shCount]);      // 111AAAAA:BBBCCCCC
 
-      emitcode ("", "%05d$:", tlbl->key + 100);
+      emitLabel (tlbl);
       break;                    // SSSSAAAA:BBBCCCCC
 
     case 6:                     // AABBBBBB:CCDDDDDD
@@ -8215,7 +8534,7 @@ AccAXRshS (char *x, int shCount)
       emitcode ("orl", "a,#0x%02x",
                 (unsigned char) ~SRMask[shCount]);      // 111111AA:BBBBBBCC
 
-      emitcode ("", "%05d$:", tlbl->key + 100);
+      emitLabel (tlbl);
       break;
     case 7:                     // ABBBBBBB:CDDDDDDD
 
@@ -8233,7 +8552,7 @@ AccAXRshS (char *x, int shCount)
       emitcode ("orl", "a,#0x%02x",
                 (unsigned char) ~SRMask[shCount]);      // 1111111A:BBBBBBBC
 
-      emitcode ("", "%05d$:", tlbl->key + 100);
+      emitLabel (tlbl);
       break;
     default:
       break;
@@ -8256,8 +8575,8 @@ shiftL2Left2Result (operand * left, int offl,
     {
       /* don't crash result[offr] */
       MOVA (aopGet (left, offl, FALSE, FALSE));
-      emitcode ("xch", "a,%s", aopGet (left, offl + MSB16, FALSE, FALSE));
-      x = aopGet (result, offr, FALSE, FALSE);
+      x = xch_a_aopGet (left, offl + MSB16, FALSE, FALSE);
+      usedB = !strncmp(x, "b", 1);
     }
   else if (aopGetUsesAcc (result, offr))
     {
@@ -8308,8 +8627,8 @@ shiftR2Left2Result (operand * left, int offl,
     {
       /* don't crash result[offr] */
       MOVA (aopGet (left, offl, FALSE, FALSE));
-      emitcode ("xch", "a,%s", aopGet (left, offl + MSB16, FALSE, FALSE));
-      x = aopGet (result, offr, FALSE, FALSE);
+      x = xch_a_aopGet (left, offl + MSB16, FALSE, FALSE);
+      usedB = !strncmp(x, "b", 1);
     }
   else if (aopGetUsesAcc (result, offr))
     {
@@ -8378,7 +8697,16 @@ shiftRLeftOrResult (operand * left, int offl,
   /* shift right accumulator */
   AccRsh (shCount);
   /* or with result */
-  emitcode ("orl", "a,%s", aopGet (result, offr, FALSE, FALSE));
+  if (aopGetUsesAcc(result, offr))
+    {
+      emitcode ("xch", "a,b");
+      MOVA (aopGet (result, offr, FALSE, FALSE));
+      emitcode ("orl", "a,b");
+    }
+  else
+    {
+      emitcode ("orl", "a,%s", aopGet (result, offr, FALSE, FALSE));
+    }
   /* back to result */
   aopPut (result, "a", offr);
 }
@@ -8389,7 +8717,7 @@ shiftRLeftOrResult (operand * left, int offl,
 static void
 genlshOne (operand * result, operand * left, int shCount)
 {
-  D(emitcode (";     genlshOne",""));
+  D (emitcode (";", "genlshOne"));
 
   shiftL1Left2Result (left, LSB, result, LSB, shCount);
 }
@@ -8402,7 +8730,7 @@ genlshTwo (operand * result, operand * left, int shCount)
 {
   int size;
 
-  D(emitcode (";     genlshTwo",""));
+  D (emitcode (";", "genlshTwo"));
 
   size = getDataSize (result);
 
@@ -8448,8 +8776,7 @@ shiftLLong (operand * left, operand * result, int offr)
       emitcode ("add", "a,acc");
       if (sameRegs (AOP (left), AOP (result)) &&
           size >= MSB16 + offr && offr != LSB)
-        emitcode ("xch", "a,%s",
-                  aopGet (left, LSB + offr, FALSE, FALSE));
+        xch_a_aopGet (left, LSB + offr, FALSE, FALSE);
       else
         aopPut (result, "a", LSB + offr);
     }
@@ -8464,8 +8791,7 @@ shiftLLong (operand * left, operand * result, int offr)
       emitcode ("rlc", "a");
       if (sameRegs (AOP (left), AOP (result)) &&
           size >= MSB24 + offr && offr != LSB)
-        emitcode ("xch", "a,%s",
-                  aopGet (left, MSB16 + offr, FALSE, FALSE));
+        xch_a_aopGet (left, MSB16 + offr, FALSE, FALSE);
       else
         aopPut (result, "a", MSB16 + offr);
     }
@@ -8480,8 +8806,7 @@ shiftLLong (operand * left, operand * result, int offr)
       emitcode ("rlc", "a");
       if (sameRegs (AOP (left), AOP (result)) &&
           size >= MSB32 + offr && offr != LSB)
-        emitcode ("xch", "a,%s",
-                  aopGet (left, MSB24 + offr, FALSE, FALSE));
+        xch_a_aopGet (left, MSB24 + offr, FALSE, FALSE);
       else
         aopPut (result, "a", MSB24 + offr);
     }
@@ -8508,7 +8833,7 @@ genlshFour (operand * result, operand * left, int shCount)
 {
   int size;
 
-  D(emitcode (";     genlshFour",""));
+  D (emitcode (";", "genlshFour"));
 
   size = AOP_SIZE (result);
 
@@ -8607,7 +8932,7 @@ genLeftShiftLiteral (operand * left,
   int shCount = (int) floatFromVal (AOP (right)->aopu.aop_lit);
   int size;
 
-  D(emitcode (";     genLeftShiftLiteral",""));
+  D (emitcode (";", "genLeftShiftLiteral"));
 
   freeAsmop (right, NULL, ic, TRUE);
 
@@ -8629,10 +8954,13 @@ genLeftShiftLiteral (operand * left,
           movLeft2Result (left, size, result, size, 0);
         }
     }
-
   else if (shCount >= (size * 8))
-    while (size--)
-      aopPut (result, zero, size);
+    {
+      while (size--)
+        {
+          aopPut (result, zero, size);
+        }
+    }
   else
     {
       switch (size)
@@ -8670,7 +8998,7 @@ genLeftShift (iCode * ic)
   symbol *tlbl, *tlbl1;
   bool pushedB;
 
-  D(emitcode (";     genLeftShift",""));
+  D (emitcode (";", "genLeftShift"));
 
   right = IC_RIGHT (ic);
   left = IC_LEFT (ic);
@@ -8693,7 +9021,7 @@ genLeftShift (iCode * ic)
      largest size of an object can be only 32 bits ) */
 
   pushedB = pushB ();
-  emitcode ("mov", "b,%s", aopGet (right, 0, FALSE, FALSE));
+  MOVB (aopGet (right, 0, FALSE, FALSE));
   emitcode ("inc", "b");
   freeAsmop (right, NULL, ic, TRUE);
   aopOp (left, ic, FALSE);
@@ -8734,9 +9062,9 @@ genLeftShift (iCode * ic)
       l = aopGet (left, 0, FALSE, FALSE);
       MOVA (l);
       emitcode ("sjmp", "%05d$", tlbl1->key + 100);
-      emitcode ("", "%05d$:", tlbl->key + 100);
+      emitLabel (tlbl);
       emitcode ("add", "a,acc");
-      emitcode ("", "%05d$:", tlbl1->key + 100);
+      emitLabel (tlbl1);
       emitcode ("djnz", "b,%05d$", tlbl->key + 100);
       popB (pushedB);
       aopPut (result, "a", 0);
@@ -8746,7 +9074,7 @@ genLeftShift (iCode * ic)
   reAdjustPreg (AOP (result));
 
   emitcode ("sjmp", "%05d$", tlbl1->key + 100);
-  emitcode ("", "%05d$:", tlbl->key + 100);
+  emitLabel (tlbl);
   l = aopGet (result, offset, FALSE, FALSE);
   MOVA (l);
   emitcode ("add", "a,acc");
@@ -8760,7 +9088,7 @@ genLeftShift (iCode * ic)
     }
   reAdjustPreg (AOP (result));
 
-  emitcode ("", "%05d$:", tlbl1->key + 100);
+  emitLabel (tlbl1);
   emitcode ("djnz", "b,%05d$", tlbl->key + 100);
   popB (pushedB);
 release:
@@ -8775,7 +9103,7 @@ static void
 genrshOne (operand * result, operand * left,
            int shCount, int sign)
 {
-  D(emitcode (";     genrshOne",""));
+  D (emitcode (";", "genrshOne"));
 
   shiftR1Left2Result (left, LSB, result, LSB, shCount, sign);
 }
@@ -8787,15 +9115,14 @@ static void
 genrshTwo (operand * result, operand * left,
            int shCount, int sign)
 {
-  D(emitcode (";     genrshTwo",""));
+  D (emitcode (";", "genrshTwo"));
 
   /* if shCount >= 8 */
   if (shCount >= 8)
     {
       shCount -= 8;
       if (shCount)
-        shiftR1Left2Result (left, MSB16, result, LSB,
-                            shCount, sign);
+        shiftR1Left2Result (left, MSB16, result, LSB, shCount, sign);
       else
         movLeft2Result (left, MSB16, result, LSB, sign);
       addSign (result, MSB16, sign);
@@ -8814,9 +9141,9 @@ static void
 shiftRLong (operand * left, int offl,
             operand * result, int sign)
 {
-  bool useSameRegs = regsInCommon (left, result);
+  bool overlapping = regsInCommon (left, result) || operandsEqu(left, result);
 
-  if (useSameRegs && offl>1)
+  if (overlapping && offl>1)
     {
       // we are in big trouble, but this shouldn't happen
       werror(E_INTERNAL_ERROR, __FILE__, __LINE__);
@@ -8831,9 +9158,9 @@ shiftRLong (operand * left, int offl,
         {
           emitcode ("rlc", "a");
           emitcode ("subb", "a,acc");
-          if (useSameRegs && sameReg (AOP (left), MSB32, AOP (result), MSB32))
+          if (overlapping && sameByte (AOP (left), MSB32, AOP (result), MSB32))
             {
-              emitcode ("xch", "a,%s", aopGet (left, MSB32, FALSE, FALSE));
+              xch_a_aopGet (left, MSB32, FALSE, FALSE);
             }
           else
             {
@@ -8843,7 +9170,16 @@ shiftRLong (operand * left, int offl,
         }
       else
         {
-          aopPut (result, zero, MSB32);
+          if (aopPutUsesAcc (result, zero, MSB32))
+            {
+              emitcode("xch", "a,b");
+              aopPut (result, zero, MSB32);
+              emitcode("xch", "a,b");
+            }
+          else
+            {
+              aopPut (result, zero, MSB32);
+            }
         }
     }
 
@@ -8858,28 +9194,29 @@ shiftRLong (operand * left, int offl,
 
   emitcode ("rrc", "a");
 
-  if (useSameRegs && offl==MSB16 &&
-      sameReg (AOP (left), MSB24, AOP (result), MSB32-offl))
+  if (overlapping && offl==MSB16 &&
+      sameByte (AOP (left), MSB24, AOP (result), MSB32-offl))
     {
-      emitcode ("xch", "a,%s",aopGet (left, MSB24, FALSE, FALSE));
+      xch_a_aopGet (left, MSB24, FALSE, FALSE);
     }
   else
     {
-      aopPut (result, "a", MSB32-offl);
+      aopPut (result, "a", MSB32 - offl);
       MOVA (aopGet (left, MSB24, FALSE, FALSE));
     }
 
   emitcode ("rrc", "a");
-  if (useSameRegs && offl==1 &&
-      sameReg (AOP (left), MSB16, AOP (result), MSB24-offl))
+  if (overlapping && offl==MSB16 &&
+      sameByte (AOP (left), MSB16, AOP (result), MSB24-offl))
     {
-      emitcode ("xch", "a,%s",aopGet (left, MSB16, FALSE, FALSE));
+      xch_a_aopGet (left, MSB16, FALSE, FALSE);
     }
   else
     {
-      aopPut (result, "a", MSB24-offl);
+      aopPut (result, "a", MSB24 - offl);
       MOVA (aopGet (left, MSB16, FALSE, FALSE));
     }
+
   emitcode ("rrc", "a");
   if (offl != LSB)
     {
@@ -8887,10 +9224,10 @@ shiftRLong (operand * left, int offl,
     }
   else
     {
-      if (useSameRegs &&
-          sameReg (AOP (left), LSB, AOP (result), MSB16-offl))
+      if (overlapping &&
+          sameByte (AOP (left), LSB, AOP (result), MSB16-offl))
         {
-          emitcode ("xch", "a,%s",aopGet (left, LSB, FALSE, FALSE));
+          xch_a_aopGet (left, LSB, FALSE, FALSE);
         }
       else
         {
@@ -8909,7 +9246,7 @@ static void
 genrshFour (operand * result, operand * left,
             int shCount, int sign)
 {
-  D(emitcode (";     genrshFour",""));
+  D (emitcode (";", "genrshFour"));
 
   /* if shifting more that 3 bytes */
   if (shCount >= 24)
@@ -8937,7 +9274,9 @@ genrshFour (operand * result, operand * left,
     {
       shCount -= 8;
       if (shCount == 1)
-        shiftRLong (left, MSB16, result, sign);
+        {
+          shiftRLong (left, MSB16, result, sign);
+        }
       else if (shCount == 0)
         {
           movLeft2Result (left, MSB16, result, LSB, 0);
@@ -8955,7 +9294,8 @@ genrshFour (operand * result, operand * left,
         }
     }
   else
-    {                           /* 1 <= shCount <= 7 */
+    {
+      /* 1 <= shCount <= 7 */
       if (shCount <= 2)
         {
           shiftRLong (left, LSB, result, sign);
@@ -8984,7 +9324,7 @@ genRightShiftLiteral (operand * left,
   int shCount = (int) floatFromVal (AOP (right)->aopu.aop_lit);
   int size;
 
-  D(emitcode (";     genRightShiftLiteral",""));
+  D (emitcode (";", "genRightShiftLiteral"));
 
   freeAsmop (right, NULL, ic, TRUE);
 
@@ -9009,10 +9349,11 @@ genRightShiftLiteral (operand * left,
 
   else if (shCount >= (size * 8))
     {
-      if (sign) {
-        /* get sign in acc.7 */
-        MOVA (aopGet (left, size - 1, FALSE, FALSE));
-      }
+      if (sign)
+        {
+          /* get sign in acc.7 */
+          MOVA (aopGet (left, size - 1, FALSE, FALSE));
+        }
       addSign (result, LSB, sign);
     }
   else
@@ -9050,7 +9391,7 @@ genSignedRightShift (iCode * ic)
   symbol *tlbl, *tlbl1;
   bool pushedB;
 
-  D(emitcode (";     genSignedRightShift",""));
+  D (emitcode (";", "genSignedRightShift"));
 
   /* we do it the hard way put the shift count in b
      and loop thru preserving the sign */
@@ -9074,7 +9415,7 @@ genSignedRightShift (iCode * ic)
      largest size of an object can be only 32 bits ) */
 
   pushedB = pushB ();
-  emitcode ("mov", "b,%s", aopGet (right, 0, FALSE, FALSE));
+  MOVB (aopGet (right, 0, FALSE, FALSE));
   emitcode ("inc", "b");
   freeAsmop (right, NULL, ic, TRUE);
   aopOp (left, ic, FALSE);
@@ -9118,10 +9459,10 @@ genSignedRightShift (iCode * ic)
       l = aopGet (left, 0, FALSE, FALSE);
       MOVA (l);
       emitcode ("sjmp", "%05d$", tlbl1->key + 100);
-      emitcode ("", "%05d$:", tlbl->key + 100);
+      emitLabel (tlbl);
       emitcode ("mov", "c,ov");
       emitcode ("rrc", "a");
-      emitcode ("", "%05d$:", tlbl1->key + 100);
+      emitLabel (tlbl1);
       emitcode ("djnz", "b,%05d$", tlbl->key + 100);
       popB (pushedB);
       aopPut (result, "a", 0);
@@ -9130,7 +9471,7 @@ genSignedRightShift (iCode * ic)
 
   reAdjustPreg (AOP (result));
   emitcode ("sjmp", "%05d$", tlbl1->key + 100);
-  emitcode ("", "%05d$:", tlbl->key + 100);
+  emitLabel (tlbl);
   emitcode ("mov", "c,ov");
   while (size--)
     {
@@ -9140,7 +9481,7 @@ genSignedRightShift (iCode * ic)
       aopPut (result, "a", offset--);
     }
   reAdjustPreg (AOP (result));
-  emitcode ("", "%05d$:", tlbl1->key + 100);
+  emitLabel (tlbl1);
   emitcode ("djnz", "b,%05d$", tlbl->key + 100);
   popB (pushedB);
 
@@ -9162,7 +9503,7 @@ genRightShift (iCode * ic)
   symbol *tlbl, *tlbl1;
   bool pushedB;
 
-  D(emitcode (";     genRightShift",""));
+  D (emitcode (";", "genRightShift"));
 
   /* if signed then we do it the hard way preserve the
      sign bit moving it inwards */
@@ -9202,7 +9543,7 @@ genRightShift (iCode * ic)
      largest size of an object can be only 32 bits ) */
 
   pushedB = pushB ();
-  emitcode ("mov", "b,%s", aopGet (right, 0, FALSE, FALSE));
+  MOVB (aopGet (right, 0, FALSE, FALSE));
   emitcode ("inc", "b");
   freeAsmop (right, NULL, ic, TRUE);
   aopOp (left, ic, FALSE);
@@ -9213,7 +9554,6 @@ genRightShift (iCode * ic)
   if (!sameRegs (AOP (left), AOP (result)) &&
       AOP_SIZE (result) > 1)
     {
-
       size = AOP_SIZE (result);
       offset = 0;
       while (size--)
@@ -9242,10 +9582,10 @@ genRightShift (iCode * ic)
       l = aopGet (left, 0, FALSE, FALSE);
       MOVA (l);
       emitcode ("sjmp", "%05d$", tlbl1->key + 100);
-      emitcode ("", "%05d$:", tlbl->key + 100);
+      emitLabel (tlbl);
       CLRC;
       emitcode ("rrc", "a");
-      emitcode ("", "%05d$:", tlbl1->key + 100);
+      emitLabel (tlbl1);
       emitcode ("djnz", "b,%05d$", tlbl->key + 100);
       popB (pushedB);
       aopPut (result, "a", 0);
@@ -9254,7 +9594,7 @@ genRightShift (iCode * ic)
 
   reAdjustPreg (AOP (result));
   emitcode ("sjmp", "%05d$", tlbl1->key + 100);
-  emitcode ("", "%05d$:", tlbl->key + 100);
+  emitLabel (tlbl);
   CLRC;
   while (size--)
     {
@@ -9265,7 +9605,7 @@ genRightShift (iCode * ic)
     }
   reAdjustPreg (AOP (result));
 
-  emitcode ("", "%05d$:", tlbl1->key + 100);
+  emitLabel (tlbl1);
   emitcode ("djnz", "b,%05d$", tlbl->key + 100);
   popB (pushedB);
 
@@ -9374,7 +9714,7 @@ genUnpackBits (operand * result, char *rname, int ptype, iCode *ifx)
   int bstr;             /* bitfield starting bit within byte */
   char buffer[10];
 
-  D(emitcode (";     genUnpackBits",""));
+  D(emitcode (";", "genUnpackBits"));
 
   etype = getSpec (operandType (result));
   rsize = getSize (operandType (result));
@@ -9414,7 +9754,7 @@ genUnpackBits (operand * result, char *rname, int ptype, iCode *ifx)
 
           emitcode ("jnb", "acc.%d,%05d$", blen - 1, tlbl->key + 100);
           emitcode ("orl", "a,#0x%02x", (unsigned char) (0xff << blen));
-          emitcode ("", "%05d$:", tlbl->key + 100);
+          emitLabel (tlbl);
         }
       aopPut (result, "a", offset++);
       goto finish;
@@ -9442,7 +9782,7 @@ genUnpackBits (operand * result, char *rname, int ptype, iCode *ifx)
 
           emitcode ("jnb", "acc.%d,%05d$", rlen - 1, tlbl->key + 100);
           emitcode ("orl", "a,#0x%02x", (unsigned char) (0xff << rlen));
-          emitcode ("", "%05d$:", tlbl->key + 100);
+          emitLabel (tlbl);
         }
       aopPut (result, "a", offset++);
     }
@@ -9481,19 +9821,24 @@ genDataPointerGet (operand * left,
   char buffer[256];
   int size, offset = 0;
 
-  D(emitcode (";     genDataPointerGet",""));
+  D (emitcode (";", "genDataPointerGet"));
 
   aopOp (result, ic, TRUE);
 
   /* get the string representation of the name */
   l = aopGet (left, 0, FALSE, TRUE);
+  l++; // remove #
   size = AOP_SIZE (result);
   while (size--)
     {
       if (offset)
-        sprintf (buffer, "(%s + %d)", l + 1, offset);
+        {
+          SNPRINTF (buffer, sizeof(buffer), "(%s + %d)", l, offset);
+        }
       else
-        sprintf (buffer, "%s", l + 1);
+        {
+          SNPRINTF (buffer, sizeof(buffer), "%s", l);
+        }
       aopPut (result, buffer, offset++);
     }
 
@@ -9518,7 +9863,7 @@ genNearPointerGet (operand * left,
   sym_link *ltype = operandType (left);
   char buffer[80];
 
-  D(emitcode (";     genNearPointerGet",""));
+  D (emitcode (";", "genNearPointerGet"));
 
   rtype = operandType (result);
   retype = getSpec (rtype);
@@ -9594,7 +9939,7 @@ genNearPointerGet (operand * left,
             }
           else
             {
-              sprintf (buffer, "@%s", rname);
+              SNPRINTF (buffer, sizeof(buffer), "@%s", rname);
               aopPut (result, buffer, offset);
             }
           offset++;
@@ -9656,7 +10001,7 @@ genPagedPointerGet (operand * left,
   char *rname;
   sym_link *rtype, *retype;
 
-  D(emitcode (";     genPagedPointerGet",""));
+  D (emitcode (";", "genPagedPointerGet"));
 
   rtype = operandType (result);
   retype = getSpec (rtype);
@@ -9759,7 +10104,7 @@ loadDptrFromOperand (operand *op, bool loadBToo)
               else
                 {
                   wassertl(FALSE, "need pointerCode");
-                  emitcode ("", "; mov b,???");
+                  emitcode (";", "mov b,???");
                   /* genPointerGet and genPointerSet originally did different
                   ** things for this case. Both seem wrong.
                   ** from genPointerGet:
@@ -9801,7 +10146,7 @@ loadDptrFromOperand (operand *op, bool loadBToo)
 }
 
 /*-----------------------------------------------------------------*/
-/* genFarPointerGet - gget value from far space                    */
+/* genFarPointerGet - get value from far space                     */
 /*-----------------------------------------------------------------*/
 static void
 genFarPointerGet (operand * left,
@@ -9810,7 +10155,7 @@ genFarPointerGet (operand * left,
   int size, offset;
   sym_link *retype = getSpec (operandType (result));
 
-  D(emitcode (";     genFarPointerGet",""));
+  D (emitcode (";", "genFarPointerGet"));
 
   aopOp (left, ic, FALSE);
   loadDptrFromOperand (left, FALSE);
@@ -9862,7 +10207,7 @@ genCodePointerGet (operand * left,
   int size, offset;
   sym_link *retype = getSpec (operandType (result));
 
-  D(emitcode (";     genCodePointerGet",""));
+  D (emitcode (";", "genCodePointerGet"));
 
   aopOp (left, ic, FALSE);
   loadDptrFromOperand (left, FALSE);
@@ -9915,17 +10260,19 @@ genGenPointerGet (operand * left,
   int size, offset;
   sym_link *retype = getSpec (operandType (result));
 
-  D(emitcode (";     genGenPointerGet",""));
+  D (emitcode (";", "genGenPointerGet"));
 
   aopOp (left, ic, FALSE);
   loadDptrFromOperand (left, TRUE);
 
-  /* so dptr know contains the address */
+  /* so dptr now contains the address */
   aopOp (result, ic, FALSE);
 
   /* if bit then unpack */
   if (IS_BITFIELD (retype))
-    genUnpackBits (result, "dptr", GPOINTER, ifx);
+    {
+      genUnpackBits (result, "dptr", GPOINTER, ifx);
+    }
   else
     {
       size = AOP_SIZE (result);
@@ -9967,7 +10314,7 @@ genPointerGet (iCode * ic, iCode *pi, iCode *ifx)
   sym_link *type, *etype;
   int p_type;
 
-  D(emitcode (";     genPointerGet",""));
+  D (emitcode (";", "genPointerGet"));
 
   left = IC_LEFT (ic);
   result = IC_RESULT (ic);
@@ -9990,11 +10337,12 @@ genPointerGet (iCode * ic, iCode *pi, iCode *ifx)
 
   /* special case when cast remat */
   if (p_type == GPOINTER && OP_SYMBOL(left)->remat &&
-      IS_CAST_ICODE(OP_SYMBOL(left)->rematiCode)) {
-          left = IC_RIGHT(OP_SYMBOL(left)->rematiCode);
-          type = operandType (left);
-          p_type = DCL_TYPE (type);
-  }
+      IS_CAST_ICODE(OP_SYMBOL(left)->rematiCode))
+    {
+      left = IC_RIGHT(OP_SYMBOL(left)->rematiCode);
+      type = operandType (left);
+      p_type = DCL_TYPE (type);
+    }
   /* now that we have the pointer type we assign
      the pointer values */
   switch (p_type)
@@ -10021,11 +10369,9 @@ genPointerGet (iCode * ic, iCode *pi, iCode *ifx)
       genGenPointerGet (left, result, ic, pi, ifx);
       break;
     }
-
 }
 
 
-
 /*-----------------------------------------------------------------*/
 /* genPackBits - generates code for packed bit storage             */
 /*-----------------------------------------------------------------*/
@@ -10041,7 +10387,7 @@ genPackBits (sym_link * etype,
   int litval;           /* source literal value (if AOP_LIT) */
   unsigned char mask;   /* bitmask within current byte */
 
-  D(emitcode (";     genPackBits",""));
+  D(emitcode (";", "genPackBits"));
 
   blen = SPEC_BLEN (etype);
   bstr = SPEC_BSTR (etype);
@@ -10157,7 +10503,6 @@ genPackBits (sym_link * etype,
         }
       emitPtrByteSet (rname, p_type, "a");
     }
-
 }
 
 
@@ -10172,18 +10517,19 @@ genDataPointerSet (operand * right,
   int size, offset = 0;
   char *l, buffer[256];
 
-  D(emitcode (";     genDataPointerSet",""));
+  D (emitcode (";", "genDataPointerSet"));
 
   aopOp (right, ic, FALSE);
 
   l = aopGet (result, 0, FALSE, TRUE);
+  l++; //remove #
   size = AOP_SIZE (right);
   while (size--)
     {
       if (offset)
-        sprintf (buffer, "(%s + %d)", l + 1, offset);
+        SNPRINTF (buffer, sizeof(buffer), "(%s + %d)", l, offset);
       else
-        sprintf (buffer, "%s", l + 1);
+        SNPRINTF (buffer, sizeof(buffer), "%s", l);
       emitcode ("mov", "%s,%s", buffer,
                 aopGet (right, offset++, FALSE, FALSE));
     }
@@ -10207,10 +10553,11 @@ genNearPointerSet (operand * right,
   sym_link *retype, *letype;
   sym_link *ptype = operandType (result);
 
-  D(emitcode (";     genNearPointerSet",""));
+  D (emitcode (";", "genNearPointerSet"));
 
   retype = getSpec (operandType (right));
   letype = getSpec (ptype);
+
   aopOp (result, ic, FALSE);
 
   /* if the result is rematerializable &
@@ -10270,14 +10617,14 @@ genNearPointerSet (operand * right,
     genPackBits ((IS_BITFIELD (retype) ? retype : letype), right, rname, POINTER);
   else
     {
-      /* we have can just get the values */
+      /* we can just get the values */
       int size = AOP_SIZE (right);
       int offset = 0;
 
       while (size--)
         {
           l = aopGet (right, offset, FALSE, TRUE);
-          if (*l == '@')
+          if ((*l == '@') || (strcmp (l, "acc") == 0))
             {
               MOVA (l);
               emitcode ("mov", "@%s,a", rname);
@@ -10336,7 +10683,7 @@ genPagedPointerSet (operand * right,
   char *rname, *l;
   sym_link *retype, *letype;
 
-  D(emitcode (";     genPagedPointerSet",""));
+  D (emitcode (";", "genPagedPointerSet"));
 
   retype = getSpec (operandType (right));
   letype = getSpec (operandType (result));
@@ -10372,7 +10719,6 @@ genPagedPointerSet (operand * right,
       while (size--)
         {
           l = aopGet (right, offset, FALSE, TRUE);
-
           MOVA (l);
           emitcode ("movx", "@%s,a", rname);
 
@@ -10425,12 +10771,12 @@ genFarPointerSet (operand * right,
   sym_link *retype = getSpec (operandType (right));
   sym_link *letype = getSpec (operandType (result));
 
-  D(emitcode (";     genFarPointerSet",""));
+  D(emitcode (";", "genFarPointerSet"));
 
   aopOp (result, ic, FALSE);
   loadDptrFromOperand (result, FALSE);
 
-  /* so dptr know contains the address */
+  /* so dptr now contains the address */
   aopOp (right, ic, FALSE);
 
   /* if bit then unpack */
@@ -10470,17 +10816,19 @@ genGenPointerSet (operand * right,
   sym_link *retype = getSpec (operandType (right));
   sym_link *letype = getSpec (operandType (result));
 
-  D(emitcode (";     genGenPointerSet",""));
+  D (emitcode (";", "genGenPointerSet"));
 
   aopOp (result, ic, FALSE);
   loadDptrFromOperand (result, TRUE);
 
-  /* so dptr know contains the address */
+  /* so dptr now contains the address */
   aopOp (right, ic, FALSE);
 
   /* if bit then unpack */
   if (IS_BITFIELD (retype) || IS_BITFIELD (letype))
-    genPackBits ((IS_BITFIELD (retype) ? retype : letype), right, "dptr", GPOINTER);
+    {
+      genPackBits ((IS_BITFIELD (retype) ? retype : letype), right, "dptr", GPOINTER);
+    }
   else
     {
       size = AOP_SIZE (right);
@@ -10515,7 +10863,7 @@ genPointerSet (iCode * ic, iCode *pi)
   sym_link *type, *etype;
   int p_type;
 
-  D(emitcode (";     genPointerSet",""));
+  D (emitcode (";", "genPointerSet"));
 
   right = IC_RIGHT (ic);
   result = IC_RESULT (ic);
@@ -10542,6 +10890,7 @@ genPointerSet (iCode * ic, iCode *pi)
           type = operandType (result);
           p_type = DCL_TYPE (type);
   }
+
   /* now that we have the pointer type we assign
      the pointer values */
   switch (p_type)
@@ -10568,7 +10917,6 @@ genPointerSet (iCode * ic, iCode *pi)
       werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
               "genPointerSet: illegal pointer type");
     }
-
 }
 
 /*-----------------------------------------------------------------*/
@@ -10581,19 +10929,22 @@ genIfx (iCode * ic, iCode * popIc)
   int isbit = 0;
   char *dup = NULL;
 
-  D(emitcode (";     genIfx",""));
+  D (emitcode (";", "genIfx"));
 
   aopOp (cond, ic, FALSE);
 
   /* get the value into acc */
   if (AOP_TYPE (cond) != AOP_CRY)
-    toBoolean (cond);
+    {
+      toBoolean (cond);
+    }
   else
     {
       isbit = 1;
       if (AOP(cond)->aopu.aop_dir)
         dup = Safe_strdup(AOP(cond)->aopu.aop_dir);
     }
+
   /* the result is now in the accumulator or a directly addressable bit */
   freeAsmop (cond, NULL, ic, TRUE);
 
@@ -10623,7 +10974,7 @@ genAddrOf (iCode * ic)
   symbol *sym = OP_SYMBOL (IC_LEFT (ic));
   int size, offset;
 
-  D(emitcode (";     genAddrOf",""));
+  D (emitcode (";", "genAddrOf"));
 
   aopOp (IC_RESULT (ic), ic, FALSE);
 
@@ -10632,15 +10983,28 @@ genAddrOf (iCode * ic)
      variable */
   if (sym->onStack)
     {
-      /* if it has an offset then we need to compute
-         it */
+      /* if it has an offset then we need to compute it */
       if (sym->stack)
         {
-          emitcode ("mov", "a,%s", SYM_BP (sym));
-          emitcode ("add", "a,#0x%02x", ((sym->stack < 0) ?
-                                         ((char) (sym->stack - _G.nRegsSaved)) :
-                                         ((char) sym->stack)) & 0xff);
-          aopPut (IC_RESULT (ic), "a", 0);
+          int stack_offset = ((sym->stack < 0) ?
+                              ((char) (sym->stack - _G.nRegsSaved)) :
+                              ((char) sym->stack)) & 0xff;
+          if ((abs(stack_offset) == 1) &&
+              !AOP_NEEDSACC(IC_RESULT (ic)) &&
+              !isOperandVolatile (IC_RESULT (ic), FALSE))
+            {
+              aopPut (IC_RESULT (ic), SYM_BP (sym), 0);
+              if (stack_offset > 0)
+                emitcode ("inc", "%s", aopGet (IC_RESULT (ic), LSB, FALSE, FALSE));
+              else
+                emitcode ("dec", "%s", aopGet (IC_RESULT (ic), LSB, FALSE, FALSE));
+            }
+          else
+            {
+              emitcode ("mov", "a,%s", SYM_BP (sym));
+              emitcode ("add", "a,#0x%02x", stack_offset & 0xff);
+              aopPut (IC_RESULT (ic), "a", 0);
+            }
         }
       else
         {
@@ -10655,7 +11019,6 @@ genAddrOf (iCode * ic)
         {
           aopPut (IC_RESULT (ic), zero, offset++);
         }
-
       goto release;
     }
 
@@ -10671,7 +11034,7 @@ genAddrOf (iCode * ic)
                  sym->rname,
                  offset * 8);
       else
-        sprintf (s, "#%s", sym->rname);
+        SNPRINTF (s, sizeof(s), "#%s", sym->rname);
       aopPut (IC_RESULT (ic), s, offset++);
     }
 
@@ -10690,7 +11053,7 @@ genFarFarAssign (operand * result, operand * right, iCode * ic)
   int offset = 0;
   char *l;
 
-  D(emitcode (";     genFarFarAssign",""));
+  D (emitcode (";", "genFarFarAssign"));
 
   /* first push the right side on to the stack */
   while (size--)
@@ -10722,7 +11085,7 @@ genAssign (iCode * ic)
   int size, offset;
   unsigned long lit = 0L;
 
-  D(emitcode(";     genAssign",""));
+  D (emitcode (";", "genAssign"));
 
   result = IC_RESULT (ic);
   right = IC_RIGHT (ic);
@@ -10740,7 +11103,6 @@ genAssign (iCode * ic)
       IS_TRUE_SYMOP (result) &&
       isOperandInFarSpace (result))
     {
-
       genFarFarAssign (result, right, ic);
       return;
     }
@@ -10756,29 +11118,7 @@ genAssign (iCode * ic)
   /* if the result is a bit */
   if (AOP_TYPE (result) == AOP_CRY)
     {
-
-      /* if the right size is a literal then
-         we know what the value is */
-      if (AOP_TYPE (right) == AOP_LIT)
-        {
-          if (((int) operandLitValue (right)))
-            aopPut (result, one, 0);
-          else
-            aopPut (result, zero, 0);
-          goto release;
-        }
-
-      /* the right is also a bit variable */
-      if (AOP_TYPE (right) == AOP_CRY)
-        {
-          emitcode ("mov", "c,%s", AOP (right)->aopu.aop_dir);
-          aopPut (result, "c", 0);
-          goto release;
-        }
-
-      /* we need to or */
-      toBoolean (right);
-      aopPut (result, "a", 0);
+      assignBit (result, right);
       goto release;
     }
 
@@ -10788,6 +11128,7 @@ genAssign (iCode * ic)
   offset = 0;
   if (AOP_TYPE (right) == AOP_LIT)
     lit = (unsigned long) floatFromVal (AOP (right)->aopu.aop_lit);
+
   if ((size > 1) &&
       (AOP_TYPE (result) != AOP_REG) &&
       (AOP_TYPE (right) == AOP_LIT) &&
@@ -10803,7 +11144,11 @@ genAssign (iCode * ic)
           offset++;
           size--;
         }
-      emitcode ("clr", "a");
+      /* And now fill the rest with zeros. */
+      if (size)
+        {
+          emitcode ("clr", "a");
+        }
       while (size--)
         {
           aopPut (result, "a", offset);
@@ -10836,7 +11181,7 @@ genJumpTab (iCode * ic)
   char *l;
   unsigned int count;
 
-  D(emitcode (";     genJumpTab",""));
+  D (emitcode (";", "genJumpTab"));
 
   count = elementsInSet( IC_JTLABELS (ic) );
 
@@ -10868,7 +11213,7 @@ genJumpTab (iCode * ic)
       jtab = newiTempLabel (NULL);
       emitcode ("mov", "dptr,#%05d$", jtab->key + 100);
       emitcode ("jmp", "@a+dptr");
-      emitcode ("", "%05d$:", jtab->key + 100);
+      emitLabel (jtab);
       /* now generate the jump labels */
       for (jtab = setFirstItem (IC_JTLABELS (ic)); jtab;
            jtab = setNextItem (IC_JTLABELS (ic)))
@@ -10924,13 +11269,13 @@ genJumpTab (iCode * ic)
       emitcode ("ret", "");
 
       /* now generate jump table, LSB */
-      emitcode ("", "%05d$:", jtablo->key + 100);
+      emitLabel (jtablo);
       for (jtab = setFirstItem (IC_JTLABELS (ic)); jtab;
            jtab = setNextItem (IC_JTLABELS (ic)))
         emitcode (".db", "%05d$", jtab->key + 100);
 
       /* now generate jump table, MSB */
-      emitcode ("", "%05d$:", jtabhi->key + 100);
+      emitLabel (jtabhi);
       for (jtab = setFirstItem (IC_JTLABELS (ic)); jtab;
            jtab = setNextItem (IC_JTLABELS (ic)))
          emitcode (".db", "%05d$>>8", jtab->key + 100);
@@ -10949,7 +11294,7 @@ genCast (iCode * ic)
   operand *right = IC_RIGHT (ic);
   int size, offset;
 
-  D(emitcode(";     genCast",""));
+  D (emitcode (";", "genCast"));
 
   /* if they are equivalent then do nothing */
   if (operandsEqu (IC_RESULT (ic), IC_RIGHT (ic)))
@@ -10959,33 +11304,9 @@ genCast (iCode * ic)
   aopOp (result, ic, FALSE);
 
   /* if the result is a bit (and not a bitfield) */
-  // if (AOP_TYPE (result) == AOP_CRY)
   if (IS_BIT (OP_SYMBOL (result)->type))
-    /* not for bitfields */
     {
-      /* if the right size is a literal then
-         we know what the value is */
-      if (AOP_TYPE (right) == AOP_LIT)
-        {
-          if (((int) operandLitValue (right)))
-            aopPut (result, one, 0);
-          else
-            aopPut (result, zero, 0);
-
-          goto release;
-        }
-
-      /* the right is also a bit variable */
-      if (AOP_TYPE (right) == AOP_CRY)
-        {
-          emitcode ("mov", "c,%s", AOP (right)->aopu.aop_dir);
-          aopPut (result, "c", 0);
-          goto release;
-        }
-
-      /* we need to or */
-      toBoolean (right);
-      aopPut (result, "a", 0);
+      assignBit (result, right);
       goto release;
     }
 
@@ -11022,7 +11343,9 @@ genCast (iCode * ic)
       if (IS_GENPTR (ctype))
         {
           if (IS_PTR (type))
-            p_type = DCL_TYPE (type);
+            {
+              p_type = DCL_TYPE (type);
+            }
           else
             {
               if (SPEC_SCLS(etype)==S_REGISTER) {
@@ -11124,15 +11447,12 @@ genDjnz (iCode * ic, iCode * ifx)
   if (!ifx)
     return 0;
 
-  D(emitcode (";     genDjnz",""));
-
   /* if the if condition has a false label
      then we cannot save */
   if (IC_FALSE (ifx))
     return 0;
 
-  /* if the minus is not of the form
-     a = a - 1 */
+  /* if the minus is not of the form a = a - 1 */
   if (!isOperandEqual (IC_RESULT (ic), IC_LEFT (ic)) ||
       !IS_OP_LITERAL (IC_RIGHT (ic)))
     return 0;
@@ -11146,6 +11466,9 @@ genDjnz (iCode * ic, iCode * ifx)
     return 0;
 
   /* otherwise we can save BIG */
+
+  D (emitcode (";", "genDjnz"));
+
   lbl = newiTempLabel (NULL);
   lbl1 = newiTempLabel (NULL);
 
@@ -11179,6 +11502,8 @@ genDjnz (iCode * ic, iCode * ifx)
       emitcode ("dec", "%s",
                 aopGet (IC_RESULT (ic), 0, FALSE, FALSE));
       MOVA (aopGet (IC_RESULT (ic), 0, FALSE, FALSE));
+      freeAsmop (IC_RESULT (ic), NULL, ic, TRUE);
+      ifx->generated = 1;
       emitcode ("jnz", "%05d$", lbl->key + 100);
     }
   else
@@ -11187,11 +11512,12 @@ genDjnz (iCode * ic, iCode * ifx)
                 lbl->key + 100);
     }
   emitcode ("sjmp", "%05d$", lbl1->key + 100);
-  emitcode ("", "%05d$:", lbl->key + 100);
+  emitLabel (lbl);
   emitcode ("ljmp", "%05d$", IC_TRUE (ifx)->key + 100);
-  emitcode ("", "%05d$:", lbl1->key + 100);
+  emitLabel (lbl1);
 
-  freeAsmop (IC_RESULT (ic), NULL, ic, TRUE);
+  if (!ifx->generated)
+      freeAsmop (IC_RESULT (ic), NULL, ic, TRUE);
   ifx->generated = 1;
   return 1;
 }
@@ -11205,7 +11531,7 @@ genReceive (iCode * ic)
   int size = getSize (operandType (IC_RESULT (ic)));
   int offset = 0;
 
-  D(emitcode (";     genReceive",""));
+  D (emitcode (";", "genReceive"));
 
   if (ic->argreg == 1)
     { /* first parameter */
@@ -11308,7 +11634,7 @@ genDummyRead (iCode * ic)
   operand *op;
   int size, offset;
 
-  D(emitcode(";     genDummyRead",""));
+  D (emitcode(";", "genDummyRead"));
 
   op = IC_RIGHT (ic);
   if (op && IS_SYMOP (op))
@@ -11367,15 +11693,15 @@ genCritical (iCode *ic)
 {
   symbol *tlbl = newiTempLabel (NULL);
 
-  D(emitcode(";     genCritical",""));
+  D (emitcode(";", "genCritical"));
 
   if (IC_RESULT (ic))
     {
       aopOp (IC_RESULT (ic), ic, TRUE);
-      aopPut (IC_RESULT (ic), one, 0);
+      aopPut (IC_RESULT (ic), one, 0); /* save old ea in an operand */
       emitcode ("jbc", "ea,%05d$", (tlbl->key + 100)); /* atomic test & clear */
       aopPut (IC_RESULT (ic), zero, 0);
-      emitcode ("", "%05d$:", (tlbl->key + 100));
+      emitLabel (tlbl);
       freeAsmop (IC_RESULT (ic), NULL, ic, TRUE);
     }
   else
@@ -11383,7 +11709,7 @@ genCritical (iCode *ic)
       emitcode ("setb", "c");
       emitcode ("jbc", "ea,%05d$", (tlbl->key + 100)); /* atomic test & clear */
       emitcode ("clr", "c");
-      emitcode ("", "%05d$:", (tlbl->key + 100));
+      emitLabel (tlbl);
       emitcode ("push", "psw"); /* save old ea via c in psw on top of stack*/
     }
 }
@@ -11394,7 +11720,7 @@ genCritical (iCode *ic)
 static void
 genEndCritical (iCode *ic)
 {
-  D(emitcode(";     genEndCritical",""));
+  D(emitcode(";", "genEndCritical"));
 
   if (IC_RIGHT (ic))
     {
@@ -11435,7 +11761,7 @@ gen51Code (iCode * lic)
 
   /* print the allocation information */
   if (allocInfo && currFunc)
-    printAllocInfo (currFunc, codeOutFile);
+    printAllocInfo (currFunc, codeOutBuf);
   /* if debug information required */
   if (options.debug && currFunc)
     {
@@ -11459,7 +11785,7 @@ gen51Code (iCode * lic)
               debugFile->writeCLine (ic);
             }
           if (!options.noCcodeInAsm) {
-            emitcode ("", ";%s:%d: %s", ic->filename, ic->lineno,
+            emitcode (";", "%s:%d: %s", ic->filename, ic->lineno,
                       printCLine(ic->filename, ic->lineno));
           }
           cln = ic->lineno;
@@ -11467,13 +11793,14 @@ gen51Code (iCode * lic)
       #if 0
       if (ic->seqPoint && ic->seqPoint != cseq)
         {
-          emitcode ("", "; sequence point %d", ic->seqPoint);
+          emitcode (";", "sequence point %d", ic->seqPoint);
           cseq = ic->seqPoint;
         }
       #endif
       if (options.iCodeInAsm) {
         char regsInUse[80];
         int i;
+        char *iLine;
 
         #if 0
         for (i=0; i<8; i++) {
@@ -11490,7 +11817,9 @@ gen51Code (iCode * lic)
             }
         #endif
         }
-        emitcode("", "; [%s] ic:%d: %s", regsInUse, ic->seq, printILine(ic));
+        iLine = printILine(ic);
+        emitcode(";", "[%s] ic:%d: %s", regsInUse, ic->seq, iLine);
+        dbuf_free(iLine);
       }
       /* if the result is marked as
          spilt and rematerializable or code for
@@ -11595,7 +11924,7 @@ gen51Code (iCode * lic)
         case NE_OP:
 
           /* note these two are xlated by algebraic equivalence
-             during parsing SDCC.y */
+             in decorateType() in SDCCast.c */
           werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
                   "got '>=' or '<=' shouldn't have come here");
           break;
@@ -11669,7 +11998,9 @@ gen51Code (iCode * lic)
 
         case '=':
           if (POINTER_SET (ic))
-            genPointerSet (ic, hasInc (IC_RESULT(ic),ic,getSize(operandType(IC_RIGHT(ic)))));
+            genPointerSet (ic,
+                           hasInc (IC_RESULT (ic), ic,
+                                   getSize (operandType (IC_RIGHT (ic)))));
           else
             genAssign (ic);
           break;
@@ -11727,6 +12058,6 @@ gen51Code (iCode * lic)
     peepHole (&lineHead);
 
   /* now do the actual printing */
-  printLine (lineHead, codeOutFile);
+  printLine (lineHead, codeOutBuf);
   return;
 }