gem5 v24.0.0.0
Loading...
Searching...
No Matches
compiler.hh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012,2017-2018 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2006 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 */
40
41#ifndef __BASE_COMPILER_HH__
42#define __BASE_COMPILER_HH__
43
44#include <memory>
45
46#include "config/have_deprecated_namespace.hh"
47
48// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
49
50
51/*
52 * Compiler specific features.
53 */
54
55#if defined(__GNUC__) // clang or gcc.
56// Mark a structure as packed, so that no padding is added to its layout. This
57// padding might be added to, for instance, ensure certain fields have certain
58// alignment.
59# define GEM5_PACKED [[gnu::packed]]
60
61// Prevent a function from being inlined.
62# define GEM5_NO_INLINE [[gnu::noinline]]
63
64// Set the visibility of a symbol.
65# define GEM5_PUBLIC [[gnu:visibility("default")]]
66# define GEM5_LOCAL [[gnu::visibility("hidden")]]
67# define GEM5_WEAK [[gnu::weak]]
68
69// Force an alignment for a variable.
70# define GEM5_ALIGNED(alignment) [[gnu::aligned(alignment)]]
71
72// Marker for what should be an unreachable point in the code.
73# define GEM5_UNREACHABLE __builtin_unreachable()
74
75// To mark a branch condition as likely taken, wrap it's condition with
76// GEM5_LIKELY. To mark it as likely not taken, wrap it's condition with
77// GEM5_UNLIKELY. These can be replaced with the standard attributes [[likely]]
78// and [[unlikely]] in c++20, although the syntax is different enough that
79// we can't do that with direct substitution.
80# define GEM5_LIKELY(cond) __builtin_expect(!!(cond), 1)
81# define GEM5_UNLIKELY(cond) __builtin_expect(!!(cond), 0)
82
83// Mark an expression-like macro as deprecated by wrapping it in some code
84// which declares and uses a deprecated variable with the same name as the
85// macro. The wrapping macro evaluates to the same thing as the original macro.
86// The definition must be an c++ expression and not a statement because of how
87// the original macro is wrapped.
88# define GEM5_DEPRECATED_MACRO(name, definition, message) \
89 ([](){[[deprecated(message)]] int name{}; return name;}(), (definition))
90// This version is for macros which are statement-like, which frequently use
91// "do {} while (0)" to make their syntax look more like normal c++ statements.
92# define GEM5_DEPRECATED_MACRO_STMT(name, definition, message) \
93 do {{definition;} GEM5_DEPRECATED_MACRO(name, ({}), message);} while (0)
94
95// To mark a class as deprecated in favor of a new name, add a respective
96// instance of this macro to the file that used to declare the old name.
97// This macro should be used *after* the new class has been defined.
98# define GEM5_DEPRECATED_CLASS(old_class, new_class) \
99 using old_class \
100 [[deprecated("Please use the new class name: '" #new_class "'")]] = \
101 new_class
102
103// These macros should be used when namespaces are deprecated in favor of
104// a new name. They should be used wherever the namespace is declared.
105// Namespace deprecation is broken for GNU < 10 [1], so there is no
106// deprecation warning in that case. Clang only supports it from C++17 on.
107// [1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79817
108# if HAVE_DEPRECATED_NAMESPACE
109# define GEM5_DEPRECATED_NAMESPACE(old_namespace, new_namespace) \
110 namespace new_namespace {} \
111 namespace [[deprecated("Please use the new namespace: '" \
112 #new_namespace "'")]] old_namespace { \
113 using namespace new_namespace; \
114 }
115# else
116# define GEM5_DEPRECATED_NAMESPACE(old_namespace, new_namespace) \
117 namespace new_namespace {} \
118 namespace old_namespace = new_namespace
119# endif
120
121// Evaluate an expanded parameter pack in order. Multiple arguments can be
122// passed in which be evaluated in order relative to each other as a group.
123// The argument(s) must include a parameter pack to expand. This works because
124// the elements of a brace inclosed initializer list are evaluated in order,
125// as are the arguments to the comma operator, which evaluates to the last
126// value. This is compiler specific because it uses variadic macros.
127#define GEM5_FOR_EACH_IN_PACK(...) \
128do { [[maybe_unused]] int i[] = { 0, ((void)(__VA_ARGS__), 0)... }; } while (0)
129
130#else
131# error "Don't know what to do for your compiler."
132#endif
133
134// When a member variable may be unused, mark it with GEM5_CLASS_VAR_USED. This
135// needs to be limitted to clang only since clang warns on these unused
136// variables, and g++ will actually warn if you use this attribute since it
137// won't do anything there.
138#if defined(__clang__) // clang only.
139# define GEM5_CLASS_VAR_USED GEM5_VAR_USED
140#else
141# define GEM5_CLASS_VAR_USED
142#endif
143
144// Aliases for macros using the deprecated M5 prefix.
145#define M5_VAR_USED GEM5_VAR_USED
146#define M5_NODISCARD GEM5_NO_DISCARD
147#define M5_FALLTHROUGH GEM5_FALLTHROUGH
148#define M5_ATTR_PACKED GEM5_PACKED
149#define M5_NO_INLINE GEM5_NO_INLINE
150#define M5_PUBLIC GEM5_PUBLIC
151#define M5_LOCAL GEM5_LOCAL
152#define M5_WEAK GEM5_WEAK
153#define M5_ALIGNED(x) GEM5_ALIGNED(x)
154#define M5_UNREACHABLE GEM5_UNREACHABLE
155#define M5_LIKELY(x) GEM5_LIKELY(x)
156#define M5_UNLIKELY(x) GEM5_UNLIKELY(x)
157#define M5_FOR_EACH_IN_PACK(...) GEM5_FOR_EACH_IN_PACK(__VA_ARGS__)
158#define M5_CLASS_VAR_USED GEM5_CLASS_VAR_USED
159
160// Deprecated attributes which warn.
161#define GEM5_FALLTHROUGH GEM5_DEPRECATED_MACRO_STMT(GEM5_FALLTHROUGH,,\
162 "Please use the [[fallthrough]] attribute directly."); [[fallthrough]]
163#define GEM5_DEPRECATED(message) \
164 [[deprecated(message " The GEM5_DEPRECATED macro is also deprecated, "\
165 "please use the [[deprecated()]] attribute directly.")]]
166#define GEM5_DEPRECATED_ENUM_VAL(message) \
167 [[deprecated(message " The GEM5_DEPRECATED_ENUM_VAL macro is also "\
168 "deprecated, please use the [[deprecated()]] attribute "\
169 "directly.")]]
170
171// Deprecated attributes which can't be made to warn without possibly breaking
172// existing code.
173#define GEM5_NO_DISCARD [[nodiscard]]
174#define GEM5_VAR_USED [[maybe_unused]]
175
176#endif // __BASE_COMPILER_HH__

Generated on Tue Jun 18 2024 16:24:00 for gem5 by doxygen 1.11.0