* sim/ucsim/configure.in,
[fw/sdcc] / src / SDCCval.c
index 62ffcec2a1670ff07d3443ab3fc7a03778b6d5b8..b2f8fe0e1b0cf4db8434243e7c9913409bb91067 100644 (file)
@@ -57,6 +57,7 @@ newiList (int type, void *ilist)
 
   nilist->type = type;
   nilist->lineno = mylineno;
+  nilist->filename = currFname;
 
   switch (type)
     {
@@ -118,21 +119,21 @@ convertIListToConstList(initList *src, literalList **lList)
        {
            return FALSE;
        }
-       
-       if (!IS_AST_LIT_VALUE(decorateType(resolveSymbols(iLoop->init.node))))
+
+       if (!IS_AST_LIT_VALUE(decorateType(resolveSymbols(iLoop->init.node), RESULT_CHECK)))
        {
            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++;
@@ -228,7 +229,7 @@ copyIlist (initList * src)
 /*------------------------------------------------------------------*/
 /* list2int - converts the first element of the list to value       */
 /*------------------------------------------------------------------*/
-double 
+double
 list2int (initList * val)
 {
   initList *i = val;
@@ -268,19 +269,28 @@ list2expr (initList * ilist)
 /*------------------------------------------------------------------*/
 /* resolveIvalSym - resolve symbols in initial values               */
 /*------------------------------------------------------------------*/
-void 
-resolveIvalSym (initList * ilist)
+void
+resolveIvalSym (initList * ilist, sym_link * type)
 {
+  RESULT_TYPE resultType;
+
   if (!ilist)
     return;
 
   if (ilist->type == INIT_NODE)
-    ilist->init.node = decorateType (resolveSymbols (ilist->init.node));
+    {
+      if (IS_PTR (type))
+        resultType = RESULT_TYPE_INT;
+      else
+        resultType = getResultTypeFromType (getSpec (type));
+      ilist->init.node = decorateType (resolveSymbols (ilist->init.node),
+                                      resultType);
+    }
 
   if (ilist->type == INIT_DEEP)
-    resolveIvalSym (ilist->init.deep);
+    resolveIvalSym (ilist->init.deep, type);
 
-  resolveIvalSym (ilist->next);
+  resolveIvalSym (ilist->next, type);
 }
 
 /*-----------------------------------------------------------------*/
@@ -315,6 +325,7 @@ symbolVal (symbol * sym)
   return val;
 }
 
+#if defined(REDUCE_LITERALS)
 /*--------------------------------------------------------------------*/
 /* cheapestVal - convert a val to the cheapest as possible value      */
 /*--------------------------------------------------------------------*/
@@ -369,6 +380,46 @@ static value *cheapestVal (value *val) {
   return val;
 }
 
+#else
+
+static value *cheapestVal (value *val)
+{
+  if (IS_FLOAT (val->type) || IS_CHAR (val->type))
+    return val;
+
+  /* - signed/unsigned must not be changed.
+     - long must not be changed.
+
+     the only possible reduction is from signed int to signed char,
+     because it's automatically promoted back to signed int.
+
+     a reduction from unsigned int to unsigned char is a bug,
+     because an _unsigned_ char is promoted to _signed_ int! */
+  if (IS_INT(val->type) &&
+      !SPEC_USIGN(val->type) &&
+      !SPEC_LONG(val->type) &&
+      SPEC_CVAL(val->type).v_int >= -128 &&
+      SPEC_CVAL(val->type).v_int <     0)
+
+    {
+      SPEC_NOUN(val->type) = V_CHAR;
+    }
+  /* 'unsigned char' promotes to 'signed int', so that we can
+     reduce it the other way */
+  if (IS_INT(val->type) &&
+      !SPEC_USIGN(val->type) &&
+      !SPEC_LONG(val->type) &&
+      SPEC_CVAL(val->type).v_int >=   0 &&
+      SPEC_CVAL(val->type).v_int <= 255)
+
+    {
+      SPEC_NOUN(val->type) = V_CHAR;
+      SPEC_USIGN(val->type) = 1;
+    }
+  return (val);
+}
+#endif
+
 /*-----------------------------------------------------------------*/
 /* valueFromLit - creates a value from a literal                   */
 /*-----------------------------------------------------------------*/
@@ -425,7 +476,7 @@ value *constVal (char *s)
 
   val->type = val->etype = newLink (SPECIFIER);        /* create the spcifier */
   SPEC_SCLS (val->type) = S_LITERAL;
-  // let's start with an unsigned char
+  // let's start with a signed char
   SPEC_NOUN (val->type) = V_CHAR;
   SPEC_USIGN (val->type) = 0;
 
@@ -453,7 +504,6 @@ value *constVal (char *s)
     unsigned long sval;
     sscanf (s, scanFmt, &sval);
     dval=sval;
-    SPEC_USIGN (val->type) = 1;
   } else {
     sscanf (s, scanFmt, &dval);
   }
@@ -478,17 +528,29 @@ value *constVal (char *s)
       SPEC_LONG (val->type) = 1;
     }
   } else { // >=0
-    if (dval>0xff && SPEC_USIGN (val->type)) { // check if we have to promote to int
+    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 if (dval>0x7f && !SPEC_USIGN (val->type)) { // check if we have to promote to int
-      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
-      SPEC_LONG (val->type) = 1;
+      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;
+       }
+      }
     }
   }
 
