Cosmetic fix, might help me debugging
[fw/sdcc] / src / SDCCval.c
index b5685aae5bc8ac7247b5b22836d88751312183db..4a223058e97e38a9c6dcd4fed8bbdb336310f97e 100644 (file)
@@ -304,10 +304,13 @@ symbolVal (symbol * sym)
     }
 
   if (*sym->rname)
-    sprintf (val->name, "%s", sym->rname);
+    {
+       SNPRINTF (val->name, sizeof(val->name), "%s", sym->rname);
+    }
   else
-    sprintf (val->name, "_%s", sym->name);
-
+    {
+       SNPRINTF (val->name, sizeof(val->name), "_%s", sym->name);
+    }
 
   return val;
 }
@@ -340,28 +343,27 @@ value *cheapestVal (value *val) {
     if (uval<=0xffff) {
       SPEC_LONG(val->type)=0;
       SPEC_CVAL(val->type).v_uint = uval;
-    }
-    if (uval<=0xff) {
-      SPEC_NOUN(val->type)=V_CHAR;
+      if (uval<=0xff) {
+       SPEC_NOUN(val->type)=V_CHAR;
+      }
     }
   } else { // not unsigned
     if (sval<0) {
       if (sval>=-32768) {
        SPEC_LONG(val->type)=0;
-       SPEC_CVAL(val->type).v_int = sval & 0xffff;
-      }
-      if (sval>=-128) {
-       SPEC_NOUN(val->type)=V_CHAR;
-       SPEC_CVAL(val->type).v_int &= 0xff;
+       SPEC_CVAL(val->type).v_int = sval;
+       if (sval>=-128) {
+         SPEC_NOUN(val->type)=V_CHAR;
+       }
       }
     } else { // sval>=0
       SPEC_USIGN(val->type)=1;
       if (sval<=65535) {
        SPEC_LONG(val->type)=0;
        SPEC_CVAL(val->type).v_int = sval;
-      }
-      if (sval<=255) {
-       SPEC_NOUN(val->type)=V_CHAR;
+       if (sval<=255) {
+         SPEC_NOUN(val->type)=V_CHAR;
+       }
       }
     }
   }
@@ -378,11 +380,11 @@ valueFromLit (double lit)
 
   if ((((long) lit) - lit) == 0)
     {
-      sprintf (buffer, "%ld", (long) lit);
+      SNPRINTF (buffer, sizeof(buffer), "%ld", (long) lit);
       return constVal (buffer);
     }
 
-  sprintf (buffer, "%f", lit);
+  SNPRINTF (buffer, sizeof(buffer), "%f", lit);
   return constFloatVal (buffer);
 }
 
@@ -419,7 +421,7 @@ value *constVal (char *s)
   short hex = 0, octal = 0;
   char scanFmt[10];
   int scI = 0;
-  long sval;
+  double dval;
 
   val = newValue ();           /* alloc space for value   */
 
@@ -446,11 +448,17 @@ value *constVal (char *s)
   else if (hex)
     scanFmt[scI++] = 'x';
   else
-    scanFmt[scI++] = 'd';
+    scanFmt[scI++] = 'f';
 
   scanFmt[scI++] = '\0';
 
-  sscanf (s, scanFmt, &sval);
+  if (octal || hex) {
+    unsigned long sval;
+    sscanf (s, scanFmt, &sval);
+    dval=sval;
+  } else {
+    sscanf (s, scanFmt, &dval);
+  }
 
   /* Setup the flags first */
   /* set the _long flag if 'lL' is found */
@@ -459,19 +467,19 @@ value *constVal (char *s)
     SPEC_LONG (val->type) = 1;
   }
 
