* doc/sdccman.lyx: inserted footnotes about inline assembler labels,
[fw/sdcc] / src / mcs51 / gen.c
index a2841f70701081452a7f27e7ba5da925a466433a..39a01cbe694c2e0d65af6fe2f0e3d582abbb0e1a 100644 (file)
@@ -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,6 +40,7 @@
 #include "common.h"
 #include "SDCCpeeph.h"
 #include "ralloc.h"
+#include "rtrack.h"
 #include "gen.h"
 #include "dbuf_string.h"
 
@@ -66,6 +66,8 @@ 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
@@ -119,7 +121,6 @@ static char *rb1regs[] = {
 };
 
 extern struct dbuf_s *codeOutBuf;
-static void saveRBank (int, iCode *, bool);
 
 #define RESULTONSTACK(x) \
                          (IC_RESULT(x) && IC_RESULT(x)->aop && \
@@ -149,7 +150,7 @@ static unsigned char SRMask[] =
 /*-----------------------------------------------------------------*/
 /* emitcode - writes the code into a file : for now it is simple    */
 /*-----------------------------------------------------------------*/
-static void
+void
 emitcode (const char *inst, const char *fmt,...)
 {
   va_list ap;
@@ -182,17 +183,20 @@ emitcode (const char *inst, const char *fmt,...)
       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);
@@ -227,6 +231,11 @@ 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);
 }
 
@@ -240,25 +249,14 @@ movb (const char *x)
   if (!strncmp(x, "b", 2))
     return;
 
-  emitcode("mov","b,%s", x);
-}
-
-/*-----------------------------------------------------------------*/
-/* movc - moves specified value into the carry                     */
-/*-----------------------------------------------------------------*/
-static void
-movc (const char *s)
-{
-  if (!strcmp (s, zero))
-    CLRC;
-  else if (!strcmp (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");
+  /* 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);
 }
 
 /*-----------------------------------------------------------------*/
@@ -958,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;
     }
 
@@ -980,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];
@@ -991,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];
@@ -1416,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");
 
@@ -1496,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))
     {
@@ -1642,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:
@@ -1828,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
     {
@@ -1880,6 +1907,56 @@ toBoolean (operand * oper)
     }
 }
 
+/*-----------------------------------------------------------------*/
+/* 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           */
@@ -2174,6 +2251,7 @@ saveRegisters (iCode * lic)
           rsave = bitVectCplAnd (rsave, rsavebits);
           rsave = bitVectSetBit (rsave, bitVectFirstBit (rsavebits));
         }
+      freeBitVect (rsavebits);
 
       if (count == 1)
         {
@@ -2244,6 +2322,7 @@ saveRegisters (iCode * lic)
             }
         }
     }
+  freeBitVect (rsave);
 }
 
 /*-----------------------------------------------------------------*/
@@ -2273,6 +2352,7 @@ unsaveRegisters (iCode * ic)
           rsave = bitVectCplAnd (rsave, rsavebits);
           rsave = bitVectSetBit (rsave, bitVectFirstBit (rsavebits));
         }
+      freeBitVect (rsavebits);
 
       if (count == 1)
         {
@@ -2332,6 +2412,7 @@ unsaveRegisters (iCode * ic)
             }
         }
     }
+  freeBitVect (rsave);
 }
 
 
@@ -2545,7 +2626,7 @@ 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;
 
@@ -2554,7 +2635,7 @@ saveRBank (int bank, iCode * ic, bool pushPsw)
       if (!ic)
         {
           /* Assume r0 is available for use. */
-          r = REG_WITH_INDEX (R0_IDX);;
+          r = REG_WITH_INDEX (R0_IDX);
         }
       else
         {
@@ -2583,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)
@@ -2620,9 +2685,9 @@ saveRBank (int bank, iCode * ic, bool pushPsw)
     }
 
   if (ic)
-  {
-    ic->bankSaved = 1;
-  }
+    {
+      ic->bankSaved = 1;
+    }
 }
 
 /*-----------------------------------------------------------------*/
@@ -2664,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)
@@ -2732,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++;
@@ -3005,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))
     {
@@ -3015,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)
@@ -3027,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);
             }
@@ -3053,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);
             }
@@ -3065,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));
@@ -3089,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));
@@ -3121,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", "");
           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 */