@@ -527,7 +589,7 @@ unsigned char hexEscape(char **src)
 {
   char *s ;
   unsigned long value ;
-  
+
   (*src)++ ;   /* Skip over the 'x' */
   s = *src ;   /* Save for error detection */
   
@@ -869,7 +931,7 @@ valFromType (sym_link * type)
 /*------------------------------------------------------------------*/
 /* floatFromVal - value to double float conversion                  */
 /*------------------------------------------------------------------*/
-double 
+double
 floatFromVal (value * val)
 {
   if (!val)
@@ -896,7 +958,7 @@ floatFromVal (value * val)
       else
        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;
@@ -925,7 +987,6 @@ floatFromVal (value * val)
   return 0;
 }
 
-
 /*------------------------------------------------------------------*/
 /* valUnaryPM - does the unary +/- operation on a constant          */
 /*------------------------------------------------------------------*/
@@ -1023,47 +1084,38 @@ valMult (value * lval, value * rval)
 
   /* create a new value */
   val = newValue ();
-  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));
+  val->type = val->etype = computeType (lval->etype,
+                                       rval->etype,
+                                       RESULT_TYPE_INT,
+                                       '*');
+  SPEC_SCLS (val->etype) = S_LITERAL; /* will remain literal */
 
   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))
-        SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) *
-                                       (TYPE_UDWORD) floatFromVal (rval);
-      else /* int */
-        {
-          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 */
-                {
-                  TYPE_DWORD l = (TYPE_WORD) floatFromVal (lval) *
-                                 (TYPE_WORD) floatFromVal (rval);
-
-                  if (l != SPEC_CVAL (val->type).v_int)
-                    werror (W_INT_OVL);
-                }
-            }
-        }
+  else if (SPEC_LONG (val->type))
+    SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) *
+                                   (TYPE_UDWORD) floatFromVal (rval);
+  else if (SPEC_USIGN (val->type)) /* unsigned int */
+    {
+      TYPE_UDWORD ul = (TYPE_UWORD) floatFromVal (lval) *
+                       (TYPE_UWORD) floatFromVal (rval);
+
+      SPEC_CVAL (val->type).v_uint = (TYPE_UWORD) ul;
+      if (ul != (TYPE_UWORD) ul)
+        werror (W_INT_OVL);
     }
-  return cheapestVal(val);
+  else /* signed int */
+    {
+      TYPE_DWORD l = (TYPE_WORD) floatFromVal (lval) *
+                     (TYPE_WORD) floatFromVal (rval);
+
+      SPEC_CVAL (val->type).v_int = (TYPE_WORD) l;
+      if (l != (TYPE_WORD) l)
+        werror (W_INT_OVL);
+    }
+  return cheapestVal (val);
 }
 
 /*------------------------------------------------------------------*/
