More strcpy() strcat() sprintf() squashing
[fw/sdcc] / src / SDCCicode.c
index 59bcf886fc8610efcaf5f2ff3a8c2891dbc26187..62f2ceadfde566e87d1fb8cbff4a5bf56e26a99f 100644 (file)
@@ -115,6 +115,26 @@ iCodeTable codeTable[] =
   {ARRAYINIT, "arrayInit", picGenericOne, NULL},
 };
 
+// this makes it more easy to catch bugs
+struct bitVect *OP_DEFS(struct operand *op) {
+  wassert (IS_SYMOP(op));
+  return OP_SYMBOL(op)->defs;
+}
+struct bitVect *OP_DEFS_SET(struct operand *op, struct bitVect *bv) {
+  wassert (IS_SYMOP(op));
+  OP_SYMBOL(op)->defs=bv;
+  return bv;
+}
+struct bitVect *OP_USES(struct operand *op) {
+  wassert (IS_SYMOP(op));
+  return OP_SYMBOL(op)->uses;
+}
+struct bitVect *OP_USES_SET(struct operand *op, struct bitVect *bv) {
+  wassert (IS_SYMOP(op));
+  OP_SYMBOL(op)->uses=bv;
+  return bv;
+}
+
 /*-----------------------------------------------------------------*/
 /* checkConstantRange: check a constant against the type           */
 /*-----------------------------------------------------------------*/
@@ -126,9 +146,9 @@ iCodeTable codeTable[] =
      pedantic>1: "char c=200" is not allowed (evaluates to -56)
 */
 
