* Makefile.in, configure.in, configure,
[fw/sdcc] / src / SDCCval.c
index 424d918a136cbbf871cbca8e88e735edc02e696e..de08d2a6cdb632183032f34cdc0cb97606e4e735 100644 (file)
@@ -36,7 +36,7 @@ int cNestLevel;
 /* newValue - allocates and returns a new value        */
 /*-----------------------------------------------------------------*/
 value *
-newValue ()
+newValue (void)
 {
   value *val;
 
@@ -53,12 +53,11 @@ newiList (int type, void *ilist)
 {
   initList *nilist;
 
-
   nilist = Safe_alloc (sizeof (initList));
 
   nilist->type = type;
-  nilist->lineno = lexLineno;
   nilist->filename = lexFilename;
+  nilist->lineno = lexLineno;
 
   switch (type)
     {
@@ -75,7 +74,7 @@ newiList (int type, void *ilist)
 }
 
 /*------------------------------------------------------------------*/
-/* revinit   - reverses the initial values for a value  chain        */
+/* revinit   - reverses the initial values for a value chain        */
 /*------------------------------------------------------------------*/
 initList *
 revinit (initList * val)
@@ -100,19 +99,20 @@ revinit (initList * val)
 }
 
 bool
-convertIListToConstList(initList *src, literalList **lList)
+convertIListToConstList(initList *src, literalList **lList, int size)
 {
+    int cnt = 0;
     initList    *iLoop;
     literalList *head, *last, *newL;
 
     head = last = NULL;
 
-    if (!src || src->type != INIT_DEEP)
+    if (src && src->type != INIT_DEEP)
     {
         return FALSE;
     }
 
-    iLoop =  src->init.deep;
+    iLoop = src ? src->init.deep : NULL;
 
     while (iLoop)
     {
@@ -126,14 +126,19 @@ convertIListToConstList(initList *src, literalList **lList)
             return FALSE;
         }
         iLoop = iLoop->next;
+        cnt++;
+    }
+    if (!size)
+    {
+        size = cnt;
     }
 
     /* We've now established that the initializer list contains only literal values. */
 
-    iLoop = src->init.deep;
-    while (iLoop)
+    iLoop = src ? src->init.deep : NULL;
+    while (size--)
     {
-        double val = AST_LIT_VALUE(iLoop->init.node);
+        double val = iLoop ? AST_FLOAT_VALUE(iLoop->init.node) : 0;
 
         if (last && last->literalValue == val)
         {
@@ -156,7 +161,7 @@ convertIListToConstList(initList *src, literalList **lList)
             }
             last = newL;
         }
-        iLoop = iLoop->next;
+        iLoop = iLoop ? iLoop->next : NULL;
     }
 
     if (!head)
@@ -201,7 +206,7 @@ copyLiteralList(literalList *src)
 
 
 /*------------------------------------------------------------------*/
-/* copyIlist - copy initializer list            */
+/* copyIlist - copy initializer list                                */
 /*------------------------------------------------------------------*/
 initList *
 copyIlist (initList * src)
