• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015, VIXL authors
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 //   * Redistributions of source code must retain the above copyright notice,
8 //     this list of conditions and the following disclaimer.
9 //   * Redistributions in binary form must reproduce the above copyright notice,
10 //     this list of conditions and the following disclaimer in the documentation
11 //     and/or other materials provided with the distribution.
12 //   * Neither the name of ARM Limited nor the names of its contributors may be
13 //     used to endorse or promote products derived from this software without
14 //     specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #ifndef VIXL_GLOBALS_H
28 #define VIXL_GLOBALS_H
29 
30 #if __cplusplus < 201703L
31 #error VIXL requires C++17
32 #endif
33 
34 // Get standard C99 macros for integer types.
35 #ifndef __STDC_CONSTANT_MACROS
36 #define __STDC_CONSTANT_MACROS
37 #endif
38 
39 #ifndef __STDC_LIMIT_MACROS
40 #define __STDC_LIMIT_MACROS
41 #endif
42 
43 #ifndef __STDC_FORMAT_MACROS
44 #define __STDC_FORMAT_MACROS
45 #endif
46 
47 extern "C" {
48 #include <inttypes.h>
49 #include <stdint.h>
50 }
51 
52 #include <cassert>
53 #include <cstdarg>
54 #include <cstddef>
55 #include <cstdio>
56 #include <cstdlib>
57 
58 #include "platform-vixl.h"
59 
60 #ifdef VIXL_NEGATIVE_TESTING
61 #include <sstream>
62 #include <stdexcept>
63 #include <string>
64 #endif
65 
66 namespace vixl {
67 
68 typedef uint8_t byte;
69 
70 const int KBytes = 1024;
71 const int MBytes = 1024 * KBytes;
72 
73 const int kBitsPerByteLog2 = 3;
74 const int kBitsPerByte = 1 << kBitsPerByteLog2;
75 
76 template <int SizeInBits>
77 struct Unsigned;
78 
79 template <>
80 struct Unsigned<32> {
81   typedef uint32_t type;
82 };
83 
84 template <>
85 struct Unsigned<64> {
86   typedef uint64_t type;
87 };
88 
89 }  // namespace vixl
90 
91 // Detect the host's pointer size.
92 #if (UINTPTR_MAX == UINT32_MAX)
93 #define VIXL_HOST_POINTER_32
94 #elif (UINTPTR_MAX == UINT64_MAX)
95 #define VIXL_HOST_POINTER_64
96 #else
97 #error "Unsupported host pointer size."
98 #endif
99 
100 #ifdef VIXL_NEGATIVE_TESTING
101 #define VIXL_ABORT()                                                         \
102   do {                                                                       \
103     std::ostringstream oss;                                                  \
104     oss << "Aborting in " << __FILE__ << ", line " << __LINE__ << std::endl; \
105     throw std::runtime_error(oss.str());                                     \
106   } while (false)
107 #define VIXL_ABORT_WITH_MSG(msg)                                             \
108   do {                                                                       \
109     std::ostringstream oss;                                                  \
110     oss << (msg) << "in " << __FILE__ << ", line " << __LINE__ << std::endl; \
111     throw std::runtime_error(oss.str());                                     \
112   } while (false)
113 #define VIXL_CHECK(condition)                                \
114   do {                                                       \
115     if (!(condition)) {                                      \
116       std::ostringstream oss;                                \
117       oss << "Assertion failed (" #condition ")\nin ";       \
118       oss << __FILE__ << ", line " << __LINE__ << std::endl; \
119       throw std::runtime_error(oss.str());                   \
120     }                                                        \
121   } while (false)
122 #else
123 #define VIXL_ABORT()                                         \
124   do {                                                       \
125     printf("Aborting in %s, line %i\n", __FILE__, __LINE__); \
126     abort();                                                 \
127   } while (false)
128 #define VIXL_ABORT_WITH_MSG(msg)                             \
129   do {                                                       \
130     printf("%sin %s, line %i\n", (msg), __FILE__, __LINE__); \
131     abort();                                                 \
132   } while (false)
133 #define VIXL_CHECK(condition)                           \
134   do {                                                  \
135     if (!(condition)) {                                 \
136       printf("Assertion failed (%s)\nin %s, line %i\n", \
137              #condition,                                \
138              __FILE__,                                  \
139              __LINE__);                                 \
140       abort();                                          \
141     }                                                   \
142   } while (false)
143 #endif
144 #ifdef VIXL_DEBUG
145 #define VIXL_ASSERT(condition) VIXL_CHECK(condition)
146 #define VIXL_UNIMPLEMENTED()               \
147   do {                                     \
148     VIXL_ABORT_WITH_MSG("UNIMPLEMENTED "); \
149   } while (false)
150 #define VIXL_UNREACHABLE()               \
151   do {                                   \
152     VIXL_ABORT_WITH_MSG("UNREACHABLE "); \
153   } while (false)
154 #else
155 #define VIXL_ASSERT(condition) ((void)0)
156 #define VIXL_UNIMPLEMENTED() ((void)0)
157 #define VIXL_UNREACHABLE() ((void)0)
158 #endif
159 // This is not as powerful as template based assertions, but it is simple.
160 // It assumes that the descriptions are unique. If this starts being a problem,
161 // we can switch to a different implementation.
162 #define VIXL_CONCAT(a, b) a##b
163 #if __cplusplus >= 201103L
164 #define VIXL_STATIC_ASSERT_LINE(line_unused, condition, message) \
165   static_assert(condition, message)
166 #else
167 #define VIXL_STATIC_ASSERT_LINE(line, condition, message_unused)            \
168   typedef char VIXL_CONCAT(STATIC_ASSERT_LINE_, line)[(condition) ? 1 : -1] \
169       __attribute__((unused))
170 #endif
171 #define VIXL_STATIC_ASSERT(condition) \
172   VIXL_STATIC_ASSERT_LINE(__LINE__, condition, "")
173 #define VIXL_STATIC_ASSERT_MESSAGE(condition, message) \
174   VIXL_STATIC_ASSERT_LINE(__LINE__, condition, message)
175 
176 #define VIXL_WARNING(message)                                          \
177   do {                                                                 \
178     printf("WARNING in %s, line %i: %s", __FILE__, __LINE__, message); \
179   } while (false)
180 
181 template <typename T1>
182 inline void USE(const T1&) {}
183 
184 template <typename T1, typename T2>
185 inline void USE(const T1&, const T2&) {}
186 
187 template <typename T1, typename T2, typename T3>
188 inline void USE(const T1&, const T2&, const T3&) {}
189 
190 template <typename T1, typename T2, typename T3, typename T4>
191 inline void USE(const T1&, const T2&, const T3&, const T4&) {}
192 
193 #define VIXL_ALIGNMENT_EXCEPTION()                \
194   do {                                            \
195     VIXL_ABORT_WITH_MSG("ALIGNMENT EXCEPTION\t"); \
196   } while (0)
197 
198 // The clang::fallthrough attribute is used along with the Wimplicit-fallthrough
199 // argument to annotate intentional fall-through between switch labels.
200 // For more information please refer to:
201 // http://clang.llvm.org/docs/AttributeReference.html#fallthrough-clang-fallthrough
202 #ifndef __has_warning
203 #define __has_warning(x) 0
204 #endif
205 
206 // Fallthrough annotation for Clang and C++11(201103L).
207 #if __has_warning("-Wimplicit-fallthrough") && __cplusplus >= 201103L
208 #define VIXL_FALLTHROUGH() [[clang::fallthrough]]
209 // Fallthrough annotation for GCC >= 7.
210 #elif defined(__GNUC__) && __GNUC__ >= 7
211 #define VIXL_FALLTHROUGH() __attribute__((fallthrough))
212 #else
213 #define VIXL_FALLTHROUGH() \
214   do {                     \
215   } while (0)
216 #endif
217 
218 // Evaluate 'init' to an std::optional and return if it's empty. If 'init' is
219 // not empty then define a variable 'name' with the value inside the
220 // std::optional.
221 #define VIXL_DEFINE_OR_RETURN(name, init) \
222   auto opt##name = init;                  \
223   if (!opt##name) return;                 \
224   auto name = *opt##name;
225 #define VIXL_DEFINE_OR_RETURN_FALSE(name, init) \
226   auto opt##name = init;                        \
227   if (!opt##name) return false;                 \
228   auto name = *opt##name;
229 
230 #if __cplusplus >= 201103L
231 #define VIXL_NO_RETURN [[noreturn]]
232 #else
233 #define VIXL_NO_RETURN __attribute__((noreturn))
234 #endif
235 #ifdef VIXL_DEBUG
236 #define VIXL_NO_RETURN_IN_DEBUG_MODE VIXL_NO_RETURN
237 #else
238 #define VIXL_NO_RETURN_IN_DEBUG_MODE
239 #endif
240 
241 #if __cplusplus >= 201103L
242 #define VIXL_OVERRIDE override
243 #define VIXL_CONSTEXPR constexpr
244 #define VIXL_HAS_CONSTEXPR 1
245 #else
246 #define VIXL_OVERRIDE
247 #define VIXL_CONSTEXPR
248 #endif
249 
250 // With VIXL_NEGATIVE_TESTING on, VIXL_ASSERT and VIXL_CHECK will throw
251 // exceptions but C++11 marks destructors as noexcept(true) by default.
252 #if defined(VIXL_NEGATIVE_TESTING) && __cplusplus >= 201103L
253 #define VIXL_NEGATIVE_TESTING_ALLOW_EXCEPTION noexcept(false)
254 #else
255 #define VIXL_NEGATIVE_TESTING_ALLOW_EXCEPTION
256 #endif
257 
258 #ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
259 #ifndef VIXL_AARCH64_GENERATE_SIMULATOR_CODE
260 #define VIXL_AARCH64_GENERATE_SIMULATOR_CODE 1
261 #endif
262 #else
263 #ifndef VIXL_AARCH64_GENERATE_SIMULATOR_CODE
264 #define VIXL_AARCH64_GENERATE_SIMULATOR_CODE 0
265 #endif
266 #if VIXL_AARCH64_GENERATE_SIMULATOR_CODE
267 #warning "Generating Simulator instructions without Simulator support."
268 #endif
269 #endif
270 
271 // We do not have a simulator for AArch32, although we can pretend we do so that
272 // tests that require running natively can be skipped.
273 #ifndef __arm__
274 #define VIXL_INCLUDE_SIMULATOR_AARCH32
275 #ifndef VIXL_AARCH32_GENERATE_SIMULATOR_CODE
276 #define VIXL_AARCH32_GENERATE_SIMULATOR_CODE 1
277 #endif
278 #else
279 #ifndef VIXL_AARCH32_GENERATE_SIMULATOR_CODE
280 #define VIXL_AARCH32_GENERATE_SIMULATOR_CODE 0
281 #endif
282 #endif
283 
284 #ifdef USE_SIMULATOR
285 #error "Please see the release notes for USE_SIMULATOR."
286 #endif
287 
288 // Target Architecture/ISA
289 #ifdef VIXL_INCLUDE_TARGET_A64
290 #ifndef VIXL_INCLUDE_TARGET_AARCH64
291 #define VIXL_INCLUDE_TARGET_AARCH64
292 #endif
293 #endif
294 
295 #if defined(VIXL_INCLUDE_TARGET_A32) && defined(VIXL_INCLUDE_TARGET_T32)
296 #ifndef VIXL_INCLUDE_TARGET_AARCH32
297 #define VIXL_INCLUDE_TARGET_AARCH32
298 #endif
299 #elif defined(VIXL_INCLUDE_TARGET_A32)
300 #ifndef VIXL_INCLUDE_TARGET_A32_ONLY
301 #define VIXL_INCLUDE_TARGET_A32_ONLY
302 #endif
303 #else
304 #ifndef VIXL_INCLUDE_TARGET_T32_ONLY
305 #define VIXL_INCLUDE_TARGET_T32_ONLY
306 #endif
307 #endif
308 
309 
310 #endif  // VIXL_GLOBALS_H
311