* as/hc08/lkaomf51.c (OutputName): made name unsigned char,
[fw/sdcc] / src / mcs51 / gen.c
index 6f9f7252436fbd581ca7899f30b17f23f288bbf9..7ae3fa4edbb9078cf84aa7c4cdb2ef5e83cadb0d 100644 (file)
@@ -82,9 +82,9 @@ static unsigned short rbank = -1;
 #define SYM_BP(sym)   (SPEC_OCLS (sym->etype)->paged ? "_bpx" : "_bp")
 
 #define R0INB   _G.bu.bs.r0InB
-#define R1INB  _G.bu.bs.r1InB
-#define OPINB  _G.bu.bs.OpInB
-#define BINUSE _G.bu.BInUse
+#define R1INB   _G.bu.bs.r1InB
+#define OPINB   _G.bu.bs.OpInB
+#define BINUSE  _G.bu.BInUse
 
 static struct
   {
@@ -166,7 +166,7 @@ emitcode (char *inst, const char *fmt,...)
   else
       tvsprintf (lb, sizeof(lb), fmt, ap);
 
-  while (isspace (*lbp))
+  while (isspace ((unsigned char)*lbp))
       lbp++;
 
   if (lbp && *lbp)
@@ -693,7 +693,7 @@ aopForRemat (symbol * sym)
               aop->aopu.aop_immd.from_cast_remat = 1;
               ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
               ptr_type = pointerTypeToGPByte (DCL_TYPE(from_type), NULL, NULL);
-              continue ;
+              continue;
       } else break;
 
       ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
@@ -1745,14 +1745,23 @@ genNot (iCode * ic)
   /* if in bit space then a special case */
   if (AOP_TYPE (IC_LEFT (ic)) == AOP_CRY)
     {
-      emitcode ("mov", "c,%s", IC_LEFT (ic)->aop->aopu.aop_dir);
-      emitcode ("cpl", "c");
-      outBitC (IC_RESULT (ic));
+      /* if left==result then cpl bit */
+      if (sameRegs (AOP (IC_LEFT (ic)), AOP (IC_RESULT (ic))))
+        {
+          emitcode ("cpl", "%s", IC_LEFT (ic)->aop->aopu.aop_dir);
+        }
+      else
+        {
+          emitcode ("mov", "c,%s", IC_LEFT (ic)->aop->aopu.aop_dir);
+          emitcode ("cpl", "c");
+          outBitC (IC_RESULT (ic));
+        }
       goto release;
     }
 
   toBoolean (IC_LEFT (ic));
 
+  /* set C, if a == 0 */
   tlbl = newiTempLabel (NULL);
   emitcode ("cjne", "a,#0x01,%05d$", tlbl->key + 100);
   emitcode ("", "%05d$:", tlbl->key + 100);
@@ -2055,14 +2064,14 @@ saveRegisters (iCode * lic)
   else
     {
       bool bits_pushed = FALSE;
-    for (i = 0; i < mcs51_nRegs; i++)
-      {
-        if (bitVectBitValue (rsave, i))
+      for (i = 0; i < mcs51_nRegs; i++)
+        {
+          if (bitVectBitValue (rsave, i))
             {
               bits_pushed = pushReg (i, bits_pushed);
             }
         }
-      }
+    }
 }
 
 /*-----------------------------------------------------------------*/
@@ -2144,19 +2153,19 @@ unsaveRegisters (iCode * ic)
   else
     {
       bool bits_popped = FALSE;
-    for (i = mcs51_nRegs; i >= 0; i--)
-      {
-        if (bitVectBitValue (rsave, i))
+      for (i = mcs51_nRegs; i >= 0; i--)
+        {
+          if (bitVectBitValue (rsave, i))
             {
               bits_popped = popReg (i, bits_popped);
             }
         }
-      }
+    }
 }
 
 
 /*-----------------------------------------------------------------*/
-/* pushSide -                */
+/* pushSide -                                                      */
 /*-----------------------------------------------------------------*/
 static void
 pushSide (operand * oper, int size)
@@ -2312,7 +2321,7 @@ genIpush (iCode * ic)
           strcmp (l, "a"))
         {
           if (strcmp (l, prev) || *l == '@')
-          MOVA (l);
+            MOVA (l);
           emitcode ("push", "acc");
         }
       else
