compiler.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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(_MSC_VER)
  202. /* In MSVC and clang when it is pretending to be MSVC, inline behaves it does in
  203. * C++.
  204. */
  205. # define HAVE_MSVC_INLINE
  206. # elif defined(__STDC_VERSION__)
  207. # if __STDC_VERSION__ >= 199901L
  208. # define HAVE_STDC_INLINE
  209. # endif
  210. # endif
  211. #endif
  212. #ifdef HAVE_STDC_INLINE
  213. # define extern_inline inline
  214. #elif defined(HAVE_GNU_INLINE)
  215. # define extern_inline extern inline
  216. # define inline_prototypes
  217. #elif defined(HAVE_MSVC_INLINE)
  218. # define extern_inline inline
  219. #else
  220. # define inline_prototypes
  221. #endif
  222. /*
  223. * Hints to the compiler that a particular branch of code is more or
  224. * less likely to be taken.
  225. */
  226. #if HAVE___BUILTIN_EXPECT
  227. # define likely(x) __builtin_expect(!!(x), 1)
  228. # define unlikely(x) __builtin_expect(!!(x), 0)
  229. #else
  230. # define likely(x) (!!(x))
  231. # define unlikely(x) (!!(x))
  232. #endif
  233. /*
  234. * Hints about malloc-like functions that never return NULL
  235. */
  236. #ifdef HAVE_FUNC_ATTRIBUTE_RETURNS_NONNULL
  237. # define never_null __attribute__((returns_nonnull))
  238. #else
  239. # define never_null
  240. #endif
  241. #ifdef HAVE_FUNC_ATTRIBUTE_MALLOC
  242. # define safe_alloc never_null __attribute__((malloc))
  243. #else
  244. # define safe_alloc never_null
  245. #endif
  246. #ifdef HAVE_FUNC_ATTRIBUTE_ALLOC_SIZE
  247. # define safe_malloc(s) safe_alloc __attribute__((alloc_size(s)))
  248. # define safe_malloc2(s1,s2) safe_alloc __attribute__((alloc_size(s1,s2)))
  249. # define safe_realloc(s) never_null __attribute__((alloc_size(s)))
  250. #else
  251. # define safe_malloc(s) safe_alloc
  252. # define safe_malloc2(s1,s2) safe_alloc
  253. # define safe_realloc(s) never_null
  254. #endif
  255. #ifdef HAVE_FUNC_ATTRIBUTE_SENTINEL
  256. # define end_with_null __attribute__((sentinel))
  257. #else
  258. # define end_with_null
  259. #endif
  260. /*
  261. * How to tell the compiler that a function doesn't return
  262. */
  263. #ifdef HAVE_STDNORETURN_H
  264. # include <stdnoreturn.h>
  265. # define no_return noreturn void
  266. #elif defined(HAVE_FUNC_ATTRIBUTE_NORETURN)
  267. # define no_return void __attribute__((noreturn))
  268. #elif defined(_MSC_VER)
  269. # define no_return __declspec(noreturn) void
  270. #else
  271. # define no_return void
  272. #endif
  273. /*
  274. * How to tell the compiler that a function is unlikely to be executed.
  275. * This differs from unlikely() in that it is applied to a function call,
  276. * not a boolean condition.
  277. */
  278. #ifdef HAVE_FUNC_ATTRIBUTE_COLD
  279. # define unlikely_func __attribute__((cold))
  280. #else
  281. # define unlikely_func
  282. #endif
  283. /*
  284. * A fatal function is both unlikely and no_return
  285. */
  286. #define fatal_func no_return unlikely_func
  287. /*
  288. * How to tell the compiler that a function takes a printf-like string
  289. */
  290. #ifdef HAVE_FUNC_ATTRIBUTE_FORMAT
  291. # define printf_func(fmt, list) __attribute__((format(printf, fmt, list)))
  292. #else
  293. # define printf_func(fmt, list)
  294. #endif
  295. /*
  296. * How to tell the compiler that a function is pure arithmetic
  297. */
  298. #ifdef HAVE_FUNC_ATTRIBUTE_CONST
  299. # define const_func __attribute__((const))
  300. #else
  301. # define const_func
  302. #endif
  303. /*
  304. * This function has no side effects, but depends on its arguments,
  305. * memory pointed to by its arguments, or global variables.
  306. * NOTE: functions that return a value by modifying memory pointed to
  307. * by a pointer argument are *NOT* considered pure.
  308. */
  309. #ifdef HAVE_FUNC_ATTRIBUTE_PURE
  310. # define pure_func __attribute__((pure))
  311. #else
  312. # define pure_func
  313. #endif
  314. /* Determine probabilistically if something is a compile-time constant */
  315. #ifdef HAVE___BUILTIN_CONSTANT_P
  316. # define is_constant(x) __builtin_constant_p(x)
  317. #else
  318. # define is_constant(x) false
  319. #endif
  320. /* Watcom doesn't handle switch statements with 64-bit types, hack around it */
  321. #ifdef __WATCOMC__
  322. # define BOGUS_CASE 0x76543210
  323. static inline unsigned int watcom_switch_hack(uint64_t x)
  324. {
  325. if (x > (uint64_t)UINT_MAX)
  326. return BOGUS_CASE;
  327. else
  328. return (unsigned int)x;
  329. }
  330. # define switch(x) switch(sizeof(x) > sizeof(unsigned int) \
  331. ? watcom_switch_hack(x) : (unsigned int)(x))
  332. /* This is to make sure BOGUS_CASE doesn't conflict with anything real... */
  333. # define default case BOGUS_CASE: default
  334. #endif
  335. #endif /* NASM_COMPILER_H */