gem5  v21.2.0.0
inst_res.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2017 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  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions are
16  * met: redistributions of source code must retain the above copyright
17  * notice, this list of conditions and the following disclaimer;
18  * redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution;
21  * neither the name of the copyright holders nor the names of its
22  * contributors may be used to endorse or promote products derived from
23  * this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #ifndef __CPU_INST_RES_HH__
39 #define __CPU_INST_RES_HH__
40 
41 #include <any>
42 #include <type_traits>
43 
44 #include "base/logging.hh"
45 #include "base/types.hh"
46 
47 namespace gem5
48 {
49 
51 {
52  private:
53  std::any result;
54  std::function<bool(const std::any &a, const std::any &b)> equals;
55 
56  public:
59  // This InstResult is empty, and will only equal other InstResults
60  // which are also empty.
61  equals([](const std::any &a, const std::any &b) -> bool {
62  gem5_assert(!a.has_value());
63  return !b.has_value();
64  })
65  {}
66  InstResult(const InstResult &) = default;
67 
68  template <typename T>
69  explicit InstResult(T val) : result(val),
70 
71  // Set equals so it knows how to compare results of type T.
72  equals([](const std::any &a, const std::any &b) -> bool {
73  // If one has a value but the other doesn't, not equal.
74  if (a.has_value() != b.has_value())
75  return false;
76  // If they are both empty, equal.
77  if (!a.has_value())
78  return true;
79  // At least the local object should be of the right type.
80  gem5_assert(a.type() == typeid(T));
81  // If these aren't the same type, not equal.
82  if (a.type() != b.type())
83  return false;
84  // We now know these both hold a result of the right type.
85  return std::any_cast<const T&>(a) == std::any_cast<const T&>(b);
86  })
87  {
88  static_assert(!std::is_pointer_v<T>,
89  "InstResult shouldn't point to external data.");
90  // Floating point values should be converted to/from ints using
91  // floatToBits and bitsToFloat, and not stored in InstResult directly.
92  static_assert(!std::is_floating_point_v<T>,
93  "Floating point values should be converted to/from ints.");
94  }
95 
96  // Convert floating point values to integers.
97  template <typename T,
98  std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
100 
101  // Convert all integer types to RegVal.
102  template <typename T,
103  std::enable_if_t<std::is_integral_v<T> && !std::is_same_v<T, RegVal>,
104  int> = 0>
105  explicit InstResult(T val) : InstResult(static_cast<RegVal>(val)) {}
106 
107  InstResult &
108  operator=(const InstResult& that)
109  {
110  result = that.result;
111  equals = that.equals;
112  return *this;
113  }
114 
119  bool
120  operator==(const InstResult& that) const
121  {
122  return equals(result, that.result);
123  }
124 
125  bool
126  operator!=(const InstResult& that) const
127  {
128  return !operator==(that);
129  }
130 
134  template <typename T>
135  bool
136  is() const
137  {
138  static_assert(!std::is_floating_point_v<T>,
139  "Floating point values should be converted to/from ints.");
140  return result.type() == typeid(T);
141  }
142 
143  template <typename T>
144  std::enable_if_t<std::is_integral_v<T> && !std::is_same_v<T, RegVal>, bool>
145  is() const
146  {
147  return is<RegVal>();
148  }
149 
151  bool isValid() const { return result.has_value(); }
156  template <typename T>
157  T
158  as() const
159  {
160  assert(is<T>());
161  return std::any_cast<T>(result);
162  }
163 
164  template <typename T>
165  std::enable_if_t<std::is_integral_v<T> && !std::is_same_v<T, RegVal>,
166  RegVal>
167  as() const
168  {
169  return as<RegVal>();
170  }
171 
176  template <typename T>
177  T
178  asNoAssert() const
179  {
180  if (!is<T>())
181  return T{};
182  return as<T>();
183  }
184 
186 };
187 
188 } // namespace gem5
189 
190 #endif // __CPU_INST_RES_HH__
gem5::RegVal
uint64_t RegVal
Definition: types.hh:173
gem5::InstResult::operator!=
bool operator!=(const InstResult &that) const
Definition: inst_res.hh:126
gem5::InstResult::is
std::enable_if_t< std::is_integral_v< T > &&!std::is_same_v< T, RegVal >, bool > is() const
Definition: inst_res.hh:145
gem5::InstResult::operator==
bool operator==(const InstResult &that) const
Result comparison Two invalid results always differ.
Definition: inst_res.hh:120
gem5::X86ISA::val
Bitfield< 63 > val
Definition: misc.hh:775
gem5::ArmISA::a
Bitfield< 8 > a
Definition: misc_types.hh:66
gem5::InstResult::InstResult
InstResult()
Default constructor creates an invalid result.
Definition: inst_res.hh:58
gem5::InstResult::is
bool is() const
Checks.
Definition: inst_res.hh:136
gem5::InstResult::as
T as() const
Explicit cast-like operations.
Definition: inst_res.hh:158
gem5::floatToBits
static uint64_t floatToBits(double val)
Definition: types.hh:209
gem5::ArmISA::b
Bitfield< 7 > b
Definition: misc_types.hh:382
gem5::InstResult::isValid
bool isValid() const
Is this a valid result?.
Definition: inst_res.hh:151
gem5::InstResult::equals
std::function< bool(const std::any &a, const std::any &b)> equals
Definition: inst_res.hh:54
gem5::InstResult::as
std::enable_if_t< std::is_integral_v< T > &&!std::is_same_v< T, RegVal >, RegVal > as() const
Definition: inst_res.hh:167
gem5::InstResult::result
std::any result
Definition: inst_res.hh:53
gem5::InstResult::InstResult
InstResult(T val)
Definition: inst_res.hh:99
gem5::InstResult::operator=
InstResult & operator=(const InstResult &that)
Definition: inst_res.hh:108
std
Overload hash function for BasicBlockRange type.
Definition: types.hh:111
types.hh
logging.hh
gem5::InstResult::asNoAssert
T asNoAssert() const
Cast to integer without checking type.
Definition: inst_res.hh:178
gem5_assert
#define gem5_assert(cond,...)
The assert macro will function like a normal assert, but will use panic instead of straight abort().
Definition: logging.hh:318
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: tlb.cc:60
gem5::InstResult
Definition: inst_res.hh:50
gem5::InstResult::InstResult
InstResult(T val)
Definition: inst_res.hh:69

Generated on Tue Dec 21 2021 11:34:25 for gem5 by doxygen 1.8.17