* src/SDCCast.c (decorateType): fix promotion of unary minus
authorbernhardheld <bernhardheld@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Sat, 31 Dec 2005 13:17:32 +0000 (13:17 +0000)
committerbernhardheld <bernhardheld@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Sat, 31 Dec 2005 13:17:32 +0000 (13:17 +0000)
* src/SDCCsymt.c (computeType): beautified
* src/SDCCval.c (cheapestVal): beautified, old non-Ansi version removed,
(valUnaryPM, valComplement, valNot): fix sign and promotion
* support/regression/tests/uminus.c: speedup by removing superflous test case 'int'
* support/regression/tests/onebyte.c: added promotion and signedness tests for unary minus
* support/regressions/tests/bug-477927.c: disable warning about uninitialized variables

git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@4015 4a8a32a2-be11-0410-ad9d-d568d2c75423

ChangeLog
src/SDCCast.c
src/SDCCsymt.c
src/SDCCval.c
support/regression/tests/bug-477927.c
support/regression/tests/onebyte.c
support/regression/tests/uminus.c

index 708f9a3d3bb98b08b28ce7418677667927ab4db2..860625774539541d15b2d8fbf5a582e20536336c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2005-12-31 Bernhard Held <bernhard AT bernhardheld.de>
+
+       * src/SDCCast.c (decorateType): fix promotion of unary minus
+       * src/SDCCsymt.c (computeType): beautified
+       * src/SDCCval.c (cheapestVal): beautified, old non-Ansi version removed,
+       (valUnaryPM, valComplement, valNot): fix sign and promotion
+       * support/regression/tests/uminus.c: speedup by removing superflous
+       test case 'int'
+       * support/regression/tests/onebyte.c: added promotion and signedness
+       tests for unary minus
+       * support/regressions/tests/bug-477927.c: disable warning about
+       uninitialized variables
+
 2005-12-28 Bernhard Held <bernhard AT bernhardheld.de>
 
        * device/lib/Makefile.in: added --std-sdcc99 to CFLAGS
index 0a608a796a0b1c9dd89c7ca7dcce4b8e1f577cc7..3a91217c983507e25be4c2f2bc618865b4dd002b 100644 (file)
@@ -3287,11 +3287,15 @@ decorateType (ast * tree, RESULT_TYPE resultType)
               tree->opval.val = valUnaryPM (valFromType (LETYPE (tree)));
               tree->left = NULL;
               TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
-              SPEC_USIGN(TETYPE(tree)) = 0;
               return tree;
             }
+          tree->left  = addCast (tree->left, resultType, TRUE);
+          TETYPE (tree) = getSpec (TTYPE (tree) =
+                                     computeType (LTYPE (tree),
+                                                  NULL,
+                                                  resultType,
+                                                  tree->opval.op));
           LRVAL (tree) = 1;
-          TETYPE(tree) = getSpec (TTYPE (tree) = LTYPE (tree));
           return tree;
         }
 
