1 /*
2  * Copyright 2017-2019 Bruno Haible <bruno@clisp.org>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16  */
17 
18 /* Define some canonical CPU and ABI indicators.
19    References:
20      - host-cpu-c-abi.m4 from gnulib
21      - https://sourceforge.net/p/predef/wiki/Architectures/
22      - GCC source code: definitions of macro TARGET_CPU_CPP_BUILTINS
23      - clang source code: defineMacro invocations in Basic/Targets.cpp,
24        especially in getTargetDefines methods.
25    Limitation: Unlike host-cpu-c-abi.m4, this preprocessor-based approach
26    can not reliably distinguish __arm__ and __armhf__.
27  */
28 
29 #ifndef __i386__
30 #if defined(__i386__) /* GCC, clang */ || defined(__i386) /* Sun C */ || defined(_M_IX86) /* MSVC */
31 #define __i386__ 1
32 #endif
33 #endif
34 
35 #ifndef __m68k__
36 #if defined(__m68k__) /* GCC */
37 #define __m68k__ 1
38 #endif
39 #endif
40 
41 /* On mips, there are three ABIs:
42    - 32 or o32: It defines _MIPS_SIM == _ABIO32 and _MIPS_SZLONG == 32.
43    - n32: It defines _MIPS_SIM == _ABIN32 and _MIPS_SZLONG == 32.
44    - 64: It defines _MIPS_SZLONG == 64.
45  */
46 /* Note: When __mipsn32__ or __mips64__ is defined, __mips__ may or may not be
47    defined as well. To test for the MIPS o32 ABI, use
48      #if defined(__mips__) && !defined(__mipsn32__) && !defined(__mips64__)
49  */
50 /* To distinguish little-endian and big-endian arm, use the preprocessor
51    defines _MIPSEB vs. _MIPSEL. */
52 #ifndef __mips__
53 #if defined(__mips) /* GCC, clang, IRIX cc */ /* Note: GCC, clang also define __mips__. */
54 #define __mips__ 1
55 #endif
56 #endif
57 #ifndef __mipsn32__
58 #if defined(__mips__) && (_MIPS_SIM == _ABIN32)
59 #define __mipsn32__ 1
60 #endif
61 #endif
62 #ifndef __mips64__
63 #if defined(__mips__) && defined(_MIPS_SZLONG) && (_MIPS_SZLONG == 64)
64 #define __mips64__ 1
65 #endif
66 #endif
67 
68 /* Note: When __sparc64__ is defined, __sparc__ may or may not be defined as
69    well. To test for the SPARC 32-bit ABI, use
70      #if defined(__sparc__) && !defined(__sparc64__)
71  */
72 #ifndef __sparc__
73 #if defined(__sparc) /* GCC, clang, Sun C */ /* Note: GCC, clang also define __sparc__. */
74 #define __sparc__ 1
75 #endif
76 #endif
77 #ifndef __sparc64__
78 #if defined(__sparcv9) /* GCC/Solaris, Sun C */ || defined(__arch64__) /* GCC/Linux */
79 #define __sparc64__ 1
80 #endif
81 #endif
82 
83 #ifndef __alpha__
84 #if defined(__alpha) /* GCC, DEC C */ /* Note: GCC also defines __alpha__. */
85 #define __alpha__ 1
86 #endif
87 #endif
88 
89 /* On hppa, the C compiler may be generating 32-bit code or 64-bit code.
90    In the latter case, it defines _LP64 and __LP64__.
91  */
92 /* Note: When __hppa64__ is defined, __hppa__ may or may not be defined as well.
93    To test for the HP-PA 32-bit ABI, use
94      #if defined(__hppa__) && !defined(__hppa64__)
95  */
96 #ifndef __hppa__
97 #if defined(__hppa) /* GCC, HP C */ /* Note: GCC also defines __hppa__. */
98 #define __hppa__ 1
99 #endif
100 #endif
101 #ifndef __hppa64__
102 #if defined(__hppa__) && defined(__LP64__)
103 #define __hppa64__ 1
104 #endif
105 #endif
106 
107 /* Distinguish arm which passes floating-point arguments and return values
108    in integer registers (r0, r1, ...) - this is gcc -mfloat-abi=soft or
109    gcc -mfloat-abi=softfp - from arm which passes them in float registers
110    (s0, s1, ...) and double registers (d0, d1, ...) - this is
111    gcc -mfloat-abi=hard. GCC 4.6 or newer sets the preprocessor defines
112    __ARM_PCS (for the first case) and __ARM_PCS_VFP (for the second case),
113    but older GCC does not. */
114 /* Note: When __armhf__ is defined, __arm__ may or may not be defined as well.
115    To test for the ARM ABI that does not use floating-point registers for
116    parameter passing, use
117      #if defined(__arm__) && !defined(__armhf__)
118  */
119 /* To distinguish little-endian and big-endian arm, use the preprocessor
120    defines __ARMEL__ vs. __ARMEB__. */
121 #ifndef __arm__
122 #if defined(__arm__) /* GCC, clang */ || defined(_M_ARM) /* MSVC */
123 #define __arm__ 1
124 #endif
125 #endif
126 #ifndef __armhf__
127 #if defined(__arm__) && defined(__ARM_PCS_VFP) /* GCC */
128 #define __armhf__ 1
129 #endif
130 #endif
131 
132 /* On arm64 systems, the C compiler may be generating code in one of these ABIs:
133    - aarch64 instruction set, 64-bit pointers, 64-bit 'long': arm64.
134    - aarch64 instruction set, 32-bit pointers, 32-bit 'long': arm64-ilp32.
135    - 32-bit instruction set, 32-bit pointers, 32-bit 'long': arm or armhf
136      (see above).
137  */
138 /* Note: When __arm64_ilp32__ is defined, __arm64__ may or may not be defined as
139    well. To test for the arm64 64-bit ABI, use
140      #if defined(__arm64__) && !defined(__arm64_ilp32__)
141  */
142 /* To distinguish little-endian and big-endian arm64, use the preprocessor
143    defines __AARCH64EL__ vs. __AARCH64EB__. */
144 #ifndef __arm64__
145 #if defined(__aarch64__) /* GCC, clang */ || defined(_M_ARM64) /* MSVC */
146 #define __arm64__ 1
147 #endif
148 #endif
149 #ifndef __arm64_ilp32__
150 #if defined(__arm64__) && (defined(__ILP32__) || defined (_ILP32))
151 #define __arm64_ilp32__ 1
152 #endif
153 #endif
154 
155 /* On powerpc and powerpc64, different ABIs are in use on AIX vs. Mac OS X vs.
156    Linux,*BSD. To distinguish them, use the OS dependent defines
157      #if defined(_AIX)
158      #if (defined(__MACH__) && defined(__APPLE__))
159      #if !(defined(_AIX) || (defined(__MACH__) && defined(__APPLE__)))
160  */
161 /* On powerpc64, there are two ABIs on Linux: The AIX compatible one and the
162    ELFv2 one. The latter defines _CALL_ELF=2.
163  */
164 /* Note: When __powerpc64__ is defined, __powerpc__ may or may not be defined as
165    well. To test for the SPARC 32-bit ABI, use
166      #if defined(__powerpc__) && !defined(__powerpc64__)
167    Note: When __powerpc64_elfv2__ is defined, __powerpc64__ may or may not be
168    defined as well. To test for the SPARC 32-bit ABI, use
169      #if defined(__powerpc64__) && !defined(__powerpc64_elfv2__)
170  */
171 #ifndef __powerpc__
172 #if defined(_ARCH_PPC) /* GCC, XLC */ /* Note: On AIX, Linux also __powerpc__ is defined; whereas on Mac OS X also __ppc__ is defined. On AIX also _IBMR2 is defined. */
173 #define __powerpc__ 1
174 #endif
175 #endif
176 #ifndef __powerpc64__
177 #if defined(_ARCH_PPC64) /* GCC, XLC */ /* Note: On Linux, also __powerpc64__ is defined. */
178 #define __powerpc64__ 1
179 #endif
180 #endif
181 #ifndef __powerpc64_elfv2__
182 #if defined(__powerpc64__) && defined(_CALL_ELF) && _CALL_ELF == 2
183 #define __powerpc64_elfv2__ 1
184 #endif
185 #endif
186 
187 /* On ia64 on HP-UX, the C compiler may be generating 64-bit code or 32-bit
188    code. In the latter case, it defines _ILP32.
189  */
190 /* Note: When __ia64_ilp32__ is defined, __ia64__ may or may not be defined as
191    well. To test for the ia64 64-bit ABI, use
192      #if defined(__ia64__) && !defined(__ia64_ilp32__)
193  */
194 #ifndef __ia64__
195 #if defined(__ia64__) /* GCC, HP C */ /* Note: GCC, HP C also define __ia64. */
196 #define __ia64__ 1
197 #endif
198 #endif
199 #ifndef __ia64_ilp32__
200 #if defined(__ia64__) && defined(_ILP32)
201 #define __ia64_ilp32__ 1
202 #endif
203 #endif
204 
205 /* On x86_64 systems, the C compiler may be generating code in one of these ABIs:
206    - 64-bit instruction set, 64-bit pointers, 64-bit 'long': x86_64.
207    - 64-bit instruction set, 64-bit pointers, 32-bit 'long': x86_64
208      with native Windows (mingw, MSVC).
209    - 64-bit instruction set, 32-bit pointers, 32-bit 'long': x86_64-x32.
210    - 32-bit instruction set, 32-bit pointers, 32-bit 'long': i386 (see above). */
211 /* Note: When __x86_64_x32__ is defined, __x86_64__ may or may not be defined as
212    well. To test for the x86_64 64-bit ABI, use
213      #if defined(__x86_64__) && !defined(__x86_64_x32__)
214  */
215 #ifndef __x86_64__
216 #if (defined(__x86_64__) || defined(__amd64__)) /* GCC, clang, Sun C */ || (defined(_M_X64) || defined(_M_AMD64)) /* MSVC */
217 #define __x86_64__ 1
218 #endif
219 #endif
220 #ifndef __x86_64_x32__
221 #if defined(__x86_64__) && (defined(__ILP32__) || defined(_ILP32))
222 #define __x86_64_x32__ 1
223 #endif
224 #endif
225 
226 /* Note: When __s390x__ is defined, __s390__ may or may not be defined as well.
227    To test for the S/390 31-bit ABI, use
228      #if defined(__s390__) && !defined(__s390x__)
229  */
230 #ifndef __s390__
231 #if defined(__s390__) /* GCC, clang */
232 #define __s390__ 1
233 #endif
234 #endif
235 #ifndef __s390x__
236 #if defined(__s390x__) /* GCC, clang */
237 #define __s390x__ 1
238 #endif
239 #endif
240 
241 #ifndef __riscv32__
242 #if defined(__riscv) && __riscv_xlen == 32 && !defined(__LP64__) /* GCC */
243 #define __riscv32__ 1
244 #endif
245 #endif
246 
247 #ifndef __riscv64__
248 #if defined(__riscv) && __riscv_xlen == 64 && defined(__LP64__) /* GCC */
249 #define __riscv64__ 1
250 #endif
251 #endif
252