1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 2007-2020 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34 /*
35 * compiler.h
36 *
37 * Compiler-specific macros for NASM. Feel free to add support for
38 * other compilers in here.
39 *
40 * This header file should be included before any other header.
41 */
42
43 #ifndef NASM_COMPILER_H
44 #define NASM_COMPILER_H 1
45
46 /*
47 * At least DJGPP and Cygwin have broken header files if __STRICT_ANSI__
48 * is defined.
49 */
50 #ifdef __GNUC__
51 # undef __STRICT_ANSI__
52 #endif
53
54 /* On Microsoft platforms we support multibyte character sets in filenames */
55 #define _MBCS 1
56
57 #ifdef HAVE_CONFIG_H
58 # include "config/config.h"
59 #else
60 # if defined(_MSC_VER) && (_MSC_VER >= 1310)
61 # include "config/msvc.h"
62 # elif defined(__WATCOMC__)
63 # include "config/watcom.h"
64 # else
65 # include "config/unknown.h"
66 # endif
67 /* This unconditionally defines some macros we really want */
68 # include "config/unconfig.h"
69 #endif /* Configuration file */
70
71 /* This is required to get the standard <inttypes.h> macros when compiling
72 with a C++ compiler. This must be defined *before* <inttypes.h> is
73 included, directly or indirectly. */
74 #define __STDC_CONSTANT_MACROS 1
75 #define __STDC_LIMIT_MACROS 1
76 #define __STDC_FORMAT_MACROS 1
77
78 #ifdef HAVE_INTTYPES_H
79 # include <inttypes.h>
80 #else
81 # include "nasmint.h"
82 #endif
83
84 #include <assert.h>
85 #include <stddef.h>
86 #include <stdarg.h>
87 #include <stdio.h>
88 #include <stdlib.h>
89 #include <string.h>
90 #include <limits.h>
91 #include <errno.h>
92
93 #ifdef HAVE_STRINGS_H
94 # include <strings.h>
95 #endif
96 #ifdef HAVE_SYS_TYPES_H
97 # include <sys/types.h>
98 #endif
99
100 #ifdef HAVE_ENDIAN_H
101 # include <endian.h>
102 #elif defined(HAVE_SYS_ENDIAN_H)
103 # include <sys/endian.h>
104 #elif defined(HAVE_MACHINE_ENDIAN_H)
105 # include <machine/endian.h>
106 #endif
107
108 /*
109 * If we have BYTE_ORDER defined, or the compiler provides
110 * __BIG_ENDIAN__ or __LITTLE_ENDIAN__, trust it over what autoconf
111 * came up with, especially since autoconf obviously can't figure
112 * things out for a universal compiler.
113 */
114 #if defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__)
115 # undef WORDS_LITTLEENDIAN
116 # undef WORDS_BIGENDIAN
117 # define WORDS_BIGENDIAN 1
118 #elif defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
119 # undef WORDS_LITTLEENDIAN
120 # undef WORDS_BIGENDIAN
121 # define WORDS_LITTLEENDIAN 1
122 #elif defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)
123 # undef WORDS_LITTLEENDIAN
124 # undef WORDS_BIGENDIAN
125 # if BYTE_ORDER == LITTLE_ENDIAN
126 # define WORDS_LITTLEENDIAN 1
127 # elif BYTE_ORDER == BIG_ENDIAN
128 # define WORDS_BIGENDIAN 1
129 # endif
130 #endif
131
132 /*
133 * Define this to 1 for faster performance if this is a littleendian
134 * platform *and* it can do arbitrary unaligned memory references. It
135 * is safe to leave it defined to 0 even if that is true.
136 */
137 #if defined(__386__) || defined(__i386__) || defined(__x86_64__) \
138 || defined(_M_IX86) || defined(_M_X64)
139 # define X86_MEMORY 1
140 # undef WORDS_BIGENDIAN
141 # undef WORDS_LITTLEENDIAN
142 # define WORDS_LITTLEENDIAN 1
143 #else
144 # define X86_MEMORY 0
145 #endif
146
147 /* Some versions of MSVC have these only with underscores in front */
148 #ifndef HAVE_SNPRINTF
149 # ifdef HAVE__SNPRINTF
150 # define snprintf _snprintf
151 # else
152 int snprintf(char *, size_t, const char *, ...);
153 # endif
154 #endif
155
156 #ifndef HAVE_VSNPRINTF
157 # ifdef HAVE__VSNPRINTF
158 # define vsnprintf _vsnprintf
159 # else
160 int vsnprintf(char *, size_t, const char *, va_list);
161 # endif
162 #endif
163
164 #if !defined(HAVE_STRLCPY) || !HAVE_DECL_STRLCPY
165 size_t strlcpy(char *, const char *, size_t);
166 #endif
167
168 #if !defined(HAVE_STRCHRNUL) || !HAVE_DECL_STRCHRNUL
169 char *strrchrnul(const char *, int);
170 #endif
171
172 #ifndef __cplusplus /* C++ has false, true, bool as keywords */
173 # ifdef HAVE_STDBOOL_H
174 # include <stdbool.h>
175 # elif defined(HAVE__BOOL)
176 typedef _Bool bool;
177 # define false 0
178 # define true 1
179 # else
180 /* This is sort of dangerous, since casts will behave different than
181 casting to the standard boolean type. Always use !!, not (bool). */
182 typedef enum bool { false, true } bool;
183 # endif
184 #endif
185
186 /* Create a NULL pointer of the same type as the address of
187 the argument, without actually evaluating said argument. */
188 #define nullas(p) (0 ? &(p) : NULL)
189
190 /* Convert an offsetted NULL pointer dereference to a size_t offset.
191 Technically non-portable as taking the offset from a NULL pointer
192 is undefined behavior, but... */
193 #define null_offset(p) ((size_t)((const char *)&(p) - (const char *)NULL))
194
195 /* Provide a substitute for offsetof() if we don't have one. This
196 variant works on most (but not *all*) systems... */
197 #ifndef offsetof
198 # define offsetof(t,m) null_offset(((t *)NULL)->m)
199 #endif
200
201 /* If typeof is defined as a macro, assume we have typeof even if
202 HAVE_TYPEOF is not declared (e.g. due to not using autoconf.) */
203 #ifdef typeof
204 # define HAVE_TYPEOF 1
205 #endif
206
207 /* This is like offsetof(), but takes an object rather than a type. */
208 #ifndef offsetin
209 # ifdef HAVE_TYPEOF
210 # define offsetin(p,m) offsetof(typeof(p),m)
211 # else
212 # define offsetin(p,m) null_offset(nullas(p)->m)
213 # endif
214 #endif
215
216 /* The container_of construct: if p is a pointer to member m of
217 container class c, then return a pointer to the container of which
218 *p is a member. */
219 #ifndef container_of
220 # define container_of(p, c, m) ((c *)((char *)(p) - offsetof(c,m)))
221 #endif
222
223 /* Some misguided platforms hide the defs for these */
224 #if defined(HAVE_STRCASECMP) && !HAVE_DECL_STRCASECMP
225 int strcasecmp(const char *, const char *);
226 #endif
227
228 #if defined(HAVE_STRICMP) && !HAVE_DECL_STRICMP
229 int stricmp(const char *, const char *);
230 #endif
231
232 #if defined(HAVE_STRNCASECMP) && !HAVE_DECL_STRNCASECMP
233 int strncasecmp(const char *, const char *, size_t);
234 #endif
235
236 #if defined(HAVE_STRNICMP) && !HAVE_DECL_STRNICMP
237 int strnicmp(const char *, const char *, size_t);
238 #endif
239
240 #if defined(HAVE_STRSEP) && !HAVE_DECL_STRSEP
241 char *strsep(char **, const char *);
242 #endif
243
244 #if !HAVE_DECL_STRNLEN
245 size_t strnlen(const char *s, size_t maxlen);
246 #endif
247
248 #ifndef HAVE_MEMPCPY
mempcpy(void * dst,const void * src,size_t n)249 static inline void *mempcpy(void *dst, const void *src, size_t n)
250 {
251 return (char *)memcpy(dst, src, n) + n;
252 }
253 #endif
254
255 /*
256 * Hack to support external-linkage inline functions
257 */
258 #ifndef HAVE_STDC_INLINE
259 # ifdef __GNUC__
260 # ifdef __GNUC_STDC_INLINE__
261 # define HAVE_STDC_INLINE
262 # else
263 # define HAVE_GNU_INLINE
264 # endif
265 # elif defined(__GNUC_GNU_INLINE__)
266 /* Some other compiler implementing only GNU inline semantics? */
267 # define HAVE_GNU_INLINE
268 # elif defined(__STDC_VERSION__)
269 # if __STDC_VERSION__ >= 199901L
270 # define HAVE_STDC_INLINE
271 # endif
272 # endif
273 #endif
274
275 #ifdef HAVE_STDC_INLINE
276 # define extern_inline inline
277 #elif defined(HAVE_GNU_INLINE)
278 # define extern_inline extern inline
279 # define inline_prototypes
280 #else
281 # define inline_prototypes
282 #endif
283
284 /*
285 * Hints to the compiler that a particular branch of code is more or
286 * less likely to be taken.
287 */
288 #if HAVE___BUILTIN_EXPECT
289 # define likely(x) __builtin_expect(!!(x), 1)
290 # define unlikely(x) __builtin_expect(!!(x), 0)
291 #else
292 # define likely(x) (!!(x))
293 # define unlikely(x) (!!(x))
294 #endif
295
296 #define safe_alloc never_null malloc_func
297 #define safe_alloc_ptr never_null_ptr malloc_func_ptr
298
299 #define safe_malloc(s) safe_alloc alloc_size_func1(s)
300 #define safe_malloc2(s1,s2) safe_alloc alloc_size_func2(s1,s2)
301 #define safe_realloc(s) never_null alloc_size_func1(s)
302 #define safe_malloc_ptr(s) safe_alloc_ptr alloc_size_func1_ptr(s)
303 #define safe_malloc2_ptr(s1,s2) safe_alloc_ptr alloc_size_func2_ptr(s1,s2)
304 #define safe_realloc_ptr(s) never_null_ptr alloc_size_func1_ptr(s)
305
306 /*
307 * How to tell the compiler that a function doesn't return
308 */
309 #ifdef HAVE_STDNORETURN_H
310 # include <stdnoreturn.h>
311 # define no_return noreturn void
312 #elif defined(_MSC_VER)
313 # define no_return __declspec(noreturn) void
314 #else
315 # define no_return void noreturn_func
316 #endif
317
318 /*
319 * A fatal function is both unlikely and no_return
320 */
321 #define fatal_func no_return unlikely_func
322 #define fatal_func_ptr no_return unlikely_func_ptr
323
324 /*
325 * How to tell the compiler that a function takes a printf-like string
326 */
327 #define printf_func(fmt, list) format_func3(printf,fmt,list)
328 #define printf_func_ptr(fmt, list) format_func3_ptr(printf,fmt,list)
329 #define vprintf_func(fmt) format_func3(printf,fmt,0)
330 #define vprintf_func_ptr(fmt) format_func3_ptr(printf,fmt,0)
331
332 /* Determine probabilistically if something is a compile-time constant */
333 #ifdef HAVE___BUILTIN_CONSTANT_P
334 # if defined(__GNUC__) && (__GNUC__ >= 5)
335 # define is_constant(x) __builtin_constant_p((x))
336 # else
337 # define is_constant(x) false
338 # endif
339 #else
340 # define is_constant(x) false
341 #endif
342
343 /*
344 * If we can guarantee that a particular expression is constant, use it,
345 * otherwise use a different version.
346 */
347 #if defined(__GNUC__) && (__GNUC__ >= 3)
348 # define not_pedantic_start \
349 _Pragma("GCC diagnostic push") \
350 _Pragma("GCC diagnostic ignored \"-Wpedantic\"")
351 # define not_pedantic_end \
352 _Pragma("GCC diagnostic pop")
353 #else
354 # define not_pedantic_start
355 # define not_pedantic_end
356 #endif
357
358 #ifdef HAVE___BUILTIN_CHOOSE_EXPR
359 # define if_constant(x,y) __builtin_choose_expr(is_constant(x),(x),(y))
360 #else
361 # define if_constant(x,y) (y)
362 #endif
363
364 /*
365 * The autoconf documentation states:
366 *
367 * `va_copy'
368 * The C99 standard provides `va_copy' for copying `va_list'
369 * variables. It may be available in older environments too, though
370 * possibly as `__va_copy' (e.g., `gcc' in strict pre-C99 mode).
371 * These can be tested with `#ifdef'. A fallback to `memcpy (&dst,
372 * &src, sizeof (va_list))' gives maximum portability.
373 */
374 #ifndef va_copy
375 # ifdef __va_copy
376 # define va_copy(dst,src) __va_copy(dst,src)
377 # else
378 # define va_copy(dst,src) memcpy(&(dst),&(src),sizeof(va_list))
379 # endif
380 #endif
381
382 /*
383 * If SIZE_MAX is not defined, rely on size_t being unsigned
384 */
385 #ifndef SIZE_MAX
386 # define SIZE_MAX (((size_t)0) - 1)
387 #endif
388
389 /* Watcom doesn't handle switch statements with 64-bit types, hack around it */
390 #ifdef __WATCOMC__
391 # define BOGUS_CASE 0x76543210
392
watcom_switch_hack(uint64_t x)393 static inline unsigned int watcom_switch_hack(uint64_t x)
394 {
395 if (x > (uint64_t)UINT_MAX)
396 return BOGUS_CASE;
397 else
398 return (unsigned int)x;
399 }
400
401 # define switch(x) switch(sizeof(x) > sizeof(unsigned int) \
402 ? watcom_switch_hack(x) : (unsigned int)(x))
403
404 /* This is to make sure BOGUS_CASE doesn't conflict with anything real... */
405 # define default case BOGUS_CASE: default
406 #endif
407
408 #endif /* NASM_COMPILER_H */
409