@@ -262,6 +267,8 @@ list2val (initList * val)
 ast *
 list2expr (initList * ilist)
 {
+  if (!ilist)
+    return NULL;
   if (ilist->type == INIT_DEEP)
     return list2expr (ilist->init.deep);
   return ilist->init.node;
@@ -639,7 +646,7 @@ valueFromLit (double lit)
 /* constFloatVal - converts a FLOAT constant to value              */
 /*-----------------------------------------------------------------*/
 value *
-constFloatVal (char *s)
+constFloatVal (const char *s)
 {
   value *val = newValue ();
   double sval;
@@ -649,7 +656,7 @@ constFloatVal (char *s)
   if (p == s)
     {
       werror (E_INVALID_FLOAT_CONST, s);
-      return constVal ("0");
+      return constCharVal (0);
     }
 
   val->type = val->etype = newLink (SPECIFIER);
@@ -664,7 +671,7 @@ constFloatVal (char *s)
 /* constFixed16x16Val - converts a FIXED16X16 constant to value    */
 /*-----------------------------------------------------------------*/
 value *
-constFixed16x16Val (char *s)
+constFixed16x16Val (const char *s)
 {
   value *val = newValue ();
   double sval;
@@ -674,7 +681,7 @@ constFixed16x16Val (char *s)
   if (p == s)
     {
       werror (E_INVALID_FLOAT_CONST, s);
-      return constVal ("0");
+      return constCharVal (0);
     }
 
   val->type = val->etype = newLink (SPECIFIER);
@@ -691,95 +698,115 @@ constFixed16x16Val (char *s)
 value *constVal (const char *s)
 {
   value *val;
-  short hex = 0, octal = 0;
+  char *p;
   double dval;
+  bool is_integral = 0;
 
   val = newValue ();            /* alloc space for value   */
 
-  val->type = val->etype = newLink (SPECIFIER); /* create the spcifier */
+  val->type = val->etype = newLink (SPECIFIER); /* create the specifier */
   SPEC_SCLS (val->type) = S_LITERAL;
   /* let's start with a signed char */
   SPEC_NOUN (val->type) = V_CHAR;
   SPEC_USIGN (val->type) = 0;
 
-  hex = ((strchr (s, 'x') || strchr (s, 'X')) ? 1 : 0);
-
-  /* set the octal flag   */
-  if (!hex && *s == '0' && *(s + 1))
-    octal = 1;
-
   errno = 0;
-  if (hex || octal) {
-    unsigned long sval;
-    sval = strtoul (s, NULL, 0);
-    dval = sval;
-    if (errno) {
+  if (s[0] == '0')
+    {
+      if (s[1] == 'b' || s[1] == 'B')
+        dval = strtoul (s + 2, &p, 2);
+      else
+        dval = strtoul (s, &p, 0);
+      is_integral = 1;
+    }
+  else
+    dval = strtod (s, &p);
+
+  if (errno)
+    {
       dval = 4294967295.0;
       werror (W_INVALID_INT_CONST, s, dval);
     }
-  } else {
-    dval = strtod(s, NULL);
-  }
 
   /* Setup the flags first */
   /* set the unsigned flag if 'uU' is found */
-  if (strchr (s, 'u') || strchr (s, 'U')) {
-    SPEC_USIGN (val->type) = 1;
-  }
+  if (strchr (p, 'u') || strchr (p, 'U'))
+    {
+      SPEC_USIGN (val->type) = 1;
+    }
 
   /* set the b_long flag if 'lL' is found */
-  if (strchr (s, 'l') || strchr (s, 'L')) {
-    SPEC_NOUN (val->type) = V_INT;
-    SPEC_LONG (val->type) = 1;
-  } else {
-    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;
-      }
-    } else { /* >=0 */
-      if (dval>0xff ||  /* check if we have to promote to int */
-          SPEC_USIGN (val->type)) { /* if it's unsigned, we can't use unsigned
-                                     char. After an integral promotion it will
-                                     be a signed int; this certainly isn't what
-                                     the programer wants */
-        SPEC_NOUN (val->type) = V_INT;
-      }
-      else { /* store char's always as unsigned; this helps other optimizations */
-        SPEC_USIGN (val->type) = 1;
-      }
-      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 */
-        if ((hex || octal) && /* hex or octal constants may be stored in unsigned type */
-            dval<=0xffff) {
-          SPEC_USIGN (val->type) = 1;
-        } else {
-          SPEC_LONG (val->type) = 1;
-          if (dval>0x7fffffff) {
-            SPEC_USIGN (val->type) = 1;
-          }
+  if (strchr (p, 'l') || strchr (p, 'L'))
+    {
+      SPEC_NOUN (val->type) = V_INT;
+      SPEC_LONG (val->type) = 1;
+    }
+  else
+    {
+      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;
+            }
+        }
+      else
+        { /* >=0 */
+          if (dval > 0xff ||  /* check if we have to promote to int */
+            SPEC_USIGN (val->type))
+            { /* if it's unsigned, we can't use unsigned
+                 char. After an integral promotion it will
+                 be a signed int; this certainly isn't what
+                 the programer wants */
+              SPEC_NOUN (val->type) = V_INT;
+            }
+          else
+            { /* store char's always as unsigned; this helps other optimizations */
+              SPEC_USIGN (val->type) = 1;
+            }
+          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 */
+              if (is_integral && /* integral (hex, octal and binary)  constants may be stored in unsigned type */
+                dval <= 0xffff)
+                {
+                  SPEC_USIGN (val->type) = 1;
+                }
+              else
+                {
+                  SPEC_LONG (val->type) = 1;
+                  if (dval > 0x7fffffff)
+                    {
+                      SPEC_USIGN (val->type) = 1;
+                    }
+                }
+            }
         }
-      }
     }
-  }
 
   /* check for out of range */
-  if (dval<-2147483648.0) {
-    dval = -2147483648.0;
-    werror (W_INVALID_INT_CONST, s, dval);
-  }
-  if (dval>2147483647.0 && !SPEC_USIGN (val->type)) {
-    dval = 2147483647.0;
-    werror (W_INVALID_INT_CONST, s, dval);
-  }
-  if (dval>4294967295.0) {
-    dval = 4294967295.0;
-    werror (W_INVALID_INT_CONST, s, dval);
-  }
+  if (dval < -2147483648.0)
+    {
+      dval = -2147483648.0;
+      werror (W_INVALID_INT_CONST, s, dval);
+    }
+  if (dval > 2147483647.0 && !SPEC_USIGN (val->type))
+    {
+      dval = 2147483647.0;
+      werror (W_INVALID_INT_CONST, s, dval);
+    }
+  if (dval > 4294967295.0)
+    {
+      dval = 4294967295.0;
+      werror (W_INVALID_INT_CONST, s, dval);
+    }
 
   if (SPEC_LONG (val->type))
     {
@@ -807,8 +834,22 @@ value *constVal (const char *s)
   return val;
 }
 
+value *constCharVal (unsigned char v)
+{
+  value *val = newValue ();            /* alloc space for value   */
+
+  val->type = val->etype = newLink (SPECIFIER); /* create the specifier */
+  SPEC_SCLS (val->type) = S_LITERAL;
+  /* let's start with a signed char */
+  SPEC_NOUN (val->type) = V_CHAR;
+  SPEC_USIGN (val->type) = 1;
+  SPEC_CVAL (val->type).v_uint = v;
+
+  return val;
+}
+
 /*------------------------------------------------------------------*/
-/* strVal - converts a string constant to a value       */
+/* strVal - converts a string constant to a value                   */
 /*------------------------------------------------------------------*/
 value *
 strVal (const char *s)
@@ -918,60 +959,39 @@ copyValue (value * src)
 }
 
 /*------------------------------------------------------------------*/
