nimbase.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. /* --------------- how int64 constants should be declared: ----------- */
  128. #if defined(__GNUC__) || defined(__LCC__) || \
  129. defined(__POCC__) || defined(__DMC__) || defined(_MSC_VER)
  130. # define IL64(x) x##LL
  131. #else /* works only without LL */
  132. # define IL64(x) ((NI64)x)
  133. #endif
  134. /* ---------------- casting without correct aliasing rules ----------- */
  135. #if defined(__GNUC__)
  136. # define NIM_CAST(type, ptr) (((union{type __x__;}*)(ptr))->__x__)
  137. #else
  138. # define NIM_CAST(type, ptr) ((type)(ptr))
  139. #endif
  140. /* ------------------------------------------------------------------- */
  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_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_SAFECALL_PTR(rettype, name) rettype (__stdcall *name)
  154. # ifdef __cplusplus
  155. # define N_LIB_EXPORT extern "C" __declspec(dllexport)
  156. # else
  157. # define N_LIB_EXPORT extern __declspec(dllexport)
  158. # endif
  159. # define N_LIB_IMPORT extern __declspec(dllimport)
  160. #else
  161. # define N_LIB_PRIVATE __attribute__((visibility("hidden")))
  162. # if defined(__GNUC__)
  163. # define N_CDECL(rettype, name) rettype name
  164. # define N_STDCALL(rettype, name) rettype name
  165. # define N_SYSCALL(rettype, name) rettype name
  166. # define N_FASTCALL(rettype, name) __attribute__((fastcall)) rettype name
  167. # define N_SAFECALL(rettype, name) rettype name
  168. /* function pointers with calling convention: */
  169. # define N_CDECL_PTR(rettype, name) rettype (*name)
  170. # define N_STDCALL_PTR(rettype, name) rettype (*name)
  171. # define N_SYSCALL_PTR(rettype, name) rettype (*name)
  172. # define N_FASTCALL_PTR(rettype, name) __attribute__((fastcall)) rettype (*name)
  173. # define N_SAFECALL_PTR(rettype, name) rettype (*name)
  174. # else
  175. # define N_CDECL(rettype, name) rettype name
  176. # define N_STDCALL(rettype, name) rettype name
  177. # define N_SYSCALL(rettype, name) rettype name
  178. # define N_FASTCALL(rettype, name) rettype name
  179. # define N_SAFECALL(rettype, name) rettype name
  180. /* function pointers with calling convention: */
  181. # define N_CDECL_PTR(rettype, name) rettype (*name)
  182. # define N_STDCALL_PTR(rettype, name) rettype (*name)
  183. # define N_SYSCALL_PTR(rettype, name) rettype (*name)
  184. # define N_FASTCALL_PTR(rettype, name) rettype (*name)
  185. # define N_SAFECALL_PTR(rettype, name) rettype (*name)
  186. # endif
  187. # ifdef __cplusplus
  188. # define N_LIB_EXPORT extern "C"
  189. # else
  190. # define N_LIB_EXPORT extern
  191. # endif
  192. # define N_LIB_IMPORT extern
  193. #endif
  194. #define N_NOCONV(rettype, name) rettype name
  195. /* specify no calling convention */
  196. #define N_NOCONV_PTR(rettype, name) rettype (*name)
  197. #if defined(__GNUC__) || defined(__ICC__)
  198. # define N_NOINLINE(rettype, name) rettype __attribute__((noinline)) name
  199. #elif defined(_MSC_VER)
  200. # define N_NOINLINE(rettype, name) __declspec(noinline) rettype name
  201. #else
  202. # define N_NOINLINE(rettype, name) rettype name
  203. #endif
  204. #define N_NOINLINE_PTR(rettype, name) rettype (*name)
  205. #if defined(__BORLANDC__) || defined(__WATCOMC__) || \
  206. defined(__POCC__) || defined(_MSC_VER) || defined(WIN32) || defined(_WIN32)
  207. /* these compilers have a fastcall so use it: */
  208. # ifdef __TINYC__
  209. # define N_NIMCALL(rettype, name) rettype __attribute((__fastcall)) name
  210. # define N_NIMCALL_PTR(rettype, name) rettype (__attribute((__fastcall)) *name)
  211. # define N_RAW_NIMCALL __attribute((__fastcall))
  212. # else
  213. # define N_NIMCALL(rettype, name) rettype __fastcall name
  214. # define N_NIMCALL_PTR(rettype, name) rettype (__fastcall *name)
  215. # define N_RAW_NIMCALL __fastcall
  216. # endif
  217. #else
  218. # define N_NIMCALL(rettype, name) rettype name /* no modifier */
  219. # define N_NIMCALL_PTR(rettype, name) rettype (*name)
  220. # define N_RAW_NIMCALL
  221. #endif
  222. #define N_CLOSURE(rettype, name) N_NIMCALL(rettype, name)
  223. #define N_CLOSURE_PTR(rettype, name) N_NIMCALL_PTR(rettype, name)
  224. /* ----------------------------------------------------------------------- */
  225. #define COMMA ,
  226. #include <limits.h>
  227. #include <stddef.h>
  228. /* C99 compiler? */
  229. #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901))
  230. # define HAVE_STDINT_H
  231. #endif
  232. /* Known compiler with stdint.h that doesn't fit the general pattern? */
  233. #if defined(__LCC__) || defined(__DMC__) || defined(__POCC__) || \
  234. defined(__AVR__) || (defined(__cplusplus) && (__cplusplus < 201103))
  235. # define HAVE_STDINT_H
  236. #endif
  237. #if (!defined(HAVE_STDINT_H) && defined(__cplusplus) && (__cplusplus >= 201103))
  238. # define HAVE_CSTDINT
  239. #endif
  240. /* wrap all Nim typedefs into namespace Nim */
  241. #ifdef USE_NIM_NAMESPACE
  242. #ifdef HAVE_CSTDINT
  243. #include <cstdint>
  244. #else
  245. #include <stdint.h>
  246. #endif
  247. namespace USE_NIM_NAMESPACE {
  248. #endif
  249. /* bool types (C++ has it): */
  250. #ifdef __cplusplus
  251. # ifndef NIM_TRUE
  252. # define NIM_TRUE true
  253. # endif
  254. # ifndef NIM_FALSE
  255. # define NIM_FALSE false
  256. # endif
  257. # define NIM_BOOL bool
  258. # if __cplusplus >= 201103L
  259. # /* nullptr is more type safe (less implicit conversions than 0) */
  260. # define NIM_NIL nullptr
  261. # else
  262. # /* consider using NULL if comment below for NIM_NIL doesn't apply to C++ */
  263. # define NIM_NIL 0
  264. # endif
  265. #else
  266. # ifdef bool
  267. # define NIM_BOOL bool
  268. # else
  269. typedef unsigned char NIM_BOOL;
  270. # endif
  271. # ifndef NIM_TRUE
  272. # define NIM_TRUE ((NIM_BOOL) 1)
  273. # endif
  274. # ifndef NIM_FALSE
  275. # define NIM_FALSE ((NIM_BOOL) 0)
  276. # endif
  277. # define NIM_NIL ((void*)0) /* C's NULL is fucked up in some C compilers, so
  278. the generated code does not rely on it anymore */
  279. #endif
  280. #if defined(__BORLANDC__) || defined(__DMC__) \
  281. || defined(__WATCOMC__) || defined(_MSC_VER)
  282. typedef signed char NI8;
  283. typedef signed short int NI16;
  284. typedef signed int NI32;
  285. typedef __int64 NI64;
  286. /* XXX: Float128? */
  287. typedef unsigned char NU8;
  288. typedef unsigned short int NU16;
  289. typedef unsigned int NU32;
  290. typedef unsigned __int64 NU64;
  291. #elif defined(HAVE_STDINT_H)
  292. #ifndef USE_NIM_NAMESPACE
  293. # include <stdint.h>
  294. #endif
  295. typedef int8_t NI8;
  296. typedef int16_t NI16;
  297. typedef int32_t NI32;
  298. typedef int64_t NI64;
  299. typedef uint8_t NU8;
  300. typedef uint16_t NU16;
  301. typedef uint32_t NU32;
  302. typedef uint64_t NU64;
  303. #elif defined(HAVE_CSTDINT)
  304. #ifndef USE_NIM_NAMESPACE
  305. # include <cstdint>
  306. #endif
  307. typedef std::int8_t NI8;
  308. typedef std::int16_t NI16;
  309. typedef std::int32_t NI32;
  310. typedef std::int64_t NI64;
  311. typedef std::uint8_t NU8;
  312. typedef std::uint16_t NU16;
  313. typedef std::uint32_t NU32;
  314. typedef std::uint64_t NU64;
  315. #else
  316. /* Unknown compiler/version, do our best */
  317. #ifdef __INT8_TYPE__
  318. typedef __INT8_TYPE__ NI8;
  319. #else
  320. typedef signed char NI8;
  321. #endif
  322. #ifdef __INT16_TYPE__
  323. typedef __INT16_TYPE__ NI16;
  324. #else
  325. typedef signed short int NI16;
  326. #endif
  327. #ifdef __INT32_TYPE__
  328. typedef __INT32_TYPE__ NI32;
  329. #else
  330. typedef signed int NI32;
  331. #endif
  332. #ifdef __INT64_TYPE__
  333. typedef __INT64_TYPE__ NI64;
  334. #else
  335. typedef long long int NI64;
  336. #endif
  337. /* XXX: Float128? */
  338. #ifdef __UINT8_TYPE__
  339. typedef __UINT8_TYPE__ NU8;
  340. #else
  341. typedef unsigned char NU8;
  342. #endif
  343. #ifdef __UINT16_TYPE__
  344. typedef __UINT16_TYPE__ NU16;
  345. #else
  346. typedef unsigned short int NU16;
  347. #endif
  348. #ifdef __UINT32_TYPE__
  349. typedef __UINT32_TYPE__ NU32;
  350. #else
  351. typedef unsigned int NU32;
  352. #endif
  353. #ifdef __UINT64_TYPE__
  354. typedef __UINT64_TYPE__ NU64;
  355. #else
  356. typedef unsigned long long int NU64;
  357. #endif
  358. #endif
  359. #ifdef NIM_INTBITS
  360. # if NIM_INTBITS == 64
  361. typedef NI64 NI;
  362. typedef NU64 NU;
  363. # elif NIM_INTBITS == 32
  364. typedef NI32 NI;
  365. typedef NU32 NU;
  366. # elif NIM_INTBITS == 16
  367. typedef NI16 NI;
  368. typedef NU16 NU;
  369. # elif NIM_INTBITS == 8
  370. typedef NI8 NI;
  371. typedef NU8 NU;
  372. # else
  373. # error "invalid bit width for int"
  374. # endif
  375. #endif
  376. // for now there isn't an easy way for C code to reach the program result
  377. // when hot code reloading is ON - users will have to:
  378. // load the nimhcr.dll, get the hcrGetGlobal proc from there and use it
  379. #ifndef NIM_HOT_CODE_RELOADING
  380. extern NI nim_program_result;
  381. #endif
  382. typedef float NF32;
  383. typedef double NF64;
  384. typedef double NF;
  385. typedef char NIM_CHAR;
  386. typedef char* NCSTRING;
  387. #ifdef NIM_BIG_ENDIAN
  388. # define NIM_IMAN 1
  389. #else
  390. # define NIM_IMAN 0
  391. #endif
  392. #define NIM_STRLIT_FLAG ((NU)(1) << ((NIM_INTBITS) - 2)) /* This has to be the same as system.strlitFlag! */
  393. #define STRING_LITERAL(name, str, length) \
  394. static const struct { \
  395. TGenericSeq Sup; \
  396. NIM_CHAR data[(length) + 1]; \
  397. } name = {{length, (NI) ((NU)length | NIM_STRLIT_FLAG)}, str}
  398. /* declared size of a sequence/variable length array: */
  399. #if defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)
  400. # define SEQ_DECL_SIZE /* empty is correct! */
  401. #else
  402. # define SEQ_DECL_SIZE 1000000
  403. #endif
  404. #define ALLOC_0(size) calloc(1, size)
  405. #define DL_ALLOC_0(size) dlcalloc(1, size)
  406. #define GenericSeqSize sizeof(TGenericSeq)
  407. #define paramCount() cmdCount
  408. // NAN definition copied from math.h included in the Windows SDK version 10.0.14393.0
  409. #ifndef NAN
  410. # ifndef _HUGE_ENUF
  411. # define _HUGE_ENUF 1e+300 // _HUGE_ENUF*_HUGE_ENUF must overflow
  412. # endif
  413. # define NAN_INFINITY ((float)(_HUGE_ENUF * _HUGE_ENUF))
  414. # define NAN ((float)(NAN_INFINITY * 0.0F))
  415. #endif
  416. #ifndef INF
  417. # ifdef INFINITY
  418. # define INF INFINITY
  419. # elif defined(HUGE_VAL)
  420. # define INF HUGE_VAL
  421. # elif defined(_MSC_VER)
  422. # include <float.h>
  423. # define INF (DBL_MAX+DBL_MAX)
  424. # else
  425. # define INF (1.0 / 0.0)
  426. # endif
  427. #endif
  428. typedef struct TFrame_ TFrame;
  429. struct TFrame_ {
  430. TFrame* prev;
  431. NCSTRING procname;
  432. NI line;
  433. NCSTRING filename;
  434. NI16 len;
  435. NI16 calldepth;
  436. };
  437. #define NIM_POSIX_INIT __attribute__((constructor))
  438. #ifdef __GNUC__
  439. # define NIM_LIKELY(x) __builtin_expect(x, 1)
  440. # define NIM_UNLIKELY(x) __builtin_expect(x, 0)
  441. /* We need the following for the posix wrapper. In particular it will give us
  442. POSIX_SPAWN_USEVFORK: */
  443. # ifndef _GNU_SOURCE
  444. # define _GNU_SOURCE
  445. # endif
  446. #else
  447. # define NIM_LIKELY(x) (x)
  448. # define NIM_UNLIKELY(x) (x)
  449. #endif
  450. #if 0 // defined(__GNUC__) || defined(__clang__)
  451. // not needed anymore because the stack marking cares about
  452. // interior pointers now
  453. static inline void GCGuard (void *ptr) { asm volatile ("" :: "X" (ptr)); }
  454. # define GC_GUARD __attribute__ ((cleanup(GCGuard)))
  455. #else
  456. # define GC_GUARD
  457. #endif
  458. /* Test to see if Nim and the C compiler agree on the size of a pointer.
  459. On disagreement, your C compiler will say something like:
  460. "error: 'Nim_and_C_compiler_disagree_on_target_architecture' declared as an array with a negative size" */
  461. typedef int Nim_and_C_compiler_disagree_on_target_architecture[sizeof(NI) == sizeof(void*) && NIM_INTBITS == sizeof(NI)*8 ? 1 : -1];
  462. #ifdef USE_NIM_NAMESPACE
  463. }
  464. #endif
  465. #ifdef __cplusplus
  466. # define NIM_EXTERNC extern "C"
  467. #else
  468. # define NIM_EXTERNC
  469. #endif
  470. #if defined(_MSC_VER)
  471. # define NIM_ALIGN(x) __declspec(align(x))
  472. #else
  473. # define NIM_ALIGN(x) __attribute__((aligned(x)))
  474. #endif
  475. /* ---------------- platform specific includes ----------------------- */
  476. /* VxWorks related includes */
  477. #if defined(__VXWORKS__)
  478. # include <sys/types.h>
  479. # include <types/vxWind.h>
  480. # include <tool/gnu/toolMacros.h>
  481. #elif defined(__FreeBSD__)
  482. # include <sys/types.h>
  483. #endif
  484. /* Compile with -d:checkAbi and a sufficiently C11:ish compiler to enable */
  485. #define NIM_CHECK_SIZE(typ, sz) \
  486. _Static_assert(sizeof(typ) == sz, "Nim & C disagree on type size")
  487. #endif /* NIMBASE_H */