]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Core/RegisterValue.cpp
Import OpenCSD -- an ARM CoreSight(tm) Trace Decode Library.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Core / RegisterValue.cpp
1 //===-- RegisterValue.cpp ---------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Core/RegisterValue.h"
11
12 #include "lldb/Core/DumpDataExtractor.h"
13 #include "lldb/Core/Scalar.h"
14 #include "lldb/Interpreter/Args.h"
15 #include "lldb/Utility/DataExtractor.h"
16 #include "lldb/Utility/Status.h"
17 #include "lldb/Utility/Stream.h"
18 #include "lldb/Utility/StreamString.h"
19 #include "lldb/lldb-defines.h"       // for LLDB_INVALID_ADDRESS
20 #include "lldb/lldb-private-types.h" // for RegisterInfo, type128
21
22 #include "llvm/ADT/ArrayRef.h"
23 #include "llvm/ADT/StringRef.h"
24
25 #include <cstdint> // for uint8_t, uint32_t, uint64_t
26 #include <string>  // for string
27 #include <tuple>   // for tie, tuple
28 #include <vector>
29
30 #include <assert.h>   // for assert
31 #include <inttypes.h> // for PRIx64
32 #include <stdio.h>    // for sscanf
33
34 using namespace lldb;
35 using namespace lldb_private;
36
37 bool RegisterValue::Dump(Stream *s, const RegisterInfo *reg_info,
38                          bool prefix_with_name, bool prefix_with_alt_name,
39                          Format format,
40                          uint32_t reg_name_right_align_at) const {
41   DataExtractor data;
42   if (GetData(data)) {
43     bool name_printed = false;
44     // For simplicity, alignment of the register name printing applies only
45     // in the most common case where:
46     //
47     //     prefix_with_name^prefix_with_alt_name is true
48     //
49     StreamString format_string;
50     if (reg_name_right_align_at && (prefix_with_name ^ prefix_with_alt_name))
51       format_string.Printf("%%%us", reg_name_right_align_at);
52     else
53       format_string.Printf("%%s");
54     std::string fmt = format_string.GetString();
55     if (prefix_with_name) {
56       if (reg_info->name) {
57         s->Printf(fmt.c_str(), reg_info->name);
58         name_printed = true;
59       } else if (reg_info->alt_name) {
60         s->Printf(fmt.c_str(), reg_info->alt_name);
61         prefix_with_alt_name = false;
62         name_printed = true;
63       }
64     }
65     if (prefix_with_alt_name) {
66       if (name_printed)
67         s->PutChar('/');
68       if (reg_info->alt_name) {
69         s->Printf(fmt.c_str(), reg_info->alt_name);
70         name_printed = true;
71       } else if (!name_printed) {
72         // No alternate name but we were asked to display a name, so show the
73         // main name
74         s->Printf(fmt.c_str(), reg_info->name);
75         name_printed = true;
76       }
77     }
78     if (name_printed)
79       s->PutCString(" = ");
80
81     if (format == eFormatDefault)
82       format = reg_info->format;
83
84     DumpDataExtractor(data, s,
85                       0,                    // Offset in "data"
86                       format,               // Format to use when dumping
87                       reg_info->byte_size,  // item_byte_size
88                       1,                    // item_count
89                       UINT32_MAX,           // num_per_line
90                       LLDB_INVALID_ADDRESS, // base_addr
91                       0,                    // item_bit_size
92                       0);                   // item_bit_offset
93     return true;
94   }
95   return false;
96 }
97
98 bool RegisterValue::GetData(DataExtractor &data) const {
99   return data.SetData(GetBytes(), GetByteSize(), GetByteOrder()) > 0;
100 }
101
102 uint32_t RegisterValue::GetAsMemoryData(const RegisterInfo *reg_info, void *dst,
103                                         uint32_t dst_len,
104                                         lldb::ByteOrder dst_byte_order,
105                                         Status &error) const {
106   if (reg_info == nullptr) {
107     error.SetErrorString("invalid register info argument.");
108     return 0;
109   }
110
111   // ReadRegister should have already been called on this object prior to
112   // calling this.
113   if (GetType() == eTypeInvalid) {
114     // No value has been read into this object...
115     error.SetErrorStringWithFormat(
116         "invalid register value type for register %s", reg_info->name);
117     return 0;
118   }
119
120   if (dst_len > kMaxRegisterByteSize) {
121     error.SetErrorString("destination is too big");
122     return 0;
123   }
124
125   const uint32_t src_len = reg_info->byte_size;
126
127   // Extract the register data into a data extractor
128   DataExtractor reg_data;
129   if (!GetData(reg_data)) {
130     error.SetErrorString("invalid register value to copy into");
131     return 0;
132   }
133
134   // Prepare a memory buffer that contains some or all of the register value
135   const uint32_t bytes_copied =
136       reg_data.CopyByteOrderedData(0,               // src offset
137                                    src_len,         // src length
138                                    dst,             // dst buffer
139                                    dst_len,         // dst length
140                                    dst_byte_order); // dst byte order
141   if (bytes_copied == 0)
142     error.SetErrorStringWithFormat(
143         "failed to copy data for register write of %s", reg_info->name);
144
145   return bytes_copied;
146 }
147
148 uint32_t RegisterValue::SetFromMemoryData(const RegisterInfo *reg_info,
149                                           const void *src, uint32_t src_len,
150                                           lldb::ByteOrder src_byte_order,
151                                           Status &error) {
152   if (reg_info == nullptr) {
153     error.SetErrorString("invalid register info argument.");
154     return 0;
155   }
156
157   // Moving from addr into a register
158   //
159   // Case 1: src_len == dst_len
160   //
161   //   |AABBCCDD| Address contents
162   //   |AABBCCDD| Register contents
163   //
164   // Case 2: src_len > dst_len
165   //
166   //   Status!  (The register should always be big enough to hold the data)
167   //
168   // Case 3: src_len < dst_len
169   //
170   //   |AABB| Address contents
171   //   |AABB0000| Register contents [on little-endian hardware]
172   //   |0000AABB| Register contents [on big-endian hardware]
173   if (src_len > kMaxRegisterByteSize) {
174     error.SetErrorStringWithFormat(
175         "register buffer is too small to receive %u bytes of data.", src_len);
176     return 0;
177   }
178
179   const uint32_t dst_len = reg_info->byte_size;
180
181   if (src_len > dst_len) {
182     error.SetErrorStringWithFormat(
183         "%u bytes is too big to store in register %s (%u bytes)", src_len,
184         reg_info->name, dst_len);
185     return 0;
186   }
187
188   // Use a data extractor to correctly copy and pad the bytes read into the
189   // register value
190   DataExtractor src_data(src, src_len, src_byte_order, 4);
191
192   error = SetValueFromData(reg_info, src_data, 0, true);
193   if (error.Fail())
194     return 0;
195
196   // If SetValueFromData succeeded, we must have copied all of src_len
197   return src_len;
198 }
199
200 bool RegisterValue::GetScalarValue(Scalar &scalar) const {
201   switch (m_type) {
202   case eTypeInvalid:
203     break;
204   case eTypeBytes: {
205     switch (buffer.length) {
206     default:
207       break;
208     case 1:
209       scalar = *(const uint8_t *)buffer.bytes;
210       return true;
211     case 2:
212       scalar = *(const uint16_t *)buffer.bytes;
213       return true;
214     case 4:
215       scalar = *(const uint32_t *)buffer.bytes;
216       return true;
217     case 8:
218       scalar = *(const uint64_t *)buffer.bytes;
219       return true;
220     case 16:
221     case 32:
222       if (buffer.length % sizeof(uint64_t) == 0) {
223         const auto length_in_bits = buffer.length * 8;
224         const auto length_in_uint64 = buffer.length / sizeof(uint64_t);
225         scalar =
226             llvm::APInt(length_in_bits,
227                         llvm::ArrayRef<uint64_t>((const uint64_t *)buffer.bytes,
228                                                  length_in_uint64));
229         return true;
230       }
231       break;
232     }
233   } break;
234   case eTypeUInt8:
235   case eTypeUInt16:
236   case eTypeUInt32:
237   case eTypeUInt64:
238   case eTypeUInt128:
239   case eTypeFloat:
240   case eTypeDouble:
241   case eTypeLongDouble:
242     scalar = m_scalar;
243     return true;
244   }
245   return false;
246 }
247
248 void RegisterValue::Clear() { m_type = eTypeInvalid; }
249
250 RegisterValue::Type RegisterValue::SetType(const RegisterInfo *reg_info) {
251   // To change the type, we simply copy the data in again, using the new format
252   RegisterValue copy;
253   DataExtractor copy_data;
254   if (copy.CopyValue(*this) && copy.GetData(copy_data))
255     SetValueFromData(reg_info, copy_data, 0, true);
256
257   return m_type;
258 }
259
260 Status RegisterValue::SetValueFromData(const RegisterInfo *reg_info,
261                                        DataExtractor &src,
262                                        lldb::offset_t src_offset,
263                                        bool partial_data_ok) {
264   Status error;
265
266   if (src.GetByteSize() == 0) {
267     error.SetErrorString("empty data.");
268     return error;
269   }
270
271   if (reg_info->byte_size == 0) {
272     error.SetErrorString("invalid register info.");
273     return error;
274   }
275
276   uint32_t src_len = src.GetByteSize() - src_offset;
277
278   if (!partial_data_ok && (src_len < reg_info->byte_size)) {
279     error.SetErrorString("not enough data.");
280     return error;
281   }
282
283   // Cap the data length if there is more than enough bytes for this register
284   // value
285   if (src_len > reg_info->byte_size)
286     src_len = reg_info->byte_size;
287
288   // Zero out the value in case we get partial data...
289   memset(buffer.bytes, 0, sizeof(buffer.bytes));
290
291   type128 int128;
292
293   m_type = eTypeInvalid;
294   switch (reg_info->encoding) {
295   case eEncodingInvalid:
296     break;
297   case eEncodingUint:
298   case eEncodingSint:
299     if (reg_info->byte_size == 1)
300       SetUInt8(src.GetMaxU32(&src_offset, src_len));
301     else if (reg_info->byte_size <= 2)
302       SetUInt16(src.GetMaxU32(&src_offset, src_len));
303     else if (reg_info->byte_size <= 4)
304       SetUInt32(src.GetMaxU32(&src_offset, src_len));
305     else if (reg_info->byte_size <= 8)
306       SetUInt64(src.GetMaxU64(&src_offset, src_len));
307     else if (reg_info->byte_size <= 16) {
308       uint64_t data1 = src.GetU64(&src_offset);
309       uint64_t data2 = src.GetU64(&src_offset);
310       if (src.GetByteSize() == eByteOrderBig) {
311         int128.x[0] = data1;
312         int128.x[1] = data2;
313       } else {
314         int128.x[0] = data2;
315         int128.x[1] = data1;
316       }
317       SetUInt128(llvm::APInt(128, 2, int128.x));
318     }
319     break;
320   case eEncodingIEEE754:
321     if (reg_info->byte_size == sizeof(float))
322       SetFloat(src.GetFloat(&src_offset));
323     else if (reg_info->byte_size == sizeof(double))
324       SetDouble(src.GetDouble(&src_offset));
325     else if (reg_info->byte_size == sizeof(long double))
326       SetLongDouble(src.GetLongDouble(&src_offset));
327     break;
328   case eEncodingVector: {
329     m_type = eTypeBytes;
330     buffer.length = reg_info->byte_size;
331     buffer.byte_order = src.GetByteOrder();
332     assert(buffer.length <= kMaxRegisterByteSize);
333     if (buffer.length > kMaxRegisterByteSize)
334       buffer.length = kMaxRegisterByteSize;
335     if (src.CopyByteOrderedData(
336             src_offset,    // offset within "src" to start extracting data
337             src_len,       // src length
338             buffer.bytes,  // dst buffer
339             buffer.length, // dst length
340             buffer.byte_order) == 0) // dst byte order
341     {
342       error.SetErrorStringWithFormat(
343           "failed to copy data for register write of %s", reg_info->name);
344       return error;
345     }
346   }
347   }
348
349   if (m_type == eTypeInvalid)
350     error.SetErrorStringWithFormat(
351         "invalid register value type for register %s", reg_info->name);
352   return error;
353 }
354
355 // Helper function for RegisterValue::SetValueFromString()
356 static bool ParseVectorEncoding(const RegisterInfo *reg_info,
357                                 llvm::StringRef vector_str,
358                                 const uint32_t byte_size,
359                                 RegisterValue *reg_value) {
360   // Example: vector_str = "{0x2c 0x4b 0x2a 0x3e 0xd0 0x4f 0x2a 0x3e 0xac 0x4a
361   // 0x2a 0x3e 0x84 0x4f 0x2a 0x3e}".
362   vector_str = vector_str.trim();
363   vector_str.consume_front("{");
364   vector_str.consume_back("}");
365   vector_str = vector_str.trim();
366
367   char Sep = ' ';
368
369   // The first split should give us:
370   // ('0x2c', '0x4b 0x2a 0x3e 0xd0 0x4f 0x2a 0x3e 0xac 0x4a 0x2a 0x3e 0x84 0x4f
371   // 0x2a 0x3e').
372   llvm::StringRef car;
373   llvm::StringRef cdr = vector_str;
374   std::tie(car, cdr) = vector_str.split(Sep);
375   std::vector<uint8_t> bytes;
376   unsigned byte = 0;
377
378   // Using radix auto-sensing by passing 0 as the radix.
379   // Keep on processing the vector elements as long as the parsing succeeds and
380   // the vector size is < byte_size.
381   while (!car.getAsInteger(0, byte) && bytes.size() < byte_size) {
382     bytes.push_back(byte);
383     std::tie(car, cdr) = cdr.split(Sep);
384   }
385
386   // Check for vector of exact byte_size elements.
387   if (bytes.size() != byte_size)
388     return false;
389
390   reg_value->SetBytes(&(bytes.front()), byte_size, eByteOrderLittle);
391   return true;
392 }
393
394 Status RegisterValue::SetValueFromString(const RegisterInfo *reg_info,
395                                          llvm::StringRef value_str) {
396   Status error;
397   if (reg_info == nullptr) {
398     error.SetErrorString("Invalid register info argument.");
399     return error;
400   }
401
402   m_type = eTypeInvalid;
403   if (value_str.empty()) {
404     error.SetErrorString("Invalid c-string value string.");
405     return error;
406   }
407   const uint32_t byte_size = reg_info->byte_size;
408
409   uint64_t uval64;
410   int64_t ival64;
411   float flt_val;
412   double dbl_val;
413   long double ldbl_val;
414   switch (reg_info->encoding) {
415   case eEncodingInvalid:
416     error.SetErrorString("Invalid encoding.");
417     break;
418
419   case eEncodingUint:
420     if (byte_size > sizeof(uint64_t)) {
421       error.SetErrorStringWithFormat(
422           "unsupported unsigned integer byte size: %u", byte_size);
423       break;
424     }
425     if (value_str.getAsInteger(0, uval64)) {
426       error.SetErrorStringWithFormat(
427           "'%s' is not a valid unsigned integer string value",
428           value_str.str().c_str());
429       break;
430     }
431
432     if (!Args::UInt64ValueIsValidForByteSize(uval64, byte_size)) {
433       error.SetErrorStringWithFormat(
434           "value 0x%" PRIx64
435           " is too large to fit in a %u byte unsigned integer value",
436           uval64, byte_size);
437       break;
438     }
439
440     if (!SetUInt(uval64, reg_info->byte_size)) {
441       error.SetErrorStringWithFormat(
442           "unsupported unsigned integer byte size: %u", byte_size);
443       break;
444     }
445     // TODO: Shouldn't we be setting m_type here?
446     break;
447
448   case eEncodingSint:
449     if (byte_size > sizeof(long long)) {
450       error.SetErrorStringWithFormat("unsupported signed integer byte size: %u",
451                                      byte_size);
452       break;
453     }
454
455     if (value_str.getAsInteger(0, ival64)) {
456       error.SetErrorStringWithFormat(
457           "'%s' is not a valid signed integer string value",
458           value_str.str().c_str());
459       break;
460     }
461
462     if (!Args::SInt64ValueIsValidForByteSize(ival64, byte_size)) {
463       error.SetErrorStringWithFormat(
464           "value 0x%" PRIx64
465           " is too large to fit in a %u byte signed integer value",
466           ival64, byte_size);
467       break;
468     }
469
470     if (!SetUInt(ival64, reg_info->byte_size)) {
471       error.SetErrorStringWithFormat("unsupported signed integer byte size: %u",
472                                      byte_size);
473       break;
474     }
475
476     // TODO: Shouldn't we be setting m_type here?
477     break;
478
479   case eEncodingIEEE754: {
480     std::string value_string = value_str;
481     if (byte_size == sizeof(float)) {
482       if (::sscanf(value_string.c_str(), "%f", &flt_val) != 1) {
483         error.SetErrorStringWithFormat("'%s' is not a valid float string value",
484                                        value_string.c_str());
485         break;
486       }
487       m_scalar = flt_val;
488       m_type = eTypeFloat;
489     } else if (byte_size == sizeof(double)) {
490       if (::sscanf(value_string.c_str(), "%lf", &dbl_val) != 1) {
491         error.SetErrorStringWithFormat("'%s' is not a valid float string value",
492                                        value_string.c_str());
493         break;
494       }
495       m_scalar = dbl_val;
496       m_type = eTypeDouble;
497     } else if (byte_size == sizeof(long double)) {
498       if (::sscanf(value_string.c_str(), "%Lf", &ldbl_val) != 1) {
499         error.SetErrorStringWithFormat("'%s' is not a valid float string value",
500                                        value_string.c_str());
501         break;
502       }
503       m_scalar = ldbl_val;
504       m_type = eTypeLongDouble;
505     } else {
506       error.SetErrorStringWithFormat("unsupported float byte size: %u",
507                                      byte_size);
508       return error;
509     }
510     break;
511   }
512   case eEncodingVector:
513     if (!ParseVectorEncoding(reg_info, value_str, byte_size, this))
514       error.SetErrorString("unrecognized vector encoding string value.");
515     break;
516   }
517
518   return error;
519 }
520
521 bool RegisterValue::SignExtend(uint32_t sign_bitpos) {
522   switch (m_type) {
523   case eTypeInvalid:
524     break;
525
526   case eTypeUInt8:
527   case eTypeUInt16:
528   case eTypeUInt32:
529   case eTypeUInt64:
530   case eTypeUInt128:
531     return m_scalar.SignExtend(sign_bitpos);
532   case eTypeFloat:
533   case eTypeDouble:
534   case eTypeLongDouble:
535   case eTypeBytes:
536     break;
537   }
538   return false;
539 }
540
541 bool RegisterValue::CopyValue(const RegisterValue &rhs) {
542   if (this == &rhs)
543     return rhs.m_type == eTypeInvalid ? false : true;
544
545   m_type = rhs.m_type;
546   switch (m_type) {
547   case eTypeInvalid:
548     return false;
549   case eTypeUInt8:
550   case eTypeUInt16:
551   case eTypeUInt32:
552   case eTypeUInt64:
553   case eTypeUInt128:
554   case eTypeFloat:
555   case eTypeDouble:
556   case eTypeLongDouble:
557     m_scalar = rhs.m_scalar;
558     break;
559   case eTypeBytes:
560     assert(rhs.buffer.length <= kMaxRegisterByteSize);
561     ::memcpy(buffer.bytes, rhs.buffer.bytes, kMaxRegisterByteSize);
562     buffer.length = rhs.buffer.length;
563     buffer.byte_order = rhs.buffer.byte_order;
564     break;
565   }
566   return true;
567 }
568
569 uint16_t RegisterValue::GetAsUInt16(uint16_t fail_value,
570                                     bool *success_ptr) const {
571   if (success_ptr)
572     *success_ptr = true;
573
574   switch (m_type) {
575   default:
576     break;
577   case eTypeUInt8:
578   case eTypeUInt16:
579     return m_scalar.UShort(fail_value);
580   case eTypeBytes: {
581     switch (buffer.length) {
582     default:
583       break;
584     case 1:
585     case 2:
586       return *(const uint16_t *)buffer.bytes;
587     }
588   } break;
589   }
590   if (success_ptr)
591     *success_ptr = false;
592   return fail_value;
593 }
594
595 uint32_t RegisterValue::GetAsUInt32(uint32_t fail_value,
596                                     bool *success_ptr) const {
597   if (success_ptr)
598     *success_ptr = true;
599   switch (m_type) {
600   default:
601     break;
602   case eTypeUInt8:
603   case eTypeUInt16:
604   case eTypeUInt32:
605   case eTypeFloat:
606   case eTypeDouble:
607   case eTypeLongDouble:
608     return m_scalar.UInt(fail_value);
609   case eTypeBytes: {
610     switch (buffer.length) {
611     default:
612       break;
613     case 1:
614     case 2:
615     case 4:
616       return *(const uint32_t *)buffer.bytes;
617     }
618   } break;
619   }
620   if (success_ptr)
621     *success_ptr = false;
622   return fail_value;
623 }
624
625 uint64_t RegisterValue::GetAsUInt64(uint64_t fail_value,
626                                     bool *success_ptr) const {
627   if (success_ptr)
628     *success_ptr = true;
629   switch (m_type) {
630   default:
631     break;
632   case eTypeUInt8:
633   case eTypeUInt16:
634   case eTypeUInt32:
635   case eTypeUInt64:
636   case eTypeFloat:
637   case eTypeDouble:
638   case eTypeLongDouble:
639     return m_scalar.ULongLong(fail_value);
640   case eTypeBytes: {
641     switch (buffer.length) {
642     default:
643       break;
644     case 1:
645       return *(const uint8_t *)buffer.bytes;
646     case 2:
647       return *(const uint16_t *)buffer.bytes;
648     case 4:
649       return *(const uint32_t *)buffer.bytes;
650     case 8:
651       return *(const uint64_t *)buffer.bytes;
652     }
653   } break;
654   }
655   if (success_ptr)
656     *success_ptr = false;
657   return fail_value;
658 }
659
660 llvm::APInt RegisterValue::GetAsUInt128(const llvm::APInt &fail_value,
661                                         bool *success_ptr) const {
662   if (success_ptr)
663     *success_ptr = true;
664   switch (m_type) {
665   default:
666     break;
667   case eTypeUInt8:
668   case eTypeUInt16:
669   case eTypeUInt32:
670   case eTypeUInt64:
671   case eTypeUInt128:
672   case eTypeFloat:
673   case eTypeDouble:
674   case eTypeLongDouble:
675     return m_scalar.UInt128(fail_value);
676   case eTypeBytes: {
677     switch (buffer.length) {
678     default:
679       break;
680     case 1:
681     case 2:
682     case 4:
683     case 8:
684     case 16:
685       return llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
686                          ((const type128 *)buffer.bytes)->x);
687     }
688   } break;
689   }
690   if (success_ptr)
691     *success_ptr = false;
692   return fail_value;
693 }
694
695 float RegisterValue::GetAsFloat(float fail_value, bool *success_ptr) const {
696   if (success_ptr)
697     *success_ptr = true;
698   switch (m_type) {
699   default:
700     break;
701   case eTypeUInt32:
702   case eTypeUInt64:
703   case eTypeUInt128:
704   case eTypeFloat:
705   case eTypeDouble:
706   case eTypeLongDouble:
707     return m_scalar.Float(fail_value);
708   }
709   if (success_ptr)
710     *success_ptr = false;
711   return fail_value;
712 }
713
714 double RegisterValue::GetAsDouble(double fail_value, bool *success_ptr) const {
715   if (success_ptr)
716     *success_ptr = true;
717   switch (m_type) {
718   default:
719     break;
720
721   case eTypeUInt32:
722   case eTypeUInt64:
723   case eTypeUInt128:
724   case eTypeFloat:
725   case eTypeDouble:
726   case eTypeLongDouble:
727     return m_scalar.Double(fail_value);
728   }
729   if (success_ptr)
730     *success_ptr = false;
731   return fail_value;
732 }
733
734 long double RegisterValue::GetAsLongDouble(long double fail_value,
735                                            bool *success_ptr) const {
736   if (success_ptr)
737     *success_ptr = true;
738   switch (m_type) {
739   default:
740     break;
741
742   case eTypeUInt32:
743   case eTypeUInt64:
744   case eTypeUInt128:
745   case eTypeFloat:
746   case eTypeDouble:
747   case eTypeLongDouble:
748     return m_scalar.LongDouble();
749   }
750   if (success_ptr)
751     *success_ptr = false;
752   return fail_value;
753 }
754
755 const void *RegisterValue::GetBytes() const {
756   switch (m_type) {
757   case eTypeInvalid:
758     break;
759   case eTypeUInt8:
760   case eTypeUInt16:
761   case eTypeUInt32:
762   case eTypeUInt64:
763   case eTypeUInt128:
764   case eTypeFloat:
765   case eTypeDouble:
766   case eTypeLongDouble:
767     return m_scalar.GetBytes();
768   case eTypeBytes:
769     return buffer.bytes;
770   }
771   return nullptr;
772 }
773
774 uint32_t RegisterValue::GetByteSize() const {
775   switch (m_type) {
776   case eTypeInvalid:
777     break;
778   case eTypeUInt8:
779     return 1;
780   case eTypeUInt16:
781     return 2;
782   case eTypeUInt32:
783   case eTypeUInt64:
784   case eTypeUInt128:
785   case eTypeFloat:
786   case eTypeDouble:
787   case eTypeLongDouble:
788     return m_scalar.GetByteSize();
789   case eTypeBytes:
790     return buffer.length;
791   }
792   return 0;
793 }
794
795 bool RegisterValue::SetUInt(uint64_t uint, uint32_t byte_size) {
796   if (byte_size == 0) {
797     SetUInt64(uint);
798   } else if (byte_size == 1) {
799     SetUInt8(uint);
800   } else if (byte_size <= 2) {
801     SetUInt16(uint);
802   } else if (byte_size <= 4) {
803     SetUInt32(uint);
804   } else if (byte_size <= 8) {
805     SetUInt64(uint);
806   } else if (byte_size <= 16) {
807     SetUInt128(llvm::APInt(128, uint));
808   } else
809     return false;
810   return true;
811 }
812
813 void RegisterValue::SetBytes(const void *bytes, size_t length,
814                              lldb::ByteOrder byte_order) {
815   // If this assertion fires off we need to increase the size of
816   // buffer.bytes, or make it something that is allocated on
817   // the heap. Since the data buffer is in a union, we can't make it
818   // a collection class like SmallVector...
819   if (bytes && length > 0) {
820     assert(length <= sizeof(buffer.bytes) &&
821            "Storing too many bytes in a RegisterValue.");
822     m_type = eTypeBytes;
823     buffer.length = length;
824     memcpy(buffer.bytes, bytes, length);
825     buffer.byte_order = byte_order;
826   } else {
827     m_type = eTypeInvalid;
828     buffer.length = 0;
829   }
830 }
831
832 bool RegisterValue::operator==(const RegisterValue &rhs) const {
833   if (m_type == rhs.m_type) {
834     switch (m_type) {
835     case eTypeInvalid:
836       return true;
837     case eTypeUInt8:
838     case eTypeUInt16:
839     case eTypeUInt32:
840     case eTypeUInt64:
841     case eTypeUInt128:
842     case eTypeFloat:
843     case eTypeDouble:
844     case eTypeLongDouble:
845       return m_scalar == rhs.m_scalar;
846     case eTypeBytes:
847       if (buffer.length != rhs.buffer.length)
848         return false;
849       else {
850         uint8_t length = buffer.length;
851         if (length > kMaxRegisterByteSize)
852           length = kMaxRegisterByteSize;
853         return memcmp(buffer.bytes, rhs.buffer.bytes, length) == 0;
854       }
855       break;
856     }
857   }
858   return false;
859 }
860
861 bool RegisterValue::operator!=(const RegisterValue &rhs) const {
862   if (m_type != rhs.m_type)
863     return true;
864   switch (m_type) {
865   case eTypeInvalid:
866     return false;
867   case eTypeUInt8:
868   case eTypeUInt16:
869   case eTypeUInt32:
870   case eTypeUInt64:
871   case eTypeUInt128:
872   case eTypeFloat:
873   case eTypeDouble:
874   case eTypeLongDouble:
875     return m_scalar != rhs.m_scalar;
876   case eTypeBytes:
877     if (buffer.length != rhs.buffer.length) {
878       return true;
879     } else {
880       uint8_t length = buffer.length;
881       if (length > kMaxRegisterByteSize)
882         length = kMaxRegisterByteSize;
883       return memcmp(buffer.bytes, rhs.buffer.bytes, length) != 0;
884     }
885     break;
886   }
887   return true;
888 }
889
890 bool RegisterValue::ClearBit(uint32_t bit) {
891   switch (m_type) {
892   case eTypeInvalid:
893     break;
894
895   case eTypeUInt8:
896   case eTypeUInt16:
897   case eTypeUInt32:
898   case eTypeUInt64:
899   case eTypeUInt128:
900     if (bit < (GetByteSize() * 8)) {
901       return m_scalar.ClearBit(bit);
902     }
903     break;
904
905   case eTypeFloat:
906   case eTypeDouble:
907   case eTypeLongDouble:
908     break;
909
910   case eTypeBytes:
911     if (buffer.byte_order == eByteOrderBig ||
912         buffer.byte_order == eByteOrderLittle) {
913       uint32_t byte_idx;
914       if (buffer.byte_order == eByteOrderBig)
915         byte_idx = buffer.length - (bit / 8) - 1;
916       else
917         byte_idx = bit / 8;
918
919       const uint32_t byte_bit = bit % 8;
920       if (byte_idx < buffer.length) {
921         buffer.bytes[byte_idx] &= ~(1u << byte_bit);
922         return true;
923       }
924     }
925     break;
926   }
927   return false;
928 }
929
930 bool RegisterValue::SetBit(uint32_t bit) {
931   switch (m_type) {
932   case eTypeInvalid:
933     break;
934
935   case eTypeUInt8:
936   case eTypeUInt16:
937   case eTypeUInt32:
938   case eTypeUInt64:
939   case eTypeUInt128:
940     if (bit < (GetByteSize() * 8)) {
941       return m_scalar.SetBit(bit);
942     }
943     break;
944
945   case eTypeFloat:
946   case eTypeDouble:
947   case eTypeLongDouble:
948     break;
949
950   case eTypeBytes:
951     if (buffer.byte_order == eByteOrderBig ||
952         buffer.byte_order == eByteOrderLittle) {
953       uint32_t byte_idx;
954       if (buffer.byte_order == eByteOrderBig)
955         byte_idx = buffer.length - (bit / 8) - 1;
956       else
957         byte_idx = bit / 8;
958
959       const uint32_t byte_bit = bit % 8;
960       if (byte_idx < buffer.length) {
961         buffer.bytes[byte_idx] |= (1u << byte_bit);
962         return true;
963       }
964     }
965     break;
966   }
967   return false;
968 }