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 #include "llvm/ADT/APSInt.h"
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::GetAs(T fail_value) const {
648   switch (GetCategory(m_type)) {
649   case Category::Void:
650     break;
651   case Category::Integral:
652     if (IsSigned(m_type))
653       return m_integer.sextOrTrunc(sizeof(T) * 8).getSExtValue();
654     return m_integer.zextOrTrunc(sizeof(T) * 8).getZExtValue();
655   case Category::Float: {
656     llvm::APSInt result(sizeof(T) * 8, std::is_unsigned<T>::value);
657     bool isExact;
658     m_float.convertToInteger(result, llvm::APFloat::rmTowardZero, &isExact);
659     return result.getSExtValue();
660   }
661   }
662   return fail_value;
663 }
664 
665 signed char Scalar::SChar(signed char fail_value) const {
666   return GetAs<signed char>(fail_value);
667 }
668 
669 unsigned char Scalar::UChar(unsigned char fail_value) const {
670   return GetAs<unsigned char>(fail_value);
671 }
672 
673 short Scalar::SShort(short fail_value) const {
674   return GetAs<short>(fail_value);
675 }
676 
677 unsigned short Scalar::UShort(unsigned short fail_value) const {
678   return GetAs<unsigned short>(fail_value);
679 }
680 
681 int Scalar::SInt(int fail_value) const { return GetAs<int>(fail_value); }
682 
683 unsigned int Scalar::UInt(unsigned int fail_value) const {
684   return GetAs<unsigned int>(fail_value);
685 }
686 
687 long Scalar::SLong(long fail_value) const { return GetAs<long>(fail_value); }
688 
689 unsigned long Scalar::ULong(unsigned long fail_value) const {
690   return GetAs<unsigned long>(fail_value);
691 }
692 
693 long long Scalar::SLongLong(long long fail_value) const {
694   return GetAs<long long>(fail_value);
695 }
696 
697 unsigned long long Scalar::ULongLong(unsigned long long fail_value) const {
698   return GetAs<unsigned long long>(fail_value);
699 }
700 
701 llvm::APInt Scalar::SInt128(const llvm::APInt &fail_value) const {
702   switch (m_type) {
703   case e_void:
704     break;
705   case e_sint:
706   case e_uint:
707   case e_slong:
708   case e_ulong:
709   case e_slonglong:
710   case e_ulonglong:
711   case e_sint128:
712   case e_uint128:
713   case e_sint256:
714   case e_uint256:
715   case e_sint512:
716   case e_uint512:
717     return m_integer;
718   case e_float:
719   case e_double:
720   case e_long_double:
721     return m_float.bitcastToAPInt();
722   }
723   return fail_value;
724 }
725 
726 llvm::APInt Scalar::UInt128(const llvm::APInt &fail_value) const {
727   switch (m_type) {
728   case e_void:
729     break;
730   case e_sint:
731   case e_uint:
732   case e_slong:
733   case e_ulong:
734   case e_slonglong:
735   case e_ulonglong:
736   case e_sint128:
737   case e_uint128:
738   case e_sint256:
739   case e_uint256:
740   case e_sint512:
741   case e_uint512:
742     return m_integer;
743   case e_float:
744   case e_double:
745   case e_long_double:
746     return m_float.bitcastToAPInt();
747   }
748   return fail_value;
749 }
750 
751 float Scalar::Float(float fail_value) const {
752   switch (m_type) {
753   case e_void:
754     break;
755   case e_sint:
756   case e_slong:
757   case e_slonglong:
758   case e_sint128:
759   case e_sint256:
760   case e_sint512:
761     return llvm::APIntOps::RoundSignedAPIntToFloat(m_integer);
762 
763   case e_uint:
764   case e_ulong:
765   case e_ulonglong:
766   case e_uint128:
767   case e_uint256:
768   case e_uint512:
769     return llvm::APIntOps::RoundAPIntToFloat(m_integer);
770 
771   case e_float:
772     return m_float.convertToFloat();
773   case e_double:
774     return static_cast<float_t>(m_float.convertToDouble());
775   case e_long_double:
776     llvm::APInt ldbl_val = m_float.bitcastToAPInt();
777     return ldbl_val.bitsToFloat();
778   }
779   return fail_value;
780 }
781 
782 double Scalar::Double(double fail_value) const {
783   switch (m_type) {
784   case e_void:
785     break;
786   case e_sint:
787   case e_slong:
788   case e_slonglong:
789   case e_sint128:
790   case e_sint256:
791   case e_sint512:
792     return llvm::APIntOps::RoundSignedAPIntToDouble(m_integer);
793 
794   case e_uint:
795   case e_ulong:
796   case e_ulonglong:
797   case e_uint128:
798   case e_uint256:
799   case e_uint512:
800     return llvm::APIntOps::RoundAPIntToDouble(m_integer);
801 
802   case e_float:
803     return static_cast<double_t>(m_float.convertToFloat());
804   case e_double:
805     return m_float.convertToDouble();
806   case e_long_double:
807     llvm::APInt ldbl_val = m_float.bitcastToAPInt();
808     return ldbl_val.bitsToFloat();
809   }
810   return fail_value;
811 }
812 
813 long double Scalar::LongDouble(long double fail_value) const {
814   switch (m_type) {
815   case e_void:
816     break;
817   case e_sint:
818   case e_slong:
819   case e_slonglong:
820   case e_sint128:
821   case e_sint256:
822   case e_sint512:
823     return static_cast<long_double_t>(
824         llvm::APIntOps::RoundSignedAPIntToDouble(m_integer));
825 
826   case e_uint:
827   case e_ulong:
828   case e_ulonglong:
829   case e_uint128:
830   case e_uint256:
831   case e_uint512:
832     return static_cast<long_double_t>(
833         llvm::APIntOps::RoundAPIntToDouble(m_integer));
834 
835   case e_float:
836     return static_cast<long_double_t>(m_float.convertToFloat());
837   case e_double:
838     return static_cast<long_double_t>(m_float.convertToDouble());
839   case e_long_double:
840     llvm::APInt ldbl_val = m_float.bitcastToAPInt();
841     return static_cast<long_double_t>(ldbl_val.bitsToDouble());
842   }
843   return fail_value;
844 }
845 
846 Scalar &Scalar::operator+=(const Scalar &rhs) {
847   Scalar temp_value;
848   const Scalar *a;
849   const Scalar *b;
850   if ((m_type = PromoteToMaxType(*this, rhs, temp_value, a, b)) !=
851       Scalar::e_void) {
852     switch (m_type) {
853     case e_void:
854       break;
855     case e_sint:
856     case e_uint:
857     case e_slong:
858     case e_ulong:
859     case e_slonglong:
860     case e_ulonglong:
861     case e_sint128:
862     case e_uint128:
863     case e_sint256:
864     case e_uint256:
865     case e_sint512:
866     case e_uint512:
867       m_integer = a->m_integer + b->m_integer;
868       break;
869 
870     case e_float:
871     case e_double:
872     case e_long_double:
873       m_float = a->m_float + b->m_float;
874       break;
875     }
876   }
877   return *this;
878 }
879 
880 Scalar &Scalar::operator<<=(const Scalar &rhs) {
881   switch (m_type) {
882   case e_void:
883   case e_float:
884   case e_double:
885   case e_long_double:
886     m_type = e_void;
887     break;
888 
889   case e_sint:
890   case e_uint:
891   case e_slong:
892   case e_ulong:
893   case e_slonglong:
894   case e_ulonglong:
895   case e_sint128:
896   case e_uint128:
897   case e_sint256:
898   case e_uint256:
899   case e_sint512:
900   case e_uint512:
901     switch (rhs.m_type) {
902     case e_void:
903     case e_float:
904     case e_double:
905     case e_long_double:
906       m_type = e_void;
907       break;
908     case e_sint:
909     case e_uint:
910     case e_slong:
911     case e_ulong:
912     case e_slonglong:
913     case e_ulonglong:
914     case e_sint128:
915     case e_uint128:
916     case e_sint256:
917     case e_uint256:
918     case e_sint512:
919     case e_uint512:
920       m_integer = m_integer << rhs.m_integer;
921       break;
922     }
923     break;
924   }
925   return *this;
926 }
927 
928 bool Scalar::ShiftRightLogical(const Scalar &rhs) {
929   switch (m_type) {
930   case e_void:
931   case e_float:
932   case e_double:
933   case e_long_double:
934     m_type = e_void;
935     break;
936 
937   case e_sint:
938   case e_uint:
939   case e_slong:
940   case e_ulong:
941   case e_slonglong:
942   case e_ulonglong:
943   case e_sint128:
944   case e_uint128:
945   case e_sint256:
946   case e_uint256:
947   case e_sint512:
948   case e_uint512:
949     switch (rhs.m_type) {
950     case e_void:
951     case e_float:
952     case e_double:
953     case e_long_double:
954       m_type = e_void;
955       break;
956     case e_sint:
957     case e_uint:
958     case e_slong:
959     case e_ulong:
960     case e_slonglong:
961     case e_ulonglong:
962     case e_sint128:
963     case e_uint128:
964     case e_sint256:
965     case e_uint256:
966     case e_sint512:
967     case e_uint512:
968       m_integer = m_integer.lshr(rhs.m_integer);
969       break;
970     }
971     break;
972   }
973   return m_type != e_void;
974 }
975 
976 Scalar &Scalar::operator>>=(const Scalar &rhs) {
977   switch (m_type) {
978   case e_void:
979   case e_float:
980   case e_double:
981   case e_long_double:
982     m_type = e_void;
983     break;
984 
985   case e_sint:
986   case e_uint:
987   case e_slong:
988   case e_ulong:
989   case e_slonglong:
990   case e_ulonglong:
991   case e_sint128:
992   case e_uint128:
993   case e_sint256:
994   case e_uint256:
995   case e_sint512:
996   case e_uint512:
997     switch (rhs.m_type) {
998     case e_void:
999     case e_float:
1000     case e_double:
1001     case e_long_double:
1002       m_type = e_void;
1003       break;
1004     case e_sint:
1005     case e_uint:
1006     case e_slong:
1007     case e_ulong:
1008     case e_slonglong:
1009     case e_ulonglong:
1010     case e_sint128:
1011     case e_uint128:
1012     case e_sint256:
1013     case e_uint256:
1014     case e_sint512:
1015     case e_uint512:
1016       m_integer = m_integer.ashr(rhs.m_integer);
1017       break;
1018     }
1019     break;
1020   }
1021   return *this;
1022 }
1023 
1024 Scalar &Scalar::operator&=(const Scalar &rhs) {
1025   switch (m_type) {
1026   case e_void:
1027   case e_float:
1028   case e_double:
1029   case e_long_double:
1030     m_type = e_void;
1031     break;
1032 
1033   case e_sint:
1034   case e_uint:
1035   case e_slong:
1036   case e_ulong:
1037   case e_slonglong:
1038   case e_ulonglong:
1039   case e_sint128:
1040   case e_uint128:
1041   case e_sint256:
1042   case e_uint256:
1043   case e_sint512:
1044   case e_uint512:
1045     switch (rhs.m_type) {
1046     case e_void:
1047     case e_float:
1048     case e_double:
1049     case e_long_double:
1050       m_type = e_void;
1051       break;
1052     case e_sint:
1053     case e_uint:
1054     case e_slong:
1055     case e_ulong:
1056     case e_slonglong:
1057     case e_ulonglong:
1058     case e_sint128:
1059     case e_uint128:
1060     case e_sint256:
1061     case e_uint256:
1062     case e_sint512:
1063     case e_uint512:
1064       m_integer &= rhs.m_integer;
1065       break;
1066     }
1067     break;
1068   }
1069   return *this;
1070 }
1071 
1072 bool Scalar::AbsoluteValue() {
1073   switch (m_type) {
1074   case e_void:
1075     break;
1076 
1077   case e_sint:
1078   case e_slong:
1079   case e_slonglong:
1080   case e_sint128:
1081   case e_sint256:
1082   case e_sint512:
1083     if (m_integer.isNegative())
1084       m_integer = -m_integer;
1085     return true;
1086 
1087   case e_uint:
1088   case e_ulong:
1089   case e_ulonglong:
1090     return true;
1091   case e_uint128:
1092   case e_uint256:
1093   case e_uint512:
1094   case e_float:
1095   case e_double:
1096   case e_long_double:
1097     m_float.clearSign();
1098     return true;
1099   }
1100   return false;
1101 }
1102 
1103 bool Scalar::UnaryNegate() {
1104   switch (m_type) {
1105   case e_void:
1106     break;
1107   case e_sint:
1108   case e_uint:
1109   case e_slong:
1110   case e_ulong:
1111   case e_slonglong:
1112   case e_ulonglong:
1113   case e_sint128:
1114   case e_uint128:
1115   case e_sint256:
1116   case e_uint256:
1117   case e_sint512:
1118   case e_uint512:
1119     m_integer = -m_integer;
1120     return true;
1121   case e_float:
1122   case e_double:
1123   case e_long_double:
1124     m_float.changeSign();
1125     return true;
1126   }
1127   return false;
1128 }
1129 
1130 bool Scalar::OnesComplement() {
1131   switch (m_type) {
1132   case e_sint:
1133   case e_uint:
1134   case e_slong:
1135   case e_ulong:
1136   case e_slonglong:
1137   case e_ulonglong:
1138   case e_sint128:
1139   case e_uint128:
1140   case e_sint256:
1141   case e_uint256:
1142   case e_sint512:
1143   case e_uint512:
1144     m_integer = ~m_integer;
1145     return true;
1146 
1147   case e_void:
1148   case e_float:
1149   case e_double:
1150   case e_long_double:
1151     break;
1152   }
1153   return false;
1154 }
1155 
1156 const Scalar lldb_private::operator+(const Scalar &lhs, const Scalar &rhs) {
1157   Scalar result;
1158   Scalar temp_value;
1159   const Scalar *a;
1160   const Scalar *b;
1161   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
1162       Scalar::e_void) {
1163     switch (result.m_type) {
1164     case Scalar::e_void:
1165       break;
1166     case Scalar::e_sint:
1167     case Scalar::e_uint:
1168     case Scalar::e_slong:
1169     case Scalar::e_ulong:
1170     case Scalar::e_slonglong:
1171     case Scalar::e_ulonglong:
1172     case Scalar::e_sint128:
1173     case Scalar::e_uint128:
1174     case Scalar::e_sint256:
1175     case Scalar::e_uint256:
1176     case Scalar::e_sint512:
1177     case Scalar::e_uint512:
1178       result.m_integer = a->m_integer + b->m_integer;
1179       break;
1180     case Scalar::e_float:
1181     case Scalar::e_double:
1182     case Scalar::e_long_double:
1183       result.m_float = a->m_float + b->m_float;
1184       break;
1185     }
1186   }
1187   return result;
1188 }
1189 
1190 const Scalar lldb_private::operator-(const Scalar &lhs, const Scalar &rhs) {
1191   Scalar result;
1192   Scalar temp_value;
1193   const Scalar *a;
1194   const Scalar *b;
1195   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
1196       Scalar::e_void) {
1197     switch (result.m_type) {
1198     case Scalar::e_void:
1199       break;
1200     case Scalar::e_sint:
1201     case Scalar::e_uint:
1202     case Scalar::e_slong:
1203     case Scalar::e_ulong:
1204     case Scalar::e_slonglong:
1205     case Scalar::e_ulonglong:
1206     case Scalar::e_sint128:
1207     case Scalar::e_uint128:
1208     case Scalar::e_sint256:
1209     case Scalar::e_uint256:
1210     case Scalar::e_sint512:
1211     case Scalar::e_uint512:
1212       result.m_integer = a->m_integer - b->m_integer;
1213       break;
1214     case Scalar::e_float:
1215     case Scalar::e_double:
1216     case Scalar::e_long_double:
1217       result.m_float = a->m_float - b->m_float;
1218       break;
1219     }
1220   }
1221   return result;
1222 }
1223 
1224 const Scalar lldb_private::operator/(const Scalar &lhs, const Scalar &rhs) {
1225   Scalar result;
1226   Scalar temp_value;
1227   const Scalar *a;
1228   const Scalar *b;
1229   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
1230       Scalar::e_void) {
1231     switch (result.m_type) {
1232     case Scalar::e_void:
1233       break;
1234     case Scalar::e_sint:
1235     case Scalar::e_slong:
1236     case Scalar::e_slonglong:
1237     case Scalar::e_sint128:
1238     case Scalar::e_sint256:
1239     case Scalar::e_sint512:
1240       if (b->m_integer != 0) {
1241         result.m_integer = a->m_integer.sdiv(b->m_integer);
1242         return result;
1243       }
1244       break;
1245     case Scalar::e_uint:
1246     case Scalar::e_ulong:
1247     case Scalar::e_ulonglong:
1248     case Scalar::e_uint128:
1249     case Scalar::e_uint256:
1250     case Scalar::e_uint512:
1251       if (b->m_integer != 0) {
1252         result.m_integer = a->m_integer.udiv(b->m_integer);
1253         return result;
1254       }
1255       break;
1256     case Scalar::e_float:
1257     case Scalar::e_double:
1258     case Scalar::e_long_double:
1259       if (!b->m_float.isZero()) {
1260         result.m_float = a->m_float / b->m_float;
1261         return result;
1262       }
1263       break;
1264     }
1265   }
1266   // For division only, the only way it should make it here is if a promotion
1267   // failed, or if we are trying to do a divide by zero.
1268   result.m_type = Scalar::e_void;
1269   return result;
1270 }
1271 
1272 const Scalar lldb_private::operator*(const Scalar &lhs, const Scalar &rhs) {
1273   Scalar result;
1274   Scalar temp_value;
1275   const Scalar *a;
1276   const Scalar *b;
1277   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
1278       Scalar::e_void) {
1279     switch (result.m_type) {
1280     case Scalar::e_void:
1281       break;
1282     case Scalar::e_sint:
1283     case Scalar::e_uint:
1284     case Scalar::e_slong:
1285     case Scalar::e_ulong:
1286     case Scalar::e_slonglong:
1287     case Scalar::e_ulonglong:
1288     case Scalar::e_sint128:
1289     case Scalar::e_uint128:
1290     case Scalar::e_sint256:
1291     case Scalar::e_uint256:
1292     case Scalar::e_sint512:
1293     case Scalar::e_uint512:
1294       result.m_integer = a->m_integer * b->m_integer;
1295       break;
1296     case Scalar::e_float:
1297     case Scalar::e_double:
1298     case Scalar::e_long_double:
1299       result.m_float = a->m_float * b->m_float;
1300       break;
1301     }
1302   }
1303   return result;
1304 }
1305 
1306 const Scalar lldb_private::operator&(const Scalar &lhs, const Scalar &rhs) {
1307   Scalar result;
1308   Scalar temp_value;
1309   const Scalar *a;
1310   const Scalar *b;
1311   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
1312       Scalar::e_void) {
1313     switch (result.m_type) {
1314     case Scalar::e_sint:
1315     case Scalar::e_uint:
1316     case Scalar::e_slong:
1317     case Scalar::e_ulong:
1318     case Scalar::e_slonglong:
1319     case Scalar::e_ulonglong:
1320     case Scalar::e_sint128:
1321     case Scalar::e_uint128:
1322     case Scalar::e_sint256:
1323     case Scalar::e_uint256:
1324     case Scalar::e_sint512:
1325     case Scalar::e_uint512:
1326       result.m_integer = a->m_integer & b->m_integer;
1327       break;
1328     case Scalar::e_void:
1329     case Scalar::e_float:
1330     case Scalar::e_double:
1331     case Scalar::e_long_double:
1332       // No bitwise AND on floats, doubles of long doubles
1333       result.m_type = Scalar::e_void;
1334       break;
1335     }
1336   }
1337   return result;
1338 }
1339 
1340 const Scalar lldb_private::operator|(const Scalar &lhs, const Scalar &rhs) {
1341   Scalar result;
1342   Scalar temp_value;
1343   const Scalar *a;
1344   const Scalar *b;
1345   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
1346       Scalar::e_void) {
1347     switch (result.m_type) {
1348     case Scalar::e_sint:
1349     case Scalar::e_uint:
1350     case Scalar::e_slong:
1351     case Scalar::e_ulong:
1352     case Scalar::e_slonglong:
1353     case Scalar::e_ulonglong:
1354     case Scalar::e_sint128:
1355     case Scalar::e_uint128:
1356     case Scalar::e_sint256:
1357     case Scalar::e_uint256:
1358     case Scalar::e_sint512:
1359     case Scalar::e_uint512:
1360       result.m_integer = a->m_integer | b->m_integer;
1361       break;
1362 
1363     case Scalar::e_void:
1364     case Scalar::e_float:
1365     case Scalar::e_double:
1366     case Scalar::e_long_double:
1367       // No bitwise AND on floats, doubles of long doubles
1368       result.m_type = Scalar::e_void;
1369       break;
1370     }
1371   }
1372   return result;
1373 }
1374 
1375 const Scalar lldb_private::operator%(const Scalar &lhs, const Scalar &rhs) {
1376   Scalar result;
1377   Scalar temp_value;
1378   const Scalar *a;
1379   const Scalar *b;
1380   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
1381       Scalar::e_void) {
1382     switch (result.m_type) {
1383     default:
1384       break;
1385     case Scalar::e_void:
1386       break;
1387     case Scalar::e_sint:
1388     case Scalar::e_slong:
1389     case Scalar::e_slonglong:
1390     case Scalar::e_sint128:
1391     case Scalar::e_sint256:
1392     case Scalar::e_sint512:
1393       if (b->m_integer != 0) {
1394         result.m_integer = a->m_integer.srem(b->m_integer);
1395         return result;
1396       }
1397       break;
1398     case Scalar::e_uint:
1399     case Scalar::e_ulong:
1400     case Scalar::e_ulonglong:
1401     case Scalar::e_uint128:
1402     case Scalar::e_uint256:
1403     case Scalar::e_uint512:
1404       if (b->m_integer != 0) {
1405         result.m_integer = a->m_integer.urem(b->m_integer);
1406         return result;
1407       }
1408       break;
1409     }
1410   }
1411   result.m_type = Scalar::e_void;
1412   return result;
1413 }
1414 
1415 const Scalar lldb_private::operator^(const Scalar &lhs, const Scalar &rhs) {
1416   Scalar result;
1417   Scalar temp_value;
1418   const Scalar *a;
1419   const Scalar *b;
1420   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
1421       Scalar::e_void) {
1422     switch (result.m_type) {
1423     case Scalar::e_sint:
1424     case Scalar::e_uint:
1425     case Scalar::e_slong:
1426     case Scalar::e_ulong:
1427     case Scalar::e_slonglong:
1428     case Scalar::e_ulonglong:
1429     case Scalar::e_sint128:
1430     case Scalar::e_uint128:
1431     case Scalar::e_sint256:
1432     case Scalar::e_uint256:
1433     case Scalar::e_sint512:
1434     case Scalar::e_uint512:
1435       result.m_integer = a->m_integer ^ b->m_integer;
1436       break;
1437 
1438     case Scalar::e_void:
1439     case Scalar::e_float:
1440     case Scalar::e_double:
1441     case Scalar::e_long_double:
1442       // No bitwise AND on floats, doubles of long doubles
1443       result.m_type = Scalar::e_void;
1444       break;
1445     }
1446   }
1447   return result;
1448 }
1449 
1450 const Scalar lldb_private::operator<<(const Scalar &lhs, const Scalar &rhs) {
1451   Scalar result = lhs;
1452   result <<= rhs;
1453   return result;
1454 }
1455 
1456 const Scalar lldb_private::operator>>(const Scalar &lhs, const Scalar &rhs) {
1457   Scalar result = lhs;
1458   result >>= rhs;
1459   return result;
1460 }
1461 
1462 Status Scalar::SetValueFromCString(const char *value_str, Encoding encoding,
1463                                    size_t byte_size) {
1464   Status error;
1465   if (value_str == nullptr || value_str[0] == '\0') {
1466     error.SetErrorString("Invalid c-string value string.");
1467     return error;
1468   }
1469   switch (encoding) {
1470   case eEncodingInvalid:
1471     error.SetErrorString("Invalid encoding.");
1472     break;
1473 
1474   case eEncodingUint:
1475     if (byte_size <= sizeof(uint64_t)) {
1476       uint64_t uval64;
1477       if (!llvm::to_integer(value_str, uval64))
1478         error.SetErrorStringWithFormat(
1479             "'%s' is not a valid unsigned integer string value", value_str);
1480       else if (!UIntValueIsValidForSize(uval64, byte_size))
1481         error.SetErrorStringWithFormat(
1482             "value 0x%" PRIx64 " is too large to fit in a %" PRIu64
1483             " byte unsigned integer value",
1484             uval64, static_cast<uint64_t>(byte_size));
1485       else {
1486         m_type = Scalar::GetValueTypeForUnsignedIntegerWithByteSize(byte_size);
1487         switch (m_type) {
1488         case e_uint:
1489           m_integer = llvm::APInt(sizeof(uint_t) * 8, uval64, false);
1490           break;
1491         case e_ulong:
1492           m_integer = llvm::APInt(sizeof(ulong_t) * 8, uval64, false);
1493           break;
1494         case e_ulonglong:
1495           m_integer = llvm::APInt(sizeof(ulonglong_t) * 8, uval64, false);
1496           break;
1497         default:
1498           error.SetErrorStringWithFormat(
1499               "unsupported unsigned integer byte size: %" PRIu64 "",
1500               static_cast<uint64_t>(byte_size));
1501           break;
1502         }
1503       }
1504     } else {
1505       error.SetErrorStringWithFormat(
1506           "unsupported unsigned integer byte size: %" PRIu64 "",
1507           static_cast<uint64_t>(byte_size));
1508       return error;
1509     }
1510     break;
1511 
1512   case eEncodingSint:
1513     if (byte_size <= sizeof(int64_t)) {
1514       int64_t sval64;
1515       if (!llvm::to_integer(value_str, sval64))
1516         error.SetErrorStringWithFormat(
1517             "'%s' is not a valid signed integer string value", value_str);
1518       else if (!SIntValueIsValidForSize(sval64, byte_size))
1519         error.SetErrorStringWithFormat(
1520             "value 0x%" PRIx64 " is too large to fit in a %" PRIu64
1521             " byte signed integer value",
1522             sval64, static_cast<uint64_t>(byte_size));
1523       else {
1524         m_type = Scalar::GetValueTypeForSignedIntegerWithByteSize(byte_size);
1525         switch (m_type) {
1526         case e_sint:
1527           m_integer = llvm::APInt(sizeof(sint_t) * 8, sval64, true);
1528           break;
1529         case e_slong:
1530           m_integer = llvm::APInt(sizeof(slong_t) * 8, sval64, true);
1531           break;
1532         case e_slonglong:
1533           m_integer = llvm::APInt(sizeof(slonglong_t) * 8, sval64, true);
1534           break;
1535         default:
1536           error.SetErrorStringWithFormat(
1537               "unsupported signed integer byte size: %" PRIu64 "",
1538               static_cast<uint64_t>(byte_size));
1539           break;
1540         }
1541       }
1542     } else {
1543       error.SetErrorStringWithFormat(
1544           "unsupported signed integer byte size: %" PRIu64 "",
1545           static_cast<uint64_t>(byte_size));
1546       return error;
1547     }
1548     break;
1549 
1550   case eEncodingIEEE754:
1551     static float f_val;
1552     static double d_val;
1553     static long double l_val;
1554     if (byte_size == sizeof(float)) {
1555       if (::sscanf(value_str, "%f", &f_val) == 1) {
1556         m_float = llvm::APFloat(f_val);
1557         m_type = e_float;
1558       } else
1559         error.SetErrorStringWithFormat("'%s' is not a valid float string value",
1560                                        value_str);
1561     } else if (byte_size == sizeof(double)) {
1562       if (::sscanf(value_str, "%lf", &d_val) == 1) {
1563         m_float = llvm::APFloat(d_val);
1564         m_type = e_double;
1565       } else
1566         error.SetErrorStringWithFormat("'%s' is not a valid float string value",
1567                                        value_str);
1568     } else if (byte_size == sizeof(long double)) {
1569       if (::sscanf(value_str, "%Lf", &l_val) == 1) {
1570         m_float = llvm::APFloat(
1571             llvm::APFloat::x87DoubleExtended(),
1572             llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
1573                         (reinterpret_cast<type128 *>(&l_val))->x));
1574         m_type = e_long_double;
1575       } else
1576         error.SetErrorStringWithFormat("'%s' is not a valid float string value",
1577                                        value_str);
1578     } else {
1579       error.SetErrorStringWithFormat("unsupported float byte size: %" PRIu64 "",
1580                                      static_cast<uint64_t>(byte_size));
1581       return error;
1582     }
1583     break;
1584 
1585   case eEncodingVector:
1586     error.SetErrorString("vector encoding unsupported.");
1587     break;
1588   }
1589   if (error.Fail())
1590     m_type = e_void;
1591 
1592   return error;
1593 }
1594 
1595 Status Scalar::SetValueFromData(DataExtractor &data, lldb::Encoding encoding,
1596                                 size_t byte_size) {
1597   Status error;
1598 
1599   type128 int128;
1600   type256 int256;
1601   switch (encoding) {
1602   case lldb::eEncodingInvalid:
1603     error.SetErrorString("invalid encoding");
1604     break;
1605   case lldb::eEncodingVector:
1606     error.SetErrorString("vector encoding unsupported");
1607     break;
1608   case lldb::eEncodingUint: {
1609     lldb::offset_t offset = 0;
1610 
1611     switch (byte_size) {
1612     case 1:
1613       operator=(data.GetU8(&offset));
1614       break;
1615     case 2:
1616       operator=(data.GetU16(&offset));
1617       break;
1618     case 4:
1619       operator=(data.GetU32(&offset));
1620       break;
1621     case 8:
1622       operator=(data.GetU64(&offset));
1623       break;
1624     case 16:
1625       if (data.GetByteOrder() == eByteOrderBig) {
1626         int128.x[1] = data.GetU64(&offset);
1627         int128.x[0] = data.GetU64(&offset);
1628       } else {
1629         int128.x[0] = data.GetU64(&offset);
1630         int128.x[1] = data.GetU64(&offset);
1631       }
1632       operator=(llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128, int128.x));
1633       break;
1634     case 32:
1635       if (data.GetByteOrder() == eByteOrderBig) {
1636         int256.x[3] = data.GetU64(&offset);
1637         int256.x[2] = data.GetU64(&offset);
1638         int256.x[1] = data.GetU64(&offset);
1639         int256.x[0] = data.GetU64(&offset);
1640       } else {
1641         int256.x[0] = data.GetU64(&offset);
1642         int256.x[1] = data.GetU64(&offset);
1643         int256.x[2] = data.GetU64(&offset);
1644         int256.x[3] = data.GetU64(&offset);
1645       }
1646       operator=(llvm::APInt(BITWIDTH_INT256, NUM_OF_WORDS_INT256, int256.x));
1647       break;
1648     default:
1649       error.SetErrorStringWithFormat(
1650           "unsupported unsigned integer byte size: %" PRIu64 "",
1651           static_cast<uint64_t>(byte_size));
1652       break;
1653     }
1654   } break;
1655   case lldb::eEncodingSint: {
1656     lldb::offset_t offset = 0;
1657 
1658     switch (byte_size) {
1659     case 1:
1660       operator=(static_cast<int8_t>(data.GetU8(&offset)));
1661       break;
1662     case 2:
1663       operator=(static_cast<int16_t>(data.GetU16(&offset)));
1664       break;
1665     case 4:
1666       operator=(static_cast<int32_t>(data.GetU32(&offset)));
1667       break;
1668     case 8:
1669       operator=(static_cast<int64_t>(data.GetU64(&offset)));
1670       break;
1671     case 16:
1672       if (data.GetByteOrder() == eByteOrderBig) {
1673         int128.x[1] = data.GetU64(&offset);
1674         int128.x[0] = data.GetU64(&offset);
1675       } else {
1676         int128.x[0] = data.GetU64(&offset);
1677         int128.x[1] = data.GetU64(&offset);
1678       }
1679       operator=(llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128, int128.x));
1680       break;
1681     case 32:
1682       if (data.GetByteOrder() == eByteOrderBig) {
1683         int256.x[3] = data.GetU64(&offset);
1684         int256.x[2] = data.GetU64(&offset);
1685         int256.x[1] = data.GetU64(&offset);
1686         int256.x[0] = data.GetU64(&offset);
1687       } else {
1688         int256.x[0] = data.GetU64(&offset);
1689         int256.x[1] = data.GetU64(&offset);
1690         int256.x[2] = data.GetU64(&offset);
1691         int256.x[3] = data.GetU64(&offset);
1692       }
1693       operator=(llvm::APInt(BITWIDTH_INT256, NUM_OF_WORDS_INT256, int256.x));
1694       break;
1695     default:
1696       error.SetErrorStringWithFormat(
1697           "unsupported signed integer byte size: %" PRIu64 "",
1698           static_cast<uint64_t>(byte_size));
1699       break;
1700     }
1701   } break;
1702   case lldb::eEncodingIEEE754: {
1703     lldb::offset_t offset = 0;
1704 
1705     if (byte_size == sizeof(float))
1706       operator=(data.GetFloat(&offset));
1707     else if (byte_size == sizeof(double))
1708       operator=(data.GetDouble(&offset));
1709     else if (byte_size == sizeof(long double))
1710       operator=(data.GetLongDouble(&offset));
1711     else
1712       error.SetErrorStringWithFormat("unsupported float byte size: %" PRIu64 "",
1713                                      static_cast<uint64_t>(byte_size));
1714   } break;
1715   }
1716 
1717   return error;
1718 }
1719 
1720 bool Scalar::SignExtend(uint32_t sign_bit_pos) {
1721   const uint32_t max_bit_pos = GetByteSize() * 8;
1722 
1723   if (sign_bit_pos < max_bit_pos) {
1724     switch (m_type) {
1725     case Scalar::e_void:
1726     case Scalar::e_float:
1727     case Scalar::e_double:
1728     case Scalar::e_long_double:
1729       return false;
1730 
1731     case Scalar::e_sint:
1732     case Scalar::e_uint:
1733     case Scalar::e_slong:
1734     case Scalar::e_ulong:
1735     case Scalar::e_slonglong:
1736     case Scalar::e_ulonglong:
1737     case Scalar::e_sint128:
1738     case Scalar::e_uint128:
1739     case Scalar::e_sint256:
1740     case Scalar::e_uint256:
1741     case Scalar::e_sint512:
1742     case Scalar::e_uint512:
1743       if (max_bit_pos == sign_bit_pos)
1744         return true;
1745       else if (sign_bit_pos < (max_bit_pos - 1)) {
1746         llvm::APInt sign_bit = llvm::APInt::getSignMask(sign_bit_pos + 1);
1747         llvm::APInt bitwize_and = m_integer & sign_bit;
1748         if (bitwize_and.getBoolValue()) {
1749           const llvm::APInt mask =
1750               ~(sign_bit) + llvm::APInt(m_integer.getBitWidth(), 1);
1751           m_integer |= mask;
1752         }
1753         return true;
1754       }
1755       break;
1756     }
1757   }
1758   return false;
1759 }
1760 
1761 size_t Scalar::GetAsMemoryData(void *dst, size_t dst_len,
1762                                lldb::ByteOrder dst_byte_order,
1763                                Status &error) const {
1764   // Get a data extractor that points to the native scalar data
1765   DataExtractor data;
1766   if (!GetData(data)) {
1767     error.SetErrorString("invalid scalar value");
1768     return 0;
1769   }
1770 
1771   const size_t src_len = data.GetByteSize();
1772 
1773   // Prepare a memory buffer that contains some or all of the register value
1774   const size_t bytes_copied =
1775       data.CopyByteOrderedData(0,               // src offset
1776                                src_len,         // src length
1777                                dst,             // dst buffer
1778                                dst_len,         // dst length
1779                                dst_byte_order); // dst byte order
1780   if (bytes_copied == 0)
1781     error.SetErrorString("failed to copy data");
1782 
1783   return bytes_copied;
1784 }
1785 
1786 bool Scalar::ExtractBitfield(uint32_t bit_size, uint32_t bit_offset) {
1787   if (bit_size == 0)
1788     return true;
1789 
1790   switch (m_type) {
1791   case Scalar::e_void:
1792   case Scalar::e_float:
1793   case Scalar::e_double:
1794   case Scalar::e_long_double:
1795     break;
1796 
1797   case Scalar::e_sint:
1798   case Scalar::e_slong:
1799   case Scalar::e_slonglong:
1800   case Scalar::e_sint128:
1801   case Scalar::e_sint256:
1802   case Scalar::e_sint512:
1803     m_integer = m_integer.ashr(bit_offset)
1804                     .sextOrTrunc(bit_size)
1805                     .sextOrSelf(8 * GetByteSize());
1806     return true;
1807 
1808   case Scalar::e_uint:
1809   case Scalar::e_ulong:
1810   case Scalar::e_ulonglong:
1811   case Scalar::e_uint128:
1812   case Scalar::e_uint256:
1813   case Scalar::e_uint512:
1814     m_integer = m_integer.lshr(bit_offset)
1815                     .zextOrTrunc(bit_size)
1816                     .zextOrSelf(8 * GetByteSize());
1817     return true;
1818   }
1819   return false;
1820 }
1821 
1822 bool lldb_private::operator==(const Scalar &lhs, const Scalar &rhs) {
1823   // If either entry is void then we can just compare the types
1824   if (lhs.m_type == Scalar::e_void || rhs.m_type == Scalar::e_void)
1825     return lhs.m_type == rhs.m_type;
1826 
1827   Scalar temp_value;
1828   const Scalar *a;
1829   const Scalar *b;
1830   llvm::APFloat::cmpResult result;
1831   switch (PromoteToMaxType(lhs, rhs, temp_value, a, b)) {
1832   case Scalar::e_void:
1833     break;
1834   case Scalar::e_sint:
1835   case Scalar::e_uint:
1836   case Scalar::e_slong:
1837   case Scalar::e_ulong:
1838   case Scalar::e_slonglong:
1839   case Scalar::e_ulonglong:
1840   case Scalar::e_sint128:
1841   case Scalar::e_uint128:
1842   case Scalar::e_sint256:
1843   case Scalar::e_uint256:
1844   case Scalar::e_sint512:
1845   case Scalar::e_uint512:
1846     return a->m_integer == b->m_integer;
1847   case Scalar::e_float:
1848   case Scalar::e_double:
1849   case Scalar::e_long_double:
1850     result = a->m_float.compare(b->m_float);
1851     if (result == llvm::APFloat::cmpEqual)
1852       return true;
1853   }
1854   return false;
1855 }
1856 
1857 bool lldb_private::operator!=(const Scalar &lhs, const Scalar &rhs) {
1858   return !(lhs == rhs);
1859 }
1860 
1861 bool lldb_private::operator<(const Scalar &lhs, const Scalar &rhs) {
1862   if (lhs.m_type == Scalar::e_void || rhs.m_type == Scalar::e_void)
1863     return false;
1864 
1865   Scalar temp_value;
1866   const Scalar *a;
1867   const Scalar *b;
1868   llvm::APFloat::cmpResult result;
1869   switch (PromoteToMaxType(lhs, rhs, temp_value, a, b)) {
1870   case Scalar::e_void:
1871     break;
1872   case Scalar::e_sint:
1873   case Scalar::e_slong:
1874   case Scalar::e_slonglong:
1875   case Scalar::e_sint128:
1876   case Scalar::e_sint256:
1877   case Scalar::e_sint512:
1878   case Scalar::e_uint512:
1879     return a->m_integer.slt(b->m_integer);
1880   case Scalar::e_uint:
1881   case Scalar::e_ulong:
1882   case Scalar::e_ulonglong:
1883   case Scalar::e_uint128:
1884   case Scalar::e_uint256:
1885     return a->m_integer.ult(b->m_integer);
1886   case Scalar::e_float:
1887   case Scalar::e_double:
1888   case Scalar::e_long_double:
1889     result = a->m_float.compare(b->m_float);
1890     if (result == llvm::APFloat::cmpLessThan)
1891       return true;
1892   }
1893   return false;
1894 }
1895 
1896 bool lldb_private::operator<=(const Scalar &lhs, const Scalar &rhs) {
1897   return !(rhs < lhs);
1898 }
1899 
1900 bool lldb_private::operator>(const Scalar &lhs, const Scalar &rhs) {
1901   return rhs < lhs;
1902 }
1903 
1904 bool lldb_private::operator>=(const Scalar &lhs, const Scalar &rhs) {
1905   return !(lhs < rhs);
1906 }
1907 
1908 bool Scalar::ClearBit(uint32_t bit) {
1909   switch (m_type) {
1910   case e_void:
1911     break;
1912   case e_sint:
1913   case e_uint:
1914   case e_slong:
1915   case e_ulong:
1916   case e_slonglong:
1917   case e_ulonglong:
1918   case e_sint128:
1919   case e_uint128:
1920   case e_sint256:
1921   case e_uint256:
1922   case e_sint512:
1923   case e_uint512:
1924     m_integer.clearBit(bit);
1925     return true;
1926   case e_float:
1927   case e_double:
1928   case e_long_double:
1929     break;
1930   }
1931   return false;
1932 }
1933 
1934 bool Scalar::SetBit(uint32_t bit) {
1935   switch (m_type) {
1936   case e_void:
1937     break;
1938   case e_sint:
1939   case e_uint:
1940   case e_slong:
1941   case e_ulong:
1942   case e_slonglong:
1943   case e_ulonglong:
1944   case e_sint128:
1945   case e_uint128:
1946   case e_sint256:
1947   case e_uint256:
1948   case e_sint512:
1949   case e_uint512:
1950     m_integer.setBit(bit);
1951     return true;
1952   case e_float:
1953   case e_double:
1954   case e_long_double:
1955     break;
1956   }
1957   return false;
1958 }
1959 
1960 llvm::raw_ostream &lldb_private::operator<<(llvm::raw_ostream &os, const Scalar &scalar) {
1961   StreamString s;
1962   scalar.GetValue(&s, /*show_type*/ true);
1963   return os << s.GetString();
1964 }
1965