Fixed some warnings in MSVC build.
[fw/sdcc] / src / SDCCval.c
index c540a4fe1ec2c40637d449b08293ff80c326bcc6..82eec2db056e6aa05f6ba6c94e0362ab1f923fcd 100644 (file)
@@ -39,7 +39,7 @@ newValue ()
 {
   value *val;
 
-  val = Safe_calloc (1, sizeof (value));
+  val = Safe_alloc (sizeof (value));
 
   return val;
 }
@@ -53,10 +53,10 @@ newiList (int type, void *ilist)
   initList *nilist;
 
 
-  nilist = Safe_calloc (1, sizeof (initList));
+  nilist = Safe_alloc (sizeof (initList));
 
   nilist->type = type;
-  nilist->lineno = yylineno;
+  nilist->lineno = mylineno;
 
   switch (type)
     {
@@ -139,7 +139,7 @@ convertIListToConstList(initList *src, literalList **lList)
        }
        else
        {
-           newL = Safe_malloc(sizeof(literalList));
+           newL = Safe_alloc(sizeof(literalList));
            newL->literalValue = val;
            newL->count = 1;
            newL->next = NULL;
@@ -175,7 +175,7 @@ copyLiteralList(literalList *src)
     
     while (src)
     {
-       newL = Safe_malloc(sizeof(literalList));
+       newL = Safe_alloc(sizeof(literalList));
        
        newL->literalValue = src->literalValue;
        newL->count = src->count;
@@ -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;
 }
@@ -316,8 +319,8 @@ symbolVal (symbol * sym)
 /* cheapestVal - convert a val to the cheapest as possible value      */
 /*--------------------------------------------------------------------*/
 value *cheapestVal (value *val) {
-  long sval=0;
-  unsigned long uval=0;
+  TYPE_DWORD  sval=0;
+  TYPE_UDWORD uval=0;
 
   if (IS_FLOAT(val->type) || IS_CHAR(val->type))
     return val;
@@ -337,31 +340,29 @@ value *cheapestVal (value *val) {
   }
 
   if (SPEC_USIGN(val->type)) {
-    if (uval<=0xff) {
-      SPEC_NOUN(val->type)=V_CHAR;
+    if (uval<=0xffff) {
       SPEC_LONG(val->type)=0;
-    } else {
-      if (uval<=0xffff) {
-       SPEC_LONG(val->type)=0;
+      SPEC_CVAL(val->type).v_uint = (TYPE_UWORD)uval;
+      if (uval<=0xff) {
+       SPEC_NOUN(val->type)=V_CHAR;
       }
     }
-  } else {
+  } else { // not unsigned
     if (sval<0) {
-      if (sval>=-128) {
-       SPEC_NOUN(val->type)=V_CHAR;
+      if (sval>=-32768) {
        SPEC_LONG(val->type)=0;
-      } else {
-       if (sval>=-32768) {
-         SPEC_LONG(val->type)=0;
+       SPEC_CVAL(val->type).v_int = (TYPE_WORD)sval;
+       if (sval>=-128) {
+         SPEC_NOUN(val->type)=V_CHAR;
        }
       }
-    } else {
-      if (sval<=127) {
-       SPEC_NOUN(val->type)=V_CHAR;
+    } else { // sval>=0
+      SPEC_USIGN(val->type)=1;
+      if (sval<=65535) {
        SPEC_LONG(val->type)=0;
-      } else {
-       if (sval<=32767) {
-         SPEC_LONG(val->type)=0;
+       SPEC_CVAL(val->type).v_int = (TYPE_WORD)sval;
+       if (sval<=255) {
+         SPEC_NOUN(val->type)=V_CHAR;
        }
       }
     }
@@ -377,13 +378,13 @@ valueFromLit (double lit)
 {
   char buffer[50];
 
-  if ((((long) lit) - lit) == 0)
+  if ((((TYPE_DWORD) lit) - lit) == 0)
     {
-      sprintf (buffer, "%ld", (long) lit);
+      SNPRINTF (buffer, sizeof(buffer), "%d", (TYPE_DWORD) lit);
       return constVal (buffer);
     }
 
-  sprintf (buffer, "%f", lit);
+  SNPRINTF (buffer, sizeof(buffer), "%f", lit);
   return constFloatVal (buffer);
 }
 
@@ -394,16 +395,15 @@ value *
 constFloatVal (char *s)
 {
   value *val = newValue ();
-  float sval;
+  double sval;
 
-  if (sscanf (s, "%f", &sval) != 1)
+  if (sscanf (s, "%lf", &sval) != 1)
     {
       werror (E_INVALID_FLOAT_CONST, s);
       return constVal ("0");
     }
 
-  val->type = val->etype = newLink ();
-  val->type->class = SPECIFIER;
+  val->type = val->etype = newLink (SPECIFIER);
   SPEC_NOUN (val->type) = V_FLOAT;
   SPEC_SCLS (val->type) = S_LITERAL;
   SPEC_CVAL (val->type).v_float = sval;
@@ -420,20 +420,15 @@ value *constVal (char *s)
   short hex = 0, octal = 0;
   char scanFmt[10];
   int scI = 0;
-  unsigned long sval;
+  double dval;
 
   val = newValue ();           /* alloc space for value   */
 
-  val->type = val->etype = newLink (); /* create the spcifier */
-  val->type->class = SPECIFIER;
+  val->type = val->etype = newLink (SPECIFIER);        /* create the spcifier */
   SPEC_SCLS (val->type) = S_LITERAL;
   // let's start with an unsigned char
   SPEC_NOUN (val->type) = V_CHAR;
-  SPEC_USIGN (val->type) = 1;
-
-  /* set the _long flag if 'lL' is found */
-  if (strchr (s, 'l') || strchr (s, 'L'))
-    SPEC_LONG (val->type) = 1;
+  SPEC_USIGN (val->type) = 0;
 
   hex = ((strchr (s, 'x') || strchr (s, 'X')) ? 1 : 0);
 
@@ -444,43 +439,83 @@ value *constVal (char *s)
   /* create the scan string */
   scanFmt[scI++] = '%';
 
+  scanFmt[scI++] = 'l';
+
   if (octal)
     scanFmt[scI++] = 'o';
   else if (hex)
     scanFmt[scI++] = 'x';
   else
-    scanFmt[scI++] = 'd';
+    scanFmt[scI++] = 'f';
 
-  scanFmt[scI++] = 'L';
   scanFmt[scI++] = '\0';
 
-  sscanf (s, scanFmt, &sval);
+  if (octal || hex) {
+    unsigned long sval;
+    sscanf (s, scanFmt, &sval);
+    dval=sval;
+    SPEC_USIGN (val->type) = 1;
+  } else {
+    sscanf (s, scanFmt, &dval);
+  }
+
+  /* Setup the flags first */
+  /* set the _long flag if 'lL' is found */
+  if (strchr (s, 'l') || strchr (s, 'L')) {
+    SPEC_NOUN (val->type) = V_INT;
+    SPEC_LONG (val->type) = 1;
+  }
+
+  /* set the unsigned flag if 'uU' is found */
+  if (strchr (s, 'u') || strchr (s, 'U')) {
+    SPEC_USIGN (val->type) = 1;
+  }
 
-  if (sval<0) { // "-28u" will still be signed and negative
-    SPEC_USIGN (val->type) = 0;
-    if (sval<-32768) { // check if we have to promote to long
+  if (dval<0) { // "-28u" will still be signed and negative
+    if (dval<-128) { // check if we have to promote to int
       SPEC_NOUN (val->type) = V_INT;
+    }
+    if (dval<-32768) { // check if we have to promote to long int
       SPEC_LONG (val->type) = 1;
-      SPEC_CVAL (val->type).v_long=sval;
-    } else {
-      SPEC_CVAL (val->type).v_int=sval;
-      if (sval<-128) { // check if we have to promote to int
-       SPEC_NOUN (val->type) = V_INT;
-      }
     }
-  } else {
-    if (sval>0xffff) { // check if we have to promote to long
+  } else { // >=0
+    if (dval>0xff && SPEC_USIGN (val->type)) { // check if we have to promote to int
       SPEC_NOUN (val->type) = V_INT;
+    }
+    else if (dval>0x7f && !SPEC_USIGN (val->type)) { // check if we have to promote to int
+      SPEC_NOUN (val->type) = V_INT;
+    }
+    if (dval>0xffff && SPEC_USIGN (val->type)) { // check if we have to promote to long
+      SPEC_LONG (val->type) = 1;
+    }
+    else if (dval>0x7fff && !SPEC_USIGN (val->type)) { // check if we have to promote to long int
       SPEC_LONG (val->type) = 1;
-      SPEC_CVAL (val->type).v_ulong=sval;
-    } else {
-      SPEC_CVAL (val->type).v_uint=sval;
-      if (sval>0xff) { // check if we have to promote to int
-       SPEC_NOUN (val->type) = V_INT;
-      }
     }
   }
 
+  if (SPEC_LONG (val->type))
+    {
+      if (SPEC_USIGN (val->type))
+        {
+          SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD)dval;
+        }
+      else
+        {
+          SPEC_CVAL (val->type).v_long = (TYPE_DWORD)dval;
+        }
+    }
+  else
+    {
+      if (SPEC_USIGN (val->type))
+        {
+          SPEC_CVAL (val->type).v_uint = (TYPE_UWORD)dval;
+        }
+      else
+        {
+          SPEC_CVAL (val->type).v_int = (TYPE_WORD)dval;
+        }
+    }
+
   return val;
 }
 
@@ -489,40 +524,34 @@ value *constVal (char *s)
     /param src Pointer to 'x' from start of hex character value
 */
 
-char hexEscape(char **src)
-
+unsigned char hexEscape(char **src)
 {
-char *s ;
-unsigned long value ;
-
-(*src)++ ;     /* Skip over the 'x' */
-s = *src ;     /* Save for error detection */
-
-value = strtol (*src, src, 16);
-
-if (s == *src) 
-  {
-  // no valid hex found
-  werror(E_INVALID_HEX);
-  } 
-
-else 
-  {
-  if (value > 255) 
-    {
-    werror(W_ESC_SEQ_OOR_FOR_CHAR);
+  char *s ;
+  unsigned long value ;
+  
+  (*src)++ ;   /* Skip over the 'x' */
+  s = *src ;   /* Save for error detection */
+  
+  value = strtol (*src, src, 16);
+  
+  if (s == *src) {
+      // no valid hex found
+      werror(E_INVALID_HEX);
+  } else {
+    if (value > 255) {
+      werror(W_ESC_SEQ_OOR_FOR_CHAR);
     }
   }
-
-return (char) value;
+  return (char) value;
 }
+
 /*------------------------------------------------------------------*/
 /* octalEscape - process an octal constant of max three digits      */
 /* return the octal value, throw a warning for illegal octal        */
 /* adjust src to point at the last proccesed char                   */
 /*------------------------------------------------------------------*/
 
-char octalEscape (char **str) {
+unsigned char octalEscape (char **str) {
   int digits;
   unsigned value=0;
 
@@ -645,14 +674,13 @@ strVal (char *s)
   val = newValue ();           /* get a new one */
 
   /* get a declarator */
-  val->type = newLink ();
+  val->type = newLink (DECLARATOR);
   DCL_TYPE (val->type) = ARRAY;
-  val->type->next = val->etype = newLink ();
-  val->etype->class = SPECIFIER;
+  val->type->next = val->etype = newLink (SPECIFIER);
   SPEC_NOUN (val->etype) = V_CHAR;
   SPEC_SCLS (val->etype) = S_LITERAL;
 
-  SPEC_CVAL (val->etype).v_char = Safe_calloc (1, strlen (s) + 1);
+  SPEC_CVAL (val->etype).v_char = Safe_alloc (strlen (s) + 1);
   DCL_ELEM (val->type) = copyStr (SPEC_CVAL (val->etype).v_char, s);
 
   return val;
@@ -738,7 +766,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);
 
@@ -752,12 +780,10 @@ value *
 charVal (char *s)
 {
   value *val;
-//  unsigned uValue ;
 
   val = newValue ();
 
-  val->type = val->etype = newLink ();
-  val->type->class = SPECIFIER;
+  val->type = val->etype = newLink (SPECIFIER);
   SPEC_NOUN (val->type) = V_CHAR;
   SPEC_USIGN(val->type) = 1;
   SPEC_SCLS (val->type) = S_LITERAL;
@@ -770,37 +796,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' :
@@ -811,20 +837,20 @@ charVal (char *s)
        case '5' :
        case '6' :
        case '7' :
-         SPEC_CVAL (val->type).v_int = octalEscape(&s);
+         SPEC_CVAL (val->type).v_uint = octalEscape(&s);
          break;
 
        case 'x':
-         SPEC_CVAL (val->type).v_int = hexEscape(&s) ;
+         SPEC_CVAL (val->type).v_uint = hexEscape(&s) ;
          break;
 
        default:
-         SPEC_CVAL (val->type).v_int = *s;
+         SPEC_CVAL (val->type).v_uint = (unsigned char)*s;
          break;
        }
     }
   else                         /* not a backslash */
-    SPEC_CVAL (val->type).v_int = *s;
+    SPEC_CVAL (val->type).v_uint = (unsigned char)*s;
 
   return val;
 }
@@ -842,7 +868,7 @@ valFromType (sym_link * type)
 }
 
 /*------------------------------------------------------------------*/
-/* floatFromVal - value to unsinged integer conversion        */
+/* floatFromVal - value to double float conversion                  */
 /*------------------------------------------------------------------*/
 double 
 floatFromVal (value * val)
@@ -863,23 +889,41 @@ floatFromVal (value * val)
 
   if (SPEC_NOUN (val->etype) == V_FLOAT)
     return (double) SPEC_CVAL (val->etype).v_float;
-  else
+
+  if (SPEC_LONG (val->etype))
     {
-      if (SPEC_LONG (val->etype))
-       {
-         if (SPEC_USIGN (val->etype))
-           return (double) SPEC_CVAL (val->etype).v_ulong;
-         else
-           return (double) SPEC_CVAL (val->etype).v_long;
-       }
+      if (SPEC_USIGN (val->etype))
+       return (double) SPEC_CVAL (val->etype).v_ulong;
       else
-       {
-         if (SPEC_USIGN (val->etype))
-           return (double) SPEC_CVAL (val->etype).v_uint;
-         else
-           return (double) SPEC_CVAL (val->etype).v_int;
-       }
+       return (double) SPEC_CVAL (val->etype).v_long;
     }
+  
+  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;
+  }
+
+  if (SPEC_NOUN (val->etype) == V_CHAR) {
+    if (SPEC_USIGN (val->etype))
+      return (double) (unsigned char)SPEC_CVAL (val->etype).v_uint;
+    else
+      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;
 }
 
 
@@ -909,6 +953,14 @@ valUnaryPM (value * val)
            SPEC_CVAL (val->etype).v_int = -SPEC_CVAL (val->etype).v_int;
        }
     }
+  // -(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;
 }
 
@@ -933,6 +985,8 @@ valComplement (value * val)
       else
        SPEC_CVAL (val->etype).v_int = ~SPEC_CVAL (val->etype).v_int;
     }
+  // ~(unsigned 3) now really is signed
+  SPEC_USIGN(val->etype)=0;
   return val;
 }
 
@@ -970,36 +1024,42 @@ valMult (value * lval, value * rval)
 
   /* create a new value */
   val = newValue ();
-  val->type = val->etype = newLink ();
-  val->type->class = SPECIFIER;
+  val->type = val->etype = newLink (SPECIFIER);
   SPEC_NOUN (val->type) = (IS_FLOAT (lval->etype) ||
                           IS_FLOAT (rval->etype) ? V_FLOAT : V_INT);
-  SPEC_SCLS (val->type) = S_LITERAL;   /* will remain literal */
-  SPEC_USIGN (val->type) = (SPEC_USIGN (lval->etype) & SPEC_USIGN (rval->etype));
-  SPEC_LONG (val->type) = (SPEC_LONG (lval->etype) | SPEC_LONG (rval->etype));
+  SPEC_SCLS  (val->type) = S_LITERAL;  /* will remain literal */
+  SPEC_USIGN (val->type) = (SPEC_USIGN (lval->etype) | SPEC_USIGN (rval->etype));
+  SPEC_LONG  (val->type) = (SPEC_LONG  (lval->etype) | SPEC_LONG  (rval->etype));
 
   if (IS_FLOAT (val->type))
     SPEC_CVAL (val->type).v_float = floatFromVal (lval) * floatFromVal (rval);
   else
     {
+      /* signed and unsigned mul are the same, as long as the precision of the
+         result isn't bigger than the precision of the operands. */
       if (SPEC_LONG (val->type))
-       {
-         if (SPEC_USIGN (val->type))
-           SPEC_CVAL (val->type).v_ulong = (unsigned long) floatFromVal (lval) *
-             (unsigned long) floatFromVal (rval);
-         else
-           SPEC_CVAL (val->type).v_long = (long) floatFromVal (lval) *
-             (long) floatFromVal (rval);
-       }
+        SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) *
+                                       (TYPE_UDWORD) floatFromVal (rval);
       else
-       {
-         if (SPEC_USIGN (val->type))
-           SPEC_CVAL (val->type).v_uint = (unsigned) floatFromVal (lval) *
-             (unsigned) floatFromVal (rval);
-         else
-           SPEC_CVAL (val->type).v_int = (int) floatFromVal (lval) *
-             (int) floatFromVal (rval);
-       }
+        {
+          TYPE_UDWORD ul = (TYPE_UWORD) floatFromVal (lval) *
+                           (TYPE_UWORD) floatFromVal (rval);
+          
+          SPEC_CVAL (val->type).v_uint = (TYPE_UWORD) ul;
+          if (!options.lessPedantic)
+            {
+              if (SPEC_USIGN (val->type))
+                {
+                  if (ul != SPEC_CVAL (val->type).v_uint)
+                    werror (W_INT_OVL);
+                }
+              else /* signed result */
+                {
+                  if ((TYPE_DWORD) ul != SPEC_CVAL (val->type).v_int)
+                    werror (W_INT_OVL);
+                }
+            }
+        }
     }
   return cheapestVal(val);
 }
@@ -1020,12 +1080,11 @@ valDiv (value * lval, value * rval)
 
   /* create a new value */
   val = newValue ();
-  val->type = val->etype = newLink();
-  val->type->class = SPECIFIER;
+  val->type = val->etype = newLink(SPECIFIER);
   SPEC_NOUN (val->type) = (IS_FLOAT (lval->etype) ||
                           IS_FLOAT (rval->etype) ? V_FLOAT : V_INT);
   SPEC_SCLS (val->etype) = S_LITERAL;
-  SPEC_USIGN (val->type) = (SPEC_USIGN (lval->etype) & SPEC_USIGN (rval->etype));
+  SPEC_USIGN (val->type) = (SPEC_USIGN (lval->etype) | SPEC_USIGN (rval->etype));
   SPEC_LONG (val->type) = (SPEC_LONG (lval->etype) | SPEC_LONG (rval->etype));
 
   if (IS_FLOAT (val->type))
@@ -1066,11 +1125,10 @@ valMod (value * lval, value * rval)
 
   /* create a new value */
   val = newValue ();
-  val->type = val->etype = newLink ();
-  val->type->class = SPECIFIER;
+  val->type = val->etype = newLink (SPECIFIER);
   SPEC_NOUN (val->type) = V_INT;       /* type is int */
   SPEC_SCLS (val->type) = S_LITERAL;   /* will remain literal */
-  SPEC_USIGN (val->type) = (SPEC_USIGN (lval->etype) & SPEC_USIGN (rval->etype));
+  SPEC_USIGN (val->type) = (SPEC_USIGN (lval->etype) | SPEC_USIGN (rval->etype));
   SPEC_LONG (val->type) = (SPEC_LONG (lval->etype) | SPEC_LONG (rval->etype));
 
   if (SPEC_LONG (val->type))
@@ -1106,8 +1164,7 @@ valPlus (value * lval, value * rval)
 
   /* create a new value */
   val = newValue ();
-  val->type = val->etype = newLink ();
-  val->type->class = SPECIFIER;
+  val->type = val->etype = newLink (SPECIFIER);
   SPEC_NOUN (val->type) = (IS_FLOAT (lval->etype) ||
                           IS_FLOAT (rval->etype) ? V_FLOAT : V_INT);
   SPEC_SCLS (val->type) = S_LITERAL;   /* will remain literal */
@@ -1116,7 +1173,7 @@ valPlus (value * lval, value * rval)
     SPEC_USIGN (rval->etype) &&
     (floatFromVal(lval)+floatFromVal(rval))>=0;
     
-  SPEC_LONG (val->type) = (SPEC_LONG (lval->etype) | SPEC_LONG (rval->etype));
+  SPEC_LONG (val->type) = 1;
 
   if (IS_FLOAT (val->type))
     SPEC_CVAL (val->type).v_float = floatFromVal (lval) + floatFromVal (rval);
@@ -1131,16 +1188,6 @@ valPlus (value * lval, value * rval)
            SPEC_CVAL (val->type).v_long = (long) floatFromVal (lval) +
              (long) floatFromVal (rval);
        }
-      else
-       {
-         if (SPEC_USIGN (val->type)) {
-           SPEC_CVAL (val->type).v_uint = (unsigned) floatFromVal (lval) +
-             (unsigned) floatFromVal (rval);
-         } else {
-           SPEC_CVAL (val->type).v_int = (int) floatFromVal (lval) +
-             (int) floatFromVal (rval);
-         }
-       }
     }
   return cheapestVal(val);
 }