@@ -1082,39 +1134,33 @@ valDiv (value * lval, value * rval)
 
   /* create a new value */
   val = newValue ();
-  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_LONG (val->type) = (SPEC_LONG (lval->etype) | SPEC_LONG (rval->etype));
+  val->type = val->etype = computeType (lval->etype,
+                                       rval->etype,
+                                       RESULT_TYPE_INT,
+                                       '/');
+  SPEC_SCLS (val->etype) = S_LITERAL; /* will remain literal */
 
   if (IS_FLOAT (val->type))
     SPEC_CVAL (val->type).v_float = floatFromVal (lval) / floatFromVal (rval);
+  else if (SPEC_LONG (val->type))
+    {
+      if (SPEC_USIGN (val->type))
+       SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) /
+         (TYPE_UDWORD) floatFromVal (rval);
+      else
+       SPEC_CVAL (val->type).v_long = (TYPE_DWORD) floatFromVal (lval) /
+         (TYPE_DWORD) 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 = (TYPE_UWORD) floatFromVal (lval) /
+          (TYPE_UWORD) 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_int = (TYPE_WORD) floatFromVal (lval) /
+          (TYPE_WORD) floatFromVal (rval);
     }
-  return cheapestVal(val);
+  return cheapestVal (val);
 }
 
 /*------------------------------------------------------------------*/
@@ -1126,34 +1172,32 @@ valMod (value * lval, value * rval)
   value *val;
 
   /* create a new value */
-  val = newValue ();
-  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_LONG (val->type) = (SPEC_LONG (lval->etype) | SPEC_LONG (rval->etype));
+  val = newValue();
+  val->type = val->etype = computeType (lval->etype,
+                                       rval->etype,
+                                       RESULT_TYPE_INT,
+                                       '%');
+  SPEC_SCLS (val->etype) = S_LITERAL; /* will remain literal */
 
   if (SPEC_LONG (val->type))
     {
       if (SPEC_USIGN (val->type))
-       SPEC_CVAL (val->type).v_ulong = (unsigned long) floatFromVal (lval) %
-         (unsigned long) floatFromVal (rval);
+       SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) %
+         (TYPE_UDWORD) floatFromVal (rval);
       else
-       SPEC_CVAL (val->type).v_long = (unsigned long) floatFromVal (lval) %
-         (unsigned long) floatFromVal (rval);
+       SPEC_CVAL (val->type).v_long = (TYPE_DWORD) floatFromVal (lval) %
+         (TYPE_DWORD) 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 = (unsigned) floatFromVal (lval) %
-         (unsigned) floatFromVal (rval);
-      }
+      if (SPEC_USIGN (val->type))
+        SPEC_CVAL (val->type).v_uint = (TYPE_UWORD) floatFromVal (lval) %
+          (TYPE_UWORD) floatFromVal (rval);
+      else
+        SPEC_CVAL (val->type).v_int = (TYPE_WORD) floatFromVal (lval) %
+          (TYPE_WORD) floatFromVal (rval);
     }
-
-  return cheapestVal(val);
+  return cheapestVal (val);
 }
 
 /*------------------------------------------------------------------*/
@@ -1165,33 +1209,34 @@ valPlus (value * lval, value * rval)
   value *val;
 
   /* create a new value */
-  val = newValue ();
-  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) &&
-    (floatFromVal(lval)+floatFromVal(rval))>=0;
-    
-  SPEC_LONG (val->type) = 1;
-
+  val = newValue();
+  val->type = val->etype = computeType (lval->etype,
+                                       rval->etype,
+                                       RESULT_TYPE_INT,
+                                       '+');
+  SPEC_SCLS (val->etype) = S_LITERAL; /* will remain literal */
+  
   if (IS_FLOAT (val->type))
     SPEC_CVAL (val->type).v_float = floatFromVal (lval) + floatFromVal (rval);
