compiler.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 2007-2017 The NASM Authors - All Rights Reserved
  4. * See the file AUTHORS included with the NASM distribution for
  5. * the specific copyright holders.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following
  9. * conditions are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials provided
  16. * with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  19. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  20. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * ----------------------------------------------------------------------- */
  33. /*
  34. * compiler.h
  35. *
  36. * Compiler-specific macros for NASM. Feel free to add support for
  37. * other compilers in here.
  38. *
  39. * This header file should be included before any other header.
  40. */
  41. #ifndef NASM_COMPILER_H
  42. #define NASM_COMPILER_H 1
  43. /*
  44. * At least DJGPP and Cygwin have broken header files if __STRICT_ANSI__
  45. * is defined.
  46. */
  47. #ifdef __GNUC__
  48. # undef __STRICT_ANSI__
  49. #endif
  50. /* On Microsoft platforms we support multibyte character sets in filenames */
  51. #define _MBCS 1
  52. #ifdef HAVE_CONFIG_H
  53. # include "config/config.h"
  54. #elif defined(_MSC_VER) && (_MSC_VER >= 1310)
  55. # include "config/msvc.h"
  56. #elif defined(__WATCOMC__)
  57. # include "config/watcom.h"
  58. #else
  59. # include "config/unknown.h"
  60. #endif /* Configuration file */
  61. /* This is required to get the standard <inttypes.h> macros when compiling
  62. with a C++ compiler. This must be defined *before* <inttypes.h> is
  63. included, directly or indirectly. */
  64. #define __STDC_CONSTANT_MACROS 1
  65. #define __STDC_LIMIT_MACROS 1
  66. #define __STDC_FORMAT_MACROS 1
  67. #ifdef HAVE_INTTYPES_H
  68. # include <inttypes.h>
  69. #else
  70. # include "nasmint.h"
  71. #endif
  72. #include <assert.h>
  73. #include <stddef.h>
  74. #include <stdarg.h>
  75. #include <stdio.h>
  76. #include <limits.h>
  77. #ifdef HAVE_SYS_TYPES_H
  78. # include <sys/types.h>
  79. #endif
  80. #ifdef HAVE_ENDIAN_H
  81. # include <endian.h>
  82. #elif defined(HAVE_SYS_ENDIAN_H)
  83. # include <sys/endian.h>
  84. #elif defined(HAVE_MACHINE_ENDIAN_H)
  85. # include <machine/endian.h>
  86. #endif
  87. /*
  88. * If we have BYTE_ORDER defined, or the compiler provides
  89. * __BIG_ENDIAN__ or __LITTLE_ENDIAN__, trust it over what autoconf
  90. * came up with, especially since autoconf obviously can't figure
  91. * things out for a universal compiler.
  92. */
  93. #if defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__)
  94. # undef WORDS_LITTLEENDIAN
  95. # undef WORDS_BIGENDIAN
  96. # define WORDS_BIGENDIAN 1
  97. #elif defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
  98. # undef WORDS_LITTLEENDIAN
  99. # undef WORDS_BIGENDIAN
  100. # define WORDS_LITTLEENDIAN 1
  101. #elif defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)
  102. # undef WORDS_LITTLEENDIAN
  103. # undef WORDS_BIGENDIAN
  104. # if BYTE_ORDER == LITTLE_ENDIAN
  105. # define WORDS_LITTLEENDIAN 1
  106. # elif BYTE_ORDER == BIG_ENDIAN
  107. # define WORDS_BIGENDIAN 1
  108. # endif
  109. #endif
  110. /*
  111. * Define this to 1 for faster performance if this is a littleendian
  112. * platform *and* it can do arbitrary unaligned memory references. It
  113. * is safe to leave it defined to 0 even if that is true.
  114. */
  115. #if defined(__386__) || defined(__i386__) || defined(__x86_64__) \
  116. || defined(_M_IX86) || defined(_M_X64)
  117. # define X86_MEMORY 1
  118. # undef WORDS_BIGENDIAN
  119. # undef WORDS_LITTLEENDIAN
  120. # define WORDS_LITTLEENDIAN 1
  121. #else
  122. # define X86_MEMORY 0
  123. #endif
  124. /* Some versions of MSVC have these only with underscores in front */
  125. #ifndef HAVE_SNPRINTF
  126. # ifdef HAVE__SNPRINTF
  127. # define snprintf _snprintf
  128. # else
  129. int snprintf(char *, size_t, const char *, ...);
  130. # endif
  131. #endif
  132. #ifndef HAVE_VSNPRINTF
  133. # ifdef HAVE__VSNPRINTF
  134. # define vsnprintf _vsnprintf
  135. # else
  136. int vsnprintf(char *, size_t, const char *, va_list);
  137. # endif
  138. #endif
  139. #if !defined(HAVE_STRLCPY) || !HAVE_DECL_STRLCPY
  140. size_t strlcpy(char *, const char *, size_t);
  141. #endif
  142. #if !defined(HAVE_STRCHRNUL) || !HAVE_DECL_STRCHRNUL
  143. char *strrchrnul(const char *, int);
  144. #endif
  145. #ifndef __cplusplus /* C++ has false, true, bool as keywords */
  146. # ifdef HAVE_STDBOOL_H
  147. # include <stdbool.h>
  148. # elif defined(HAVE__BOOL)
  149. # typedef _Bool bool
  150. # define false 0
  151. # define true 1
  152. # else
  153. /* This is sort of dangerous, since casts will behave different than
  154. casting to the standard boolean type. Always use !!, not (bool). */
  155. typedef enum bool { false, true } bool;
  156. # endif
  157. #endif
  158. /* Provide a substitute for offsetof() if we don't have one. This
  159. variant works on most (but not *all*) systems... */
  160. #ifndef offsetof
  161. # define offsetof(t,m) ((size_t)&(((t *)0)->m))
  162. #endif
  163. /* The container_of construct: if p is a pointer to member m of
  164. container class c, then return a pointer to the container of which
  165. *p is a member. */
  166. #ifndef container_of
  167. # define container_of(p, c, m) ((c *)((char *)(p) - offsetof(c,m)))
  168. #endif
  169. /* Some misguided platforms hide the defs for these */
  170. #if defined(HAVE_STRCASECMP) && !HAVE_DECL_STRCASECMP
  171. int strcasecmp(const char *, const char *);
  172. #endif
  173. #if defined(HAVE_STRICMP) && !HAVE_DECL_STRICMP
  174. int stricmp(const char *, const char *);
  175. #endif
  176. #if defined(HAVE_STRNCASECMP) && !HAVE_DECL_STRNCASECMP
  177. int strncasecmp(const char *, const char *, size_t);
  178. #endif
  179. #if defined(HAVE_STRNICMP) && !HAVE_DECL_STRNICMP
  180. int strnicmp(const char *, const char *, size_t);
  181. #endif
  182. #if defined(HAVE_STRSEP) && !HAVE_DECL_STRSEP
  183. char *strsep(char **, const char *);
  184. #endif
  185. #if !HAVE_DECL_STRNLEN
  186. size_t strnlen(const char *s, size_t maxlen);
  187. #endif
  188. /*
  189. * Hack to support external-linkage inline functions
  190. */
  191. #ifndef HAVE_STDC_INLINE
  192. # ifdef __GNUC__
  193. # ifdef __GNUC_STDC_INLINE__
  194. # define HAVE_STDC_INLINE
  195. # else
  196. # define HAVE_GNU_INLINE
  197. # endif
  198. # elif defined(__GNUC_GNU_INLINE__)
  199. /* Some other compiler implementing only GNU inline semantics? */
  200. # define HAVE_GNU_INLINE
  201. # elif defined(__STDC_VERSION__)
  202. # if __STDC_VERSION__ >= 199901L
  203. # define HAVE_STDC_INLINE
  204. # endif
  205. # endif
  206. #endif
  207. #ifdef HAVE_STDC_INLINE
  208. # define extern_inline inline
  209. #elif defined(HAVE_GNU_INLINE)
  210. # define extern_inline extern inline
  211. # define inline_prototypes
  212. #else
  213. # define inline_prototypes
  214. #endif
  215. /*
  216. * Hints to the compiler that a particular branch of code is more or
  217. * less likely to be taken.
  218. */
  219. #if HAVE___BUILTIN_EXPECT
  220. # define likely(x) __builtin_expect(!!(x), 1)
  221. # define unlikely(x) __builtin_expect(!!(x), 0)
  222. #else
  223. # define likely(x) (!!(x))
  224. # define unlikely(x) (!!(x))
  225. #endif
  226. /*
  227. * Hints about malloc-like functions that never return NULL
  228. */
  229. #ifdef HAVE_FUNC_ATTRIBUTE_RETURNS_NONNULL
  230. # define never_null __attribute__((returns_nonnull))
  231. #else
  232. # define never_null
  233. #endif
  234. #ifdef HAVE_FUNC_ATTRIBUTE_MALLOC
  235. # define safe_alloc never_null __attribute__((malloc))
  236. #else
  237. # define safe_alloc never_null
  238. #endif
  239. #ifdef HAVE_FUNC_ATTRIBUTE_ALLOC_SIZE
  240. # define safe_malloc(s) safe_alloc __attribute__((alloc_size(s)))
  241. # define safe_malloc2(s1,s2) safe_alloc __attribute__((alloc_size(s1,s2)))
  242. # define safe_realloc(s) never_null __attribute__((alloc_size(s)))
  243. #else
  244. # define safe_malloc(s) safe_alloc
  245. # define safe_malloc2(s1,s2) safe_alloc
  246. # define safe_realloc(s) never_null
  247. #endif
  248. #ifdef HAVE_FUNC_ATTRIBUTE_SENTINEL
  249. # define end_with_null __attribute__((sentinel))
  250. #else
  251. # define end_with_null
  252. #endif
  253. /*
  254. * How to tell the compiler that a function doesn't return
  255. */
  256. #ifdef HAVE_STDNORETURN_H
  257. # include <stdnoreturn.h>
  258. # define no_return noreturn void
  259. #elif defined(HAVE_FUNC_ATTRIBUTE_NORETURN)
  260. # define no_return void __attribute__((noreturn))
  261. #elif defined(_MSC_VER)
  262. # define no_return __declspec(noreturn) void
  263. #else
  264. # define no_return void
  265. #endif
  266. /*
  267. * How to tell the compiler that a function is unlikely to be executed.
  268. * This differs from unlikely() in that it is applied to a function call,
  269. * not a boolean condition.
  270. */
  271. #ifdef HAVE_FUNC_ATTRIBUTE_COLD
  272. # define unlikely_func __attribute__((cold))
  273. #else
  274. # define unlikely_func
  275. #endif
  276. /*
  277. * A fatal function is both unlikely and no_return
  278. */
  279. #define fatal_func no_return unlikely_func
  280. /*
  281. * How to tell the compiler that a function takes a printf-like string
  282. */
  283. #ifdef HAVE_FUNC_ATTRIBUTE_FORMAT
  284. # define printf_func(fmt, list) __attribute__((format(printf, fmt, list)))
  285. #else
  286. # define printf_func(fmt, list)
  287. #endif
  288. /*
  289. * How to tell the compiler that a function is pure arithmetic
  290. */
  291. #ifdef HAVE_FUNC_ATTRIBUTE_CONST
  292. # define const_func __attribute__((const))
  293. #else
  294. # define const_func
  295. #endif
  296. /*
  297. * This function has no side effects, but depends on its arguments,
  298. * memory pointed to by its arguments, or global variables.
  299. * NOTE: functions that return a value by modifying memory pointed to
  300. * by a pointer argument are *NOT* considered pure.
  301. */
  302. #ifdef HAVE_FUNC_ATTRIBUTE_PURE
  303. # define pure_func __attribute__((pure))
  304. #else
  305. # define pure_func
  306. #endif
  307. /* Determine probabilistically if something is a compile-time constant */
  308. #ifdef HAVE___BUILTIN_CONSTANT_P
  309. # define is_constant(x) __builtin_constant_p(x)
  310. #else
  311. # define is_constant(x) false
  312. #endif
  313. /* Watcom doesn't handle switch statements with 64-bit types, hack around it */
  314. #ifdef __WATCOMC__
  315. # define BOGUS_CASE 0x76543210
  316. static inline unsigned int watcom_switch_hack(uint64_t x)
  317. {
  318. if (x > (uint64_t)UINT_MAX)
  319. return BOGUS_CASE;
  320. else
  321. return (unsigned int)x;
  322. }
  323. # define switch(x) switch(sizeof(x) > sizeof(unsigned int) \
  324. ? watcom_switch_hack(x) : (unsigned int)(x))
  325. /* This is to make sure BOGUS_CASE doesn't conflict with anything real... */
  326. # define default case BOGUS_CASE: default
  327. #endif
  328. #endif /* NASM_COMPILER_H */