-  if (sval<0) { // "-28u" will still be signed and negative
+  if (dval<0) { // "-28u" will still be signed and negative
     SPEC_USIGN (val->type) = 0;
-    if (sval<-128) { // check if we have to promote to int
+    if (dval<-128) { // check if we have to promote to int
       SPEC_NOUN (val->type) = V_INT;
     }
-    if (sval<-32768) { // check if we have to promote to long int
+    if (dval<-32768) { // check if we have to promote to long int
       SPEC_LONG (val->type) = 1;
     }
   } else { // >=0
-    if (sval>0xff) { // check if we have to promote to int
+    if (dval>0xff) { // check if we have to promote to int
       SPEC_NOUN (val->type) = V_INT;
     }
-    if (sval>0xffff) { // check if we have to promote to long int
+    if (dval>0xffff) { // check if we have to promote to long int
       SPEC_LONG (val->type) = 1;
     }
   }
@@ -480,22 +488,22 @@ value *constVal (char *s)
     {
       if (SPEC_USIGN (val->type))
         {
-          SPEC_CVAL (val->type).v_ulong = sval;
+          SPEC_CVAL (val->type).v_ulong = dval;
         }
       else
         {
-          SPEC_CVAL (val->type).v_long = sval;
+          SPEC_CVAL (val->type).v_long = dval;
         }
     }
   else
     {
       if (SPEC_USIGN (val->type))
         {
-          SPEC_CVAL (val->type).v_uint = sval;
+          SPEC_CVAL (val->type).v_uint = dval;
         }
       else
         {
-          SPEC_CVAL (val->type).v_int = sval;
+          SPEC_CVAL (val->type).v_int = dval;
         }
     }
 
@@ -750,7 +758,7 @@ copyValue (value * src)
 
   dest = newValue ();
   dest->sym = copySymbol (src->sym);
-  strcpy (dest->name, src->name);
+  strncpyz (dest->name, src->name, SDCC_NAME_MAX);
   dest->type = (src->type ? copyLinkChain (src->type) : NULL);
   dest->etype = (src->type ? getSpec (dest->type) : NULL);
 
@@ -782,37 +790,37 @@ charVal (char *s)
       switch (*s)
        {
        case 'n':
-         SPEC_CVAL (val->type).v_int = '\n';
+         SPEC_CVAL (val->type).v_uint = '\n';
          break;
        case 't':
-         SPEC_CVAL (val->type).v_int = '\t';
+         SPEC_CVAL (val->type).v_uint = '\t';
          break;
        case 'v':
-         SPEC_CVAL (val->type).v_int = '\v';
+         SPEC_CVAL (val->type).v_uint = '\v';
          break;
        case 'b':
-         SPEC_CVAL (val->type).v_int = '\b';
+         SPEC_CVAL (val->type).v_uint = '\b';
          break;
        case 'r':
-         SPEC_CVAL (val->type).v_int = '\r';
+         SPEC_CVAL (val->type).v_uint = '\r';
          break;
        case 'f':
-         SPEC_CVAL (val->type).v_int = '\f';
+         SPEC_CVAL (val->type).v_uint = '\f';
          break;
        case 'a':
-         SPEC_CVAL (val->type).v_int = '\a';
+         SPEC_CVAL (val->type).v_uint = '\a';
          break;
        case '\\':
-         SPEC_CVAL (val->type).v_int = '\\';
+         SPEC_CVAL (val->type).v_uint = '\\';
          break;
        case '\?':
-         SPEC_CVAL (val->type).v_int = '\?';
+         SPEC_CVAL (val->type).v_uint = '\?';
          break;
        case '\'':
-         SPEC_CVAL (val->type).v_int = '\'';
+         SPEC_CVAL (val->type).v_uint = '\'';
          break;
        case '\"':
-         SPEC_CVAL (val->type).v_int = '\"';
+         SPEC_CVAL (val->type).v_uint = '\"';
          break;
 
        case '0' :
@@ -854,7 +862,7 @@ valFromType (sym_link * type)
 }
 
 /*------------------------------------------------------------------*/
-/* floatFromVal - value to unsinged integer conversion        */
+/* floatFromVal - value to double float conversion                  */
 /*------------------------------------------------------------------*/
 double 
 floatFromVal (value * val)
@@ -884,17 +892,32 @@ floatFromVal (value * val)
        return (double) SPEC_CVAL (val->etype).v_long;
     }
   
-  if (SPEC_NOUN(val->etype)==V_INT) {
+  if (SPEC_NOUN (val->etype) == V_INT) {
     if (SPEC_USIGN (val->etype))
       return (double) SPEC_CVAL (val->etype).v_uint;
     else
       return (double) SPEC_CVAL (val->etype).v_int;
-  } else { // SPEC_NOUN==V_CHAR
+  }
+
+  if (SPEC_NOUN (val->etype) == V_CHAR) {
     if (SPEC_USIGN (val->etype))
-      return (double) ((unsigned char)SPEC_CVAL (val->etype).v_uint);
+      return (double) (unsigned char)SPEC_CVAL (val->etype).v_uint;
     else
-      return (double) ((signed char)SPEC_CVAL (val->etype).v_int);
+      return (double) (signed char)SPEC_CVAL (val->etype).v_int;
+  }
+
+  if (IS_BITVAR(val->etype)) {
+    return (double) SPEC_CVAL (val->etype).v_uint;
+  }
+
+  if (SPEC_NOUN (val->etype) == V_VOID) {
+    return (double) SPEC_CVAL (val->etype).v_ulong;
   }
+
+  // we are lost !
+  werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
+         "floatFromVal: unknown value");
+  return 0;
 }
 
 
@@ -922,13 +945,16 @@ valUnaryPM (value * val)
            SPEC_CVAL (val->etype).v_uint = 0-SPEC_CVAL (val->etype).v_uint;
          else
            SPEC_CVAL (val->etype).v_int = -SPEC_CVAL (val->etype).v_int;
-         if (SPEC_NOUN (val->etype)==V_CHAR) {
-           SPEC_CVAL (val->etype).v_uint &= 0xff;
-         }
        }
     }
   // -(unsigned 3) now really is signed
   SPEC_USIGN(val->etype)=0;
+  // -(unsigned char)135 now really is an int
+  if (SPEC_NOUN(val->etype) == V_CHAR) {
+    if (SPEC_CVAL(val->etype).v_int < -128) {
+      SPEC_NOUN(val->etype) = V_INT;
+    }
+  }
   return val;
 }
 
