Pick three bugfixes from next branch to trunk for inclusion in 4.5.0 RC2, as discusse...
[sdcc.git] / sdcc / support / regression / tests / bitfields-checks.c.in
blobab7c483078a616865a3cb1570cfe0be6791e8d1b
1 /** Test for checks of bitfields or bits in a byte.
2 struct: 0, 1
3 varType: 0, 1, 2, 3, 4, 5, 6
4 bit: 0, 1, 2, 3, 4, 5, 6, 7
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 a lack of memory, still.
11 // mcs51 creates invalid assembler. 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 #include <testfwk.h>
17 #ifdef __sun__
18 #include <inttypes.h>
19 #else
20 #include <stdint.h>
21 #endif
23 #define VAR_TYPE ({varType})
24 #define BIT_TO_TEST {bit}
25 #define TYPE_IS_STRUCT ({struct})
28 typedef struct
30 // Use MSB to LSB order for hosts that use this order for bitfields
31 #if defined(PORT_HOST) && (defined(__ppc__) || defined(__PPC__) || defined(__sparc) || defined(__sparc64__))
32 unsigned int bit7 : 1;
33 unsigned int bit6 : 1;
34 unsigned int bit5 : 1;
35 unsigned int bit4 : 1;
36 unsigned int bit3 : 1;
37 unsigned int bit2 : 1;
38 unsigned int bit1 : 1;
39 unsigned int bit0 : 1;
40 #else
41 // Use LSB to MSB order for SDCC and generic hosts (likely to be
42 // x86 or other little endian)
43 unsigned int bit0 : 1;
44 unsigned int bit1 : 1;
45 unsigned int bit2 : 1;
46 unsigned int bit3 : 1;
47 unsigned int bit4 : 1;
48 unsigned int bit5 : 1;
49 unsigned int bit6 : 1;
50 unsigned int bit7 : 1;
51 #endif
52 }struct_8bits;
54 #define bitToTest__(b) bit ## b
55 #define bitToTest_(b) bitToTest__(b)
57 #define bitToTest bitToTest_(BIT_TO_TEST)
59 #if TYPE_IS_STRUCT
60 #define TYPE struct_8bits
61 #else
62 #define TYPE uint8_t
63 #endif
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 TYPE volatileBits;
88 #elif VAR_TYPE == 1
89 #ifdef ABS_ADDR1
90 #define volatileBits (*(volatile TYPE*)ABS_ADDR1)
91 #else
92 #define DUMMY_CASE
93 #endif
94 #elif VAR_TYPE == 2
95 #ifdef ABS_ADDR2
96 #define volatileBits (*(volatile TYPE*)ABS_ADDR2)
97 #else
98 #define DUMMY_CASE
99 #endif
100 #elif VAR_TYPE == 3
101 #define VOLATILE_BITS_DEF volatile TYPE volatileBits
102 #elif VAR_TYPE == 4
103 #define VOLATILE_BITS_DEF static volatile TYPE volatileBits
104 #elif VAR_TYPE == 5
105 #ifdef ABS_ADDR1
106 #define VOLATILE_BITS_DEF static volatile TYPE __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 TYPE __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
126 #ifndef DUMMY_CASE
128 #if TYPE_IS_STRUCT
129 #define AS_UINT8(x) (*(uint8_t *)&x)
130 void
131 bit_test_byte(void)
133 VOLATILE_BITS_DEF;
134 volatile uint8_t dummy;
136 AS_UINT8(volatileBits) = 1 << BIT_TO_TEST;
137 ASSERT(AS_UINT8(volatileBits) == (1 << BIT_TO_TEST));
138 ASSERT(volatileBits.bitToTest);
139 dummy = 0;
140 if(volatileBits.bitToTest) dummy = 1;
141 ASSERT(dummy);
143 AS_UINT8(volatileBits) = ~(1 << BIT_TO_TEST);
144 ASSERT(AS_UINT8(volatileBits) == (uint8_t)(~(1 << BIT_TO_TEST)));
145 ASSERT(!volatileBits.bitToTest);
146 dummy = 0;
147 if(!volatileBits.bitToTest) dummy = 1;
148 ASSERT(dummy);
150 AS_UINT8(volatileBits) = 0x00;
151 volatileBits.bitToTest = 1;
152 ASSERT(AS_UINT8(volatileBits) == (1 << BIT_TO_TEST));
154 AS_UINT8(volatileBits) = 0xFF;
155 volatileBits.bitToTest = 1;
156 ASSERT(AS_UINT8(volatileBits) == 0xFF);
158 AS_UINT8(volatileBits) = 0x00;
159 volatileBits.bitToTest = 0;
160 ASSERT(AS_UINT8(volatileBits) == 0x00);
162 AS_UINT8(volatileBits) = 0xFF;
163 volatileBits.bitToTest = 0;
164 ASSERT(AS_UINT8(volatileBits) == (uint8_t)(~(1 << BIT_TO_TEST)));
166 AS_UINT8(volatileBits) = 0x00;
167 volatileBits.bitToTest ^= 1;
168 ASSERT(AS_UINT8(volatileBits) == (1 << BIT_TO_TEST));
170 AS_UINT8(volatileBits) = 0xFF;
171 volatileBits.bitToTest ^= 1;
172 ASSERT(AS_UINT8(volatileBits) == (uint8_t)(~(1 << BIT_TO_TEST)));
174 AS_UINT8(volatileBits) = 0x00; // keep it to keep environment of all assert calls the same way
176 #else
177 void
178 bit_test_byte(void)
180 VOLATILE_BITS_DEF;
181 volatile uint8_t dummy;
183 // Casts to int16_t are used because the code generator generates different code
184 // and as a result also different peephole rules are applied
186 volatileBits = 1 << BIT_TO_TEST;
187 ASSERT(volatileBits & (1 << BIT_TO_TEST));
188 ASSERT(volatileBits & (int16_t)(1 << BIT_TO_TEST));
189 dummy = 0;
190 if(volatileBits & (int16_t)(1 << BIT_TO_TEST)) dummy = 1;
191 ASSERT(dummy);
193 volatileBits = ~(1 << BIT_TO_TEST);
194 ASSERT(!(volatileBits & (1 << BIT_TO_TEST)));
195 ASSERT(!(volatileBits & (int16_t)(1 << BIT_TO_TEST)));
196 dummy = 0;
197 if(!(volatileBits & (int16_t)(1 << BIT_TO_TEST))) dummy = 1;
198 ASSERT(dummy);
200 volatileBits = 0x00;
201 volatileBits |= 1 << BIT_TO_TEST;
202 ASSERT(volatileBits == (1 << BIT_TO_TEST));
204 volatileBits = 0xFF;
205 volatileBits |= 1 << BIT_TO_TEST;
206 ASSERT(volatileBits == 0xFF);
208 volatileBits = 0x00;
209 volatileBits &= (uint8_t)(~(1 << BIT_TO_TEST));
210 ASSERT(volatileBits == 0x00);
212 volatileBits = 0xFF;
213 volatileBits &= (uint8_t)(~(1 << BIT_TO_TEST));
214 ASSERT(volatileBits == (uint8_t)(~(1 << BIT_TO_TEST)));
216 volatileBits = 0x00;
217 volatileBits ^= 1 << BIT_TO_TEST;
218 ASSERT(volatileBits == (1 << BIT_TO_TEST));
220 volatileBits = 0xFF;
221 volatileBits ^= 1 << BIT_TO_TEST;
222 ASSERT(volatileBits == (uint8_t)(~(1 << BIT_TO_TEST)));
224 volatileBits = 0x00; // keep it to keep environment of all assert calls the same way
226 #endif
228 #endif // DUMMY_CASE
230 static void
231 testBitfieldsChecks(void)
233 #ifndef DUMMY_CASE
234 bit_test_byte();
235 #endif