1 //===-- RegisterValue.h -----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLDB_UTILITY_REGISTERVALUE_H
11 #define LLDB_UTILITY_REGISTERVALUE_H
13 #include "lldb/Utility/Endian.h"
14 #include "lldb/Utility/Scalar.h"
15 #include "lldb/Utility/Status.h"
16 #include "lldb/lldb-enumerations.h"
17 #include "lldb/lldb-types.h"
18 #include "llvm/ADT/APInt.h"
19 #include "llvm/ADT/StringRef.h"
23 namespace lldb_private {
30 enum { kMaxRegisterByteSize = 64u };
45 RegisterValue() : m_type(eTypeInvalid), m_scalar((unsigned long)0) {}
47 explicit RegisterValue(uint8_t inst) : m_type(eTypeUInt8) { m_scalar = inst; }
49 explicit RegisterValue(uint16_t inst) : m_type(eTypeUInt16) {
53 explicit RegisterValue(uint32_t inst) : m_type(eTypeUInt32) {
57 explicit RegisterValue(uint64_t inst) : m_type(eTypeUInt64) {
61 explicit RegisterValue(llvm::APInt inst) : m_type(eTypeUInt128) {
62 m_scalar = llvm::APInt(inst);
65 explicit RegisterValue(float value) : m_type(eTypeFloat) { m_scalar = value; }
67 explicit RegisterValue(double value) : m_type(eTypeDouble) {
71 explicit RegisterValue(long double value) : m_type(eTypeLongDouble) {
75 explicit RegisterValue(uint8_t *bytes, size_t length,
76 lldb::ByteOrder byte_order) {
77 SetBytes(bytes, length, byte_order);
80 RegisterValue::Type GetType() const { return m_type; }
82 bool CopyValue(const RegisterValue &rhs);
84 void SetType(RegisterValue::Type type) { m_type = type; }
86 RegisterValue::Type SetType(const RegisterInfo *reg_info);
88 bool GetData(DataExtractor &data) const;
90 // Copy the register value from this object into a buffer in "dst" and obey
91 // the "dst_byte_order" when copying the data. Also watch out in case
92 // "dst_len" is longer or shorter than the register value described by
93 // "reg_info" and only copy the least significant bytes of the register
94 // value, or pad the destination with zeroes if the register byte size is
95 // shorter that "dst_len" (all while correctly abiding the "dst_byte_order").
96 // Returns the number of bytes copied into "dst".
97 uint32_t GetAsMemoryData(const RegisterInfo *reg_info, void *dst,
98 uint32_t dst_len, lldb::ByteOrder dst_byte_order,
101 uint32_t SetFromMemoryData(const RegisterInfo *reg_info, const void *src,
102 uint32_t src_len, lldb::ByteOrder src_byte_order,
105 bool GetScalarValue(Scalar &scalar) const;
107 uint8_t GetAsUInt8(uint8_t fail_value = UINT8_MAX,
108 bool *success_ptr = nullptr) const {
109 if (m_type == eTypeUInt8) {
112 return m_scalar.UChar(fail_value);
119 uint16_t GetAsUInt16(uint16_t fail_value = UINT16_MAX,
120 bool *success_ptr = nullptr) const;
122 uint32_t GetAsUInt32(uint32_t fail_value = UINT32_MAX,
123 bool *success_ptr = nullptr) const;
125 uint64_t GetAsUInt64(uint64_t fail_value = UINT64_MAX,
126 bool *success_ptr = nullptr) const;
128 llvm::APInt GetAsUInt128(const llvm::APInt &fail_value,
129 bool *success_ptr = nullptr) const;
131 float GetAsFloat(float fail_value = 0.0f, bool *success_ptr = nullptr) const;
133 double GetAsDouble(double fail_value = 0.0,
134 bool *success_ptr = nullptr) const;
136 long double GetAsLongDouble(long double fail_value = 0.0,
137 bool *success_ptr = nullptr) const;
139 void SetValueToInvalid() { m_type = eTypeInvalid; }
141 bool ClearBit(uint32_t bit);
143 bool SetBit(uint32_t bit);
145 bool operator==(const RegisterValue &rhs) const;
147 bool operator!=(const RegisterValue &rhs) const;
149 void operator=(uint8_t uint) {
154 void operator=(uint16_t uint) {
155 m_type = eTypeUInt16;
159 void operator=(uint32_t uint) {
160 m_type = eTypeUInt32;
164 void operator=(uint64_t uint) {
165 m_type = eTypeUInt64;
169 void operator=(llvm::APInt uint) {
170 m_type = eTypeUInt128;
171 m_scalar = llvm::APInt(uint);
174 void operator=(float f) {
179 void operator=(double f) {
180 m_type = eTypeDouble;
184 void operator=(long double f) {
185 m_type = eTypeLongDouble;
189 void SetUInt8(uint8_t uint) {
194 void SetUInt16(uint16_t uint) {
195 m_type = eTypeUInt16;
199 void SetUInt32(uint32_t uint, Type t = eTypeUInt32) {
204 void SetUInt64(uint64_t uint, Type t = eTypeUInt64) {
209 void SetUInt128(llvm::APInt uint) {
210 m_type = eTypeUInt128;
214 bool SetUInt(uint64_t uint, uint32_t byte_size);
216 void SetFloat(float f) {
221 void SetDouble(double f) {
222 m_type = eTypeDouble;
226 void SetLongDouble(long double f) {
227 m_type = eTypeLongDouble;
231 void SetBytes(const void *bytes, size_t length, lldb::ByteOrder byte_order);
233 bool SignExtend(uint32_t sign_bitpos);
235 Status SetValueFromString(const RegisterInfo *reg_info,
236 llvm::StringRef value_str);
237 Status SetValueFromString(const RegisterInfo *reg_info,
238 const char *value_str) = delete;
240 Status SetValueFromData(const RegisterInfo *reg_info, DataExtractor &data,
241 lldb::offset_t offset, bool partial_data_ok);
243 const void *GetBytes() const;
245 lldb::ByteOrder GetByteOrder() const {
246 if (m_type == eTypeBytes)
247 return buffer.byte_order;
248 return endian::InlHostByteOrder();
251 uint32_t GetByteSize() const;
253 static uint32_t GetMaxByteSize() { return kMaxRegisterByteSize; }
258 RegisterValue::Type m_type;
262 uint8_t bytes[kMaxRegisterByteSize]; // This must be big enough to hold any
263 // register for any supported target.
265 lldb::ByteOrder byte_order;
269 } // namespace lldb_private
271 #endif // LLDB_UTILITY_REGISTERVALUE_H