19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/StringRef.h"
35 return data.
SetData(GetBytes(), GetByteSize(), GetByteOrder()) > 0;
38 uint32_t RegisterValue::GetAsMemoryData(
const RegisterInfo ®_info,
void *dst,
44 if (GetType() == eTypeInvalid) {
46 error.SetErrorStringWithFormat(
47 "invalid register value type for register %s", reg_info.name);
51 if (dst_len > kMaxRegisterByteSize) {
52 error.SetErrorString(
"destination is too big");
56 const uint32_t src_len = reg_info.byte_size;
60 if (!GetData(reg_data)) {
61 error.SetErrorString(
"invalid register value to copy into");
72 if (bytes_copied == 0)
73 error.SetErrorStringWithFormat(
74 "failed to copy data for register write of %s", reg_info.name);
79 uint32_t RegisterValue::SetFromMemoryData(
const RegisterInfo ®_info,
99 if (src_len > kMaxRegisterByteSize) {
100 error.SetErrorStringWithFormat(
101 "register buffer is too small to receive %u bytes of data.", src_len);
105 const uint32_t dst_len = reg_info.byte_size;
107 if (src_len > dst_len) {
108 error.SetErrorStringWithFormat(
109 "%u bytes is too big to store in register %s (%u bytes)", src_len,
110 reg_info.name, dst_len);
118 error = SetValueFromData(reg_info, src_data, 0,
true);
126 bool RegisterValue::GetScalarValue(
Scalar &scalar)
const {
131 DataExtractor data(buffer.bytes, buffer.length, buffer.byte_order, 1);
143 case eTypeLongDouble:
150 void RegisterValue::Clear() { m_type = eTypeInvalid; }
157 Status error = SetValueFromData(reg_info, copy_data, 0,
true);
158 assert(
error.Success() &&
"Expected SetValueFromData to succeed.");
165 Status RegisterValue::SetValueFromData(
const RegisterInfo ®_info,
168 bool partial_data_ok) {
172 error.SetErrorString(
"empty data.");
176 if (reg_info.byte_size == 0) {
177 error.SetErrorString(
"invalid register info.");
183 if (!partial_data_ok && (src_len < reg_info.byte_size)) {
184 error.SetErrorString(
"not enough data.");
190 if (src_len > reg_info.byte_size)
191 src_len = reg_info.byte_size;
194 memset(buffer.bytes, 0,
sizeof(buffer.bytes));
198 m_type = eTypeInvalid;
199 switch (reg_info.encoding) {
204 if (reg_info.byte_size == 1)
205 SetUInt8(src.
GetMaxU32(&src_offset, src_len));
206 else if (reg_info.byte_size <= 2)
207 SetUInt16(src.
GetMaxU32(&src_offset, src_len));
208 else if (reg_info.byte_size <= 4)
209 SetUInt32(src.
GetMaxU32(&src_offset, src_len));
210 else if (reg_info.byte_size <= 8)
211 SetUInt64(src.
GetMaxU64(&src_offset, src_len));
212 else if (reg_info.byte_size <= 16) {
213 uint64_t data1 = src.
GetU64(&src_offset);
214 uint64_t data2 = src.
GetU64(&src_offset);
222 SetUInt128(llvm::APInt(128, 2, int128.x));
226 if (reg_info.byte_size ==
sizeof(
float))
227 SetFloat(src.
GetFloat(&src_offset));
228 else if (reg_info.byte_size ==
sizeof(
double))
230 else if (reg_info.byte_size ==
sizeof(
long double))
235 buffer.length = reg_info.byte_size;
237 assert(buffer.length <= kMaxRegisterByteSize);
238 if (buffer.length > kMaxRegisterByteSize)
239 buffer.length = kMaxRegisterByteSize;
245 buffer.byte_order) == 0)
247 error.SetErrorStringWithFormat(
248 "failed to copy data for register write of %s", reg_info.name);
254 if (m_type == eTypeInvalid)
255 error.SetErrorStringWithFormat(
256 "invalid register value type for register %s", reg_info.name);
262 llvm::StringRef vector_str,
267 vector_str = vector_str.trim();
268 vector_str.consume_front(
"{");
269 vector_str.consume_back(
"}");
270 vector_str = vector_str.trim();
278 llvm::StringRef cdr = vector_str;
279 std::tie(car, cdr) = vector_str.split(Sep);
280 std::vector<uint8_t> bytes;
286 while (!car.getAsInteger(0,
byte) && bytes.size() < byte_size) {
287 bytes.push_back(
byte);
288 std::tie(car, cdr) = cdr.split(Sep);
292 if (bytes.size() != byte_size)
300 size_t total_byte_size) {
301 if (total_byte_size > 8)
304 if (total_byte_size == 8)
308 (
static_cast<uint64_t
>(1) <<
static_cast<uint64_t
>(total_byte_size * 8)) -
310 return uval64 <= max;
314 size_t total_byte_size) {
315 if (total_byte_size > 8)
318 if (total_byte_size == 8)
321 const int64_t max = (
static_cast<int64_t
>(1)
322 <<
static_cast<uint64_t
>(total_byte_size * 8 - 1)) -
324 const int64_t min = ~(max);
325 return min <= sval64 && sval64 <= max;
328 Status RegisterValue::SetValueFromString(
const RegisterInfo *reg_info,
329 llvm::StringRef value_str) {
331 if (reg_info ==
nullptr) {
332 error.SetErrorString(
"Invalid register info argument.");
336 m_type = eTypeInvalid;
337 if (value_str.empty()) {
338 error.SetErrorString(
"Invalid c-string value string.");
341 const uint32_t byte_size = reg_info->byte_size;
347 long double ldbl_val;
348 switch (reg_info->encoding) {
350 error.SetErrorString(
"Invalid encoding.");
354 if (byte_size >
sizeof(uint64_t)) {
355 error.SetErrorStringWithFormat(
356 "unsupported unsigned integer byte size: %u", byte_size);
359 if (value_str.getAsInteger(0, uval64)) {
360 error.SetErrorStringWithFormat(
361 "'%s' is not a valid unsigned integer string value",
362 value_str.str().c_str());
367 error.SetErrorStringWithFormat(
369 " is too large to fit in a %u byte unsigned integer value",
374 if (!SetUInt(uval64, reg_info->byte_size)) {
375 error.SetErrorStringWithFormat(
376 "unsupported unsigned integer byte size: %u", byte_size);
382 if (byte_size >
sizeof(
long long)) {
383 error.SetErrorStringWithFormat(
"unsupported signed integer byte size: %u",
388 if (value_str.getAsInteger(0, ival64)) {
389 error.SetErrorStringWithFormat(
390 "'%s' is not a valid signed integer string value",
391 value_str.str().c_str());
396 error.SetErrorStringWithFormat(
398 " is too large to fit in a %u byte signed integer value",
403 if (!SetUInt(ival64, reg_info->byte_size)) {
404 error.SetErrorStringWithFormat(
"unsupported signed integer byte size: %u",
412 if (byte_size ==
sizeof(
float)) {
413 if (::sscanf(value_string.c_str(),
"%f", &flt_val) != 1) {
414 error.SetErrorStringWithFormat(
"'%s' is not a valid float string value",
415 value_string.c_str());
420 }
else if (byte_size ==
sizeof(
double)) {
421 if (::sscanf(value_string.c_str(),
"%lf", &dbl_val) != 1) {
422 error.SetErrorStringWithFormat(
"'%s' is not a valid float string value",
423 value_string.c_str());
427 m_type = eTypeDouble;
428 }
else if (byte_size ==
sizeof(
long double)) {
429 if (::sscanf(value_string.c_str(),
"%Lf", &ldbl_val) != 1) {
430 error.SetErrorStringWithFormat(
"'%s' is not a valid float string value",
431 value_string.c_str());
435 m_type = eTypeLongDouble;
437 error.SetErrorStringWithFormat(
"unsupported float byte size: %u",
445 error.SetErrorString(
"unrecognized vector encoding string value.");
452 bool RegisterValue::SignExtend(
uint32_t sign_bitpos) {
462 return m_scalar.SignExtend(sign_bitpos);
465 case eTypeLongDouble:
474 return rhs.
m_type != eTypeInvalid;
487 case eTypeLongDouble:
492 ::memcpy(buffer.bytes, rhs.
buffer.
bytes, kMaxRegisterByteSize);
501 bool *success_ptr)
const {
510 return m_scalar.UShort(fail_value);
512 switch (buffer.length) {
517 return *
reinterpret_cast<const uint16_t *
>(buffer.bytes);
522 *success_ptr =
false;
527 bool *success_ptr)
const {
538 case eTypeLongDouble:
539 return m_scalar.UInt(fail_value);
541 switch (buffer.length) {
547 return *
reinterpret_cast<const uint32_t *
>(buffer.bytes);
552 *success_ptr =
false;
556 uint64_t RegisterValue::GetAsUInt64(uint64_t fail_value,
557 bool *success_ptr)
const {
569 case eTypeLongDouble:
570 return m_scalar.ULongLong(fail_value);
572 switch (buffer.length) {
576 return *(
const uint8_t *)buffer.bytes;
578 return *
reinterpret_cast<const uint16_t *
>(buffer.bytes);
580 return *
reinterpret_cast<const uint32_t *
>(buffer.bytes);
582 return *
reinterpret_cast<const uint64_t *
>(buffer.bytes);
587 *success_ptr =
false;
591 llvm::APInt RegisterValue::GetAsUInt128(
const llvm::APInt &fail_value,
592 bool *success_ptr)
const {
605 case eTypeLongDouble:
606 return m_scalar.UInt128(fail_value);
608 switch (buffer.length) {
617 (
reinterpret_cast<const type128 *
>(buffer.bytes))->x);
622 *success_ptr =
false;
626 float RegisterValue::GetAsFloat(
float fail_value,
bool *success_ptr)
const {
637 case eTypeLongDouble:
638 return m_scalar.Float(fail_value);
641 *success_ptr =
false;
645 double RegisterValue::GetAsDouble(
double fail_value,
bool *success_ptr)
const {
657 case eTypeLongDouble:
658 return m_scalar.Double(fail_value);
661 *success_ptr =
false;
665 long double RegisterValue::GetAsLongDouble(
long double fail_value,
666 bool *success_ptr)
const {
678 case eTypeLongDouble:
679 return m_scalar.LongDouble();
682 *success_ptr =
false;
686 const void *RegisterValue::GetBytes()
const {
697 case eTypeLongDouble:
698 m_scalar.GetBytes(buffer.bytes);
719 case eTypeLongDouble:
720 return m_scalar.GetByteSize();
722 return buffer.length;
727 bool RegisterValue::SetUInt(uint64_t uint,
uint32_t byte_size) {
728 if (byte_size == 0) {
730 }
else if (byte_size == 1) {
732 }
else if (byte_size <= 2) {
734 }
else if (byte_size <= 4) {
736 }
else if (byte_size <= 8) {
738 }
else if (byte_size <= 16) {
739 SetUInt128(llvm::APInt(128, uint));
745 void RegisterValue::SetBytes(
const void *bytes,
size_t length,
750 if (bytes && length > 0) {
751 assert(length <=
sizeof(buffer.bytes) &&
752 "Storing too many bytes in a RegisterValue.");
754 buffer.length = length;
755 memcpy(buffer.bytes, bytes, length);
756 buffer.byte_order = byte_order;
758 m_type = eTypeInvalid;
764 if (m_type == rhs.
m_type) {
775 case eTypeLongDouble:
782 if (length > kMaxRegisterByteSize)
783 length = kMaxRegisterByteSize;
784 return memcmp(buffer.bytes, rhs.
buffer.
bytes, length) == 0;
793 return !(*
this == rhs);
806 if (
bit < (GetByteSize() * 8)) {
807 return m_scalar.ClearBit(
bit);
813 case eTypeLongDouble:
821 byte_idx = buffer.length - (
bit / 8) - 1;
826 if (byte_idx < buffer.length) {
827 buffer.bytes[byte_idx] &= ~(1u << byte_bit);
846 if (
bit < (GetByteSize() * 8)) {
847 return m_scalar.SetBit(
bit);
853 case eTypeLongDouble:
861 byte_idx = buffer.length - (
bit / 8) - 1;
866 if (byte_idx < buffer.length) {
867 buffer.bytes[byte_idx] |= (1u << byte_bit);