+  else  if (SPEC_LONG (val->type))
+    {
+      if (SPEC_USIGN (val->type))
+        SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) +
+         (TYPE_UDWORD) floatFromVal (rval);
+      else
+        SPEC_CVAL (val->type).v_long = (TYPE_DWORD) floatFromVal (lval) +
+          (TYPE_DWORD) 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 = (TYPE_UWORD) floatFromVal (lval) +
+          (TYPE_UWORD) floatFromVal (rval);
+      else
+        SPEC_CVAL (val->type).v_int = (TYPE_WORD) floatFromVal (lval) +
+          (TYPE_WORD) floatFromVal (rval);
     }
-  return cheapestVal(val);
+  return cheapestVal (val);
 }
 
 /*------------------------------------------------------------------*/
@@ -1203,45 +1248,34 @@ valMinus (value * lval, value * rval)
   value *val;
 
   /* create a new value */
-  val = newValue ();
-  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) &&
-    (floatFromVal(lval)-floatFromVal(rval))>=0;
-
-  SPEC_LONG (val->type) = (SPEC_LONG (lval->etype) | SPEC_LONG (rval->etype));
-
+  val = newValue();
+  val->type = val->etype = computeType (lval->etype,
+                                       rval->etype,
+                                       RESULT_TYPE_INT,
+                                       '-');
+  SPEC_SCLS (val->etype) = S_LITERAL; /* will remain literal */
+  
   if (IS_FLOAT (val->type))
     SPEC_CVAL (val->type).v_float = floatFromVal (lval) - floatFromVal (rval);
-  else
+  else  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 = (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);
-         }
-       }
+        SPEC_CVAL (val->type).v_long = (TYPE_DWORD) floatFromVal (lval) -
+          (TYPE_DWORD) floatFromVal (rval);
     }
-  return cheapestVal(val);
+  else
+   {
+     if (SPEC_USIGN (val->type))
+       SPEC_CVAL (val->type).v_uint = (TYPE_UWORD) floatFromVal (lval) -
+         (TYPE_UWORD) floatFromVal (rval);
+     else
+       SPEC_CVAL (val->type).v_int = (TYPE_WORD) floatFromVal (lval) -
+         (TYPE_WORD) floatFromVal (rval);
+    }
+  return cheapestVal (val);
 }
 
 /*------------------------------------------------------------------*/
@@ -1253,25 +1287,53 @@ valShift (value * lval, value * rval, int lr)
   value *val;
 
   /* create a new value */
-  val = newValue ();
-  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) = 1;
+  val = newValue();
+  val->type = val->etype = computeType (lval->etype,
+                                       NULL,
+                                       RESULT_TYPE_INT,
+                                       'S');
+  SPEC_SCLS (val->etype) = S_LITERAL; /* will remain literal */
+
+  if (getSize (val->type) * 8 <= (TYPE_UDWORD) floatFromVal (rval) &&
+       /* left shift */
+      (lr ||
+       /* right shift and unsigned */
+       (!lr && SPEC_USIGN (rval->type))))
+    {
+      werror (W_SHIFT_CHANGED, (lr ? "left" : "right"));
+    }
 
   if (SPEC_LONG (val->type))
     {
       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);
+        {
+          SPEC_CVAL (val->type).v_ulong = lr ?
+           (TYPE_UDWORD) floatFromVal (lval) << (TYPE_UDWORD) floatFromVal (rval) : \
+           (TYPE_UDWORD) floatFromVal (lval) >> (TYPE_UDWORD) floatFromVal (rval);
+        }
       else
-       SPEC_CVAL (val->type).v_long = lr ?
-         (long) floatFromVal (lval) << (long) floatFromVal (rval) : \
-         (long) floatFromVal (lval) >> (long) floatFromVal (rval);
+        {
+          SPEC_CVAL (val->type).v_long = lr ?
+           (TYPE_DWORD) floatFromVal (lval) << (TYPE_UDWORD) floatFromVal (rval) : \
+           (TYPE_DWORD) floatFromVal (lval) >> (TYPE_UDWORD) floatFromVal (rval);
+        }
     }
