struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tcc / 95_bitfields.c
blob4ac38da66d3afc2a8afe995b44fc3dda0fe30ebe
1 /* ----------------------------------------------------------------------- */
2 #if TEST == 1
4 struct M P A __s
6 unsigned x : 12;
7 unsigned char y : 7;
8 unsigned z : 28;
9 unsigned a: 4;
10 unsigned b: 5;
12 TEST_STRUCT(0x333,0x44,0x555555,6,7);
15 /* ----------------------------------------------------------------------- */
16 #elif TEST == 2
18 struct M P __s
20 int x: 12;
21 char y: 6;
22 long long z:63;
23 A char a:4;
24 long long b:2;
27 TEST_STRUCT(3,30,0x123456789abcdef0LL,5,2);
30 /* ----------------------------------------------------------------------- */
31 #elif TEST == 3
33 struct M P __s
35 unsigned x:5, y:5, :0, z:5; char a:5; A short b:5;
37 TEST_STRUCT(21,23,25,6,14);
40 /* ----------------------------------------------------------------------- */
41 #elif TEST == 4
43 struct M P __s {
44 int x : 3;
45 int : 2;
46 int y : 1;
47 int : 0;
48 int z : 5;
49 int a : 7;
50 unsigned int b : 7;
52 TEST_STRUCT(3,1,15,120,120);
55 /* ----------------------------------------------------------------------- */
56 #elif TEST == 5
58 struct M P __s {
59 long long x : 45;
60 long long : 2;
61 long long y : 30;
62 unsigned long long z : 38;
63 char a; short b;
65 TEST_STRUCT(0x123456789ULL, 120<<25, 120, 0x44, 0x77);
68 /* ----------------------------------------------------------------------- */
69 #elif TEST == 6
71 struct M P __s {
72 int a;
73 signed char b;
74 int x : 12, y : 4, : 0, : 4, z : 3;
75 char d;
77 TEST_STRUCT(1,2,3,4,-3);
80 /* ----------------------------------------------------------------------- */
81 #elif TEST == 7
83 #ifdef _WIN32
84 typedef long long int ll;
85 #else
86 typedef long int ll;
87 #endif
88 struct M P __s {
89 ll d : 16;
90 ll b : 16;
91 ll x : 16;
92 ll y : 1;
93 ll z : 2;
94 ll a : 11;
95 ll e : 1;
96 ll f : 1;
98 TEST_STRUCT(1,2,3,4,5);
101 /* ----------------------------------------------------------------------- */
102 #elif defined PACK
104 #if PACK
105 # pragma pack(push,1)
106 # define P //_P
107 #else
108 # define P
109 #endif
111 printf("\n\n" + 2*top);
112 #define TEST 1
113 #include SELF
114 top = 0;
115 #define TEST 2
116 #include SELF
117 #define TEST 3
118 #include SELF
119 #define TEST 4
120 #include SELF
121 #define TEST 5
122 #include SELF
123 #define TEST 6
124 #include SELF
125 #define TEST 7
126 #include SELF
128 #if PACK
129 # pragma pack(pop)
130 #endif
132 #undef P
133 #undef PACK
135 /* ----------------------------------------------------------------------- */
136 #elif defined ALIGN
138 #if ALIGN
139 # define A _A(16)
140 #else
141 # define A
142 #endif
144 #define PACK 0
145 #include SELF
146 #define PACK 1
147 #include SELF
149 #undef A
150 #undef ALIGN
152 /* ----------------------------------------------------------------------- */
153 #elif defined MS_BF
155 #if MS_BF
156 # ifdef __TINYC__
157 # pragma comment(option, "-mms-bitfields")
158 # elif defined __GNUC__
159 # define M __attribute__((ms_struct))
160 # endif
161 #else
162 # ifdef __TINYC__
163 # pragma comment(option, "-mno-ms-bitfields")
164 # elif defined __GNUC__
165 # define M __attribute__((gcc_struct))
166 # endif
167 #endif
168 #ifndef M
169 # define M
170 #endif
172 #define ALIGN 0
173 #include SELF
174 #define ALIGN 1
175 #include SELF
177 #undef M
178 #undef MS_BF
180 /* ----------------------------------------------------------------------- */
181 #else
183 #include <stdio.h>
184 #include <string.h>
185 /* some gcc headers #define __attribute__ to empty if it's not gcc */
186 #undef __attribute__
188 void dump(void *p, int s)
190 int i;
191 for (i = s; --i >= 0;)
192 printf("%02X", ((unsigned char*)p)[i]);
193 printf("\n");
196 #define pv(m) \
197 printf(sizeof (s->m + 0) == 8 ? " %016llx" : " %02x", s->m)
199 #define TEST_STRUCT(v1,v2,v3,v4,v5) { \
200 struct __s _s, *s = & _s; \
201 printf("\n---- TEST %d%s%s%s ----\n" + top, \
202 TEST, MS_BF?" - MS-BITFIELDS":"", \
203 PACK?" - PACKED":"", \
204 ALIGN?" - WITH ALIGN":""); \
205 memset(s, 0, sizeof *s); \
206 s->x = -1, s->y = -1, s->z = -1, s->a = -1, s->b = -1; \
207 printf("bits in use : "), dump(s, sizeof *s); \
208 s->x = v1, s->y = v2, s->z = v3, s->a += v4, ++s->a, s->b = v5; \
209 printf("bits as set : "), dump(s, sizeof *s); \
210 printf("values :"), pv(x), pv(y), pv(z), pv(a), pv(b), printf("\n"); \
211 printf("align/size : %d %d\n", alignof(struct __s),sizeof(struct __s)); \
214 #ifdef _MSC_VER
215 # define _A(n) __declspec(align(n))
216 # define _P
217 # define alignof(x) __alignof(x)
218 #else
219 # define _A(n) __attribute__((aligned(n)))
220 # define _P __attribute__((packed))
221 # define alignof(x) __alignof__(x)
222 #endif
224 #ifndef MS_BITFIELDS
225 # define MS_BITFIELDS 0
226 #endif
228 #define SELF "95_bitfields.c"
230 int top = 1;
232 int main()
234 #define MS_BF MS_BITFIELDS
235 #include SELF
236 return 0;
239 /* ----------------------------------------------------------------------- */
240 #endif
241 #undef TEST