]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Core/RegisterValue.cpp
Merge ^/head r319801 through r320041.
[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   m_type = rhs.m_type;
543   switch (m_type) {
544   case eTypeInvalid:
545     return false;
546   case eTypeUInt8:
547   case eTypeUInt16:
548   case eTypeUInt32:
549   case eTypeUInt64:
550   case eTypeUInt128:
551   case eTypeFloat:
552   case eTypeDouble:
553   case eTypeLongDouble:
554     m_scalar = rhs.m_scalar;
555     break;
556   case eTypeBytes:
557     assert(rhs.buffer.length <= kMaxRegisterByteSize);
558     ::memcpy(buffer.bytes, rhs.buffer.bytes, kMaxRegisterByteSize);
559     buffer.length = rhs.buffer.length;
560     buffer.byte_order = rhs.buffer.byte_order;
561     break;
562   }
563   return true;
564 }
565
566 uint16_t RegisterValue::GetAsUInt16(uint16_t fail_value,
567                                     bool *success_ptr) const {
568   if (success_ptr)
569     *success_ptr = true;
570
571   switch (m_type) {
572   default:
573     break;
574   case eTypeUInt8:
575   case eTypeUInt16:
576     return m_scalar.UShort(fail_value);
577   case eTypeBytes: {
578     switch (buffer.length) {
579     default:
580       break;
581     case 1:
582     case 2:
583       return *(const uint16_t *)buffer.bytes;
584     }
585   } break;
586   }
587   if (success_ptr)
588     *success_ptr = false;
589   return fail_value;
590 }
591
592 uint32_t RegisterValue::GetAsUInt32(uint32_t fail_value,
593                                     bool *success_ptr) const {
594   if (success_ptr)
595     *success_ptr = true;
596   switch (m_type) {
597   default:
598     break;
599   case eTypeUInt8:
600   case eTypeUInt16:
601   case eTypeUInt32:
602   case eTypeFloat:
603   case eTypeDouble:
604   case eTypeLongDouble:
605     return m_scalar.UInt(fail_value);
606   case eTypeBytes: {
607     switch (buffer.length) {
608     default:
609       break;
610     case 1:
611     case 2:
612     case 4:
613       return *(const uint32_t *)buffer.bytes;
614     }
615   } break;
616   }
617   if (success_ptr)
618     *success_ptr = false;
619   return fail_value;
620 }
621
622 uint64_t RegisterValue::GetAsUInt64(uint64_t fail_value,
623                                     bool *success_ptr) const {
624   if (success_ptr)
625     *success_ptr = true;
626   switch (m_type) {
627   default:
628     break;
629   case eTypeUInt8:
630   case eTypeUInt16:
631   case eTypeUInt32:
632   case eTypeUInt64:
633   case eTypeFloat:
634   case eTypeDouble:
635   case eTypeLongDouble:
636     return m_scalar.ULongLong(fail_value);
637   case eTypeBytes: {
638     switch (buffer.length) {
639     default:
640       break;
641     case 1:
642       return *(const uint8_t *)buffer.bytes;
643     case 2:
644       return *(const uint16_t *)buffer.bytes;
645     case 4:
646       return *(const uint32_t *)buffer.bytes;
647     case 8:
648       return *(const uint64_t *)buffer.bytes;
649     }
650   } break;
651   }
652   if (success_ptr)
653     *success_ptr = false;
654   return fail_value;
655 }
656
657 llvm::APInt RegisterValue::GetAsUInt128(const llvm::APInt &fail_value,
658                                         bool *success_ptr) const {
659   if (success_ptr)
660     *success_ptr = true;
661   switch (m_type) {
662   default:
663     break;
664   case eTypeUInt8:
665   case eTypeUInt16:
666   case eTypeUInt32:
667   case eTypeUInt64:
668   case eTypeUInt128:
669   case eTypeFloat:
670   case eTypeDouble:
671   case eTypeLongDouble:
672     return m_scalar.UInt128(fail_value);
673   case eTypeBytes: {
674     switch (buffer.length) {
675     default:
676       break;
677     case 1:
678     case 2:
679     case 4:
680     case 8:
681     case 16:
682       return llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
683                          ((const type128 *)buffer.bytes)->x);
684     }
685   } break;
686   }
687   if (success_ptr)
688     *success_ptr = false;
689   return fail_value;
690 }
691
692 float RegisterValue::GetAsFloat(float fail_value, bool *success_ptr) const {
693   if (success_ptr)
694     *success_ptr = true;
695   switch (m_type) {
696   default:
697     break;
698   case eTypeUInt32:
699   case eTypeUInt64:
700   case eTypeUInt128:
701   case eTypeFloat:
702   case eTypeDouble:
703   case eTypeLongDouble:
704     return m_scalar.Float(fail_value);
705   }
706   if (success_ptr)
707     *success_ptr = false;
708   return fail_value;
709 }
710
711 double RegisterValue::GetAsDouble(double fail_value, bool *success_ptr) const {
712   if (success_ptr)
713     *success_ptr = true;
714   switch (m_type) {
715   default:
716     break;
717
718   case eTypeUInt32:
719   case eTypeUInt64:
720   case eTypeUInt128:
721   case eTypeFloat:
722   case eTypeDouble:
723   case eTypeLongDouble:
724     return m_scalar.Double(fail_value);
725   }
726   if (success_ptr)
727     *success_ptr = false;
728   return fail_value;
729 }
730
731 long double RegisterValue::GetAsLongDouble(long double fail_value,
732                                            bool *success_ptr) const {
733   if (success_ptr)
734     *success_ptr = true;
735   switch (m_type) {
736   default:
737     break;
738
739   case eTypeUInt32:
740   case eTypeUInt64:
741   case eTypeUInt128:
742   case eTypeFloat:
743   case eTypeDouble:
744   case eTypeLongDouble:
745     return m_scalar.LongDouble();
746   }
747   if (success_ptr)
748     *success_ptr = false;
749   return fail_value;
750 }
751
752 const void *RegisterValue::GetBytes() const {
753   switch (m_type) {
754   case eTypeInvalid:
755     break;
756   case eTypeUInt8:
757   case eTypeUInt16:
758   case eTypeUInt32:
759   case eTypeUInt64:
760   case eTypeUInt128:
761   case eTypeFloat:
762   case eTypeDouble:
763   case eTypeLongDouble:
764     return m_scalar.GetBytes();
765   case eTypeBytes:
766     return buffer.bytes;
767   }
768   return nullptr;
769 }
770
771 uint32_t RegisterValue::GetByteSize() const {
772   switch (m_type) {
773   case eTypeInvalid:
774     break;
775   case eTypeUInt8:
776     return 1;
777   case eTypeUInt16:
778     return 2;
779   case eTypeUInt32:
780   case eTypeUInt64:
781   case eTypeUInt128:
782   case eTypeFloat:
783   case eTypeDouble:
784   case eTypeLongDouble:
785     return m_scalar.GetByteSize();
786   case eTypeBytes:
787     return buffer.length;
788   }
789   return 0;
790 }
791
792 bool RegisterValue::SetUInt(uint64_t uint, uint32_t byte_size) {
793   if (byte_size == 0) {
794     SetUInt64(uint);
795   } else if (byte_size == 1) {
796     SetUInt8(uint);
797   } else if (byte_size <= 2) {
798     SetUInt16(uint);
799   } else if (byte_size <= 4) {
800     SetUInt32(uint);
801   } else if (byte_size <= 8) {
802     SetUInt64(uint);
803   } else if (byte_size <= 16) {
804     SetUInt128(llvm::APInt(128, uint));
805   } else
806     return false;
807   return true;
808 }
809
810 void RegisterValue::SetBytes(const void *bytes, size_t length,
811                              lldb::ByteOrder byte_order) {
812   // If this assertion fires off we need to increase the size of
813   // buffer.bytes, or make it something that is allocated on
814   // the heap. Since the data buffer is in a union, we can't make it
815   // a collection class like SmallVector...
816   if (bytes && length > 0) {
817     assert(length <= sizeof(buffer.bytes) &&
818            "Storing too many bytes in a RegisterValue.");
819     m_type = eTypeBytes;
820     buffer.length = length;
821     memcpy(buffer.bytes, bytes, length);
822     buffer.byte_order = byte_order;
823   } else {
824     m_type = eTypeInvalid;
825     buffer.length = 0;
826   }
827 }
828
829 bool RegisterValue::operator==(const RegisterValue &rhs) const {
830   if (m_type == rhs.m_type) {
831     switch (m_type) {
832     case eTypeInvalid:
833       return true;
834     case eTypeUInt8:
835     case eTypeUInt16:
836     case eTypeUInt32:
837     case eTypeUInt64:
838     case eTypeUInt128:
839     case eTypeFloat:
840     case eTypeDouble:
841     case eTypeLongDouble:
842       return m_scalar == rhs.m_scalar;
843     case eTypeBytes:
844       if (buffer.length != rhs.buffer.length)
845         return false;
846       else {
847         uint8_t length = buffer.length;
848         if (length > kMaxRegisterByteSize)
849           length = kMaxRegisterByteSize;
850         return memcmp(buffer.bytes, rhs.buffer.bytes, length) == 0;
851       }
852       break;
853     }
854   }
855   return false;
856 }
857
858 bool RegisterValue::operator!=(const RegisterValue &rhs) const {
859   if (m_type != rhs.m_type)
860     return true;
861   switch (m_type) {
862   case eTypeInvalid:
863     return false;
864   case eTypeUInt8:
865   case eTypeUInt16:
866   case eTypeUInt32:
867   case eTypeUInt64:
868   case eTypeUInt128:
869   case eTypeFloat:
870   case eTypeDouble:
871   case eTypeLongDouble:
872     return m_scalar != rhs.m_scalar;
873   case eTypeBytes:
874     if (buffer.length != rhs.buffer.length) {
875       return true;
876     } else {
877       uint8_t length = buffer.length;
878       if (length > kMaxRegisterByteSize)
879         length = kMaxRegisterByteSize;
880       return memcmp(buffer.bytes, rhs.buffer.bytes, length) != 0;
881     }
882     break;
883   }
884   return true;
885 }
886
887 bool RegisterValue::ClearBit(uint32_t bit) {
888   switch (m_type) {
889   case eTypeInvalid:
890     break;
891
892   case eTypeUInt8:
893   case eTypeUInt16:
894   case eTypeUInt32:
895   case eTypeUInt64:
896   case eTypeUInt128:
897     if (bit < (GetByteSize() * 8)) {
898       return m_scalar.ClearBit(bit);
899     }
900     break;
901
902   case eTypeFloat:
903   case eTypeDouble:
904   case eTypeLongDouble:
905     break;
906
907   case eTypeBytes:
908     if (buffer.byte_order == eByteOrderBig ||
909         buffer.byte_order == eByteOrderLittle) {
910       uint32_t byte_idx;
911       if (buffer.byte_order == eByteOrderBig)
912         byte_idx = buffer.length - (bit / 8) - 1;
913       else
914         byte_idx = bit / 8;
915
916       const uint32_t byte_bit = bit % 8;
917       if (byte_idx < buffer.length) {
918         buffer.bytes[byte_idx] &= ~(1u << byte_bit);
919         return true;
920       }
921     }
922     break;
923   }
924   return false;
925 }
926
927 bool RegisterValue::SetBit(uint32_t bit) {
928   switch (m_type) {
929   case eTypeInvalid:
930     break;
931
932   case eTypeUInt8:
933   case eTypeUInt16:
934   case eTypeUInt32:
935   case eTypeUInt64:
936   case eTypeUInt128:
937     if (bit < (GetByteSize() * 8)) {
938       return m_scalar.SetBit(bit);
939     }
940     break;
941
942   case eTypeFloat:
943   case eTypeDouble:
944   case eTypeLongDouble:
945     break;
946
947   case eTypeBytes:
948     if (buffer.byte_order == eByteOrderBig ||
949         buffer.byte_order == eByteOrderLittle) {
950       uint32_t byte_idx;
951       if (buffer.byte_order == eByteOrderBig)
952         byte_idx = buffer.length - (bit / 8) - 1;
953       else
954         byte_idx = bit / 8;
955
956       const uint32_t byte_bit = bit % 8;
957       if (byte_idx < buffer.length) {
958         buffer.bytes[byte_idx] |= (1u << byte_bit);
959         return true;
960       }
961     }
962     break;
963   }
964   return false;
965 }