nimbase.h 20 KB

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