-/* charVal - converts a character constant to a value       */
+/* charVal - converts a character constant to a value               */
 /*------------------------------------------------------------------*/
 value *
 charVal (const char *s)
 {
-  value *val;
-
-  val = newValue ();
-
-  val->type = val->etype = newLink (SPECIFIER);
-  SPEC_NOUN (val->type) = V_CHAR;
-  SPEC_USIGN(val->type) = 1;
-  SPEC_SCLS (val->type) = S_LITERAL;
-
-  s++;                          /* get rid of quotation */
+  /* get rid of quotation */
   /* if \ then special processing */
-  if (*s == '\\')
+  if (*++s == '\\')
     {
-      s++;                      /* go beyond the backslash  */
-      switch (*s)
+      switch (*++s)             /* go beyond the backslash  */
         {
         case 'n':
-          SPEC_CVAL (val->type).v_uint = '\n';
-          break;
+          return constCharVal ('\n');
         case 't':
-          SPEC_CVAL (val->type).v_uint = '\t';
-          break;
+          return constCharVal ('\t');
         case 'v':
-          SPEC_CVAL (val->type).v_uint = '\v';
-          break;
+          return constCharVal ('\v');
         case 'b':
-          SPEC_CVAL (val->type).v_uint = '\b';
-          break;
+          return constCharVal ('\b');
         case 'r':
-          SPEC_CVAL (val->type).v_uint = '\r';
-          break;
+          return constCharVal ('\r');
         case 'f':
-          SPEC_CVAL (val->type).v_uint = '\f';
-          break;
+          return constCharVal ('\f');
         case 'a':
-          SPEC_CVAL (val->type).v_uint = '\a';
-          break;
+          return constCharVal ('\a');
         case '\\':
-          SPEC_CVAL (val->type).v_uint = '\\';
-          break;
+          return constCharVal ('\\');
         case '\?':
-          SPEC_CVAL (val->type).v_uint = '\?';
-          break;
+          return constCharVal ('\?');
         case '\'':
-          SPEC_CVAL (val->type).v_uint = '\'';
-          break;
+          return constCharVal ('\'');
         case '\"':
-          SPEC_CVAL (val->type).v_uint = '\"';
-          break;
+          return constCharVal ('\"');
 
         case '0' :
         case '1' :
@@ -981,22 +1001,17 @@ charVal (const char *s)
         case '5' :
         case '6' :
         case '7' :
-          SPEC_CVAL (val->type).v_uint = octalEscape(&s);
-          break;
+          return constCharVal (octalEscape (&s));
 
         case 'x':
-          SPEC_CVAL (val->type).v_uint = hexEscape(&s) ;
-          break;
+          return constCharVal (hexEscape (&s));
 
         default:
-          SPEC_CVAL (val->type).v_uint = (unsigned char)*s;
-          break;
+           return constCharVal (*s);
         }
     }
   else                          /* not a backslash */
-    SPEC_CVAL (val->type).v_uint = (unsigned char)*s;
-
-  return val;
+    return constCharVal (*s);
 }
 
 /*------------------------------------------------------------------*/
