* support/regression/tests/bitfields.c:
[fw/sdcc] / support / regression / tests / bitfields.c
index 7007741142d02d80e72e5cbbf18dd29f8df8c824..a7882e9fd243532edbbdbc537bf368cf53294744 100644 (file)
@@ -1,5 +1,7 @@
 /** Bitfield tests.
 
+  SDCC pic16 port currently does not support bitfields of size > 8,
+  so they are ifdefed out.
 */
 #include <testfwk.h>
 
@@ -8,6 +10,7 @@ struct {
   char c3_5 : 5;
 } c_bf;
 
+#if !defined(SDCC_pic16)
 struct {
   int i0_7 : 7;
   int i7_9 : 9;
@@ -19,7 +22,6 @@ struct {
   long l17_15 : 15;
 } l_bf;
 
-
 struct {
   unsigned int b0 : 1;
   unsigned int b1 : 1;
@@ -32,6 +34,7 @@ struct {
   unsigned int b8 : 1;
   unsigned int b9 : 1;
 } sb_bf;
+#endif  /* !SDCC_pic16 */
 
 struct {
   unsigned int b0 : 1;
@@ -56,6 +59,7 @@ struct {
   unsigned int b1 : 1;
 } size2a_bf;
 
+#if !defined(SDCC_pic16)
 struct {
   unsigned int b0 : 1;
   unsigned int b1 : 1;
@@ -90,7 +94,7 @@ struct {
   signed int s7_1  : 1;
   signed int s8_9  : 9;
 } s_bf;
-
+#endif  /* !SDCC_pic16 */
 
 void
 testBitfieldSizeof(void)
@@ -102,12 +106,13 @@ testBitfieldSizeof(void)
   ASSERT( sizeof(size1a_bf) >= 1);
   ASSERT( sizeof(size1b_bf) >= 1);
   ASSERT( sizeof(size1c_bf) >= 1);
+#if !defined(SDCC_pic16)
   ASSERT( sizeof(size2b_bf) >= 2);
   ASSERT( sizeof(size2c_bf) >= 2);
   ASSERT( sizeof(size2d_bf) >= 2);
   ASSERT( sizeof(size3a_bf) >= 2);
   ASSERT( sizeof(size1a_bf) <= sizeof(size1b_bf));
-
+#endif  /* !SDCC_pic16 */
   /* Some SDCC specific assertions. SDCC uses 8 bit storage units.
      Bitfields that are less than 8 bits, but would (due to earlier
      bitfield declarations) span a storage unit boundary are
@@ -120,10 +125,12 @@ testBitfieldSizeof(void)
   ASSERT( sizeof(size1b_bf) == 1);
   ASSERT( sizeof(size1c_bf) == 1);
   ASSERT( sizeof(size2a_bf) == 2);
+#if !defined(SDCC_pic16)
   ASSERT( sizeof(size2b_bf) == 2);
   ASSERT( sizeof(size2c_bf) == 2);
   ASSERT( sizeof(size2d_bf) == 2);
   ASSERT( sizeof(size3a_bf) == 3);
+#endif  /* !SDCC_pic16 */
 #endif
 }
 
@@ -131,6 +138,7 @@ testBitfieldSizeof(void)
 void
 testBitfieldsSingleBitLiteral(void)
 {
+#if !defined(SDCC_pic16)
   size2b_bf.b0 = 0;
   size2b_bf.b1 = 0;
   size2b_bf.b2 = 0;
@@ -178,11 +186,13 @@ testBitfieldsSingleBitLiteral(void)
   ASSERT(size2b_bf.b7==0);
   ASSERT(size2b_bf.b8==0);
   ASSERT(size2b_bf.b9==1);
+#endif  /* !SDCC_pic16 */
 }
 
 void
 testBitfieldsSingleBit(void)
 {
+#if !defined(SDCC_pic16)
   volatile unsigned char c;
 
   c = 0;
@@ -235,11 +245,13 @@ testBitfieldsSingleBit(void)
   ASSERT(size2b_bf.b7==0);
   ASSERT(size2b_bf.b8==0);
   ASSERT(size2b_bf.b9==1);
+#endif  /* !SDCC_pic16 */
 }
 
 void
 testBitfieldsMultibitLiteral(void)
 {
+#if !defined(SDCC_pic16)
   size2c_bf.b0 = 0xff; /* should truncate to 0x0f */
   size2c_bf.b1 = 0;
   ASSERT(size2c_bf.b0==0x0f);
@@ -279,11 +291,13 @@ testBitfieldsMultibitLiteral(void)
   size2d_bf.b1 = 5;
   ASSERT(size2d_bf.b0==0x0a46);
   ASSERT(size2d_bf.b1==5);
+#endif  /* !SDCC_pic16 */
 }
 
 void
 testBitfieldsMultibit(void)
 {
+#if !defined(SDCC_pic16)
   volatile int allones = 0xffff;
   volatile int zero = 0;
   volatile int x;
@@ -321,6 +335,7 @@ testBitfieldsMultibit(void)
   size2d_bf.b1 = x;
   ASSERT(size2d_bf.b0==0x0a46);
   ASSERT(size2d_bf.b1==5);
+#endif  /* !SDCC_pic16 */
 }
 
 void
@@ -328,7 +343,12 @@ testBitfields(void)
 {
   c_bf.c0_3 = 2;
   c_bf.c3_5 = 3;
+#if defined(PORT_HOST) && (defined(__ppc__) || defined(__PPC__))
+  /* bitfields on powerpc architecture are allocated from left to right */
+  ASSERT(*(char *)(&c_bf) == ((2<<(8-3)) + 3) );
+#else
   ASSERT(*(char *)(&c_bf) == (2 + (3<<3)) );
+#endif
 
 #if 0 // not yet
   i_bf.i0_7 = 23;
@@ -345,6 +365,7 @@ testBitfields(void)
 void
 testSignedBitfields(void)
 {
+#if !defined(SDCC_pic16)
   s_bf.s0_7 =   0xf0;
   s_bf.s7_1 =      1;
   s_bf.s8_9 = 0xfff8;
@@ -363,4 +384,39 @@ testSignedBitfields(void)
   ASSERT(s_bf.s8_9 == 0xff);
   ASSERT(s_bf.s0_7 > 0);
   ASSERT(s_bf.s8_9 > 0);
+#endif  /* !SDCC_pic16 */
+}
+
+/* test case for enhancement request #2291335 : Unnamed bit-field initialization */
+
+struct s2291335_1 {
+  int a : 2;
+  char  : 2;
+  int b : 2;
+};
+
+struct s2291335_2 {
+  int a : 2;
+  char  : 0;
+  int b : 2;
+};
+
+struct s2291335_1 gs2291335_1 = {1, 2};
+struct s2291335_2 gs2291335_2 = {1, 2};
+
+void
+__test2291335(void)
+{
+  struct s2291335_1 ls2291335_1 = {1, 2};
+  struct s2291335_2 ls2291335_2 = {1, 2};
+
+  ASSERT(gs2291335_1.a == 1);
+  ASSERT(gs2291335_1.b == 2);
+  ASSERT(gs2291335_2.a == 1);
+  ASSERT(gs2291335_2.b == 2);
+
+  ASSERT(ls2291335_1.a == 1);
+  ASSERT(ls2291335_1.b == 2);
+  ASSERT(ls2291335_2.a == 1);
+  ASSERT(ls2291335_2.b == 2);
 }