• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 #include "lldb/Core/DataExtractor.h"
17 #include "lldb/Core/Error.h"
18 #include "lldb/Core/Scalar.h"
19 #include "lldb/Core/Stream.h"
20 #include "lldb/Core/StreamString.h"
21 #include "lldb/Interpreter/Args.h"
22 
23 using namespace lldb;
24 using namespace lldb_private;
25 
26 
27 bool
Dump(Stream * s,const RegisterInfo * reg_info,bool prefix_with_name,bool prefix_with_alt_name,Format format,uint32_t reg_name_right_align_at) const28 RegisterValue::Dump (Stream *s,
29                      const RegisterInfo *reg_info,
30                      bool prefix_with_name,
31                      bool prefix_with_alt_name,
32                      Format format,
33                      uint32_t reg_name_right_align_at) const
34 {
35     DataExtractor data;
36     if (GetData (data))
37     {
38         bool name_printed = false;
39         // For simplicity, alignment of the register name printing applies only
40         // in the most common case where:
41         //
42         //     prefix_with_name^prefix_with_alt_name is true
43         //
44         StreamString format_string;
45         if (reg_name_right_align_at && (prefix_with_name^prefix_with_alt_name))
46             format_string.Printf("%%%us", reg_name_right_align_at);
47         else
48             format_string.Printf("%%s");
49         const char *fmt = format_string.GetData();
50         if (prefix_with_name)
51         {
52             if (reg_info->name)
53             {
54                 s->Printf (fmt, reg_info->name);
55                 name_printed = true;
56             }
57             else if (reg_info->alt_name)
58             {
59                 s->Printf (fmt, reg_info->alt_name);
60                 prefix_with_alt_name = false;
61                 name_printed = true;
62             }
63         }
64         if (prefix_with_alt_name)
65         {
66             if (name_printed)
67                 s->PutChar ('/');
68             if (reg_info->alt_name)
69             {
70                 s->Printf (fmt, reg_info->alt_name);
71                 name_printed = true;
72             }
73             else if (!name_printed)
74             {
75                 // No alternate name but we were asked to display a name, so show the main name
76                 s->Printf (fmt, reg_info->name);
77                 name_printed = true;
78             }
79         }
80         if (name_printed)
81             s->PutCString (" = ");
82 
83         if (format == eFormatDefault)
84             format = reg_info->format;
85 
86         data.Dump (s,
87                    0,                       // Offset in "data"
88                    format,                  // Format to use when dumping
89                    reg_info->byte_size,     // item_byte_size
90                    1,                       // item_count
91                    UINT32_MAX,              // num_per_line
92                    LLDB_INVALID_ADDRESS,    // base_addr
93                    0,                       // item_bit_size
94                    0);                      // item_bit_offset
95         return true;
96     }
97     return false;
98 }
99 
100 
101 bool
GetData(DataExtractor & data) const102 RegisterValue::GetData (DataExtractor &data) const
103 {
104     return data.SetData(GetBytes(), GetByteSize(), GetByteOrder()) > 0;
105 }
106 
107 
108 uint32_t
GetAsMemoryData(const RegisterInfo * reg_info,void * dst,uint32_t dst_len,lldb::ByteOrder dst_byte_order,Error & error) const109 RegisterValue::GetAsMemoryData (const RegisterInfo *reg_info,
110                                 void *dst,
111                                 uint32_t dst_len,
112                                 lldb::ByteOrder dst_byte_order,
113                                 Error &error) const
114 {
115     if (reg_info == NULL)
116     {
117         error.SetErrorString ("invalid register info argument.");
118         return 0;
119     }
120 
121     // ReadRegister should have already been called on tgus object prior to
122     // calling this.
123     if (GetType() == eTypeInvalid)
124     {
125         // No value has been read into this object...
126         error.SetErrorStringWithFormat("invalid register value type for register %s", reg_info->name);
127         return 0;
128     }
129 
130     if (dst_len > kMaxRegisterByteSize)
131     {
132         error.SetErrorString ("destination is too big");
133         return 0;
134     }
135 
136     const uint32_t src_len = reg_info->byte_size;
137 
138     // Extract the register data into a data extractor
139     DataExtractor reg_data;
140     if (!GetData(reg_data))
141     {
142         error.SetErrorString ("invalid register value to copy into");
143         return 0;
144     }
145 
146     // Prepare a memory buffer that contains some or all of the register value
147     const uint32_t bytes_copied = reg_data.CopyByteOrderedData (0,                  // src offset
148                                                                 src_len,            // src length
149                                                                 dst,                // dst buffer
150                                                                 dst_len,            // dst length
151                                                                 dst_byte_order);    // dst byte order
152     if (bytes_copied == 0)
153         error.SetErrorStringWithFormat("failed to copy data for register write of %s", reg_info->name);
154 
155     return bytes_copied;
156 }
157 
158 uint32_t
SetFromMemoryData(const RegisterInfo * reg_info,const void * src,uint32_t src_len,lldb::ByteOrder src_byte_order,Error & error)159 RegisterValue::SetFromMemoryData (const RegisterInfo *reg_info,
160                                   const void *src,
161                                   uint32_t src_len,
162                                   lldb::ByteOrder src_byte_order,
163                                   Error &error)
164 {
165     if (reg_info == NULL)
166     {
167         error.SetErrorString ("invalid register info argument.");
168         return 0;
169     }
170 
171     // Moving from addr into a register
172     //
173     // Case 1: src_len == dst_len
174     //
175     //   |AABBCCDD| Address contents
176     //   |AABBCCDD| Register contents
177     //
178     // Case 2: src_len > dst_len
179     //
180     //   Error!  (The register should always be big enough to hold the data)
181     //
182     // Case 3: src_len < dst_len
183     //
184     //   |AABB| Address contents
185     //   |AABB0000| Register contents [on little-endian hardware]
186     //   |0000AABB| Register contents [on big-endian hardware]
187     if (src_len > kMaxRegisterByteSize)
188     {
189         error.SetErrorStringWithFormat ("register buffer is too small to receive %u bytes of data.", src_len);
190         return 0;
191     }
192 
193     const uint32_t dst_len = reg_info->byte_size;
194 
195     if (src_len > dst_len)
196     {
197         error.SetErrorStringWithFormat("%u bytes is too big to store in register %s (%u bytes)", src_len, reg_info->name, dst_len);
198         return 0;
199     }
200 
201     // Use a data extractor to correctly copy and pad the bytes read into the
202     // register value
203     DataExtractor src_data (src, src_len, src_byte_order, 4);
204 
205     // Given the register info, set the value type of this RegisterValue object
206     SetType (reg_info);
207     // And make sure we were able to figure out what that register value was
208     RegisterValue::Type value_type = GetType();
209     if (value_type == eTypeInvalid)
210     {
211         // No value has been read into this object...
212         error.SetErrorStringWithFormat("invalid register value type for register %s", reg_info->name);
213         return 0;
214     }
215     else if (value_type == eTypeBytes)
216     {
217         m_data.buffer.byte_order = src_byte_order;
218         // Make sure to set the buffer length of the destination buffer to avoid
219         // problems due to uninitalized variables.
220         m_data.buffer.length = src_len;
221     }
222 
223     const uint32_t bytes_copied = src_data.CopyByteOrderedData (0,               // src offset
224                                                                 src_len,         // src length
225                                                                 GetBytes(),      // dst buffer
226                                                                 GetByteSize(),   // dst length
227                                                                 GetByteOrder()); // dst byte order
228     if (bytes_copied == 0)
229         error.SetErrorStringWithFormat("failed to copy data for register write of %s", reg_info->name);
230 
231     return bytes_copied;
232 }
233 
234 bool
GetScalarValue(Scalar & scalar) const235 RegisterValue::GetScalarValue (Scalar &scalar) const
236 {
237     switch (m_type)
238     {
239         case eTypeInvalid:      break;
240         case eTypeBytes:
241         {
242             switch (m_data.buffer.length)
243             {
244             default:    break;
245             case 1:     scalar = m_data.uint8; return true;
246             case 2:     scalar = m_data.uint16; return true;
247             case 4:     scalar = m_data.uint32; return true;
248             case 8:     scalar = m_data.uint64; return true;
249             }
250         }
251         case eTypeUInt8:        scalar = m_data.uint8; return true;
252         case eTypeUInt16:       scalar = m_data.uint16; return true;
253         case eTypeUInt32:       scalar = m_data.uint32; return true;
254         case eTypeUInt64:       scalar = m_data.uint64; return true;
255 #if defined (ENABLE_128_BIT_SUPPORT)
256         case eTypeUInt128:      break;
257 #endif
258         case eTypeFloat:        scalar = m_data.ieee_float; return true;
259         case eTypeDouble:       scalar = m_data.ieee_double; return true;
260         case eTypeLongDouble:   scalar = m_data.ieee_long_double; return true;
261     }
262     return false;
263 }
264 
265 void
Clear()266 RegisterValue::Clear()
267 {
268     m_type = eTypeInvalid;
269 }
270 
271 RegisterValue::Type
SetType(const RegisterInfo * reg_info)272 RegisterValue::SetType (const RegisterInfo *reg_info)
273 {
274     m_type = eTypeInvalid;
275     const uint32_t byte_size = reg_info->byte_size;
276     switch (reg_info->encoding)
277     {
278         case eEncodingInvalid:
279             break;
280 
281         case eEncodingUint:
282         case eEncodingSint:
283             if (byte_size == 1)
284                 m_type = eTypeUInt8;
285             else if (byte_size <= 2)
286                 m_type = eTypeUInt16;
287             else if (byte_size <= 4)
288                 m_type = eTypeUInt32;
289             else if (byte_size <= 8)
290                 m_type = eTypeUInt64;
291 #if defined (ENABLE_128_BIT_SUPPORT)
292             else if (byte_size <= 16)
293                 m_type = eTypeUInt128;
294 #endif
295             break;
296 
297         case eEncodingIEEE754:
298             if (byte_size == sizeof(float))
299                 m_type = eTypeFloat;
300             else if (byte_size == sizeof(double))
301                 m_type = eTypeDouble;
302             else if (byte_size == sizeof(long double))
303                 m_type = eTypeLongDouble;
304             break;
305 
306         case eEncodingVector:
307             m_type = eTypeBytes;
308             break;
309     }
310     return m_type;
311 }
312 
313 Error
SetValueFromData(const RegisterInfo * reg_info,DataExtractor & src,lldb::offset_t src_offset,bool partial_data_ok)314 RegisterValue::SetValueFromData (const RegisterInfo *reg_info, DataExtractor &src, lldb::offset_t src_offset, bool partial_data_ok)
315 {
316     Error error;
317 
318     if (src.GetByteSize() == 0)
319     {
320         error.SetErrorString ("empty data.");
321         return error;
322     }
323 
324     if (reg_info->byte_size == 0)
325     {
326         error.SetErrorString ("invalid register info.");
327         return error;
328     }
329 
330     uint32_t src_len = src.GetByteSize() - src_offset;
331 
332     if (!partial_data_ok && (src_len < reg_info->byte_size))
333     {
334         error.SetErrorString ("not enough data.");
335         return error;
336     }
337 
338     // Cap the data length if there is more than enough bytes for this register
339     // value
340     if (src_len > reg_info->byte_size)
341         src_len = reg_info->byte_size;
342 
343     // Zero out the value in case we get partial data...
344     memset (m_data.buffer.bytes, 0, sizeof (m_data.buffer.bytes));
345 
346     switch (SetType (reg_info))
347     {
348         case eTypeInvalid:
349             error.SetErrorString("");
350             break;
351         case eTypeUInt8:    SetUInt8  (src.GetMaxU32 (&src_offset, src_len)); break;
352         case eTypeUInt16:   SetUInt16 (src.GetMaxU32 (&src_offset, src_len)); break;
353         case eTypeUInt32:   SetUInt32 (src.GetMaxU32 (&src_offset, src_len)); break;
354         case eTypeUInt64:   SetUInt64 (src.GetMaxU64 (&src_offset, src_len)); break;
355 #if defined (ENABLE_128_BIT_SUPPORT)
356         case eTypeUInt128:
357             {
358                 __uint128_t data1 = src.GetU64 (&src_offset);
359                 __uint128_t data2 = src.GetU64 (&src_offset);
360                 if (src.GetByteSize() == eByteOrderBig)
361                     SetUInt128 (data1 << 64 + data2);
362                 else
363                     SetUInt128 (data2 << 64 + data1);
364             }
365             break;
366 #endif
367         case eTypeFloat:        SetFloat (src.GetFloat (&src_offset));      break;
368         case eTypeDouble:       SetDouble(src.GetDouble (&src_offset));     break;
369         case eTypeLongDouble:   SetFloat (src.GetLongDouble (&src_offset)); break;
370         case eTypeBytes:
371         {
372             m_data.buffer.length = reg_info->byte_size;
373             m_data.buffer.byte_order = src.GetByteOrder();
374             assert (m_data.buffer.length <= kMaxRegisterByteSize);
375             if (m_data.buffer.length > kMaxRegisterByteSize)
376                 m_data.buffer.length = kMaxRegisterByteSize;
377             if (src.CopyByteOrderedData (src_offset,                    // offset within "src" to start extracting data
378                                          src_len,                       // src length
379                                          m_data.buffer.bytes,           // dst buffer
380                                          m_data.buffer.length,          // dst length
381                                          m_data.buffer.byte_order) == 0)// dst byte order
382             {
383                 error.SetErrorString ("data copy failed data.");
384                 return error;
385             }
386         }
387     }
388 
389     return error;
390 }
391 
392 #include "llvm/ADT/StringRef.h"
393 #include <vector>
StripSpaces(llvm::StringRef & Str)394 static inline void StripSpaces(llvm::StringRef &Str)
395 {
396     while (!Str.empty() && isspace(Str[0]))
397         Str = Str.substr(1);
398     while (!Str.empty() && isspace(Str.back()))
399         Str = Str.substr(0, Str.size()-1);
400 }
LStrip(llvm::StringRef & Str,char c)401 static inline void LStrip(llvm::StringRef &Str, char c)
402 {
403     if (!Str.empty() && Str.front() == c)
404         Str = Str.substr(1);
405 }
RStrip(llvm::StringRef & Str,char c)406 static inline void RStrip(llvm::StringRef &Str, char c)
407 {
408     if (!Str.empty() && Str.back() == c)
409         Str = Str.substr(0, Str.size()-1);
410 }
411 // Helper function for RegisterValue::SetValueFromCString()
412 static bool
ParseVectorEncoding(const RegisterInfo * reg_info,const char * vector_str,const uint32_t byte_size,RegisterValue * reg_value)413 ParseVectorEncoding(const RegisterInfo *reg_info, const char *vector_str, const uint32_t byte_size, RegisterValue *reg_value)
414 {
415     // Example: vector_str = "{0x2c 0x4b 0x2a 0x3e 0xd0 0x4f 0x2a 0x3e 0xac 0x4a 0x2a 0x3e 0x84 0x4f 0x2a 0x3e}".
416     llvm::StringRef Str(vector_str);
417     StripSpaces(Str);
418     LStrip(Str, '{');
419     RStrip(Str, '}');
420     StripSpaces(Str);
421 
422     char Sep = ' ';
423 
424     // The first split should give us:
425     // ('0x2c', '0x4b 0x2a 0x3e 0xd0 0x4f 0x2a 0x3e 0xac 0x4a 0x2a 0x3e 0x84 0x4f 0x2a 0x3e').
426     std::pair<llvm::StringRef, llvm::StringRef> Pair = Str.split(Sep);
427     std::vector<uint8_t> bytes;
428     unsigned byte = 0;
429 
430     // Using radix auto-sensing by passing 0 as the radix.
431     // Keep on processing the vector elements as long as the parsing succeeds and the vector size is < byte_size.
432     while (!Pair.first.getAsInteger(0, byte) && bytes.size() < byte_size) {
433         bytes.push_back(byte);
434         Pair = Pair.second.split(Sep);
435     }
436 
437     // Check for vector of exact byte_size elements.
438     if (bytes.size() != byte_size)
439         return false;
440 
441     reg_value->SetBytes(&(bytes.front()), byte_size, eByteOrderLittle);
442     return true;
443 }
444 Error
SetValueFromCString(const RegisterInfo * reg_info,const char * value_str)445 RegisterValue::SetValueFromCString (const RegisterInfo *reg_info, const char *value_str)
446 {
447     Error error;
448     if (reg_info == NULL)
449     {
450         error.SetErrorString ("Invalid register info argument.");
451         return error;
452     }
453 
454     if (value_str == NULL || value_str[0] == '\0')
455     {
456         error.SetErrorString ("Invalid c-string value string.");
457         return error;
458     }
459     bool success = false;
460     const uint32_t byte_size = reg_info->byte_size;
461     switch (reg_info->encoding)
462     {
463         case eEncodingInvalid:
464             error.SetErrorString ("Invalid encoding.");
465             break;
466 
467         case eEncodingUint:
468             if (byte_size <= sizeof (uint64_t))
469             {
470                 uint64_t uval64 = Args::StringToUInt64(value_str, UINT64_MAX, 0, &success);
471                 if (!success)
472                     error.SetErrorStringWithFormat ("'%s' is not a valid unsigned integer string value", value_str);
473                 else if (!Args::UInt64ValueIsValidForByteSize (uval64, byte_size))
474                     error.SetErrorStringWithFormat ("value 0x%" PRIx64 " is too large to fit in a %u byte unsigned integer value", uval64, byte_size);
475                 else
476                 {
477                     if (!SetUInt (uval64, reg_info->byte_size))
478                         error.SetErrorStringWithFormat ("unsupported unsigned integer byte size: %u", byte_size);
479                 }
480             }
481             else
482             {
483                 error.SetErrorStringWithFormat ("unsupported unsigned integer byte size: %u", byte_size);
484                 return error;
485             }
486             break;
487 
488         case eEncodingSint:
489             if (byte_size <= sizeof (long long))
490             {
491                 uint64_t sval64 = Args::StringToSInt64(value_str, INT64_MAX, 0, &success);
492                 if (!success)
493                     error.SetErrorStringWithFormat ("'%s' is not a valid signed integer string value", value_str);
494                 else if (!Args::SInt64ValueIsValidForByteSize (sval64, byte_size))
495                     error.SetErrorStringWithFormat ("value 0x%" PRIx64 " is too large to fit in a %u byte signed integer value", sval64, byte_size);
496                 else
497                 {
498                     if (!SetUInt (sval64, reg_info->byte_size))
499                         error.SetErrorStringWithFormat ("unsupported signed integer byte size: %u", byte_size);
500                 }
501             }
502             else
503             {
504                 error.SetErrorStringWithFormat ("unsupported signed integer byte size: %u", byte_size);
505                 return error;
506             }
507             break;
508 
509         case eEncodingIEEE754:
510             if (byte_size == sizeof (float))
511             {
512                 if (::sscanf (value_str, "%f", &m_data.ieee_float) == 1)
513                     m_type = eTypeFloat;
514                 else
515                     error.SetErrorStringWithFormat ("'%s' is not a valid float string value", value_str);
516             }
517             else if (byte_size == sizeof (double))
518             {
519                 if (::sscanf (value_str, "%lf", &m_data.ieee_double) == 1)
520                     m_type = eTypeDouble;
521                 else
522                     error.SetErrorStringWithFormat ("'%s' is not a valid float string value", value_str);
523             }
524             else if (byte_size == sizeof (long double))
525             {
526                 if (::sscanf (value_str, "%Lf", &m_data.ieee_long_double) == 1)
527                     m_type = eTypeLongDouble;
528                 else
529                     error.SetErrorStringWithFormat ("'%s' is not a valid float string value", value_str);
530             }
531             else
532             {
533                 error.SetErrorStringWithFormat ("unsupported float byte size: %u", byte_size);
534                 return error;
535             }
536             break;
537 
538         case eEncodingVector:
539             if (!ParseVectorEncoding(reg_info, value_str, byte_size, this))
540                 error.SetErrorString ("unrecognized vector encoding string value.");
541             break;
542     }
543     if (error.Fail())
544         m_type = eTypeInvalid;
545 
546     return error;
547 }
548 
549 
550 bool
SignExtend(uint32_t sign_bitpos)551 RegisterValue::SignExtend (uint32_t sign_bitpos)
552 {
553     switch (m_type)
554     {
555         case eTypeInvalid:
556             break;
557 
558         case eTypeUInt8:
559             if (sign_bitpos == (8-1))
560                 return true;
561             else if (sign_bitpos < (8-1))
562             {
563                 uint8_t sign_bit = 1u << sign_bitpos;
564                 if (m_data.uint8 & sign_bit)
565                 {
566                     const uint8_t mask = ~(sign_bit) + 1u;
567                     m_data.uint8 |= mask;
568                 }
569                 return true;
570             }
571             break;
572 
573         case eTypeUInt16:
574             if (sign_bitpos == (16-1))
575                 return true;
576             else if (sign_bitpos < (16-1))
577             {
578                 uint16_t sign_bit = 1u << sign_bitpos;
579                 if (m_data.uint16 & sign_bit)
580                 {
581                     const uint16_t mask = ~(sign_bit) + 1u;
582                     m_data.uint16 |= mask;
583                 }
584                 return true;
585             }
586             break;
587 
588         case eTypeUInt32:
589             if (sign_bitpos == (32-1))
590                 return true;
591             else if (sign_bitpos < (32-1))
592             {
593                 uint32_t sign_bit = 1u << sign_bitpos;
594                 if (m_data.uint32 & sign_bit)
595                 {
596                     const uint32_t mask = ~(sign_bit) + 1u;
597                     m_data.uint32 |= mask;
598                 }
599                 return true;
600             }
601             break;
602 
603         case eTypeUInt64:
604             if (sign_bitpos == (64-1))
605                 return true;
606             else if (sign_bitpos < (64-1))
607             {
608                 uint64_t sign_bit = 1ull << sign_bitpos;
609                 if (m_data.uint64 & sign_bit)
610                 {
611                     const uint64_t mask = ~(sign_bit) + 1ull;
612                     m_data.uint64 |= mask;
613                 }
614                 return true;
615             }
616             break;
617 
618 #if defined (ENABLE_128_BIT_SUPPORT)
619         case eTypeUInt128:
620             if (sign_bitpos == (128-1))
621                 return true;
622             else if (sign_bitpos < (128-1))
623             {
624                 __uint128_t sign_bit = (__uint128_t)1u << sign_bitpos;
625                 if (m_data.uint128 & sign_bit)
626                 {
627                     const uint128_t mask = ~(sign_bit) + 1u;
628                     m_data.uint128 |= mask;
629                 }
630                 return true;
631             }
632             break;
633 #endif
634         case eTypeFloat:
635         case eTypeDouble:
636         case eTypeLongDouble:
637         case eTypeBytes:
638             break;
639     }
640     return false;
641 }
642 
643 bool
CopyValue(const RegisterValue & rhs)644 RegisterValue::CopyValue (const RegisterValue &rhs)
645 {
646     m_type = rhs.m_type;
647     switch (m_type)
648     {
649         case eTypeInvalid:
650             return false;
651         case eTypeUInt8:        m_data.uint8 = rhs.m_data.uint8; break;
652         case eTypeUInt16:       m_data.uint16 = rhs.m_data.uint16; break;
653         case eTypeUInt32:       m_data.uint32 = rhs.m_data.uint32; break;
654         case eTypeUInt64:       m_data.uint64 = rhs.m_data.uint64; break;
655 #if defined (ENABLE_128_BIT_SUPPORT)
656         case eTypeUInt128:      m_data.uint128 = rhs.m_data.uint128; break;
657 #endif
658         case eTypeFloat:        m_data.ieee_float = rhs.m_data.ieee_float; break;
659         case eTypeDouble:       m_data.ieee_double = rhs.m_data.ieee_double; break;
660         case eTypeLongDouble:   m_data.ieee_long_double = rhs.m_data.ieee_long_double; break;
661         case eTypeBytes:
662             assert (rhs.m_data.buffer.length <= kMaxRegisterByteSize);
663             ::memcpy (m_data.buffer.bytes, rhs.m_data.buffer.bytes, kMaxRegisterByteSize);
664             m_data.buffer.length = rhs.m_data.buffer.length;
665             m_data.buffer.byte_order = rhs.m_data.buffer.byte_order;
666             break;
667     }
668     return true;
669 }
670 
671 uint16_t
GetAsUInt16(uint16_t fail_value,bool * success_ptr) const672 RegisterValue::GetAsUInt16 (uint16_t fail_value, bool *success_ptr) const
673 {
674     if (success_ptr)
675         *success_ptr = true;
676 
677     switch (m_type)
678     {
679         default:            break;
680         case eTypeUInt8:    return m_data.uint8;
681         case eTypeUInt16:   return m_data.uint16;
682         case eTypeBytes:
683         {
684             switch (m_data.buffer.length)
685             {
686             default:    break;
687             case 1:     return m_data.uint8;
688             case 2:     return m_data.uint16;
689             }
690         }
691         break;
692     }
693     if (success_ptr)
694         *success_ptr = false;
695     return fail_value;
696 }
697 
698 uint32_t
GetAsUInt32(uint32_t fail_value,bool * success_ptr) const699 RegisterValue::GetAsUInt32 (uint32_t fail_value, bool *success_ptr) const
700 {
701     if (success_ptr)
702         *success_ptr = true;
703     switch (m_type)
704     {
705         default:            break;
706         case eTypeUInt8:    return m_data.uint8;
707         case eTypeUInt16:   return m_data.uint16;
708         case eTypeUInt32:   return m_data.uint32;
709         case eTypeFloat:
710             if (sizeof(float) == sizeof(uint32_t))
711                 return m_data.uint32;
712             break;
713         case eTypeDouble:
714             if (sizeof(double) == sizeof(uint32_t))
715                 return m_data.uint32;
716             break;
717         case eTypeLongDouble:
718             if (sizeof(long double) == sizeof(uint32_t))
719                 return m_data.uint32;
720             break;
721         case eTypeBytes:
722         {
723             switch (m_data.buffer.length)
724             {
725             default:    break;
726             case 1:     return m_data.uint8;
727             case 2:     return m_data.uint16;
728             case 4:     return m_data.uint32;
729             }
730         }
731         break;
732     }
733     if (success_ptr)
734         *success_ptr = false;
735     return fail_value;
736 }
737 
738 uint64_t
GetAsUInt64(uint64_t fail_value,bool * success_ptr) const739 RegisterValue::GetAsUInt64 (uint64_t fail_value, bool *success_ptr) const
740 {
741     if (success_ptr)
742         *success_ptr = true;
743     switch (m_type)
744     {
745         default:            break;
746         case eTypeUInt8:    return m_data.uint8;
747         case eTypeUInt16:   return m_data.uint16;
748         case eTypeUInt32:   return m_data.uint32;
749         case eTypeUInt64:   return m_data.uint64;
750         case eTypeFloat:
751             if (sizeof(float) == sizeof(uint64_t))
752                 return m_data.uint64;
753             break;
754         case eTypeDouble:
755             if (sizeof(double) == sizeof(uint64_t))
756                 return m_data.uint64;
757             break;
758         case eTypeLongDouble:
759             if (sizeof(long double) == sizeof(uint64_t))
760                 return m_data.uint64;
761             break;
762         case eTypeBytes:
763         {
764             switch (m_data.buffer.length)
765             {
766             default:    break;
767             case 1:     return m_data.uint8;
768             case 2:     return m_data.uint16;
769             case 4:     return m_data.uint32;
770             case 8:     return m_data.uint64;
771             }
772         }
773         break;
774     }
775     if (success_ptr)
776         *success_ptr = false;
777     return fail_value;
778 }
779 
780 #if defined (ENABLE_128_BIT_SUPPORT)
781 __uint128_t
GetAsUInt128(__uint128_t fail_value,bool * success_ptr) const782 RegisterValue::GetAsUInt128 (__uint128_t fail_value, bool *success_ptr) const
783 {
784     if (success_ptr)
785         *success_ptr = true;
786     switch (m_type)
787     {
788         default:            break;
789         case eTypeUInt8:    return m_data.uint8;
790         case eTypeUInt16:   return m_data.uint16;
791         case eTypeUInt32:   return m_data.uint32;
792         case eTypeUInt64:   return m_data.uint64;
793         case eTypeUInt128:  return m_data.uint128;
794         case eTypeFloat:
795             if (sizeof(float) == sizeof(__uint128_t))
796                 return m_data.uint128;
797             break;
798         case eTypeDouble:
799             if (sizeof(double) == sizeof(__uint128_t))
800                 return m_data.uint128;
801             break;
802         case eTypeLongDouble:
803             if (sizeof(long double) == sizeof(__uint128_t))
804                 return m_data.uint128;
805             break;
806         case eTypeBytes:
807         {
808             switch (m_data.buffer.length)
809             {
810             default:
811                 break;
812             case 1:     return m_data.uint8;
813             case 2:     return m_data.uint16;
814             case 4:     return m_data.uint32;
815             case 8:     return m_data.uint64;
816             case 16:    return m_data.uint128;
817             }
818         }
819         break;
820     }
821     if (success_ptr)
822         *success_ptr = false;
823     return fail_value;
824 }
825 #endif
826 float
GetAsFloat(float fail_value,bool * success_ptr) const827 RegisterValue::GetAsFloat (float fail_value, bool *success_ptr) const
828 {
829     if (success_ptr)
830         *success_ptr = true;
831     switch (m_type)
832     {
833         default:            break;
834         case eTypeUInt32:
835             if (sizeof(float) == sizeof(m_data.uint32))
836                 return m_data.ieee_float;
837             break;
838         case eTypeUInt64:
839             if (sizeof(float) == sizeof(m_data.uint64))
840                 return m_data.ieee_float;
841             break;
842 #if defined (ENABLE_128_BIT_SUPPORT)
843         case eTypeUInt128:
844             if (sizeof(float) == sizeof(m_data.uint128))
845                 return m_data.ieee_float;
846             break;
847 #endif
848         case eTypeFloat:    return m_data.ieee_float;
849         case eTypeDouble:
850             if (sizeof(float) == sizeof(double))
851                 return m_data.ieee_float;
852             break;
853         case eTypeLongDouble:
854             if (sizeof(float) == sizeof(long double))
855                 return m_data.ieee_float;
856             break;
857     }
858     if (success_ptr)
859         *success_ptr = false;
860     return fail_value;
861 }
862 
863 double
GetAsDouble(double fail_value,bool * success_ptr) const864 RegisterValue::GetAsDouble (double fail_value, bool *success_ptr) const
865 {
866     if (success_ptr)
867         *success_ptr = true;
868     switch (m_type)
869     {
870         default:
871             break;
872 
873         case eTypeUInt32:
874             if (sizeof(double) == sizeof(m_data.uint32))
875                 return m_data.ieee_double;
876             break;
877 
878         case eTypeUInt64:
879             if (sizeof(double) == sizeof(m_data.uint64))
880                 return m_data.ieee_double;
881             break;
882 
883 #if defined (ENABLE_128_BIT_SUPPORT)
884         case eTypeUInt128:
885             if (sizeof(double) == sizeof(m_data.uint128))
886                 return m_data.ieee_double;
887 #endif
888         case eTypeFloat:    return m_data.ieee_float;
889         case eTypeDouble:   return m_data.ieee_double;
890 
891         case eTypeLongDouble:
892             if (sizeof(double) == sizeof(long double))
893                 return m_data.ieee_double;
894             break;
895     }
896     if (success_ptr)
897         *success_ptr = false;
898     return fail_value;
899 }
900 
901 long double
GetAsLongDouble(long double fail_value,bool * success_ptr) const902 RegisterValue::GetAsLongDouble (long double fail_value, bool *success_ptr) const
903 {
904     if (success_ptr)
905         *success_ptr = true;
906     switch (m_type)
907     {
908         default:
909             break;
910 
911         case eTypeUInt32:
912             if (sizeof(long double) == sizeof(m_data.uint32))
913                 return m_data.ieee_long_double;
914             break;
915 
916         case eTypeUInt64:
917             if (sizeof(long double) == sizeof(m_data.uint64))
918                 return m_data.ieee_long_double;
919             break;
920 
921 #if defined (ENABLE_128_BIT_SUPPORT)
922         case eTypeUInt128:
923             if (sizeof(long double) == sizeof(m_data.uint128))
924                 return m_data.ieee_long_double;
925 #endif
926         case eTypeFloat:        return m_data.ieee_float;
927         case eTypeDouble:       return m_data.ieee_double;
928         case eTypeLongDouble:   return m_data.ieee_long_double;
929             break;
930     }
931     if (success_ptr)
932         *success_ptr = false;
933     return fail_value;
934 }
935 
936 const void *
GetBytes() const937 RegisterValue::GetBytes () const
938 {
939     switch (m_type)
940     {
941         case eTypeInvalid:      break;
942         case eTypeUInt8:        return &m_data.uint8;
943         case eTypeUInt16:       return &m_data.uint16;
944         case eTypeUInt32:       return &m_data.uint32;
945         case eTypeUInt64:       return &m_data.uint64;
946 #if defined (ENABLE_128_BIT_SUPPORT)
947         case eTypeUInt128:      return &m_data.uint128;
948 #endif
949         case eTypeFloat:        return &m_data.ieee_float;
950         case eTypeDouble:       return &m_data.ieee_double;
951         case eTypeLongDouble:   return &m_data.ieee_long_double;
952         case eTypeBytes:        return m_data.buffer.bytes;
953     }
954     return NULL;
955 }
956 
957 void *
GetBytes()958 RegisterValue::GetBytes ()
959 {
960     switch (m_type)
961     {
962         case eTypeInvalid:      break;
963         case eTypeUInt8:        return &m_data.uint8;
964         case eTypeUInt16:       return &m_data.uint16;
965         case eTypeUInt32:       return &m_data.uint32;
966         case eTypeUInt64:       return &m_data.uint64;
967 #if defined (ENABLE_128_BIT_SUPPORT)
968         case eTypeUInt128:      return &m_data.uint128;
969 #endif
970         case eTypeFloat:        return &m_data.ieee_float;
971         case eTypeDouble:       return &m_data.ieee_double;
972         case eTypeLongDouble:   return &m_data.ieee_long_double;
973         case eTypeBytes:        return m_data.buffer.bytes;
974     }
975     return NULL;
976 }
977 
978 uint32_t
GetByteSize() const979 RegisterValue::GetByteSize () const
980 {
981     switch (m_type)
982     {
983         case eTypeInvalid: break;
984         case eTypeUInt8:        return sizeof(m_data.uint8);
985         case eTypeUInt16:       return sizeof(m_data.uint16);
986         case eTypeUInt32:       return sizeof(m_data.uint32);
987         case eTypeUInt64:       return sizeof(m_data.uint64);
988 #if defined (ENABLE_128_BIT_SUPPORT)
989         case eTypeUInt128:      return sizeof(m_data.uint128);
990 #endif
991         case eTypeFloat:        return sizeof(m_data.ieee_float);
992         case eTypeDouble:       return sizeof(m_data.ieee_double);
993         case eTypeLongDouble:   return sizeof(m_data.ieee_long_double);
994         case eTypeBytes: return m_data.buffer.length;
995     }
996     return 0;
997 }
998 
999 
1000 bool
SetUInt(uint64_t uint,uint32_t byte_size)1001 RegisterValue::SetUInt (uint64_t uint, uint32_t byte_size)
1002 {
1003     if (byte_size == 0)
1004     {
1005         SetUInt64 (uint);
1006     }
1007     else if (byte_size == 1)
1008     {
1009         SetUInt8 (uint);
1010     }
1011     else if (byte_size <= 2)
1012     {
1013         SetUInt16 (uint);
1014     }
1015     else if (byte_size <= 4)
1016     {
1017         SetUInt32 (uint);
1018     }
1019     else if (byte_size <= 8)
1020     {
1021         SetUInt64 (uint);
1022     }
1023 #if defined (ENABLE_128_BIT_SUPPORT)
1024     else if (byte_size <= 16)
1025     {
1026         SetUInt128 (uint);
1027     }
1028 #endif
1029     else
1030         return false;
1031     return true;
1032 }
1033 
1034 void
SetBytes(const void * bytes,size_t length,lldb::ByteOrder byte_order)1035 RegisterValue::SetBytes (const void *bytes, size_t length, lldb::ByteOrder byte_order)
1036 {
1037     // If this assertion fires off we need to increase the size of
1038     // m_data.buffer.bytes, or make it something that is allocated on
1039     // the heap. Since the data buffer is in a union, we can't make it
1040     // a collection class like SmallVector...
1041     if (bytes && length > 0)
1042     {
1043         assert (length <= sizeof (m_data.buffer.bytes) && "Storing too many bytes in a RegisterValue.");
1044         m_type = eTypeBytes;
1045         m_data.buffer.length = length;
1046         memcpy (m_data.buffer.bytes, bytes, length);
1047         m_data.buffer.byte_order = byte_order;
1048     }
1049     else
1050     {
1051         m_type = eTypeInvalid;
1052         m_data.buffer.length = 0;
1053     }
1054 }
1055 
1056 
1057 bool
operator ==(const RegisterValue & rhs) const1058 RegisterValue::operator == (const RegisterValue &rhs) const
1059 {
1060     if (m_type == rhs.m_type)
1061     {
1062         switch (m_type)
1063         {
1064             case eTypeInvalid:      return true;
1065             case eTypeUInt8:        return m_data.uint8 == rhs.m_data.uint8;
1066             case eTypeUInt16:       return m_data.uint16 == rhs.m_data.uint16;
1067             case eTypeUInt32:       return m_data.uint32 == rhs.m_data.uint32;
1068             case eTypeUInt64:       return m_data.uint64 == rhs.m_data.uint64;
1069 #if defined (ENABLE_128_BIT_SUPPORT)
1070             case eTypeUInt128:      return m_data.uint128 == rhs.m_data.uint128;
1071 #endif
1072             case eTypeFloat:        return m_data.ieee_float == rhs.m_data.ieee_float;
1073             case eTypeDouble:       return m_data.ieee_double == rhs.m_data.ieee_double;
1074             case eTypeLongDouble:   return m_data.ieee_long_double == rhs.m_data.ieee_long_double;
1075             case eTypeBytes:
1076                 if (m_data.buffer.length != rhs.m_data.buffer.length)
1077                     return false;
1078                 else
1079                 {
1080                     uint8_t length = m_data.buffer.length;
1081                     if (length > kMaxRegisterByteSize)
1082                         length = kMaxRegisterByteSize;
1083                     return memcmp (m_data.buffer.bytes, rhs.m_data.buffer.bytes, length) == 0;
1084                 }
1085                 break;
1086         }
1087     }
1088     return false;
1089 }
1090 
1091 bool
operator !=(const RegisterValue & rhs) const1092 RegisterValue::operator != (const RegisterValue &rhs) const
1093 {
1094     if (m_type != rhs.m_type)
1095         return true;
1096     switch (m_type)
1097     {
1098         case eTypeInvalid:      return false;
1099         case eTypeUInt8:        return m_data.uint8 != rhs.m_data.uint8;
1100         case eTypeUInt16:       return m_data.uint16 != rhs.m_data.uint16;
1101         case eTypeUInt32:       return m_data.uint32 != rhs.m_data.uint32;
1102         case eTypeUInt64:       return m_data.uint64 != rhs.m_data.uint64;
1103 #if defined (ENABLE_128_BIT_SUPPORT)
1104         case eTypeUInt128:      return m_data.uint128 != rhs.m_data.uint128;
1105 #endif
1106         case eTypeFloat:        return m_data.ieee_float != rhs.m_data.ieee_float;
1107         case eTypeDouble:       return m_data.ieee_double != rhs.m_data.ieee_double;
1108         case eTypeLongDouble:   return m_data.ieee_long_double != rhs.m_data.ieee_long_double;
1109         case eTypeBytes:
1110             if (m_data.buffer.length != rhs.m_data.buffer.length)
1111             {
1112                 return true;
1113             }
1114             else
1115             {
1116                 uint8_t length = m_data.buffer.length;
1117                 if (length > kMaxRegisterByteSize)
1118                     length = kMaxRegisterByteSize;
1119                 return memcmp (m_data.buffer.bytes, rhs.m_data.buffer.bytes, length) != 0;
1120             }
1121             break;
1122     }
1123     return true;
1124 }
1125 
1126 bool
ClearBit(uint32_t bit)1127 RegisterValue::ClearBit (uint32_t bit)
1128 {
1129     switch (m_type)
1130     {
1131         case eTypeInvalid:
1132             break;
1133 
1134         case eTypeUInt8:
1135             if (bit < 8)
1136             {
1137                 m_data.uint8 &= ~(1u << bit);
1138                 return true;
1139             }
1140             break;
1141 
1142         case eTypeUInt16:
1143             if (bit < 16)
1144             {
1145                 m_data.uint16 &= ~(1u << bit);
1146                 return true;
1147             }
1148             break;
1149 
1150         case eTypeUInt32:
1151             if (bit < 32)
1152             {
1153                 m_data.uint32 &= ~(1u << bit);
1154                 return true;
1155             }
1156             break;
1157 
1158         case eTypeUInt64:
1159             if (bit < 64)
1160             {
1161                 m_data.uint64 &= ~(1ull << (uint64_t)bit);
1162                 return true;
1163             }
1164             break;
1165 #if defined (ENABLE_128_BIT_SUPPORT)
1166         case eTypeUInt128:
1167             if (bit < 64)
1168             {
1169                 m_data.uint128 &= ~((__uint128_t)1ull << (__uint128_t)bit);
1170                 return true;
1171             }
1172 #endif
1173         case eTypeFloat:
1174         case eTypeDouble:
1175         case eTypeLongDouble:
1176             break;
1177 
1178         case eTypeBytes:
1179             if (m_data.buffer.byte_order == eByteOrderBig || m_data.buffer.byte_order == eByteOrderLittle)
1180             {
1181                 uint32_t byte_idx;
1182                 if (m_data.buffer.byte_order == eByteOrderBig)
1183                     byte_idx = m_data.buffer.length - (bit / 8) - 1;
1184                 else
1185                     byte_idx = bit / 8;
1186 
1187                 const uint32_t byte_bit = bit % 8;
1188                 if (byte_idx < m_data.buffer.length)
1189                 {
1190                     m_data.buffer.bytes[byte_idx] &= ~(1u << byte_bit);
1191                     return true;
1192                 }
1193             }
1194             break;
1195     }
1196     return false;
1197 }
1198 
1199 
1200 bool
SetBit(uint32_t bit)1201 RegisterValue::SetBit (uint32_t bit)
1202 {
1203     switch (m_type)
1204     {
1205         case eTypeInvalid:
1206             break;
1207 
1208         case eTypeUInt8:
1209             if (bit < 8)
1210             {
1211                 m_data.uint8 |= (1u << bit);
1212                 return true;
1213             }
1214             break;
1215 
1216         case eTypeUInt16:
1217             if (bit < 16)
1218             {
1219                 m_data.uint16 |= (1u << bit);
1220                 return true;
1221             }
1222             break;
1223 
1224         case eTypeUInt32:
1225             if (bit < 32)
1226             {
1227                 m_data.uint32 |= (1u << bit);
1228                 return true;
1229             }
1230             break;
1231 
1232         case eTypeUInt64:
1233             if (bit < 64)
1234             {
1235                 m_data.uint64 |= (1ull << (uint64_t)bit);
1236                 return true;
1237             }
1238             break;
1239 #if defined (ENABLE_128_BIT_SUPPORT)
1240         case eTypeUInt128:
1241             if (bit < 64)
1242             {
1243                 m_data.uint128 |= ((__uint128_t)1ull << (__uint128_t)bit);
1244                 return true;
1245             }
1246 #endif
1247         case eTypeFloat:
1248         case eTypeDouble:
1249         case eTypeLongDouble:
1250             break;
1251 
1252         case eTypeBytes:
1253             if (m_data.buffer.byte_order == eByteOrderBig || m_data.buffer.byte_order == eByteOrderLittle)
1254             {
1255                 uint32_t byte_idx;
1256                 if (m_data.buffer.byte_order == eByteOrderBig)
1257                     byte_idx = m_data.buffer.length - (bit / 8) - 1;
1258                 else
1259                     byte_idx = bit / 8;
1260 
1261                 const uint32_t byte_bit = bit % 8;
1262                 if (byte_idx < m_data.buffer.length)
1263                 {
1264                     m_data.buffer.bytes[byte_idx] |= (1u << byte_bit);
1265                     return true;
1266                 }
1267             }
1268             break;
1269     }
1270     return false;
1271 }
1272 
1273