-
-  return cheapestVal(val);
+  else
+    {
+      if (SPEC_USIGN (val->type))
+        {
+          SPEC_CVAL (val->type).v_uint = lr ?
+           (TYPE_UWORD) floatFromVal (lval) << (TYPE_UDWORD) floatFromVal (rval) : \
+           (TYPE_UWORD) floatFromVal (lval) >> (TYPE_UDWORD) floatFromVal (rval);
+        }
+      else
+        {
+          SPEC_CVAL (val->type).v_int = lr ?
+           (TYPE_WORD) floatFromVal (lval) << (TYPE_UDWORD) floatFromVal (rval) : \
+           (TYPE_WORD) floatFromVal (lval) >> (TYPE_UDWORD) floatFromVal (rval);
+        }
+    }
+  return cheapestVal (val);
 }
 
 /*------------------------------------------------------------------*/
@@ -1309,11 +1371,58 @@ valCompare (value * lval, value * rval, int ctype)
       break;
 
     case EQ_OP:
-      SPEC_CVAL (val->type).v_int = floatFromVal (lval) == floatFromVal (rval);
+      if (SPEC_NOUN(lval->type) == V_FLOAT ||
+         SPEC_NOUN(rval->type) == V_FLOAT)
+       {
+         SPEC_CVAL (val->type).v_int = floatFromVal (lval) == floatFromVal (rval);
+       }
+      else
+       {
+         /* integrals: ignore signedness */
+         TYPE_UDWORD l, r;
+
+         l = (TYPE_UDWORD) floatFromVal (lval);
+         r = (TYPE_UDWORD) floatFromVal (rval);
+         /* In order to correctly compare 'signed int' and 'unsigned int' it's
+            neccessary to strip them to 16 bit.
+            Literals are reduced to their cheapest type, therefore left and
+            right might have different types. It's neccessary to find a
+            common type: int (used for char too) or long */
+         if (!IS_LONG (lval->etype) &&
+             !IS_LONG (rval->etype))
+           {
+             r = (TYPE_UWORD) r;
+             l = (TYPE_UWORD) l;
+           }
+         SPEC_CVAL (val->type).v_int = l == r;
+       }
       break;
-
     case NE_OP:
-      SPEC_CVAL (val->type).v_int = floatFromVal (lval) != floatFromVal (rval);
+      if (SPEC_NOUN(lval->type) == V_FLOAT ||
+         SPEC_NOUN(rval->type) == V_FLOAT)
+       {
+         SPEC_CVAL (val->type).v_int = floatFromVal (lval) != floatFromVal (rval);
+       }
+      else
+       {
+         /* integrals: ignore signedness */
+         TYPE_UDWORD l, r;
+
+         l = (TYPE_UDWORD) floatFromVal (lval);
+         r = (TYPE_UDWORD) floatFromVal (rval);
+         /* In order to correctly compare 'signed int' and 'unsigned int' it's
+            neccessary to strip them to 16 bit.
+            Literals are reduced to their cheapest type, therefore left and
+            right might have different types. It's neccessary to find a
+            common type: int (used for char too) or long */
+         if (!IS_LONG (lval->etype) &&
+             !IS_LONG (rval->etype))
+           {
+             r = (TYPE_UWORD) r;
+             l = (TYPE_UWORD) l;
+           }
+         SPEC_CVAL (val->type).v_int = l != r;
+       }
       break;
 
     }
@@ -1331,9 +1440,9 @@ valBitwise (value * lval, value * rval, int op)
 
   /* create a new value */
   val = newValue ();
-  val->type = copyLinkChain (getSize(rval->type) > getSize(lval->type) ?
-                            rval->type : lval->type);
+  val->type = computeType (lval->etype, rval->etype, RESULT_TYPE_CHAR, op);
   val->etype = getSpec (val->type);