@@ -3178,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))))
@@ -3292,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"))
@@ -3315,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);
                     }
                 }
             }
@@ -3337,7 +3421,7 @@ genFunction (iCode * ic)
                 }
             }
         }
-        else
+      else
         {
             /* This ISR uses a non-zero bank.
              *
@@ -3522,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"))
@@ -3547,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]);
@@ -3726,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
@@ -3742,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);
                     }
                 }
             }
@@ -3758,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);
             }
@@ -3800,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)
         {
@@ -3966,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:
@@ -4260,10 +4349,10 @@ genPlusBits (iCode * ic)
 {
   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");
       emitLabel (lbl);
@@ -4272,7 +4361,6 @@ genPlusBits (iCode * 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,%s", zero);
@@ -4442,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);
@@ -4471,7 +4559,7 @@ genPlus (iCode * ic)
            skip_bytes++;
          }
        if (skip_bytes)
-         D(emitcode (";     genPlus shortcut",""));
+         D(emitcode (";", "genPlus shortcut"));
     }
 
   while (size--)
@@ -4939,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
         {
@@ -5142,7 +5230,7 @@ genDivbits (operand * left,
   char *l;
   bool pushedB;
 
-  D(emitcode (";     genDivbits",""));
+  D(emitcode (";", "genDivbits"));
 
   pushedB = pushB ();
 
@@ -5175,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:
@@ -5214,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);
@@ -5387,7 +5475,7 @@ genDiv (iCode * ic)
 
   D (emitcode (";", "genDiv"));
 
-  /* assign the amsops */
+  /* assign the asmops */
   aopOp (left, ic, FALSE);
   aopOp (right, ic, FALSE);
   aopOp (result, ic, TRUE);
@@ -5528,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);
@@ -5906,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);
@@ -5936,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);
@@ -5961,8 +6049,9 @@ gencjneshort (operand * left, operand * right, symbol * lbl)
   /* 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;
@@ -6467,10 +6556,10 @@ genAnd (iCode * ic, iCode * ifx)
   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
@@ -6891,10 +6980,10 @@ genOr (iCode * ic, iCode * ifx)
   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
@@ -7268,10 +7357,10 @@ genXor (iCode * ic, iCode * ifx)
   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
@@ -7302,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);
 
@@ -7388,7 +7478,7 @@ genXor (iCode * ic, iCode * ifx)
       // 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;
@@ -7923,7 +8013,7 @@ genSwap (iCode * ic)
 {
   operand *left, *result;
 
-  D(emitcode (";     genSwap",""));
+  D(emitcode (";", "genSwap"));
 
   left = IC_LEFT (ic);
   result = IC_RESULT (ic);
@@ -9624,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));
@@ -10014,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:
@@ -10297,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);
@@ -10681,7 +10771,7 @@ 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);
@@ -11028,28 +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;
     }
 
@@ -11237,29 +11306,7 @@ genCast (iCode * ic)
   /* if the result is a bit (and not a bitfield) */
   if (IS_BIT (OP_SYMBOL (result)->type))
     {
-      /* 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;
     }
 
@@ -11673,7 +11720,7 @@ genCritical (iCode *ic)
 static void
 genEndCritical (iCode *ic)
 {
-  D(emitcode(";     genEndCritical",""));
+  D(emitcode(";", "genEndCritical"));
 
   if (IC_RIGHT (ic))
     {
@@ -11738,7 +11785,7 @@ gen51Code (iCode * lic)
               debugFile->writeCLine (ic);
             }
           if (!options.noCcodeInAsm) {
-            emitcode ("", ";\t%s:%d: %s", ic->filename, ic->lineno,
+            emitcode (";", "%s:%d: %s", ic->filename, ic->lineno,
                       printCLine(ic->filename, ic->lineno));
           }
           cln = ic->lineno;
@@ -11746,7 +11793,7 @@ 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
@@ -11771,7 +11818,7 @@ gen51Code (iCode * lic)
         #endif
         }
         iLine = printILine(ic);
-        emitcode("", "; [%s] ic:%d: %s", regsInUse, ic->seq, printILine(ic));
+        emitcode(";", "[%s] ic:%d: %s", regsInUse, ic->seq, iLine);
         dbuf_free(iLine);
       }
       /* if the result is marked as