@@ -952,10 +978,9 @@ valComplement (value * val)
        SPEC_CVAL (val->etype).v_uint = ~SPEC_CVAL (val->etype).v_uint;
       else
        SPEC_CVAL (val->etype).v_int = ~SPEC_CVAL (val->etype).v_int;
-      if (SPEC_NOUN (val->etype)==V_CHAR) {
-       SPEC_CVAL (val->etype).v_uint &= 0xff;
-      }
     }
+  // ~(unsigned 3) now really is signed
+  SPEC_USIGN(val->etype)=0;
   return val;
 }
 
@@ -979,9 +1004,6 @@ valNot (value * val)
        SPEC_CVAL (val->etype).v_uint = !SPEC_CVAL (val->etype).v_uint;
       else
        SPEC_CVAL (val->etype).v_int = !SPEC_CVAL (val->etype).v_int;
-      if (SPEC_NOUN (val->etype)==V_CHAR) {
-       SPEC_CVAL (val->etype).v_uint &= 0xff;
-      }
     }
   return val;
 }
@@ -1290,7 +1312,8 @@ valBitwise (value * lval, value * rval, int op)
 
   /* create a new value */
   val = newValue ();
-  val->type = copyLinkChain (lval->type);
+  val->type = copyLinkChain (getSize(rval->type) > getSize(lval->type) ?
+                            rval->type : lval->type);
   val->etype = getSpec (val->type);
 
   switch (op)
@@ -1408,34 +1431,27 @@ valCastLiteral (sym_link * dtype, double fval)
   SPEC_SCLS (val->etype) = S_LITERAL;
   /* if it is not a specifier then we can assume that */
   /* it will be an unsigned long                      */
-  if (!IS_SPEC (val->type))
-    {
+  if (!IS_SPEC (val->type)) {
       SPEC_CVAL (val->etype).v_ulong = (unsigned long) fval;
       return val;
-    }
+  }
 
   if (SPEC_NOUN (val->etype) == V_FLOAT)
