nimbase.h 19 KB

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