• 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 // Get standard C99 macros for integer types.
31 #ifndef __STDC_CONSTANT_MACROS
32 #define __STDC_CONSTANT_MACROS
33 #endif
34 
35 #ifndef __STDC_LIMIT_MACROS
36 #define __STDC_LIMIT_MACROS
37 #endif
38 
39 #ifndef __STDC_FORMAT_MACROS
40 #define __STDC_FORMAT_MACROS
41 #endif
42 
43 extern "C" {
44 #include <inttypes.h>
45 #include <stdint.h>
46 }
47 
48 #include <cassert>
49 #include <cstdarg>
50 #include <cstddef>
51 #include <cstdio>
52 #include <cstdlib>
53 
54 #include "platform-vixl.h"
55 
56 #ifdef VIXL_NEGATIVE_TESTING
57 #include <stdexcept>
58 #include <string>
59 #include <sstream>
60 #endif
61 
62 namespace vixl {
63 
64 typedef uint8_t byte;
65 
66 // Type for half-precision (16 bit) floating point numbers.
67 typedef uint16_t float16;
68 
69 const int KBytes = 1024;
70 const int MBytes = 1024 * KBytes;
71 
72 const int kBitsPerByte = 8;
73 
74 }  // namespace vixl
75 
76 // Detect the host's pointer size.
77 #if (UINTPTR_MAX == UINT32_MAX)
78 #define VIXL_HOST_POINTER_32
79 #elif(UINTPTR_MAX == UINT64_MAX)
80 #define VIXL_HOST_POINTER_64
81 #else
82 #error "Unsupported host pointer size."
83 #endif
84 
85 #ifdef VIXL_NEGATIVE_TESTING
86 #define VIXL_ABORT()                                                         \
87   do {                                                                       \
88     std::ostringstream oss;                                                  \
89     oss << "Aborting in " << __FILE__ << ", line " << __LINE__ << std::endl; \
90     throw std::runtime_error(oss.str());                                     \
91   } while (false)
92 #define VIXL_ABORT_WITH_MSG(msg)                                             \
93   do {                                                                       \
94     std::ostringstream oss;                                                  \
95     oss << (msg) << "in " << __FILE__ << ", line " << __LINE__ << std::endl; \
96     throw std::runtime_error(oss.str());                                     \
97   } while (false)
98 #define VIXL_CHECK(condition)                                \
99   do {                                                       \
100     if (!(condition)) {                                      \
101       std::ostringstream oss;                                \
102       oss << "Assertion failed (" #condition ")\nin ";       \
103       oss << __FILE__ << ", line " << __LINE__ << std::endl; \
104       throw std::runtime_error(oss.str());                   \
105     }                                                        \
106   } while (false)
107 #define VIXL_THROW_IN_NEGATIVE_TESTING_MODE(error) throw(error)
108 #else
109 #define VIXL_ABORT()                                         \
110   do {                                                       \
111     printf("Aborting in %s, line %i\n", __FILE__, __LINE__); \
112     abort();                                                 \
113   } while (false)
114 #define VIXL_ABORT_WITH_MSG(msg)                             \
115   do {                                                       \
116     printf("%sin %s, line %i\n", (msg), __FILE__, __LINE__); \
117     abort();                                                 \
118   } while (false)
119 #define VIXL_CHECK(condition)                                       \
120   do {                                                              \
121     if (!(condition)) {                                             \
122       printf("Assertion failed (" #condition ")\nin %s, line %i\n", \
123              __FILE__,                                              \
124              __LINE__);                                             \
125       abort();                                                      \
126     }                                                               \
127   } while (false)
128 #define VIXL_THROW_IN_NEGATIVE_TESTING_MODE(error)
129 #endif
130 #ifdef VIXL_DEBUG
131 #define VIXL_ASSERT(condition) assert(condition)
132 #define VIXL_UNIMPLEMENTED()               \
133   do {                                     \
134     VIXL_ABORT_WITH_MSG("UNIMPLEMENTED "); \
135   } while (false)
136 #define VIXL_UNREACHABLE()               \
137   do {                                   \
138     VIXL_ABORT_WITH_MSG("UNREACHABLE "); \
139   } while (false)
140 #else
141 #define VIXL_ASSERT(condition) ((void)0)
142 #define VIXL_UNIMPLEMENTED() ((void)0)
143 #define VIXL_UNREACHABLE() ((void)0)
144 #endif
145 // This is not as powerful as template based assertions, but it is simple.
146 // It assumes that the descriptions are unique. If this starts being a problem,
147 // we can switch to a different implemention.
148 #define VIXL_CONCAT(a, b) a##b
149 #if __cplusplus >= 201103L
150 #define VIXL_STATIC_ASSERT_LINE(line_unused, condition, message) \
151   static_assert(condition, message)
152 #else
153 #define VIXL_STATIC_ASSERT_LINE(line, condition, message_unused)            \
154   typedef char VIXL_CONCAT(STATIC_ASSERT_LINE_, line)[(condition) ? 1 : -1] \
155       __attribute__((unused))
156 #endif
157 #define VIXL_STATIC_ASSERT(condition) \
158   VIXL_STATIC_ASSERT_LINE(__LINE__, condition, "")
159 #define VIXL_STATIC_ASSERT_MESSAGE(condition, message) \
160   VIXL_STATIC_ASSERT_LINE(__LINE__, condition, message)
161 
162 #define VIXL_WARNING(message)                                          \
163   do {                                                                 \
164     printf("WARNING in %s, line %i: %s", __FILE__, __LINE__, message); \
165   } while (false)
166 
167 template <typename T1>
USE(const T1 &)168 inline void USE(const T1&) {}
169 
170 template <typename T1, typename T2>
USE(const T1 &,const T2 &)171 inline void USE(const T1&, const T2&) {}
172 
173 template <typename T1, typename T2, typename T3>
USE(const T1 &,const T2 &,const T3 &)174 inline void USE(const T1&, const T2&, const T3&) {}
175 
176 template <typename T1, typename T2, typename T3, typename T4>
USE(const T1 &,const T2 &,const T3 &,const T4 &)177 inline void USE(const T1&, const T2&, const T3&, const T4&) {}
178 
179 #define VIXL_ALIGNMENT_EXCEPTION()            \
180   do {                                        \
181     fprintf(stderr, "ALIGNMENT EXCEPTION\t"); \
182     VIXL_ABORT();                             \
183   } while (0)
184 
185 // The clang::fallthrough attribute is used along with the Wimplicit-fallthrough
186 // argument to annotate intentional fall-through between switch labels.
187 // For more information please refer to:
188 // http://clang.llvm.org/docs/AttributeReference.html#fallthrough-clang-fallthrough
189 #ifndef __has_warning
190 #define __has_warning(x) 0
191 #endif
192 
193 // Note: This option is only available for Clang. And will only be enabled for
194 // C++11(201103L).
195 #if __has_warning("-Wimplicit-fallthrough") && __cplusplus >= 201103L
196 #define VIXL_FALLTHROUGH() [[clang::fallthrough]]
197 #else
198 #define VIXL_FALLTHROUGH() \
199   do {                     \
200   } while (0)
201 #endif
202 
203 #if __cplusplus >= 201103L
204 #define VIXL_NO_RETURN [[noreturn]]
205 #else
206 #define VIXL_NO_RETURN __attribute__((noreturn))
207 #endif
208 #ifdef VIXL_DEBUG
209 #define VIXL_NO_RETURN_IN_DEBUG_MODE VIXL_NO_RETURN
210 #else
211 #define VIXL_NO_RETURN_IN_DEBUG_MODE
212 #endif
213 
214 #if __cplusplus >= 201103L
215 #define VIXL_OVERRIDE override
216 #else
217 #define VIXL_OVERRIDE
218 #endif
219 
220 // Some functions might only be marked as "noreturn" for the DEBUG build. This
221 // macro should be used for such cases (for more details see what
222 // VIXL_UNREACHABLE expands to).
223 #ifdef VIXL_DEBUG
224 #define VIXL_DEBUG_NO_RETURN VIXL_NO_RETURN
225 #else
226 #define VIXL_DEBUG_NO_RETURN
227 #endif
228 
229 #ifdef VIXL_INCLUDE_SIMULATOR_AARCH64
230 #ifndef VIXL_AARCH64_GENERATE_SIMULATOR_CODE
231 #define VIXL_AARCH64_GENERATE_SIMULATOR_CODE 1
232 #endif
233 #else
234 #ifndef VIXL_AARCH64_GENERATE_SIMULATOR_CODE
235 #define VIXL_AARCH64_GENERATE_SIMULATOR_CODE 0
236 #endif
237 #if VIXL_AARCH64_GENERATE_SIMULATOR_CODE
238 #warning "Generating Simulator instructions without Simulator support."
239 #endif
240 #endif
241 
242 // We do not have a simulator for AArch32, although we can pretend we do so that
243 // tests that require running natively can be skipped.
244 #ifndef __arm__
245 #define VIXL_INCLUDE_SIMULATOR_AARCH32
246 #ifndef VIXL_AARCH32_GENERATE_SIMULATOR_CODE
247 #define VIXL_AARCH32_GENERATE_SIMULATOR_CODE 1
248 #endif
249 #else
250 #ifndef VIXL_AARCH32_GENERATE_SIMULATOR_CODE
251 #define VIXL_AARCH32_GENERATE_SIMULATOR_CODE 0
252 #endif
253 #endif
254 
255 #ifdef USE_SIMULATOR
256 #error "Please see the release notes for USE_SIMULATOR."
257 #endif
258 
259 // Target Architecture/ISA
260 #ifdef VIXL_INCLUDE_TARGET_A64
261 #define VIXL_INCLUDE_TARGET_AARCH64
262 #endif
263 
264 #if defined(VIXL_INCLUDE_TARGET_A32) && defined(VIXL_INCLUDE_TARGET_T32)
265 #define VIXL_INCLUDE_TARGET_AARCH32
266 #elif defined(VIXL_INCLUDE_TARGET_A32)
267 #define VIXL_INCLUDE_TARGET_A32_ONLY
268 #else
269 #define VIXL_INCLUDE_TARGET_T32_ONLY
270 #endif
271 
272 
273 #endif  // VIXL_GLOBALS_H
274