Zydis  v2.0.0
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(__unix)
70 # define ZYDIS_UNIX
71 # define ZYDIS_POSIX
72 #elif defined(__posix)
73 # define ZYDIS_POSIX
74 #else
75 # define ZYDIS_UNKNOWN_PLATFORM
76 #endif
77 
78 /* ============================================================================================== */
79 /* Architecture detection */
80 /* ============================================================================================== */
81 
82 #if defined(_M_AMD64) || defined(__x86_64__)
83 # define ZYDIS_X64
84 #elif defined(_M_IX86) || defined(__i386__)
85 # define ZYDIS_X86
86 #elif defined(_M_ARM64) || defined(__aarch64__)
87 # define ZYDIS_AARCH64
88 #elif defined(_M_ARM) || defined(_M_ARMT) || defined(__arm__) || defined(__thumb__)
89 # define ZYDIS_ARM
90 #else
91 # error "Unsupported architecture detected"
92 #endif
93 
94 /* ============================================================================================== */
95 /* Debug/Release detection */
96 /* ============================================================================================== */
97 
98 #if defined(ZYDIS_MSVC) || defined(ZYDIS_BORLAND)
99 # ifdef _DEBUG
100 # define ZYDIS_DEBUG
101 # else
102 # define ZYDIS_RELEASE
103 # endif
104 #elif defined(ZYDIS_GNUC) || defined(ZYDIS_ICC)
105 # ifdef NDEBUG
106 # define ZYDIS_RELEASE
107 # else
108 # define ZYDIS_DEBUG
109 # endif
110 #else
111 # define ZYDIS_RELEASE
112 #endif
113 
114 /* ============================================================================================== */
115 /* Misc compatibility macros */
116 /* ============================================================================================== */
117 
118 #if defined(ZYDIS_MSVC) || defined(ZYDIS_BORLAND)
119 # define ZYDIS_INLINE __inline
120 #else
121 # define ZYDIS_INLINE static inline
122 #endif
123 
124 /* ============================================================================================== */
125 /* Debugging and optimization macros */
126 /* ============================================================================================== */
127 
128 #if defined(ZYDIS_NO_LIBC)
129 # define ZYDIS_ASSERT(condition)
130 #else
131 # include <assert.h>
132 # define ZYDIS_ASSERT(condition) assert(condition)
133 #endif
134 
135 #if defined(ZYDIS_RELEASE)
136 # if defined(ZYDIS_CLANG) // GCC eagerly evals && RHS, we have to use nested ifs.
137 # if __has_builtin(__builtin_unreachable)
138 # define ZYDIS_UNREACHABLE __builtin_unreachable()
139 # else
140 # define ZYDIS_UNREACHABLE for(;;)
141 # endif
142 # elif defined(ZYDIS_GCC) && ((__GNUC__ == 4 && __GNUC_MINOR__ > 4) || __GNUC__ > 4)
143 # define ZYDIS_UNREACHABLE __builtin_unreachable()
144 # elif defined(ZYDIS_ICC)
145 # ifdef ZYDIS_WINDOWS
146 # include <stdlib.h> // "missing return statement" workaround
147 # define ZYDIS_UNREACHABLE __assume(0); (void)abort()
148 # else
149 # define ZYDIS_UNREACHABLE __builtin_unreachable()
150 # endif
151 # elif defined(ZYDIS_MSVC)
152 # define ZYDIS_UNREACHABLE __assume(0)
153 # else
154 # define ZYDIS_UNREACHABLE for(;;)
155 # endif
156 #elif defined(ZYDIS_NO_LIBC)
157 # define ZYDIS_UNREACHABLE for(;;)
158 #else
159 # include <stdlib.h>
160 # define ZYDIS_UNREACHABLE { assert(0); abort(); }
161 #endif
162 
163 /* ============================================================================================== */
164 /* Utils */
165 /* ============================================================================================== */
166 
170 #if __STDC_VERSION__ >= 201112L
171 # define ZYDIS_STATIC_ASSERT(x) _Static_assert(x, #x)
172 #else
173 # define ZYDIS_STATIC_ASSERT(x) typedef int ZYDIS_SASSERT_IMPL[(x) ? 1 : -1]
174 #endif
175 
179 #define ZYDIS_BITFIELD(x) : x
180 
184 #define ZYDIS_UNUSED_PARAMETER(x) (void)(x)
185 
189 #define ZYDIS_FALLTHROUGH
190 
194 #define ZYDIS_ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
195 
196 /* ============================================================================================== */
197 
198 #endif /* ZYDIS_DEFINES_H */