nimbase.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. */
  18. #ifndef NIMBASE_H
  19. #define NIMBASE_H
  20. /*------------ declaring a custom attribute to support using LLVM's Address Sanitizer ------------ */
  21. /*
  22. This definition exists to provide support for using the LLVM ASAN (Address SANitizer) tooling with Nim. This
  23. should only be used to mark implementations of the GC system that raise false flags with the ASAN tooling, or
  24. for functions that are hot and need to be disabled for performance reasons. Based on the official ASAN
  25. documentation, both the clang and gcc compilers are supported. In addition to that, a check is performed to
  26. verify that the necessary attribute is supported by the compiler.
  27. To flag a proc as ignored, append the following code pragma to the proc declaration:
  28. {.codegenDecl: "CLANG_NO_SANITIZE_ADDRESS $# $#$#".}
  29. For further information, please refer to the official documentation:
  30. https://github.com/google/sanitizers/wiki/AddressSanitizer
  31. */
  32. #define CLANG_NO_SANITIZE_ADDRESS
  33. #if defined(__clang__)
  34. # if __has_attribute(no_sanitize_address)
  35. # undef CLANG_NO_SANITIZE_ADDRESS
  36. # define CLANG_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
  37. # endif
  38. #endif
  39. /* ------------ ignore typical warnings in Nim-generated files ------------- */
  40. #if defined(__GNUC__) || defined(__clang__)
  41. # pragma GCC diagnostic ignored "-Wpragmas"
  42. # pragma GCC diagnostic ignored "-Wwritable-strings"
  43. # pragma GCC diagnostic ignored "-Winvalid-noreturn"
  44. # pragma GCC diagnostic ignored "-Wformat"
  45. # pragma GCC diagnostic ignored "-Wlogical-not-parentheses"
  46. # pragma GCC diagnostic ignored "-Wlogical-op-parentheses"
  47. # pragma GCC diagnostic ignored "-Wshadow"
  48. # pragma GCC diagnostic ignored "-Wunused-function"
  49. # pragma GCC diagnostic ignored "-Wunused-variable"
  50. # pragma GCC diagnostic ignored "-Winvalid-offsetof"
  51. # pragma GCC diagnostic ignored "-Wtautological-compare"
  52. # pragma GCC diagnostic ignored "-Wswitch-bool"
  53. # pragma GCC diagnostic ignored "-Wmacro-redefined"
  54. # pragma GCC diagnostic ignored "-Wincompatible-pointer-types-discards-qualifiers"
  55. # pragma GCC diagnostic ignored "-Wpointer-bool-conversion"
  56. # pragma GCC diagnostic ignored "-Wconstant-conversion"
  57. #endif
  58. #if defined(_MSC_VER)
  59. # pragma warning(disable: 4005 4100 4101 4189 4191 4200 4244 4293 4296 4309)
  60. # pragma warning(disable: 4310 4365 4456 4477 4514 4574 4611 4668 4702 4706)
  61. # pragma warning(disable: 4710 4711 4774 4800 4809 4820 4996 4090 4297)
  62. #endif
  63. /* ------------------------------------------------------------------------- */
  64. #if defined(__GNUC__)
  65. # define _GNU_SOURCE 1
  66. #endif
  67. #if defined(__TINYC__)
  68. /*# define __GNUC__ 3
  69. # define GCC_MAJOR 4
  70. # define __GNUC_MINOR__ 4
  71. # define __GNUC_PATCHLEVEL__ 5 */
  72. # define __DECLSPEC_SUPPORTED 1
  73. #endif
  74. /* calling convention mess ----------------------------------------------- */
  75. #if defined(__GNUC__) || defined(__LCC__) || defined(__POCC__) \
  76. || defined(__TINYC__)
  77. /* these should support C99's inline */
  78. /* the test for __POCC__ has to come before the test for _MSC_VER,
  79. because PellesC defines _MSC_VER too. This is brain-dead. */
  80. # define N_INLINE(rettype, name) inline rettype name
  81. #elif defined(__BORLANDC__) || defined(_MSC_VER)
  82. /* Borland's compiler is really STRANGE here; note that the __fastcall
  83. keyword cannot be before the return type, but __inline cannot be after
  84. the return type, so we do not handle this mess in the code generator
  85. but rather here. */
  86. # define N_INLINE(rettype, name) __inline rettype name
  87. #elif defined(__DMC__)
  88. # define N_INLINE(rettype, name) inline rettype name
  89. #elif defined(__WATCOMC__)
  90. # define N_INLINE(rettype, name) __inline rettype name
  91. #else /* others are less picky: */
  92. # define N_INLINE(rettype, name) rettype __inline name
  93. #endif
  94. #define N_INLINE_PTR(rettype, name) rettype (*name)
  95. #if defined(__POCC__)
  96. # define NIM_CONST /* PCC is really picky with const modifiers */
  97. # undef _MSC_VER /* Yeah, right PCC defines _MSC_VER even if it is
  98. not that compatible. Well done. */
  99. #elif defined(__cplusplus)
  100. # define NIM_CONST /* C++ is picky with const modifiers */
  101. #else
  102. # define NIM_CONST const
  103. #endif
  104. /*
  105. NIM_THREADVAR declaration based on
  106. http://stackoverflow.com/questions/18298280/how-to-declare-a-variable-as-thread-local-portably
  107. */
  108. #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112 && !defined __STDC_NO_THREADS__
  109. # define NIM_THREADVAR _Thread_local
  110. #elif defined _WIN32 && ( \
  111. defined _MSC_VER || \
  112. defined __ICL || \
  113. defined __DMC__ || \
  114. defined __BORLANDC__ )
  115. # define NIM_THREADVAR __declspec(thread)
  116. #elif defined(__TINYC__) || defined(__GENODE__)
  117. # define NIM_THREADVAR
  118. /* note that ICC (linux) and Clang are covered by __GNUC__ */
  119. #elif defined __GNUC__ || \
  120. defined __SUNPRO_C || \
  121. defined __xlC__
  122. # define NIM_THREADVAR __thread
  123. #else
  124. # error "Cannot define NIM_THREADVAR"
  125. #endif
  126. /* --------------- how int64 constants should be declared: ----------- */
  127. #if defined(__GNUC__) || defined(__LCC__) || \
  128. defined(__POCC__) || defined(__DMC__) || defined(_MSC_VER)
  129. # define IL64(x) x##LL
  130. #else /* works only without LL */
  131. # define IL64(x) ((NI64)x)
  132. #endif
  133. /* ---------------- casting without correct aliasing rules ----------- */
  134. #if defined(__GNUC__)
  135. # define NIM_CAST(type, ptr) (((union{type __x__;}*)(ptr))->__x__)
  136. #else
  137. # define NIM_CAST(type, ptr) ((type)(ptr))
  138. #endif
  139. /* ------------------------------------------------------------------- */
  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_SAFECALL(rettype, name) rettype __safecall 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_SAFECALL_PTR(rettype, name) rettype (__safecall *name)
  153. # ifdef __cplusplus
  154. # define N_LIB_EXPORT extern "C" __declspec(dllexport)
  155. # else
  156. # define N_LIB_EXPORT extern __declspec(dllexport)
  157. # endif
  158. # define N_LIB_IMPORT extern __declspec(dllimport)
  159. #else
  160. # define N_LIB_PRIVATE __attribute__((visibility("hidden")))
  161. # if defined(__GNUC__)
  162. # define N_CDECL(rettype, name) rettype name
  163. # define N_STDCALL(rettype, name) rettype name
  164. # define N_SYSCALL(rettype, name) rettype name
  165. # define N_FASTCALL(rettype, name) __attribute__((fastcall)) rettype name
  166. # define N_SAFECALL(rettype, name) rettype name
  167. /* function pointers with calling convention: */
  168. # define N_CDECL_PTR(rettype, name) rettype (*name)
  169. # define N_STDCALL_PTR(rettype, name) rettype (*name)
  170. # define N_SYSCALL_PTR(rettype, name) rettype (*name)
  171. # define N_FASTCALL_PTR(rettype, name) __attribute__((fastcall)) rettype (*name)
  172. # define N_SAFECALL_PTR(rettype, name) rettype (*name)
  173. # else
  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) 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) rettype (*name)
  184. # define N_SAFECALL_PTR(rettype, name) rettype (*name)
  185. # endif
  186. # ifdef __cplusplus
  187. # define N_LIB_EXPORT extern "C"
  188. # else
  189. # define N_LIB_EXPORT extern
  190. # endif
  191. # define N_LIB_IMPORT extern
  192. #endif
  193. #define N_NOCONV(rettype, name) rettype name
  194. /* specify no calling convention */
  195. #define N_NOCONV_PTR(rettype, name) rettype (*name)
  196. #if defined(__GNUC__) || defined(__ICC__)
  197. # define N_NOINLINE(rettype, name) rettype __attribute__((noinline)) name
  198. #elif defined(_MSC_VER)
  199. # define N_NOINLINE(rettype, name) __declspec(noinline) rettype name
  200. #else
  201. # define N_NOINLINE(rettype, name) rettype name
  202. #endif
  203. #define N_NOINLINE_PTR(rettype, name) rettype (*name)
  204. #if defined(__BORLANDC__) || defined(__WATCOMC__) || \
  205. defined(__POCC__) || defined(_MSC_VER) || defined(WIN32) || defined(_WIN32)
  206. /* these compilers have a fastcall so use it: */
  207. # ifdef __TINYC__
  208. # define N_NIMCALL(rettype, name) rettype __attribute((__fastcall)) name
  209. # define N_NIMCALL_PTR(rettype, name) rettype (__attribute((__fastcall)) *name)
  210. # define N_RAW_NIMCALL __attribute((__fastcall))
  211. # else
  212. # define N_NIMCALL(rettype, name) rettype __fastcall name
  213. # define N_NIMCALL_PTR(rettype, name) rettype (__fastcall *name)
  214. # define N_RAW_NIMCALL __fastcall
  215. # endif
  216. #else
  217. # define N_NIMCALL(rettype, name) rettype name /* no modifier */
  218. # define N_NIMCALL_PTR(rettype, name) rettype (*name)
  219. # define N_RAW_NIMCALL
  220. #endif
  221. #define N_CLOSURE(rettype, name) N_NIMCALL(rettype, name)
  222. #define N_CLOSURE_PTR(rettype, name) N_NIMCALL_PTR(rettype, name)
  223. /* ----------------------------------------------------------------------- */
  224. #define COMMA ,
  225. #include <limits.h>
  226. #include <stddef.h>
  227. /* C99 compiler? */
  228. #if (defined(__STD_VERSION__) && (__STD_VERSION__ >= 199901))
  229. # define HAVE_STDINT_H
  230. #endif
  231. #if defined(__LCC__) || defined(__DMC__) || defined(__POCC__)
  232. # define HAVE_STDINT_H
  233. #endif
  234. /* wrap all Nim typedefs into namespace Nim */
  235. #ifdef USE_NIM_NAMESPACE
  236. namespace USE_NIM_NAMESPACE {
  237. #endif
  238. /* bool types (C++ has it): */
  239. #ifdef __cplusplus
  240. # ifndef NIM_TRUE
  241. # define NIM_TRUE true
  242. # endif
  243. # ifndef NIM_FALSE
  244. # define NIM_FALSE false
  245. # endif
  246. # define NIM_BOOL bool
  247. # if __cplusplus >= 201103L
  248. # /* nullptr is more type safe (less implicit conversions than 0) */
  249. # define NIM_NIL nullptr
  250. # else
  251. # /* consider using NULL if comment below for NIM_NIL doesn't apply to C++ */
  252. # define NIM_NIL 0
  253. # endif
  254. #else
  255. # ifdef bool
  256. # define NIM_BOOL bool
  257. # else
  258. typedef unsigned char NIM_BOOL;
  259. # endif
  260. # ifndef NIM_TRUE
  261. # define NIM_TRUE ((NIM_BOOL) 1)
  262. # endif
  263. # ifndef NIM_FALSE
  264. # define NIM_FALSE ((NIM_BOOL) 0)
  265. # endif
  266. # define NIM_NIL ((void*)0) /* C's NULL is fucked up in some C compilers, so
  267. the generated code does not rely on it anymore */
  268. #endif
  269. #if defined(__BORLANDC__) || defined(__DMC__) \
  270. || defined(__WATCOMC__) || defined(_MSC_VER)
  271. typedef signed char NI8;
  272. typedef signed short int NI16;
  273. typedef signed int NI32;
  274. /* XXX: Float128? */
  275. typedef unsigned char NU8;
  276. typedef unsigned short int NU16;
  277. typedef unsigned __int64 NU64;
  278. typedef __int64 NI64;
  279. typedef unsigned int NU32;
  280. #elif defined(HAVE_STDINT_H)
  281. # include <stdint.h>
  282. typedef int8_t NI8;
  283. typedef int16_t NI16;
  284. typedef int32_t NI32;
  285. typedef int64_t NI64;
  286. typedef uint64_t NU64;
  287. typedef uint8_t NU8;
  288. typedef uint16_t NU16;
  289. typedef uint32_t NU32;
  290. #else
  291. typedef signed char NI8;
  292. typedef signed short int NI16;
  293. typedef signed int NI32;
  294. /* XXX: Float128? */
  295. typedef unsigned char NU8;
  296. typedef unsigned short int NU16;
  297. typedef unsigned long long int NU64;
  298. typedef long long int NI64;
  299. typedef unsigned int NU32;
  300. #endif
  301. #ifdef NIM_INTBITS
  302. # if NIM_INTBITS == 64
  303. typedef NI64 NI;
  304. typedef NU64 NU;
  305. # elif NIM_INTBITS == 32
  306. typedef NI32 NI;
  307. typedef NU32 NU;
  308. # elif NIM_INTBITS == 16
  309. typedef NI16 NI;
  310. typedef NU16 NU;
  311. # elif NIM_INTBITS == 8
  312. typedef NI8 NI;
  313. typedef NU8 NU;
  314. # else
  315. # error "invalid bit width for int"
  316. # endif
  317. #endif
  318. extern NI nim_program_result;
  319. typedef float NF32;
  320. typedef double NF64;
  321. typedef double NF;
  322. typedef char NIM_CHAR;
  323. typedef char* NCSTRING;
  324. #ifdef NIM_BIG_ENDIAN
  325. # define NIM_IMAN 1
  326. #else
  327. # define NIM_IMAN 0
  328. #endif
  329. static N_INLINE(NI, float64ToInt32)(double x) {
  330. /* nowadays no hack necessary anymore */
  331. return x >= 0 ? (NI)(x+0.5) : (NI)(x-0.5);
  332. }
  333. static N_INLINE(NI32, float32ToInt32)(float x) {
  334. /* nowadays no hack necessary anymore */
  335. return x >= 0 ? (NI32)(x+0.5) : (NI32)(x-0.5);
  336. }
  337. #define float64ToInt64(x) ((NI64) (x))
  338. #define NIM_STRLIT_FLAG ((NU)(1) << ((NIM_INTBITS) - 2)) /* This has to be the same as system.strlitFlag! */
  339. #define STRING_LITERAL(name, str, length) \
  340. static const struct { \
  341. TGenericSeq Sup; \
  342. NIM_CHAR data[(length) + 1]; \
  343. } name = {{length, (NI) ((NU)length | NIM_STRLIT_FLAG)}, str}
  344. typedef struct TStringDesc* string;
  345. /* declared size of a sequence/variable length array: */
  346. #if defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)
  347. # define SEQ_DECL_SIZE /* empty is correct! */
  348. #else
  349. # define SEQ_DECL_SIZE 1000000
  350. #endif
  351. #define ALLOC_0(size) calloc(1, size)
  352. #define DL_ALLOC_0(size) dlcalloc(1, size)
  353. #define GenericSeqSize sizeof(TGenericSeq)
  354. #define paramCount() cmdCount
  355. // NAN definition copied from math.h included in the Windows SDK version 10.0.14393.0
  356. #ifndef NAN
  357. # ifndef _HUGE_ENUF
  358. # define _HUGE_ENUF 1e+300 // _HUGE_ENUF*_HUGE_ENUF must overflow
  359. # endif
  360. # define NAN_INFINITY ((float)(_HUGE_ENUF * _HUGE_ENUF))
  361. # define NAN ((float)(NAN_INFINITY * 0.0F))
  362. #endif
  363. #ifndef INF
  364. # ifdef INFINITY
  365. # define INF INFINITY
  366. # elif defined(HUGE_VAL)
  367. # define INF HUGE_VAL
  368. # elif defined(_MSC_VER)
  369. # include <float.h>
  370. # define INF (DBL_MAX+DBL_MAX)
  371. # else
  372. # define INF (1.0 / 0.0)
  373. # endif
  374. #endif
  375. typedef struct TFrame_ TFrame;
  376. struct TFrame_ {
  377. TFrame* prev;
  378. NCSTRING procname;
  379. NI line;
  380. NCSTRING filename;
  381. NI16 len;
  382. NI16 calldepth;
  383. };
  384. #ifdef NIM_NEW_MANGLING_RULES
  385. #define nimfr_(proc, file) \
  386. TFrame FR_; \
  387. FR_.procname = proc; FR_.filename = file; FR_.line = 0; FR_.len = 0; nimFrame(&FR_);
  388. #define nimfrs_(proc, file, slots, length) \
  389. struct {TFrame* prev;NCSTRING procname;NI line;NCSTRING filename; NI len; VarSlot s[slots];} FR_; \
  390. FR_.procname = proc; FR_.filename = file; FR_.line = 0; FR_.len = length; nimFrame((TFrame*)&FR_);
  391. #define nimln_(n, file) \
  392. FR_.line = n; FR_.filename = file;
  393. #else
  394. #define nimfr(proc, file) \
  395. TFrame FR; \
  396. FR.procname = proc; FR.filename = file; FR.line = 0; FR.len = 0; nimFrame(&FR);
  397. #define nimfrs(proc, file, slots, length) \
  398. struct {TFrame* prev;NCSTRING procname;NI line;NCSTRING filename; NI len; VarSlot s[slots];} FR; \
  399. FR.procname = proc; FR.filename = file; FR.line = 0; FR.len = length; nimFrame((TFrame*)&FR);
  400. #define nimln(n, file) \
  401. FR.line = n; FR.filename = file;
  402. #endif
  403. #define NIM_POSIX_INIT __attribute__((constructor))
  404. #ifdef __GNUC__
  405. # define likely(x) __builtin_expect(x, 1)
  406. # define unlikely(x) __builtin_expect(x, 0)
  407. /* We need the following for the posix wrapper. In particular it will give us
  408. POSIX_SPAWN_USEVFORK: */
  409. # ifndef _GNU_SOURCE
  410. # define _GNU_SOURCE
  411. # endif
  412. #else
  413. # define likely(x) (x)
  414. # define unlikely(x) (x)
  415. #endif
  416. #if 0 // defined(__GNUC__) || defined(__clang__)
  417. // not needed anymore because the stack marking cares about
  418. // interior pointers now
  419. static inline void GCGuard (void *ptr) { asm volatile ("" :: "X" (ptr)); }
  420. # define GC_GUARD __attribute__ ((cleanup(GCGuard)))
  421. #else
  422. # define GC_GUARD
  423. #endif
  424. /* Test to see if Nim and the C compiler agree on the size of a pointer.
  425. On disagreement, your C compiler will say something like:
  426. "error: 'Nim_and_C_compiler_disagree_on_target_architecture' declared as an array with a negative size" */
  427. typedef int Nim_and_C_compiler_disagree_on_target_architecture[sizeof(NI) == sizeof(void*) && NIM_INTBITS == sizeof(NI)*8 ? 1 : -1];
  428. #ifdef USE_NIM_NAMESPACE
  429. }
  430. #endif
  431. #ifdef __cplusplus
  432. # define NIM_EXTERNC extern "C"
  433. #else
  434. # define NIM_EXTERNC
  435. #endif
  436. /* ---------------- platform specific includes ----------------------- */
  437. /* VxWorks related includes */
  438. #if defined(__VXWORKS__)
  439. # include <sys/types.h>
  440. # include <types/vxWind.h>
  441. # include <tool/gnu/toolMacros.h>
  442. #elif defined(__FreeBSD__)
  443. # include <sys/types.h>
  444. #endif
  445. /* Compile with -d:checkAbi and a sufficiently C11:ish compiler to enable */
  446. #define NIM_CHECK_SIZE(typ, sz) \
  447. _Static_assert(sizeof(typ) == sz, "Nim & C disagree on type size")
  448. #endif /* NIMBASE_H */