xref: /freebsd/sys/powerpc/include/stdarg.h (revision 224af215a6fe8d5e5e2c91cc97c48bdd67c991c7)
1 /*-
2  * Copyright (c) 2002 David E. O'Brien.  All rights reserved.
3  * Copyright (c) 2000 Tsubai Masanari.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  *	$NetBSD: stdarg.h,v 1.5 2000/02/27 17:50:21 tsubai Exp $
28  * $FreeBSD$
29  */
30 
31 #ifndef _POWERPC_STDARG_H_
32 #define	_POWERPC_STDARG_H_
33 
34 #include <sys/_types.h>
35 
36 typedef	__va_list	va_list;
37 
38 #if defined(__GNUC__) && (__GNUC__ == 2 && __GNUC_MINOR__ > 95 || __GNUC__ >= 3)
39 
40 #define	va_start(ap, last) \
41 	__builtin_stdarg_start((ap), (last))
42 
43 #define	va_arg(ap, type) \
44 	__builtin_va_arg((ap), type)
45 
46 #define	va_copy(dest, src) \
47 	__builtin_va_copy((dest), (src))
48 
49 #define	va_end(ap) \
50 	__builtin_va_end(ap)
51 
52 #else	/* ! __GNUC__ post GCC 2.95 */
53 
54 #ifdef __lint__
55 
56 #define	va_start(ap, last)	((ap) = *(va_list *)0)
57 #define	va_arg(ap, type)	(*(type *)(void *)&(ap))
58 
59 #else
60 
61 #if defined(__GNUC__) && (__GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ >= 95)
62 #define	va_start(ap, last)						\
63 	(__builtin_next_arg(last),					\
64 	 __builtin_memcpy((void *)&(ap), __builtin_saveregs (),		\
65 	 sizeof(__gnuc_va_list)))
66 #else
67 #define	va_start(ap, last)						\
68 	(__builtin_next_arg(last),					\
69 	 (ap).__stack = __va_stack_args,				\
70 	 (ap).__base = __va_reg_args,					\
71 	 (ap).__gpr = __va_first_gpr,					\
72 	 (ap).__fpr = __va_first_fpr)
73 #endif
74 
75 #define	__va_first_gpr	(__builtin_args_info(0))
76 #define	__va_first_fpr	(__builtin_args_info(1) - 32 - 1)
77 #define	__va_stack_args							\
78 	((char *)__builtin_saveregs() +					\
79 	 (__va_first_gpr >= 8 ? __va_first_gpr - 8 : 0) * sizeof(int))
80 #define	__va_reg_args							\
81 	((char *)__builtin_frame_address(0) + __builtin_args_info(4))
82 
83 #define	__INTEGER_TYPE_CLASS	1
84 #define	__REAL_TYPE_CLASS	8
85 #define	__RECORD_TYPE_CLASS	12
86 
87 #define	__va_longlong(type)						\
88 	(__builtin_classify_type(*(type *)0) == __INTEGER_TYPE_CLASS &&	\
89 	 sizeof(type) == 8)
90 
91 #define	__va_double(type)						\
92 	(__builtin_classify_type(*(type *)0) == __REAL_TYPE_CLASS)
93 
94 #define	__va_struct(type)						\
95 	(__builtin_classify_type(*(type *)0) >= __RECORD_TYPE_CLASS)
96 
97 #define	__va_size(type)							\
98 	((sizeof(type) + sizeof(int) - 1) / sizeof(int) * sizeof(int))
99 
100 #define	__va_savedgpr(ap, type)						\
101 	((ap).__base + (ap).__gpr * sizeof(int) - sizeof(type))
102 
103 #define	__va_savedfpr(ap, type)						\
104 	((ap).__base + 8 * sizeof(int) + (ap).__fpr * sizeof(double) -	\
105 	 sizeof(type))
106 
107 #define	__va_stack(ap, type)						\
108 	((ap).__stack += __va_size(type) +				\
109 			(__va_longlong(type) ? (int)(ap).__stack & 4 : 0), \
110 	 (ap).__stack - sizeof(type))
111 
112 #define	__va_gpr(ap, type)						\
113 	((ap).__gpr += __va_size(type) / sizeof(int) +			\
114 		      (__va_longlong(type) ? (ap).__gpr & 1 : 0),	\
115 	 (ap).__gpr <= 8 ? __va_savedgpr(ap, type) : __va_stack(ap, type))
116 
117 #define	__va_fpr(ap, type)						\
118 	((ap).__fpr++,							\
119 	 (ap).__fpr <= 8 ? __va_savedfpr(ap, type) : __va_stack(ap, type))
120 
121 #define	va_arg(ap, type)						\
122 	(*(type *)(__va_struct(type) ? (*(void **)__va_gpr(ap, void *)) : \
123 		   __va_double(type) ? __va_fpr(ap, type) :		\
124 		   __va_gpr(ap, type)))
125 
126 #endif /* __lint__ */
127 
128 #define	va_end(ap)
129 
130 #if !defined(_ANSI_SOURCE) &&						\
131     (!defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE) ||		\
132      defined(_ISOC99_SOURCE) || (__STDC_VERSION__ - 0) >= 199901L)
133 #define	va_copy(dest, src)						\
134 	((dest) = (src))
135 #endif
136 
137 #endif /* __GNUC__ post GCC 2.95 */
138 
139 #endif /* _POWERPC_STDARG_H_ */
140