Pick three bugfixes from next branch to trunk for inclusion in 4.5.0 RC2, as discusse...
[sdcc.git] / sdcc / support / regression / tests / bug-3788.c
blob0b60862a2ac586c290b04df9f7934bff7221ca86
1 /** bug-3788.c: The value of a write to a bit-field in a union member was used as read value for another union member.
2 */
4 #include <testfwk.h>
6 enum g_bits_defs {
7 BITS_RESERVED_0 = 0x1, BITS_RESERVED_1 = 0x2,
8 BITS_RESERVED_2 = 0x4, BITS_RESERVED_3 = 0x8,
9 MYBIT4 = 0x10, MYBIT5 = 0x20,
10 BITS_RESERVED_6 = 0x40, BITS_RESERVED_7 = 0x80 };
12 union {
13 struct {
14 unsigned char BITS_RESERVED_0 : 1;
15 unsigned char BITS_RESERVED_1 : 1;
16 unsigned char BITS_RESERVED_2 : 1;
17 unsigned char BITS_RESERVED_3 : 1;
18 unsigned char MYBIT4 : 1;
19 unsigned char MYBIT5 : 1;
20 unsigned char BITS_RESERVED_6 : 1;
21 unsigned char BITS_RESERVED_7 : 1;
22 } a;
23 unsigned char is;
24 } g_bits;
26 void print_special( unsigned char c );
28 void send_loop_try( unsigned char c )
30 (g_bits).a.MYBIT5 = 1;;
31 if ( ((MYBIT4) & (g_bits).is) ) // The read of (g_bits).is was incorrectly optimized into a constant 1.
32 print_special( c );
35 unsigned char d = 0x5a;
37 void testBug( void )
39 (g_bits).a.MYBIT4 = 1;;
40 send_loop_try( 0xa5 );
41 // The order of allocation of bit-fields within a unit
42 // (high-order to low-order or low-order to high-order) is
43 // implementation-defined. This test assumes the SDCC order,
44 // It fails e.g. for powerpc64-linux-gnu.
45 #ifdef __SDCC
46 ASSERT (d == 0xa5);
47 #endif
50 void print_special( unsigned char c )
52 d = c;