tccdefs.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /* tccdefs.h
  2. Nothing is defined before this file except target machine, target os
  3. and the few things related to option settings in tccpp.c:tcc_predefs().
  4. This file is either included at runtime as is, or converted and
  5. included as C-strings at compile-time (depending on CONFIG_TCC_PREDEFS).
  6. Note that line indent matters:
  7. - in lines starting at column 1, platform macros are replaced by
  8. corresponding TCC target compile-time macros. See conftest.c for
  9. the list of platform macros supported in lines starting at column 1.
  10. - only lines indented >= 4 are actually included into the executable,
  11. check tccdefs_.h.
  12. */
  13. #if __SIZEOF_POINTER__ == 4
  14. /* 32bit systems. */
  15. #if defined __OpenBSD__
  16. #define __SIZE_TYPE__ unsigned long
  17. #define __PTRDIFF_TYPE__ long
  18. #else
  19. #define __SIZE_TYPE__ unsigned int
  20. #define __PTRDIFF_TYPE__ int
  21. #endif
  22. #define __ILP32__ 1
  23. #define __INT64_TYPE__ long long
  24. #elif __SIZEOF_LONG__ == 4
  25. /* 64bit Windows. */
  26. #define __SIZE_TYPE__ unsigned long long
  27. #define __PTRDIFF_TYPE__ long long
  28. #define __LLP64__ 1
  29. #define __INT64_TYPE__ long long
  30. #else
  31. /* Other 64bit systems. */
  32. #define __SIZE_TYPE__ unsigned long
  33. #define __PTRDIFF_TYPE__ long
  34. #define __LP64__ 1
  35. # if defined __linux__
  36. #define __INT64_TYPE__ long
  37. # else /* APPLE, BSD */
  38. #define __INT64_TYPE__ long long
  39. # endif
  40. #endif
  41. #define __SIZEOF_INT__ 4
  42. #define __INT_MAX__ 0x7fffffff
  43. #if __SIZEOF_LONG__ == 4
  44. #define __LONG_MAX__ 0x7fffffffL
  45. #else
  46. #define __LONG_MAX__ 0x7fffffffffffffffL
  47. #endif
  48. #define __SIZEOF_LONG_LONG__ 8
  49. #define __LONG_LONG_MAX__ 0x7fffffffffffffffLL
  50. #define __CHAR_BIT__ 8
  51. #define __ORDER_LITTLE_ENDIAN__ 1234
  52. #define __ORDER_BIG_ENDIAN__ 4321
  53. #define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
  54. #if defined _WIN32
  55. #define __WCHAR_TYPE__ unsigned short
  56. #define __WINT_TYPE__ unsigned short
  57. #elif defined __linux__
  58. #define __WCHAR_TYPE__ int
  59. #define __WINT_TYPE__ unsigned int
  60. #else
  61. #define __WCHAR_TYPE__ int
  62. #define __WINT_TYPE__ int
  63. #endif
  64. #if __STDC_VERSION__ >= 201112L
  65. # define __STDC_NO_ATOMICS__ 1
  66. # define __STDC_NO_COMPLEX__ 1
  67. # define __STDC_NO_THREADS__ 1
  68. #if !defined _WIN32
  69. # define __STDC_UTF_16__ 1
  70. # define __STDC_UTF_32__ 1
  71. #endif
  72. #endif
  73. #if defined _WIN32
  74. #define __declspec(x) __attribute__((x))
  75. #define __cdecl
  76. #elif defined __FreeBSD__
  77. #define __GNUC__ 9
  78. #define __GNUC_MINOR__ 3
  79. #define __GNUC_PATCHLEVEL__ 0
  80. #define __GNUC_STDC_INLINE__ 1
  81. #define __NO_TLS 1
  82. #define __RUNETYPE_INTERNAL 1
  83. # if __SIZEOF_POINTER__ == 8
  84. /* FIXME, __int128_t is used by setjump */
  85. #define __int128_t struct { unsigned char _dummy[16] __attribute((aligned(16))); }
  86. #define __SIZEOF_SIZE_T__ 8
  87. #define __SIZEOF_PTRDIFF_T__ 8
  88. #else
  89. #define __SIZEOF_SIZE_T__ 4
  90. #define __SIZEOF_PTRDIFF_T__ 4
  91. # endif
  92. #elif defined __FreeBSD_kernel__
  93. #elif defined __NetBSD__
  94. #define __GNUC__ 4
  95. #define __GNUC_MINOR__ 1
  96. #define __GNUC_PATCHLEVEL__ 0
  97. #define _Pragma(x)
  98. #define __ELF__ 1
  99. #if defined __aarch64__
  100. #define _LOCORE /* avoids usage of __asm */
  101. #endif
  102. #elif defined __OpenBSD__
  103. #define __GNUC__ 4
  104. #define _ANSI_LIBRARY 1
  105. #elif defined __APPLE__
  106. /* emulate APPLE-GCC to make libc's headerfiles compile: */
  107. #define __GNUC__ 4 /* darwin emits warning on GCC<4 */
  108. #define __APPLE_CC__ 1 /* for <TargetConditionals.h> */
  109. #define __LITTLE_ENDIAN__ 1
  110. #define _DONT_USE_CTYPE_INLINE_ 1
  111. /* avoids usage of GCC/clang specific builtins in libc-headerfiles: */
  112. #define __FINITE_MATH_ONLY__ 1
  113. #define _FORTIFY_SOURCE 0
  114. //#define __has_builtin(x) 0
  115. #define _Float16 short unsigned int /* fake type just for size & alignment (macOS Sequoia) */
  116. #elif defined __ANDROID__
  117. #define BIONIC_IOCTL_NO_SIGNEDNESS_OVERLOAD
  118. #else
  119. /* Linux */
  120. #endif
  121. /* Some derived integer types needed to get stdint.h to compile correctly on some platforms */
  122. #ifndef __NetBSD__
  123. #define __UINTPTR_TYPE__ unsigned __PTRDIFF_TYPE__
  124. #define __INTPTR_TYPE__ __PTRDIFF_TYPE__
  125. #endif
  126. #define __INT32_TYPE__ int
  127. #if defined __aarch64__
  128. /* GCC's __uint128_t appears in some Linux/OSX header files. Make it a
  129. synonym for long double to get the size and alignment right. */
  130. #define __uint128_t long double
  131. #endif
  132. #if !defined _WIN32
  133. /* glibc defines. We do not support __USER_NAME_PREFIX__ */
  134. #define __REDIRECT(name, proto, alias) name proto __asm__ (#alias)
  135. #define __REDIRECT_NTH(name, proto, alias) name proto __asm__ (#alias) __THROW
  136. #define __REDIRECT_NTHNL(name, proto, alias) name proto __asm__ (#alias) __THROWNL
  137. #endif
  138. /* not implemented */
  139. #define __PRETTY_FUNCTION__ __FUNCTION__
  140. #define __has_builtin(x) 0
  141. #define __has_feature(x) 0
  142. #define __has_attribute(x) 0
  143. /* C23 Keywords */
  144. #define _Nonnull
  145. #define _Nullable
  146. #define _Nullable_result
  147. #define _Null_unspecified
  148. /* skip __builtin... with -E */
  149. #ifndef __TCC_PP__
  150. #define __builtin_offsetof(type, field) ((__SIZE_TYPE__)&((type*)0)->field)
  151. #define __builtin_extract_return_addr(x) x
  152. #if !defined __linux__ && !defined _WIN32
  153. /* used by math.h */
  154. #define __builtin_huge_val() 1e500
  155. #define __builtin_huge_valf() 1e50f
  156. #define __builtin_huge_vall() 1e5000L
  157. # if defined __APPLE__
  158. #define __builtin_nanf(ignored_string) (0.0F/0.0F)
  159. /* used by floats.h to implement FLT_ROUNDS C99 macro. 1 == to nearest */
  160. #define __builtin_flt_rounds() 1
  161. /* used by _fd_def.h */
  162. #define __builtin_bzero(p, ignored_size) bzero(p, sizeof(*(p)))
  163. # else
  164. #define __builtin_nanf(ignored_string) (0.0F/0.0F)
  165. # endif
  166. #endif
  167. /* __builtin_va_list */
  168. #if defined __x86_64__
  169. #if !defined _WIN32
  170. /* GCC compatible definition of va_list. */
  171. enum __va_arg_type {
  172. __va_gen_reg, __va_float_reg, __va_stack
  173. };
  174. typedef struct {
  175. unsigned gp_offset, fp_offset;
  176. union {
  177. unsigned overflow_offset;
  178. char *overflow_arg_area;
  179. };
  180. char *reg_save_area;
  181. } __builtin_va_list[1];
  182. static inline void *__va_arg(__builtin_va_list ap, int arg_type,
  183. int size, int align)
  184. {
  185. size = (size + 7) & ~7;
  186. align = (align + 7) & ~7;
  187. switch ((enum __va_arg_type)arg_type) {
  188. case __va_gen_reg:
  189. if (ap->gp_offset + size <= 48) {
  190. ap->gp_offset += size;
  191. return ap->reg_save_area + ap->gp_offset - size;
  192. }
  193. goto use_overflow_area;
  194. case __va_float_reg:
  195. if (ap->fp_offset < 128 + 48) {
  196. ap->fp_offset += 16;
  197. if (size == 8)
  198. return ap->reg_save_area + ap->fp_offset - 16;
  199. if (ap->fp_offset < 128 + 48) {
  200. double *p = (double *)(ap->reg_save_area + ap->fp_offset);
  201. p[-1] = p[0];
  202. ap->fp_offset += 16;
  203. return ap->reg_save_area + ap->fp_offset - 32;
  204. }
  205. }
  206. goto use_overflow_area;
  207. case __va_stack:
  208. use_overflow_area:
  209. ap->overflow_arg_area += size;
  210. ap->overflow_arg_area =
  211. (char*)((long long)(ap->overflow_arg_area + align - 1) & -align);
  212. return ap->overflow_arg_area - size;
  213. default: /* should never happen */
  214. char *a = (char *)0; *a = 0; // abort
  215. return 0;
  216. }
  217. }
  218. #define __builtin_va_start(ap, last) \
  219. (*(ap) = *(__builtin_va_list)((char*)__builtin_frame_address(0) - 24))
  220. #define __builtin_va_arg(ap, t) \
  221. (*(t *)(__va_arg(ap, __builtin_va_arg_types(t), sizeof(t), __alignof__(t))))
  222. #define __builtin_va_copy(dest, src) (*(dest) = *(src))
  223. #else /* _WIN64 */
  224. typedef char *__builtin_va_list;
  225. #define __builtin_va_arg(ap, t) ((sizeof(t) > 8 || (sizeof(t) & (sizeof(t) - 1))) \
  226. ? **(t **)((ap += 8) - 8) : *(t *)((ap += 8) - 8))
  227. #endif
  228. #elif defined __arm__
  229. typedef char *__builtin_va_list;
  230. #define _tcc_alignof(type) ((int)&((struct {char c;type x;} *)0)->x)
  231. #define _tcc_align(addr,type) (((unsigned)addr + _tcc_alignof(type) - 1) \
  232. & ~(_tcc_alignof(type) - 1))
  233. #define __builtin_va_start(ap,last) (ap = ((char *)&(last)) + ((sizeof(last)+3)&~3))
  234. #define __builtin_va_arg(ap,type) (ap = (void *) ((_tcc_align(ap,type)+sizeof(type)+3) \
  235. &~3), *(type *)(ap - ((sizeof(type)+3)&~3)))
  236. #elif defined __aarch64__
  237. #if defined __APPLE__
  238. typedef struct {
  239. void *__stack;
  240. } __builtin_va_list;
  241. #else
  242. typedef struct {
  243. void *__stack, *__gr_top, *__vr_top;
  244. int __gr_offs, __vr_offs;
  245. } __builtin_va_list;
  246. #endif
  247. #elif defined __riscv
  248. typedef char *__builtin_va_list;
  249. #define __va_reg_size (__riscv_xlen >> 3)
  250. #define _tcc_align(addr,type) (((unsigned long)addr + __alignof__(type) - 1) \
  251. & -(__alignof__(type)))
  252. #define __builtin_va_arg(ap,type) (*(sizeof(type) > (2*__va_reg_size) ? *(type **)((ap += __va_reg_size) - __va_reg_size) : (ap = (va_list)(_tcc_align(ap,type) + (sizeof(type)+__va_reg_size - 1)& -__va_reg_size), (type *)(ap - ((sizeof(type)+ __va_reg_size - 1)& -__va_reg_size)))))
  253. #else /* __i386__ */
  254. typedef char *__builtin_va_list;
  255. #define __builtin_va_start(ap,last) (ap = ((char *)&(last)) + ((sizeof(last)+3)&~3))
  256. #define __builtin_va_arg(ap,t) (*(t*)((ap+=(sizeof(t)+3)&~3)-((sizeof(t)+3)&~3)))
  257. #endif
  258. #define __builtin_va_end(ap) (void)(ap)
  259. #ifndef __builtin_va_copy
  260. # define __builtin_va_copy(dest, src) (dest) = (src)
  261. #endif
  262. /* TCC BBUILTIN AND BOUNDS ALIASES */
  263. #ifdef __leading_underscore
  264. # define __RENAME(X) __asm__("_"X)
  265. #else
  266. # define __RENAME(X) __asm__(X)
  267. #endif
  268. #ifdef __TCC_BCHECK__
  269. # define __BUILTINBC(ret,name,params) ret __builtin_##name params __RENAME("__bound_"#name);
  270. # define __BOUND(ret,name,params) ret name params __RENAME("__bound_"#name);
  271. #else
  272. # define __BUILTINBC(ret,name,params) ret __builtin_##name params __RENAME(#name);
  273. # define __BOUND(ret,name,params)
  274. #endif
  275. #ifdef _WIN32
  276. #define __BOTH __BOUND
  277. #define __BUILTIN(ret,name,params)
  278. #else
  279. #define __BOTH(ret,name,params) __BUILTINBC(ret,name,params)__BOUND(ret,name,params)
  280. #define __BUILTIN(ret,name,params) ret __builtin_##name params __RENAME(#name);
  281. #endif
  282. __BOTH(void*, memcpy, (void *, const void*, __SIZE_TYPE__))
  283. __BOTH(void*, memmove, (void *, const void*, __SIZE_TYPE__))
  284. __BOTH(void*, memset, (void *, int, __SIZE_TYPE__))
  285. __BOTH(int, memcmp, (const void *, const void*, __SIZE_TYPE__))
  286. __BOTH(__SIZE_TYPE__, strlen, (const char *))
  287. __BOTH(char*, strcpy, (char *, const char *))
  288. __BOTH(char*, strncpy, (char *, const char*, __SIZE_TYPE__))
  289. __BOTH(int, strcmp, (const char*, const char*))
  290. __BOTH(int, strncmp, (const char*, const char*, __SIZE_TYPE__))
  291. __BOTH(char*, strcat, (char*, const char*))
  292. __BOTH(char*, strncat, (char*, const char*, __SIZE_TYPE__))
  293. __BOTH(char*, strchr, (const char*, int))
  294. __BOTH(char*, strrchr, (const char*, int))
  295. __BOTH(char*, strdup, (const char*))
  296. #if defined __ARM_EABI__
  297. __BOUND(void*,__aeabi_memcpy,(void*,const void*,__SIZE_TYPE__))
  298. __BOUND(void*,__aeabi_memmove,(void*,const void*,__SIZE_TYPE__))
  299. __BOUND(void*,__aeabi_memmove4,(void*,const void*,__SIZE_TYPE__))
  300. __BOUND(void*,__aeabi_memmove8,(void*,const void*,__SIZE_TYPE__))
  301. __BOUND(void*,__aeabi_memset,(void*,int,__SIZE_TYPE__))
  302. #endif
  303. #if defined __linux__ || defined __APPLE__ // HAVE MALLOC_REDIR
  304. #define __MAYBE_REDIR __BUILTIN
  305. #else
  306. #define __MAYBE_REDIR __BOTH
  307. #endif
  308. __MAYBE_REDIR(void*, malloc, (__SIZE_TYPE__))
  309. __MAYBE_REDIR(void*, realloc, (void *, __SIZE_TYPE__))
  310. __MAYBE_REDIR(void*, calloc, (__SIZE_TYPE__, __SIZE_TYPE__))
  311. __MAYBE_REDIR(void*, memalign, (__SIZE_TYPE__, __SIZE_TYPE__))
  312. __MAYBE_REDIR(void, free, (void*))
  313. __BOTH(void*, alloca, (__SIZE_TYPE__))
  314. void *alloca(__SIZE_TYPE__);
  315. __BUILTIN(void, abort, (void))
  316. __BOUND(void, longjmp, ())
  317. #if !defined _WIN32
  318. __BOUND(void*, mmap, ())
  319. __BOUND(int, munmap, ())
  320. #endif
  321. #undef __BUILTINBC
  322. #undef __BUILTIN
  323. #undef __BOUND
  324. #undef __BOTH
  325. #undef __MAYBE_REDIR
  326. #undef __RENAME
  327. #define __BUILTIN_EXTERN(name,u) \
  328. int __builtin_##name(u int); \
  329. int __builtin_##name##l(u long); \
  330. int __builtin_##name##ll(u long long);
  331. __BUILTIN_EXTERN(ffs,)
  332. __BUILTIN_EXTERN(clz, unsigned)
  333. __BUILTIN_EXTERN(ctz, unsigned)
  334. __BUILTIN_EXTERN(clrsb,)
  335. __BUILTIN_EXTERN(popcount, unsigned)
  336. __BUILTIN_EXTERN(parity, unsigned)
  337. #undef __BUILTIN_EXTERN
  338. #endif /* ndef __TCC_PP__ */