gem5  v22.1.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 <cstdint>
42 #include <cstring>
43 #include <memory>
44 #include <string>
45 #include <variant>
46 
47 #include "base/logging.hh"
48 #include "base/types.hh"
49 #include "cpu/reg_class.hh"
50 
51 namespace gem5
52 {
53 
55 {
56  private:
57  using BlobPtr = std::unique_ptr<const uint8_t[]>;
58 
59  std::variant<BlobPtr, RegVal> value;
60  const RegClass *_regClass = nullptr;
61 
62  bool blob() const { return std::holds_alternative<BlobPtr>(value); }
63  bool valid() const { return _regClass != nullptr; }
64 
65  // Raw accessors with no safety checks.
66  RegVal getRegVal() const { return std::get<RegVal>(value); }
67  const void *getBlob() const { return std::get<BlobPtr>(value).get(); }
68 
69  // Store copies of blobs, not a pointer to the original.
70  void
71  set(const void *val)
72  {
73  uint8_t *temp = nullptr;
74  if (val) {
75  const size_t size = _regClass->regBytes();
76  temp = new uint8_t[size];
77  std::memcpy(temp, val, size);
78  }
79  value = BlobPtr(temp);
80  }
81 
82  void set(RegVal val) { value = val; }
83 
84  void
85  set(const InstResult &other)
86  {
87  other.blob() ? set(other.getBlob()) : set(other.getRegVal());
88  }
89 
90  public:
93  InstResult(const InstResult &other) : _regClass(other._regClass)
94  {
95  set(other);
96  }
97 
98  InstResult(const RegClass &reg_class, RegVal val) :
99  _regClass(&reg_class)
100  {
101  set(val);
102  }
103 
104  InstResult(const RegClass &reg_class, const void *val) :
105  _regClass(&reg_class)
106  {
107  set(val);
108  }
109 
110  InstResult &
111  operator=(const InstResult &that)
112  {
113  _regClass = that._regClass;
114  set(that);
115 
116  return *this;
117  }
118 
123  bool
124  operator==(const InstResult& that) const
125  {
126  if (blob() != that.blob() || _regClass != that._regClass)
127  return false;
128 
129  if (blob()) {
130  const void *my_blob = getBlob();
131  const void *their_blob = that.getBlob();
132 
133  // Invalid results always differ.
134  if (!my_blob || !their_blob)
135  return false;
136 
137  // Check the contents of the blobs, not their addresses.
138  return std::memcmp(getBlob(), that.getBlob(),
139  _regClass->regBytes()) == 0;
140  } else {
141  return getRegVal() == that.getRegVal();
142  }
143  }
144 
145  bool
146  operator!=(const InstResult& that) const
147  {
148  return !operator==(that);
149  }
150 
151  const RegClass &regClass() const { return *_regClass; }
152  bool isValid() const { return valid(); }
153  bool isBlob() const { return blob(); }
154 
155  RegVal
156  asRegVal() const
157  {
158  assert(!blob());
159  return getRegVal();
160  }
161 
162  const void *
163  asBlob() const
164  {
165  assert(blob());
166  return getBlob();
167  }
168 
169  std::string
170  asString() const
171  {
172  if (blob()) {
173  return _regClass->valString(getBlob());
174  } else {
175  RegVal reg = getRegVal();
176  return _regClass->valString(&reg);
177  }
178  }
179 };
180 
181 } // namespace gem5
182 
183 #endif // __CPU_INST_RES_HH__
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
InstResult()
Default constructor creates an invalid result.
Definition: inst_res.hh:92
bool isValid() const
Definition: inst_res.hh:152
bool operator==(const InstResult &that) const
Result comparison Two invalid results always differ.
Definition: inst_res.hh:124
void set(RegVal val)
Definition: inst_res.hh:82
InstResult(const InstResult &other)
Definition: inst_res.hh:93
const RegClass & regClass() const
Definition: inst_res.hh:151
void set(const void *val)
Definition: inst_res.hh:71
RegVal getRegVal() const
Definition: inst_res.hh:66
void set(const InstResult &other)
Definition: inst_res.hh:85
RegVal asRegVal() const
Definition: inst_res.hh:156
std::variant< BlobPtr, RegVal > value
Definition: inst_res.hh:59
InstResult & operator=(const InstResult &that)
Definition: inst_res.hh:111
const void * asBlob() const
Definition: inst_res.hh:163
bool operator!=(const InstResult &that) const
Definition: inst_res.hh:146
const RegClass * _regClass
Definition: inst_res.hh:60
std::string asString() const
Definition: inst_res.hh:170
InstResult(const RegClass &reg_class, const void *val)
Definition: inst_res.hh:104
std::unique_ptr< const uint8_t[]> BlobPtr
Definition: inst_res.hh:57
const void * getBlob() const
Definition: inst_res.hh:67
InstResult(const RegClass &reg_class, RegVal val)
Definition: inst_res.hh:98
bool valid() const
Definition: inst_res.hh:63
bool isBlob() const
Definition: inst_res.hh:153
bool blob() const
Definition: inst_res.hh:62
std::string valString(const void *val) const
Definition: reg_class.hh:243
constexpr size_t regBytes() const
Definition: reg_class.hh:236
Bitfield< 5, 3 > reg
Definition: types.hh:92
Bitfield< 63 > val
Definition: misc.hh:776
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
uint64_t RegVal
Definition: types.hh:173

Generated on Wed Dec 21 2022 10:22:30 for gem5 by doxygen 1.9.1