nimbase.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /*
  2. Nim's Runtime Library
  3. (c) Copyright 2015 Andreas Rumpf
  4. See the file "copying.txt", included in this
  5. distribution, for details about the copyright.
  6. */
  7. /* compiler symbols:
  8. __BORLANDC__
  9. _MSC_VER
  10. __WATCOMC__
  11. __LCC__
  12. __GNUC__
  13. __DMC__
  14. __POCC__
  15. __TINYC__
  16. __clang__
  17. __AVR__
  18. */
  19. #ifndef NIMBASE_H
  20. #define NIMBASE_H
  21. /*------------ declaring a custom attribute to support using LLVM's Address Sanitizer ------------ */
  22. /*
  23. This definition exists to provide support for using the LLVM ASAN (Address SANitizer) tooling with Nim. This
  24. should only be used to mark implementations of the GC system that raise false flags with the ASAN tooling, or
  25. for functions that are hot and need to be disabled for performance reasons. Based on the official ASAN
  26. documentation, both the clang and gcc compilers are supported. In addition to that, a check is performed to
  27. verify that the necessary attribute is supported by the compiler.
  28. To flag a proc as ignored, append the following code pragma to the proc declaration:
  29. {.codegenDecl: "CLANG_NO_SANITIZE_ADDRESS $# $#$#".}
  30. For further information, please refer to the official documentation:
  31. https://github.com/google/sanitizers/wiki/AddressSanitizer
  32. */
  33. #define CLANG_NO_SANITIZE_ADDRESS
  34. #if defined(__clang__)
  35. # if __has_attribute(no_sanitize_address)
  36. # undef CLANG_NO_SANITIZE_ADDRESS
  37. # define CLANG_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
  38. # endif
  39. #endif
  40. /* ------------ ignore typical warnings in Nim-generated files ------------- */
  41. #if defined(__GNUC__) || defined(__clang__)
  42. # pragma GCC diagnostic ignored "-Wpragmas"
  43. # pragma GCC diagnostic ignored "-Wwritable-strings"
  44. # pragma GCC diagnostic ignored "-Winvalid-noreturn"
  45. # pragma GCC diagnostic ignored "-Wformat"
  46. # pragma GCC diagnostic ignored "-Wlogical-not-parentheses"
  47. # pragma GCC diagnostic ignored "-Wlogical-op-parentheses"
  48. # pragma GCC diagnostic ignored "-Wshadow"
  49. # pragma GCC diagnostic ignored "-Wunused-function"
  50. # pragma GCC diagnostic ignored "-Wunused-variable"
  51. # pragma GCC diagnostic ignored "-Winvalid-offsetof"
  52. # pragma GCC diagnostic ignored "-Wtautological-compare"
  53. # pragma GCC diagnostic ignored "-Wswitch-bool"
  54. # pragma GCC diagnostic ignored "-Wmacro-redefined"
  55. # pragma GCC diagnostic ignored "-Wincompatible-pointer-types-discards-qualifiers"
  56. # pragma GCC diagnostic ignored "-Wpointer-bool-conversion"
  57. # pragma GCC diagnostic ignored "-Wconstant-conversion"
  58. #endif
  59. #if defined(_MSC_VER)
  60. # pragma warning(disable: 4005 4100 4101 4189 4191 4200 4244 4293 4296 4309)
  61. # pragma warning(disable: 4310 4365 4456 4477 4514 4574 4611 4668 4702 4706)
  62. # pragma warning(disable: 4710 4711 4774 4800 4809 4820 4996 4090 4297)
  63. #endif
  64. /* ------------------------------------------------------------------------- */
  65. #if defined(__GNUC__) && !defined(__ZEPHYR__)
  66. /* Zephyr does some magic in it's headers that override the GCC stdlib. This breaks that. */
  67. # define _GNU_SOURCE 1
  68. #endif
  69. #if defined(__TINYC__)
  70. /*# define __GNUC__ 3
  71. # define GCC_MAJOR 4
  72. # define __GNUC_MINOR__ 4
  73. # define __GNUC_PATCHLEVEL__ 5 */
  74. # define __DECLSPEC_SUPPORTED 1
  75. #endif
  76. /* calling convention mess ----------------------------------------------- */
  77. #if defined(__GNUC__) || defined(__LCC__) || defined(__POCC__) \
  78. || defined(__TINYC__)
  79. /* these should support C99's inline */
  80. /* the test for __POCC__ has to come before the test for _MSC_VER,
  81. because PellesC defines _MSC_VER too. This is brain-dead. */
  82. # define N_INLINE(rettype, name) inline rettype name
  83. #elif defined(__BORLANDC__) || defined(_MSC_VER)
  84. /* Borland's compiler is really STRANGE here; note that the __fastcall
  85. keyword cannot be before the return type, but __inline cannot be after
  86. the return type, so we do not handle this mess in the code generator
  87. but rather here. */
  88. # define N_INLINE(rettype, name) __inline rettype name
  89. #elif defined(__DMC__)
  90. # define N_INLINE(rettype, name) inline rettype name
  91. #elif defined(__WATCOMC__)
  92. # define N_INLINE(rettype, name) __inline rettype name
  93. #else /* others are less picky: */
  94. # define N_INLINE(rettype, name) rettype __inline name
  95. #endif
  96. #define N_INLINE_PTR(rettype, name) rettype (*name)
  97. #if defined(__POCC__)
  98. # define NIM_CONST /* PCC is really picky with const modifiers */
  99. # undef _MSC_VER /* Yeah, right PCC defines _MSC_VER even if it is
  100. not that compatible. Well done. */
  101. #elif defined(__cplusplus)
  102. # define NIM_CONST /* C++ is picky with const modifiers */
  103. #else
  104. # define NIM_CONST const
  105. #endif
  106. /*
  107. NIM_THREADVAR declaration based on
  108. http://stackoverflow.com/questions/18298280/how-to-declare-a-variable-as-thread-local-portably
  109. */
  110. #if defined _WIN32
  111. # if defined _MSC_VER || defined __DMC__ || defined __BORLANDC__
  112. # define NIM_THREADVAR __declspec(thread)
  113. # else
  114. # define NIM_THREADVAR __thread
  115. # endif
  116. #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112 && !defined __STDC_NO_THREADS__
  117. # define NIM_THREADVAR _Thread_local
  118. #elif defined _WIN32 && ( \
  119. defined _MSC_VER || \
  120. defined __ICL || \
  121. defined __DMC__ || \
  122. defined __BORLANDC__ )
  123. # define NIM_THREADVAR __declspec(thread)
  124. #elif defined(__TINYC__) || defined(__GENODE__)
  125. # define NIM_THREADVAR
  126. /* note that ICC (linux) and Clang are covered by __GNUC__ */
  127. #elif defined __GNUC__ || \
  128. defined __SUNPRO_C || \
  129. defined __xlC__
  130. # define NIM_THREADVAR __thread
  131. #else
  132. # error "Cannot define NIM_THREADVAR"
  133. #endif
  134. #if defined(__cplusplus)
  135. #define NIM_THREAD_LOCAL thread_local
  136. #endif
  137. /* --------------- how int64 constants should be declared: ----------- */
  138. #if defined(__GNUC__) || defined(__LCC__) || \
  139. defined(__POCC__) || defined(__DMC__) || defined(_MSC_VER)
  140. # define IL64(x) x##LL
  141. #else /* works only without LL */
  142. # define IL64(x) ((NI64)x)
  143. #endif
  144. /* ---------------- casting without correct aliasing rules ----------- */
  145. #if defined(__GNUC__)
  146. # define NIM_CAST(type, ptr) (((union{type __x__;}*)(ptr))->__x__)
  147. #else
  148. # define NIM_CAST(type, ptr) ((type)(ptr))
  149. #endif
  150. /* ------------------------------------------------------------------- */
  151. #ifdef __cplusplus
  152. # define NIM_EXTERNC extern "C"
  153. #else
  154. # define NIM_EXTERNC
  155. #endif
  156. #if defined(WIN32) || defined(_WIN32) /* only Windows has this mess... */
  157. # define N_LIB_PRIVATE
  158. # define N_CDECL(rettype, name) rettype __cdecl name
  159. # define N_STDCALL(rettype, name) rettype __stdcall name
  160. # define N_SYSCALL(rettype, name) rettype __syscall name
  161. # define N_FASTCALL(rettype, name) rettype __fastcall name
  162. # define N_THISCALL(rettype, name) rettype __thiscall name
  163. # define N_SAFECALL(rettype, name) rettype __stdcall name
  164. /* function pointers with calling convention: */
  165. # define N_CDECL_PTR(rettype, name) rettype (__cdecl *name)
  166. # define N_STDCALL_PTR(rettype, name) rettype (__stdcall *name)
  167. # define N_SYSCALL_PTR(rettype, name) rettype (__syscall *name)
  168. # define N_FASTCALL_PTR(rettype, name) rettype (__fastcall *name)
  169. # define N_THISCALL_PTR(rettype, name) rettype (__thiscall *name)
  170. # define N_SAFECALL_PTR(rettype, name) rettype (__stdcall *name)
  171. # ifdef __cplusplus
  172. # define N_LIB_EXPORT NIM_EXTERNC __declspec(dllexport)
  173. # else
  174. # define N_LIB_EXPORT NIM_EXTERNC __declspec(dllexport)
  175. # endif
  176. # define N_LIB_EXPORT_VAR __declspec(dllexport)
  177. # define N_LIB_IMPORT extern __declspec(dllimport)
  178. #else
  179. # define N_LIB_PRIVATE __attribute__((visibility("hidden")))
  180. # if defined(__GNUC__)
  181. # define N_CDECL(rettype, name) rettype name
  182. # define N_STDCALL(rettype, name) rettype name
  183. # define N_SYSCALL(rettype, name) rettype name
  184. # define N_FASTCALL(rettype, name) __attribute__((fastcall)) rettype name
  185. # define N_SAFECALL(rettype, name) rettype name
  186. /* function pointers with calling convention: */
  187. # define N_CDECL_PTR(rettype, name) rettype (*name)
  188. # define N_STDCALL_PTR(rettype, name) rettype (*name)
  189. # define N_SYSCALL_PTR(rettype, name) rettype (*name)
  190. # define N_FASTCALL_PTR(rettype, name) __attribute__((fastcall)) rettype (*name)
  191. # define N_SAFECALL_PTR(rettype, name) rettype (*name)
  192. # else
  193. # define N_CDECL(rettype, name) rettype name
  194. # define N_STDCALL(rettype, name) rettype name
  195. # define N_SYSCALL(rettype, name) rettype name
  196. # define N_FASTCALL(rettype, name) rettype name
  197. # define N_SAFECALL(rettype, name) rettype name
  198. /* function pointers with calling convention: */
  199. # define N_CDECL_PTR(rettype, name) rettype (*name)
  200. # define N_STDCALL_PTR(rettype, name) rettype (*name)
  201. # define N_SYSCALL_PTR(rettype, name) rettype (*name)
  202. # define N_FASTCALL_PTR(rettype, name) rettype (*name)
  203. # define N_SAFECALL_PTR(rettype, name) rettype (*name)
  204. # endif
  205. # define N_LIB_EXPORT NIM_EXTERNC __attribute__((visibility("default")))
  206. # define N_LIB_EXPORT_VAR __attribute__((visibility("default")))
  207. # define N_LIB_IMPORT extern
  208. #endif
  209. #define N_NOCONV(rettype, name) rettype name
  210. /* specify no calling convention */
  211. #define N_NOCONV_PTR(rettype, name) rettype (*name)
  212. #if defined(__GNUC__) || defined(__ICC__)
  213. # define N_NOINLINE(rettype, name) rettype __attribute__((__noinline__)) name
  214. #elif defined(_MSC_VER)
  215. # define N_NOINLINE(rettype, name) __declspec(noinline) rettype name
  216. #else
  217. # define N_NOINLINE(rettype, name) rettype name
  218. #endif
  219. #define N_NOINLINE_PTR(rettype, name) rettype (*name)
  220. #if defined(__BORLANDC__) || defined(__WATCOMC__) || \
  221. defined(__POCC__) || defined(_MSC_VER) || defined(WIN32) || defined(_WIN32)
  222. /* these compilers have a fastcall so use it: */
  223. # ifdef __TINYC__
  224. # define N_NIMCALL(rettype, name) rettype __attribute((__fastcall)) name
  225. # define N_NIMCALL_PTR(rettype, name) rettype (__attribute((__fastcall)) *name)
  226. # define N_RAW_NIMCALL __attribute((__fastcall))
  227. # else
  228. # define N_NIMCALL(rettype, name) rettype __fastcall name
  229. # define N_NIMCALL_PTR(rettype, name) rettype (__fastcall *name)
  230. # define N_RAW_NIMCALL __fastcall
  231. # endif
  232. #else
  233. # define N_NIMCALL(rettype, name) rettype name /* no modifier */
  234. # define N_NIMCALL_PTR(rettype, name) rettype (*name)
  235. # define N_RAW_NIMCALL
  236. #endif
  237. #define N_CLOSURE(rettype, name) N_NIMCALL(rettype, name)
  238. #define N_CLOSURE_PTR(rettype, name) N_NIMCALL_PTR(rettype, name)
  239. /* ----------------------------------------------------------------------- */
  240. #define COMMA ,
  241. #include <limits.h>
  242. #include <stddef.h>
  243. // define NIM_STATIC_ASSERT
  244. // example use case: CT sizeof for importc types verification
  245. // where we have {.completeStruct.} (or lack of {.incompleteStruct.})
  246. #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L)
  247. #define NIM_STATIC_ASSERT(x, msg) _Static_assert((x), msg)
  248. #elif defined(__cplusplus)
  249. #define NIM_STATIC_ASSERT(x, msg) static_assert((x), msg)
  250. #else
  251. #define NIM_STATIC_ASSERT(x, msg) typedef int NIM_STATIC_ASSERT_AUX[(x) ? 1 : -1];
  252. // On failure, your C compiler will say something like:
  253. // "error: 'NIM_STATIC_ASSERT_AUX' declared as an array with a negative size"
  254. // we could use a better fallback to also show line number, using:
  255. // http://www.pixelbeat.org/programming/gcc/static_assert.html
  256. #endif
  257. /* C99 compiler? */
  258. #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901))
  259. # define HAVE_STDINT_H
  260. #endif
  261. /* Known compiler with stdint.h that doesn't fit the general pattern? */
  262. #if defined(__LCC__) || defined(__DMC__) || defined(__POCC__) || \
  263. defined(__AVR__) || (defined(__cplusplus) && (__cplusplus < 201103))
  264. # define HAVE_STDINT_H
  265. #endif
  266. #if (!defined(HAVE_STDINT_H) && defined(__cplusplus) && (__cplusplus >= 201103))
  267. # define HAVE_CSTDINT
  268. #endif
  269. /* wrap all Nim typedefs into namespace Nim */
  270. #ifdef USE_NIM_NAMESPACE
  271. #ifdef HAVE_CSTDINT
  272. #include <cstdint>
  273. #else
  274. #include <stdint.h>
  275. #endif
  276. namespace USE_NIM_NAMESPACE {
  277. #endif
  278. // preexisting check, seems paranoid, maybe remove
  279. #if defined(NIM_TRUE) || defined(NIM_FALSE) || defined(NIM_BOOL)
  280. #error "nim reserved preprocessor macros clash"
  281. #endif
  282. /* bool types (C++ has it): */
  283. #ifdef __cplusplus
  284. #define NIM_BOOL bool
  285. #elif (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901)
  286. // see #13798: to avoid conflicts for code emitting `#include <stdbool.h>`
  287. #define NIM_BOOL _Bool
  288. #else
  289. typedef unsigned char NIM_BOOL; // best effort
  290. #endif
  291. NIM_STATIC_ASSERT(sizeof(NIM_BOOL) == 1, ""); // check whether really needed
  292. NIM_STATIC_ASSERT(CHAR_BIT == 8, "");
  293. // fail fast for (rare) environments where this doesn't hold, as some implicit
  294. // assumptions would need revisiting (e.g. `uint8` or https://github.com/nim-lang/Nim/pull/18505)
  295. #define NIM_TRUE true
  296. #define NIM_FALSE false
  297. #ifdef __cplusplus
  298. # if __cplusplus >= 201103L
  299. # /* nullptr is more type safe (less implicit conversions than 0) */
  300. # define NIM_NIL nullptr
  301. # else
  302. # // both `((void*)0)` and `NULL` would cause codegen to emit
  303. # // error: assigning to 'Foo *' from incompatible type 'void *'
  304. # // but codegen could be fixed if need. See also potential caveat regarding
  305. # // NULL.
  306. # // However, `0` causes other issues, see #13798
  307. # define NIM_NIL 0
  308. # endif
  309. #else
  310. # include <stdbool.h>
  311. # define NIM_NIL ((void*)0) /* C's NULL is fucked up in some C compilers, so
  312. the generated code does not rely on it anymore */
  313. #endif
  314. #if defined(__BORLANDC__) || defined(__DMC__) \
  315. || defined(__WATCOMC__) || defined(_MSC_VER)
  316. typedef signed char NI8;
  317. typedef signed short int NI16;
  318. typedef signed int NI32;
  319. typedef __int64 NI64;
  320. /* XXX: Float128? */
  321. typedef unsigned char NU8;
  322. typedef unsigned short int NU16;
  323. typedef unsigned int NU32;
  324. typedef unsigned __int64 NU64;
  325. #elif defined(HAVE_STDINT_H)
  326. #ifndef USE_NIM_NAMESPACE
  327. # include <stdint.h>
  328. #endif
  329. typedef int8_t NI8;
  330. typedef int16_t NI16;
  331. typedef int32_t NI32;
  332. typedef int64_t NI64;
  333. typedef uint8_t NU8;
  334. typedef uint16_t NU16;
  335. typedef uint32_t NU32;
  336. typedef uint64_t NU64;
  337. #elif defined(HAVE_CSTDINT)
  338. #ifndef USE_NIM_NAMESPACE
  339. # include <cstdint>
  340. #endif
  341. typedef std::int8_t NI8;
  342. typedef std::int16_t NI16;
  343. typedef std::int32_t NI32;
  344. typedef std::int64_t NI64;
  345. typedef std::uint8_t NU8;
  346. typedef std::uint16_t NU16;
  347. typedef std::uint32_t NU32;
  348. typedef std::uint64_t NU64;
  349. #else
  350. /* Unknown compiler/version, do our best */
  351. #ifdef __INT8_TYPE__
  352. typedef __INT8_TYPE__ NI8;
  353. #else
  354. typedef signed char NI8;
  355. #endif
  356. #ifdef __INT16_TYPE__
  357. typedef __INT16_TYPE__ NI16;
  358. #else
  359. typedef signed short int NI16;
  360. #endif
  361. #ifdef __INT32_TYPE__
  362. typedef __INT32_TYPE__ NI32;
  363. #else
  364. typedef signed int NI32;
  365. #endif
  366. #ifdef __INT64_TYPE__
  367. typedef __INT64_TYPE__ NI64;
  368. #else
  369. typedef long long int NI64;
  370. #endif
  371. /* XXX: Float128? */
  372. #ifdef __UINT8_TYPE__
  373. typedef __UINT8_TYPE__ NU8;
  374. #else
  375. typedef unsigned char NU8;
  376. #endif
  377. #ifdef __UINT16_TYPE__
  378. typedef __UINT16_TYPE__ NU16;
  379. #else
  380. typedef unsigned short int NU16;
  381. #endif
  382. #ifdef __UINT32_TYPE__
  383. typedef __UINT32_TYPE__ NU32;
  384. #else
  385. typedef unsigned int NU32;
  386. #endif
  387. #ifdef __UINT64_TYPE__
  388. typedef __UINT64_TYPE__ NU64;
  389. #else
  390. typedef unsigned long long int NU64;
  391. #endif
  392. #endif
  393. #ifdef NIM_INTBITS
  394. # if NIM_INTBITS == 64
  395. typedef NI64 NI;
  396. typedef NU64 NU;
  397. # elif NIM_INTBITS == 32
  398. typedef NI32 NI;
  399. typedef NU32 NU;
  400. # elif NIM_INTBITS == 16
  401. typedef NI16 NI;
  402. typedef NU16 NU;
  403. # elif NIM_INTBITS == 8
  404. typedef NI8 NI;
  405. typedef NU8 NU;
  406. # else
  407. # error "invalid bit width for int"
  408. # endif
  409. #endif
  410. // for now there isn't an easy way for C code to reach the program result
  411. // when hot code reloading is ON - users will have to:
  412. // load the nimhcr.dll, get the hcrGetGlobal proc from there and use it
  413. #ifndef NIM_HOT_CODE_RELOADING
  414. extern NI nim_program_result;
  415. #endif
  416. typedef float NF32;
  417. typedef double NF64;
  418. typedef double NF;
  419. typedef char NIM_CHAR;
  420. typedef char* NCSTRING;
  421. #ifdef NIM_BIG_ENDIAN
  422. # define NIM_IMAN 1
  423. #else
  424. # define NIM_IMAN 0
  425. #endif
  426. #define NIM_STRLIT_FLAG ((NU)(1) << ((NIM_INTBITS) - 2)) /* This has to be the same as system.strlitFlag! */
  427. #define STRING_LITERAL(name, str, length) \
  428. static const struct { \
  429. TGenericSeq Sup; \
  430. NIM_CHAR data[(length) + 1]; \
  431. } name = {{length, (NI) ((NU)length | NIM_STRLIT_FLAG)}, str}
  432. /* declared size of a sequence/variable length array: */
  433. #if defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)
  434. # define SEQ_DECL_SIZE /* empty is correct! */
  435. #else
  436. # define SEQ_DECL_SIZE 1000000
  437. #endif
  438. #define ALLOC_0(size) calloc(1, size)
  439. #define DL_ALLOC_0(size) dlcalloc(1, size)
  440. #define paramCount() cmdCount
  441. // NAN definition copied from math.h included in the Windows SDK version 10.0.14393.0
  442. #ifndef NAN
  443. # ifndef _HUGE_ENUF
  444. # define _HUGE_ENUF 1e+300 // _HUGE_ENUF*_HUGE_ENUF must overflow
  445. # endif
  446. # define NAN_INFINITY ((float)(_HUGE_ENUF * _HUGE_ENUF))
  447. # define NAN ((float)(NAN_INFINITY * 0.0F))
  448. #endif
  449. #ifndef INF
  450. # ifdef INFINITY
  451. # define INF INFINITY
  452. # elif defined(HUGE_VAL)
  453. # define INF HUGE_VAL
  454. # elif defined(_MSC_VER)
  455. # include <float.h>
  456. # define INF (DBL_MAX+DBL_MAX)
  457. # else
  458. # define INF (1.0 / 0.0)
  459. # endif
  460. #endif
  461. typedef struct TFrame_ TFrame;
  462. struct TFrame_ {
  463. TFrame* prev;
  464. NCSTRING procname;
  465. NI line;
  466. NCSTRING filename;
  467. NI16 len;
  468. NI16 calldepth;
  469. NI frameMsgLen;
  470. };
  471. #define NIM_POSIX_INIT __attribute__((constructor))
  472. #ifdef __GNUC__
  473. # define NIM_LIKELY(x) __builtin_expect(x, 1)
  474. # define NIM_UNLIKELY(x) __builtin_expect(x, 0)
  475. /* We need the following for the posix wrapper. In particular it will give us
  476. POSIX_SPAWN_USEVFORK: */
  477. # ifndef _GNU_SOURCE
  478. # define _GNU_SOURCE
  479. # endif
  480. #else
  481. # define NIM_LIKELY(x) (x)
  482. # define NIM_UNLIKELY(x) (x)
  483. #endif
  484. #if 0 // defined(__GNUC__) || defined(__clang__)
  485. // not needed anymore because the stack marking cares about
  486. // interior pointers now
  487. static inline void GCGuard (void *ptr) { asm volatile ("" :: "X" (ptr)); }
  488. # define GC_GUARD __attribute__ ((cleanup(GCGuard)))
  489. #else
  490. # define GC_GUARD
  491. #endif
  492. // Test to see if Nim and the C compiler agree on the size of a pointer.
  493. NIM_STATIC_ASSERT(sizeof(NI) == sizeof(void*) && NIM_INTBITS == sizeof(NI)*8, "");
  494. #ifdef USE_NIM_NAMESPACE
  495. }
  496. #endif
  497. #if defined(_MSC_VER)
  498. # define NIM_ALIGN(x) __declspec(align(x))
  499. # define NIM_ALIGNOF(x) __alignof(x)
  500. #else
  501. # define NIM_ALIGN(x) __attribute__((aligned(x)))
  502. # define NIM_ALIGNOF(x) __alignof__(x)
  503. #endif
  504. /* ---------------- platform specific includes ----------------------- */
  505. /* VxWorks related includes */
  506. #if defined(__VXWORKS__)
  507. # include <sys/types.h>
  508. # include <types/vxWind.h>
  509. # include <tool/gnu/toolMacros.h>
  510. #elif defined(__FreeBSD__)
  511. # include <sys/types.h>
  512. #endif
  513. /* these exist to make the codegen logic simpler */
  514. #define nimModInt(a, b, res) (((*res) = (a) % (b)), 0)
  515. #define nimModInt64(a, b, res) (((*res) = (a) % (b)), 0)
  516. #if (!defined(_MSC_VER) || defined(__clang__)) && !defined(NIM_EmulateOverflowChecks)
  517. /* these exist because we cannot have .compilerProcs that are importc'ed
  518. by a different name */
  519. #define nimAddInt64(a, b, res) __builtin_saddll_overflow(a, b, (long long int*)res)
  520. #define nimSubInt64(a, b, res) __builtin_ssubll_overflow(a, b, (long long int*)res)
  521. #define nimMulInt64(a, b, res) __builtin_smulll_overflow(a, b, (long long int*)res)
  522. #if NIM_INTBITS == 32
  523. #define nimAddInt(a, b, res) __builtin_sadd_overflow(a, b, res)
  524. #define nimSubInt(a, b, res) __builtin_ssub_overflow(a, b, res)
  525. #define nimMulInt(a, b, res) __builtin_smul_overflow(a, b, res)
  526. #else
  527. /* map it to the 'long long' variant */
  528. #define nimAddInt(a, b, res) __builtin_saddll_overflow(a, b, (long long int*)res)
  529. #define nimSubInt(a, b, res) __builtin_ssubll_overflow(a, b, (long long int*)res)
  530. #define nimMulInt(a, b, res) __builtin_smulll_overflow(a, b, (long long int*)res)
  531. #endif
  532. #endif
  533. #define NIM_NOALIAS __restrict
  534. /* __restrict is said to work for all the C(++) compilers out there that we support */
  535. #endif /* NIMBASE_H */