* sim/ucsim/cmd.src/cmdutil.cc: NUL device is detected as CG_FILE type
[fw/sdcc] / src / mcs51 / gen.c
index 973b2e35d6e4c430f29e840bc0e5a2e71e0ccb24..a2841f70701081452a7f27e7ba5da925a466433a 100644 (file)
@@ -42,6 +42,7 @@
 #include "SDCCpeeph.h"
 #include "ralloc.h"
 #include "gen.h"
+#include "dbuf_string.h"
 
 char *aopLiteral (value * val, int offset);
 char *aopLiteralLong (value * val, int offset, int size);
@@ -71,11 +72,11 @@ static unsigned short rbank = -1;
 #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] == REG_WITH_INDEX(R0_IDX) || \
@@ -117,7 +118,7 @@ static char *rb1regs[] = {
     "b0",  "b1",  "b2",  "b3",  "b4",  "b5",  "b6",  "b7"
 };
 
-extern FILE *codeOutFile;
+extern struct dbuf_s *codeOutBuf;
 static void saveRBank (int, iCode *, bool);
 
 #define RESULTONSTACK(x) \
@@ -149,32 +150,33 @@ static unsigned char SRMask[] =
 /* emitcode - writes the code into a file : for now it is simple    */
 /*-----------------------------------------------------------------*/
 static void
-emitcode (char *inst, const char *fmt,...)
+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++;
@@ -192,12 +194,15 @@ emitcode (char *inst, const char *fmt,...)
   lineCurr->ic = _G.current_iCode;
   lineCurr->isComment = (*lbp==';');
   va_end (ap);
+
+  dbuf_destroy(&dbuf);
 }
 
 static void
 emitLabel (symbol *tlbl)
 {
-  emitcode ("", "%05d$:", (tlbl->key +100));
+  emitcode ("", "%05d$:", tlbl->key + 100);
+  lineCurr->isLabel = 1;
 }
 
 /*-----------------------------------------------------------------*/
@@ -244,9 +249,9 @@ movb (const char *x)
 static void
 movc (const char *s)
 {
-  if (s == zero)
+  if (!strcmp (s, zero))
     CLRC;
-  else if (s == one)
+  else if (!strcmp (s, one))
     SETC;
   else if (strcmp (s, "c"))
     {/* it's not in carry already */
@@ -423,8 +428,8 @@ endOfWorld:
       (*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 */
+  /* 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;
@@ -434,7 +439,7 @@ endOfWorld:
     {
       (*aopp)->type = AOP_R1;
       return REG_WITH_INDEX (R1_IDX);
-  }
+    }
 
   /* now this is REALLY the end of the world */
   werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
@@ -624,9 +629,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)))
@@ -649,7 +654,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");
@@ -755,7 +760,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);
     }
 
@@ -849,11 +854,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;
 
@@ -964,8 +972,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;
         }
@@ -1083,7 +1090,7 @@ freeAsmop (operand * op, asmop * aaop, iCode * ic, bool pop)
           emitcode ("mov", "r1,b");
           R1INB--;
         }
-      if (_G.r1Pushed)
+      else if (_G.r1Pushed)
         {
           if (pop)
             {
@@ -1129,7 +1136,6 @@ freeAsmop (operand * op, asmop * aaop, iCode * ic, bool pop)
             emitcode ("pop", "ar1");
             _G.r1Pushed--;
           }
-
         if (_G.r0Pushed)
           {
             emitcode ("pop", "ar0");
@@ -1391,8 +1397,8 @@ 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
         {
@@ -1516,20 +1522,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),
+          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"))
         {
@@ -1875,6 +1881,32 @@ toBoolean (operand * oper)
 }
 
 
+/*-------------------------------------------------------------------*/
+/* 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                          */
 /*-----------------------------------------------------------------*/
@@ -2119,8 +2151,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
@@ -2520,15 +2552,15 @@ saveRBank (int bank, iCode * ic, bool pushPsw)
   if (options.useXstack)
     {
       if (!ic)
-      {
+        {
           /* Assume r0 is available for use. */
           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);
@@ -3179,12 +3211,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   */
 /*-----------------------------------------------------------------*/
@@ -3228,6 +3254,7 @@ genFunction (iCode * ic)
   emitcode (";", "-----------------------------------------");
 
   emitcode ("", "%s:", sym->rname);
+  lineCurr->isLabel = 1;
   ftype = operandType (IC_LEFT (ic));
   _G.currentFunc = sym;
 
@@ -3440,7 +3467,6 @@ genFunction (iCode * ic)
         }
     }
 
-
   if (fReentrant)
     {
       if (options.useXstack)
@@ -3590,14 +3616,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");
         }
@@ -3679,7 +3705,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");
         }
     }
