fixed the initialized array of structures
[fw/sdcc] / src / SDCCval.c
index c61a6c8c19af9525e8c2412ef7a9ba2692ea919d..d893ccba608a3a902fbe64b934858e0cd78eb62e 100644 (file)
@@ -348,10 +348,9 @@ value *cheapestVal (value *val) {
     if (sval<0) {
       if (sval>=-32768) {
        SPEC_LONG(val->type)=0;
-       SPEC_CVAL(val->type).v_int = sval & 0xffff;
+       SPEC_CVAL(val->type).v_int = sval;
        if (sval>=-128) {
          SPEC_NOUN(val->type)=V_CHAR;
-         SPEC_CVAL(val->type).v_int &= 0xff;
        }
       }
     } else { // sval>=0
@@ -890,17 +889,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_ulong;
+  }
+
+  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;
 }
 
 
@@ -928,13 +942,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;
 }
 
@@ -958,10 +975,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;
 }
 
@@ -985,9 +1001,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;
 }
@@ -1414,34 +1427,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;
 }
 
@@ -1451,7 +1457,6 @@ valCastLiteral (sym_link * dtype, double fval)
 int 
 getNelements (sym_link * type, initList * ilist)
 {
-  sym_link *etype = getSpec (type);
   int i;
 
   if (!ilist)
@@ -1462,13 +1467,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;
        }
 
@@ -1485,7 +1490,6 @@ getNelements (sym_link * type, initList * ilist)
       i++;
       ilist = ilist->next;
     }
-
   return i;
 }