+  SPEC_SCLS (val->etype) = S_LITERAL;
 
   switch (op)
     {
@@ -1341,19 +1450,19 @@ valBitwise (value * lval, value * rval, int op)
       if (SPEC_LONG (val->type))
        {
          if (SPEC_USIGN (val->type))
-           SPEC_CVAL (val->type).v_ulong = (unsigned long) floatFromVal (lval) &
-             (unsigned long) floatFromVal (rval);
+           SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) &
+             (TYPE_UDWORD) floatFromVal (rval);
          else
-           SPEC_CVAL (val->type).v_long = (long) floatFromVal (lval) &
-             (long) floatFromVal (rval);
+           SPEC_CVAL (val->type).v_long = (TYPE_DWORD) floatFromVal (lval) &
+             (TYPE_DWORD) floatFromVal (rval);
        }
       else
        {
          if (SPEC_USIGN (val->type))
-           SPEC_CVAL (val->type).v_uint = (unsigned) floatFromVal (lval) &
-             (unsigned) floatFromVal (rval);
+           SPEC_CVAL (val->type).v_uint = (TYPE_UWORD) floatFromVal (lval) &
+             (TYPE_UWORD) floatFromVal (rval);
          else
-           SPEC_CVAL (val->type).v_int = (int) floatFromVal (lval) & (int) floatFromVal (rval);
+           SPEC_CVAL (val->type).v_int = (TYPE_WORD) floatFromVal (lval) & (TYPE_WORD) floatFromVal (rval);
        }
       break;
 
@@ -1361,20 +1470,20 @@ valBitwise (value * lval, value * rval, int op)
       if (SPEC_LONG (val->type))
        {
          if (SPEC_USIGN (val->type))
-           SPEC_CVAL (val->type).v_ulong = (unsigned long) floatFromVal (lval) |
-             (unsigned long) floatFromVal (rval);
+           SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) |
+             (TYPE_UDWORD) floatFromVal (rval);
          else
-           SPEC_CVAL (val->type).v_long = (long) floatFromVal (lval) |
-             (long) floatFromVal (rval);
+           SPEC_CVAL (val->type).v_long = (TYPE_DWORD) floatFromVal (lval) |
+             (TYPE_DWORD) floatFromVal (rval);
        }
       else
        {
          if (SPEC_USIGN (val->type))
-           SPEC_CVAL (val->type).v_uint = (unsigned) floatFromVal (lval) |
-             (unsigned) floatFromVal (rval);
+           SPEC_CVAL (val->type).v_uint = (TYPE_UWORD) floatFromVal (lval) |
+             (TYPE_UWORD) floatFromVal (rval);
          else
            SPEC_CVAL (val->type).v_int =
-             (int) floatFromVal (lval) | (int) floatFromVal (rval);
+             (TYPE_WORD) floatFromVal (lval) | (TYPE_WORD) floatFromVal (rval);
        }
 
       break;
@@ -1383,24 +1492,24 @@ valBitwise (value * lval, value * rval, int op)
       if (SPEC_LONG (val->type))
        {
          if (SPEC_USIGN (val->type))
-           SPEC_CVAL (val->type).v_ulong = (unsigned long) floatFromVal (lval) ^
-             (unsigned long) floatFromVal (rval);
+           SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) ^
+             (TYPE_UDWORD) floatFromVal (rval);
          else
-           SPEC_CVAL (val->type).v_long = (long) floatFromVal (lval) ^
-             (long) floatFromVal (rval);
+           SPEC_CVAL (val->type).v_long = (TYPE_DWORD) floatFromVal (lval) ^
+             (TYPE_DWORD) floatFromVal (rval);
        }
       else
        {
          if (SPEC_USIGN (val->type))
-           SPEC_CVAL (val->type).v_uint = (unsigned) floatFromVal (lval) ^
-             (unsigned) floatFromVal (rval);
+           SPEC_CVAL (val->type).v_uint = (TYPE_UWORD) floatFromVal (lval) ^
+             (TYPE_UWORD) floatFromVal (rval);
          else
            SPEC_CVAL (val->type).v_int =
-             (int) floatFromVal (lval) ^ (int) floatFromVal (rval);
+             (TYPE_WORD) floatFromVal (lval) ^ (TYPE_WORD) floatFromVal (rval);
        }
       break;
     }
-
+    
   return cheapestVal(val);
 }
 
@@ -1417,7 +1526,7 @@ valLogicAndOr (value * lval, value * rval, int op)
   val->type = val->etype = newCharLink ();
   val->type->class = SPECIFIER;
   SPEC_SCLS (val->type) = S_LITERAL;   /* will remain literal */
