Zydis  v2.0.3
Defines.h
Go to the documentation of this file.
1 /***************************************************************************************************
2 
3  Zyan Disassembler Library (Zydis)
4 
5  Original Author : Florian Bernd
6 
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24 
25 ***************************************************************************************************/
26 
32 #ifndef ZYDIS_DEFINES_H
33 #define ZYDIS_DEFINES_H
34 
35 #include <ZydisExportConfig.h>
36 
37 /* ============================================================================================== */
38 /* Compiler detection */
39 /* ============================================================================================== */
40 
41 #if defined(__clang__)
42 # define ZYDIS_CLANG
43 # define ZYDIS_GNUC
44 #elif defined(__ICC) || defined(__INTEL_COMPILER)
45 # define ZYDIS_ICC
46 #elif defined(__GNUC__) || defined(__GNUG__)
47 # define ZYDIS_GCC
48 # define ZYDIS_GNUC
49 #elif defined(_MSC_VER)
50 # define ZYDIS_MSVC
51 #elif defined(__BORLANDC__)
52 # define ZYDIS_BORLAND
53 #else
54 # define ZYDIS_UNKNOWN_COMPILER
55 #endif
56 
57 /* ============================================================================================== */
58 /* Platform detection */
59 /* ============================================================================================== */
60 
61 #if defined(_WIN32)
62 # define ZYDIS_WINDOWS
63 #elif defined(__APPLE__)
64 # define ZYDIS_APPLE
65 # define ZYDIS_POSIX
66 #elif defined(__linux)
67 # define ZYDIS_LINUX
68 # define ZYDIS_POSIX
69 #elif defined(__FreeBSD__)
70 # define ZYDIS_FREEBSD
71 # define ZYDIS_POSIX
72 #elif defined(__unix)
73 # define ZYDIS_UNIX
74 # define ZYDIS_POSIX
75 #elif defined(__posix)
76 # define ZYDIS_POSIX
77 #else
78 # define ZYDIS_UNKNOWN_PLATFORM
79 #endif
80 
81 /* ============================================================================================== */
82 /* Architecture detection */
83 /* ============================================================================================== */
84 
85 #if defined(_M_AMD64) || defined(__x86_64__)
86 # define ZYDIS_X64
87 #elif defined(_M_IX86) || defined(__i386__)
88 # define ZYDIS_X86
89 #elif defined(_M_ARM64) || defined(__aarch64__)
90 # define ZYDIS_AARCH64
91 #elif defined(_M_ARM) || defined(_M_ARMT) || defined(__arm__) || defined(__thumb__)
92 # define ZYDIS_ARM
93 #else
94 # error "Unsupported architecture detected"
95 #endif
96 
97 /* ============================================================================================== */
98 /* Debug/Release detection */
99 /* ============================================================================================== */
100 
101 #if defined(ZYDIS_MSVC) || defined(ZYDIS_BORLAND)
102 # ifdef _DEBUG
103 # define ZYDIS_DEBUG
104 # else
105 # define ZYDIS_RELEASE
106 # endif
107 #elif defined(ZYDIS_GNUC) || defined(ZYDIS_ICC)
108 # ifdef NDEBUG
109 # define ZYDIS_RELEASE
110 # else
111 # define ZYDIS_DEBUG
112 # endif
113 #else
114 # define ZYDIS_RELEASE
115 #endif
116 
117 /* ============================================================================================== */
118 /* Misc compatibility macros */
119 /* ============================================================================================== */
120 
121 #if defined(ZYDIS_MSVC) || defined(ZYDIS_BORLAND)
122 # define ZYDIS_INLINE __inline
123 #else
124 # define ZYDIS_INLINE static inline
125 #endif
126 
127 /* ============================================================================================== */
128 /* Debugging and optimization macros */
129 /* ============================================================================================== */
130 
131 #if defined(ZYDIS_NO_LIBC)
132 # define ZYDIS_ASSERT(condition)
133 #else
134 # include <assert.h>
135 # define ZYDIS_ASSERT(condition) assert(condition)
136 #endif
137 
138 #if defined(ZYDIS_RELEASE)
139 # if defined(ZYDIS_CLANG) // GCC eagerly evals && RHS, we have to use nested ifs.
140 # if __has_builtin(__builtin_unreachable)
141 # define ZYDIS_UNREACHABLE __builtin_unreachable()
142 # else
143 # define ZYDIS_UNREACHABLE for(;;)
144 # endif
145 # elif defined(ZYDIS_GCC) && ((__GNUC__ == 4 && __GNUC_MINOR__ > 4) || __GNUC__ > 4)
146 # define ZYDIS_UNREACHABLE __builtin_unreachable()
147 # elif defined(ZYDIS_ICC)
148 # ifdef ZYDIS_WINDOWS
149 # include <stdlib.h> // "missing return statement" workaround
150 # define ZYDIS_UNREACHABLE __assume(0); (void)abort()
151 # else
152 # define ZYDIS_UNREACHABLE __builtin_unreachable()
153 # endif
154 # elif defined(ZYDIS_MSVC)
155 # define ZYDIS_UNREACHABLE __assume(0)
156 # else
157 # define ZYDIS_UNREACHABLE for(;;)
158 # endif
159 #elif defined(ZYDIS_NO_LIBC)
160 # define ZYDIS_UNREACHABLE for(;;)
161 #else
162 # include <stdlib.h>
163 # define ZYDIS_UNREACHABLE { assert(0); abort(); }
164 #endif
165 
166 /* ============================================================================================== */
167 /* Utils */
168 /* ============================================================================================== */
169 
173 #if __STDC_VERSION__ >= 201112L
174 # define ZYDIS_STATIC_ASSERT(x) _Static_assert(x, #x)
175 #else
176 # define ZYDIS_MACRO_CONCAT2(x, y) x##y
177 # define ZYDIS_MACRO_CONCAT(x, y) ZYDIS_MACRO_CONCAT2(x, y)
178 # define ZYDIS_STATIC_ASSERT(x) \
179  typedef int ZYDIS_MACRO_CONCAT(ZYDIS_SASSERT_, __COUNTER__) [(x) ? 1 : -1]
180 #endif
181 
185 #define ZYDIS_BITFIELD(x) : x
186 
190 #define ZYDIS_UNUSED_PARAMETER(x) (void)(x)
191 
195 #define ZYDIS_FALLTHROUGH
196 
200 #define ZYDIS_ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
201 
202 /* ============================================================================================== */
203 
204 #endif /* ZYDIS_DEFINES_H */