2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2002 David E. O'Brien. All rights reserved.
5 * Copyright (c) 2000 Tsubai Masanari. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * $NetBSD: stdarg.h,v 1.5 2000/02/27 17:50:21 tsubai Exp $
33 #ifndef _MACHINE_STDARG_H_
34 #define _MACHINE_STDARG_H_
36 #include <sys/cdefs.h>
37 #include <sys/_types.h>
39 #ifndef _VA_LIST_DECLARED
40 #define _VA_LIST_DECLARED
41 typedef __va_list va_list;
44 #if defined(__GNUCLIKE_BUILTIN_STDARG)
46 #define va_start(ap, last) \
47 __builtin_va_start((ap), (last))
49 #define va_arg(ap, type) \
50 __builtin_va_arg((ap), type)
52 #if __ISO_C_VISIBLE >= 1999
53 #define va_copy(dest, src) \
54 __builtin_va_copy((dest), (src))
56 #define __va_copy(dest, src) \
57 va_copy((dest), (src))
63 #else /* !__GNUCLIKE_BUILTIN_STDARG */
67 #define va_start(ap, last) ((ap) = *(va_list *)0)
68 #define va_arg(ap, type) (*(type *)(void *)&(ap))
72 #if defined(__GNUC_VA_LIST_COMPATIBILITY)
73 #define va_start(ap, last) \
74 (__builtin_next_arg(last), \
75 __builtin_memcpy((void *)&(ap), __builtin_saveregs (), \
76 sizeof(__gnuc_va_list)))
78 #define va_start(ap, last) \
79 (__builtin_next_arg(last), \
80 (ap).__stack = __va_stack_args, \
81 (ap).__base = __va_reg_args, \
82 (ap).__gpr = __va_first_gpr, \
83 (ap).__fpr = __va_first_fpr)
86 #define __va_first_gpr (__builtin_args_info(0))
87 #define __va_first_fpr (__builtin_args_info(1) - 32 - 1)
88 #define __va_stack_args \
89 ((char *)__builtin_saveregs() + \
90 (__va_first_gpr >= 8 ? __va_first_gpr - 8 : 0) * sizeof(int))
91 #define __va_reg_args \
92 ((char *)__builtin_frame_address(0) + __builtin_args_info(4))
94 #define __INTEGER_TYPE_CLASS 1
95 #define __REAL_TYPE_CLASS 8
96 #define __RECORD_TYPE_CLASS 12
98 #define __va_longlong(type) \
99 (__builtin_classify_type(*(type *)0) == __INTEGER_TYPE_CLASS && \
102 #define __va_double(type) \
103 (__builtin_classify_type(*(type *)0) == __REAL_TYPE_CLASS)
105 #define __va_struct(type) \
106 (__builtin_classify_type(*(type *)0) >= __RECORD_TYPE_CLASS)
108 #define __va_size(type) \
109 ((sizeof(type) + sizeof(int) - 1) / sizeof(int) * sizeof(int))
111 #define __va_savedgpr(ap, type) \
112 ((ap).__base + (ap).__gpr * sizeof(int) - sizeof(type))
114 #define __va_savedfpr(ap, type) \
115 ((ap).__base + 8 * sizeof(int) + (ap).__fpr * sizeof(double) - \
118 #define __va_stack(ap, type) \
119 ((ap).__stack += __va_size(type) + \
120 (__va_longlong(type) ? (int)(ap).__stack & 4 : 0), \
121 (ap).__stack - sizeof(type))
123 #define __va_gpr(ap, type) \
124 ((ap).__gpr += __va_size(type) / sizeof(int) + \
125 (__va_longlong(type) ? (ap).__gpr & 1 : 0), \
126 (ap).__gpr <= 8 ? __va_savedgpr(ap, type) : __va_stack(ap, type))
128 #define __va_fpr(ap, type) \
130 (ap).__fpr <= 8 ? __va_savedfpr(ap, type) : __va_stack(ap, type))
132 #define va_arg(ap, type) \
133 (*(type *)(__va_struct(type) ? (*(void **)__va_gpr(ap, void *)) : \
134 __va_double(type) ? __va_fpr(ap, type) : \
137 #endif /* __lint__ */
141 #if __ISO_C_VISIBLE >= 1999
142 #if !defined(_ANSI_SOURCE) && \
143 (!defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE) || \
144 defined(_ISOC99_SOURCE) || (__STDC_VERSION__ - 0) >= 199901L)
145 #define va_copy(dest, src) \
150 #endif /* __GNUCLIKE_BUILTIN_STDARG */
152 #endif /* _MACHINE_STDARG_H_ */