constVal() now returns the cheapest val as possible
[fw/sdcc] / src / SDCCval.c
index 51214a5527be2a4db634c3f41a0bb0910261da0d..502099538decf292b522dc83eb9591cb1a2a8532 100644 (file)
@@ -97,6 +97,107 @@ revinit (initList * val)
   return prev;
 }
 
+bool
+convertIListToConstList(initList *src, literalList **lList)
+{
+    initList    *iLoop;
+    literalList *head, *last, *newL;
+    
+    head = last = NULL;
+    
+    if (!src || src->type != INIT_DEEP)
+    {
+       return FALSE;
+    }
+    
+    iLoop =  src->init.deep;
+    
+    while (iLoop)
+    {
+       if (iLoop->type != INIT_NODE)
+       {
+           return FALSE;
+       }
+       
+       if (!IS_AST_LIT_VALUE(decorateType(resolveSymbols(iLoop->init.node))))
+       {
+           return FALSE;
+       }
+       iLoop = iLoop->next;
+    }
+    
+    // We've now established that the initializer list contains only literal values.
+    
+    iLoop = src->init.deep;
+    while (iLoop)
+    {
+       double val = AST_LIT_VALUE(iLoop->init.node);
+       
+       if (last && last->literalValue == val)
+       {
+           last->count++;
+       }
+       else
+       {
+           newL = Safe_malloc(sizeof(literalList));
+           newL->literalValue = val;
+           newL->count = 1;
+           newL->next = NULL;
+           
+           if (last)
+           {
+               last->next = newL;
+           }
+           else
+           {
+               head = newL;
+           }
+           last = newL;
+       }
+       iLoop = iLoop->next;
+    }
+    
+    if (!head)    
+    {
+       return FALSE;
+    }
+    
+    *lList = head;
+    return TRUE;
+}
+
+literalList *
+copyLiteralList(literalList *src)
+{
+    literalList *head, *prev, *newL;
+    
+    head = prev = NULL;
+    
+    while (src)
+    {
+       newL = Safe_malloc(sizeof(literalList));
+       
+       newL->literalValue = src->literalValue;
+       newL->count = src->count;
+       newL->next = NULL;
+       
+       if (prev)
+       {
+           prev->next = newL;
+       }
+       else
+       {
+           head = newL;
+       }
+       prev = newL;
+       src = src->next;
+    }
+    
+    return head;
+}
+
+
+
 /*------------------------------------------------------------------*/
 /* copyIlist - copy initializer list            */
 /*------------------------------------------------------------------*/
@@ -254,10 +355,9 @@ constFloatVal (char *s)
 }
 
 /*-----------------------------------------------------------------*/
-/* constVal - converts a INTEGER constant into a value       */
+/* constVal - converts an INTEGER constant into a cheapest value   */
 /*-----------------------------------------------------------------*/
