source: rtems/cpukit/include/rtems/score/basedefs.h @ 4fea054c

5
Last change on this file since 4fea054c was 1d39e96, checked in by Sebastian Huber <sebastian.huber@…>, on 10/05/18 at 06:11:09

score: Fix legacy RTEMS_STATIC_ASSERT()

In standard C pointer operands are not allowed in integer constant
expressions. Avoid a static assertion based on an array typedef since
this could lead to warnings ("variably modified 'x' at file scope" and
"typedef 'x' locally defined but not used");

This implementation requires unique messages.

  • Property mode set to 100644
File size: 12.5 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup Score
5 *
6 * @brief Basic Definitions
7 */
8
9/*
10 *  COPYRIGHT (c) 1989-2007.
11 *  On-Line Applications Research Corporation (OAR).
12 *
13 *  Copyright (c) 2010, 2018 embedded brains GmbH.
14 *
15 *  The license and distribution terms for this file may be
16 *  found in the file LICENSE in this distribution or at
17 *  http://www.rtems.org/license/LICENSE.
18 */
19
20#ifndef _RTEMS_BASEDEFS_H
21#define _RTEMS_BASEDEFS_H
22
23/**
24 *  @defgroup ScoreBaseDefs Basic Definitions
25 *
26 *  @ingroup Score
27 */
28/**@{*/
29
30#include <rtems/score/cpuopts.h>
31
32#ifndef ASM
33  #include <stddef.h>
34  #include <stdbool.h>
35  #include <stdint.h>
36#endif
37
38#ifndef TRUE
39  /**
40   *  This ensures that RTEMS has TRUE defined in all situations.
41   */
42  #define TRUE 1
43#endif
44
45#ifndef FALSE
46  /**
47   *  This ensures that RTEMS has FALSE defined in all situations.
48   */
49  #define FALSE 0
50#endif
51
52#if TRUE == FALSE
53  #error "TRUE equals FALSE"
54#endif
55
56/**
57 *  The following (in conjunction with compiler arguments) are used
58 *  to choose between the use of static inline functions and macro
59 *  functions.   The static inline implementation allows better
60 *  type checking with no cost in code size or execution speed.
61 */
62#ifdef __GNUC__
63  #define RTEMS_INLINE_ROUTINE static __inline__
64#else
65  #define RTEMS_INLINE_ROUTINE static inline
66#endif
67
68/**
69 *  The following macro is a compiler specific way to ensure that memory
70 *  writes are not reordered around certian points.  This specifically can
71 *  impact interrupt disable and thread dispatching critical sections.
72 */
73#ifdef __GNUC__
74  #define RTEMS_COMPILER_MEMORY_BARRIER() __asm__ volatile("" ::: "memory")
75#else
76  #define RTEMS_COMPILER_MEMORY_BARRIER()
77#endif
78
79/**
80 *  The following defines a compiler specific attribute which informs
81 *  the compiler that the method must not be inlined.
82 */
83#ifdef __GNUC__
84  #define RTEMS_NO_INLINE __attribute__((__noinline__))
85#else
86  #define RTEMS_NO_INLINE
87#endif
88
89/**
90 *  The following macro is a compiler specific way to indicate that
91 *  the method will NOT return to the caller.  This can assist the
92 *  compiler in code generation and avoid unreachable paths.  This
93 *  can impact the code generated following calls to
94 *  rtems_fatal_error_occurred and _Terminate.
95 */
96#if defined(RTEMS_SCHEDSIM)
97  #define RTEMS_NO_RETURN
98#elif defined(__GNUC__) && !defined(RTEMS_DEBUG)
99  #define RTEMS_NO_RETURN __attribute__((__noreturn__))
100#else
101  #define RTEMS_NO_RETURN
102#endif
103
104/* Provided for backward compatibility */
105#define RTEMS_COMPILER_NO_RETURN_ATTRIBUTE RTEMS_NO_RETURN
106
107/**
108 *  The following defines a compiler specific attribute which informs
109 *  the compiler that the method has no effect except the return value
110 *  and that the return value depends only on parameters and/or global
111 *  variables.
112 */
113#ifdef __GNUC__
114  #define RTEMS_PURE __attribute__((__pure__))
115#else
116  #define RTEMS_PURE
117#endif
118
119/* Provided for backward compatibility */
120#define RTEMS_COMPILER_PURE_ATTRIBUTE RTEMS_PURE
121
122/**
123 *  Instructs the compiler to issue a warning whenever a variable or function
124 *  with this attribute will be used.
125 */
126#ifdef __GNUC__
127  #define RTEMS_DEPRECATED __attribute__((__deprecated__))
128#else
129  #define RTEMS_DEPRECATED
130#endif
131
132/* Provided for backward compatibility */
133#define RTEMS_COMPILER_DEPRECATED_ATTRIBUTE RTEMS_DEPRECATED
134
135/**
136 * @brief Instructs the compiler to place a specific variable or function in
137 * the specified section.
138 */
139#if defined(__GNUC__)
140  #define RTEMS_SECTION( _section ) __attribute__((__section__(_section)))
141#else
142  #define RTEMS_SECTION( _section )
143#endif
144
145/**
146 * @brief Instructs the compiler that a specific variable or function is used.
147 */
148#if defined(__GNUC__)
149  #define RTEMS_USED __attribute__((__used__))
150#else
151  #define RTEMS_USED
152#endif
153
154/**
155 *  Instructs the compiler that a specific variable is deliberately unused.
156 *  This can occur when reading volatile device memory or skipping arguments
157 *  in a variable argument method.
158 */
159#if defined(__GNUC__)
160  #define RTEMS_UNUSED __attribute__((__unused__))
161#else
162  #define RTEMS_UNUSED
163#endif
164
165/* Provided for backward compatibility */
166#define RTEMS_COMPILER_UNUSED_ATTRIBUTE RTEMS_UNUSED
167
168/**
169 *  Instructs the compiler that a specific structure or union members will be
170 *  placed so that the least memory is used.
171 */
172#if defined(__GNUC__)
173  #define RTEMS_PACKED __attribute__((__packed__))
174#else
175  #define RTEMS_PACKED
176#endif
177
178/**
179 * @brief Instructs the compiler to generate an alias to the specified target
180 * function.
181 */
182#if defined(__GNUC__)
183  #define RTEMS_ALIAS( _target ) __attribute__((__alias__(#_target)))
184#else
185  #define RTEMS_ALIAS( _target )
186#endif
187
188/**
189 * @brief Instructs the compiler to generate a weak alias to the specified
190 * target function.
191 */
192#if defined(__GNUC__)
193  #define RTEMS_WEAK_ALIAS( _target ) __attribute__((__weak__, __alias__(#_target)))
194#else
195  #define RTEMS_WEAK_ALIAS( _target )
196#endif
197
198/**
199 * @brief Instructs the compiler to enforce the specified alignment.
200 */
201#if defined(__GNUC__)
202  #define RTEMS_ALIGNED( _alignment ) __attribute__((__aligned__(_alignment)))
203#else
204  #define RTEMS_ALIGNED( _alignment )
205#endif
206
207/* Provided for backward compatibility */
208#define RTEMS_COMPILER_PACKED_ATTRIBUTE RTEMS_PACKED
209
210#if defined(RTEMS_DEBUG) && !defined(RTEMS_SCHEDSIM)
211  #define _Assert_Unreachable() _Assert( 0 )
212#else
213  #define _Assert_Unreachable() do { } while ( 0 )
214#endif
215
216/**
217 * @brief Tells the compiler that this program point is unreachable.
218 */
219#if defined(__GNUC__) && !defined(RTEMS_SCHEDSIM)
220  #define RTEMS_UNREACHABLE() \
221    do { \
222      __builtin_unreachable(); \
223      _Assert_Unreachable(); \
224    } while ( 0 )
225#else
226  #define RTEMS_UNREACHABLE() _Assert_Unreachable()
227#endif
228
229/**
230 * @brief Tells the compiler that this function expects printf()-like
231 * arguments.
232 */
233#if defined(__GNUC__)
234  #define RTEMS_PRINTFLIKE( _format_pos, _ap_pos ) \
235    __attribute__((__format__(__printf__, _format_pos, _ap_pos)))
236#else
237  #define RTEMS_PRINTFLIKE( _format_pos, _ap_pos )
238#endif
239
240/**
241 * @brief Obfuscates the variable so that the compiler cannot perform
242 * optimizations based on the variable value.
243 *
244 * The variable must be simple enough to fit into a register.
245 */
246#if defined(__GNUC__)
247  #define RTEMS_OBFUSCATE_VARIABLE( _var ) __asm__("" : "+r" (_var))
248#else
249  #define RTEMS_OBFUSCATE_VARIABLE( _var ) (void) (_var)
250#endif
251
252/**
253 * @brief Declares a global symbol with the specified name.
254 *
255 * This macro must be placed at file scope.
256 *
257 * The name must be a valid designator.
258 */
259#define RTEMS_DECLARE_GLOBAL_SYMBOL( _name ) \
260  extern char _name[]
261
262/**
263 * @brief Defines a global symbol with the specified name and value.
264 *
265 * This macro must be placed at file scope.
266 *
267 * The name must be a valid designator.
268 *
269 * On the value parameters macro expansion is performed and afterwards it is
270 * stringified.  It must expand to an integer literal understood by the
271 * assembler.
272 */
273#if defined(__GNUC__)
274  #define RTEMS_DEFINE_GLOBAL_SYMBOL( _name, _value ) \
275    __asm__( \
276      "\t.globl " RTEMS_XSTRING( __USER_LABEL_PREFIX__ ) #_name \
277      "\n\t.set " RTEMS_XSTRING( __USER_LABEL_PREFIX__ ) #_name \
278      ", " RTEMS_STRING( _value ) "\n" \
279    )
280#else
281  #define RTEMS_DEFINE_GLOBAL_SYMBOL( _name, _value )
282#endif
283
284/**
285 * @brief Returns the value of the specified integral expression and tells the
286 * compiler that the predicted value is true (1).
287 *
288 * @param[in] _exp The expression.
289 *
290 * @return The value of the expression.
291 */
292#if defined(__GNUC__)
293  #define RTEMS_PREDICT_TRUE( _exp ) __builtin_expect( ( _exp ), 1 )
294#else
295  #define RTEMS_PREDICT_TRUE( _exp ) ( _exp )
296#endif
297
298/**
299 * @brief Returns the value of the specified integral expression and tells the
300 * compiler that the predicted value is false (0).
301 *
302 * @param[in] _exp The expression.
303 *
304 * @return The value of the expression.
305 */
306#if defined(__GNUC__)
307  #define RTEMS_PREDICT_FALSE( _exp ) __builtin_expect( ( _exp ), 0 )
308#else
309  #define RTEMS_PREDICT_FALSE( _exp ) ( _exp )
310#endif
311
312#if __cplusplus >= 201103L
313  #define RTEMS_STATIC_ASSERT(cond, msg) \
314    static_assert(cond, # msg)
315#elif __STDC_VERSION__ >= 201112L
316  #define RTEMS_STATIC_ASSERT(cond, msg) \
317    _Static_assert(cond, # msg)
318#else
319  #define RTEMS_STATIC_ASSERT(cond, msg) \
320    struct rtems_static_assert_ ## msg \
321      { int rtems_static_assert_ ## msg : (cond) ? 1 : -1; }
322#endif
323
324#define RTEMS_ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
325
326/*
327 * Zero-length arrays are valid in C99 as flexible array members.  C++11
328 * doesn't allow flexible array members.  Use the GNU extension which is also
329 * supported by other compilers.
330 */
331#define RTEMS_ZERO_LENGTH_ARRAY 0
332
333/**
334 * @brief Returns a pointer to the container of a specified member pointer.
335 *
336 * @param[in] _m The pointer to a member of the container.
337 * @param[in] _type The type of the container.
338 * @param[in] _member_name The designator name of the container member.
339 */
340#define RTEMS_CONTAINER_OF( _m, _type, _member_name ) \
341  ( (_type *) ( (uintptr_t) ( _m ) - offsetof( _type, _member_name ) ) )
342
343#ifdef __cplusplus
344#define RTEMS_DEQUALIFY_DEPTHX( _ptr_level, _type, _var ) \
345            (const_cast<_type>( _var ))
346#else /* Standard C code */
347
348/* The reference type idea based on libHX by Jan Engelhardt */
349#define RTEMS_TYPEOF_REFX(_ptr_level, _ptr_type) \
350  typeof(_ptr_level(union { int z; typeof(_ptr_type) x; }){0}.x)
351
352#if defined(__GNUC__) && !defined(ASM)
353#if  ((__GNUC__ * 1000 + __GNUC_MINOR__) >= 4004)
354extern void* RTEMS_DEQUALIFY_types_not_compatible(void)
355  __attribute__((error ("RTEMS_DEQUALIFY types differ not only by volatile and const")));
356#else
357extern void RTEMS_DEQUALIFY_types_not_compatible(void);
358#endif
359#define RTEMS_DEQUALIFY_DEPTHX( _ptr_level, _type, _var ) ( \
360    __builtin_choose_expr( __builtin_types_compatible_p ( \
361        RTEMS_TYPEOF_REFX( _ptr_level, _var ), \
362        RTEMS_TYPEOF_REFX( _ptr_level, _type ) \
363      ) || __builtin_types_compatible_p ( _type, void * ), \
364    (_type)(_var), \
365    RTEMS_DEQUALIFY_types_not_compatible() \
366  ) \
367)
368#endif /*__GNUC__*/
369#endif /*__cplusplus*/
370
371#ifndef RTEMS_DECONST
372#ifdef RTEMS_DEQUALIFY_DEPTHX
373#define RTEMS_DECONST( _type, _var ) \
374  RTEMS_DEQUALIFY_DEPTHX( *, _type, _var )
375#else /*RTEMS_DEQUALIFY_DEPTHX*/
376/**
377 * @brief Removes the const qualifier from a type of a variable.
378 *
379 * @param[in] _type The target type for the variable.
380 * @param[in] _var The variable.
381 */
382#define RTEMS_DECONST( _type, _var ) \
383  ((_type)(uintptr_t)(const void *) ( _var ))
384
385#endif /*RTEMS_DEQUALIFY_DEPTHX*/
386#endif /*RTEMS_DECONST*/
387
388#ifndef RTEMS_DEVOLATILE
389#ifdef RTEMS_DEQUALIFY_DEPTHX
390#define RTEMS_DEVOLATILE( _type, _var ) \
391  RTEMS_DEQUALIFY_DEPTHX( *, _type, _var )
392#else /*RTEMS_DEQUALIFY_DEPTHX*/
393/**
394 * @brief Removes the volatile qualifier from a type of a variable.
395 *
396 * @param[in] _type The target type for the variable.
397 * @param[in] _var The variable.
398 */
399#define RTEMS_DEVOLATILE( _type, _var ) \
400  ((_type)(uintptr_t)(volatile void *) ( _var ))
401
402#endif /*RTEMS_DEQUALIFY_DEPTHX*/
403#endif /*RTEMS_DEVOLATILE*/
404
405#ifndef RTEMS_DEQUALIFY
406#ifdef RTEMS_DEQUALIFY_DEPTHX
407#define RTEMS_DEQUALIFY( _type, _var ) \
408  RTEMS_DEQUALIFY_DEPTHX( *, _type, _var )
409#else /*RTEMS_DEQUALIFY_DEPTHX*/
410/**
411 * @brief Removes the all qualifiers from a type of a variable.
412 *
413 * @param[in] _type The target type for the variable.
414 * @param[in] _var The variable.
415 */
416#define RTEMS_DEQUALIFY( _type, _var ) \
417  ((_type)(uintptr_t)(const volatile void *) ( _var ))
418
419#endif /*RTEMS_DEQUALIFY_DEPTHX*/
420#endif /*RTEMS_DEQUALIFY*/
421
422/**
423 * @brief Evaluates to true if the members of two types have the same type.
424 *
425 * @param[in] _t_lhs Left hand side type.
426 * @param[in] _m_lhs Left hand side member.
427 * @param[in] _t_rhs Right hand side type.
428 * @param[in] _m_rhs Right hand side member.
429 */
430#ifdef __GNUC__
431  #define RTEMS_HAVE_MEMBER_SAME_TYPE( _t_lhs, _m_lhs, _t_rhs, _m_rhs ) \
432    __builtin_types_compatible_p( \
433      __typeof( ( (_t_lhs *) 0 )->_m_lhs ), \
434      __typeof( ( (_t_rhs *) 0 )->_m_rhs ) \
435    )
436#else
437  #define RTEMS_HAVE_MEMBER_SAME_TYPE( _t_lhs, _m_lhs, _t_rhs, _m_rhs ) \
438    true
439#endif
440
441/**
442 * @brief Concatenates _x and _y without expanding.
443 */
444#define RTEMS_CONCAT( _x, _y ) _x##_y
445
446/**
447 * @brief Concatenates expansion of _x and expansion of _y.
448 */
449#define RTEMS_XCONCAT( _x, _y ) RTEMS_CONCAT( _x, _y )
450
451/**
452 * @brief Stringifies _x  without expanding.
453 */
454#define RTEMS_STRING( _x ) #_x
455
456/**
457 * @brief Stringifies expansion of _x.
458 */
459#define RTEMS_XSTRING( _x ) RTEMS_STRING( _x )
460
461#ifndef ASM
462  #ifdef RTEMS_DEPRECATED_TYPES
463    typedef bool boolean;
464    typedef float single_precision;
465    typedef double double_precision;
466  #endif
467
468  /**
469   * XXX: Eventually proc_ptr needs to disappear!!!
470   */
471  typedef void * proc_ptr;
472#endif
473
474/**@}*/
475
476#endif /* _RTEMS_BASEDEFS_H */
Note: See TracBrowser for help on using the repository browser.