• 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 < 201402L
31 #error VIXL requires C++14
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 implemention.
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 __GNUC__ >= 7
211 #define VIXL_FALLTHROUGH() __attribute__((fallthrough))
212 #else
213 #define VIXL_FALLTHROUGH() \
214   do {                     \
215   } while (0)
216 #endif
217 
218 #if __cplusplus >= 201103L
219 #define VIXL_NO_RETURN [[noreturn]]
220 #else
221 #define VIXL_NO_RETURN __attribute__((noreturn))
222 #endif
223 #ifdef VIXL_DEBUG
224 #define VIXL_NO_RETURN_IN_DEBUG_MODE VIXL_NO_RETURN
225 #else
226 #define VIXL_NO_RETURN_IN_DEBUG_MODE
227 #endif
228 
229 #if __cplusplus >= 201103L
230 #define VIXL_OVERRIDE override
231 #define VIXL_CONSTEXPR constexpr
232 #define VIXL_HAS_CONSTEXPR 1
233 #else
234 #define VIXL_OVERRIDE
235 #define VIXL_CONSTEXPR
236 #endif
237 
238 // With VIXL_NEGATIVE_TESTING on, VIXL_ASSERT and VIXL_CHECK will throw
239 // exceptions but C++11 marks destructors as noexcept(true) by default.
240 #if defined(VIXL_NEGATIVE_TESTING) && __cplusplus >= 201103L
241 #define VIXL_NEGATIVE_TESTING_ALLOW_EXCEPTION noexcept(false)
242 #else
243 #define VIXL_NEGATIVE_TESTING_ALLOW_EXCEPTION
244 #endif
245 
246 #ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
247 #ifndef VIXL_AARCH64_GENERATE_SIMULATOR_CODE
248 #define VIXL_AARCH64_GENERATE_SIMULATOR_CODE 1
249 #endif
250 #else
251 #ifndef VIXL_AARCH64_GENERATE_SIMULATOR_CODE
252 #define VIXL_AARCH64_GENERATE_SIMULATOR_CODE 0
253 #endif
254 #if VIXL_AARCH64_GENERATE_SIMULATOR_CODE
255 #warning "Generating Simulator instructions without Simulator support."
256 #endif
257 #endif
258 
259 // We do not have a simulator for AArch32, although we can pretend we do so that
260 // tests that require running natively can be skipped.
261 #ifndef __arm__
262 #define VIXL_INCLUDE_SIMULATOR_AARCH32
263 #ifndef VIXL_AARCH32_GENERATE_SIMULATOR_CODE
264 #define VIXL_AARCH32_GENERATE_SIMULATOR_CODE 1
265 #endif
266 #else
267 #ifndef VIXL_AARCH32_GENERATE_SIMULATOR_CODE
268 #define VIXL_AARCH32_GENERATE_SIMULATOR_CODE 0
269 #endif
270 #endif
271 
272 #ifdef USE_SIMULATOR
273 #error "Please see the release notes for USE_SIMULATOR."
274 #endif
275 
276 // Target Architecture/ISA
277 #ifdef VIXL_INCLUDE_TARGET_A64
278 #ifndef VIXL_INCLUDE_TARGET_AARCH64
279 #define VIXL_INCLUDE_TARGET_AARCH64
280 #endif
281 #endif
282 
283 #if defined(VIXL_INCLUDE_TARGET_A32) && defined(VIXL_INCLUDE_TARGET_T32)
284 #ifndef VIXL_INCLUDE_TARGET_AARCH32
285 #define VIXL_INCLUDE_TARGET_AARCH32
286 #endif
287 #elif defined(VIXL_INCLUDE_TARGET_A32)
288 #ifndef VIXL_INCLUDE_TARGET_A32_ONLY
289 #define VIXL_INCLUDE_TARGET_A32_ONLY
290 #endif
291 #else
292 #ifndef VIXL_INCLUDE_TARGET_T32_ONLY
293 #define VIXL_INCLUDE_TARGET_T32_ONLY
294 #endif
295 #endif
296 
297 
298 #endif  // VIXL_GLOBALS_H
299