* src/SDCCval.c (cheapestVal): beautified
authorbernhardheld <bernhardheld@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Sat, 31 Dec 2005 15:22:55 +0000 (15:22 +0000)
committerbernhardheld <bernhardheld@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Sat, 31 Dec 2005 15:22:55 +0000 (15:22 +0000)
(valNot): ANSI: result type is int (SDCC: unsigned char)
* support/regression/tests/onebyte.c: added promotion and signedness tests for unary minus
* support/regression/tests/not.c: added

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

ChangeLog
src/SDCCval.c
support/regression/tests/not.c [new file with mode: 0644]
support/regression/tests/onebyte.c

index 860625774539541d15b2d8fbf5a582e20536336c..7254c8eaa4666ee95f13b6247f0cbb8801c6952d 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -3,13 +3,15 @@
        * 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
+       (valUnaryPM, valComplement): fix sign and promotion,
+       (valNot): ANSI: result type is int (SDCC: unsigned char)
        * 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
+       * support/regression/tests/not.c: added
 
 2005-12-28 Bernhard Held <bernhard AT bernhardheld.de>
 
index 29a982d0a0746776fde030c0b156a8fd273ba0e0..11d80e4aa9411c5e6ccd810e1ed0a3eef346f8a5 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;
@@ -1058,14 +1055,12 @@ valNot (value * val)
       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;
 }
 
diff --git a/support/regression/tests/not.c b/support/regression/tests/not.c
new file mode 100644 (file)
index 0000000..f50b696
--- /dev/null
@@ -0,0 +1,42 @@
+/** not.c test ! operator
+
+  ANSI: return type is int
+
+  attr: volatile,
+*/
+#include <testfwk.h>
+
+void
+testNot(void)
+{
+    signed char  {attr} sc;
+  unsigned char  {attr} uc;
+  unsigned int   {attr} ui;
+  unsigned long  {attr} ul;
+
+  sc = 0;
+  uc = 0;
+  ui = 0;
+  ul = 0;
+  /* remember: unsigned * signed -> unsigned */
+  /*             signed * signed ->   signed */
+  ASSERT(!(  signed char) 0 * -1 < 0);
+  ASSERT(!(unsigned char) 0 * -1 < 0);
+  ASSERT(!sc   * -1 < 0);
+  ASSERT(!uc   * -1 < 0);
+  ASSERT(! 0   * -1 < 0);
+  ASSERT(! 0u  * -1 < 0);
+  ASSERT(!ui   * -1 < 0);
+  ASSERT(! 0l  * -1 < 0);
+  ASSERT(! 0ul * -1 < 0);
+  ASSERT(!ul   * -1 < 0);
+
+  ASSERT(!(char) 0 <<  8 == 0x100);
+  ASSERT(!sc       <<  8 == 0x100);
+#if !defined(PORT_HOST)
+  ASSERT(!0  << 16 == 0);
+  ASSERT(!0l << 16 == 0);
+  ASSERT(!ui << 16 == 0);
+  ASSERT(!ul << 16 == 0);
+#endif
+}
index 326f9ad03bb412db0157f8be1cc17ea4f7aac605..b3ce9781042df764374138b3b067f25ff6e3d96e 100644 (file)
@@ -185,10 +185,15 @@ testComplement(void)
   uc = 0x80;  r16 = ~uc; ASSERT(r16 == (short) 0xff7f); ASSERT(~uc < 0);
   ASSERT(~ (char)          0x80 == (short) 0x007f); ASSERT(~ (char)          0x80 > 0);
   ASSERT(~ (unsigned char) 0x80 == (short) 0xff7f); ASSERT(~ (unsigned char) 0x80 < 0);
+
+  ASSERT(~ 1   < 0);
+  ASSERT(~ 1u  > 0);
+  ASSERT(~ 1l  < 0);
+  ASSERT(~ 1ul > 0);
 }
 
 void
-testComp(void)
+testCompare(void)
 {
   {attrL}   signed char  c;
   {attrR} unsigned char uc;
@@ -209,22 +214,30 @@ testUMinus(void)
 {
     signed char  {attrL} sc;
   unsigned char  {attrL} uc;
-  unsigned int   {attrL} us;
+    signed int   {attrL} si;
+  unsigned int   {attrL} ui;
+    signed long  {attrL} sl;
   unsigned long  {attrL} ul;
 
-  ASSERT (-(53ul) > 0);
+  ASSERT(-(53l ) < 0);
+  ASSERT(-(53ul) > 0);
   ul = 53;
-  ASSERT (-ul > 0);
-
-  ASSERT (-(53u ) > 0);
-  us = 53;
-  ASSERT (-us > 0);
-
-  ASSERT (-( 250 ) == -250);
+  ASSERT(-ul > 0);
+  sl = 53;
+  ASSERT(-sl < 0);
+
+  ASSERT(-(53  ) < 0);
+  ASSERT(-(53u ) > 0);
+  ui = 53;
+  ASSERT(-ui > 0);
+  si = 53;
+  ASSERT(-si < 0);
+
+  ASSERT(-( 250 ) == -250);
   uc = 250;
-  ASSERT (-uc == -250);
+  ASSERT(-uc == -250);
 
-  ASSERT (-(-128 ) ==  128);
+  ASSERT(-(-128 ) ==  128);
   sc = -128;
-  ASSERT (-sc == 128);
+  ASSERT(-sc == 128);
 }