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