Zydis  v2.0.0-alpha2
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 # error "Unsupported platform detected"
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 #else
87 # error "Unsupported architecture detected"
88 #endif
89 
90 /* ============================================================================================== */
91 /* Debug/Release detection */
92 /* ============================================================================================== */
93 
94 #if defined(ZYDIS_MSVC) || defined(ZYDIS_BORLAND)
95 # ifdef _DEBUG
96 # define ZYDIS_DEBUG
97 # else
98 # define ZYDIS_RELEASE
99 # endif
100 #elif defined(ZYDIS_GNUC) || defined(ZYDIS_ICC)
101 # ifdef NDEBUG
102 # define ZYDIS_RELEASE
103 # else
104 # define ZYDIS_DEBUG
105 # endif
106 #else
107 # error "Unsupported compiler detected"
108 #endif
109 
110 /* ============================================================================================== */
111 /* Misc compatibility macros */
112 /* ============================================================================================== */
113 
114 #if defined(ZYDIS_MSVC) || defined(ZYDIS_BORLAND)
115 # define ZYDIS_INLINE __inline
116 #else
117 # define ZYDIS_INLINE static inline
118 #endif
119 
120 /* ============================================================================================== */
121 /* Debugging and optimization macros */
122 /* ============================================================================================== */
123 
124 #if defined(ZYDIS_WINKERNEL)
125 # define ZYDIS_ASSERT(condition)
126 #else
127 # include <assert.h>
128 # define ZYDIS_ASSERT(condition) assert(condition)
129 #endif
130 
131 #if defined(ZYDIS_RELEASE)
132 # if defined(ZYDIS_CLANG) // GCC eagerly evals && RHS, we have to use nested ifs.
133 # if __has_builtin(__builtin_unreachable)
134 # define ZYDIS_UNREACHABLE __builtin_unreachable()
135 # else
136 # define ZYDIS_UNREACHABLE
137 # endif
138 # elif defined(ZYDIS_GCC) && ((__GNUC__ == 4 && __GNUC_MINOR__ > 4) || __GNUC__ > 4)
139 # define ZYDIS_UNREACHABLE __builtin_unreachable()
140 # elif defined(ZYDIS_ICC)
141 # ifdef ZYDIS_WINDOWS
142 # include <stdlib.h> // "missing return statement" workaround
143 # define ZYDIS_UNREACHABLE __assume(0); (void)abort()
144 # else
145 # define ZYDIS_UNREACHABLE __builtin_unreachable()
146 # endif
147 # elif defined(ZYDIS_MSVC)
148 # define ZYDIS_UNREACHABLE __assume(0)
149 # else
150 # define ZYDIS_UNREACHABLE
151 # endif
152 #elif defined(ZYDIS_WINKERNEL)
153 # define ZYDIS_UNREACHABLE
154 #else
155 # include <stdlib.h>
156 # define ZYDIS_UNREACHABLE { assert(0); abort(); }
157 #endif
158 
159 /* ============================================================================================== */
160 /* Utils */
161 /* ============================================================================================== */
162 
166 #define ZYDIS_BITFIELD(x) : x
167 
171 #define ZYDIS_ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
172 
173 /* ============================================================================================== */
174 
175 #endif /* ZYDIS_DEFINES_H */