@@ -1155,8 +1202,7 @@ valMinus (value * lval, value * rval)
 
   /* create a new value */
   val = newValue ();
-  val->type = val->etype = newLink ();
-  val->type->class = SPECIFIER;
+  val->type = val->etype = newLink (SPECIFIER);
   SPEC_NOUN (val->type) = (IS_FLOAT (lval->etype) ||
                           IS_FLOAT (rval->etype) ? V_FLOAT : V_INT);
   SPEC_SCLS (val->type) = S_LITERAL;   /* will remain literal */
@@ -1209,7 +1255,7 @@ valShift (value * lval, value * rval, int lr)
   val->type = val->etype = newIntLink ();
   SPEC_SCLS (val->type) = S_LITERAL;   /* will remain literal */
   SPEC_USIGN (val->type) = (SPEC_USIGN (lval->etype) & SPEC_USIGN (rval->etype));
-  SPEC_LONG (val->type) = (SPEC_LONG (lval->etype) | SPEC_LONG (rval->etype));
+  SPEC_LONG (val->type) = 1;
 
   if (SPEC_LONG (val->type))
     {
@@ -1222,18 +1268,6 @@ valShift (value * lval, value * rval, int lr)
          (long) floatFromVal (lval) << (long) floatFromVal (rval) : \
          (long) floatFromVal (lval) >> (long) floatFromVal (rval);
     }
