blob: 5e7e8c463e32c7a3819a9346ab3db800b475cb75 [file] [log] [blame]
H. Peter Anvinc1494ac2007-04-13 19:58:42 +00001/* ----------------------------------------------------------------------- *
2 *
H. Peter Anvin9656a582008-10-30 10:52:08 -07003 * Copyright 2007-2008 The NASM Authors - All Rights Reserved
H. Peter Anvinc1494ac2007-04-13 19:58:42 +00004 *
5 * This program is free software; you can redistribute it and/or modify
Beroset095e6a22007-12-29 09:44:23 -05006 * it under the terms of the license given in the file "LICENSE"
H. Peter Anvinc1494ac2007-04-13 19:58:42 +00007 * distributed in the NASM archive.
8 *
9 * ----------------------------------------------------------------------- */
10
11/*
12 * compiler.h
13 *
14 * Compiler-specific macros for NASM. Feel free to add support for
15 * other compilers in here.
H. Peter Anvinfe501952007-10-02 21:53:51 -070016 *
17 * This header file should be included before any other header.
H. Peter Anvinc1494ac2007-04-13 19:58:42 +000018 */
19
H. Peter Anvinfe501952007-10-02 21:53:51 -070020#ifndef NASM_COMPILER_H
21#define NASM_COMPILER_H 1
H. Peter Anvinc40f89e2007-04-13 20:06:41 +000022
23#ifdef HAVE_CONFIG_H
24# include "config.h"
H. Peter Anvin253a4e72008-06-10 13:00:27 -070025/* autoconf doesn't define these if they are redundant, but we want to
26 be able to #ifdef them... */
27#else
28/* Default these to unsupported unless we have config.h */
29# ifndef inline
30# define inline
31# endif
32# ifndef restrict
33# define restrict
34# endif
35#endif /* HAVE_CONFIG_H */
H. Peter Anvinc1494ac2007-04-13 19:58:42 +000036
H. Peter Anvin687b3632007-10-11 12:50:24 -070037/* This is required to get the standard <inttypes.h> macros when compiling
38 with a C++ compiler. This must be defined *before* <inttypes.h> is
39 included, directly or indirectly. */
40#define __STDC_CONSTANT_MACROS 1
41#define __STDC_LIMIT_MACROS 1
42#define __STDC_FORMAT_MACROS 1
43
H. Peter Anvinc1494ac2007-04-13 19:58:42 +000044#ifdef __GNUC__
45# if __GNUC__ >= 4
46# define HAVE_GNUC_4
47# endif
48# if __GNUC__ >= 3
49# define HAVE_GNUC_3
50# endif
51#endif
52
53#ifdef __GNUC__
54# define _unused __attribute__((unused))
55#else
56# define _unused
57#endif
58
H. Peter Anvinb8af9aa2007-09-17 13:53:14 -070059/* Some versions of MSVC have these only with underscores in front */
H. Peter Anvin304b6052007-09-28 10:50:20 -070060#include <stddef.h>
61#include <stdarg.h>
H. Peter Anvinfe501952007-10-02 21:53:51 -070062#include <stdio.h>
H. Peter Anvinb8af9aa2007-09-17 13:53:14 -070063
H. Peter Anvin304b6052007-09-28 10:50:20 -070064#ifndef HAVE_SNPRINTF
65# ifdef HAVE__SNPRINTF
66# define snprintf _snprintf
67# else
68int snprintf(char *, size_t, const char *, ...);
69# endif
H. Peter Anvinb8af9aa2007-09-17 13:53:14 -070070#endif
71
H. Peter Anvin304b6052007-09-28 10:50:20 -070072#ifndef HAVE_VSNPRINTF
73# ifdef HAVE__VSNPRINT
74# define vsnprintf _vsnprintf
75# else
76int vsnprintf(char *, size_t, const char *, va_list);
77# endif
H. Peter Anvinb8af9aa2007-09-17 13:53:14 -070078#endif
79
H. Peter Anvin6867acc2007-10-10 14:58:45 -070080#ifndef __cplusplus /* C++ has false, true, bool as keywords */
81# ifdef HAVE_STDBOOL_H
82# include <stdbool.h>
83# else
H. Peter Anvin253a4e72008-06-10 13:00:27 -070084/* This is sort of dangerous, since casts will behave different than
85 casting to the standard boolean type. Always use !!, not (bool). */
86typedef enum bool { false, true } bool;
H. Peter Anvin6867acc2007-10-10 14:58:45 -070087# endif
88#endif
89
H. Peter Anvin9656a582008-10-30 10:52:08 -070090/* Provide a substitute for offsetof() if we don't have one. This
91 variant works on most (but not *all*) systems... */
92#ifndef offsetof
93# define offsetof(t,m) ((size_t)&(((t *)0)->m))
94#endif
95
96/* The container_of construct: if p is a pointer to member m of
97 container class c, then return a pointer to the container of which
98 *p is a member. */
99#ifndef container_of
100# define container_of(p, c, m) ((c *)((char *)(p) - offsetof(c,m)))
101#endif
102
H. Peter Anvinc13d31a2007-10-26 18:49:29 -0700103/* Some misguided platforms hide the defs for these */
104#if defined(HAVE_STRCASECMP) && !HAVE_DECL_STRCASECMP
105int strcasecmp(const char *, const char *);
106#endif
107
108#if defined(HAVE_STRICMP) && !HAVE_DECL_STRICMP
109int stricmp(const char *, const char *);
110#endif
111
112#if defined(HAVE_STRNCASECMP) && !HAVE_DECL_STRNCASECMP
113int strncasecmp(const char *, const char *, size_t);
114#endif
115
116#if defined(HAVE_STRNICMP) && !HAVE_DECL_STRNICMP
117int strnicmp(const char *, const char *, size_t);
118#endif
119
120#if defined(HAVE_STRSEP) && !HAVE_DECL_STRSEP
121char *strsep(char **, const char *);
122#endif
123
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800124/*
125 * Define this to 1 for faster performance if this is a littleendian
126 * platform which can do unaligned memory references. It is safe
127 * to leave it defined to 0 even if that is true.
128 */
H. Peter Anvin714ad042008-02-16 15:28:02 -0800129#if defined(__386__) || defined(__i386__) || defined(__x86_64__)
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800130# define X86_MEMORY 1
H. Peter Anvin2f0f9ea2008-06-08 20:53:29 -0700131# ifndef WORDS_LITTLEENDIAN
132# define WORDS_LITTLEENDIAN 1
133# endif
H. Peter Anvind1fb15c2007-11-13 09:37:59 -0800134#else
135# define X86_MEMORY 0
136#endif
137
H. Peter Anvin51997d32008-06-10 09:35:26 -0700138/*
139 * Hints to the compiler that a particular branch of code is more or
140 * less likely to be taken.
141 */
142#if defined(__GNUC__) && __GNUC__ >= 3
143# define likely(x) __builtin_expect(!!(x), 1)
144# define unlikely(x) __builtin_expect(!!(x), 0)
145#else
146# define likely(x) (!!(x))
147# define unlikely(x) (!!(x))
148#endif
149
H. Peter Anvinfe501952007-10-02 21:53:51 -0700150#endif /* NASM_COMPILER_H */