gem5  v21.1.0.2
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  * Attributes that become standard in later versions of c++.
53  */
54 
55 // Use GEM5_FALLTHROUGH to mark when you're intentionally falling through from
56 // one case to another in a switch statement.
57 #if __has_cpp_attribute(fallthrough) // Standard in c++17.
58 # define GEM5_FALLTHROUGH [[fallthrough]]
59 #else
60 // Not supported, so it's not necessary to avoid warnings.
61 # define GEM5_FALLTHROUGH
62 #endif
63 
64 // When the return value of a function should not be discarded, mark it with
65 // GEM5_NO_DISCARD.
66 #if __has_cpp_attribute(nodiscard) // Standard in c++17, with message in c++20.
67 # define GEM5_NO_DISCARD [[nodiscard]]
68 #else
69 // Not supported, but it's optional so we can just omit it.
70 # define GEM5_NO_DISCARD
71 #endif
72 
73 // When a variable may purposefully not be used, for instance if it's only used
74 // in debug statements which might be disabled, mark it with GEM5_VAR_USED.
75 #if __has_cpp_attribute(maybe_unused) // Standard in c++17.
76 # define GEM5_VAR_USED [[maybe_unused]]
77 #elif defined(__GNUC__)
78 // gcc and clang support a custom attribute which is essentially the same
79 // thing.
80 # define GEM5_VAR_USED [[gnu::unused]]
81 #else
82 # error "Don't know what to do for your compiler."
83 #endif
84 
85 
86 /*
87  * Compiler specific features.
88  */
89 
90 #if defined(__GNUC__) // clang or gcc.
91 // Mark a structure as packed, so that no padding is added to its layout. This
92 // padding might be added to, for instance, ensure certain fields have certain
93 // alignment.
94 # define GEM5_PACKED [[gnu::packed]]
95 
96 // Prevent a function from being inlined.
97 # define GEM5_NO_INLINE [[gnu::noinline]]
98 
99 // Set the visibility of a symbol.
100 # define GEM5_PUBLIC [[gnu:visibility("default")]]
101 # define GEM5_LOCAL [[gnu::visibility("hidden")]]
102 # define GEM5_WEAK [[gnu::weak]]
103 
104 // Force an alignment for a variable.
105 # define GEM5_ALIGNED(alignment) [[gnu::aligned(alignment)]]
106 
107 // Marker for what should be an unreachable point in the code.
108 # define GEM5_UNREACHABLE __builtin_unreachable()
109 
110 // To mark a branch condition as likely taken, wrap it's condition with
111 // GEM5_LIKELY. To mark it as likely not taken, wrap it's condition with
112 // GEM5_UNLIKELY. These can be replaced with the standard attributes [[likely]]
113 // and [[unlikely]] in c++20, although the syntax is different enough that
114 // we can't do that with direct substitution.
115 # define GEM5_LIKELY(cond) __builtin_expect(!!(cond), 1)
116 # define GEM5_UNLIKELY(cond) __builtin_expect(!!(cond), 0)
117 
118 // Mark a c++ declaration as deprecated, with a message explaining what to do
119 // to update to a non-deprecated alternative.
120 # define GEM5_DEPRECATED(message) [[gnu::deprecated(message)]]
121 // Mark a C++ emum value as deprecated, with a message explaining what to do
122 // to update to a non-deprecated alternative. This wraps GEM5_DEPRECATED but
123 // is guarded by a preprocessor if directive to ensure it is not included
124 // when compiled in GCC < 6, as deprecation of enum values was introduced in
125 // GCC 6. All supported clang compilers allow enum value deprecation.
126 # if defined(__clang__) || __GNUC__ >= 6
127 # define GEM5_DEPRECATED_ENUM_VAL(message) GEM5_DEPRECATED(message)
128 # else
129 # define GEM5_DEPRECATED_ENUM_VAL(message)
130 # endif
131 // Mark an expression-like macro as deprecated by wrapping it in some code
132 // which declares and uses a deprecated variable with the same name as the
133 // macro. The wrapping macro evaluates to the same thing as the original macro.
134 // The definition must be an c++ expression and not a statement because of how
135 // the original macro is wrapped.
136 # define GEM5_DEPRECATED_MACRO(name, definition, message) \
137  ([](){GEM5_DEPRECATED(message) int name{}; return name;}(), (definition))
138 // This version is for macros which are statement-like, which frequently use
139 // "do {} while (0)" to make their syntax look more like normal c++ statements.
140 # define GEM5_DEPRECATED_MACRO_STMT(name, definition, message) \
141  do {{definition;} GEM5_DEPRECATED_MACRO(name, {}, message);} while (0)
142 
143 // To mark a class as deprecated in favor of a new name, add a respective
144 // instance of this macro to the file that used to declare the old name.
145 // This macro should be used *after* the new class has been defined.
146 # define GEM5_DEPRECATED_CLASS(old_class, new_class) \
147  using old_class \
148  GEM5_DEPRECATED("Please use the new class name: '" #new_class "'") = \
149  new_class
150 
151 // These macros should be used when namespaces are deprecated in favor of
152 // a new name. They should be used wherever the namespace is declared.
153 // Namespace deprecation is broken for GNU < 10 [1], so there is no
154 // deprecation warning in that case. Clang only supports it from C++17 on.
155 // [1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79817
156 # if HAVE_DEPRECATED_NAMESPACE
157 # define GEM5_DEPRECATED_NAMESPACE(old_namespace, new_namespace) \
158  namespace new_namespace {} \
159  namespace GEM5_DEPRECATED("Please use the new namespace: '" \
160  #new_namespace "'") old_namespace { \
161  using namespace new_namespace; \
162  }
163 # else
164 # define GEM5_DEPRECATED_NAMESPACE(old_namespace, new_namespace) \
165  namespace new_namespace {} \
166  namespace old_namespace = new_namespace
167 # endif
168 
169 // Evaluate an expanded parameter pack in order. Multiple arguments can be
170 // passed in which be evaluated in order relative to each other as a group.
171 // The argument(s) must include a parameter pack to expand. This works because
172 // the elements of a brace inclosed initializer list are evaluated in order,
173 // as are the arguments to the comma operator, which evaluates to the last
174 // value. This is compiler specific because it uses variadic macros.
175 #define GEM5_FOR_EACH_IN_PACK(...) \
176 do { GEM5_VAR_USED int i[] = { 0, ((void)(__VA_ARGS__), 0)... }; } while (0)
177 
178 #else
179 # error "Don't know what to do for your compiler."
180 #endif
181 
182 // When a member variable may be unused, mark it with GEM5_CLASS_VAR_USED. This
183 // needs to be limitted to clang only since clang warns on these unused
184 // variables, and g++ will actually warn if you use this attribute since it
185 // won't do anything there.
186 #if defined(__clang__) // clang only.
187 # define GEM5_CLASS_VAR_USED GEM5_VAR_USED
188 #else
189 # define GEM5_CLASS_VAR_USED
190 #endif
191 
192 // Aliases for macros using the deprecated M5 prefix.
193 #define M5_VAR_USED GEM5_VAR_USED
194 #define M5_NODISCARD GEM5_NO_DISCARD
195 #define M5_FALLTHROUGH GEM5_FALLTHROUGH
196 #define M5_ATTR_PACKED GEM5_PACKED
197 #define M5_NO_INLINE GEM5_NO_INLINE
198 #define M5_PUBLIC GEM5_PUBLIC
199 #define M5_LOCAL GEM5_LOCAL
200 #define M5_WEAK GEM5_WEAK
201 #define M5_ALIGNED(x) GEM5_ALIGNED(x)
202 #define M5_UNREACHABLE GEM5_UNREACHABLE
203 #define M5_LIKELY(x) GEM5_LIKELY(x)
204 #define M5_UNLIKELY(x) GEM5_UNLIKELY(x)
205 #define M5_FOR_EACH_IN_PACK(...) GEM5_FOR_EACH_IN_PACK(__VA_ARGS__)
206 #define M5_CLASS_VAR_USED GEM5_CLASS_VAR_USED
207 
208 #endif // __BASE_COMPILER_HH__

Generated on Tue Sep 21 2021 12:24:56 for gem5 by doxygen 1.8.17