mikroSDK Reference Manual
tusb_compiler.h
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2019 Ha Thach (tinyusb.org)
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 * This file is part of the TinyUSB stack.
25 */
26
32#ifndef _TUSB_COMPILER_H_
33#define _TUSB_COMPILER_H_
34
35#define TU_TOKEN(x) x
36#define TU_STRING(x) #x
37#define TU_XSTRING(x) TU_STRING(x)
38
39#define TU_STRCAT(a, b) a##b
40#define TU_STRCAT3(a, b, c) a##b##c
41
42#define TU_XSTRCAT(a, b) TU_STRCAT(a, b)
43#define TU_XSTRCAT3(a, b, c) TU_STRCAT3(a, b, c)
44
45#define TU_INCLUDE_PATH(_dir,_file) TU_XSTRING( TU_TOKEN(_dir)TU_TOKEN(_file) )
46
47#if defined __COUNTER__ && __COUNTER__ != __COUNTER__
48 #define _TU_COUNTER_ __COUNTER__
49#else
50 #define _TU_COUNTER_ __LINE__
51#endif
52
53// Compile-time Assert
54#if defined (__cplusplus) && __cplusplus >= 201103L
55 #define TU_VERIFY_STATIC static_assert
56#elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
57 #define TU_VERIFY_STATIC _Static_assert
58#elif defined(__CCRX__)
59 #define TU_VERIFY_STATIC(const_expr, _mess) typedef char TU_XSTRCAT(Line, __LINE__)[(const_expr) ? 1 : 0];
60#else
61 #define TU_VERIFY_STATIC(const_expr, _mess) enum { TU_XSTRCAT(_verify_static_, _TU_COUNTER_) = 1/(!!(const_expr)) }
62#endif
63
64/* --------------------- Fuzzing types -------------------------------------- */
65#ifdef _FUZZ
66 #define tu_static static __thread
67#else
68 #define tu_static static
69#endif
70
71// for declaration of reserved field, make use of _TU_COUNTER_
72#define TU_RESERVED TU_XSTRCAT(reserved, _TU_COUNTER_)
73
74#define TU_LITTLE_ENDIAN (0x12u)
75#define TU_BIG_ENDIAN (0x21u)
76
77/*------------------------------------------------------------------*/
78/* Count number of arguments of __VA_ARGS__
79 * - reference https://stackoverflow.com/questions/2124339/c-preprocessor-va-args-number-of-arguments
80 * - _GET_NTH_ARG() takes args >= N (64) but only expand to Nth one (64th)
81 * - _RSEQ_N() is reverse sequential to N to add padding to have
82 * Nth position is the same as the number of arguments
83 * - ##__VA_ARGS__ is used to deal with 0 paramerter (swallows comma)
84 *------------------------------------------------------------------*/
85#if !defined(__CCRX__)
86#define TU_ARGS_NUM(...) _TU_NARG(_0, ##__VA_ARGS__, _RSEQ_N())
87#else
88#define TU_ARGS_NUM(...) _TU_NARG(_0, __VA_ARGS__, _RSEQ_N())
89#endif
90
91#define _TU_NARG(...) _GET_NTH_ARG(__VA_ARGS__)
92#define _GET_NTH_ARG( \
93 _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
94 _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
95 _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
96 _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
97 _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
98 _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
99 _61,_62,_63,N,...) N
100#define _RSEQ_N() \
101 62,61,60, \
102 59,58,57,56,55,54,53,52,51,50, \
103 49,48,47,46,45,44,43,42,41,40, \
104 39,38,37,36,35,34,33,32,31,30, \
105 29,28,27,26,25,24,23,22,21,20, \
106 19,18,17,16,15,14,13,12,11,10, \
107 9,8,7,6,5,4,3,2,1,0
108
109// Apply an macro X to each of the arguments with an separated of choice
110#define TU_ARGS_APPLY(_X, _s, ...) TU_XSTRCAT(_TU_ARGS_APPLY_, TU_ARGS_NUM(__VA_ARGS__))(_X, _s, __VA_ARGS__)
111
112#define _TU_ARGS_APPLY_1(_X, _s, _a1) _X(_a1)
113#define _TU_ARGS_APPLY_2(_X, _s, _a1, _a2) _X(_a1) _s _X(_a2)
114#define _TU_ARGS_APPLY_3(_X, _s, _a1, _a2, _a3) _X(_a1) _s _TU_ARGS_APPLY_2(_X, _s, _a2, _a3)
115#define _TU_ARGS_APPLY_4(_X, _s, _a1, _a2, _a3, _a4) _X(_a1) _s _TU_ARGS_APPLY_3(_X, _s, _a2, _a3, _a4)
116#define _TU_ARGS_APPLY_5(_X, _s, _a1, _a2, _a3, _a4, _a5) _X(_a1) _s _TU_ARGS_APPLY_4(_X, _s, _a2, _a3, _a4, _a5)
117#define _TU_ARGS_APPLY_6(_X, _s, _a1, _a2, _a3, _a4, _a5, _a6) _X(_a1) _s _TU_ARGS_APPLY_5(_X, _s, _a2, _a3, _a4, _a5, _a6)
118#define _TU_ARGS_APPLY_7(_X, _s, _a1, _a2, _a3, _a4, _a5, _a6, _a7) _X(_a1) _s _TU_ARGS_APPLY_6(_X, _s, _a2, _a3, _a4, _a5, _a6, _a7)
119#define _TU_ARGS_APPLY_8(_X, _s, _a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8) _X(_a1) _s _TU_ARGS_APPLY_7(_X, _s, _a2, _a3, _a4, _a5, _a6, _a7, _a8)
120
121//--------------------------------------------------------------------+
122// Compiler porting with Attribute and Endian
123//--------------------------------------------------------------------+
124
125// TODO refactor since __attribute__ is supported across many compiler
126#if defined(__GNUC__)
127 #define TU_ATTR_ALIGNED(Bytes) __attribute__ ((aligned(Bytes)))
128 #define TU_ATTR_SECTION(sec_name) __attribute__ ((section(#sec_name)))
129 #define TU_ATTR_PACKED __attribute__ ((packed))
130 #define TU_ATTR_WEAK __attribute__ ((weak))
131 #ifndef TU_ATTR_ALWAYS_INLINE // allow to override for debug
132 #define TU_ATTR_ALWAYS_INLINE __attribute__ ((always_inline))
133 #endif
134 #define TU_ATTR_DEPRECATED(mess) __attribute__ ((deprecated(mess))) // warn if function with this attribute is used
135 #define TU_ATTR_UNUSED __attribute__ ((unused)) // Function/Variable is meant to be possibly unused
136 #define TU_ATTR_USED __attribute__ ((used)) // Function/Variable is meant to be used
137
138 #define TU_ATTR_PACKED_BEGIN
139 #define TU_ATTR_PACKED_END
140 #define TU_ATTR_BIT_FIELD_ORDER_BEGIN
141 #define TU_ATTR_BIT_FIELD_ORDER_END
142
143 #if __GNUC__ < 5
144 #define TU_ATTR_FALLTHROUGH do {} while (0) /* fallthrough */
145 #else
146 #if __has_attribute(__fallthrough__)
147 #define TU_ATTR_FALLTHROUGH __attribute__((fallthrough))
148 #else
149 #define TU_ATTR_FALLTHROUGH do {} while (0) /* fallthrough */
150 #endif
151 #endif
152
153 // Endian conversion use well-known host to network (big endian) naming
154 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
155 #define TU_BYTE_ORDER TU_LITTLE_ENDIAN
156 #else
157 #define TU_BYTE_ORDER TU_BIG_ENDIAN
158 #endif
159
160 // Unfortunately XC16 doesn't provide builtins for 32bit endian conversion
161 #if defined(__XC16)
162 #define TU_BSWAP16(u16) (__builtin_swap(u16))
163 #define TU_BSWAP32(u32) ((((u32) & 0xff000000) >> 24) | \
164 (((u32) & 0x00ff0000) >> 8) | \
165 (((u32) & 0x0000ff00) << 8) | \
166 (((u32) & 0x000000ff) << 24))
167 #else
168 #define TU_BSWAP16(u16) (__builtin_bswap16(u16))
169 #define TU_BSWAP32(u32) (__builtin_bswap32(u32))
170 #endif
171
172 #ifndef __ARMCC_VERSION
173 // List of obsolete callback function that is renamed and should not be defined.
174 // Put it here since only gcc support this pragma
175 #pragma GCC poison tud_vendor_control_request_cb
176 #endif
177
178#elif defined(__TI_COMPILER_VERSION__)
179 #define TU_ATTR_ALIGNED(Bytes) __attribute__ ((aligned(Bytes)))
180 #define TU_ATTR_SECTION(sec_name) __attribute__ ((section(#sec_name)))
181 #define TU_ATTR_PACKED __attribute__ ((packed))
182 #define TU_ATTR_WEAK __attribute__ ((weak))
183 #define TU_ATTR_ALWAYS_INLINE __attribute__ ((always_inline))
184 #define TU_ATTR_DEPRECATED(mess) __attribute__ ((deprecated(mess))) // warn if function with this attribute is used
185 #define TU_ATTR_UNUSED __attribute__ ((unused)) // Function/Variable is meant to be possibly unused
186 #define TU_ATTR_USED __attribute__ ((used))
187 #define TU_ATTR_FALLTHROUGH __attribute__((fallthrough))
188
189 #define TU_ATTR_PACKED_BEGIN
190 #define TU_ATTR_PACKED_END
191 #define TU_ATTR_BIT_FIELD_ORDER_BEGIN
192 #define TU_ATTR_BIT_FIELD_ORDER_END
193
194 // __BYTE_ORDER is defined in the TI ARM compiler, but not MSP430 (which is little endian)
195 #if ((__BYTE_ORDER__) == (__ORDER_LITTLE_ENDIAN__)) || defined(__MSP430__)
196 #define TU_BYTE_ORDER TU_LITTLE_ENDIAN
197 #else
198 #define TU_BYTE_ORDER TU_BIG_ENDIAN
199 #endif
200
201 #define TU_BSWAP16(u16) (__builtin_bswap16(u16))
202 #define TU_BSWAP32(u32) (__builtin_bswap32(u32))
203
204#elif defined(__ICCARM__)
205 #include <intrinsics.h>
206 #define TU_ATTR_ALIGNED(Bytes) __attribute__ ((aligned(Bytes)))
207 #define TU_ATTR_SECTION(sec_name) __attribute__ ((section(#sec_name)))
208 #define TU_ATTR_PACKED __attribute__ ((packed))
209 #define TU_ATTR_WEAK __attribute__ ((weak))
210 #ifndef TU_ATTR_ALWAYS_INLINE // allow to override for debug
211 #define TU_ATTR_ALWAYS_INLINE __attribute__ ((always_inline))
212 #endif
213 #define TU_ATTR_DEPRECATED(mess) __attribute__ ((deprecated(mess))) // warn if function with this attribute is used
214 #define TU_ATTR_UNUSED __attribute__ ((unused)) // Function/Variable is meant to be possibly unused
215 #define TU_ATTR_USED __attribute__ ((used)) // Function/Variable is meant to be used
216 #define TU_ATTR_FALLTHROUGH do {} while (0) /* fallthrough */
217
218 #define TU_ATTR_PACKED_BEGIN
219 #define TU_ATTR_PACKED_END
220 #define TU_ATTR_BIT_FIELD_ORDER_BEGIN
221 #define TU_ATTR_BIT_FIELD_ORDER_END
222
223 // Endian conversion use well-known host to network (big endian) naming
224 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
225 #define TU_BYTE_ORDER TU_LITTLE_ENDIAN
226 #else
227 #define TU_BYTE_ORDER TU_BIG_ENDIAN
228 #endif
229
230 #define TU_BSWAP16(u16) (__iar_builtin_REV16(u16))
231 #define TU_BSWAP32(u32) (__iar_builtin_REV(u32))
232
233#elif defined(__CCRX__)
234 #define TU_ATTR_ALIGNED(Bytes)
235 #define TU_ATTR_SECTION(sec_name)
236 #define TU_ATTR_PACKED
237 #define TU_ATTR_WEAK
238 #define TU_ATTR_ALWAYS_INLINE
239 #define TU_ATTR_DEPRECATED(mess)
240 #define TU_ATTR_UNUSED
241 #define TU_ATTR_USED
242 #define TU_ATTR_FALLTHROUGH do {} while (0) /* fallthrough */
243
244 #define TU_ATTR_PACKED_BEGIN _Pragma("pack")
245 #define TU_ATTR_PACKED_END _Pragma("packoption")
246 #define TU_ATTR_BIT_FIELD_ORDER_BEGIN _Pragma("bit_order right")
247 #define TU_ATTR_BIT_FIELD_ORDER_END _Pragma("bit_order")
248
249 // Endian conversion use well-known host to network (big endian) naming
250 #if defined(__LIT)
251 #define TU_BYTE_ORDER TU_LITTLE_ENDIAN
252 #else
253 #define TU_BYTE_ORDER TU_BIG_ENDIAN
254 #endif
255
256 #define TU_BSWAP16(u16) ((unsigned short)_builtin_revw((unsigned long)u16))
257 #define TU_BSWAP32(u32) (_builtin_revl(u32))
258
259#else
260 #error "Compiler attribute porting is required"
261#endif
262
263
264#if (TU_BYTE_ORDER == TU_LITTLE_ENDIAN)
265
266 #define tu_htons(u16) (TU_BSWAP16(u16))
267 #define tu_ntohs(u16) (TU_BSWAP16(u16))
268
269 #define tu_htonl(u32) (TU_BSWAP32(u32))
270 #define tu_ntohl(u32) (TU_BSWAP32(u32))
271
272 #define tu_htole16(u16) (u16)
273 #define tu_le16toh(u16) (u16)
274
275 #define tu_htole32(u32) (u32)
276 #define tu_le32toh(u32) (u32)
277
278#elif (TU_BYTE_ORDER == TU_BIG_ENDIAN)
279
280 #define tu_htons(u16) (u16)
281 #define tu_ntohs(u16) (u16)
282
283 #define tu_htonl(u32) (u32)
284 #define tu_ntohl(u32) (u32)
285
286 #define tu_htole16(u16) (TU_BSWAP16(u16))
287 #define tu_le16toh(u16) (TU_BSWAP16(u16))
288
289 #define tu_htole32(u32) (TU_BSWAP32(u32))
290 #define tu_le32toh(u32) (TU_BSWAP32(u32))
291
292#else
293 #error Byte order is undefined
294#endif
295
296#endif /* _TUSB_COMPILER_H_ */
297