@@ -2492,11 +2501,11 @@ unsaveRBank (int bank, iCode * ic, bool popPsw)
 /*-----------------------------------------------------------------*/
 static void genSend(set *sendSet)
 {
-    iCode *sic;
+  iCode *sic;
   int bit_count = 0;
 
   /* first we do all bit parameters */
-    for (sic = setFirstItem (sendSet); sic;
+  for (sic = setFirstItem (sendSet); sic;
        sic = setNextItem (sendSet))
     {
       aopOp (IC_LEFT (sic), sic, FALSE);
@@ -2530,6 +2539,7 @@ static void genSend(set *sendSet)
               emitcode ("mov", "b[%d],c", bit);
             }
           bit_count++;
+          BitBankUsed = 1;
         }
       freeAsmop (IC_LEFT (sic), NULL, sic, TRUE);
     }
@@ -2544,19 +2554,19 @@ static void genSend(set *sendSet)
   for (sic = setFirstItem (sendSet); sic;
        sic = setNextItem (sendSet))
     {
-          int size, offset = 0;
-          aopOp (IC_LEFT (sic), sic, FALSE);
-          size = AOP_SIZE (IC_LEFT (sic));
+      int size, offset = 0;
+      aopOp (IC_LEFT (sic), sic, FALSE);
+      size = AOP_SIZE (IC_LEFT (sic));
 
       if (sic->argreg == 1)
         {
           while (size--)
             {
               char *l = aopGet (IC_LEFT (sic), offset, FALSE, FALSE);
-                  if (strcmp (l, fReturn[offset]))
-                      emitcode ("mov", "%s,%s", fReturn[offset], l);
-                  offset++;
-              }
+              if (strcmp (l, fReturn[offset]))
+                  emitcode ("mov", "%s,%s", fReturn[offset], l);
+              offset++;
+            }
         }
       else if (sic->argreg <= 12)
         {
@@ -2565,9 +2575,28 @@ static void genSend(set *sendSet)
               emitcode ("mov","%s,%s", rb1regs[sic->argreg+offset-5],
                         aopGet (IC_LEFT (sic), offset,FALSE, FALSE));
               offset++;
-              }
-          }
-          freeAsmop (IC_LEFT (sic), NULL, sic, TRUE);
+            }
+        }
+      freeAsmop (IC_LEFT (sic), NULL, sic, TRUE);
+    }
+}
+
+/*-----------------------------------------------------------------*/
+/* selectRegBank - emit code to select the register bank           */
+/*-----------------------------------------------------------------*/
+static void
+selectRegBank (short bank, bool keepFlags)
+{
+  /* if f.e. result is in carry */
+  if (keepFlags)
+    {
+      emitcode ("anl", "psw,#0xE7");
+      if (bank)
+        emitcode ("orl", "psw,#0x%02x", (bank << 3) & 0xff);
+    }
+  else
+    {
+      emitcode ("mov", "psw,#0x%02x", (bank << 3) & 0xff);
     }
 }
 
