Zydis  v2.0.0-alpha1
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 
27 #ifndef ZYDIS_DEFINES_H
28 #define ZYDIS_DEFINES_H
29 
30 #include <assert.h>
31 #include <ZydisExportConfig.h>
32 
33 /* ============================================================================================== */
34 /* Compiler detection */
35 /* ============================================================================================== */
36 
37 #if defined(__clang__)
38 # define ZYDIS_CLANG
39 # define ZYDIS_GNUC
40 #elif defined(__ICC) || defined(__INTEL_COMPILER)
41 # define ZYDIS_ICC
42 #elif defined(__GNUC__) || defined(__GNUG__)
43 # define ZYDIS_GCC
44 # define ZYDIS_GNUC
45 #elif defined(_MSC_VER)
46 # define ZYDIS_MSVC
47 #elif defined(__BORLANDC__)
48 # define ZYDIS_BORLAND
49 #else
50 # define ZYDIS_UNKNOWN_COMPILER
51 #endif
52 
53 /* ============================================================================================== */
54 /* Platform detection */
55 /* ============================================================================================== */
56 
57 #if defined(_WIN32)
58 # define ZYDIS_WINDOWS
59 #elif defined(__APPLE__)
60 # define ZYDIS_APPLE
61 # define ZYDIS_POSIX
62 #elif defined(__linux)
63 # define ZYDIS_LINUX
64 # define ZYDIS_POSIX
65 #elif defined(__unix)
66 # define ZYDIS_UNIX
67 # define ZYDIS_POSIX
68 #elif defined(__posix)
69 # define ZYDIS_POSIX
70 #else
71 # error "Unsupported platform detected"
72 #endif
73 
74 /* ============================================================================================== */
75 /* Architecture detection */
76 /* ============================================================================================== */
77 
78 #if defined (_M_AMD64) || defined (__x86_64__)
79 # define ZYDIS_X64
80 #elif defined (_M_IX86) || defined (__i386__)
81 # define ZYDIS_X86
82 #else
83 # error "Unsupported architecture detected"
84 #endif
85 
86 /* ============================================================================================== */
87 /* Debug/Release detection */
88 /* ============================================================================================== */
89 
90 #if defined(ZYDIS_MSVC) || defined(ZYDIS_BORLAND)
91 # ifdef _DEBUG
92 # define ZYDIS_DEBUG
93 # else
94 # define ZYDIS_RELEASE
95 # endif
96 #elif defined(ZYDIS_GNUC) || defined(ZYDIS_ICC)
97 # ifdef NDEBUG
98 # define ZYDIS_RELEASE
99 # else
100 # define ZYDIS_DEBUG
101 # endif
102 #else
103 # error "Unsupported compiler detected"
104 #endif
105 
106 /* ============================================================================================== */
107 /* Misc compatibility macros */
108 /* ============================================================================================== */
109 
110 #if defined(ZYDIS_MSVC) || defined(ZYDIS_BORLAND)
111 # define ZYDIS_INLINE __inline
112 #else
113 # define ZYDIS_INLINE static inline
114 #endif
115 
116 /* ============================================================================================== */
117 /* Debugging and optimization macros */
118 /* ============================================================================================== */
119 
120 #define ZYDIS_ASSERT(condition) assert(condition)
121 
122 #if defined(ZYDIS_RELEASE)
123 # if defined(ZYDIS_GNUC)
124 # if __has_builtin(__builtin_unreachable)
125 # define ZYDIS_UNREACHABLE __builtin_unreachable()
126 # else
127 # define ZYDIS_UNREACHABLE
128 # endif
129 # else
130 # define ZYDIS_UNREACHABLE
131 # endif
132 #else
133 # define ZYDIS_UNREACHABLE assert(0)
134 #endif
135 
136 /* ============================================================================================== */
137 /* Utils */
138 /* ============================================================================================== */
139 
143 #define ZYDIS_BITFIELD(x) : x
144 
148 #define ZYDIS_ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
149 
150 /* ============================================================================================== */
151 
152 #endif /* ZYDIS_DEFINES_H */