LLDB  mainline
RegisterValue.h
Go to the documentation of this file.
1 //===-- RegisterValue.h -----------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_UTILITY_REGISTERVALUE_H
10 #define LLDB_UTILITY_REGISTERVALUE_H
11 
12 #include "lldb/Utility/Endian.h"
13 #include "lldb/Utility/Scalar.h"
14 #include "lldb/Utility/Status.h"
15 #include "lldb/lldb-enumerations.h"
16 #include "lldb/lldb-types.h"
17 #include "llvm/ADT/APInt.h"
18 #include "llvm/ADT/StringRef.h"
19 #include <cstdint>
20 #include <cstring>
21 
22 namespace lldb_private {
23 class DataExtractor;
24 class Stream;
25 struct RegisterInfo;
26 
28 public:
29  enum { kMaxRegisterByteSize = 64u };
30 
31  enum Type {
42  };
43 
44  RegisterValue() : m_type(eTypeInvalid), m_scalar((unsigned long)0) {}
45 
46  explicit RegisterValue(uint8_t inst) : m_type(eTypeUInt8) { m_scalar = inst; }
47 
49  m_scalar = inst;
50  }
51 
53  m_scalar = inst;
54  }
55 
56  explicit RegisterValue(uint64_t inst) : m_type(eTypeUInt64) {
57  m_scalar = inst;
58  }
59 
60  explicit RegisterValue(llvm::APInt inst) : m_type(eTypeUInt128) {
61  m_scalar = llvm::APInt(inst);
62  }
63 
64  explicit RegisterValue(float value) : m_type(eTypeFloat) { m_scalar = value; }
65 
66  explicit RegisterValue(double value) : m_type(eTypeDouble) {
67  m_scalar = value;
68  }
69 
70  explicit RegisterValue(long double value) : m_type(eTypeLongDouble) {
71  m_scalar = value;
72  }
73 
74  explicit RegisterValue(uint8_t *bytes, size_t length,
76  SetBytes(bytes, length, byte_order);
77  }
78 
79  RegisterValue::Type GetType() const { return m_type; }
80 
81  bool CopyValue(const RegisterValue &rhs);
82 
83  void SetType(RegisterValue::Type type) { m_type = type; }
84 
85  RegisterValue::Type SetType(const RegisterInfo *reg_info);
86 
87  bool GetData(DataExtractor &data) const;
88 
89  // Copy the register value from this object into a buffer in "dst" and obey
90  // the "dst_byte_order" when copying the data. Also watch out in case
91  // "dst_len" is longer or shorter than the register value described by
92  // "reg_info" and only copy the least significant bytes of the register
93  // value, or pad the destination with zeroes if the register byte size is
94  // shorter that "dst_len" (all while correctly abiding the "dst_byte_order").
95  // Returns the number of bytes copied into "dst".
96  uint32_t GetAsMemoryData(const RegisterInfo *reg_info, void *dst,
97  uint32_t dst_len, lldb::ByteOrder dst_byte_order,
98  Status &error) const;
99 
100  uint32_t SetFromMemoryData(const RegisterInfo *reg_info, const void *src,
101  uint32_t src_len, lldb::ByteOrder src_byte_order,
102  Status &error);
103 
104  bool GetScalarValue(Scalar &scalar) const;
105 
106  uint8_t GetAsUInt8(uint8_t fail_value = UINT8_MAX,
107  bool *success_ptr = nullptr) const {
108  if (m_type == eTypeUInt8) {
109  if (success_ptr)
110  *success_ptr = true;
111  return m_scalar.UChar(fail_value);
112  }
113  if (success_ptr)
114  *success_ptr = true;
115  return fail_value;
116  }
117 
118  uint16_t GetAsUInt16(uint16_t fail_value = UINT16_MAX,
119  bool *success_ptr = nullptr) const;
120 
122  bool *success_ptr = nullptr) const;
123 
124  uint64_t GetAsUInt64(uint64_t fail_value = UINT64_MAX,
125  bool *success_ptr = nullptr) const;
126 
127  llvm::APInt GetAsUInt128(const llvm::APInt &fail_value,
128  bool *success_ptr = nullptr) const;
129 
130  float GetAsFloat(float fail_value = 0.0f, bool *success_ptr = nullptr) const;
131 
132  double GetAsDouble(double fail_value = 0.0,
133  bool *success_ptr = nullptr) const;
134 
135  long double GetAsLongDouble(long double fail_value = 0.0,
136  bool *success_ptr = nullptr) const;
137 
139 
140  bool ClearBit(uint32_t bit);
141 
142  bool SetBit(uint32_t bit);
143 
144  bool operator==(const RegisterValue &rhs) const;
145 
146  bool operator!=(const RegisterValue &rhs) const;
147 
148  void operator=(uint8_t uint) {
149  m_type = eTypeUInt8;
150  m_scalar = uint;
151  }
152 
153  void operator=(uint16_t uint) {
155  m_scalar = uint;
156  }
157 
158  void operator=(uint32_t uint) {
160  m_scalar = uint;
161  }
162 
163  void operator=(uint64_t uint) {
165  m_scalar = uint;
166  }
167 
168  void operator=(llvm::APInt uint) {
170  m_scalar = llvm::APInt(uint);
171  }
172 
173  void operator=(float f) {
174  m_type = eTypeFloat;
175  m_scalar = f;
176  }
177 
178  void operator=(double f) {
180  m_scalar = f;
181  }
182 
183  void operator=(long double f) {
185  m_scalar = f;
186  }
187 
188  void SetUInt8(uint8_t uint) {
189  m_type = eTypeUInt8;
190  m_scalar = uint;
191  }
192 
193  void SetUInt16(uint16_t uint) {
195  m_scalar = uint;
196  }
197 
198  void SetUInt32(uint32_t uint, Type t = eTypeUInt32) {
199  m_type = t;
200  m_scalar = uint;
201  }
202 
203  void SetUInt64(uint64_t uint, Type t = eTypeUInt64) {
204  m_type = t;
205  m_scalar = uint;
206  }
207 
208  void SetUInt128(llvm::APInt uint) {
210  m_scalar = uint;
211  }
212 
213  bool SetUInt(uint64_t uint, uint32_t byte_size);
214 
215  void SetFloat(float f) {
216  m_type = eTypeFloat;
217  m_scalar = f;
218  }
219 
220  void SetDouble(double f) {
222  m_scalar = f;
223  }
224 
225  void SetLongDouble(long double f) {
227  m_scalar = f;
228  }
229 
230  void SetBytes(const void *bytes, size_t length, lldb::ByteOrder byte_order);
231 
232  bool SignExtend(uint32_t sign_bitpos);
233 
234  Status SetValueFromString(const RegisterInfo *reg_info,
235  llvm::StringRef value_str);
236  Status SetValueFromString(const RegisterInfo *reg_info,
237  const char *value_str) = delete;
238 
239  Status SetValueFromData(const RegisterInfo *reg_info, DataExtractor &data,
240  lldb::offset_t offset, bool partial_data_ok);
241 
242  const void *GetBytes() const;
243 
245  if (m_type == eTypeBytes)
246  return buffer.byte_order;
247  return endian::InlHostByteOrder();
248  }
249 
250  uint32_t GetByteSize() const;
251 
253 
254  void Clear();
255 
256 protected:
259 
260  struct {
261  uint8_t bytes[kMaxRegisterByteSize]; // This must be big enough to hold any
262  // register for any supported target.
263  uint8_t length;
265  } buffer;
266 };
267 
268 } // namespace lldb_private
269 
270 #endif // LLDB_UTILITY_REGISTERVALUE_H
void operator=(llvm::APInt uint)
bool ClearBit(uint32_t bit)
struct lldb_private::RegisterValue::@41 buffer
An data extractor class.
Definition: DataExtractor.h:47
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
bool CopyValue(const RegisterValue &rhs)
RegisterValue::Type m_type
uint32_t GetAsMemoryData(const RegisterInfo *reg_info, void *dst, uint32_t dst_len, lldb::ByteOrder dst_byte_order, Status &error) const
RegisterValue(long double value)
Definition: RegisterValue.h:70
RegisterValue(uint64_t inst)
Definition: RegisterValue.h:56
bool operator==(const RegisterValue &rhs) const
void SetUInt8(uint8_t uint)
uint64_t GetAsUInt64(uint64_t fail_value=UINT64_MAX, bool *success_ptr=nullptr) const
void SetUInt64(uint64_t uint, Type t=eTypeUInt64)
void operator=(uint32_t uint)
Status SetValueFromString(const RegisterInfo *reg_info, llvm::StringRef value_str)
bool SetUInt(uint64_t uint, uint32_t byte_size)
void SetUInt16(uint16_t uint)
void SetLongDouble(long double f)
#define UINT32_MAX
Definition: lldb-defines.h:31
void operator=(long double f)
static uint32_t bit(const uint32_t val, const uint32_t msbit)
Definition: ARMUtils.h:269
uint64_t offset_t
Definition: lldb-types.h:87
RegisterValue(uint8_t *bytes, size_t length, lldb::ByteOrder byte_order)
Definition: RegisterValue.h:74
uint16_t GetAsUInt16(uint16_t fail_value=UINT16_MAX, bool *success_ptr=nullptr) const
long double GetAsLongDouble(long double fail_value=0.0, bool *success_ptr=nullptr) const
RegisterValue(uint32_t inst)
Definition: RegisterValue.h:52
const void * GetBytes() const
Status SetValueFromData(const RegisterInfo *reg_info, DataExtractor &data, lldb::offset_t offset, bool partial_data_ok)
lldb::ByteOrder GetByteOrder() const
uint8_t bytes[kMaxRegisterByteSize]
RegisterValue(llvm::APInt inst)
Definition: RegisterValue.h:60
void operator=(uint8_t uint)
RegisterValue::Type GetType() const
Definition: RegisterValue.h:79
void SetUInt32(uint32_t uint, Type t=eTypeUInt32)
static uint32_t GetMaxByteSize()
uint32_t GetAsUInt32(uint32_t fail_value=UINT32_MAX, bool *success_ptr=nullptr) const
double GetAsDouble(double fail_value=0.0, bool *success_ptr=nullptr) const
unsigned char UChar(unsigned char fail_value=0) const
Definition: Scalar.cpp:1336
bool GetData(DataExtractor &data) const
void operator=(uint16_t uint)
void SetBytes(const void *bytes, size_t length, lldb::ByteOrder byte_order)
uint8_t GetAsUInt8(uint8_t fail_value=UINT8_MAX, bool *success_ptr=nullptr) const
llvm::APInt GetAsUInt128(const llvm::APInt &fail_value, bool *success_ptr=nullptr) const
bool GetScalarValue(Scalar &scalar) const
void SetUInt128(llvm::APInt uint)
lldb::ByteOrder InlHostByteOrder()
Definition: Endian.h:25
bool SignExtend(uint32_t sign_bitpos)
uint32_t SetFromMemoryData(const RegisterInfo *reg_info, const void *src, uint32_t src_len, lldb::ByteOrder src_byte_order, Status &error)
void operator=(uint64_t uint)
bool operator!=(const RegisterValue &rhs) const
#define UINT64_MAX
Definition: lldb-defines.h:35
float GetAsFloat(float fail_value=0.0f, bool *success_ptr=nullptr) const
RegisterValue(uint16_t inst)
Definition: RegisterValue.h:48
void SetType(RegisterValue::Type type)
Definition: RegisterValue.h:83
An error handling class.
Definition: Status.h:44