-    SPEC_CVAL (val->etype).v_float = fval;
-  else
-    {
-      if (SPEC_LONG (val->etype))
-       {
+      SPEC_CVAL (val->etype).v_float = fval;
+  else {
+      unsigned long l = fval;
+      if (SPEC_LONG (val->etype)) {
          if (SPEC_USIGN (val->etype))
-           SPEC_CVAL (val->etype).v_ulong = (unsigned long) fval;
+             SPEC_CVAL (val->etype).v_ulong = (unsigned long) l;
          else
-           SPEC_CVAL (val->etype).v_long = (long) fval;
-       }
-      else
-       {
+             SPEC_CVAL (val->etype).v_long = (long) l;
+      } else {
          if (SPEC_USIGN (val->etype))
-           SPEC_CVAL (val->etype).v_uint = (unsigned short)fval;
+             SPEC_CVAL (val->etype).v_uint = (unsigned short)l;
          else
-           SPEC_CVAL (val->etype).v_int = (short)fval;
-         if (SPEC_NOUN (val->etype)==V_CHAR) {
-           SPEC_CVAL (val->etype).v_uint &= 0xff; 
-         }
-       }
-    }
+             SPEC_CVAL (val->etype).v_int = (short)l;
+      }
+  }
   return val;
 }
 
@@ -1445,7 +1461,6 @@ valCastLiteral (sym_link * dtype, double fval)
 int 
 getNelements (sym_link * type, initList * ilist)
 {
-  sym_link *etype = getSpec (type);
   int i;
 
   if (!ilist)
@@ -1456,13 +1471,13 @@ getNelements (sym_link * type, initList * ilist)
 
   /* if type is a character array and there is only one
      (string) initialiser then get the length of the string */
-  if (IS_ARRAY (type) && IS_CHAR (etype) && !ilist->next)
+  if (IS_ARRAY (type) && IS_CHAR (type->next) && !ilist->next)
     {
       ast *iast = ilist->init.node;
       value *v = (iast->type == EX_VALUE ? iast->opval.val : NULL);
       if (!v)
        {
-         werror (W_INIT_WRONG);
+         werror (E_CONST_EXPECTED);
          return 0;
        }
 
@@ -1479,7 +1494,6 @@ getNelements (sym_link * type, initList * ilist)
       i++;
       ilist = ilist->next;
     }
-
   return i;
 }
 
@@ -1517,11 +1531,15 @@ valForArray (ast * arrExpr)
 
   val = newValue ();
   if (!lval)
-    sprintf (buffer, "%s", AST_SYMBOL (arrExpr->left)->rname);
+    {
+       SNPRINTF (buffer, sizeof(buffer), "%s", AST_SYMBOL (arrExpr->left)->rname);
+    }
   else
-    sprintf (buffer, "%s", lval->name);
+    {
+       SNPRINTF (buffer, sizeof(buffer), "%s", lval->name);
+    }
 
-  sprintf (val->name, "(%s + %d)", buffer,
+  SNPRINTF (val->name, sizeof(val->name), "(%s + %d)", buffer,
           (int) AST_LIT_VALUE (arrExpr->right) * size);
 
   val->type = newLink ();
@@ -1584,11 +1602,15 @@ valForStructElem (ast * structT, ast * elemT)
 
   val = newValue ();
   if (!lval)
-    sprintf (buffer, "%s", AST_SYMBOL (structT)->rname);
+    {
+       SNPRINTF(buffer, sizeof(buffer), "%s", AST_SYMBOL (structT)->rname);
+    }
   else
-    sprintf (buffer, "%s", lval->name);
+    {
+       SNPRINTF (buffer, sizeof(buffer), "%s", lval->name);
+    }
 
-  sprintf (val->name, "(%s + %d)", buffer,
+  SNPRINTF (val->name, sizeof(val->name), "(%s + %d)", buffer,
           (int) sym->offset);
 
   val->type = newLink ();
@@ -1628,7 +1650,7 @@ valForCastAggr (ast * aexpr, sym_link * type, ast * cnst, int op)
 
   val = newValue ();
 
-  sprintf (val->name, "(%s %c %d)",
+  SNPRINTF (val->name, sizeof(val->name), "(%s %c %d)",
           AST_SYMBOL (aexpr)->rname, op,
           getSize (type->next) * (int) AST_LIT_VALUE (cnst));
 
@@ -1651,7 +1673,7 @@ valForCastArr (ast * aexpr, sym_link * type)
 
   val = newValue ();
 
-  sprintf (val->name, "(%s)",
+  SNPRINTF (val->name, sizeof(val->name), "(%s)",
           AST_SYMBOL (aexpr)->rname);
 
   val->type = type;