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