@@ -2578,6 +2607,7 @@ static void
 genCall (iCode * ic)
 {
   sym_link *dtype;
+  sym_link *etype;
 //  bool restoreBank = FALSE;
   bool swapBanks = FALSE;
   bool accuse = FALSE;
@@ -2587,6 +2617,7 @@ genCall (iCode * ic)
   D(emitcode(";     genCall",""));
 
   dtype = operandType (IC_LEFT (ic));
+  etype = getSpec(dtype);
   /* if send set is not empty then assign */
   if (_G.sendSet)
     {
@@ -2602,23 +2633,22 @@ genCall (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 */
-  dtype = operandType (IC_LEFT (ic));
   if (currFunc && dtype && !IFFUNC_ISNAKED(dtype) &&
       (FUNC_REGBANK (currFunc->type) != FUNC_REGBANK (dtype)) &&
        !IFFUNC_ISISR (dtype))
-  {
+    {
       swapBanks = TRUE;
-  }
+    }
 
   /* if caller saves & we have not saved then */
   if (!ic->regsSaved)
       saveRegisters (ic);
 
   if (swapBanks)
-  {
+    {
         emitcode ("mov", "psw,#0x%02x",
            ((FUNC_REGBANK(dtype)) << 3) & 0xff);
-  }
+    }
 
   /* make the call */
   if (IFFUNC_ISBANKEDCALL (dtype) && !SPEC_STAT(getSpec(dtype)))
@@ -2647,20 +2677,8 @@ genCall (iCode * ic)
     }
 
   if (swapBanks)
-  {
-      /* if result is in carry */
-      if (IS_BIT (OP_SYM_ETYPE (IC_LEFT (ic))))
-        {
-          emitcode ("anl", "psw,#0xE7");
-          if (FUNC_REGBANK(currFunc->type))
-            emitcode ("orl", "psw,#0x%02x",
-                ((FUNC_REGBANK(currFunc->type)) << 3) & 0xff);
-        }
-      else
-        {
-           emitcode ("mov", "psw,#0x%02x",
-               ((FUNC_REGBANK(currFunc->type)) << 3) & 0xff);
-        }
+    {
+      selectRegBank (FUNC_REGBANK(currFunc->type), IS_BIT (etype));
     }
 
   /* if we need assign a result value */
@@ -2757,9 +2775,12 @@ genPcall (iCode * ic)
   symbol *rlbl = newiTempLabel (NULL);
 //  bool restoreBank=FALSE;
   bool swapBanks = FALSE;
+  bool resultInF0 = FALSE;
 
   D(emitcode(";     genPCall",""));
 
+  dtype = operandType (IC_LEFT (ic))->next;
+  etype = getSpec(dtype);
   /* if caller saves & we have not saved then */
   if (!ic->regsSaved)
     saveRegisters (ic);
@@ -2767,18 +2788,16 @@ 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 */
-  dtype = operandType (IC_LEFT (ic))->next;
   if (currFunc && dtype && !IFFUNC_ISNAKED(dtype) &&
       (FUNC_REGBANK (currFunc->type) != FUNC_REGBANK (dtype)) &&
       !IFFUNC_ISISR (dtype))
-  {
+    {
 //    saveRBank (FUNC_REGBANK (dtype), ic, TRUE);
 //    restoreBank=TRUE;
       swapBanks = TRUE;
       // need caution message to user here
-  }
+    }
 
-  etype = getSpec(dtype);
   if (IS_LITERAL(etype))
     {
       /* if send set is not empty then assign */
@@ -2894,13 +2913,13 @@ genPcall (iCode * ic)
         }
     }
   if (swapBanks)
-  {
-       emitcode ("mov", "psw,#0x%02x",
-          ((FUNC_REGBANK(currFunc->type)) << 3) & 0xff);
-  }
+    {
+      selectRegBank (FUNC_REGBANK(currFunc->type), IS_BIT (etype));
+    }
 
   /* if we need assign a result value */
   if ((IS_ITEMP (IC_RESULT (ic)) &&
+       !IS_BIT (OP_SYM_ETYPE (IC_RESULT (ic))) &&
        (OP_SYMBOL (IC_RESULT (ic))->nRegs ||
         OP_SYMBOL (IC_RESULT (ic))->spildir)) ||
       IS_TRUE_SYMOP (IC_RESULT (ic)))
@@ -2915,13 +2934,19 @@ genPcall (iCode * ic)
       freeAsmop (IC_RESULT (ic), NULL, ic, TRUE);
     }
 
-  /* adjust the stack for parameters if
-     required */
+  /* adjust the stack for parameters if required */
   if (ic->parmBytes)
     {
       int i;
       if (ic->parmBytes > 3)
         {
+          if (IS_BIT (OP_SYM_ETYPE (IC_LEFT (ic))) &&
+              IS_BIT (OP_SYM_ETYPE (IC_RESULT (ic))))
+            {
+              emitcode ("mov", "F0,c");
+              resultInF0 = TRUE;
+            }
+
           emitcode ("mov", "a,%s", spname);
           emitcode ("add", "a,#0x%02x", (-ic->parmBytes) & 0xff);
           emitcode ("mov", "%s,a", spname);
@@ -2936,10 +2961,19 @@ genPcall (iCode * ic)
 //  if (restoreBank)
 //    unsaveRBank (FUNC_REGBANK (dtype), ic, TRUE);
 
-  /* if we hade saved some registers then
-     unsave them */
+  /* if we had saved some registers then unsave them */
   if (ic->regsSaved && !IFFUNC_CALLEESAVES(dtype))
     unsaveRegisters (ic);
+
+  if (IS_BIT (OP_SYM_ETYPE (IC_RESULT (ic))))
+    {
+      if (resultInF0)
+          emitcode ("mov", "c,F0");
+
+      aopOp (IC_RESULT (ic), ic, FALSE);
+      assignResultValue (IC_RESULT (ic), IC_LEFT (ic));
+      freeAsmop (IC_RESULT (ic), NULL, ic, TRUE);
+    }
 }
 
 /*-----------------------------------------------------------------*/
@@ -4498,14 +4532,14 @@ genMinus (iCode * ic)
             if (!offset && !size && lit== (unsigned long) -1) {
               emitcode ("dec", "a");
             } else if (!useCarry) {
-          /* first add without previous c */
+              /* first add without previous c */
               emitcode ("add", "a,#0x%02x",
                         (unsigned int) ((lit >> (offset * 8)) & 0x0FFL));
               useCarry = TRUE;
-          } else {
-            emitcode ("addc", "a,#0x%02x",
-                      (unsigned int) ((lit >> (offset * 8)) & 0x0FFL));
-          }
+            } else {
+              emitcode ("addc", "a,#0x%02x",
+                        (unsigned int) ((lit >> (offset * 8)) & 0x0FFL));
+            }
             aopPut (IC_RESULT (ic), "a", offset++, isOperandVolatile (IC_RESULT (ic), FALSE));
             } else {
               /* no need to add zeroes */
@@ -5603,7 +5637,7 @@ genCmpLt (iCode * ic, iCode * ifx)
   aopOp (right, ic, FALSE);
   aopOp (result, ic, TRUE);
 
-  genCmp (left, right, result, ifx, sign,ic);
+  genCmp (left, right, result, ifx, sign, ic);
 
   freeAsmop (result, NULL, ic, TRUE);
 }
