nimbase.h 20 KB

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