-  SPEC_USIGN (val->type) = 0;
+  SPEC_USIGN (val->type) = 1;
 
   switch (op)
     {
@@ -1441,6 +1550,7 @@ value *
 valCastLiteral (sym_link * dtype, double fval)
 {
   value *val;
+  TYPE_UDWORD l = (TYPE_UDWORD)fval;
 
   if (!dtype)
     return NULL;
@@ -1448,27 +1558,38 @@ valCastLiteral (sym_link * dtype, double fval)
   val = newValue ();
   val->etype = getSpec (val->type = copyLinkChain (dtype));
   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)) {
-      SPEC_CVAL (val->etype).v_ulong = (unsigned long) fval;
+      SPEC_CVAL (val->etype).v_ulong = l;
       return val;
   }
 
   if (SPEC_NOUN (val->etype) == V_FLOAT)
       SPEC_CVAL (val->etype).v_float = fval;
-  else {
-      unsigned long l = (unsigned long)fval;
+  else if (SPEC_NOUN (val->etype) == V_BIT ||
+           SPEC_NOUN (val->etype) == V_SBIT)
+    SPEC_CVAL (val->etype).v_uint = l & 1;   
+  else if (SPEC_NOUN (val->etype) == V_BITFIELD)
+    SPEC_CVAL (val->etype).v_uint = l &
+                                   (0xffffu >> (16 - SPEC_BLEN (val->etype)));
+  else if (SPEC_NOUN (val->etype) == V_CHAR) {
+      if (SPEC_USIGN (val->etype))
+         SPEC_CVAL (val->etype).v_uint= (TYPE_UBYTE) l;
+      else
+         SPEC_CVAL (val->etype).v_int = (TYPE_BYTE) l;
+  } else {
       if (SPEC_LONG (val->etype)) {
          if (SPEC_USIGN (val->etype))
-             SPEC_CVAL (val->etype).v_ulong = (unsigned long) l;
+             SPEC_CVAL (val->etype).v_ulong = (TYPE_UDWORD) l;
          else
-             SPEC_CVAL (val->etype).v_long = (long) l;
+             SPEC_CVAL (val->etype).v_long = (TYPE_DWORD) l;
       } else {
          if (SPEC_USIGN (val->etype))
-             SPEC_CVAL (val->etype).v_uint = (unsigned short)l;
+             SPEC_CVAL (val->etype).v_uint = (TYPE_UWORD)l;
          else
-             SPEC_CVAL (val->etype).v_int = (short)l;
+             SPEC_CVAL (val->etype).v_int = (TYPE_WORD)l;
       }
   }
   return val;
@@ -1477,7 +1598,7 @@ valCastLiteral (sym_link * dtype, double fval)
 /*------------------------------------------------------------------*/
 /* getNelements - determines # of elements from init list           */
 /*------------------------------------------------------------------*/
-int 
+int
 getNelements (sym_link * type, initList * ilist)
 {
   int i;
@@ -1563,10 +1684,7 @@ valForArray (ast * arrExpr)
 
   val->type = newLink (DECLARATOR);
   if (SPEC_SCLS (arrExpr->left->etype) == S_CODE)
-    {
-      DCL_TYPE (val->type) = CPOINTER;
-      DCL_PTR_CONST (val->type) = port->mem.code_ro;
-    }
+    DCL_TYPE (val->type) = CPOINTER;
   else if (SPEC_SCLS (arrExpr->left->etype) == S_XDATA)
     DCL_TYPE (val->type) = FPOINTER;
   else if (SPEC_SCLS (arrExpr->left->etype) == S_XSTACK)
@@ -1634,10 +1752,7 @@ valForStructElem (ast * structT, ast * elemT)
 
   val->type = newLink (DECLARATOR);
   if (SPEC_SCLS (structT->etype) == S_CODE)
-    {
-      DCL_TYPE (val->type) = CPOINTER;
-      DCL_PTR_CONST (val->type) = port->mem.code_ro;
-    }
+    DCL_TYPE (val->type) = CPOINTER;
   else if (SPEC_SCLS (structT->etype) == S_XDATA)
     DCL_TYPE (val->type) = FPOINTER;
   else if (SPEC_SCLS (structT->etype) == S_XSTACK)