xref: /freebsd/sys/powerpc/include/stdarg.h (revision f0cfa1b168014f56c02b83e5f28412cc5f78d117)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2002 David E. O'Brien.  All rights reserved.
5  * Copyright (c) 2000 Tsubai Masanari.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
17  *
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.
28  *
29  *	$NetBSD: stdarg.h,v 1.5 2000/02/27 17:50:21 tsubai Exp $
30  * $FreeBSD$
31  */
32 
33 #ifndef _MACHINE_STDARG_H_
34 #define	_MACHINE_STDARG_H_
35 
36 #include <sys/cdefs.h>
37 #include <sys/_types.h>
38 
39 #ifndef _VA_LIST_DECLARED
40 #define	_VA_LIST_DECLARED
41 typedef	__va_list	va_list;
42 #endif
43 
44 #if defined(__GNUCLIKE_BUILTIN_STDARG)
45 
46 #define	va_start(ap, last) \
47 	__builtin_va_start((ap), (last))
48 
49 #define	va_arg(ap, type) \
50 	__builtin_va_arg((ap), type)
51 
52 #if __ISO_C_VISIBLE >= 1999
53 #define	va_copy(dest, src) \
54 	__builtin_va_copy((dest), (src))
55 
56 #define	__va_copy(dest, src) \
57 	va_copy((dest), (src))
58 #endif
59 
60 #define	va_end(ap) \
61 	__builtin_va_end(ap)
62 
63 #else	/* !__GNUCLIKE_BUILTIN_STDARG */
64 
65 #ifdef __lint__
66 
67 #define	va_start(ap, last)	((ap) = *(va_list *)0)
68 #define	va_arg(ap, type)	(*(type *)(void *)&(ap))
69 
70 #else
71 
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)))
77 #else
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)
84 #endif
85 
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))
93 
94 #define	__INTEGER_TYPE_CLASS	1
95 #define	__REAL_TYPE_CLASS	8
96 #define	__RECORD_TYPE_CLASS	12
97 
98 #define	__va_longlong(type)						\
99 	(__builtin_classify_type(*(type *)0) == __INTEGER_TYPE_CLASS &&	\
100 	 sizeof(type) == 8)
101 
102 #define	__va_double(type)						\
103 	(__builtin_classify_type(*(type *)0) == __REAL_TYPE_CLASS)
104 
105 #define	__va_struct(type)						\
106 	(__builtin_classify_type(*(type *)0) >= __RECORD_TYPE_CLASS)
107 
108 #define	__va_size(type)							\
109 	((sizeof(type) + sizeof(int) - 1) / sizeof(int) * sizeof(int))
110 
111 #define	__va_savedgpr(ap, type)						\
112 	((ap).__base + (ap).__gpr * sizeof(int) - sizeof(type))
113 
114 #define	__va_savedfpr(ap, type)						\
115 	((ap).__base + 8 * sizeof(int) + (ap).__fpr * sizeof(double) -	\
116 	 sizeof(type))
117 
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))
122 
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))
127 
128 #define	__va_fpr(ap, type)						\
129 	((ap).__fpr++,							\
130 	 (ap).__fpr <= 8 ? __va_savedfpr(ap, type) : __va_stack(ap, type))
131 
132 #define	va_arg(ap, type)						\
133 	(*(type *)(__va_struct(type) ? (*(void **)__va_gpr(ap, void *)) : \
134 		   __va_double(type) ? __va_fpr(ap, type) :		\
135 		   __va_gpr(ap, type)))
136 
137 #endif /* __lint__ */
138 
139 #define	va_end(ap)
140 
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)						\
146 	((dest) = (src))
147 #endif
148 #endif
149 
150 #endif /* __GNUCLIKE_BUILTIN_STDARG */
151 
152 #endif /* _MACHINE_STDARG_H_ */
153