]> git.gag.com Git - fw/sdcc/blobdiff - src/ds390/gen.c
* src/SDCCast.c (decorateType): fixed previouse immature commit, added optimisation
[fw/sdcc] / src / ds390 / gen.c
index 6abd6a6db6e9c3fda2ded067eec6a7b0c0ef6341..050020dae74b41eea82b429992c715fdc3c8fa69 100644 (file)
@@ -140,51 +140,49 @@ static int _lazyDPS = 0;        /* if non-zero, we are doing lazy evaluation of
 /* emitcode - writes the code into a file : for now it is simple    */
 /*-----------------------------------------------------------------*/
 static void
-emitcode (char *inst, char *fmt,...)
+emitcode (char *inst, const char *fmt,...)
 {
-    va_list ap;
-    char lb[INITIAL_INLINEASM];
-    char *lbp = lb;
+  va_list ap;
+  char lb[INITIAL_INLINEASM];
+  char *lbp = lb;
 
-    va_start (ap, fmt);
+  va_start (ap, fmt);
 
-    if (inst && *inst)
+  if (inst && *inst)
     {
-        if (fmt && *fmt)
+      if (fmt && *fmt)
         {
-            SNPRINTF (lb, sizeof(lb), "%s\t", inst);
+          SNPRINTF (lb, sizeof(lb), "%s\t", inst);
         }
-        else
+      else
         {
-            SNPRINTF (lb, sizeof(lb), "%s", inst);
+          SNPRINTF (lb, sizeof(lb), "%s", inst);
         }
 
-        tvsprintf (lb + strlen(lb), sizeof(lb) - strlen(lb),
-                   fmt, ap);
+      tvsprintf (lb + strlen(lb), sizeof(lb) - strlen(lb), fmt, ap);
     }
-    else
+  else
     {
-        tvsprintf (lb, sizeof(lb), fmt, ap);
+      tvsprintf (lb, sizeof(lb), fmt, ap);
     }
 
-
-    while (isspace (*lbp))
+  while (isspace ((unsigned char)*lbp))
     {
-        lbp++;
+      lbp++;
     }
 
-    if (lbp && *lbp)
+  if (lbp && *lbp)
     {
-        lineCurr = (lineCurr ?
-                    connectLine (lineCurr, newLineNode (lb)) :
-                    (lineHead = newLineNode (lb)));
+      lineCurr = (lineCurr ?
+                  connectLine (lineCurr, newLineNode (lb)) :
+                  (lineHead = newLineNode (lb)));
     }
 
-    lineCurr->isInline = _G.inLine;
-    lineCurr->isDebug = _G.debugLine;
-    lineCurr->ic = _G.current_iCode;
-    lineCurr->aln = ds390newAsmLineNode(_currentDPS);
-    va_end (ap);
+  lineCurr->isInline = _G.inLine;
+  lineCurr->isDebug = _G.debugLine;
+  lineCurr->ic = _G.current_iCode;
+  lineCurr->aln = ds390newAsmLineNode(_currentDPS);
+  va_end (ap);
 }
 
 /*-----------------------------------------------------------------*/
@@ -730,7 +728,7 @@ aopForRemat (symbol * sym)
 {
   iCode *ic = sym->rematiCode;
   asmop *aop = newAsmop (AOP_IMMD);
-  int ptr_type =0;
+  int ptr_type = 0;
   int val = 0;
 
   for (;;)
@@ -743,12 +741,8 @@ aopForRemat (symbol * sym)
               sym_link *from_type = operandType(IC_RIGHT(ic));
               aop->aopu.aop_immd.from_cast_remat = 1;
               ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
-              ptr_type = DCL_TYPE(from_type);
-              if (ptr_type == IPOINTER) {
-                // bug #481053
-                ptr_type = POINTER;
-              }
-              continue ;
+              ptr_type = pointerTypeToGPByte (DCL_TYPE(from_type), NULL, NULL);
+              continue;
       } else break;
 
       ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
@@ -870,7 +864,6 @@ operandsEqu (operand * op1, operand * op2)
       && strcmp (sym1->rname, sym2->rname) == 0)
     return TRUE;
 