@@ -1143,14 +1158,14 @@ double doubleFromFixed16x16(TYPE_TARGET_ULONG value)
   double tmp=0, exp=2;
 
     tmp = (value & 0xffff0000) >> 16;
-    
+
     while(value) {
       value &= 0xffff;
       if(value & 0x8000)tmp += 1/exp;
       exp *= 2;
       value <<= 1;
     }
-  
+
   return (tmp);
 #else
   return ((double)(value * 1.0) / (double)(1UL << 16));
@@ -1167,14 +1182,14 @@ TYPE_TARGET_ULONG fixed16x16FromDouble(double value)
     tmp = floor( value );
     res = tmp << 16;
     value -= tmp;
-    
+
     tmp = 0;
     while(pos--) {
       value *= 2;
       if(value >= 1.0)tmp |= (1 << pos);
       value -= floor( value );
     }
-  
+
     res |= tmp;
 
   return (res);
@@ -1355,8 +1370,7 @@ valDiv (value * lval, value * rval)
 
   if (IS_FLOAT (val->type))
     SPEC_CVAL (val->type).v_float = floatFromVal (lval) / floatFromVal (rval);
-  else
-  if (IS_FIXED16X16 (val->type))
+  else if (IS_FIXED16X16 (val->type))
     SPEC_CVAL (val->type).v_fixed16x16 = fixed16x16FromDouble( floatFromVal (lval) / floatFromVal (rval) );
   else if (SPEC_LONG (val->type))
     {
@@ -1943,7 +1957,7 @@ valForArray (ast * arrExpr)
     }
 
   SNPRINTF (val->name, sizeof(val->name), "(%s + %d)", buffer,
-           (int) AST_LIT_VALUE (arrExpr->right) * size);
+           AST_ULONG_VALUE (arrExpr->right) * size);
 
   val->type = newLink (DECLARATOR);
   if (SPEC_SCLS (arrExpr->left->etype) == S_CODE)
@@ -2049,7 +2063,7 @@ valForCastAggr (ast * aexpr, sym_link * type, ast * cnst, int op)
 
   SNPRINTF (val->name, sizeof(val->name), "(%s %c %d)",
            AST_SYMBOL (aexpr)->rname, op,
-           getSize (type->next) * (int) AST_LIT_VALUE (cnst));
+           getSize (type->next) * AST_ULONG_VALUE (cnst));
 
   val->type = type;
   val->etype = getSpec (val->type);