* src/SDCCast.c (isLoopCountable): fixed bug #1478316
[fw/sdcc] / src / SDCCval.c
index 29a982d0a0746776fde030c0b156a8fd273ba0e0..e278fb93a901f52777d12df03835ab9f3549ad9c 100644 (file)
@@ -327,22 +327,19 @@ symbolVal (symbol * sym)
 }
 
 /*--------------------------------------------------------------------*/
-/* cheapestVal - convert a val to the cheapest as possible value      */
+/* cheapestVal - try to reduce 'signed int' to 'char'                 */
 /*--------------------------------------------------------------------*/
 static value *
 cheapestVal (value *val)
 {
-  if (IS_FLOAT (val->type) || IS_FIXED (val->type) || IS_CHAR (val->type))
+  /* only int can be reduced */
+  if (!IS_INT(val->type))
     return val;
 
   /* long must not be changed */
   if (SPEC_LONG(val->type))
     return val;
 
-  /* only int can be reduced */
-  if (!IS_INT(val->type))
-    return val;
-
   /* unsigned must not be changed */
   if (SPEC_USIGN(val->type))
     return val;
@@ -1047,25 +1044,23 @@ valNot (value * val)
   if (SPEC_LONG (val->etype))
     {
       if (SPEC_USIGN (val->etype))
-        SPEC_CVAL (val->etype).v_ulong = !SPEC_CVAL (val->etype).v_ulong;
+        SPEC_CVAL (val->etype).v_int = !SPEC_CVAL (val->etype).v_ulong;
       else
-        SPEC_CVAL (val->etype).v_long = !SPEC_CVAL (val->etype).v_long;
+        SPEC_CVAL (val->etype).v_int = !SPEC_CVAL (val->etype).v_long;
     }
   else
     {
       if (SPEC_USIGN (val->etype))
-        SPEC_CVAL (val->etype).v_uint = !SPEC_CVAL (val->etype).v_uint;
+        SPEC_CVAL (val->etype).v_int = !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)
-        {
-          /* promote to 'signed int', cheapestVal() might reduce it again */
-          SPEC_USIGN(val->etype) = 0;
-          SPEC_NOUN(val->etype) = V_INT;
-        }
-      return cheapestVal (val);
     }
+  /* ANSI: result type is int, value is 0 or 1 */
+  /* sdcc will hold this in an 'unsigned char' */
+  SPEC_USIGN(val->etype) = 1;
+  SPEC_LONG (val->etype) = 0;
+  SPEC_NOUN(val->etype) = V_CHAR;
   return val;
 }