tccelf.c: write section headers before sections
[tinycc.git] / include / tccdefs.h
blob66b8fe8fdd546da28450241e7bd2d85f717bf762
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 #define __has_atttribute(x) 0
156 /* C23 Keywords */
157 #define _Nonnull
158 #define _Nullable
159 #define _Nullable_result
160 #define _Null_unspecified
162 /* skip __builtin... with -E */
163 #ifndef __TCC_PP__
165 #define __builtin_offsetof(type, field) ((__SIZE_TYPE__)&((type*)0)->field)
166 #define __builtin_extract_return_addr(x) x
167 #if !defined __linux__ && !defined _WIN32
168 /* used by math.h */
169 #define __builtin_huge_val() 1e500
170 #define __builtin_huge_valf() 1e50f
171 #define __builtin_huge_vall() 1e5000L
172 # if defined __APPLE__
173 #define __builtin_nanf(ignored_string) (0.0F/0.0F)
174 /* used by floats.h to implement FLT_ROUNDS C99 macro. 1 == to nearest */
175 #define __builtin_flt_rounds() 1
176 /* used by _fd_def.h */
177 #define __builtin_bzero(p, ignored_size) bzero(p, sizeof(*(p)))
178 # else
179 #define __builtin_nanf(ignored_string) (0.0F/0.0F)
180 # endif
181 #endif
183 /* __builtin_va_list */
184 #if defined __x86_64__
185 #if !defined _WIN32
186 /* GCC compatible definition of va_list. */
187 /* This should be in sync with the declaration in our lib/libtcc1.c */
188 typedef struct {
189 unsigned gp_offset, fp_offset;
190 union {
191 unsigned overflow_offset;
192 char *overflow_arg_area;
194 char *reg_save_area;
195 } __builtin_va_list[1];
197 void *__va_arg(__builtin_va_list ap, int arg_type, int size, int align);
198 #define __builtin_va_start(ap, last) \
199 (*(ap) = *(__builtin_va_list)((char*)__builtin_frame_address(0) - 24))
200 #define __builtin_va_arg(ap, t) \
201 (*(t *)(__va_arg(ap, __builtin_va_arg_types(t), sizeof(t), __alignof__(t))))
202 #define __builtin_va_copy(dest, src) (*(dest) = *(src))
204 #else /* _WIN64 */
205 typedef char *__builtin_va_list;
206 #define __builtin_va_arg(ap, t) ((sizeof(t) > 8 || (sizeof(t) & (sizeof(t) - 1))) \
207 ? **(t **)((ap += 8) - 8) : *(t *)((ap += 8) - 8))
208 #endif
210 #elif defined __arm__
211 typedef char *__builtin_va_list;
212 #define _tcc_alignof(type) ((int)&((struct {char c;type x;} *)0)->x)
213 #define _tcc_align(addr,type) (((unsigned)addr + _tcc_alignof(type) - 1) \
214 & ~(_tcc_alignof(type) - 1))
215 #define __builtin_va_start(ap,last) (ap = ((char *)&(last)) + ((sizeof(last)+3)&~3))
216 #define __builtin_va_arg(ap,type) (ap = (void *) ((_tcc_align(ap,type)+sizeof(type)+3) \
217 &~3), *(type *)(ap - ((sizeof(type)+3)&~3)))
219 #elif defined __aarch64__
220 #if defined __APPLE__
221 typedef struct {
222 void *__stack;
223 } __builtin_va_list;
225 #else
226 typedef struct {
227 void *__stack, *__gr_top, *__vr_top;
228 int __gr_offs, __vr_offs;
229 } __builtin_va_list;
231 #endif
232 #elif defined __riscv
233 typedef char *__builtin_va_list;
234 #define __va_reg_size (__riscv_xlen >> 3)
235 #define _tcc_align(addr,type) (((unsigned long)addr + __alignof__(type) - 1) \
236 & -(__alignof__(type)))
237 #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)))))
239 #else /* __i386__ */
240 typedef char *__builtin_va_list;
241 #define __builtin_va_start(ap,last) (ap = ((char *)&(last)) + ((sizeof(last)+3)&~3))
242 #define __builtin_va_arg(ap,t) (*(t*)((ap+=(sizeof(t)+3)&~3)-((sizeof(t)+3)&~3)))
244 #endif
245 #define __builtin_va_end(ap) (void)(ap)
246 #ifndef __builtin_va_copy
247 # define __builtin_va_copy(dest, src) (dest) = (src)
248 #endif
250 /* TCC BBUILTIN AND BOUNDS ALIASES */
251 #ifdef __leading_underscore
252 # define __RENAME(X) __asm__("_"X)
253 #else
254 # define __RENAME(X) __asm__(X)
255 #endif
257 #ifdef __TCC_BCHECK__
258 # define __BUILTINBC(ret,name,params) ret __builtin_##name params __RENAME("__bound_"#name);
259 # define __BOUND(ret,name,params) ret name params __RENAME("__bound_"#name);
260 #else
261 # define __BUILTINBC(ret,name,params) ret __builtin_##name params __RENAME(#name);
262 # define __BOUND(ret,name,params)
263 #endif
264 #ifdef _WIN32
265 #define __BOTH __BOUND
266 #define __BUILTIN(ret,name,params)
267 #else
268 #define __BOTH(ret,name,params) __BUILTINBC(ret,name,params)__BOUND(ret,name,params)
269 #define __BUILTIN(ret,name,params) ret __builtin_##name params __RENAME(#name);
270 #endif
272 __BOTH(void*, memcpy, (void *, const void*, __SIZE_TYPE__))
273 __BOTH(void*, memmove, (void *, const void*, __SIZE_TYPE__))
274 __BOTH(void*, memset, (void *, int, __SIZE_TYPE__))
275 __BOTH(int, memcmp, (const void *, const void*, __SIZE_TYPE__))
276 __BOTH(__SIZE_TYPE__, strlen, (const char *))
277 __BOTH(char*, strcpy, (char *, const char *))
278 __BOTH(char*, strncpy, (char *, const char*, __SIZE_TYPE__))
279 __BOTH(int, strcmp, (const char*, const char*))
280 __BOTH(int, strncmp, (const char*, const char*, __SIZE_TYPE__))
281 __BOTH(char*, strcat, (char*, const char*))
282 __BOTH(char*, strncat, (char*, const char*, __SIZE_TYPE__))
283 __BOTH(char*, strchr, (const char*, int))
284 __BOTH(char*, strrchr, (const char*, int))
285 __BOTH(char*, strdup, (const char*))
286 #if defined __ARM_EABI__
287 __BOUND(void*,__aeabi_memcpy,(void*,const void*,__SIZE_TYPE__))
288 __BOUND(void*,__aeabi_memmove,(void*,const void*,__SIZE_TYPE__))
289 __BOUND(void*,__aeabi_memmove4,(void*,const void*,__SIZE_TYPE__))
290 __BOUND(void*,__aeabi_memmove8,(void*,const void*,__SIZE_TYPE__))
291 __BOUND(void*,__aeabi_memset,(void*,int,__SIZE_TYPE__))
292 #endif
294 #if defined __linux__ || defined __APPLE__ // HAVE MALLOC_REDIR
295 #define __MAYBE_REDIR __BUILTIN
296 #else
297 #define __MAYBE_REDIR __BOTH
298 #endif
299 __MAYBE_REDIR(void*, malloc, (__SIZE_TYPE__))
300 __MAYBE_REDIR(void*, realloc, (void *, __SIZE_TYPE__))
301 __MAYBE_REDIR(void*, calloc, (__SIZE_TYPE__, __SIZE_TYPE__))
302 __MAYBE_REDIR(void*, memalign, (__SIZE_TYPE__, __SIZE_TYPE__))
303 __MAYBE_REDIR(void, free, (void*))
304 #if defined __i386__ || defined __x86_64__
305 __BOTH(void*, alloca, (__SIZE_TYPE__))
306 #else
307 __BUILTIN(void*, alloca, (__SIZE_TYPE__))
308 #endif
309 __BUILTIN(void, abort, (void))
310 __BOUND(void, longjmp, ())
311 #if !defined _WIN32
312 __BOUND(void*, mmap, ())
313 __BOUND(int, munmap, ())
314 #endif
315 #undef __BUILTINBC
316 #undef __BUILTIN
317 #undef __BOUND
318 #undef __BOTH
319 #undef __MAYBE_REDIR
320 #undef __RENAME
322 #define __BUILTIN_EXTERN(name,u) \
323 int __builtin_##name(u int); \
324 int __builtin_##name##l(u long); \
325 int __builtin_##name##ll(u long long);
326 __BUILTIN_EXTERN(ffs,)
327 __BUILTIN_EXTERN(clz, unsigned)
328 __BUILTIN_EXTERN(ctz, unsigned)
329 __BUILTIN_EXTERN(clrsb,)
330 __BUILTIN_EXTERN(popcount, unsigned)
331 __BUILTIN_EXTERN(parity, unsigned)
332 #undef __BUILTIN_EXTERN
334 #endif /* ndef __TCC_PP__ */