blob: d3284c62ae15a19e59122d9171d535a3502f6713 [file] [log] [blame]
Austin Schuh41baf202022-01-01 14:33:40 -08001/*
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
27/** \ingroup Group_Common
28 * \defgroup Group_Compiler Compiler
29 * \brief Group_Compiler brief
30 * @{ */
31
32#ifndef _TUSB_COMPILER_H_
33#define _TUSB_COMPILER_H_
34
35#define TU_TOKEN(x) x
36#define TU_STRING(x) #x ///< stringify without expand
37#define TU_XSTRING(x) TU_STRING(x) ///< expand then stringify
38
39#define TU_STRCAT(a, b) a##b ///< concat without expand
40#define TU_STRCAT3(a, b, c) a##b##c ///< concat without expand
41
42#define TU_XSTRCAT(a, b) TU_STRCAT(a, b) ///< expand then concat
43#define TU_XSTRCAT3(a, b, c) TU_STRCAT3(a, b, c) ///< expand then concat 3 tokens
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 (__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
55 #define TU_VERIFY_STATIC _Static_assert
56#elif defined (__cplusplus) && __cplusplus >= 201103L
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// for declaration of reserved field, make use of _TU_COUNTER_
65#define TU_RESERVED TU_XSTRCAT(reserved, _TU_COUNTER_)
66
67#define TU_LITTLE_ENDIAN (0x12u)
68#define TU_BIG_ENDIAN (0x21u)
69
70/*------------------------------------------------------------------*/
71/* Count number of arguments of __VA_ARGS__
72 * - reference https://stackoverflow.com/questions/2124339/c-preprocessor-va-args-number-of-arguments
73 * - _GET_NTH_ARG() takes args >= N (64) but only expand to Nth one (64th)
74 * - _RSEQ_N() is reverse sequential to N to add padding to have
75 * Nth position is the same as the number of arguments
76 * - ##__VA_ARGS__ is used to deal with 0 paramerter (swallows comma)
77 *------------------------------------------------------------------*/
78#define TU_ARGS_NUM(...) _TU_NARG(_0, ##__VA_ARGS__,_RSEQ_N())
79
80#define _TU_NARG(...) _GET_NTH_ARG(__VA_ARGS__)
81#define _GET_NTH_ARG( \
82 _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
83 _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
84 _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
85 _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
86 _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
87 _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
88 _61,_62,_63,N,...) N
89#define _RSEQ_N() \
90 62,61,60, \
91 59,58,57,56,55,54,53,52,51,50, \
92 49,48,47,46,45,44,43,42,41,40, \
93 39,38,37,36,35,34,33,32,31,30, \
94 29,28,27,26,25,24,23,22,21,20, \
95 19,18,17,16,15,14,13,12,11,10, \
96 9,8,7,6,5,4,3,2,1,0
97
98// Apply an macro X to each of the arguments with an separated of choice
99#define TU_ARGS_APPLY(_X, _s, ...) TU_XSTRCAT(_TU_ARGS_APPLY_, TU_ARGS_NUM(__VA_ARGS__))(_X, _s, __VA_ARGS__)
100
101#define _TU_ARGS_APPLY_1(_X, _s, _a1) _X(_a1)
102#define _TU_ARGS_APPLY_2(_X, _s, _a1, _a2) _X(_a1) _s _X(_a2)
103#define _TU_ARGS_APPLY_3(_X, _s, _a1, _a2, _a3) _X(_a1) _s _TU_ARGS_APPLY_2(_X, _s, _a2, _a3)
104#define _TU_ARGS_APPLY_4(_X, _s, _a1, _a2, _a3, _a4) _X(_a1) _s _TU_ARGS_APPLY_3(_X, _s, _a2, _a3, _a4)
105#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)
106#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)
107#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)
108#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)
109
110//--------------------------------------------------------------------+
111// Compiler porting with Attribute and Endian
112//--------------------------------------------------------------------+
113
114// TODO refactor since __attribute__ is supported across many compiler
115#if defined(__GNUC__)
116 #define TU_ATTR_ALIGNED(Bytes) __attribute__ ((aligned(Bytes)))
117 #define TU_ATTR_SECTION(sec_name) __attribute__ ((section(#sec_name)))
118 #define TU_ATTR_PACKED __attribute__ ((packed))
119 #define TU_ATTR_WEAK __attribute__ ((weak))
120 #define TU_ATTR_ALWAYS_INLINE __attribute__ ((always_inline))
121 #define TU_ATTR_DEPRECATED(mess) __attribute__ ((deprecated(mess))) // warn if function with this attribute is used
122 #define TU_ATTR_UNUSED __attribute__ ((unused)) // Function/Variable is meant to be possibly unused
123 #define TU_ATTR_USED __attribute__ ((used)) // Function/Variable is meant to be used
124
125 #define TU_ATTR_PACKED_BEGIN
126 #define TU_ATTR_PACKED_END
127 #define TU_ATTR_BIT_FIELD_ORDER_BEGIN
128 #define TU_ATTR_BIT_FIELD_ORDER_END
129
130 // Endian conversion use well-known host to network (big endian) naming
131 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
132 #define TU_BYTE_ORDER TU_LITTLE_ENDIAN
133 #else
134 #define TU_BYTE_ORDER TU_BIG_ENDIAN
135 #endif
136
137 #define TU_BSWAP16(u16) (__builtin_bswap16(u16))
138 #define TU_BSWAP32(u32) (__builtin_bswap32(u32))
139
140 // List of obsolete callback function that is renamed and should not be defined.
141 // Put it here since only gcc support this pragma
142 #pragma GCC poison tud_vendor_control_request_cb
143
144#elif defined(__TI_COMPILER_VERSION__)
145 #define TU_ATTR_ALIGNED(Bytes) __attribute__ ((aligned(Bytes)))
146 #define TU_ATTR_SECTION(sec_name) __attribute__ ((section(#sec_name)))
147 #define TU_ATTR_PACKED __attribute__ ((packed))
148 #define TU_ATTR_WEAK __attribute__ ((weak))
149 #define TU_ATTR_ALWAYS_INLINE __attribute__ ((always_inline))
150 #define TU_ATTR_DEPRECATED(mess) __attribute__ ((deprecated(mess))) // warn if function with this attribute is used
151 #define TU_ATTR_UNUSED __attribute__ ((unused)) // Function/Variable is meant to be possibly unused
152 #define TU_ATTR_USED __attribute__ ((used))
153
154 #define TU_ATTR_PACKED_BEGIN
155 #define TU_ATTR_PACKED_END
156 #define TU_ATTR_BIT_FIELD_ORDER_BEGIN
157 #define TU_ATTR_BIT_FIELD_ORDER_END
158
159 // __BYTE_ORDER is defined in the TI ARM compiler, but not MSP430 (which is little endian)
160 #if ((__BYTE_ORDER__) == (__ORDER_LITTLE_ENDIAN__)) || defined(__MSP430__)
161 #define TU_BYTE_ORDER TU_LITTLE_ENDIAN
162 #else
163 #define TU_BYTE_ORDER TU_BIG_ENDIAN
164 #endif
165
166 #define TU_BSWAP16(u16) (__builtin_bswap16(u16))
167 #define TU_BSWAP32(u32) (__builtin_bswap32(u32))
168
169#elif defined(__ICCARM__)
170 #include <intrinsics.h>
171 #define TU_ATTR_ALIGNED(Bytes) __attribute__ ((aligned(Bytes)))
172 #define TU_ATTR_SECTION(sec_name) __attribute__ ((section(#sec_name)))
173 #define TU_ATTR_PACKED __attribute__ ((packed))
174 #define TU_ATTR_WEAK __attribute__ ((weak))
175 #define TU_ATTR_ALWAYS_INLINE __attribute__ ((always_inline))
176 #define TU_ATTR_DEPRECATED(mess) __attribute__ ((deprecated(mess))) // warn if function with this attribute is used
177 #define TU_ATTR_UNUSED __attribute__ ((unused)) // Function/Variable is meant to be possibly unused
178 #define TU_ATTR_USED __attribute__ ((used)) // Function/Variable is meant to be used
179
180 #define TU_ATTR_PACKED_BEGIN
181 #define TU_ATTR_PACKED_END
182 #define TU_ATTR_BIT_FIELD_ORDER_BEGIN
183 #define TU_ATTR_BIT_FIELD_ORDER_END
184
185 // Endian conversion use well-known host to network (big endian) naming
186 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
187 #define TU_BYTE_ORDER TU_LITTLE_ENDIAN
188 #else
189 #define TU_BYTE_ORDER TU_BIG_ENDIAN
190 #endif
191
192 #define TU_BSWAP16(u16) (__iar_builtin_REV16(u16))
193 #define TU_BSWAP32(u32) (__iar_builtin_REV(u32))
194
195#elif defined(__CCRX__)
196 #define TU_ATTR_ALIGNED(Bytes)
197 #define TU_ATTR_SECTION(sec_name)
198 #define TU_ATTR_PACKED
199 #define TU_ATTR_WEAK
200 #define TU_ATTR_ALWAYS_INLINE
201 #define TU_ATTR_DEPRECATED(mess)
202 #define TU_ATTR_UNUSED
203 #define TU_ATTR_USED
204
205 #define TU_ATTR_PACKED_BEGIN _Pragma("pack")
206 #define TU_ATTR_PACKED_END _Pragma("packoption")
207 #define TU_ATTR_BIT_FIELD_ORDER_BEGIN _Pragma("bit_order right")
208 #define TU_ATTR_BIT_FIELD_ORDER_END _Pragma("bit_order")
209
210 // Endian conversion use well-known host to network (big endian) naming
211 #if defined(__LIT)
212 #define TU_BYTE_ORDER TU_LITTLE_ENDIAN
213 #else
214 #define TU_BYTE_ORDER TU_BIG_ENDIAN
215 #endif
216
217 #define TU_BSWAP16(u16) ((unsigned short)_builtin_revw((unsigned long)u16))
218 #define TU_BSWAP32(u32) (_builtin_revl(u32))
219
220#else
221 #error "Compiler attribute porting is required"
222#endif
223
224#if (TU_BYTE_ORDER == TU_LITTLE_ENDIAN)
225
226 #define tu_htons(u16) (TU_BSWAP16(u16))
227 #define tu_ntohs(u16) (TU_BSWAP16(u16))
228
229 #define tu_htonl(u32) (TU_BSWAP32(u32))
230 #define tu_ntohl(u32) (TU_BSWAP32(u32))
231
232 #define tu_htole16(u16) (u16)
233 #define tu_le16toh(u16) (u16)
234
235 #define tu_htole32(u32) (u32)
236 #define tu_le32toh(u32) (u32)
237
238#elif (TU_BYTE_ORDER == TU_BIG_ENDIAN)
239
240 #define tu_htons(u16) (u16)
241 #define tu_ntohs(u16) (u16)
242
243 #define tu_htonl(u32) (u32)
244 #define tu_ntohl(u32) (u32)
245
246 #define tu_htole16(u16) (TU_BSWAP16(u16))
247 #define tu_le16toh(u16) (TU_BSWAP16(u16))
248
249 #define tu_htole32(u32) (TU_BSWAP32(u32))
250 #define tu_le32toh(u32) (TU_BSWAP32(u32))
251
252#else
253 #error Byte order is undefined
254#endif
255
256#endif /* _TUSB_COMPILER_H_ */
257
258/// @}