index 172acf8060459d5e42d30cf370a5eed597a3d2f6..5cb19d06a213d70c525258c6a943beac6e07f439 100644 (file)
@@ -1880,28 +1880,34 @@ computeType (sym_link * type1, sym_link * type2,
           }
         else if (IS_CHAR (reType))
           {
-            if (op == '|' || op == '^')
-              return computeTypeOr (etype1, etype2, reType);
-            else if (   op == '&'
-                     && SPEC_USIGN (etype1) != SPEC_USIGN (etype2))
+            /* promotion of some special cases */
+            switch (op)
               {
-                SPEC_USIGN (reType) = 1;
-                return rType;
-              }
-            else if (op == '*')
-              {
-                SPEC_NOUN (reType) = V_INT;
-                SPEC_USIGN (reType) = 0;
-                return rType;
-              }
-            /* TODO: should be in SDCCast.c */
-            else if (   op == '/'
-                     && (   !SPEC_USIGN (etype1)
-                         || !SPEC_USIGN (etype2)))
-              {
-                SPEC_NOUN (reType) = V_INT;
-                SPEC_USIGN (reType) = 0;
-                return rType;
+                case '|':
+                case '^':
+                  return computeTypeOr (etype1, etype2, reType);
+                case '&':
+                  if (SPEC_USIGN (etype1) != SPEC_USIGN (etype2))
+                    {
+                      SPEC_USIGN (reType) = 1;
+                      return rType;
+                    }
+                  break;
+                case '*':
+                  SPEC_NOUN (reType) = V_INT;
+                  SPEC_USIGN (reType) = 0;
+                  return rType;
+                case '/':
+                  /* if both are unsigned char then no promotion required */
+                  if (!(SPEC_USIGN (etype1) && SPEC_USIGN (etype2)))
+                    {
+                      SPEC_NOUN (reType) = V_INT;
+                      SPEC_USIGN (reType) = 0;
+                      return rType;
+                    }
+                  break;
+                default:
+                  break;
               }
           }
         break;
index c798df04c211b6a9137bb7185b36f347a3161e8d..29a982d0a0746776fde030c0b156a8fd273ba0e0 100644 (file)
@@ -326,100 +326,49 @@ symbolVal (symbol * sym)
   return val;
 }
 
-#if defined(REDUCE_LITERALS)
 /*--------------------------------------------------------------------*/
 /* cheapestVal - convert a val to the cheapest as possible value      */
 /*--------------------------------------------------------------------*/
-static value *cheapestVal (value *val) {
-  TYPE_DWORD  sval=0;
-  TYPE_UDWORD uval=0;
-
-  if (IS_FLOAT(val->type) || IS_FIXED(val->type) || IS_CHAR(val->type))
+static value *
+cheapestVal (value *val)
+{
+  if (IS_FLOAT (val->type) || IS_FIXED (val->type) || IS_CHAR (val->type))
     return val;
 
-  if (SPEC_LONG(val->type)) {
-    if (SPEC_USIGN(val->type)) {
-      uval=SPEC_CVAL(val->type).v_ulong;
-    } else {
-      sval=SPEC_CVAL(val->type).v_long;
-    }
-  } else {
-    if (SPEC_USIGN(val->type)) {
-      uval=SPEC_CVAL(val->type).v_uint;
-    } else {
-      sval=SPEC_CVAL(val->type).v_int;
-    }
-  }
-
-  if (SPEC_USIGN(val->type)) {
-    if (uval<=0xffff) {
-      SPEC_LONG(val->type)=0;
-      SPEC_CVAL(val->type).v_uint = (TYPE_UWORD)uval;
-      if (uval<=0xff) {
-        SPEC_NOUN(val->type)=V_CHAR;
-      }
-    }
-  } else { // not unsigned
-    if (sval<0) {
-      if (sval>=-32768) {
-        SPEC_LONG(val->type)=0;
-        SPEC_CVAL(val->type).v_int = (TYPE_WORD)sval;
-        if (sval>=-128) {
-          SPEC_NOUN(val->type)=V_CHAR;
-        }
-      }
-    } else { // sval>=0
-      if (sval<=32767) {
-        SPEC_LONG(val->type)=0;
-        SPEC_CVAL(val->type).v_int = (TYPE_WORD)sval;
-        if (sval<=127) {
-          SPEC_NOUN(val->type)=V_CHAR;
-        }
-      }
-    }
-  }
-  return val;
-}
-
-#else
+  /* long must not be changed */
+  if (SPEC_LONG(val->type))
+    return val;
 
-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;
 
-  /* - signed/unsigned must not be changed.
-     - long must not be changed.
+  /* unsigned must not be changed */
+  if (SPEC_USIGN(val->type))
+    return val;
 
-     the only possible reduction is from signed int to signed char,
+  /* the only possible reduction is from signed int to (un)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)
-
+  if (SPEC_CVAL(val->type).v_int < -128 ||
+      SPEC_CVAL(val->type).v_int >  255)
     {
-      SPEC_NOUN(val->type) = V_CHAR;
+      /* not in the range of (un)signed char */
+      return val;
     }