-  else
-    {
-      if (SPEC_USIGN (val->type)) {
-       SPEC_CVAL (val->type).v_uint = lr ? 
-         (unsigned) floatFromVal (lval) << (unsigned) floatFromVal (rval) :\
-         (unsigned) floatFromVal (lval) >> (unsigned) floatFromVal (rval);
-      } else {
-       SPEC_CVAL (val->type).v_int = lr ?
-         (int) floatFromVal (lval) << (int) floatFromVal (rval) : \
-         (int) floatFromVal (lval) >> (int) floatFromVal (rval);
-      }
-    }
 
   return cheapestVal(val);
 }
@@ -1295,7 +1329,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)
@@ -1413,39 +1448,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 = (unsigned long)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))
-           if (SPEC_NOUN (val->etype)==V_CHAR) {
-             SPEC_CVAL (val->etype).v_uint = (unsigned char) fval;
-           } else {
-             SPEC_CVAL (val->etype).v_uint = (unsigned int) fval;
-           }
+             SPEC_CVAL (val->etype).v_uint = (unsigned short)l;
          else
-           if (SPEC_NOUN (val->etype)==V_CHAR) {
-             SPEC_CVAL (val->etype).v_int = (char) fval;
-           } else {
-             SPEC_CVAL (val->etype).v_int = (int) fval;
-           }
-       }
-    }
+             SPEC_CVAL (val->etype).v_int = (short)l;
+      }
+  }
   return val;
 }
 
@@ -1455,7 +1478,6 @@ valCastLiteral (sym_link * dtype, double fval)
 int 
 getNelements (sym_link * type, initList * ilist)
 {
-  sym_link *etype = getSpec (type);
   int i;
 
   if (!ilist)
@@ -1466,13 +1488,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;
        }
 
@@ -1489,7 +1511,6 @@ getNelements (sym_link * type, initList * ilist)
       i++;
       ilist = ilist->next;
     }
-
   return i;
 }
 
@@ -1527,14 +1548,18 @@ 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 ();
+  val->type = newLink (DECLARATOR);
   if (SPEC_SCLS (arrExpr->left->etype) == S_CODE)
     {
       DCL_TYPE (val->type) = CPOINTER;
@@ -1594,14 +1619,18 @@ 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 ();
+  val->type = newLink (DECLARATOR);
   if (SPEC_SCLS (structT->etype) == S_CODE)
     {
       DCL_TYPE (val->type) = CPOINTER;
@@ -1638,7 +1667,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));
 
@@ -1661,7 +1690,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;