struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / bitfields-bits1.c.in
blob286411f4cb23949dd4da01013f3f6a96f800e0dd
1 /** Operations with 1-bit sized bitfields
2 preBits: 0, 8, 16
3 pattern: 0, 1
4 varType: 0, 1, 2, 3, 4, 5, 6
5 */
7 #pragma disable_warning 88
9 // Disable for ds390: bug #3211
10 // Absolute addressing has some issues for pdk. And if those are fixed, there might be alack of memory, still.
11 // mcs51 fails tests. Don't know if that is a bug or just a bad choice for ABS_ADDR1 and ABS_ADDR2 below.
12 #if defined(__SDCC_ds390) || defined(__SDCC_pdk14) || defined(__SDCC_pdk15) || defined(__SDCC_mcs51)
13 #define DUMMY_CASE
14 #endif
16 // These tests assume the bitfields are allocated in LSB to MSB order
17 // but some hosts allocate them in MSB to LSB order. Disable the
18 // tests on these hosts
19 #if defined(PORT_HOST) && (defined(__ppc__) || defined(__PPC__) || defined(__sparc) || defined(__sparc64__))
20 #define DUMMY_CASE
21 #endif
23 #include <testfwk.h>
24 #ifdef __sun__
25 #include <inttypes.h>
26 #else
27 #include <stdint.h>
28 #endif
30 #define PRE_BITS ({preBits})
31 #define PATTERN ({pattern})
32 #define VAR_TYPE ({varType})
34 typedef struct
36 #if PRE_BITS > 0
37 unsigned int preBits : PRE_BITS;
38 #endif
39 unsigned int bit0 : 1;
40 unsigned int bit1 : 1;
41 unsigned int bit2 : 1;
42 unsigned int bit3 : 1;
43 unsigned int bit4 : 1;
44 unsigned int bit5 : 1;
45 unsigned int bit6 : 1;
46 unsigned int bit7 : 1;
48 unsigned int postBits : 8;
49 }struct_8bits;
51 typedef struct
53 #if PRE_BITS > 0
54 unsigned int preBits : PRE_BITS;
55 #endif
56 unsigned int bits : 8;
57 unsigned int postBits : 8;
58 }struct_8bits_joined;
61 #define bitToTest__(b) bit ## b
62 #define bitToTest_(b) bitToTest__(b)
63 #define bitToTest bitToTest_(BIT_TO_TEST)
65 #if defined(__SDCC_pic16)
66 #define ABS_ADDR1 0x0200
67 #define ABS_ADDR2 0x0204
68 #elif defined(__SDCC_pic14)
69 #define ABS_ADDR1 0x0100
70 #define ABS_ADDR2 0x0104
71 #elif defined(__SDCC_stm8)
72 #define ABS_ADDR1 0x1000
73 #define ABS_ADDR2 0x1004
74 #elif defined(__SDCC_f8)
75 #define ABS_ADDR1 0x3000
76 #define ABS_ADDR2 0x3004
77 #else
78 #if !defined(__SDCC_pdk14) && !defined(__SDCC_pdk15) // TODO: Make test suitable for pdk
79 #if !defined(PORT_HOST) // Never do absolute address test with host
80 #define ABS_ADDR1 0xCA00
81 #define ABS_ADDR2 0xCA04
82 #endif
83 #endif
84 #endif
86 #if VAR_TYPE == 0
87 volatile struct_8bits volatileBits;
88 #elif VAR_TYPE == 1
89 #ifdef ABS_ADDR1
90 #define volatileBits (*(volatile struct_8bits*)ABS_ADDR1)
91 #else
92 #define DUMMY_CASE
93 #endif
94 #elif VAR_TYPE == 2
95 #ifdef ABS_ADDR2
96 #define volatileBits (*(volatile struct_8bits*)ABS_ADDR2)
97 #else
98 #define DUMMY_CASE
99 #endif
100 #elif VAR_TYPE == 3
101 #define VOLATILE_BITS_DEF volatile struct_8bits volatileBits
102 #elif VAR_TYPE == 4
103 #define VOLATILE_BITS_DEF static volatile struct_8bits volatileBits
104 #elif VAR_TYPE == 5
105 #ifdef ABS_ADDR1
106 #define VOLATILE_BITS_DEF static volatile struct_8bits __at(ABS_ADDR1) volatileBits
107 #else
108 #define DUMMY_CASE
109 #endif
110 #elif VAR_TYPE == 6
111 #ifdef ABS_ADDR2
112 #define VOLATILE_BITS_DEF static volatile struct_8bits __at(ABS_ADDR2) volatileBits
113 #define USE_ONLY_1_BYTE
114 #else
115 #define DUMMY_CASE
116 #endif
117 #else
118 #error "Unknown VAR_TYPE case"
119 #endif
121 #ifndef VOLATILE_BITS_DEF
122 #define VOLATILE_BITS_DEF
123 #endif
125 #if PATTERN == 0
126 #define PRE_BIT_VALUE 0
127 #define POST_BIT_VALUE 0
128 #else
129 #define PRE_BIT_VALUE ((1UL << PRE_BITS) - 1)
130 #define POST_BIT_VALUE 0xFF
131 #endif
133 #ifndef DUMMY_CASE
135 void dummy_f(void)
137 return;
140 // Return type of this function is uint16_t to trigger some peephole rules for STM8
141 uint16_t
142 bits_check_value(const struct_8bits * const var, const uint8_t value)
144 #if PRE_BITS > 0
145 if(var->preBits != PRE_BIT_VALUE) return 0;
146 #endif
147 if(var->postBits != POST_BIT_VALUE) return 0;
149 if(((struct_8bits_joined*)var)->bits != value) return 0;
151 return 1;
153 void
154 bits_set_value(struct_8bits * const var, const uint8_t value)
156 #if PRE_BITS > 0
157 var->preBits = PRE_BIT_VALUE;
158 #endif
159 var->postBits = POST_BIT_VALUE;
161 ((struct_8bits_joined*)var)->bits = value;
165 void
166 bit_test_0(void)
168 VOLATILE_BITS_DEF;
170 #undef BIT_TO_TEST
171 #define BIT_TO_TEST 0
173 bits_set_value(&volatileBits, 0x00);
174 volatileBits.bitToTest = 1;
175 dummy_f();
176 ASSERT(bits_check_value(&volatileBits, 1 << BIT_TO_TEST));
178 bits_set_value(&volatileBits, 0xFF);
179 volatileBits.bitToTest = 0;
180 dummy_f();
181 ASSERT(bits_check_value(&volatileBits, ~(1 << BIT_TO_TEST)));
183 bits_set_value(&volatileBits, 0x00);
184 volatileBits.bitToTest = ! volatileBits.bitToTest;
185 dummy_f();
186 ASSERT(bits_check_value(&volatileBits, 1 << BIT_TO_TEST));
188 bits_set_value(&volatileBits, 0xFF);
189 volatileBits.bitToTest = ! volatileBits.bitToTest;
190 dummy_f();
191 ASSERT(bits_check_value(&volatileBits, ~(1 << BIT_TO_TEST)));
193 bits_set_value(&volatileBits, 0x00);
194 volatileBits.bitToTest ^= 1;
195 dummy_f();
196 ASSERT(bits_check_value(&volatileBits, 1 << BIT_TO_TEST));
198 bits_set_value(&volatileBits, 0xFF);
199 volatileBits.bitToTest ^= 1;
200 dummy_f();
201 ASSERT(bits_check_value(&volatileBits, ~(1 << BIT_TO_TEST)));
204 void
205 bit_test_1(void)
207 VOLATILE_BITS_DEF;
209 #undef BIT_TO_TEST
210 #define BIT_TO_TEST 1
212 bits_set_value(&volatileBits, 0x00);
213 volatileBits.bitToTest = 1;
214 dummy_f();
215 ASSERT(bits_check_value(&volatileBits, 1 << BIT_TO_TEST));
217 bits_set_value(&volatileBits, 0xFF);
218 volatileBits.bitToTest = 0;
219 dummy_f();
220 ASSERT(bits_check_value(&volatileBits, ~(1 << BIT_TO_TEST)));
222 bits_set_value(&volatileBits, 0x00);
223 volatileBits.bitToTest = ! volatileBits.bitToTest;
224 dummy_f();
225 ASSERT(bits_check_value(&volatileBits, 1 << BIT_TO_TEST));
227 bits_set_value(&volatileBits, 0xFF);
228 volatileBits.bitToTest = ! volatileBits.bitToTest;
229 dummy_f();
230 ASSERT(bits_check_value(&volatileBits, ~(1 << BIT_TO_TEST)));
232 bits_set_value(&volatileBits, 0x00);
233 volatileBits.bitToTest ^= 1;
234 dummy_f();
235 ASSERT(bits_check_value(&volatileBits, 1 << BIT_TO_TEST));
237 bits_set_value(&volatileBits, 0xFF);
238 volatileBits.bitToTest ^= 1;
239 dummy_f();
240 ASSERT(bits_check_value(&volatileBits, ~(1 << BIT_TO_TEST)));
243 void
244 bit_test_2(void)
246 VOLATILE_BITS_DEF;
248 #undef BIT_TO_TEST
249 #define BIT_TO_TEST 2
251 bits_set_value(&volatileBits, 0x00);
252 volatileBits.bitToTest = 1;
253 dummy_f();
254 ASSERT(bits_check_value(&volatileBits, 1 << BIT_TO_TEST));
256 bits_set_value(&volatileBits, 0xFF);
257 volatileBits.bitToTest = 0;
258 dummy_f();
259 ASSERT(bits_check_value(&volatileBits, ~(1 << BIT_TO_TEST)));
261 bits_set_value(&volatileBits, 0x00);
262 volatileBits.bitToTest = ! volatileBits.bitToTest;
263 dummy_f();
264 ASSERT(bits_check_value(&volatileBits, 1 << BIT_TO_TEST));
266 bits_set_value(&volatileBits, 0xFF);
267 volatileBits.bitToTest = ! volatileBits.bitToTest;
268 dummy_f();
269 ASSERT(bits_check_value(&volatileBits, ~(1 << BIT_TO_TEST)));
271 bits_set_value(&volatileBits, 0x00);
272 volatileBits.bitToTest ^= 1;
273 dummy_f();
274 ASSERT(bits_check_value(&volatileBits, 1 << BIT_TO_TEST));
276 bits_set_value(&volatileBits, 0xFF);
277 volatileBits.bitToTest ^= 1;
278 dummy_f();
279 ASSERT(bits_check_value(&volatileBits, ~(1 << BIT_TO_TEST)));
282 void
283 bit_test_3(void)
285 VOLATILE_BITS_DEF;
287 #undef BIT_TO_TEST
288 #define BIT_TO_TEST 3
290 bits_set_value(&volatileBits, 0x00);
291 volatileBits.bitToTest = 1;
292 dummy_f();
293 ASSERT(bits_check_value(&volatileBits, 1 << BIT_TO_TEST));
295 bits_set_value(&volatileBits, 0xFF);
296 volatileBits.bitToTest = 0;
297 dummy_f();
298 ASSERT(bits_check_value(&volatileBits, ~(1 << BIT_TO_TEST)));
300 bits_set_value(&volatileBits, 0x00);
301 volatileBits.bitToTest = ! volatileBits.bitToTest;
302 dummy_f();
303 ASSERT(bits_check_value(&volatileBits, 1 << BIT_TO_TEST));
305 bits_set_value(&volatileBits, 0xFF);
306 volatileBits.bitToTest = ! volatileBits.bitToTest;
307 dummy_f();
308 ASSERT(bits_check_value(&volatileBits, ~(1 << BIT_TO_TEST)));
310 bits_set_value(&volatileBits, 0x00);
311 volatileBits.bitToTest ^= 1;
312 dummy_f();
313 ASSERT(bits_check_value(&volatileBits, 1 << BIT_TO_TEST));
315 bits_set_value(&volatileBits, 0xFF);
316 volatileBits.bitToTest ^= 1;
317 dummy_f();
318 ASSERT(bits_check_value(&volatileBits, ~(1 << BIT_TO_TEST)));
321 void
322 bit_test_4(void)
324 VOLATILE_BITS_DEF;
326 #undef BIT_TO_TEST
327 #define BIT_TO_TEST 4
329 bits_set_value(&volatileBits, 0x00);
330 volatileBits.bitToTest = 1;
331 dummy_f();
332 ASSERT(bits_check_value(&volatileBits, 1 << BIT_TO_TEST));
334 bits_set_value(&volatileBits, 0xFF);
335 volatileBits.bitToTest = 0;
336 dummy_f();
337 ASSERT(bits_check_value(&volatileBits, ~(1 << BIT_TO_TEST)));
339 bits_set_value(&volatileBits, 0x00);
340 volatileBits.bitToTest = ! volatileBits.bitToTest;
341 dummy_f();
342 ASSERT(bits_check_value(&volatileBits, 1 << BIT_TO_TEST));
344 bits_set_value(&volatileBits, 0xFF);
345 volatileBits.bitToTest = ! volatileBits.bitToTest;
346 dummy_f();
347 ASSERT(bits_check_value(&volatileBits, ~(1 << BIT_TO_TEST)));
349 bits_set_value(&volatileBits, 0x00);
350 volatileBits.bitToTest ^= 1;
351 dummy_f();
352 ASSERT(bits_check_value(&volatileBits, 1 << BIT_TO_TEST));
354 bits_set_value(&volatileBits, 0xFF);
355 volatileBits.bitToTest ^= 1;
356 dummy_f();
357 ASSERT(bits_check_value(&volatileBits, ~(1 << BIT_TO_TEST)));
360 void
361 bit_test_5(void)
363 VOLATILE_BITS_DEF;
365 #undef BIT_TO_TEST
366 #define BIT_TO_TEST 5
368 bits_set_value(&volatileBits, 0x00);
369 volatileBits.bitToTest = 1;
370 dummy_f();
371 ASSERT(bits_check_value(&volatileBits, 1 << BIT_TO_TEST));
373 bits_set_value(&volatileBits, 0xFF);
374 volatileBits.bitToTest = 0;
375 dummy_f();
376 ASSERT(bits_check_value(&volatileBits, ~(1 << BIT_TO_TEST)));
378 bits_set_value(&volatileBits, 0x00);
379 volatileBits.bitToTest = ! volatileBits.bitToTest;
380 dummy_f();
381 ASSERT(bits_check_value(&volatileBits, 1 << BIT_TO_TEST));
383 bits_set_value(&volatileBits, 0xFF);
384 volatileBits.bitToTest = ! volatileBits.bitToTest;
385 dummy_f();
386 ASSERT(bits_check_value(&volatileBits, ~(1 << BIT_TO_TEST)));
388 bits_set_value(&volatileBits, 0x00);
389 volatileBits.bitToTest ^= 1;
390 dummy_f();
391 ASSERT(bits_check_value(&volatileBits, 1 << BIT_TO_TEST));
393 bits_set_value(&volatileBits, 0xFF);
394 volatileBits.bitToTest ^= 1;
395 dummy_f();
396 ASSERT(bits_check_value(&volatileBits, ~(1 << BIT_TO_TEST)));
399 void
400 bit_test_6(void)
402 VOLATILE_BITS_DEF;
404 #undef BIT_TO_TEST
405 #define BIT_TO_TEST 6
407 bits_set_value(&volatileBits, 0x00);
408 volatileBits.bitToTest = 1;
409 dummy_f();
410 ASSERT(bits_check_value(&volatileBits, 1 << BIT_TO_TEST));
412 bits_set_value(&volatileBits, 0xFF);
413 volatileBits.bitToTest = 0;
414 dummy_f();
415 ASSERT(bits_check_value(&volatileBits, ~(1 << BIT_TO_TEST)));
417 bits_set_value(&volatileBits, 0x00);
418 volatileBits.bitToTest = ! volatileBits.bitToTest;
419 dummy_f();
420 ASSERT(bits_check_value(&volatileBits, 1 << BIT_TO_TEST));
422 bits_set_value(&volatileBits, 0xFF);
423 volatileBits.bitToTest = ! volatileBits.bitToTest;
424 dummy_f();
425 ASSERT(bits_check_value(&volatileBits, ~(1 << BIT_TO_TEST)));
427 bits_set_value(&volatileBits, 0x00);
428 volatileBits.bitToTest ^= 1;
429 dummy_f();
430 ASSERT(bits_check_value(&volatileBits, 1 << BIT_TO_TEST));
432 bits_set_value(&volatileBits, 0xFF);
433 volatileBits.bitToTest ^= 1;
434 dummy_f();
435 ASSERT(bits_check_value(&volatileBits, ~(1 << BIT_TO_TEST)));
438 void
439 bit_test_7(void)
441 VOLATILE_BITS_DEF;
443 #undef BIT_TO_TEST
444 #define BIT_TO_TEST 7
446 bits_set_value(&volatileBits, 0x00);
447 volatileBits.bitToTest = 1;
448 dummy_f();
449 ASSERT(bits_check_value(&volatileBits, 1 << BIT_TO_TEST));
451 bits_set_value(&volatileBits, 0xFF);
452 volatileBits.bitToTest = 0;
453 dummy_f();
454 ASSERT(bits_check_value(&volatileBits, ~(1 << BIT_TO_TEST)));
456 bits_set_value(&volatileBits, 0x00);
457 volatileBits.bitToTest = ! volatileBits.bitToTest;
458 dummy_f();
459 ASSERT(bits_check_value(&volatileBits, 1 << BIT_TO_TEST));
461 bits_set_value(&volatileBits, 0xFF);
462 volatileBits.bitToTest = ! volatileBits.bitToTest;
463 dummy_f();
464 ASSERT(bits_check_value(&volatileBits, ~(1 << BIT_TO_TEST)));
466 bits_set_value(&volatileBits, 0x00);
467 volatileBits.bitToTest ^= 1;
468 dummy_f();
469 ASSERT(bits_check_value(&volatileBits, 1 << BIT_TO_TEST));
471 bits_set_value(&volatileBits, 0xFF);
472 volatileBits.bitToTest ^= 1;
473 dummy_f();
474 ASSERT(bits_check_value(&volatileBits, ~(1 << BIT_TO_TEST)));
476 #endif
478 static void
479 testBitfields(void)
481 #ifndef DUMMY_CASE
482 bit_test_0();
483 bit_test_1();
484 bit_test_2();
485 bit_test_3();
487 bit_test_4();
488 bit_test_5();
489 bit_test_6();
490 bit_test_7();
491 #endif