* doc/sdccman.lyx: inserted footnotes about inline assembler labels,
[fw/sdcc] / src / mcs51 / gen.c
index f9fc2e1b70912d38d9161be7fe5c98d7c4b52505..39a01cbe694c2e0d65af6fe2f0e3d582abbb0e1a 100644 (file)
@@ -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
@@ -181,19 +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);
@@ -1491,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))
     {
@@ -1666,28 +1670,40 @@ aopPut (operand * result, const char *s, int offset)
       break;
 
     case AOP_CRY:
-      /* if result no bit variable */
-      if (!aop->aopu.aop_dir)
-        {
-          assert (!strcmp (s, "c"));
-          /* inefficient: move carry into A and use jz/jnz */
-          emitcode ("clr", "a");
-          emitcode ("rlc", "a");
-          accuse = TRUE;
-        }
-      else 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))
+      // 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"))
         {
           MOVA (s);
           /* set C, if a >= 1 */
           emitcode ("add", "a,#0xff");
-          emitcode ("mov", "%s,c", aop->aopu.aop_dir);
+          s = "c";
         }
+      // now source is zero, one or carry
+
+      /* if result no bit variable */
+      if (!d)
+        {
+          if (!strcmp (s, "c"))
+            {
+              /* inefficient: move carry into A and use jz/jnz */
+              emitcode ("clr", "a");
+              emitcode ("rlc", "a");
+              accuse = TRUE;
+            }
+          else
+            {
+              MOVA (s);
+              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:
@@ -2669,9 +2685,9 @@ saveRBank (int bank, iCode * ic, bool pushPsw)
     }
 
   if (ic)
-  {
-    ic->bankSaved = 1;
-  }
+    {
+      ic->bankSaved = 1;
+    }
 }
 
 /*-----------------------------------------------------------------*/
@@ -3405,7 +3421,7 @@ genFunction (iCode * ic)
                 }
             }
         }
-        else
+      else
         {
             /* This ISR uses a non-zero bank.
              *
@@ -3590,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"))
@@ -3615,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]);
@@ -4514,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);
@@ -4543,7 +4559,7 @@ genPlus (iCode * ic)
            skip_bytes++;
          }
        if (skip_bytes)
-         D(emitcode (";     genPlus shortcut",""));
+         D(emitcode (";", "genPlus shortcut"));
     }
 
   while (size--)
@@ -5214,7 +5230,7 @@ genDivbits (operand * left,
   char *l;
   bool pushedB;
 
-  D(emitcode (";     genDivbits",""));
+  D(emitcode (";", "genDivbits"));
 
   pushedB = pushB ();
 
@@ -5247,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:
@@ -6033,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;
@@ -6539,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
@@ -6963,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
@@ -7340,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
@@ -7996,7 +8013,7 @@ genSwap (iCode * ic)
 {
   operand *left, *result;
 
-  D(emitcode (";     genSwap",""));
+  D(emitcode (";", "genSwap"));
 
   left = IC_LEFT (ic);
   result = IC_RESULT (ic);
@@ -9697,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));
@@ -10087,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:
@@ -10370,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);
@@ -10754,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);
@@ -11703,7 +11720,7 @@ genCritical (iCode *ic)
 static void
 genEndCritical (iCode *ic)
 {
-  D(emitcode(";     genEndCritical",""));
+  D(emitcode(";", "genEndCritical"));
 
   if (IC_RIGHT (ic))
     {
@@ -11768,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;
@@ -11776,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
@@ -11801,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