-
   /* if left is a tmp & right is not */
   if (IS_ITEMP (op1) &&
       !IS_ITEMP (op2) &&
@@ -988,7 +981,6 @@ aopOp (operand * op, iCode * ic, bool result, bool useDP2)
 
   sym = OP_SYMBOL (op);
 
-
   /* if the type is a conditional */
   if (sym->regType == REG_CND)
     {
@@ -1918,29 +1910,46 @@ genCpl (iCode * ic)
   int offset = 0;
   int size;
   symbol *tlbl;
+  sym_link *letype = getSpec (operandType (IC_LEFT (ic)));
 
-  D (emitcode (";", "genCpl "););
-
+  D(emitcode (";", "genCpl"));
 
   /* assign asmOps to operand & result */
   aopOp (IC_LEFT (ic), ic, FALSE, FALSE);
   aopOp (IC_RESULT (ic), ic, TRUE, AOP_USESDPTR(IC_LEFT (ic)));
 
   /* special case if in bit space */
-  if (AOP_TYPE (IC_RESULT (ic)) == AOP_CRY) {
-    if (AOP_TYPE (IC_LEFT (ic)) == AOP_CRY) {
-      emitcode ("mov", "c,%s", IC_LEFT (ic)->aop->aopu.aop_dir);
-      emitcode ("cpl", "c");
-      emitcode ("mov", "%s,c", IC_RESULT (ic)->aop->aopu.aop_dir);
+  if (AOP_TYPE (IC_RESULT (ic)) == AOP_CRY)
+    {
+      char *l;
+
+      if (AOP_TYPE (IC_LEFT (ic)) == AOP_CRY ||
+          (SPEC_USIGN (letype) && IS_CHAR (letype)))
+        {
+          /* promotion rules are responsible for this strange result:
+             bit -> int -> ~int -> bit
+             uchar -> int -> ~int -> bit
+          */
+          emitcode ("setb", "%s", IC_RESULT (ic)->aop->aopu.aop_dir);
+          goto release;
+        }
+      tlbl=newiTempLabel(NULL);
+      l = aopGet (AOP (IC_LEFT (ic)), offset++, FALSE, FALSE, NULL);
+      if (AOP_TYPE (IC_LEFT (ic)) == AOP_ACC ||
+          AOP_TYPE (IC_LEFT (ic)) == AOP_REG ||
+          IS_AOP_PREG (IC_LEFT (ic)))
+        {
+          emitcode ("cjne", "%s,#0xFF,%05d$", l, tlbl->key + 100);
+        }
+      else
+        {
+          MOVA (l);
+          emitcode ("cjne", "a,#0xFF,%05d$", tlbl->key + 100);
+        }
+      emitcode ("", "%05d$:", tlbl->key+100);
+      outBitC (IC_RESULT(ic));
       goto release;
     }
-    tlbl=newiTempLabel(NULL);
-    emitcode ("cjne", "%s,#0x01,%05d$",
-              aopGet(AOP(IC_LEFT(ic)), 0, FALSE,FALSE,NULL), tlbl->key+100);
-    emitcode ("", "%05d$:", tlbl->key+100);
-    outBitC (IC_RESULT(ic));
-    goto release;
-  }
 
   size = AOP_SIZE (IC_RESULT (ic));
   _startLazyDPSEvaluation ();
@@ -1975,7 +1984,7 @@ genUminusFloat (operand * op, operand * result)
   size = AOP_SIZE (op) - 1;
 
   while (size--)
-  {
+    {
       aopPut (AOP (result),
               aopGet (AOP (op), offset, FALSE, FALSE, NULL),
               offset);
@@ -2536,12 +2545,12 @@ saveRBank (int bank, iCode * ic, bool pushPsw)
 
   if (aop)
   {
-       freeAsmop (NULL, aop, ic, TRUE);
+    freeAsmop (NULL, aop, ic, TRUE);
   }
 
   if (ic)
   {
-      ic->bankSaved = 1;
+    ic->bankSaved = 1;
   }
 }
 
@@ -2642,7 +2651,7 @@ genCall (iCode * ic)
     if (!ic->regsSaved)
       saveRegisters (ic);
 
-  /* if send set is not empty the assign */
+  /* if send set is not empty then assign */
   /* We've saved all the registers we care about;
   * therefore, we may clobber any register not used
   * in the calling convention (i.e. anything not in
@@ -3791,54 +3800,55 @@ genPlusIncr (iCode * ic)
 
       if (AOP_TYPE (IC_RESULT (ic)) == AOP_REG ||
           IS_AOP_PREG (IC_RESULT (ic)))
-      {
-        emitcode ("cjne", "%s,#0,!tlabel", l, tlbl->key + 100);
-      }
+        {
+          emitcode ("cjne", "%s,#0,!tlabel", l, tlbl->key + 100);
+        }
       else
-      {
+        {
           emitcode ("clr", "a");
           emitcode ("cjne", "a,%s,!tlabel", l, tlbl->key + 100);
-      }
+        }
 
       l = aopGet (AOP (IC_RESULT (ic)), MSB16, FALSE, FALSE, NULL);
       emitcode ("inc", "%s", l);
       if (size > 2)
         {
-            if (!strcmp(l, "acc"))
+          if (!strcmp(l, "acc"))
             {
                 emitcode("jnz", "!tlabel", tlbl->key + 100);
             }
-            else if (AOP_TYPE (IC_RESULT (ic)) == AOP_REG ||
-                     IS_AOP_PREG (IC_RESULT (ic)))
+          else if (AOP_TYPE (IC_RESULT (ic)) == AOP_REG ||
+                   IS_AOP_PREG (IC_RESULT (ic)))
             {
                 emitcode ("cjne", "%s,#0,!tlabel", l, tlbl->key + 100);
             }
-            else
+          else
             {
                 emitcode ("cjne", "a,%s,!tlabel", l, tlbl->key + 100);
             }
 
-            l = aopGet (AOP (IC_RESULT (ic)), MSB24, FALSE, FALSE, NULL);
-            emitcode ("inc", "%s", l);
+          l = aopGet (AOP (IC_RESULT (ic)), MSB24, FALSE, FALSE, NULL);
+          emitcode ("inc", "%s", l);
         }
       if (size > 3)
         {
-            if (!strcmp(l, "acc"))
+          if (!strcmp(l, "acc"))
             {
                 emitcode("jnz", "!tlabel", tlbl->key + 100);
             }
-            else if (AOP_TYPE (IC_RESULT (ic)) == AOP_REG ||
-                     IS_AOP_PREG (IC_RESULT (ic)))
+          else if (AOP_TYPE (IC_RESULT (ic)) == AOP_REG ||
+                   IS_AOP_PREG (IC_RESULT (ic)))
             {
                 emitcode ("cjne", "%s,#0,!tlabel", l, tlbl->key + 100);
             }
-            else
+          else
             {
                 emitcode ("cjne", "a,%s,!tlabel", l, tlbl->key + 100);
             }
 
-            l = aopGet (AOP (IC_RESULT (ic)), MSB32, FALSE, FALSE, NULL);
-            emitcode ("inc", "%s", l);  }
+          l = aopGet (AOP (IC_RESULT (ic)), MSB32, FALSE, FALSE, NULL);
+          emitcode ("inc", "%s", l);
+        }
 
       if (emitTlbl)
         {
@@ -3849,8 +3859,12 @@ genPlusIncr (iCode * ic)
 
   if (AOP_TYPE(IC_RESULT(ic))==AOP_STR && IS_ITEMP(IC_RESULT(ic)) &&
       !AOP_USESDPTR(IC_LEFT(ic)) && icount <= 5 && size <= 3 &&
-      options.model == MODEL_FLAT24 ) {
-
+      options.model == MODEL_FLAT24 )
+    {
+      if (IC_RESULT(ic)->isGptr)
+        {
+          emitcode ("mov","b,%s",aopGet(AOP (IC_LEFT (ic)), 3, FALSE, FALSE, NULL));
+        }
       switch (size) {
       case 3:
           emitcode ("mov","dpx,%s",aopGet(AOP (IC_LEFT (ic)), 2, FALSE, FALSE, NULL));
@@ -3988,7 +4002,7 @@ adjustArithmeticResult (iCode * ic)
     {
       char buff[5];
       SNPRINTF (buff, sizeof(buff),
-                "#%d", pointerCode (getSpec (operandType (IC_LEFT (ic)))));
+                "#%d", pointerTypeToGPByte (pointerCode (getSpec (operandType (IC_LEFT (ic)))), NULL, NULL));
       aopPut (AOP (IC_RESULT (ic)), buff, GPTRSIZE - 1);
     }
 }
@@ -6393,7 +6407,7 @@ hasInc (operand *op, iCode *ic, int osize)
           return lic;
       }
       /* if the operand used or deffed */
-      if (bitVectBitValue(OP_USES(op),lic->key) || (unsigned) lic->defKey == op->key) {
+      if (bitVectBitValue(OP_USES(op),lic->key) || lic->defKey == op->key) {
           return NULL;
       }
       /* if GOTO or IFX */
@@ -10185,31 +10199,31 @@ genGenPointerGet (operand * left,
      then we do nothing else we move the value to dptr */
   if (AOP_TYPE (left) != AOP_STR)
     {
-      /* if this is remateriazable */
+      /* if this is rematerializable */
       if (AOP_TYPE (left) == AOP_IMMD)
         {
           emitcode ("mov", "dptr,%s", aopGet (AOP (left), 0, TRUE, FALSE, NULL));
           if (AOP(left)->aopu.aop_immd.from_cast_remat)
             {
-                MOVB(aopGet(AOP (left), AOP_SIZE(left)-1, FALSE, FALSE, NULL));
+              MOVB(aopGet(AOP (left), AOP_SIZE(left)-1, FALSE, FALSE, NULL));
             }
-            else
+          else
             {
-                emitcode ("mov", "b,#%d", pointerCode (retype));
+              emitcode ("mov", "b,#%d", pointerCode (retype));
             }
         }
       else
         {                       /* we need to get it byte by byte */
-            _startLazyDPSEvaluation ();
-            emitcode ("mov", "dpl,%s", aopGet (AOP(left),0,FALSE,FALSE,NULL));
-            emitcode ("mov", "dph,%s", aopGet (AOP(left),1,FALSE,FALSE,NULL));
-            if (options.model == MODEL_FLAT24) {
-                emitcode ("mov", "dpx,%s", aopGet (AOP(left),2,FALSE,FALSE,NULL));
-                emitcode ("mov", "b,%s", aopGet (AOP(left),3,FALSE,FALSE,NULL));
-            } else {
-                emitcode ("mov", "b,%s", aopGet (AOP(left),2,FALSE,FALSE,NULL));
-            }
-            _endLazyDPSEvaluation ();
+          _startLazyDPSEvaluation ();
+          emitcode ("mov", "dpl,%s", aopGet (AOP(left),0,FALSE,FALSE,NULL));
+          emitcode ("mov", "dph,%s", aopGet (AOP(left),1,FALSE,FALSE,NULL));
+          if (options.model == MODEL_FLAT24) {
+              emitcode ("mov", "dpx,%s", aopGet (AOP(left),2,FALSE,FALSE,NULL));
+              emitcode ("mov", "b,%s", aopGet (AOP(left),3,FALSE,FALSE,NULL));
+          } else {
+              emitcode ("mov", "b,%s", aopGet (AOP(left),2,FALSE,FALSE,NULL));
+          }
+          _endLazyDPSEvaluation ();
         }
     }
 
@@ -10311,11 +10325,12 @@ genPointerGet (iCode * ic, iCode *pi)
     }
   /* special case when cast remat */
   if (p_type == GPOINTER && OP_SYMBOL(left)->remat &&
-      IS_CAST_ICODE(OP_SYMBOL(left)->rematiCode)) {
-          left = IC_RIGHT(OP_SYMBOL(left)->rematiCode);
-          type = operandType (left);
-          p_type = DCL_TYPE (type);
-  }
+      IS_CAST_ICODE(OP_SYMBOL(left)->rematiCode))
+    {
+      left = IC_RIGHT(OP_SYMBOL(left)->rematiCode);
+      type = operandType (left);
+      p_type = DCL_TYPE (type);
+    }
   /* now that we have the pointer type we assign
      the pointer values */
   switch (p_type)
@@ -11705,7 +11720,7 @@ genCast (iCode * ic)
                     exit(1);
                 }
 
-                SNPRINTF(gpValStr, sizeof(gpValStr), "#0x%d", gpVal);
+                SNPRINTF(gpValStr, sizeof(gpValStr), "#0x%x", gpVal);
                 aopPut (AOP (result), gpValStr, GPTRSIZE - 1);
             }
           goto release;
@@ -13432,7 +13447,6 @@ gen390Code (iCode * lic)
 
   for (ic = lic; ic; ic = ic->next)
     {
-
       _G.current_iCode = ic;
 
       if (ic->lineno && cln != ic->lineno)