-value *
-constVal (char *s)
+value *constVal (char *s)
 {
   value *val;
   short hex = 0, octal = 0;
@@ -269,12 +369,10 @@ constVal (char *s)
 
   val->type = val->etype = newLink (); /* create the spcifier */
   val->type->class = SPECIFIER;
-  SPEC_NOUN (val->type) = V_INT;
   SPEC_SCLS (val->type) = S_LITERAL;
-
-  /* set the _unsigned flag if 'uU' found */
-  if (strchr (s, 'u') || strchr (s, 'U'))
-    SPEC_USIGN (val->type) = 1;
+  // 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'))
@@ -293,47 +391,42 @@ constVal (char *s)
     scanFmt[scI++] = 'o';
   else if (hex)
     scanFmt[scI++] = 'x';
-  else if (SPEC_USIGN (val->type))
-    scanFmt[scI++] = 'u';
   else
     scanFmt[scI++] = 'd';
 
+  scanFmt[scI++] = 'L';
   scanFmt[scI++] = '\0';
 
-  /* if hex or octal then set the unsigned flag   */
-  if (hex || octal)
-    {
-      SPEC_USIGN (val->type) = 1;
-      sscanf (s, scanFmt, &sval);
-    }
-  else
-    sval = atol (s);
-
+  sscanf (s, scanFmt, &sval);
 
-  if (SPEC_LONG (val->type) || 
-      (SPEC_USIGN(val->type) && sval>0xffff) ||
-      (!SPEC_USIGN(val->type) && ((long)sval>32767 || (long)sval<-32768)) {
-      if (SPEC_USIGN (val->type))
-       SPEC_CVAL (val->type).v_ulong = sval;
-      else
-       SPEC_CVAL (val->type).v_long = sval;
+  if (sval<0) { // "-28u" will still be signed and negative
+    SPEC_USIGN (val->type) = 0;
+    if (sval<-32768) { // check if have to promote to long
+      SPEC_NOUN (val->type) = V_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 (SPEC_USIGN (val->type))
-       SPEC_CVAL (val->type).v_uint = sval;
-      else
-       SPEC_CVAL (val->type).v_int = sval;
+  } else {
+    if (sval>0xffff) { // check if we have to promote to long
+      SPEC_NOUN (val->type) = V_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;
+      }
     }
-
-  // check the size and make it a char if possible
-  if (sval < 256)
-    SPEC_NOUN (val->etype) = V_CHAR;
+  }
 
   return val;
-
 }
+
 /*! /fn char hexEscape(char **src)
 
     /param src Pointer to 'x' from start of hex character value
@@ -609,6 +702,7 @@ charVal (char *s)
   val->type = val->etype = newLink ();
   val->type->class = SPECIFIER;
   SPEC_NOUN (val->type) = V_CHAR;
+  SPEC_USIGN(val->type) = 1;
   SPEC_SCLS (val->type) = S_LITERAL;
 
   s++;                         /* get rid of quotation */
@@ -870,10 +964,10 @@ valDiv (value * lval, value * rval)
 
   /* create a new value */
   val = newValue ();
-  val->type = val->etype = ((floatFromVal (lval) / floatFromVal (rval)) < 256 ?
-                           newCharLink () : newIntLink ());
-  if (IS_FLOAT (lval->etype) || IS_FLOAT (rval->etype))
-    SPEC_NOUN (val->etype) = V_FLOAT;
+  val->type = val->etype = newLink();
+  val->type->class = 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_LONG (val->type) = (SPEC_LONG (lval->etype) | SPEC_LONG (rval->etype));
@@ -893,12 +987,22 @@ valDiv (value * lval, value * rval)
        }
       else
        {
-         if (SPEC_USIGN (val->type))
+         if (SPEC_USIGN (val->type)) {
            SPEC_CVAL (val->type).v_uint = (unsigned) floatFromVal (lval) /
              (unsigned) floatFromVal (rval);
-         else
+           if (/* IS_CHAR (lval->etype) && IS_CHAR (rval->etype) && */
+               (SPEC_CVAL (val->type).v_uint <=255)) {
+             SPEC_NOUN (val->type) = V_CHAR;
+           }
+         } else {
            SPEC_CVAL (val->type).v_int = (int) floatFromVal (lval) /
              (int) floatFromVal (rval);
+           if (/* IS_CHAR (lval->etype) && IS_CHAR (rval->etype) && */
+               (SPEC_CVAL (val->type).v_int >=-128) &&
+               (SPEC_CVAL (val->type).v_int <=127)) {
+             SPEC_NOUN (val->type) = V_CHAR;
+           }
+         }
        }
     }
   return val;
@@ -932,12 +1036,22 @@ valMod (value * lval, value * rval)
     }
   else
     {
-      if (SPEC_USIGN (val->type))
+      if (SPEC_USIGN (val->type)) {
        SPEC_CVAL (val->type).v_uint = (unsigned) floatFromVal (lval) %
          (unsigned) floatFromVal (rval);
-      else
+       if (/* IS_CHAR (lval->etype) && IS_CHAR (rval->etype) && */
+           (SPEC_CVAL (val->type).v_uint <=255)) {
+         SPEC_NOUN (val->type) = V_CHAR;
+       }
+      } else {
        SPEC_CVAL (val->type).v_int = (unsigned) floatFromVal (lval) %
          (unsigned) floatFromVal (rval);
+       if (/* IS_CHAR (lval->etype) && IS_CHAR (rval->etype) && */
+           (SPEC_CVAL (val->type).v_int >=-128) &&
+           (SPEC_CVAL (val->type).v_int <=127)) {
+         SPEC_NOUN (val->type) = V_CHAR;
+       }
+      }
     }
 
   return val;
@@ -976,12 +1090,22 @@ valPlus (value * lval, value * rval)
        }
       else
        {
-         if (SPEC_USIGN (val->type))
+         if (SPEC_USIGN (val->type)) {
            SPEC_CVAL (val->type).v_uint = (unsigned) floatFromVal (lval) +
              (unsigned) floatFromVal (rval);
-         else
+           if (/* IS_CHAR (lval->etype) && IS_CHAR (rval->etype) && */
+               (SPEC_CVAL (val->type).v_uint <=255)) {
+             SPEC_NOUN (val->type) = V_CHAR;
+           }
+         } else {
            SPEC_CVAL (val->type).v_int = (int) floatFromVal (lval) +
              (int) floatFromVal (rval);
+           if (/* IS_CHAR (lval->etype) && IS_CHAR (rval->etype) && */
+               (SPEC_CVAL (val->type).v_int >=-128) &&
+               (SPEC_CVAL (val->type).v_int <=127)) {
+             SPEC_NOUN (val->type) = V_CHAR;
+           }
+         }
        }
     }
   return val;