@@ -3993,7 +4020,7 @@ genLabel (iCode * ic)
   if (IC_LABEL (ic) == entryLabel)
     return;
 
-  emitcode ("", "%05d$:", (IC_LABEL (ic)->key + 100));
+  emitLabel (IC_LABEL (ic));
 }
 
 /*-----------------------------------------------------------------*/
@@ -4054,10 +4081,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) &&
@@ -4075,7 +4104,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;
         }
@@ -4191,6 +4220,14 @@ genPlusIncr (iCode * ic)
       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;
 }
 
@@ -4238,7 +4275,7 @@ genPlusBits (iCode * ic)
       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));
     }
 }
@@ -4310,7 +4347,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);
     }
 }
@@ -4377,7 +4415,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++);
             }
         }
@@ -4513,10 +4551,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))
@@ -4533,7 +4573,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;
         }
@@ -4627,6 +4667,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;
 }
 
@@ -4731,7 +4779,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");
@@ -4787,7 +4835,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));
@@ -5380,7 +5428,7 @@ genModbits (operand * left,
   char *l;
   bool pushedB;
 
-  D(emitcode (";     genModbits",""));
+  D (emitcode (";", "genModbits"));
 
   pushedB = pushB ();
 
@@ -5412,7 +5460,7 @@ genModOneByte (operand * left,
   symbol *lbl;
   int size, offset;
 
-  D(emitcode (";     genModOneByte",""));
+  D (emitcode (";", "genModOneByte"));
 
   size = AOP_SIZE (result) - 1;
   offset = 1;
@@ -5749,6 +5797,33 @@ genCmp (operand * left, operand * right,
                     }
                   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;
+                }
             }
           CLRC;
           while (size--)
@@ -5764,24 +5839,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++;
-                          MOVB (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
                 {
@@ -6533,7 +6598,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;
             }
@@ -6623,7 +6688,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);
@@ -6643,8 +6708,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));
                 }
             }
         }
@@ -6668,30 +6732,41 @@ 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++;
             }
@@ -6748,15 +6823,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");
                 }
@@ -6792,7 +6884,7 @@ 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);
@@ -6986,7 +7078,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);
@@ -7007,8 +7099,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));
                     }
                 }
             }
@@ -7028,16 +7119,46 @@ 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++;
             }
@@ -7077,17 +7198,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");
                 }
@@ -7214,7 +7352,6 @@ genXor (iCode * ic, iCode * ifx)
                     }
                 }
             }
-
         }
       else
         {
@@ -7296,7 +7433,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);
@@ -7316,8 +7453,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));
                 }
             }
         }
@@ -7342,19 +7478,46 @@ 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++;
             }
@@ -7384,17 +7547,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");
                 }
@@ -8305,8 +8485,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))
     {
@@ -8357,8 +8537,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))
     {
@@ -8427,7 +8607,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);
 }
@@ -8497,8 +8686,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);
     }
@@ -8513,8 +8701,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);
     }
@@ -8529,8 +8716,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);
     }
@@ -8656,7 +8842,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);
 
@@ -8678,7 +8864,6 @@ genLeftShiftLiteral (operand * left,
           movLeft2Result (left, size, result, size, 0);
         }
     }
-
   else if (shCount >= (size * 8))
     {
       while (size--)
@@ -8866,9 +9051,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__);
@@ -8883,9 +9068,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
             {
@@ -8895,7 +9080,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);
+            }
         }
     }
 