-void checkConstantRange(sym_link *ltype, value *val, char *msg, int pedantic) {
+void checkConstantRange(sym_link *ltype, value *val, char *msg, 
+                       int pedantic) {
   double max;
-  char message[132]="";
   int warnings=0;
   int negative=0;
   long v;
@@ -178,8 +198,9 @@ void checkConstantRange(sym_link *ltype, value *val, char *msg, int pedantic) {
     warnings++;
   }
 
+#if 0 // temporary disabled, leaving the warning as a reminder
   if (warnings) {
-    sprintf (message, "for %s %s in %s", 
+    SNPRINTF (message, sizeof(message), "for %s %s in %s", 
             SPEC_USIGN(ltype) ? "unsigned" : "signed",
             nounName(ltype), msg);
     werror (W_CONST_RANGE, message);
@@ -187,6 +208,7 @@ void checkConstantRange(sym_link *ltype, value *val, char *msg, int pedantic) {
     if (pedantic>1)
       fatalError++;
   }
+#endif
 }
 
 /*-----------------------------------------------------------------*/
@@ -222,13 +244,14 @@ printOperand (operand * op, FILE * file)
     case SYMBOL:
 #define REGA 1
 #ifdef REGA
-      fprintf (file, "%s [k%d lr%d:%d so:%d]{ ia%d re%d rm%d nos%d ru%d}",             /*{ar%d rm%d ru%d p%d a%d u%d i%d au%d k%d ks%d}"  , */
+      fprintf (file, "%s [k%d lr%d:%d so:%d]{ ia%d re%d rm%d nos%d ru%d dp%d}",                /*{ar%d rm%d ru%d p%d a%d u%d i%d au%d k%d ks%d}"  , */
               (OP_SYMBOL (op)->rname[0] ? OP_SYMBOL (op)->rname : OP_SYMBOL (op)->name),
               op->key,
               OP_LIVEFROM (op), OP_LIVETO (op),
               OP_SYMBOL (op)->stack,
-              op->isaddr, OP_SYMBOL (op)->isreqv, OP_SYMBOL (op)->remat,OP_SYMBOL(op)->noSpilLoc,
-              OP_SYMBOL(op)->ruonly
+              op->isaddr, OP_SYMBOL (op)->isreqv, 
+              OP_SYMBOL (op)->remat,OP_SYMBOL(op)->noSpilLoc,
+              OP_SYMBOL(op)->ruonly,OP_SYMBOL(op)->dptr
        );
       {
        fprintf (file, "{");
@@ -383,6 +406,9 @@ PRINTFUNC (picGenericOne)
   if (!IC_RESULT (ic) && !IC_LEFT (ic))
     fprintf (of, s);
 
+  if (ic->op == SEND || ic->op == RECEIVE) {
+      fprintf(of,"{argreg = %d}",ic->argreg);
+  }
   fprintf (of, "\n");
 }
 
@@ -587,11 +613,16 @@ newiTemp (char *s)
   symbol *itmp;
 
   if (s)
-    sprintf (buffer, "%s", s);
+  {
+      SNPRINTF (buffer, sizeof(buffer), "%s", s);
+  }
   else
-    sprintf (buffer, "iTemp%d", iTempNum++);
+  {
+      SNPRINTF (buffer, sizeof(buffer), "iTemp%d", iTempNum++);
+  }
+    
   itmp = newSymbol (buffer, 1);
-  strcpy (itmp->rname, itmp->name);
+  strncpyz (itmp->rname, itmp->name, SDCC_NAME_MAX);
   itmp->isitmp = 1;
 
   return itmp;
@@ -610,10 +641,12 @@ newiTempLabel (char *s)
     return itmplbl;
 
   if (s)
-    itmplbl = newSymbol (s, 1);
+    {
+       itmplbl = newSymbol (s, 1);
+    }
   else
     {
-      sprintf (buffer, "iTempLbl%d", iTempLblNum++);
+      SNPRINTF (buffer, sizeof(buffer), "iTempLbl%d", iTempLblNum++);
       itmplbl = newSymbol (buffer, 1);
     }
 
@@ -632,7 +665,7 @@ newiTempPreheaderLabel ()
 {
   symbol *itmplbl;
 
-  sprintf (buffer, "preHeaderLbl%d", iTempLblNum++);
+  SNPRINTF (buffer, sizeof(buffer), "preHeaderLbl%d", iTempLblNum++);
   itmplbl = newSymbol (buffer, 1);
 
   itmplbl->isitmp = 1;
@@ -858,6 +891,7 @@ isOperandLiteral (operand * op)
 
   return 0;
 }
+
 /*-----------------------------------------------------------------*/
 /* isOperandInFarSpace - will return true if operand is in farSpace */
 /*-----------------------------------------------------------------*/
@@ -886,6 +920,64 @@ isOperandInFarSpace (operand * op)
   return (IN_FARSPACE (SPEC_OCLS (etype)) ? TRUE : FALSE);
 }
 
+/*------------------------------------------------------------------*/
+/* isOperandInDirSpace - will return true if operand is in dirSpace */
+/*------------------------------------------------------------------*/
+bool 
+isOperandInDirSpace (operand * op)
+{
+  sym_link *etype;
+
+  if (!op)
+    return FALSE;
+
+  if (!IS_SYMOP (op))
+    return FALSE;
+
+  if (!IS_TRUE_SYMOP (op))
+    {
+      if (SPIL_LOC (op))
+       etype = SPIL_LOC (op)->etype;
+      else
+       return FALSE;
+    }
+  else
+    {
+      etype = getSpec (operandType (op));
+    }
+  return (IN_DIRSPACE (SPEC_OCLS (etype)) ? TRUE : FALSE);
+}
+
+/*--------------------------------------------------------------------*/
+/* isOperandInCodeSpace - will return true if operand is in codeSpace */
+/*--------------------------------------------------------------------*/
+bool 
+isOperandInCodeSpace (operand * op)
+{
+  sym_link *etype;
+
+  if (!op)
+    return FALSE;
+
+  if (!IS_SYMOP (op))
+    return FALSE;
+
+  etype = getSpec (operandType (op));
+
+  if (!IS_TRUE_SYMOP (op))
+    {
+      if (SPIL_LOC (op))
+       etype = SPIL_LOC (op)->etype;
+      else
+       return FALSE;
+    }
+  else
+    {
+      etype = getSpec (operandType (op));
+    }
+  return (IN_CODESPACE (SPEC_OCLS (etype)) ? TRUE : FALSE);
+}
+
 /*-----------------------------------------------------------------*/
 /* isOperandOnStack - will return true if operand is on stack      */
 /*-----------------------------------------------------------------*/
@@ -901,8 +993,12 @@ isOperandOnStack (operand * op)
     return FALSE;
 
   etype = getSpec (operandType (op));
+  if (IN_STACK (etype) ||
+      OP_SYMBOL(op)->onStack ||
+      (SPIL_LOC(op) && SPIL_LOC(op)->onStack))
+    return TRUE;
 
-  return ((IN_STACK (etype)) ? TRUE : FALSE);
+  return FALSE;
 }
 
 /*-----------------------------------------------------------------*/
@@ -1009,14 +1105,27 @@ operandOperation (operand * left, operand * right,
                                  (unsigned long) operandLitValue (right) :
                                  (long) operandLitValue (right)));
       break;
-    case RIGHT_OP:
-      retval = operandFromLit ((SPEC_USIGN(let) ? 
-                                 (unsigned long) operandLitValue (left) :
-                                 (long) operandLitValue (left)) >>
-                                (SPEC_USIGN(ret) ? 
-                                 (unsigned long) operandLitValue (right) :
-                                 (long) operandLitValue (right)));
+    case RIGHT_OP: {
+      double lval = operandLitValue(left), rval = operandLitValue(right);
+      double res=0;
+      switch ((SPEC_USIGN(let) ? 2 : 0) + (SPEC_USIGN(ret) ? 1 : 0)) 
+       {
+       case 0: // left=unsigned right=unsigned
+         res=(unsigned long)lval >> (unsigned long)rval;
+         break;
+       case 1: // left=unsigned right=signed
+         res=(unsigned long)lval >> (signed long)rval;
+         break;
+       case 2: // left=signed right=unsigned
+         res=(signed long)lval >> (unsigned long)rval;
+         break;
+       case 3: // left=signed right=signed
+         res=(signed long)lval >> (signed long)rval;
+         break;
+       }
+      retval = operandFromLit (res);
       break;
+    }
     case EQ_OP:
       retval = operandFromLit (operandLitValue (left) ==
                               operandLitValue (right));
@@ -1535,6 +1644,7 @@ usualBinaryConversions (operand ** op1, operand ** op2)
   sym_link *ltype = operandType (*op1);
   
   ctype = computeType (ltype, rtype);
+
   *op1 = geniCodeCast (ctype, *op1, TRUE);
   *op2 = geniCodeCast (ctype, *op2, TRUE);
   
@@ -1576,7 +1686,6 @@ geniCodeRValue (operand * op, bool force)
   if (IS_SPEC (type) &&
       IS_TRUE_SYMOP (op) &&
       (!IN_FARSPACE (SPEC_OCLS (etype)) ||
-      /* TARGET_IS_DS390)) */
       (options.model == MODEL_FLAT24) ))
     {
       op = operandFromOperand (op);
@@ -1632,7 +1741,7 @@ geniCodeCast (sym_link * type, operand * op, bool implicit)
   if (IS_PTR(type)) { // to a pointer
     if (!IS_PTR(optype) && !IS_FUNC(optype) && !IS_AGGREGATE(optype)) { // from a non pointer
       if (IS_INTEGRAL(optype)) { 
-       // maybe this is NULL, than it's ok.
+       // maybe this is NULL, than it's ok. 
        if (!(IS_LITERAL(optype) && (SPEC_CVAL(optype).v_ulong ==0))) {
          if (!TARGET_IS_Z80 && !TARGET_IS_GBZ80 && IS_GENPTR(type)) {
            // no way to set the storage
@@ -1712,7 +1821,8 @@ geniCodeCast (sym_link * type, operand * op, bool implicit)
   /* preserve the storage class & output class */
   /* of the original variable                  */
   restype = getSpec (operandType (IC_RESULT (ic)));
-  SPEC_SCLS (restype) = SPEC_SCLS (opetype);
+  if (!IS_LITERAL(opetype))
+      SPEC_SCLS (restype) = SPEC_SCLS (opetype);
   SPEC_OCLS (restype) = SPEC_OCLS (opetype);
 
   ADDTOCHAIN (ic);
@@ -1822,10 +1932,12 @@ geniCodeDivision (operand * left, operand * right)
 
   resType = usualBinaryConversions (&left, &right);
 
-  /* if the right is a literal & power of 2 */
-  /* then make it a right shift             */
+  /* if the right is a literal & power of 2 
+     and left is unsigned then make it a    
+     right shift */
   if (IS_LITERAL (retype) &&
       !IS_FLOAT (letype) &&
+      SPEC_USIGN(letype) &&
       (p2 = powof2 ((unsigned long)
                    floatFromVal (right->operand.valOperand)))) {
     ic = newiCode (RIGHT_OP, left, operandFromLit (p2)); /* right shift */
@@ -1895,6 +2007,11 @@ geniCodePtrPtrSubtract (operand * left, operand * right)
   ADDTOCHAIN (ic);
 
 subtractExit:
+  if (IS_VOID(ltype->next) || IS_VOID(rtype->next)) {
+    return result;
+  }
+
+  // should we really do this? is this ANSI?
   return geniCodeDivision (result,
                           operandFromLit (getSize (ltype->next)));
 }
@@ -1951,7 +2068,7 @@ geniCodeSubtract (operand * left, operand * right)
 /* geniCodeAdd - generates iCode for addition                      */
 /*-----------------------------------------------------------------*/
 operand *
-geniCodeAdd (operand * left, operand * right,int lvl)
+geniCodeAdd (operand * left, operand * right, int lvl)
 {
   iCode *ic;
   sym_link *resType;
@@ -1959,9 +2076,11 @@ geniCodeAdd (operand * left, operand * right,int lvl)
   int isarray = 0;
   LRTYPE;
 
+#if 0
   /* if left is an array then array access */
   if (IS_ARRAY (ltype))
     return geniCodeArray (left, right,lvl);
+#endif
 
   /* if the right side is LITERAL zero */
   /* return the left side              */
@@ -1972,8 +2091,8 @@ geniCodeAdd (operand * left, operand * right,int lvl)
   if (IS_LITERAL (letype) && left->isLiteral && !floatFromVal (valFromType (letype)))
     return right;
 
-  /* if left is an array or pointer then size */
-  if (IS_PTR (ltype))
+  /* if left is a pointer then size */
+  if (IS_PTR (ltype) || IS_ARRAY(ltype))
     {
       isarray = left->isaddr;
       // there is no need to multiply with 1
@@ -1984,7 +2103,7 @@ geniCodeAdd (operand * left, operand * right,int lvl)
       resType = copyLinkChain (ltype);
     }
   else
-    {                          /* make them the same size */
+    { // make them the same size
       resType = usualBinaryConversions (&left, &right);
     }
 
@@ -2084,7 +2203,7 @@ geniCodeArray (operand * left, operand * right,int lvl)
        {
          left = geniCodeRValue (left, FALSE);
        }
-      return geniCodeDerefPtr (geniCodeAdd (left, right,lvl),lvl);
+      return geniCodeDerefPtr (geniCodeAdd (left, right, lvl), lvl);
     }
 
   right = geniCodeMultiply (right,
@@ -2705,10 +2824,12 @@ geniCodeSEParms (ast * parms,int lvl)
       IS_ADDRESS_OF_OP (parms->right))
     parms->right->left->lvalue = 1;
 
-  parms->opval.oprnd =
+  parms->opval.oprnd = 
     geniCodeRValue (ast2iCode (parms,lvl+1), FALSE);
-
+               
   parms->type = EX_OPERAND;
+  AST_ARGREG(parms) = parms->etype ? SPEC_ARGREG(parms->etype) :
+               SPEC_ARGREG(parms->ftype);
 }
 
 /*-----------------------------------------------------------------*/
@@ -2761,6 +2882,7 @@ geniCodeParms (ast * parms, value *argVals, int *stack,
       IFFUNC_ISBUILTIN(func->type))
     {
       ic = newiCode (SEND, pval, NULL);
+      ic->argreg = SPEC_ARGREG(parms->etype);
       ic->builtinSEND = FUNC_ISBUILTIN(func->type);
       ADDTOCHAIN (ic);
     }
@@ -2773,7 +2895,7 @@ geniCodeParms (ast * parms, value *argVals, int *stack,
          /* assign */
          operand *top = operandFromSymbol (argVals->sym);
          /* clear useDef and other bitVectors */
-         OP_USES (top) = OP_DEFS (top) = OP_SYMBOL(top)->clashes = NULL;
+         OP_USES_SET ((top), OP_DEFS_SET ((top), OP_SYMBOL(top)->clashes = NULL));
          geniCodeAssign (top, pval, 1);
        }
       else
@@ -2818,10 +2940,11 @@ geniCodeCall (operand * left, ast * parms,int lvl)
   geniCodeParms (parms, NULL, &stack, getSpec (operandType (left)), OP_SYMBOL (left),lvl);
 
   /* now call : if symbol then pcall */
-  if (IS_OP_POINTER (left) || IS_ITEMP(left))
+  if (IS_OP_POINTER (left) || IS_ITEMP(left)) {
     ic = newiCode (PCALL, left, NULL);
-  else
+  } else {
     ic = newiCode (CALL, left, NULL);
+  }
 
   type = copyLinkChain (operandType (left)->next);
   etype = getSpec (type);
@@ -2845,7 +2968,7 @@ geniCodeReceive (value * args)
   /* for all arguments that are passed in registers */
   while (args)
     {
-
+      int first = 1;
       if (IS_REGPARM (args->etype))
        {
          operand *opr = operandFromValue (args);
@@ -2860,7 +2983,6 @@ geniCodeReceive (value * args)
 
              if (IN_FARSPACE (SPEC_OCLS (sym->etype)) &&
                  options.stackAuto == 0 &&
-                 /* !TARGET_IS_DS390) */
                  (!(options.model == MODEL_FLAT24)) )
                {
                }
@@ -2876,8 +2998,12 @@ geniCodeReceive (value * args)
                }
            }
 
-         ic = newiCode (RECEIVE, NULL, NULL);
-         currFunc->recvSize = getSize (sym->etype);
+         ic = newiCode (RECEIVE, NULL, NULL);    
+         ic->argreg = SPEC_ARGREG(args->etype);
+         if (first) {
+             currFunc->recvSize = getSize (sym->type);
+             first = 0;
+         }
          IC_RESULT (ic) = opr;
          ADDTOCHAIN (ic);
        }
@@ -2914,7 +3040,7 @@ geniCodeFunctionBody (ast * tree,int lvl)
 
   /* create a proc icode */
   ic = newiCode (FUNCTION, func, NULL);
-  ic->lineno = OP_SYMBOL (func)->lineDef;
+  lineno=ic->lineno = OP_SYMBOL (func)->lineDef;
 
   ADDTOCHAIN (ic);
 
@@ -3029,7 +3155,8 @@ geniCodeJumpTable (operand * cond, value * caseVals, ast * tree)
   /* all integer numbers between the maximum & minimum must */
   /* be present , the maximum value should not exceed 255 */
   min = max = (int) floatFromVal (vch = caseVals);
-  sprintf (buffer, "_case_%d_%d",
+  SNPRINTF (buffer, sizeof(buffer), 
+           "_case_%d_%d",
           tree->values.switchVals.swNum,
           min);
   addSet (&labels, newiTempLabel (buffer));
@@ -3042,7 +3169,8 @@ geniCodeJumpTable (operand * cond, value * caseVals, ast * tree)
     {
       if (((t = (int) floatFromVal (vch)) - max) != 1)
        return 0;
-      sprintf (buffer, "_case_%d_%d",
+      SNPRINTF (buffer, sizeof(buffer), 
+               "_case_%d_%d",
               tree->values.switchVals.swNum,
               t);
       addSet (&labels, newiTempLabel (buffer));
@@ -3058,9 +3186,14 @@ geniCodeJumpTable (operand * cond, value * caseVals, ast * tree)
     return 0;
 
   if (tree->values.switchVals.swDefault)
-    sprintf (buffer, "_default_%d", tree->values.switchVals.swNum);
+    {
+       SNPRINTF (buffer, sizeof(buffer), "_default_%d", tree->values.switchVals.swNum);
+    }
   else
-    sprintf (buffer, "_swBrk_%d", tree->values.switchVals.swNum);
+    {
+       SNPRINTF (buffer, sizeof(buffer), "_swBrk_%d", tree->values.switchVals.swNum);
+    }
+    
 
   falseLabel = newiTempLabel (buffer);
 
@@ -3123,7 +3256,7 @@ geniCodeSwitch (ast * tree,int lvl)
                                        operandFromValue (caseVals),
                                        EQ_OP);
 
-      sprintf (buffer, "_case_%d_%d",
+      SNPRINTF (buffer, sizeof(buffer), "_case_%d_%d",
               tree->values.switchVals.swNum,
               (int) floatFromVal (caseVals));
       trueLabel = newiTempLabel (buffer);
@@ -3137,9 +3270,13 @@ geniCodeSwitch (ast * tree,int lvl)
 
   /* if default is present then goto break else break */
   if (tree->values.switchVals.swDefault)
-    sprintf (buffer, "_default_%d", tree->values.switchVals.swNum);
+    {
+       SNPRINTF (buffer, sizeof(buffer), "_default_%d", tree->values.switchVals.swNum);
+    }
   else
-    sprintf (buffer, "_swBrk_%d", tree->values.switchVals.swNum);
+    {
+       SNPRINTF (buffer, sizeof(buffer), "_swBrk_%d", tree->values.switchVals.swNum);
+    }
 
   falseLabel = newiTempLabel (buffer);
   geniCodeGoto (falseLabel);
@@ -3254,6 +3391,7 @@ ast2iCode (ast * tree,int lvl)
   operand *right = NULL;
   if (!tree)
     return NULL;
+
   /* set the global variables for filename & line number */
   if (tree->filename)
     filename = tree->filename;