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