@@ -8910,28 +9104,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)
     {
@@ -8939,10 +9134,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
         {
@@ -9009,7 +9204,8 @@ genrshFour (operand * result, operand * left,
         }
     }
   else
-    {                           /* 1 <= shCount <= 7 */
+    {
+      /* 1 <= shCount <= 7 */
       if (shCount <= 2)
         {
           shiftRLong (left, LSB, result, sign);
@@ -9038,7 +9234,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);
 
@@ -9063,10 +9259,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
@@ -9534,22 +9731,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)
         {
-          SNPRINTF (buffer, sizeof(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++);
     }
 
@@ -9574,7 +9773,7 @@ genNearPointerGet (operand * left,
   sym_link *ltype = operandType (left);
   char buffer[80];
 
-  D(emitcode (";     genNearPointerGet",""));
+  D (emitcode (";", "genNearPointerGet"));
 
   rtype = operandType (result);
   retype = getSpec (rtype);
@@ -9650,7 +9849,7 @@ genNearPointerGet (operand * left,
             }
           else
             {
-              sprintf (buffer, "@%s", rname);
+              SNPRINTF (buffer, sizeof(buffer), "@%s", rname);
               aopPut (result, buffer, offset);
             }
           offset++;
@@ -9712,7 +9911,7 @@ genPagedPointerGet (operand * left,
   char *rname;
   sym_link *rtype, *retype;
 
-  D(emitcode (";     genPagedPointerGet",""));
+  D (emitcode (";", "genPagedPointerGet"));
 
   rtype = operandType (result);
   retype = getSpec (rtype);
@@ -9866,7 +10065,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);
@@ -9918,7 +10117,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);
@@ -10233,13 +10432,14 @@ genDataPointerSet (operand * right,
   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));
     }
@@ -10486,7 +10686,7 @@ genFarPointerSet (operand * right,
   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 */
@@ -10526,12 +10726,12 @@ 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 */
@@ -10696,11 +10896,25 @@ genAddrOf (iCode * ic)
       /* 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
         {
@@ -10730,7 +10944,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++);
     }
 
@@ -10749,7 +10963,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--)
@@ -10898,7 +11112,7 @@ genJumpTab (iCode * ic)
   char *l;
   unsigned int count;
 
-  D(emitcode (";     genJumpTab",""));
+  D (emitcode (";", "genJumpTab"));
 
   count = elementsInSet( IC_JTLABELS (ic) );
 
@@ -11021,9 +11235,7 @@ 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 */
@@ -11439,7 +11651,7 @@ genCritical (iCode *ic)
   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);
       emitLabel (tlbl);
@@ -11502,7 +11714,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)
     {
@@ -11526,7 +11738,7 @@ gen51Code (iCode * lic)
               debugFile->writeCLine (ic);
             }
           if (!options.noCcodeInAsm) {
-            emitcode ("", ";%s:%d: %s", ic->filename, ic->lineno,
+            emitcode ("", ";\t%s:%d: %s", ic->filename, ic->lineno,
                       printCLine(ic->filename, ic->lineno));
           }
           cln = ic->lineno;
@@ -11541,6 +11753,7 @@ gen51Code (iCode * lic)
       if (options.iCodeInAsm) {
         char regsInUse[80];
         int i;
+        char *iLine;
 
         #if 0
         for (i=0; i<8; i++) {
@@ -11557,7 +11770,9 @@ gen51Code (iCode * lic)
             }
         #endif
         }
+        iLine = printILine(ic);
         emitcode("", "; [%s] ic:%d: %s", regsInUse, ic->seq, printILine(ic));
+        dbuf_free(iLine);
       }
       /* if the result is marked as
          spilt and rematerializable or code for
@@ -11662,7 +11877,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;
@@ -11736,7 +11951,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;
@@ -11794,6 +12011,6 @@ gen51Code (iCode * lic)
     peepHole (&lineHead);
 
   /* now do the actual printing */
-  printLine (lineHead, codeOutFile);
+  printLine (lineHead, codeOutBuf);
   return;
 }