tccdefs.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. /* This should be in sync with the declaration in our lib/libtcc1.c */
  172. typedef struct {
  173. unsigned gp_offset, fp_offset;
  174. union {
  175. unsigned overflow_offset;
  176. char *overflow_arg_area;
  177. };
  178. char *reg_save_area;
  179. } __builtin_va_list[1];
  180. void *__va_arg(__builtin_va_list ap, int arg_type, int size, int align);
  181. #define __builtin_va_start(ap, last) \
  182. (*(ap) = *(__builtin_va_list)((char*)__builtin_frame_address(0) - 24))
  183. #define __builtin_va_arg(ap, t) \
  184. (*(t *)(__va_arg(ap, __builtin_va_arg_types(t), sizeof(t), __alignof__(t))))
  185. #define __builtin_va_copy(dest, src) (*(dest) = *(src))
  186. #else /* _WIN64 */
  187. typedef char *__builtin_va_list;
  188. #define __builtin_va_arg(ap, t) ((sizeof(t) > 8 || (sizeof(t) & (sizeof(t) - 1))) \
  189. ? **(t **)((ap += 8) - 8) : *(t *)((ap += 8) - 8))
  190. #endif
  191. #elif defined __arm__
  192. typedef char *__builtin_va_list;
  193. #define _tcc_alignof(type) ((int)&((struct {char c;type x;} *)0)->x)
  194. #define _tcc_align(addr,type) (((unsigned)addr + _tcc_alignof(type) - 1) \
  195. & ~(_tcc_alignof(type) - 1))
  196. #define __builtin_va_start(ap,last) (ap = ((char *)&(last)) + ((sizeof(last)+3)&~3))
  197. #define __builtin_va_arg(ap,type) (ap = (void *) ((_tcc_align(ap,type)+sizeof(type)+3) \
  198. &~3), *(type *)(ap - ((sizeof(type)+3)&~3)))
  199. #elif defined __aarch64__
  200. #if defined __APPLE__
  201. typedef struct {
  202. void *__stack;
  203. } __builtin_va_list;
  204. #else
  205. typedef struct {
  206. void *__stack, *__gr_top, *__vr_top;
  207. int __gr_offs, __vr_offs;
  208. } __builtin_va_list;
  209. #endif
  210. #elif defined __riscv
  211. typedef char *__builtin_va_list;
  212. #define __va_reg_size (__riscv_xlen >> 3)
  213. #define _tcc_align(addr,type) (((unsigned long)addr + __alignof__(type) - 1) \
  214. & -(__alignof__(type)))
  215. #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)))))
  216. #else /* __i386__ */
  217. typedef char *__builtin_va_list;
  218. #define __builtin_va_start(ap,last) (ap = ((char *)&(last)) + ((sizeof(last)+3)&~3))
  219. #define __builtin_va_arg(ap,t) (*(t*)((ap+=(sizeof(t)+3)&~3)-((sizeof(t)+3)&~3)))
  220. #endif
  221. #define __builtin_va_end(ap) (void)(ap)
  222. #ifndef __builtin_va_copy
  223. # define __builtin_va_copy(dest, src) (dest) = (src)
  224. #endif
  225. /* TCC BBUILTIN AND BOUNDS ALIASES */
  226. #ifdef __leading_underscore
  227. # define __RENAME(X) __asm__("_"X)
  228. #else
  229. # define __RENAME(X) __asm__(X)
  230. #endif
  231. #ifdef __TCC_BCHECK__
  232. # define __BUILTINBC(ret,name,params) ret __builtin_##name params __RENAME("__bound_"#name);
  233. # define __BOUND(ret,name,params) ret name params __RENAME("__bound_"#name);
  234. #else
  235. # define __BUILTINBC(ret,name,params) ret __builtin_##name params __RENAME(#name);
  236. # define __BOUND(ret,name,params)
  237. #endif
  238. #ifdef _WIN32
  239. #define __BOTH __BOUND
  240. #define __BUILTIN(ret,name,params)
  241. #else
  242. #define __BOTH(ret,name,params) __BUILTINBC(ret,name,params)__BOUND(ret,name,params)
  243. #define __BUILTIN(ret,name,params) ret __builtin_##name params __RENAME(#name);
  244. #endif
  245. __BOTH(void*, memcpy, (void *, const void*, __SIZE_TYPE__))
  246. __BOTH(void*, memmove, (void *, const void*, __SIZE_TYPE__))
  247. __BOTH(void*, memset, (void *, int, __SIZE_TYPE__))
  248. __BOTH(int, memcmp, (const void *, const void*, __SIZE_TYPE__))
  249. __BOTH(__SIZE_TYPE__, strlen, (const char *))
  250. __BOTH(char*, strcpy, (char *, const char *))
  251. __BOTH(char*, strncpy, (char *, const char*, __SIZE_TYPE__))
  252. __BOTH(int, strcmp, (const char*, const char*))
  253. __BOTH(int, strncmp, (const char*, const char*, __SIZE_TYPE__))
  254. __BOTH(char*, strcat, (char*, const char*))
  255. __BOTH(char*, strncat, (char*, const char*, __SIZE_TYPE__))
  256. __BOTH(char*, strchr, (const char*, int))
  257. __BOTH(char*, strrchr, (const char*, int))
  258. __BOTH(char*, strdup, (const char*))
  259. #if defined __ARM_EABI__
  260. __BOUND(void*,__aeabi_memcpy,(void*,const void*,__SIZE_TYPE__))
  261. __BOUND(void*,__aeabi_memmove,(void*,const void*,__SIZE_TYPE__))
  262. __BOUND(void*,__aeabi_memmove4,(void*,const void*,__SIZE_TYPE__))
  263. __BOUND(void*,__aeabi_memmove8,(void*,const void*,__SIZE_TYPE__))
  264. __BOUND(void*,__aeabi_memset,(void*,int,__SIZE_TYPE__))
  265. #endif
  266. #if defined __linux__ || defined __APPLE__ // HAVE MALLOC_REDIR
  267. #define __MAYBE_REDIR __BUILTIN
  268. #else
  269. #define __MAYBE_REDIR __BOTH
  270. #endif
  271. __MAYBE_REDIR(void*, malloc, (__SIZE_TYPE__))
  272. __MAYBE_REDIR(void*, realloc, (void *, __SIZE_TYPE__))
  273. __MAYBE_REDIR(void*, calloc, (__SIZE_TYPE__, __SIZE_TYPE__))
  274. __MAYBE_REDIR(void*, memalign, (__SIZE_TYPE__, __SIZE_TYPE__))
  275. __MAYBE_REDIR(void, free, (void*))
  276. #if defined __i386__ || defined __x86_64__
  277. __BOTH(void*, alloca, (__SIZE_TYPE__))
  278. #else
  279. __BUILTIN(void*, alloca, (__SIZE_TYPE__))
  280. #endif
  281. __BUILTIN(void, abort, (void))
  282. __BOUND(void, longjmp, ())
  283. #if !defined _WIN32
  284. __BOUND(void*, mmap, ())
  285. __BOUND(int, munmap, ())
  286. #endif
  287. #undef __BUILTINBC
  288. #undef __BUILTIN
  289. #undef __BOUND
  290. #undef __BOTH
  291. #undef __MAYBE_REDIR
  292. #undef __RENAME
  293. #define __BUILTIN_EXTERN(name,u) \
  294. int __builtin_##name(u int); \
  295. int __builtin_##name##l(u long); \
  296. int __builtin_##name##ll(u long long);
  297. __BUILTIN_EXTERN(ffs,)
  298. __BUILTIN_EXTERN(clz, unsigned)
  299. __BUILTIN_EXTERN(ctz, unsigned)
  300. __BUILTIN_EXTERN(clrsb,)
  301. __BUILTIN_EXTERN(popcount, unsigned)
  302. __BUILTIN_EXTERN(parity, unsigned)
  303. #undef __BUILTIN_EXTERN
  304. #endif /* ndef __TCC_PP__ */