@@ -1011,20 +1135,32 @@ valMinus (value * lval, value * rval)
     {
       if (SPEC_LONG (val->type))
        {
-         if (SPEC_USIGN (val->type))
-           SPEC_CVAL (val->type).v_ulong = (unsigned long) floatFromVal (lval) -
+         if (SPEC_USIGN (val->type)) {
+           SPEC_CVAL (val->type).v_ulong = 
+             (unsigned long) floatFromVal (lval) -
              (unsigned long) floatFromVal (rval);
-         else
+         } else {
            SPEC_CVAL (val->type).v_long = (long) floatFromVal (lval) -
              (long) floatFromVal (rval);
+         }
        }
       else
        {
-         if (SPEC_USIGN (val->type))
+         if (SPEC_USIGN (val->type)) {
            SPEC_CVAL (val->type).v_uint = (unsigned) floatFromVal (lval) -
              (unsigned) floatFromVal (rval);
-         else
+           if (/* IS_CHAR (lval->etype) && IS_CHAR (rval->etype) && */
+               (SPEC_CVAL (val->type).v_uint <=255)) {
+             SPEC_NOUN (val->type) = V_CHAR;
+           }
+         } else {
            SPEC_CVAL (val->type).v_int = (int) floatFromVal (lval) - (int) floatFromVal (rval);
+           if (/* IS_CHAR (lval->etype) && IS_CHAR (rval->etype) && */
+               (SPEC_CVAL (val->type).v_int >=-128) &&
+               (SPEC_CVAL (val->type).v_int <=127)) {
+             SPEC_NOUN (val->type) = V_CHAR;
+           }
+         }
        }
     }
   return val;
@@ -1040,54 +1176,42 @@ valShift (value * lval, value * rval, int lr)
 
   /* create a new value */
   val = newValue ();
-  val->type = val->etype = newLink ();
-  val->type->class = SPECIFIER;
-  SPEC_NOUN (val->type) = V_INT;       /* type is int */
+  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));
 
-  if (lr)
+  if (SPEC_LONG (val->type))
     {
-      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);
-       }
+      if (SPEC_USIGN (val->type))
+       SPEC_CVAL (val->type).v_ulong = lr ? 
+         (unsigned long) floatFromVal (lval) << (unsigned long) floatFromVal (rval) : \
+         (unsigned long) floatFromVal (lval) >> (unsigned 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);
-       }
+       SPEC_CVAL (val->type).v_long = lr ?
+         (long) floatFromVal (lval) << (long) floatFromVal (rval) : \
+         (long) floatFromVal (lval) >> (long) floatFromVal (rval);
     }
   else
     {
-      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);
+      if (SPEC_USIGN (val->type)) {
+       SPEC_CVAL (val->type).v_uint = lr ? 
+         (unsigned) floatFromVal (lval) << (unsigned) floatFromVal (rval) :\
+         (unsigned) floatFromVal (lval) >> (unsigned) floatFromVal (rval);
+       if (/* IS_CHAR (lval->etype) && IS_CHAR (rval->etype) && */
+           (SPEC_CVAL (val->type).v_uint <=255)) {
+         SPEC_NOUN (val->type) = V_CHAR;
        }
-      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);
+      } else {
+       SPEC_CVAL (val->type).v_int = lr ?
+         (int) floatFromVal (lval) << (int) floatFromVal (rval) : \
+         (int) floatFromVal (lval) >> (int) floatFromVal (rval);
+       if (/* IS_CHAR (lval->etype) && IS_CHAR (rval->etype) && */
+           (SPEC_CVAL (val->type).v_int >=-128) &&
+           (SPEC_CVAL (val->type).v_int <=127)) {
+         SPEC_NOUN (val->type) = V_CHAR;
        }
+      }
     }
 
   return val;
@@ -1310,22 +1434,22 @@ getNelements (sym_link * type, initList * ilist)
     ilist = ilist->init.deep;
 
   /* if type is a character array and there is only one
-     initialiser then get the length of the string */
+     (string) initialiser then get the length of the string */
   if (IS_ARRAY (type) && IS_CHAR (etype) && !ilist->next)
     {
       ast *iast = ilist->init.node;
       value *v = (iast->type == EX_VALUE ? iast->opval.val : NULL);
       if (!v)
        {
-         werror (E_INIT_WRONG);
+         werror (W_INIT_WRONG);
          return 0;
        }
-      if (!IS_ARRAY (v->type) || !IS_CHAR (v->etype))
+
+      if (IS_ARRAY (v->type) && IS_CHAR (v->etype))
+       // yep, it's a string
        {
-         werror (E_INIT_WRONG);
-         return 0;
+         return DCL_ELEM (v->type);
        }
-      return DCL_ELEM (v->type);
     }
 
   i = 0;