A uint64 bug fix on x86-64
[tinycc/kirr.git] / stdarg.h
blob899358ce68939e77f25e4f527d12e8ddf1f534ec
1 #ifndef _STDARG_H
2 #define _STDARG_H
4 #ifdef __x86_64__
6 #ifdef __TINYC__
8 #include <stdlib.h>
10 /* GCC compatible definition of va_list. */
11 struct __va_list_struct {
12 unsigned int gp_offset;
13 unsigned int fp_offset;
14 union {
15 unsigned int overflow_offset;
16 char *overflow_arg_area;
18 char *reg_save_area;
21 typedef struct __va_list_struct *va_list;
23 /* avoid #define malloc tcc_malloc.
24 XXX: add __malloc or something into libtcc? */
25 inline void *__va_list_malloc(size_t size) { return malloc(size); }
26 inline void __va_list_free(void *ptr) { free(ptr); }
28 /* XXX: this lacks the support of aggregated types. */
29 #define va_start(ap, last) \
30 (ap = (va_list)__va_list_malloc(sizeof(struct __va_list_struct)), \
31 *ap = *(struct __va_list_struct*)( \
32 (char*)__builtin_frame_address(0) - 16), \
33 ap->overflow_arg_area = ((char *)__builtin_frame_address(0) + \
34 ap->overflow_offset), \
35 ap->reg_save_area = (char *)__builtin_frame_address(0) - 176 - 16 \
37 #define va_arg(ap, type) \
38 (*(type*)(__builtin_types_compatible_p(type, long double) \
39 ? (ap->overflow_arg_area += 16, \
40 ap->overflow_arg_area - 16) \
41 : __builtin_types_compatible_p(type, double) \
42 ? (ap->fp_offset < 128 + 48 \
43 ? (ap->fp_offset += 16, \
44 ap->reg_save_area + ap->fp_offset - 16) \
45 : (ap->overflow_arg_area += 8, \
46 ap->overflow_arg_area - 8)) \
47 : (ap->gp_offset < 48 \
48 ? (ap->gp_offset += 8, \
49 ap->reg_save_area + ap->gp_offset - 8) \
50 : (ap->overflow_arg_area += 8, \
51 ap->overflow_arg_area - 8)) \
53 #define va_copy(dest, src) \
54 ((dest) = (va_list)malloc(sizeof(struct __va_list_struct)), \
55 *(dest) = *(src))
56 #define va_end(ap) __va_list_free(ap)
58 #else
60 /* for GNU C */
62 typedef __builtin_va_list va_list;
64 #define va_start(ap, last) __builtin_va_start(ap, last)
65 #define va_arg(ap, type) __builtin_va_arg(ap, type)
66 #define va_copy(dest, src) __builtin_va_copy(dest, src)
67 #define va_end(ap) __builtin_va_end(ap)
69 #endif
71 #else
73 typedef char *va_list;
75 /* only correct for i386 */
76 #define va_start(ap,last) ap = ((char *)&(last)) + ((sizeof(last)+3)&~3)
77 #define va_arg(ap,type) (ap += (sizeof(type)+3)&~3, *(type *)(ap - ((sizeof(type)+3)&~3)))
78 #define va_copy(dest, src) (dest) = (src)
79 #define va_end(ap)
81 #endif
83 /* fix a buggy dependency on GCC in libio.h */
84 typedef va_list __gnuc_va_list;
85 #define _VA_LIST_DEFINED
87 #endif /* _STDARG_H */