+
+  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)
-
+  if (SPEC_CVAL(val->type).v_int >= 0)
     {
-      SPEC_NOUN(val->type) = V_CHAR;
       SPEC_USIGN(val->type) = 1;
     }
   return (val);
 }
-#endif
 
 /*-----------------------------------------------------------------*/
 /* valueFromLit - creates a value from a literal                   */
@@ -1043,16 +992,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)
+            {
+              /* promote to 'signed int', cheapestVal() might reduce it again */
+              SPEC_USIGN(val->etype) = 0;
+              SPEC_NOUN(val->etype) = V_INT;
+            }
+          return cheapestVal (val);
         }
     }
-  // -(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;
 }
 
@@ -1076,13 +1025,15 @@ 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)
-        if (   SPEC_CVAL(val->etype).v_int < -128
-            || SPEC_CVAL(val->etype).v_int >  127)
+        {
+          /* promote to 'signed int', cheapestVal() might reduce it again */
+          SPEC_USIGN(val->etype) = 0;
           SPEC_NOUN(val->etype) = V_INT;
+        }
+      return cheapestVal (val);
     }
-  // ~(unsigned 3) now really is signed
-  SPEC_USIGN(val->etype)=0;
   return val;
 }
 
@@ -1106,6 +1057,14 @@ 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)
+        {
+          /* promote to 'signed int', cheapestVal() might reduce it again */
+          SPEC_USIGN(val->etype) = 0;
+          SPEC_NOUN(val->etype) = V_INT;
+        }
+      return cheapestVal (val);
     }
   return val;
 }
index b0ab6c3ef453ede5433ba4b5ea092c4b8c3ce603..6acc995baee796a2ddf5370473fd822dfe39006d 100644 (file)
@@ -29,6 +29,11 @@ spoil(UBYTE ignored)
 
 UBYTE accu[2];
 
+#if !defined(PORT_HOST)
+#  pragma save
+#  pragma disable_warning 84
+#endif
+
 void 
 testLoopInit(void)
 {
@@ -45,4 +50,6 @@ testLoopInit(void)
   while(t != 3);
 }
 
-
+#if !defined(PORT_HOST)
+#  pragma restore
+#endif
index a6ffc9d192059c76b772f5234f5c6cf6fc484881..326f9ad03bb412db0157f8be1cc17ea4f7aac605 100644 (file)
@@ -203,3 +203,28 @@ testComp(void)
   ASSERT(!(c >  uc));
   ASSERT(!(c >= uc));
 }
+
+void
+testUMinus(void)
+{
+    signed char  {attrL} sc;
+  unsigned char  {attrL} uc;
+  unsigned int   {attrL} us;
+  unsigned long  {attrL} ul;
+
+  ASSERT (-(53ul) > 0);
+  ul = 53;
+  ASSERT (-ul > 0);
+
+  ASSERT (-(53u ) > 0);
+  us = 53;
+  ASSERT (-us > 0);
+
+  ASSERT (-( 250 ) == -250);
+  uc = 250;
+  ASSERT (-uc == -250);
+
+  ASSERT (-(-128 ) ==  128);
+  sc = -128;
+  ASSERT (-sc == 128);
+}
index 1a5732b925f63a1ffa7e3865b54d7407d44afcc1..0009840d8d4716df830321060e3851e4379ea254 100644 (file)
@@ -1,7 +1,7 @@
 /* Test unary minus
 
-    lefttype: int, char, short, long
-    resulttype: int, char, short, long
+    lefttype: char, short, long
+    resulttype: char, short, long
     storage: static,
     attr: volatile,
  */
@@ -20,7 +20,6 @@ testUMinus(void)
 
   left = -76;
   result = -left;
-  
+
   ASSERT(result == 76);
 }
-