1 //===-- Scalar.cpp --------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Utility/Scalar.h"
10 #include "lldb/Utility/DataBufferHeap.h"
11 #include "lldb/Utility/DataExtractor.h"
12 #include "lldb/Utility/Endian.h"
13 #include "lldb/Utility/Status.h"
14 #include "lldb/Utility/Stream.h"
15 #include "lldb/Utility/StreamString.h"
16 #include "lldb/lldb-types.h"
17 
18 #include "llvm/ADT/SmallString.h"
19 
20 #include <cinttypes>
21 #include <cstdio>
22 
23 using namespace lldb;
24 using namespace lldb_private;
25 
26 // Promote to max type currently follows the ANSI C rule for type promotion in
27 // expressions.
28 static Scalar::Type PromoteToMaxType(
29     const Scalar &lhs,  // The const left hand side object
30     const Scalar &rhs,  // The const right hand side object
31     Scalar &temp_value, // A modifiable temp value than can be used to hold
32                         // either the promoted lhs or rhs object
33     const Scalar *&promoted_lhs_ptr, // Pointer to the resulting possibly
34                                      // promoted value of lhs (at most one of
35                                      // lhs/rhs will get promoted)
36     const Scalar *&promoted_rhs_ptr  // Pointer to the resulting possibly
37                                      // promoted value of rhs (at most one of
38                                      // lhs/rhs will get promoted)
39 ) {
40   Scalar result;
41   // Initialize the promoted values for both the right and left hand side
42   // values to be the objects themselves. If no promotion is needed (both right
43   // and left have the same type), then the temp_value will not get used.
44   promoted_lhs_ptr = &lhs;
45   promoted_rhs_ptr = &rhs;
46   // Extract the types of both the right and left hand side values
47   Scalar::Type lhs_type = lhs.GetType();
48   Scalar::Type rhs_type = rhs.GetType();
49 
50   if (lhs_type > rhs_type) {
51     // Right hand side need to be promoted
52     temp_value = rhs; // Copy right hand side into the temp value
53     if (temp_value.Promote(lhs_type)) // Promote it
54       promoted_rhs_ptr =
55           &temp_value; // Update the pointer for the promoted right hand side
56   } else if (lhs_type < rhs_type) {
57     // Left hand side need to be promoted
58     temp_value = lhs; // Copy left hand side value into the temp value
59     if (temp_value.Promote(rhs_type)) // Promote it
60       promoted_lhs_ptr =
61           &temp_value; // Update the pointer for the promoted left hand side
62   }
63 
64   // Make sure our type promotion worked as expected
65   if (promoted_lhs_ptr->GetType() == promoted_rhs_ptr->GetType())
66     return promoted_lhs_ptr->GetType(); // Return the resulting max type
67 
68   // Return the void type (zero) if we fail to promote either of the values.
69   return Scalar::e_void;
70 }
71 
72 Scalar::Scalar() : m_type(e_void), m_float(static_cast<float>(0)) {}
73 
74 bool Scalar::GetData(DataExtractor &data, size_t limit_byte_size) const {
75   size_t byte_size = GetByteSize();
76   if (byte_size == 0) {
77     data.Clear();
78     return false;
79   }
80   auto buffer_up = std::make_unique<DataBufferHeap>(byte_size, 0);
81   GetBytes(buffer_up->GetData());
82   lldb::offset_t offset = 0;
83 
84   if (limit_byte_size < byte_size) {
85     if (endian::InlHostByteOrder() == eByteOrderLittle) {
86       // On little endian systems if we want fewer bytes from the current
87       // type we just specify fewer bytes since the LSByte is first...
88       byte_size = limit_byte_size;
89     } else if (endian::InlHostByteOrder() == eByteOrderBig) {
90       // On big endian systems if we want fewer bytes from the current type
91       // have to advance our initial byte pointer and trim down the number of
92       // bytes since the MSByte is first
93       offset = byte_size - limit_byte_size;
94       byte_size = limit_byte_size;
95     }
96   }
97 
98   data.SetData(std::move(buffer_up), offset, byte_size);
99   data.SetByteOrder(endian::InlHostByteOrder());
100   return true;
101 }
102 
103 void Scalar::GetBytes(llvm::MutableArrayRef<uint8_t> storage) const {
104   assert(storage.size() >= GetByteSize());
105 
106   switch (m_type) {
107   case e_void:
108     break;
109   case e_sint:
110   case e_uint:
111   case e_slong:
112   case e_ulong:
113   case e_slonglong:
114   case e_ulonglong:
115   case e_sint128:
116   case e_uint128:
117   case e_sint256:
118   case e_uint256:
119   case e_sint512:
120   case e_uint512:
121     StoreIntToMemory(m_integer, storage.data(),
122                      (m_integer.getBitWidth() + 7) / 8);
123     break;
124   case e_float: {
125     float val = m_float.convertToFloat();
126     memcpy(storage.data(), &val, sizeof(val));
127     break;
128   }
129   case e_double: {
130     double val = m_float.convertToDouble();
131     memcpy(storage.data(), &val, sizeof(double));
132     break;
133   }
134   case e_long_double: {
135     llvm::APInt val = m_float.bitcastToAPInt();
136     StoreIntToMemory(val, storage.data(), storage.size());
137     break;
138   }
139   }
140 }
141 
142 size_t Scalar::GetByteSize() const {
143   switch (m_type) {
144   case e_void:
145     break;
146   case e_sint:
147   case e_uint:
148   case e_slong:
149   case e_ulong:
150   case e_slonglong:
151   case e_ulonglong:
152   case e_sint128:
153   case e_uint128:
154   case e_sint256:
155   case e_uint256:
156   case e_sint512:
157   case e_uint512:
158     return (m_integer.getBitWidth() / 8);
159   case e_float:
160     return sizeof(float_t);
161   case e_double:
162     return sizeof(double_t);
163   case e_long_double:
164     return sizeof(long_double_t);
165   }
166   return 0;
167 }
168 
169 bool Scalar::IsZero() const {
170   llvm::APInt zero_int = llvm::APInt::getNullValue(m_integer.getBitWidth() / 8);
171   switch (m_type) {
172   case e_void:
173     break;
174   case e_sint:
175   case e_uint:
176   case e_slong:
177   case e_ulong:
178   case e_slonglong:
179   case e_ulonglong:
180   case e_sint128:
181   case e_uint128:
182   case e_sint256:
183   case e_uint256:
184   case e_uint512:
185   case e_sint512:
186     return llvm::APInt::isSameValue(zero_int, m_integer);
187   case e_float:
188   case e_double:
189   case e_long_double:
190     return m_float.isZero();
191   }
192   return false;
193 }
194 
195 void Scalar::GetValue(Stream *s, bool show_type) const {
196   if (show_type)
197     s->Printf("(%s) ", GetTypeAsCString());
198 
199   switch (m_type) {
200   case e_void:
201     break;
202   case e_sint:
203   case e_slong:
204   case e_slonglong:
205   case e_sint128:
206   case e_sint256:
207   case e_sint512:
208     s->PutCString(m_integer.toString(10, true));
209     break;
210   case e_uint:
211   case e_ulong:
212   case e_ulonglong:
213   case e_uint128:
214   case e_uint256:
215   case e_uint512:
216     s->PutCString(m_integer.toString(10, false));
217     break;
218   case e_float:
219   case e_double:
220   case e_long_double:
221     llvm::SmallString<24> string;
222     m_float.toString(string);
223     s->Printf("%s", string.c_str());
224     break;
225   }
226 }
227 
228 const char *Scalar::GetTypeAsCString() const {
229   switch (m_type) {
230   case e_void:
231     return "void";
232   case e_sint:
233     return "int";
234   case e_uint:
235     return "unsigned int";
236   case e_slong:
237     return "long";
238   case e_ulong:
239     return "unsigned long";
240   case e_slonglong:
241     return "long long";
242   case e_ulonglong:
243     return "unsigned long long";
244   case e_sint128:
245     return "int128_t";
246   case e_uint128:
247     return "unsigned int128_t";
248   case e_sint256:
249     return "int256_t";
250   case e_uint256:
251     return "unsigned int256_t";
252   case e_sint512:
253     return "int512_t";
254   case e_uint512:
255     return "unsigned int512_t";
256   case e_float:
257     return "float";
258   case e_double:
259     return "double";
260   case e_long_double:
261     return "long double";
262   }
263   return "<invalid Scalar type>";
264 }
265 
266 Scalar::~Scalar() = default;
267 
268 Scalar::Type Scalar::GetBestTypeForBitSize(size_t bit_size, bool sign) {
269   // Scalar types are always host types, hence the sizeof().
270   if (sign) {
271     if (bit_size <= sizeof(int)*8) return Scalar::e_sint;
272     if (bit_size <= sizeof(long)*8) return Scalar::e_slong;
273     if (bit_size <= sizeof(long long)*8) return Scalar::e_slonglong;
274     if (bit_size <= 128) return Scalar::e_sint128;
275     if (bit_size <= 256) return Scalar::e_sint256;
276     if (bit_size <= 512) return Scalar::e_sint512;
277   } else {
278     if (bit_size <= sizeof(unsigned int)*8) return Scalar::e_uint;
279     if (bit_size <= sizeof(unsigned long)*8) return Scalar::e_ulong;
280     if (bit_size <= sizeof(unsigned long long)*8) return Scalar::e_ulonglong;
281     if (bit_size <= 128) return Scalar::e_uint128;
282     if (bit_size <= 256) return Scalar::e_uint256;
283     if (bit_size <= 512) return Scalar::e_uint512;
284   }
285   return Scalar::e_void;
286 }
287 
288 void Scalar::TruncOrExtendTo(uint16_t bits, bool sign) {
289   m_integer = sign ? m_integer.sextOrTrunc(bits) : m_integer.zextOrTrunc(bits);
290   m_type = GetBestTypeForBitSize(bits, sign);
291 }
292 
293 static size_t GetBitSize(Scalar::Type type) {
294   switch (type) {
295   case Scalar::e_void:
296     return 0;
297   case Scalar::e_sint:
298     return 8 * sizeof(int);
299   case Scalar::e_uint:
300     return 8 * sizeof(unsigned int);
301   case Scalar::e_slong:
302     return 8 * sizeof(long);
303   case Scalar::e_ulong:
304     return 8 * sizeof(unsigned long);
305   case Scalar::e_slonglong:
306     return 8 * sizeof(long long);
307   case Scalar::e_ulonglong:
308     return 8 * sizeof(unsigned long long);
309   case Scalar::e_sint128:
310   case Scalar::e_uint128:
311     return BITWIDTH_INT128;
312   case Scalar::e_sint256:
313   case Scalar::e_uint256:
314     return BITWIDTH_INT256;
315   case Scalar::e_sint512:
316   case Scalar::e_uint512:
317     return BITWIDTH_INT512;
318   case Scalar::e_float:
319     return 8 * sizeof(float);
320   case Scalar::e_double:
321     return 8 * sizeof(double);
322   case Scalar::e_long_double:
323     return 8 * sizeof(long double);
324   }
325   llvm_unreachable("Unhandled type!");
326 }
327 
328 static bool IsSigned(Scalar::Type type) {
329   switch (type) {
330   case Scalar::e_void:
331   case Scalar::e_uint:
332   case Scalar::e_ulong:
333   case Scalar::e_ulonglong:
334   case Scalar::e_uint128:
335   case Scalar::e_uint256:
336   case Scalar::e_uint512:
337     return false;
338   case Scalar::e_sint:
339   case Scalar::e_slong:
340   case Scalar::e_slonglong:
341   case Scalar::e_sint128:
342   case Scalar::e_sint256:
343   case Scalar::e_sint512:
344   case Scalar::e_float:
345   case Scalar::e_double:
346   case Scalar::e_long_double:
347     return true;
348   }
349   llvm_unreachable("Unhandled type!");
350 }
351 
352 namespace {
353 enum class Category { Void, Integral, Float };
354 }
355 
356 static Category GetCategory(Scalar::Type type) {
357   switch (type) {
358   case Scalar::e_void:
359     return Category::Void;
360   case Scalar::e_float:
361   case Scalar::e_double:
362   case Scalar::e_long_double:
363     return Category::Float;
364   case Scalar::e_sint:
365   case Scalar::e_slong:
366   case Scalar::e_slonglong:
367   case Scalar::e_sint128:
368   case Scalar::e_sint256:
369   case Scalar::e_sint512:
370   case Scalar::e_uint:
371   case Scalar::e_ulong:
372   case Scalar::e_ulonglong:
373   case Scalar::e_uint128:
374   case Scalar::e_uint256:
375   case Scalar::e_uint512:
376     return Category::Integral;
377   }
378   llvm_unreachable("Unhandled type!");
379 }
380 
381 static const llvm::fltSemantics &GetFltSemantics(Scalar::Type type) {
382   switch (type) {
383   case Scalar::e_void:
384   case Scalar::e_sint:
385   case Scalar::e_slong:
386   case Scalar::e_slonglong:
387   case Scalar::e_sint128:
388   case Scalar::e_sint256:
389   case Scalar::e_sint512:
390   case Scalar::e_uint:
391   case Scalar::e_ulong:
392   case Scalar::e_ulonglong:
393   case Scalar::e_uint128:
394   case Scalar::e_uint256:
395   case Scalar::e_uint512:
396     llvm_unreachable("Only floating point types supported!");
397   case Scalar::e_float:
398     return llvm::APFloat::IEEEsingle();
399   case Scalar::e_double:
400     return llvm::APFloat::IEEEdouble();
401   case Scalar::e_long_double:
402     return llvm::APFloat::x87DoubleExtended();
403   }
404   llvm_unreachable("Unhandled type!");
405 }
406 
407 bool Scalar::Promote(Scalar::Type type) {
408   bool success = false;
409   switch (GetCategory(m_type)) {
410   case Category::Void:
411     break;
412   case Category::Integral:
413     switch (GetCategory(type)) {
414     case Category::Void:
415       break;
416     case Category::Integral:
417       if (type < m_type)
418         break;
419       success = true;
420       if (IsSigned(m_type))
421         m_integer = m_integer.sextOrTrunc(GetBitSize(type));
422       else
423         m_integer = m_integer.zextOrTrunc(GetBitSize(type));
424       break;
425     case Category::Float:
426       m_float = llvm::APFloat(GetFltSemantics(type));
427       m_float.convertFromAPInt(m_integer, IsSigned(m_type),
428                                llvm::APFloat::rmNearestTiesToEven);
429       success = true;
430       break;
431     }
432     break;
433   case Category::Float:
434     switch (GetCategory(type)) {
435     case Category::Void:
436     case Category::Integral:
437       break;
438     case Category::Float:
439       if (type < m_type)
440         break;
441       bool ignore;
442       success = true;
443       m_float.convert(GetFltSemantics(type), llvm::APFloat::rmNearestTiesToEven,
444                       &ignore);
445     }
446   }
447 
448   if (success)
449     m_type = type;
450   return success;
451 }
452 
453 const char *Scalar::GetValueTypeAsCString(Scalar::Type type) {
454   switch (type) {
455   case e_void:
456     return "void";
457   case e_sint:
458     return "int";
459   case e_uint:
460     return "unsigned int";
461   case e_slong:
462     return "long";
463   case e_ulong:
464     return "unsigned long";
465   case e_slonglong:
466     return "long long";
467   case e_ulonglong:
468     return "unsigned long long";
469   case e_float:
470     return "float";
471   case e_double:
472     return "double";
473   case e_long_double:
474     return "long double";
475   case e_sint128:
476     return "int128_t";
477   case e_uint128:
478     return "uint128_t";
479   case e_sint256:
480     return "int256_t";
481   case e_uint256:
482     return "uint256_t";
483   case e_sint512:
484     return "int512_t";
485   case e_uint512:
486     return "uint512_t";
487   }
488   return "???";
489 }
490 
491 Scalar::Type
492 Scalar::GetValueTypeForSignedIntegerWithByteSize(size_t byte_size) {
493   if (byte_size <= sizeof(sint_t))
494     return e_sint;
495   if (byte_size <= sizeof(slong_t))
496     return e_slong;
497   if (byte_size <= sizeof(slonglong_t))
498     return e_slonglong;
499   return e_void;
500 }
501 
502 Scalar::Type
503 Scalar::GetValueTypeForUnsignedIntegerWithByteSize(size_t byte_size) {
504   if (byte_size <= sizeof(uint_t))
505     return e_uint;
506   if (byte_size <= sizeof(ulong_t))
507     return e_ulong;
508   if (byte_size <= sizeof(ulonglong_t))
509     return e_ulonglong;
510   return e_void;
511 }
512 
513 Scalar::Type Scalar::GetValueTypeForFloatWithByteSize(size_t byte_size) {
514   if (byte_size == sizeof(float_t))
515     return e_float;
516   if (byte_size == sizeof(double_t))
517     return e_double;
518   if (byte_size == sizeof(long_double_t))
519     return e_long_double;
520   return e_void;
521 }
522 
523 bool Scalar::MakeSigned() {
524   bool success = false;
525 
526   switch (m_type) {
527   case e_void:
528     break;
529   case e_sint:
530     success = true;
531     break;
532   case e_uint:
533     m_type = e_sint;
534     success = true;
535     break;
536   case e_slong:
537     success = true;
538     break;
539   case e_ulong:
540     m_type = e_slong;
541     success = true;
542     break;
543   case e_slonglong:
544     success = true;
545     break;
546   case e_ulonglong:
547     m_type = e_slonglong;
548     success = true;
549     break;
550   case e_sint128:
551     success = true;
552     break;
553   case e_uint128:
554     m_type = e_sint128;
555     success = true;
556     break;
557   case e_sint256:
558     success = true;
559     break;
560   case e_uint256:
561     m_type = e_sint256;
562     success = true;
563     break;
564   case e_sint512:
565     success = true;
566     break;
567   case e_uint512:
568     m_type = e_sint512;
569     success = true;
570     break;
571   case e_float:
572     success = true;
573     break;
574   case e_double:
575     success = true;
576     break;
577   case e_long_double:
578     success = true;
579     break;
580   }
581 
582   return success;
583 }
584 
585 bool Scalar::MakeUnsigned() {
586   bool success = false;
587 
588   switch (m_type) {
589   case e_void:
590     break;
591   case e_sint:
592     m_type = e_uint;
593     success = true;
594     break;
595   case e_uint:
596     success = true;
597     break;
598   case e_slong:
599     m_type = e_ulong;
600     success = true;
601     break;
602   case e_ulong:
603     success = true;
604     break;
605   case e_slonglong:
606     m_type = e_ulonglong;
607     success = true;
608     break;
609   case e_ulonglong:
610     success = true;
611     break;
612   case e_sint128:
613     m_type = e_uint128;
614     success = true;
615     break;
616   case e_uint128:
617     success = true;
618     break;
619   case e_sint256:
620     m_type = e_uint256;
621     success = true;
622     break;
623   case e_uint256:
624     success = true;
625     break;
626   case e_sint512:
627     m_type = e_uint512;
628     success = true;
629     break;
630   case e_uint512:
631     success = true;
632     break;
633   case e_float:
634     success = true;
635     break;
636   case e_double:
637     success = true;
638     break;
639   case e_long_double:
640     success = true;
641     break;
642   }
643 
644   return success;
645 }
646 
647 template <typename T> T Scalar::GetAsSigned(T fail_value) const {
648   switch (m_type) {
649   case e_void:
650     break;
651   case e_sint:
652   case e_uint:
653   case e_slong:
654   case e_ulong:
655   case e_slonglong:
656   case e_ulonglong:
657   case e_sint128:
658   case e_uint128:
659   case e_sint256:
660   case e_uint256:
661   case e_sint512:
662   case e_uint512:
663     return m_integer.sextOrTrunc(sizeof(T) * 8).getSExtValue();
664 
665   case e_float:
666     return static_cast<T>(m_float.convertToFloat());
667   case e_double:
668     return static_cast<T>(m_float.convertToDouble());
669   case e_long_double:
670     llvm::APInt ldbl_val = m_float.bitcastToAPInt();
671     return static_cast<T>(
672         (ldbl_val.sextOrTrunc(sizeof(schar_t) * 8)).getSExtValue());
673   }
674   return fail_value;
675 }
676 
677 template <typename T> T Scalar::GetAsUnsigned(T fail_value) const {
678   switch (m_type) {
679   case e_void:
680     break;
681   case e_sint:
682   case e_uint:
683   case e_slong:
684   case e_ulong:
685   case e_slonglong:
686   case e_ulonglong:
687   case e_sint128:
688   case e_uint128:
689   case e_sint256:
690   case e_uint256:
691   case e_sint512:
692   case e_uint512:
693     return m_integer.zextOrTrunc(sizeof(T) * 8).getZExtValue();
694 
695   case e_float:
696     return static_cast<T>(m_float.convertToFloat());
697   case e_double:
698     return static_cast<T>(m_float.convertToDouble());
699   case e_long_double:
700     llvm::APInt ldbl_val = m_float.bitcastToAPInt();
701     return static_cast<T>((ldbl_val.zextOrTrunc(sizeof(T) * 8)).getZExtValue());
702   }
703   return fail_value;
704 }
705 
706 signed char Scalar::SChar(signed char fail_value) const {
707   return GetAsSigned<signed char>(fail_value);
708 }
709 
710 unsigned char Scalar::UChar(unsigned char fail_value) const {
711   return GetAsUnsigned<unsigned char>(fail_value);
712 }
713 
714 short Scalar::SShort(short fail_value) const {
715   return GetAsSigned<short>(fail_value);
716 }
717 
718 unsigned short Scalar::UShort(unsigned short fail_value) const {
719   return GetAsUnsigned<unsigned short>(fail_value);
720 }
721 
722 int Scalar::SInt(int fail_value) const { return GetAsSigned<int>(fail_value); }
723 
724 unsigned int Scalar::UInt(unsigned int fail_value) const {
725   return GetAsUnsigned<unsigned int>(fail_value);
726 }
727 
728 long Scalar::SLong(long fail_value) const { return GetAsSigned<long>(fail_value); }
729 
730 unsigned long Scalar::ULong(unsigned long fail_value) const {
731   return GetAsUnsigned<unsigned long>(fail_value);
732 }
733 
734 long long Scalar::SLongLong(long long fail_value) const {
735   return GetAsSigned<long long>(fail_value);
736 }
737 
738 unsigned long long Scalar::ULongLong(unsigned long long fail_value) const {
739   return GetAsUnsigned<unsigned long long>(fail_value);
740 }
741 
742 llvm::APInt Scalar::SInt128(const llvm::APInt &fail_value) const {
743   switch (m_type) {
744   case e_void:
745     break;
746   case e_sint:
747   case e_uint:
748   case e_slong:
749   case e_ulong:
750   case e_slonglong:
751   case e_ulonglong:
752   case e_sint128:
753   case e_uint128:
754   case e_sint256:
755   case e_uint256:
756   case e_sint512:
757   case e_uint512:
758     return m_integer;
759   case e_float:
760   case e_double:
761   case e_long_double:
762     return m_float.bitcastToAPInt();
763   }
764   return fail_value;
765 }
766 
767 llvm::APInt Scalar::UInt128(const llvm::APInt &fail_value) const {
768   switch (m_type) {
769   case e_void:
770     break;
771   case e_sint:
772   case e_uint:
773   case e_slong:
774   case e_ulong:
775   case e_slonglong:
776   case e_ulonglong:
777   case e_sint128:
778   case e_uint128:
779   case e_sint256:
780   case e_uint256:
781   case e_sint512:
782   case e_uint512:
783     return m_integer;
784   case e_float:
785   case e_double:
786   case e_long_double:
787     return m_float.bitcastToAPInt();
788   }
789   return fail_value;
790 }
791 
792 float Scalar::Float(float fail_value) const {
793   switch (m_type) {
794   case e_void:
795     break;
796   case e_sint:
797   case e_uint:
798   case e_slong:
799   case e_ulong:
800   case e_slonglong:
801   case e_ulonglong:
802   case e_sint128:
803   case e_uint128:
804   case e_sint256:
805   case e_uint256:
806   case e_sint512:
807   case e_uint512:
808     return llvm::APIntOps::RoundAPIntToFloat(m_integer);
809   case e_float:
810     return m_float.convertToFloat();
811   case e_double:
812     return static_cast<float_t>(m_float.convertToDouble());
813   case e_long_double:
814     llvm::APInt ldbl_val = m_float.bitcastToAPInt();
815     return ldbl_val.bitsToFloat();
816   }
817   return fail_value;
818 }
819 
820 double Scalar::Double(double fail_value) const {
821   switch (m_type) {
822   case e_void:
823     break;
824   case e_sint:
825   case e_uint:
826   case e_slong:
827   case e_ulong:
828   case e_slonglong:
829   case e_ulonglong:
830   case e_sint128:
831   case e_uint128:
832   case e_sint256:
833   case e_uint256:
834   case e_sint512:
835   case e_uint512:
836     return llvm::APIntOps::RoundAPIntToDouble(m_integer);
837   case e_float:
838     return static_cast<double_t>(m_float.convertToFloat());
839   case e_double:
840     return m_float.convertToDouble();
841   case e_long_double:
842     llvm::APInt ldbl_val = m_float.bitcastToAPInt();
843     return ldbl_val.bitsToFloat();
844   }
845   return fail_value;
846 }
847 
848 long double Scalar::LongDouble(long double fail_value) const {
849   switch (m_type) {
850   case e_void:
851     break;
852   case e_sint:
853   case e_uint:
854   case e_slong:
855   case e_ulong:
856   case e_slonglong:
857   case e_ulonglong:
858   case e_sint128:
859   case e_uint128:
860   case e_sint256:
861   case e_uint256:
862   case e_sint512:
863   case e_uint512:
864     return static_cast<long_double_t>(
865         llvm::APIntOps::RoundAPIntToDouble(m_integer));
866   case e_float:
867     return static_cast<long_double_t>(m_float.convertToFloat());
868   case e_double:
869     return static_cast<long_double_t>(m_float.convertToDouble());
870   case e_long_double:
871     llvm::APInt ldbl_val = m_float.bitcastToAPInt();
872     return static_cast<long_double_t>(ldbl_val.bitsToDouble());
873   }
874   return fail_value;
875 }
876 
877 Scalar &Scalar::operator+=(const Scalar &rhs) {
878   Scalar temp_value;
879   const Scalar *a;
880   const Scalar *b;
881   if ((m_type = PromoteToMaxType(*this, rhs, temp_value, a, b)) !=
882       Scalar::e_void) {
883     switch (m_type) {
884     case e_void:
885       break;
886     case e_sint:
887     case e_uint:
888     case e_slong:
889     case e_ulong:
890     case e_slonglong:
891     case e_ulonglong:
892     case e_sint128:
893     case e_uint128:
894     case e_sint256:
895     case e_uint256:
896     case e_sint512:
897     case e_uint512:
898       m_integer = a->m_integer + b->m_integer;
899       break;
900 
901     case e_float:
902     case e_double:
903     case e_long_double:
904       m_float = a->m_float + b->m_float;
905       break;
906     }
907   }
908   return *this;
909 }
910 
911 Scalar &Scalar::operator<<=(const Scalar &rhs) {
912   switch (m_type) {
913   case e_void:
914   case e_float:
915   case e_double:
916   case e_long_double:
917     m_type = e_void;
918     break;
919 
920   case e_sint:
921   case e_uint:
922   case e_slong:
923   case e_ulong:
924   case e_slonglong:
925   case e_ulonglong:
926   case e_sint128:
927   case e_uint128:
928   case e_sint256:
929   case e_uint256:
930   case e_sint512:
931   case e_uint512:
932     switch (rhs.m_type) {
933     case e_void:
934     case e_float:
935     case e_double:
936     case e_long_double:
937       m_type = e_void;
938       break;
939     case e_sint:
940     case e_uint:
941     case e_slong:
942     case e_ulong:
943     case e_slonglong:
944     case e_ulonglong:
945     case e_sint128:
946     case e_uint128:
947     case e_sint256:
948     case e_uint256:
949     case e_sint512:
950     case e_uint512:
951       m_integer = m_integer << rhs.m_integer;
952       break;
953     }
954     break;
955   }
956   return *this;
957 }
958 
959 bool Scalar::ShiftRightLogical(const Scalar &rhs) {
960   switch (m_type) {
961   case e_void:
962   case e_float:
963   case e_double:
964   case e_long_double:
965     m_type = e_void;
966     break;
967 
968   case e_sint:
969   case e_uint:
970   case e_slong:
971   case e_ulong:
972   case e_slonglong:
973   case e_ulonglong:
974   case e_sint128:
975   case e_uint128:
976   case e_sint256:
977   case e_uint256:
978   case e_sint512:
979   case e_uint512:
980     switch (rhs.m_type) {
981     case e_void:
982     case e_float:
983     case e_double:
984     case e_long_double:
985       m_type = e_void;
986       break;
987     case e_sint:
988     case e_uint:
989     case e_slong:
990     case e_ulong:
991     case e_slonglong:
992     case e_ulonglong:
993     case e_sint128:
994     case e_uint128:
995     case e_sint256:
996     case e_uint256:
997     case e_sint512:
998     case e_uint512:
999       m_integer = m_integer.lshr(rhs.m_integer);
1000       break;
1001     }
1002     break;
1003   }
1004   return m_type != e_void;
1005 }
1006 
1007 Scalar &Scalar::operator>>=(const Scalar &rhs) {
1008   switch (m_type) {
1009   case e_void:
1010   case e_float:
1011   case e_double:
1012   case e_long_double:
1013     m_type = e_void;
1014     break;
1015 
1016   case e_sint:
1017   case e_uint:
1018   case e_slong:
1019   case e_ulong:
1020   case e_slonglong:
1021   case e_ulonglong:
1022   case e_sint128:
1023   case e_uint128:
1024   case e_sint256:
1025   case e_uint256:
1026   case e_sint512:
1027   case e_uint512:
1028     switch (rhs.m_type) {
1029     case e_void:
1030     case e_float:
1031     case e_double:
1032     case e_long_double:
1033       m_type = e_void;
1034       break;
1035     case e_sint:
1036     case e_uint:
1037     case e_slong:
1038     case e_ulong:
1039     case e_slonglong:
1040     case e_ulonglong:
1041     case e_sint128:
1042     case e_uint128:
1043     case e_sint256:
1044     case e_uint256:
1045     case e_sint512:
1046     case e_uint512:
1047       m_integer = m_integer.ashr(rhs.m_integer);
1048       break;
1049     }
1050     break;
1051   }
1052   return *this;
1053 }
1054 
1055 Scalar &Scalar::operator&=(const Scalar &rhs) {
1056   switch (m_type) {
1057   case e_void:
1058   case e_float:
1059   case e_double:
1060   case e_long_double:
1061     m_type = e_void;
1062     break;
1063 
1064   case e_sint:
1065   case e_uint:
1066   case e_slong:
1067   case e_ulong:
1068   case e_slonglong:
1069   case e_ulonglong:
1070   case e_sint128:
1071   case e_uint128:
1072   case e_sint256:
1073   case e_uint256:
1074   case e_sint512:
1075   case e_uint512:
1076     switch (rhs.m_type) {
1077     case e_void:
1078     case e_float:
1079     case e_double:
1080     case e_long_double:
1081       m_type = e_void;
1082       break;
1083     case e_sint:
1084     case e_uint:
1085     case e_slong:
1086     case e_ulong:
1087     case e_slonglong:
1088     case e_ulonglong:
1089     case e_sint128:
1090     case e_uint128:
1091     case e_sint256:
1092     case e_uint256:
1093     case e_sint512:
1094     case e_uint512:
1095       m_integer &= rhs.m_integer;
1096       break;
1097     }
1098     break;
1099   }
1100   return *this;
1101 }
1102 
1103 bool Scalar::AbsoluteValue() {
1104   switch (m_type) {
1105   case e_void:
1106     break;
1107 
1108   case e_sint:
1109   case e_slong:
1110   case e_slonglong:
1111   case e_sint128:
1112   case e_sint256:
1113   case e_sint512:
1114     if (m_integer.isNegative())
1115       m_integer = -m_integer;
1116     return true;
1117 
1118   case e_uint:
1119   case e_ulong:
1120   case e_ulonglong:
1121     return true;
1122   case e_uint128:
1123   case e_uint256:
1124   case e_uint512:
1125   case e_float:
1126   case e_double:
1127   case e_long_double:
1128     m_float.clearSign();
1129     return true;
1130   }
1131   return false;
1132 }
1133 
1134 bool Scalar::UnaryNegate() {
1135   switch (m_type) {
1136   case e_void:
1137     break;
1138   case e_sint:
1139   case e_uint:
1140   case e_slong:
1141   case e_ulong:
1142   case e_slonglong:
1143   case e_ulonglong:
1144   case e_sint128:
1145   case e_uint128:
1146   case e_sint256:
1147   case e_uint256:
1148   case e_sint512:
1149   case e_uint512:
1150     m_integer = -m_integer;
1151     return true;
1152   case e_float:
1153   case e_double:
1154   case e_long_double:
1155     m_float.changeSign();
1156     return true;
1157   }
1158   return false;
1159 }
1160 
1161 bool Scalar::OnesComplement() {
1162   switch (m_type) {
1163   case e_sint:
1164   case e_uint:
1165   case e_slong:
1166   case e_ulong:
1167   case e_slonglong:
1168   case e_ulonglong:
1169   case e_sint128:
1170   case e_uint128:
1171   case e_sint256:
1172   case e_uint256:
1173   case e_sint512:
1174   case e_uint512:
1175     m_integer = ~m_integer;
1176     return true;
1177 
1178   case e_void:
1179   case e_float:
1180   case e_double:
1181   case e_long_double:
1182     break;
1183   }
1184   return false;
1185 }
1186 
1187 const Scalar lldb_private::operator+(const Scalar &lhs, const Scalar &rhs) {
1188   Scalar result;
1189   Scalar temp_value;
1190   const Scalar *a;
1191   const Scalar *b;
1192   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
1193       Scalar::e_void) {
1194     switch (result.m_type) {
1195     case Scalar::e_void:
1196       break;
1197     case Scalar::e_sint:
1198     case Scalar::e_uint:
1199     case Scalar::e_slong:
1200     case Scalar::e_ulong:
1201     case Scalar::e_slonglong:
1202     case Scalar::e_ulonglong:
1203     case Scalar::e_sint128:
1204     case Scalar::e_uint128:
1205     case Scalar::e_sint256:
1206     case Scalar::e_uint256:
1207     case Scalar::e_sint512:
1208     case Scalar::e_uint512:
1209       result.m_integer = a->m_integer + b->m_integer;
1210       break;
1211     case Scalar::e_float:
1212     case Scalar::e_double:
1213     case Scalar::e_long_double:
1214       result.m_float = a->m_float + b->m_float;
1215       break;
1216     }
1217   }
1218   return result;
1219 }
1220 
1221 const Scalar lldb_private::operator-(const Scalar &lhs, const Scalar &rhs) {
1222   Scalar result;
1223   Scalar temp_value;
1224   const Scalar *a;
1225   const Scalar *b;
1226   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
1227       Scalar::e_void) {
1228     switch (result.m_type) {
1229     case Scalar::e_void:
1230       break;
1231     case Scalar::e_sint:
1232     case Scalar::e_uint:
1233     case Scalar::e_slong:
1234     case Scalar::e_ulong:
1235     case Scalar::e_slonglong:
1236     case Scalar::e_ulonglong:
1237     case Scalar::e_sint128:
1238     case Scalar::e_uint128:
1239     case Scalar::e_sint256:
1240     case Scalar::e_uint256:
1241     case Scalar::e_sint512:
1242     case Scalar::e_uint512:
1243       result.m_integer = a->m_integer - b->m_integer;
1244       break;
1245     case Scalar::e_float:
1246     case Scalar::e_double:
1247     case Scalar::e_long_double:
1248       result.m_float = a->m_float - b->m_float;
1249       break;
1250     }
1251   }
1252   return result;
1253 }
1254 
1255 const Scalar lldb_private::operator/(const Scalar &lhs, const Scalar &rhs) {
1256   Scalar result;
1257   Scalar temp_value;
1258   const Scalar *a;
1259   const Scalar *b;
1260   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
1261       Scalar::e_void) {
1262     switch (result.m_type) {
1263     case Scalar::e_void:
1264       break;
1265     case Scalar::e_sint:
1266     case Scalar::e_slong:
1267     case Scalar::e_slonglong:
1268     case Scalar::e_sint128:
1269     case Scalar::e_sint256:
1270     case Scalar::e_sint512:
1271       if (b->m_integer != 0) {
1272         result.m_integer = a->m_integer.sdiv(b->m_integer);
1273         return result;
1274       }
1275       break;
1276     case Scalar::e_uint:
1277     case Scalar::e_ulong:
1278     case Scalar::e_ulonglong:
1279     case Scalar::e_uint128:
1280     case Scalar::e_uint256:
1281     case Scalar::e_uint512:
1282       if (b->m_integer != 0) {
1283         result.m_integer = a->m_integer.udiv(b->m_integer);
1284         return result;
1285       }
1286       break;
1287     case Scalar::e_float:
1288     case Scalar::e_double:
1289     case Scalar::e_long_double:
1290       if (!b->m_float.isZero()) {
1291         result.m_float = a->m_float / b->m_float;
1292         return result;
1293       }
1294       break;
1295     }
1296   }
1297   // For division only, the only way it should make it here is if a promotion
1298   // failed, or if we are trying to do a divide by zero.
1299   result.m_type = Scalar::e_void;
1300   return result;
1301 }
1302 
1303 const Scalar lldb_private::operator*(const Scalar &lhs, const Scalar &rhs) {
1304   Scalar result;
1305   Scalar temp_value;
1306   const Scalar *a;
1307   const Scalar *b;
1308   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
1309       Scalar::e_void) {
1310     switch (result.m_type) {
1311     case Scalar::e_void:
1312       break;
1313     case Scalar::e_sint:
1314     case Scalar::e_uint:
1315     case Scalar::e_slong:
1316     case Scalar::e_ulong:
1317     case Scalar::e_slonglong:
1318     case Scalar::e_ulonglong:
1319     case Scalar::e_sint128:
1320     case Scalar::e_uint128:
1321     case Scalar::e_sint256:
1322     case Scalar::e_uint256:
1323     case Scalar::e_sint512:
1324     case Scalar::e_uint512:
1325       result.m_integer = a->m_integer * b->m_integer;
1326       break;
1327     case Scalar::e_float:
1328     case Scalar::e_double:
1329     case Scalar::e_long_double:
1330       result.m_float = a->m_float * b->m_float;
1331       break;
1332     }
1333   }
1334   return result;
1335 }
1336 
1337 const Scalar lldb_private::operator&(const Scalar &lhs, const Scalar &rhs) {
1338   Scalar result;
1339   Scalar temp_value;
1340   const Scalar *a;
1341   const Scalar *b;
1342   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
1343       Scalar::e_void) {
1344     switch (result.m_type) {
1345     case Scalar::e_sint:
1346     case Scalar::e_uint:
1347     case Scalar::e_slong:
1348     case Scalar::e_ulong:
1349     case Scalar::e_slonglong:
1350     case Scalar::e_ulonglong:
1351     case Scalar::e_sint128:
1352     case Scalar::e_uint128:
1353     case Scalar::e_sint256:
1354     case Scalar::e_uint256:
1355     case Scalar::e_sint512:
1356     case Scalar::e_uint512:
1357       result.m_integer = a->m_integer & b->m_integer;
1358       break;
1359     case Scalar::e_void:
1360     case Scalar::e_float:
1361     case Scalar::e_double:
1362     case Scalar::e_long_double:
1363       // No bitwise AND on floats, doubles of long doubles
1364       result.m_type = Scalar::e_void;
1365       break;
1366     }
1367   }
1368   return result;
1369 }
1370 
1371 const Scalar lldb_private::operator|(const Scalar &lhs, const Scalar &rhs) {
1372   Scalar result;
1373   Scalar temp_value;
1374   const Scalar *a;
1375   const Scalar *b;
1376   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
1377       Scalar::e_void) {
1378     switch (result.m_type) {
1379     case Scalar::e_sint:
1380     case Scalar::e_uint:
1381     case Scalar::e_slong:
1382     case Scalar::e_ulong:
1383     case Scalar::e_slonglong:
1384     case Scalar::e_ulonglong:
1385     case Scalar::e_sint128:
1386     case Scalar::e_uint128:
1387     case Scalar::e_sint256:
1388     case Scalar::e_uint256:
1389     case Scalar::e_sint512:
1390     case Scalar::e_uint512:
1391       result.m_integer = a->m_integer | b->m_integer;
1392       break;
1393 
1394     case Scalar::e_void:
1395     case Scalar::e_float:
1396     case Scalar::e_double:
1397     case Scalar::e_long_double:
1398       // No bitwise AND on floats, doubles of long doubles
1399       result.m_type = Scalar::e_void;
1400       break;
1401     }
1402   }
1403   return result;
1404 }
1405 
1406 const Scalar lldb_private::operator%(const Scalar &lhs, const Scalar &rhs) {
1407   Scalar result;
1408   Scalar temp_value;
1409   const Scalar *a;
1410   const Scalar *b;
1411   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
1412       Scalar::e_void) {
1413     switch (result.m_type) {
1414     default:
1415       break;
1416     case Scalar::e_void:
1417       break;
1418     case Scalar::e_sint:
1419     case Scalar::e_slong:
1420     case Scalar::e_slonglong:
1421     case Scalar::e_sint128:
1422     case Scalar::e_sint256:
1423     case Scalar::e_sint512:
1424       if (b->m_integer != 0) {
1425         result.m_integer = a->m_integer.srem(b->m_integer);
1426         return result;
1427       }
1428       break;
1429     case Scalar::e_uint:
1430     case Scalar::e_ulong:
1431     case Scalar::e_ulonglong:
1432     case Scalar::e_uint128:
1433     case Scalar::e_uint256:
1434     case Scalar::e_uint512:
1435       if (b->m_integer != 0) {
1436         result.m_integer = a->m_integer.urem(b->m_integer);
1437         return result;
1438       }
1439       break;
1440     }
1441   }
1442   result.m_type = Scalar::e_void;
1443   return result;
1444 }
1445 
1446 const Scalar lldb_private::operator^(const Scalar &lhs, const Scalar &rhs) {
1447   Scalar result;
1448   Scalar temp_value;
1449   const Scalar *a;
1450   const Scalar *b;
1451   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
1452       Scalar::e_void) {
1453     switch (result.m_type) {
1454     case Scalar::e_sint:
1455     case Scalar::e_uint:
1456     case Scalar::e_slong:
1457     case Scalar::e_ulong:
1458     case Scalar::e_slonglong:
1459     case Scalar::e_ulonglong:
1460     case Scalar::e_sint128:
1461     case Scalar::e_uint128:
1462     case Scalar::e_sint256:
1463     case Scalar::e_uint256:
1464     case Scalar::e_sint512:
1465     case Scalar::e_uint512:
1466       result.m_integer = a->m_integer ^ b->m_integer;
1467       break;
1468 
1469     case Scalar::e_void:
1470     case Scalar::e_float:
1471     case Scalar::e_double:
1472     case Scalar::e_long_double:
1473       // No bitwise AND on floats, doubles of long doubles
1474       result.m_type = Scalar::e_void;
1475       break;
1476     }
1477   }
1478   return result;
1479 }
1480 
1481 const Scalar lldb_private::operator<<(const Scalar &lhs, const Scalar &rhs) {
1482   Scalar result = lhs;
1483   result <<= rhs;
1484   return result;
1485 }
1486 
1487 const Scalar lldb_private::operator>>(const Scalar &lhs, const Scalar &rhs) {
1488   Scalar result = lhs;
1489   result >>= rhs;
1490   return result;
1491 }
1492 
1493 Status Scalar::SetValueFromCString(const char *value_str, Encoding encoding,
1494                                    size_t byte_size) {
1495   Status error;
1496   if (value_str == nullptr || value_str[0] == '\0') {
1497     error.SetErrorString("Invalid c-string value string.");
1498     return error;
1499   }
1500   switch (encoding) {
1501   case eEncodingInvalid:
1502     error.SetErrorString("Invalid encoding.");
1503     break;
1504 
1505   case eEncodingUint:
1506     if (byte_size <= sizeof(uint64_t)) {
1507       uint64_t uval64;
1508       if (!llvm::to_integer(value_str, uval64))
1509         error.SetErrorStringWithFormat(
1510             "'%s' is not a valid unsigned integer string value", value_str);
1511       else if (!UIntValueIsValidForSize(uval64, byte_size))
1512         error.SetErrorStringWithFormat(
1513             "value 0x%" PRIx64 " is too large to fit in a %" PRIu64
1514             " byte unsigned integer value",
1515             uval64, static_cast<uint64_t>(byte_size));
1516       else {
1517         m_type = Scalar::GetValueTypeForUnsignedIntegerWithByteSize(byte_size);
1518         switch (m_type) {
1519         case e_uint:
1520           m_integer = llvm::APInt(sizeof(uint_t) * 8, uval64, false);
1521           break;
1522         case e_ulong:
1523           m_integer = llvm::APInt(sizeof(ulong_t) * 8, uval64, false);
1524           break;
1525         case e_ulonglong:
1526           m_integer = llvm::APInt(sizeof(ulonglong_t) * 8, uval64, false);
1527           break;
1528         default:
1529           error.SetErrorStringWithFormat(
1530               "unsupported unsigned integer byte size: %" PRIu64 "",
1531               static_cast<uint64_t>(byte_size));
1532           break;
1533         }
1534       }
1535     } else {
1536       error.SetErrorStringWithFormat(
1537           "unsupported unsigned integer byte size: %" PRIu64 "",
1538           static_cast<uint64_t>(byte_size));
1539       return error;
1540     }
1541     break;
1542 
1543   case eEncodingSint:
1544     if (byte_size <= sizeof(int64_t)) {
1545       int64_t sval64;
1546       if (!llvm::to_integer(value_str, sval64))
1547         error.SetErrorStringWithFormat(
1548             "'%s' is not a valid signed integer string value", value_str);
1549       else if (!SIntValueIsValidForSize(sval64, byte_size))
1550         error.SetErrorStringWithFormat(
1551             "value 0x%" PRIx64 " is too large to fit in a %" PRIu64
1552             " byte signed integer value",
1553             sval64, static_cast<uint64_t>(byte_size));
1554       else {
1555         m_type = Scalar::GetValueTypeForSignedIntegerWithByteSize(byte_size);
1556         switch (m_type) {
1557         case e_sint:
1558           m_integer = llvm::APInt(sizeof(sint_t) * 8, sval64, true);
1559           break;
1560         case e_slong:
1561           m_integer = llvm::APInt(sizeof(slong_t) * 8, sval64, true);
1562           break;
1563         case e_slonglong:
1564           m_integer = llvm::APInt(sizeof(slonglong_t) * 8, sval64, true);
1565           break;
1566         default:
1567           error.SetErrorStringWithFormat(
1568               "unsupported signed integer byte size: %" PRIu64 "",
1569               static_cast<uint64_t>(byte_size));
1570           break;
1571         }
1572       }
1573     } else {
1574       error.SetErrorStringWithFormat(
1575           "unsupported signed integer byte size: %" PRIu64 "",
1576           static_cast<uint64_t>(byte_size));
1577       return error;
1578     }
1579     break;
1580 
1581   case eEncodingIEEE754:
1582     static float f_val;
1583     static double d_val;
1584     static long double l_val;
1585     if (byte_size == sizeof(float)) {
1586       if (::sscanf(value_str, "%f", &f_val) == 1) {
1587         m_float = llvm::APFloat(f_val);
1588         m_type = e_float;
1589       } else
1590         error.SetErrorStringWithFormat("'%s' is not a valid float string value",
1591                                        value_str);
1592     } else if (byte_size == sizeof(double)) {
1593       if (::sscanf(value_str, "%lf", &d_val) == 1) {
1594         m_float = llvm::APFloat(d_val);
1595         m_type = e_double;
1596       } else
1597         error.SetErrorStringWithFormat("'%s' is not a valid float string value",
1598                                        value_str);
1599     } else if (byte_size == sizeof(long double)) {
1600       if (::sscanf(value_str, "%Lf", &l_val) == 1) {
1601         m_float = llvm::APFloat(
1602             llvm::APFloat::x87DoubleExtended(),
1603             llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
1604                         (reinterpret_cast<type128 *>(&l_val))->x));
1605         m_type = e_long_double;
1606       } else
1607         error.SetErrorStringWithFormat("'%s' is not a valid float string value",
1608                                        value_str);
1609     } else {
1610       error.SetErrorStringWithFormat("unsupported float byte size: %" PRIu64 "",
1611                                      static_cast<uint64_t>(byte_size));
1612       return error;
1613     }
1614     break;
1615 
1616   case eEncodingVector:
1617     error.SetErrorString("vector encoding unsupported.");
1618     break;
1619   }
1620   if (error.Fail())
1621     m_type = e_void;
1622 
1623   return error;
1624 }
1625 
1626 Status Scalar::SetValueFromData(DataExtractor &data, lldb::Encoding encoding,
1627                                 size_t byte_size) {
1628   Status error;
1629 
1630   type128 int128;
1631   type256 int256;
1632   switch (encoding) {
1633   case lldb::eEncodingInvalid:
1634     error.SetErrorString("invalid encoding");
1635     break;
1636   case lldb::eEncodingVector:
1637     error.SetErrorString("vector encoding unsupported");
1638     break;
1639   case lldb::eEncodingUint: {
1640     lldb::offset_t offset = 0;
1641 
1642     switch (byte_size) {
1643     case 1:
1644       operator=(data.GetU8(&offset));
1645       break;
1646     case 2:
1647       operator=(data.GetU16(&offset));
1648       break;
1649     case 4:
1650       operator=(data.GetU32(&offset));
1651       break;
1652     case 8:
1653       operator=(data.GetU64(&offset));
1654       break;
1655     case 16:
1656       if (data.GetByteOrder() == eByteOrderBig) {
1657         int128.x[1] = data.GetU64(&offset);
1658         int128.x[0] = data.GetU64(&offset);
1659       } else {
1660         int128.x[0] = data.GetU64(&offset);
1661         int128.x[1] = data.GetU64(&offset);
1662       }
1663       operator=(llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128, int128.x));
1664       break;
1665     case 32:
1666       if (data.GetByteOrder() == eByteOrderBig) {
1667         int256.x[3] = data.GetU64(&offset);
1668         int256.x[2] = data.GetU64(&offset);
1669         int256.x[1] = data.GetU64(&offset);
1670         int256.x[0] = data.GetU64(&offset);
1671       } else {
1672         int256.x[0] = data.GetU64(&offset);
1673         int256.x[1] = data.GetU64(&offset);
1674         int256.x[2] = data.GetU64(&offset);
1675         int256.x[3] = data.GetU64(&offset);
1676       }
1677       operator=(llvm::APInt(BITWIDTH_INT256, NUM_OF_WORDS_INT256, int256.x));
1678       break;
1679     default:
1680       error.SetErrorStringWithFormat(
1681           "unsupported unsigned integer byte size: %" PRIu64 "",
1682           static_cast<uint64_t>(byte_size));
1683       break;
1684     }
1685   } break;
1686   case lldb::eEncodingSint: {
1687     lldb::offset_t offset = 0;
1688 
1689     switch (byte_size) {
1690     case 1:
1691       operator=(static_cast<int8_t>(data.GetU8(&offset)));
1692       break;
1693     case 2:
1694       operator=(static_cast<int16_t>(data.GetU16(&offset)));
1695       break;
1696     case 4:
1697       operator=(static_cast<int32_t>(data.GetU32(&offset)));
1698       break;
1699     case 8:
1700       operator=(static_cast<int64_t>(data.GetU64(&offset)));
1701       break;
1702     case 16:
1703       if (data.GetByteOrder() == eByteOrderBig) {
1704         int128.x[1] = data.GetU64(&offset);
1705         int128.x[0] = data.GetU64(&offset);
1706       } else {
1707         int128.x[0] = data.GetU64(&offset);
1708         int128.x[1] = data.GetU64(&offset);
1709       }
1710       operator=(llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128, int128.x));
1711       break;
1712     case 32:
1713       if (data.GetByteOrder() == eByteOrderBig) {
1714         int256.x[3] = data.GetU64(&offset);
1715         int256.x[2] = data.GetU64(&offset);
1716         int256.x[1] = data.GetU64(&offset);
1717         int256.x[0] = data.GetU64(&offset);
1718       } else {
1719         int256.x[0] = data.GetU64(&offset);
1720         int256.x[1] = data.GetU64(&offset);
1721         int256.x[2] = data.GetU64(&offset);
1722         int256.x[3] = data.GetU64(&offset);
1723       }
1724       operator=(llvm::APInt(BITWIDTH_INT256, NUM_OF_WORDS_INT256, int256.x));
1725       break;
1726     default:
1727       error.SetErrorStringWithFormat(
1728           "unsupported signed integer byte size: %" PRIu64 "",
1729           static_cast<uint64_t>(byte_size));
1730       break;
1731     }
1732   } break;
1733   case lldb::eEncodingIEEE754: {
1734     lldb::offset_t offset = 0;
1735 
1736     if (byte_size == sizeof(float))
1737       operator=(data.GetFloat(&offset));
1738     else if (byte_size == sizeof(double))
1739       operator=(data.GetDouble(&offset));
1740     else if (byte_size == sizeof(long double))
1741       operator=(data.GetLongDouble(&offset));
1742     else
1743       error.SetErrorStringWithFormat("unsupported float byte size: %" PRIu64 "",
1744                                      static_cast<uint64_t>(byte_size));
1745   } break;
1746   }
1747 
1748   return error;
1749 }
1750 
1751 bool Scalar::SignExtend(uint32_t sign_bit_pos) {
1752   const uint32_t max_bit_pos = GetByteSize() * 8;
1753 
1754   if (sign_bit_pos < max_bit_pos) {
1755     switch (m_type) {
1756     case Scalar::e_void:
1757     case Scalar::e_float:
1758     case Scalar::e_double:
1759     case Scalar::e_long_double:
1760       return false;
1761 
1762     case Scalar::e_sint:
1763     case Scalar::e_uint:
1764     case Scalar::e_slong:
1765     case Scalar::e_ulong:
1766     case Scalar::e_slonglong:
1767     case Scalar::e_ulonglong:
1768     case Scalar::e_sint128:
1769     case Scalar::e_uint128:
1770     case Scalar::e_sint256:
1771     case Scalar::e_uint256:
1772     case Scalar::e_sint512:
1773     case Scalar::e_uint512:
1774       if (max_bit_pos == sign_bit_pos)
1775         return true;
1776       else if (sign_bit_pos < (max_bit_pos - 1)) {
1777         llvm::APInt sign_bit = llvm::APInt::getSignMask(sign_bit_pos + 1);
1778         llvm::APInt bitwize_and = m_integer & sign_bit;
1779         if (bitwize_and.getBoolValue()) {
1780           const llvm::APInt mask =
1781               ~(sign_bit) + llvm::APInt(m_integer.getBitWidth(), 1);
1782           m_integer |= mask;
1783         }
1784         return true;
1785       }
1786       break;
1787     }
1788   }
1789   return false;
1790 }
1791 
1792 size_t Scalar::GetAsMemoryData(void *dst, size_t dst_len,
1793                                lldb::ByteOrder dst_byte_order,
1794                                Status &error) const {
1795   // Get a data extractor that points to the native scalar data
1796   DataExtractor data;
1797   if (!GetData(data)) {
1798     error.SetErrorString("invalid scalar value");
1799     return 0;
1800   }
1801 
1802   const size_t src_len = data.GetByteSize();
1803 
1804   // Prepare a memory buffer that contains some or all of the register value
1805   const size_t bytes_copied =
1806       data.CopyByteOrderedData(0,               // src offset
1807                                src_len,         // src length
1808                                dst,             // dst buffer
1809                                dst_len,         // dst length
1810                                dst_byte_order); // dst byte order
1811   if (bytes_copied == 0)
1812     error.SetErrorString("failed to copy data");
1813 
1814   return bytes_copied;
1815 }
1816 
1817 bool Scalar::ExtractBitfield(uint32_t bit_size, uint32_t bit_offset) {
1818   if (bit_size == 0)
1819     return true;
1820 
1821   switch (m_type) {
1822   case Scalar::e_void:
1823   case Scalar::e_float:
1824   case Scalar::e_double:
1825   case Scalar::e_long_double:
1826     break;
1827 
1828   case Scalar::e_sint:
1829   case Scalar::e_slong:
1830   case Scalar::e_slonglong:
1831   case Scalar::e_sint128:
1832   case Scalar::e_sint256:
1833   case Scalar::e_sint512:
1834     m_integer = m_integer.ashr(bit_offset)
1835                     .sextOrTrunc(bit_size)
1836                     .sextOrSelf(8 * GetByteSize());
1837     return true;
1838 
1839   case Scalar::e_uint:
1840   case Scalar::e_ulong:
1841   case Scalar::e_ulonglong:
1842   case Scalar::e_uint128:
1843   case Scalar::e_uint256:
1844   case Scalar::e_uint512:
1845     m_integer = m_integer.lshr(bit_offset)
1846                     .zextOrTrunc(bit_size)
1847                     .zextOrSelf(8 * GetByteSize());
1848     return true;
1849   }
1850   return false;
1851 }
1852 
1853 bool lldb_private::operator==(const Scalar &lhs, const Scalar &rhs) {
1854   // If either entry is void then we can just compare the types
1855   if (lhs.m_type == Scalar::e_void || rhs.m_type == Scalar::e_void)
1856     return lhs.m_type == rhs.m_type;
1857 
1858   Scalar temp_value;
1859   const Scalar *a;
1860   const Scalar *b;
1861   llvm::APFloat::cmpResult result;
1862   switch (PromoteToMaxType(lhs, rhs, temp_value, a, b)) {
1863   case Scalar::e_void:
1864     break;
1865   case Scalar::e_sint:
1866   case Scalar::e_uint:
1867   case Scalar::e_slong:
1868   case Scalar::e_ulong:
1869   case Scalar::e_slonglong:
1870   case Scalar::e_ulonglong:
1871   case Scalar::e_sint128:
1872   case Scalar::e_uint128:
1873   case Scalar::e_sint256:
1874   case Scalar::e_uint256:
1875   case Scalar::e_sint512:
1876   case Scalar::e_uint512:
1877     return a->m_integer == b->m_integer;
1878   case Scalar::e_float:
1879   case Scalar::e_double:
1880   case Scalar::e_long_double:
1881     result = a->m_float.compare(b->m_float);
1882     if (result == llvm::APFloat::cmpEqual)
1883       return true;
1884   }
1885   return false;
1886 }
1887 
1888 bool lldb_private::operator!=(const Scalar &lhs, const Scalar &rhs) {
1889   return !(lhs == rhs);
1890 }
1891 
1892 bool lldb_private::operator<(const Scalar &lhs, const Scalar &rhs) {
1893   if (lhs.m_type == Scalar::e_void || rhs.m_type == Scalar::e_void)
1894     return false;
1895 
1896   Scalar temp_value;
1897   const Scalar *a;
1898   const Scalar *b;
1899   llvm::APFloat::cmpResult result;
1900   switch (PromoteToMaxType(lhs, rhs, temp_value, a, b)) {
1901   case Scalar::e_void:
1902     break;
1903   case Scalar::e_sint:
1904   case Scalar::e_slong:
1905   case Scalar::e_slonglong:
1906   case Scalar::e_sint128:
1907   case Scalar::e_sint256:
1908   case Scalar::e_sint512:
1909   case Scalar::e_uint512:
1910     return a->m_integer.slt(b->m_integer);
1911   case Scalar::e_uint:
1912   case Scalar::e_ulong:
1913   case Scalar::e_ulonglong:
1914   case Scalar::e_uint128:
1915   case Scalar::e_uint256:
1916     return a->m_integer.ult(b->m_integer);
1917   case Scalar::e_float:
1918   case Scalar::e_double:
1919   case Scalar::e_long_double:
1920     result = a->m_float.compare(b->m_float);
1921     if (result == llvm::APFloat::cmpLessThan)
1922       return true;
1923   }
1924   return false;
1925 }
1926 
1927 bool lldb_private::operator<=(const Scalar &lhs, const Scalar &rhs) {
1928   return !(rhs < lhs);
1929 }
1930 
1931 bool lldb_private::operator>(const Scalar &lhs, const Scalar &rhs) {
1932   return rhs < lhs;
1933 }
1934 
1935 bool lldb_private::operator>=(const Scalar &lhs, const Scalar &rhs) {
1936   return !(lhs < rhs);
1937 }
1938 
1939 bool Scalar::ClearBit(uint32_t bit) {
1940   switch (m_type) {
1941   case e_void:
1942     break;
1943   case e_sint:
1944   case e_uint:
1945   case e_slong:
1946   case e_ulong:
1947   case e_slonglong:
1948   case e_ulonglong:
1949   case e_sint128:
1950   case e_uint128:
1951   case e_sint256:
1952   case e_uint256:
1953   case e_sint512:
1954   case e_uint512:
1955     m_integer.clearBit(bit);
1956     return true;
1957   case e_float:
1958   case e_double:
1959   case e_long_double:
1960     break;
1961   }
1962   return false;
1963 }
1964 
1965 bool Scalar::SetBit(uint32_t bit) {
1966   switch (m_type) {
1967   case e_void:
1968     break;
1969   case e_sint:
1970   case e_uint:
1971   case e_slong:
1972   case e_ulong:
1973   case e_slonglong:
1974   case e_ulonglong:
1975   case e_sint128:
1976   case e_uint128:
1977   case e_sint256:
1978   case e_uint256:
1979   case e_sint512:
1980   case e_uint512:
1981     m_integer.setBit(bit);
1982     return true;
1983   case e_float:
1984   case e_double:
1985   case e_long_double:
1986     break;
1987   }
1988   return false;
1989 }
1990 
1991 llvm::raw_ostream &lldb_private::operator<<(llvm::raw_ostream &os, const Scalar &scalar) {
1992   StreamString s;
1993   scalar.GetValue(&s, /*show_type*/ true);
1994   return os << s.GetString();
1995 }
1996