nimbase.h 19 KB

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