@@ -6387,12 +6421,12 @@ genAnd (iCode * ic, iCode * ifx)
                           MOVA (aopGet (right, offset, FALSE, FALSE));
                           emitcode("anl", "a,b");
                         }
-                } else {
+                    } else {
                       MOVA (aopGet (right, offset, FALSE, FALSE));
-                  emitcode ("anl", "a,%s",
+                      emitcode ("anl", "a,%s",
                                 aopGet (left, offset, FALSE, FALSE));
+                    }
                 }
-              }
               emitcode ("jnz", "%05d$", tlbl->key + 100);
               offset++;
             }
@@ -7093,7 +7127,7 @@ genInline (iCode * ic)
       else
         {
           /* Add \n for labels, not dirs such as c:\mydir */
-          if ( (*bp == ':') && (isspace(bp[1])) )
+          if ( (*bp == ':') && (isspace((unsigned char)bp[1])) )
             {
               bp++;
               *bp = '\0';
@@ -10665,16 +10699,17 @@ genDjnz (iCode * ic, iCode * ifx)
 static void
 genReceive (iCode * ic)
 {
-    int size = getSize (operandType (IC_RESULT (ic)));
-    int offset = 0;
+  int size = getSize (operandType (IC_RESULT (ic)));
+  int offset = 0;
 
   D(emitcode (";     genReceive",""));
 
-  if (ic->argreg == 1) { /* first parameter */
+  if (ic->argreg == 1)
+    { /* first parameter */
       if (isOperandInFarSpace (IC_RESULT (ic)) &&
           (OP_SYMBOL (IC_RESULT (ic))->isspilt ||
-           IS_TRUE_SYMOP (IC_RESULT (ic)))) {
-
+           IS_TRUE_SYMOP (IC_RESULT (ic))))
+        {
           regs *tempRegs[4];
           int receivingA = 0;
           int roffset = 0;
@@ -10716,33 +10751,48 @@ genReceive (iCode * ic)
             }
 
           offset = fReturnSizeMCS51 - size;
-          while (size--) {
+          while (size--)
+            {
               emitcode ("push", "%s", (strcmp (fReturn[fReturnSizeMCS51 - offset - 1], "a") ?
                                        fReturn[fReturnSizeMCS51 - offset - 1] : "acc"));
               offset++;
-          }
+            }
           aopOp (IC_RESULT (ic), ic, FALSE);
           size = AOP_SIZE (IC_RESULT (ic));
           offset = 0;
-          while (size--) {
+          while (size--)
+            {
               emitcode ("pop", "acc");
               aopPut (IC_RESULT (ic), "a", offset++, isOperandVolatile (IC_RESULT (ic), FALSE));
-          }
-
-      } else {
+            }
+        }
+      else
+        {
           _G.accInUse++;
           aopOp (IC_RESULT (ic), ic, FALSE);
           _G.accInUse--;
           assignResultValue (IC_RESULT (ic), NULL);
-      }
-  } else { /* second receive onwards */
+        }
+    }
+  else if (ic->argreg > 12)
+    { /* bit parameters */
+      if (OP_SYMBOL (IC_RESULT (ic))->regs[0]->rIdx != ic->argreg-5)
+        {
+          aopOp (IC_RESULT (ic), ic, FALSE);
+          emitcode ("mov", "c,%s", rb1regs[ic->argreg-5]);
+          outBitC(IC_RESULT (ic));
+        }
+    }
+  else
+    { /* other parameters */
       int rb1off ;
       aopOp (IC_RESULT (ic), ic, FALSE);
       rb1off = ic->argreg;
-      while (size--) {
+      while (size--)
+        {
           aopPut (IC_RESULT (ic), rb1regs[rb1off++ -5], offset++, isOperandVolatile (IC_RESULT (ic), FALSE));
-      }
-  }
+        }
+    }
 
 release:
   freeAsmop (IC_RESULT (ic), NULL, ic, TRUE);