nimbase.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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 4820 4996 4090)
  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. #if defined(__POCC__)
  95. # define NIM_CONST /* PCC is really picky with const modifiers */
  96. # undef _MSC_VER /* Yeah, right PCC defines _MSC_VER even if it is
  97. not that compatible. Well done. */
  98. #elif defined(__cplusplus)
  99. # define NIM_CONST /* C++ is picky with const modifiers */
  100. #else
  101. # define NIM_CONST const
  102. #endif
  103. /*
  104. NIM_THREADVAR declaration based on
  105. http://stackoverflow.com/questions/18298280/how-to-declare-a-variable-as-thread-local-portably
  106. */
  107. #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112 && !defined __STDC_NO_THREADS__
  108. # define NIM_THREADVAR _Thread_local
  109. #elif defined _WIN32 && ( \
  110. defined _MSC_VER || \
  111. defined __ICL || \
  112. defined __DMC__ || \
  113. defined __BORLANDC__ )
  114. # define NIM_THREADVAR __declspec(thread)
  115. /* note that ICC (linux) and Clang are covered by __GNUC__ */
  116. #elif defined __GNUC__ || \
  117. defined __SUNPRO_C || \
  118. defined __xlC__
  119. # define NIM_THREADVAR __thread
  120. #elif defined __TINYC__
  121. # define NIM_THREADVAR
  122. #else
  123. # error "Cannot define NIM_THREADVAR"
  124. #endif
  125. /* --------------- how int64 constants should be declared: ----------- */
  126. #if defined(__GNUC__) || defined(__LCC__) || \
  127. defined(__POCC__) || defined(__DMC__) || defined(_MSC_VER)
  128. # define IL64(x) x##LL
  129. #else /* works only without LL */
  130. # define IL64(x) ((NI64)x)
  131. #endif
  132. /* ---------------- casting without correct aliasing rules ----------- */
  133. #if defined(__GNUC__)
  134. # define NIM_CAST(type, ptr) (((union{type __x__;}*)(ptr))->__x__)
  135. #else
  136. # define NIM_CAST(type, ptr) ((type)(ptr))
  137. #endif
  138. /* ------------------------------------------------------------------- */
  139. #if defined(WIN32) || defined(_WIN32) /* only Windows has this mess... */
  140. # define N_CDECL(rettype, name) rettype __cdecl name
  141. # define N_STDCALL(rettype, name) rettype __stdcall name
  142. # define N_SYSCALL(rettype, name) rettype __syscall name
  143. # define N_FASTCALL(rettype, name) rettype __fastcall name
  144. # define N_SAFECALL(rettype, name) rettype __safecall name
  145. /* function pointers with calling convention: */
  146. # define N_CDECL_PTR(rettype, name) rettype (__cdecl *name)
  147. # define N_STDCALL_PTR(rettype, name) rettype (__stdcall *name)
  148. # define N_SYSCALL_PTR(rettype, name) rettype (__syscall *name)
  149. # define N_FASTCALL_PTR(rettype, name) rettype (__fastcall *name)
  150. # define N_SAFECALL_PTR(rettype, name) rettype (__safecall *name)
  151. # ifdef __cplusplus
  152. # define N_LIB_EXPORT extern "C" __declspec(dllexport)
  153. # else
  154. # define N_LIB_EXPORT extern __declspec(dllexport)
  155. # endif
  156. # define N_LIB_IMPORT extern __declspec(dllimport)
  157. #else
  158. # if defined(__GNUC__)
  159. # define N_CDECL(rettype, name) rettype name
  160. # define N_STDCALL(rettype, name) rettype name
  161. # define N_SYSCALL(rettype, name) rettype name
  162. # define N_FASTCALL(rettype, name) __attribute__((fastcall)) rettype name
  163. # define N_SAFECALL(rettype, name) rettype name
  164. /* function pointers with calling convention: */
  165. # define N_CDECL_PTR(rettype, name) rettype (*name)
  166. # define N_STDCALL_PTR(rettype, name) rettype (*name)
  167. # define N_SYSCALL_PTR(rettype, name) rettype (*name)
  168. # define N_FASTCALL_PTR(rettype, name) __attribute__((fastcall)) rettype (*name)
  169. # define N_SAFECALL_PTR(rettype, name) rettype (*name)
  170. # else
  171. # define N_CDECL(rettype, name) rettype name
  172. # define N_STDCALL(rettype, name) rettype name
  173. # define N_SYSCALL(rettype, name) rettype name
  174. # define N_FASTCALL(rettype, name) rettype name
  175. # define N_SAFECALL(rettype, name) rettype name
  176. /* function pointers with calling convention: */
  177. # define N_CDECL_PTR(rettype, name) rettype (*name)
  178. # define N_STDCALL_PTR(rettype, name) rettype (*name)
  179. # define N_SYSCALL_PTR(rettype, name) rettype (*name)
  180. # define N_FASTCALL_PTR(rettype, name) rettype (*name)
  181. # define N_SAFECALL_PTR(rettype, name) rettype (*name)
  182. # endif
  183. # ifdef __cplusplus
  184. # define N_LIB_EXPORT extern "C"
  185. # else
  186. # define N_LIB_EXPORT extern
  187. # endif
  188. # define N_LIB_IMPORT extern
  189. #endif
  190. #define N_NOCONV(rettype, name) rettype name
  191. /* specify no calling convention */
  192. #define N_NOCONV_PTR(rettype, name) rettype (*name)
  193. #if defined(__GNUC__) || defined(__ICC__)
  194. # define N_NOINLINE(rettype, name) rettype __attribute__((noinline)) name
  195. #elif defined(_MSC_VER)
  196. # define N_NOINLINE(rettype, name) __declspec(noinline) rettype name
  197. #else
  198. # define N_NOINLINE(rettype, name) rettype name
  199. #endif
  200. #define N_NOINLINE_PTR(rettype, name) rettype (*name)
  201. #if defined(__BORLANDC__) || defined(__WATCOMC__) || \
  202. defined(__POCC__) || defined(_MSC_VER) || defined(WIN32) || defined(_WIN32)
  203. /* these compilers have a fastcall so use it: */
  204. # ifdef __TINYC__
  205. # define N_NIMCALL(rettype, name) rettype __attribute((__fastcall)) name
  206. # define N_NIMCALL_PTR(rettype, name) rettype (__attribute((__fastcall)) *name)
  207. # define N_RAW_NIMCALL __attribute((__fastcall))
  208. # else
  209. # define N_NIMCALL(rettype, name) rettype __fastcall name
  210. # define N_NIMCALL_PTR(rettype, name) rettype (__fastcall *name)
  211. # define N_RAW_NIMCALL __fastcall
  212. # endif
  213. #else
  214. # define N_NIMCALL(rettype, name) rettype name /* no modifier */
  215. # define N_NIMCALL_PTR(rettype, name) rettype (*name)
  216. # define N_RAW_NIMCALL
  217. #endif
  218. #define N_CLOSURE(rettype, name) N_NIMCALL(rettype, name)
  219. #define N_CLOSURE_PTR(rettype, name) N_NIMCALL_PTR(rettype, name)
  220. /* ----------------------------------------------------------------------- */
  221. #define COMMA ,
  222. #include <limits.h>
  223. #include <stddef.h>
  224. /* C99 compiler? */
  225. #if (defined(__STD_VERSION__) && (__STD_VERSION__ >= 199901))
  226. # define HAVE_STDINT_H
  227. #endif
  228. #if defined(__LCC__) || defined(__DMC__) || defined(__POCC__)
  229. # define HAVE_STDINT_H
  230. #endif
  231. /* bool types (C++ has it): */
  232. #ifdef __cplusplus
  233. # ifndef NIM_TRUE
  234. # define NIM_TRUE true
  235. # endif
  236. # ifndef NIM_FALSE
  237. # define NIM_FALSE false
  238. # endif
  239. # define NIM_BOOL bool
  240. # define NIM_NIL 0
  241. struct NimException
  242. {
  243. NimException(struct Exception* exp, const char* msg): exp(exp), msg(msg) {}
  244. struct Exception* exp;
  245. const char* msg;
  246. };
  247. #else
  248. # ifdef bool
  249. # define NIM_BOOL bool
  250. # else
  251. typedef unsigned char NIM_BOOL;
  252. # endif
  253. # ifndef NIM_TRUE
  254. # define NIM_TRUE ((NIM_BOOL) 1)
  255. # endif
  256. # ifndef NIM_FALSE
  257. # define NIM_FALSE ((NIM_BOOL) 0)
  258. # endif
  259. # define NIM_NIL ((void*)0) /* C's NULL is fucked up in some C compilers, so
  260. the generated code does not rely on it anymore */
  261. #endif
  262. #if defined(__BORLANDC__) || defined(__DMC__) \
  263. || defined(__WATCOMC__) || defined(_MSC_VER)
  264. typedef signed char NI8;
  265. typedef signed short int NI16;
  266. typedef signed int NI32;
  267. /* XXX: Float128? */
  268. typedef unsigned char NU8;
  269. typedef unsigned short int NU16;
  270. typedef unsigned __int64 NU64;
  271. typedef __int64 NI64;
  272. typedef unsigned int NU32;
  273. #elif defined(HAVE_STDINT_H)
  274. # include <stdint.h>
  275. typedef int8_t NI8;
  276. typedef int16_t NI16;
  277. typedef int32_t NI32;
  278. typedef int64_t NI64;
  279. typedef uint64_t NU64;
  280. typedef uint8_t NU8;
  281. typedef uint16_t NU16;
  282. typedef uint32_t NU32;
  283. #else
  284. typedef signed char NI8;
  285. typedef signed short int NI16;
  286. typedef signed int NI32;
  287. /* XXX: Float128? */
  288. typedef unsigned char NU8;
  289. typedef unsigned short int NU16;
  290. typedef unsigned long long int NU64;
  291. typedef long long int NI64;
  292. typedef unsigned int NU32;
  293. #endif
  294. #ifdef NIM_INTBITS
  295. # if NIM_INTBITS == 64
  296. typedef NI64 NI;
  297. typedef NU64 NU;
  298. # elif NIM_INTBITS == 32
  299. typedef NI32 NI;
  300. typedef NU32 NU;
  301. # elif NIM_INTBITS == 16
  302. typedef NI16 NI;
  303. typedef NU16 NU;
  304. # elif NIM_INTBITS == 8
  305. typedef NI8 NI;
  306. typedef NU8 NU;
  307. # else
  308. # error "invalid bit width for int"
  309. # endif
  310. #endif
  311. extern NI nim_program_result;
  312. typedef float NF32;
  313. typedef double NF64;
  314. typedef double NF;
  315. typedef char NIM_CHAR;
  316. typedef char* NCSTRING;
  317. #ifdef NIM_BIG_ENDIAN
  318. # define NIM_IMAN 1
  319. #else
  320. # define NIM_IMAN 0
  321. #endif
  322. static N_INLINE(NI, float64ToInt32)(double x) {
  323. /* nowadays no hack necessary anymore */
  324. return x >= 0 ? (NI)(x+0.5) : (NI)(x-0.5);
  325. }
  326. static N_INLINE(NI32, float32ToInt32)(float x) {
  327. /* nowadays no hack necessary anymore */
  328. return x >= 0 ? (NI32)(x+0.5) : (NI32)(x-0.5);
  329. }
  330. #define float64ToInt64(x) ((NI64) (x))
  331. #define NIM_STRLIT_FLAG ((NU)(1) << ((NIM_INTBITS) - 2)) /* This has to be the same as system.strlitFlag! */
  332. #define STRING_LITERAL(name, str, length) \
  333. static const struct { \
  334. TGenericSeq Sup; \
  335. NIM_CHAR data[(length) + 1]; \
  336. } name = {{length, (NI) ((NU)length | NIM_STRLIT_FLAG)}, str}
  337. typedef struct TStringDesc* string;
  338. /* declared size of a sequence/variable length array: */
  339. #if defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)
  340. # define SEQ_DECL_SIZE /* empty is correct! */
  341. #else
  342. # define SEQ_DECL_SIZE 1000000
  343. #endif
  344. #define ALLOC_0(size) calloc(1, size)
  345. #define DL_ALLOC_0(size) dlcalloc(1, size)
  346. #define GenericSeqSize sizeof(TGenericSeq)
  347. #define paramCount() cmdCount
  348. #ifndef NAN
  349. # define NAN (0.0 / 0.0)
  350. #endif
  351. #ifndef INF
  352. # ifdef INFINITY
  353. # define INF INFINITY
  354. # elif defined(HUGE_VAL)
  355. # define INF HUGE_VAL
  356. # elif defined(_MSC_VER)
  357. # include <float.h>
  358. # define INF (DBL_MAX+DBL_MAX)
  359. # else
  360. # define INF (1.0 / 0.0)
  361. # endif
  362. #endif
  363. typedef struct TFrame TFrame;
  364. struct TFrame {
  365. TFrame* prev;
  366. NCSTRING procname;
  367. NI line;
  368. NCSTRING filename;
  369. NI16 len;
  370. NI16 calldepth;
  371. };
  372. #ifdef NIM_NEW_MANGLING_RULES
  373. #define nimfr_(proc, file) \
  374. TFrame FR_; \
  375. FR_.procname = proc; FR_.filename = file; FR_.line = 0; FR_.len = 0; nimFrame(&FR_);
  376. #define nimfrs_(proc, file, slots, length) \
  377. struct {TFrame* prev;NCSTRING procname;NI line;NCSTRING filename; NI len; VarSlot s[slots];} FR_; \
  378. FR_.procname = proc; FR_.filename = file; FR_.line = 0; FR_.len = length; nimFrame((TFrame*)&FR_);
  379. #define nimln_(n, file) \
  380. FR_.line = n; FR_.filename = file;
  381. #else
  382. #define nimfr(proc, file) \
  383. TFrame FR; \
  384. FR.procname = proc; FR.filename = file; FR.line = 0; FR.len = 0; nimFrame(&FR);
  385. #define nimfrs(proc, file, slots, length) \
  386. struct {TFrame* prev;NCSTRING procname;NI line;NCSTRING filename; NI len; VarSlot s[slots];} FR; \
  387. FR.procname = proc; FR.filename = file; FR.line = 0; FR.len = length; nimFrame((TFrame*)&FR);
  388. #define nimln(n, file) \
  389. FR.line = n; FR.filename = file;
  390. #endif
  391. #define NIM_POSIX_INIT __attribute__((constructor))
  392. #ifdef __GNUC__
  393. # define likely(x) __builtin_expect(x, 1)
  394. # define unlikely(x) __builtin_expect(x, 0)
  395. /* We need the following for the posix wrapper. In particular it will give us
  396. POSIX_SPAWN_USEVFORK: */
  397. # ifndef _GNU_SOURCE
  398. # define _GNU_SOURCE
  399. # endif
  400. #else
  401. # define likely(x) (x)
  402. # define unlikely(x) (x)
  403. #endif
  404. #if 0 // defined(__GNUC__) || defined(__clang__)
  405. // not needed anymore because the stack marking cares about
  406. // interior pointers now
  407. static inline void GCGuard (void *ptr) { asm volatile ("" :: "X" (ptr)); }
  408. # define GC_GUARD __attribute__ ((cleanup(GCGuard)))
  409. #else
  410. # define GC_GUARD
  411. #endif
  412. /* Test to see if Nim and the C compiler agree on the size of a pointer.
  413. On disagreement, your C compiler will say something like:
  414. "error: 'Nim_and_C_compiler_disagree_on_target_architecture' declared as an array with a negative size" */
  415. typedef int Nim_and_C_compiler_disagree_on_target_architecture[sizeof(NI) == sizeof(void*) && NIM_INTBITS == sizeof(NI)*8 ? 1 : -1];
  416. #endif
  417. #ifdef __cplusplus
  418. # define NIM_EXTERNC extern "C"
  419. #else
  420. # define NIM_EXTERNC
  421. #endif
  422. /* ---------------- platform specific includes ----------------------- */
  423. /* VxWorks related includes */
  424. #if defined(__VXWORKS__)
  425. # include <sys/types.h>
  426. # include <types/vxWind.h>
  427. # include <tool/gnu/toolMacros.h>
  428. #elif defined(__FreeBSD__)
  429. # include <sys/types.h>
  430. #endif
  431. #if defined(__GENODE__)
  432. #include <libc/component.h>
  433. extern Libc::Env *genodeEnv;
  434. #endif
  435. /* Compile with -d:checkAbi and a sufficiently C11:ish compiler to enable */
  436. #define NIM_CHECK_SIZE(typ, sz) \
  437. _Static_assert(sizeof(typ) == sz, "Nim & C disagree on type size")