nimbase.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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__)
  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(__LCC__) || defined(__POCC__) \
  77. || defined(__TINYC__)
  78. /* these should support C99's inline */
  79. /* the test for __POCC__ has to come before the test for _MSC_VER,
  80. because PellesC defines _MSC_VER too. This is brain-dead. */
  81. # define N_INLINE(rettype, name) inline rettype name
  82. #elif defined(__BORLANDC__) || defined(_MSC_VER)
  83. /* Borland's compiler is really STRANGE here; note that the __fastcall
  84. keyword cannot be before the return type, but __inline cannot be after
  85. the return type, so we do not handle this mess in the code generator
  86. but rather here. */
  87. # define N_INLINE(rettype, name) __inline rettype name
  88. #elif defined(__DMC__)
  89. # define N_INLINE(rettype, name) inline rettype name
  90. #elif defined(__WATCOMC__)
  91. # define N_INLINE(rettype, name) __inline rettype name
  92. #else /* others are less picky: */
  93. # define N_INLINE(rettype, name) rettype __inline name
  94. #endif
  95. #define N_INLINE_PTR(rettype, name) rettype (*name)
  96. #if defined(__POCC__)
  97. # define NIM_CONST /* PCC is really picky with const modifiers */
  98. # undef _MSC_VER /* Yeah, right PCC defines _MSC_VER even if it is
  99. not that compatible. Well done. */
  100. #elif defined(__cplusplus)
  101. # define NIM_CONST /* C++ is picky with const modifiers */
  102. #else
  103. # define NIM_CONST const
  104. #endif
  105. /*
  106. NIM_THREADVAR declaration based on
  107. http://stackoverflow.com/questions/18298280/how-to-declare-a-variable-as-thread-local-portably
  108. */
  109. #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112 && !defined __STDC_NO_THREADS__
  110. # define NIM_THREADVAR _Thread_local
  111. #elif defined _WIN32 && ( \
  112. defined _MSC_VER || \
  113. defined __ICL || \
  114. defined __DMC__ || \
  115. defined __BORLANDC__ )
  116. # define NIM_THREADVAR __declspec(thread)
  117. #elif defined(__TINYC__) || defined(__GENODE__)
  118. # define NIM_THREADVAR
  119. /* note that ICC (linux) and Clang are covered by __GNUC__ */
  120. #elif defined __GNUC__ || \
  121. defined __SUNPRO_C || \
  122. defined __xlC__
  123. # define NIM_THREADVAR __thread
  124. #else
  125. # error "Cannot define NIM_THREADVAR"
  126. #endif
  127. #if defined(__cplusplus)
  128. #define NIM_THREAD_LOCAL thread_local
  129. #endif
  130. /* --------------- how int64 constants should be declared: ----------- */
  131. #if defined(__GNUC__) || defined(__LCC__) || \
  132. defined(__POCC__) || defined(__DMC__) || defined(_MSC_VER)
  133. # define IL64(x) x##LL
  134. #else /* works only without LL */
  135. # define IL64(x) ((NI64)x)
  136. #endif
  137. /* ---------------- casting without correct aliasing rules ----------- */
  138. #if defined(__GNUC__)
  139. # define NIM_CAST(type, ptr) (((union{type __x__;}*)(ptr))->__x__)
  140. #else
  141. # define NIM_CAST(type, ptr) ((type)(ptr))
  142. #endif
  143. /* ------------------------------------------------------------------- */
  144. #ifdef __cplusplus
  145. # define NIM_EXTERNC extern "C"
  146. #else
  147. # define NIM_EXTERNC
  148. #endif
  149. #if defined(WIN32) || defined(_WIN32) /* only Windows has this mess... */
  150. # define N_LIB_PRIVATE
  151. # define N_CDECL(rettype, name) rettype __cdecl name
  152. # define N_STDCALL(rettype, name) rettype __stdcall name
  153. # define N_SYSCALL(rettype, name) rettype __syscall name
  154. # define N_FASTCALL(rettype, name) rettype __fastcall name
  155. # define N_THISCALL(rettype, name) rettype __thiscall name
  156. # define N_SAFECALL(rettype, name) rettype __stdcall name
  157. /* function pointers with calling convention: */
  158. # define N_CDECL_PTR(rettype, name) rettype (__cdecl *name)
  159. # define N_STDCALL_PTR(rettype, name) rettype (__stdcall *name)
  160. # define N_SYSCALL_PTR(rettype, name) rettype (__syscall *name)
  161. # define N_FASTCALL_PTR(rettype, name) rettype (__fastcall *name)
  162. # define N_THISCALL_PTR(rettype, name) rettype (__thiscall *name)
  163. # define N_SAFECALL_PTR(rettype, name) rettype (__stdcall *name)
  164. # ifdef __cplusplus
  165. # define N_LIB_EXPORT NIM_EXTERNC __declspec(dllexport)
  166. # else
  167. # define N_LIB_EXPORT NIM_EXTERNC __declspec(dllexport)
  168. # endif
  169. # define N_LIB_EXPORT_VAR __declspec(dllexport)
  170. # define N_LIB_IMPORT extern __declspec(dllimport)
  171. #else
  172. # define N_LIB_PRIVATE __attribute__((visibility("hidden")))
  173. # if defined(__GNUC__)
  174. # define N_CDECL(rettype, name) rettype name
  175. # define N_STDCALL(rettype, name) rettype name
  176. # define N_SYSCALL(rettype, name) rettype name
  177. # define N_FASTCALL(rettype, name) __attribute__((fastcall)) rettype name
  178. # define N_SAFECALL(rettype, name) rettype name
  179. /* function pointers with calling convention: */
  180. # define N_CDECL_PTR(rettype, name) rettype (*name)
  181. # define N_STDCALL_PTR(rettype, name) rettype (*name)
  182. # define N_SYSCALL_PTR(rettype, name) rettype (*name)
  183. # define N_FASTCALL_PTR(rettype, name) __attribute__((fastcall)) rettype (*name)
  184. # define N_SAFECALL_PTR(rettype, name) rettype (*name)
  185. # else
  186. # define N_CDECL(rettype, name) rettype name
  187. # define N_STDCALL(rettype, name) rettype name
  188. # define N_SYSCALL(rettype, name) rettype name
  189. # define N_FASTCALL(rettype, name) rettype name
  190. # define N_SAFECALL(rettype, name) rettype name
  191. /* function pointers with calling convention: */
  192. # define N_CDECL_PTR(rettype, name) rettype (*name)
  193. # define N_STDCALL_PTR(rettype, name) rettype (*name)
  194. # define N_SYSCALL_PTR(rettype, name) rettype (*name)
  195. # define N_FASTCALL_PTR(rettype, name) rettype (*name)
  196. # define N_SAFECALL_PTR(rettype, name) rettype (*name)
  197. # endif
  198. # define N_LIB_EXPORT NIM_EXTERNC __attribute__((visibility("default")))
  199. # define N_LIB_EXPORT_VAR __attribute__((visibility("default")))
  200. # define N_LIB_IMPORT extern
  201. #endif
  202. #define N_NOCONV(rettype, name) rettype name
  203. /* specify no calling convention */
  204. #define N_NOCONV_PTR(rettype, name) rettype (*name)
  205. #if defined(__GNUC__) || defined(__ICC__)
  206. # define N_NOINLINE(rettype, name) rettype __attribute__((__noinline__)) name
  207. #elif defined(_MSC_VER)
  208. # define N_NOINLINE(rettype, name) __declspec(noinline) rettype name
  209. #else
  210. # define N_NOINLINE(rettype, name) rettype name
  211. #endif
  212. #define N_NOINLINE_PTR(rettype, name) rettype (*name)
  213. #if defined(__BORLANDC__) || defined(__WATCOMC__) || \
  214. defined(__POCC__) || defined(_MSC_VER) || defined(WIN32) || defined(_WIN32)
  215. /* these compilers have a fastcall so use it: */
  216. # ifdef __TINYC__
  217. # define N_NIMCALL(rettype, name) rettype __attribute((__fastcall)) name
  218. # define N_NIMCALL_PTR(rettype, name) rettype (__attribute((__fastcall)) *name)
  219. # define N_RAW_NIMCALL __attribute((__fastcall))
  220. # else
  221. # define N_NIMCALL(rettype, name) rettype __fastcall name
  222. # define N_NIMCALL_PTR(rettype, name) rettype (__fastcall *name)
  223. # define N_RAW_NIMCALL __fastcall
  224. # endif
  225. #else
  226. # define N_NIMCALL(rettype, name) rettype name /* no modifier */
  227. # define N_NIMCALL_PTR(rettype, name) rettype (*name)
  228. # define N_RAW_NIMCALL
  229. #endif
  230. #define N_CLOSURE(rettype, name) N_NIMCALL(rettype, name)
  231. #define N_CLOSURE_PTR(rettype, name) N_NIMCALL_PTR(rettype, name)
  232. /* ----------------------------------------------------------------------- */
  233. #define COMMA ,
  234. #include <limits.h>
  235. #include <stddef.h>
  236. // define NIM_STATIC_ASSERT
  237. // example use case: CT sizeof for importc types verification
  238. // where we have {.completeStruct.} (or lack of {.incompleteStruct.})
  239. #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L)
  240. #define NIM_STATIC_ASSERT(x, msg) _Static_assert((x), msg)
  241. #elif defined(__cplusplus)
  242. #define NIM_STATIC_ASSERT(x, msg) static_assert((x), msg)
  243. #else
  244. #define NIM_STATIC_ASSERT(x, msg) typedef int NIM_STATIC_ASSERT_AUX[(x) ? 1 : -1];
  245. // On failure, your C compiler will say something like:
  246. // "error: 'NIM_STATIC_ASSERT_AUX' declared as an array with a negative size"
  247. // we could use a better fallback to also show line number, using:
  248. // http://www.pixelbeat.org/programming/gcc/static_assert.html
  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(__LCC__) || defined(__DMC__) || defined(__POCC__) || \
  256. 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. #define NIM_TRUE true
  286. #define NIM_FALSE false
  287. #ifdef __cplusplus
  288. # if __cplusplus >= 201103L
  289. # /* nullptr is more type safe (less implicit conversions than 0) */
  290. # define NIM_NIL nullptr
  291. # else
  292. # // both `((void*)0)` and `NULL` would cause codegen to emit
  293. # // error: assigning to 'Foo *' from incompatible type 'void *'
  294. # // but codegen could be fixed if need. See also potential caveat regarding
  295. # // NULL.
  296. # // However, `0` causes other issues, see #13798
  297. # define NIM_NIL 0
  298. # endif
  299. #else
  300. # include <stdbool.h>
  301. # define NIM_NIL ((void*)0) /* C's NULL is fucked up in some C compilers, so
  302. the generated code does not rely on it anymore */
  303. #endif
  304. #if defined(__BORLANDC__) || defined(__DMC__) \
  305. || defined(__WATCOMC__) || defined(_MSC_VER)
  306. typedef signed char NI8;
  307. typedef signed short int NI16;
  308. typedef signed int NI32;
  309. typedef __int64 NI64;
  310. /* XXX: Float128? */
  311. typedef unsigned char NU8;
  312. typedef unsigned short int NU16;
  313. typedef unsigned int NU32;
  314. typedef unsigned __int64 NU64;
  315. #elif defined(HAVE_STDINT_H)
  316. #ifndef USE_NIM_NAMESPACE
  317. # include <stdint.h>
  318. #endif
  319. typedef int8_t NI8;
  320. typedef int16_t NI16;
  321. typedef int32_t NI32;
  322. typedef int64_t NI64;
  323. typedef uint8_t NU8;
  324. typedef uint16_t NU16;
  325. typedef uint32_t NU32;
  326. typedef uint64_t NU64;
  327. #elif defined(HAVE_CSTDINT)
  328. #ifndef USE_NIM_NAMESPACE
  329. # include <cstdint>
  330. #endif
  331. typedef std::int8_t NI8;
  332. typedef std::int16_t NI16;
  333. typedef std::int32_t NI32;
  334. typedef std::int64_t NI64;
  335. typedef std::uint8_t NU8;
  336. typedef std::uint16_t NU16;
  337. typedef std::uint32_t NU32;
  338. typedef std::uint64_t NU64;
  339. #else
  340. /* Unknown compiler/version, do our best */
  341. #ifdef __INT8_TYPE__
  342. typedef __INT8_TYPE__ NI8;
  343. #else
  344. typedef signed char NI8;
  345. #endif
  346. #ifdef __INT16_TYPE__
  347. typedef __INT16_TYPE__ NI16;
  348. #else
  349. typedef signed short int NI16;
  350. #endif
  351. #ifdef __INT32_TYPE__
  352. typedef __INT32_TYPE__ NI32;
  353. #else
  354. typedef signed int NI32;
  355. #endif
  356. #ifdef __INT64_TYPE__
  357. typedef __INT64_TYPE__ NI64;
  358. #else
  359. typedef long long int NI64;
  360. #endif
  361. /* XXX: Float128? */
  362. #ifdef __UINT8_TYPE__
  363. typedef __UINT8_TYPE__ NU8;
  364. #else
  365. typedef unsigned char NU8;
  366. #endif
  367. #ifdef __UINT16_TYPE__
  368. typedef __UINT16_TYPE__ NU16;
  369. #else
  370. typedef unsigned short int NU16;
  371. #endif
  372. #ifdef __UINT32_TYPE__
  373. typedef __UINT32_TYPE__ NU32;
  374. #else
  375. typedef unsigned int NU32;
  376. #endif
  377. #ifdef __UINT64_TYPE__
  378. typedef __UINT64_TYPE__ NU64;
  379. #else
  380. typedef unsigned long long int NU64;
  381. #endif
  382. #endif
  383. #ifdef NIM_INTBITS
  384. # if NIM_INTBITS == 64
  385. typedef NI64 NI;
  386. typedef NU64 NU;
  387. # elif NIM_INTBITS == 32
  388. typedef NI32 NI;
  389. typedef NU32 NU;
  390. # elif NIM_INTBITS == 16
  391. typedef NI16 NI;
  392. typedef NU16 NU;
  393. # elif NIM_INTBITS == 8
  394. typedef NI8 NI;
  395. typedef NU8 NU;
  396. # else
  397. # error "invalid bit width for int"
  398. # endif
  399. #endif
  400. // for now there isn't an easy way for C code to reach the program result
  401. // when hot code reloading is ON - users will have to:
  402. // load the nimhcr.dll, get the hcrGetGlobal proc from there and use it
  403. #ifndef NIM_HOT_CODE_RELOADING
  404. extern NI nim_program_result;
  405. #endif
  406. typedef float NF32;
  407. typedef double NF64;
  408. typedef double NF;
  409. typedef char NIM_CHAR;
  410. typedef char* NCSTRING;
  411. #ifdef NIM_BIG_ENDIAN
  412. # define NIM_IMAN 1
  413. #else
  414. # define NIM_IMAN 0
  415. #endif
  416. #define NIM_STRLIT_FLAG ((NU)(1) << ((NIM_INTBITS) - 2)) /* This has to be the same as system.strlitFlag! */
  417. #define STRING_LITERAL(name, str, length) \
  418. static const struct { \
  419. TGenericSeq Sup; \
  420. NIM_CHAR data[(length) + 1]; \
  421. } name = {{length, (NI) ((NU)length | NIM_STRLIT_FLAG)}, str}
  422. /* declared size of a sequence/variable length array: */
  423. #if defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)
  424. # define SEQ_DECL_SIZE /* empty is correct! */
  425. #else
  426. # define SEQ_DECL_SIZE 1000000
  427. #endif
  428. #define ALLOC_0(size) calloc(1, size)
  429. #define DL_ALLOC_0(size) dlcalloc(1, size)
  430. #define paramCount() cmdCount
  431. // NAN definition copied from math.h included in the Windows SDK version 10.0.14393.0
  432. #ifndef NAN
  433. # ifndef _HUGE_ENUF
  434. # define _HUGE_ENUF 1e+300 // _HUGE_ENUF*_HUGE_ENUF must overflow
  435. # endif
  436. # define NAN_INFINITY ((float)(_HUGE_ENUF * _HUGE_ENUF))
  437. # define NAN ((float)(NAN_INFINITY * 0.0F))
  438. #endif
  439. #ifndef INF
  440. # ifdef INFINITY
  441. # define INF INFINITY
  442. # elif defined(HUGE_VAL)
  443. # define INF HUGE_VAL
  444. # elif defined(_MSC_VER)
  445. # include <float.h>
  446. # define INF (DBL_MAX+DBL_MAX)
  447. # else
  448. # define INF (1.0 / 0.0)
  449. # endif
  450. #endif
  451. typedef struct TFrame_ TFrame;
  452. struct TFrame_ {
  453. TFrame* prev;
  454. NCSTRING procname;
  455. NI line;
  456. NCSTRING filename;
  457. NI16 len;
  458. NI16 calldepth;
  459. NI frameMsgLen;
  460. };
  461. #define NIM_POSIX_INIT __attribute__((constructor))
  462. #ifdef __GNUC__
  463. # define NIM_LIKELY(x) __builtin_expect(x, 1)
  464. # define NIM_UNLIKELY(x) __builtin_expect(x, 0)
  465. /* We need the following for the posix wrapper. In particular it will give us
  466. POSIX_SPAWN_USEVFORK: */
  467. # ifndef _GNU_SOURCE
  468. # define _GNU_SOURCE
  469. # endif
  470. #else
  471. # define NIM_LIKELY(x) (x)
  472. # define NIM_UNLIKELY(x) (x)
  473. #endif
  474. #if 0 // defined(__GNUC__) || defined(__clang__)
  475. // not needed anymore because the stack marking cares about
  476. // interior pointers now
  477. static inline void GCGuard (void *ptr) { asm volatile ("" :: "X" (ptr)); }
  478. # define GC_GUARD __attribute__ ((cleanup(GCGuard)))
  479. #else
  480. # define GC_GUARD
  481. #endif
  482. // Test to see if Nim and the C compiler agree on the size of a pointer.
  483. NIM_STATIC_ASSERT(sizeof(NI) == sizeof(void*) && NIM_INTBITS == sizeof(NI)*8, "");
  484. #ifdef USE_NIM_NAMESPACE
  485. }
  486. #endif
  487. #if defined(_MSC_VER)
  488. # define NIM_ALIGN(x) __declspec(align(x))
  489. # define NIM_ALIGNOF(x) __alignof(x)
  490. #else
  491. # define NIM_ALIGN(x) __attribute__((aligned(x)))
  492. # define NIM_ALIGNOF(x) __alignof__(x)
  493. #endif
  494. /* ---------------- platform specific includes ----------------------- */
  495. /* VxWorks related includes */
  496. #if defined(__VXWORKS__)
  497. # include <sys/types.h>
  498. # include <types/vxWind.h>
  499. # include <tool/gnu/toolMacros.h>
  500. #elif defined(__FreeBSD__)
  501. # include <sys/types.h>
  502. #endif
  503. /* these exist to make the codegen logic simpler */
  504. #define nimModInt(a, b, res) (((*res) = (a) % (b)), 0)
  505. #define nimModInt64(a, b, res) (((*res) = (a) % (b)), 0)
  506. #if (!defined(_MSC_VER) || defined(__clang__)) && !defined(NIM_EmulateOverflowChecks)
  507. /* these exist because we cannot have .compilerProcs that are importc'ed
  508. by a different name */
  509. #define nimAddInt64(a, b, res) __builtin_saddll_overflow(a, b, (long long int*)res)
  510. #define nimSubInt64(a, b, res) __builtin_ssubll_overflow(a, b, (long long int*)res)
  511. #define nimMulInt64(a, b, res) __builtin_smulll_overflow(a, b, (long long int*)res)
  512. #if NIM_INTBITS == 32
  513. #define nimAddInt(a, b, res) __builtin_sadd_overflow(a, b, res)
  514. #define nimSubInt(a, b, res) __builtin_ssub_overflow(a, b, res)
  515. #define nimMulInt(a, b, res) __builtin_smul_overflow(a, b, res)
  516. #else
  517. /* map it to the 'long long' variant */
  518. #define nimAddInt(a, b, res) __builtin_saddll_overflow(a, b, (long long int*)res)
  519. #define nimSubInt(a, b, res) __builtin_ssubll_overflow(a, b, (long long int*)res)
  520. #define nimMulInt(a, b, res) __builtin_smulll_overflow(a, b, (long long int*)res)
  521. #endif
  522. #endif
  523. #define NIM_NOALIAS __restrict
  524. /* __restrict is said to work for all the C(++) compilers out there that we support */
  525. #endif /* NIMBASE_H */