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 namespace {
27 enum class Category { Void, Integral, Float };
28 }
29 
30 static Category GetCategory(Scalar::Type type) {
31   switch (type) {
32   case Scalar::e_void:
33     return Category::Void;
34   case Scalar::e_float:
35   case Scalar::e_double:
36   case Scalar::e_long_double:
37     return Category::Float;
38   case Scalar::e_sint:
39   case Scalar::e_slong:
40   case Scalar::e_slonglong:
41   case Scalar::e_sint128:
42   case Scalar::e_sint256:
43   case Scalar::e_sint512:
44   case Scalar::e_uint:
45   case Scalar::e_ulong:
46   case Scalar::e_ulonglong:
47   case Scalar::e_uint128:
48   case Scalar::e_uint256:
49   case Scalar::e_uint512:
50     return Category::Integral;
51   }
52   llvm_unreachable("Unhandled type!");
53 }
54 
55 static bool IsSigned(Scalar::Type type) {
56   switch (type) {
57   case Scalar::e_void:
58   case Scalar::e_uint:
59   case Scalar::e_ulong:
60   case Scalar::e_ulonglong:
61   case Scalar::e_uint128:
62   case Scalar::e_uint256:
63   case Scalar::e_uint512:
64     return false;
65   case Scalar::e_sint:
66   case Scalar::e_slong:
67   case Scalar::e_slonglong:
68   case Scalar::e_sint128:
69   case Scalar::e_sint256:
70   case Scalar::e_sint512:
71   case Scalar::e_float:
72   case Scalar::e_double:
73   case Scalar::e_long_double:
74     return true;
75   }
76   llvm_unreachable("Unhandled type!");
77 }
78 
79 
80 // Promote to max type currently follows the ANSI C rule for type promotion in
81 // expressions.
82 static Scalar::Type PromoteToMaxType(
83     const Scalar &lhs,  // The const left hand side object
84     const Scalar &rhs,  // The const right hand side object
85     Scalar &temp_value, // A modifiable temp value than can be used to hold
86                         // either the promoted lhs or rhs object
87     const Scalar *&promoted_lhs_ptr, // Pointer to the resulting possibly
88                                      // promoted value of lhs (at most one of
89                                      // lhs/rhs will get promoted)
90     const Scalar *&promoted_rhs_ptr  // Pointer to the resulting possibly
91                                      // promoted value of rhs (at most one of
92                                      // lhs/rhs will get promoted)
93 ) {
94   Scalar result;
95   // Initialize the promoted values for both the right and left hand side
96   // values to be the objects themselves. If no promotion is needed (both right
97   // and left have the same type), then the temp_value will not get used.
98   promoted_lhs_ptr = &lhs;
99   promoted_rhs_ptr = &rhs;
100   // Extract the types of both the right and left hand side values
101   Scalar::Type lhs_type = lhs.GetType();
102   Scalar::Type rhs_type = rhs.GetType();
103 
104   if (lhs_type > rhs_type) {
105     // Right hand side need to be promoted
106     temp_value = rhs; // Copy right hand side into the temp value
107     if (temp_value.Promote(lhs_type)) // Promote it
108       promoted_rhs_ptr =
109           &temp_value; // Update the pointer for the promoted right hand side
110   } else if (lhs_type < rhs_type) {
111     // Left hand side need to be promoted
112     temp_value = lhs; // Copy left hand side value into the temp value
113     if (temp_value.Promote(rhs_type)) // Promote it
114       promoted_lhs_ptr =
115           &temp_value; // Update the pointer for the promoted left hand side
116   }
117 
118   // Make sure our type promotion worked as expected
119   if (promoted_lhs_ptr->GetType() == promoted_rhs_ptr->GetType())
120     return promoted_lhs_ptr->GetType(); // Return the resulting max type
121 
122   // Return the void type (zero) if we fail to promote either of the values.
123   return Scalar::e_void;
124 }
125 
126 Scalar::Scalar() : m_type(e_void), m_float(static_cast<float>(0)) {}
127 
128 bool Scalar::GetData(DataExtractor &data, size_t limit_byte_size) const {
129   size_t byte_size = GetByteSize();
130   if (byte_size == 0) {
131     data.Clear();
132     return false;
133   }
134   auto buffer_up = std::make_unique<DataBufferHeap>(byte_size, 0);
135   GetBytes(buffer_up->GetData());
136   lldb::offset_t offset = 0;
137 
138   if (limit_byte_size < byte_size) {
139     if (endian::InlHostByteOrder() == eByteOrderLittle) {
140       // On little endian systems if we want fewer bytes from the current
141       // type we just specify fewer bytes since the LSByte is first...
142       byte_size = limit_byte_size;
143     } else if (endian::InlHostByteOrder() == eByteOrderBig) {
144       // On big endian systems if we want fewer bytes from the current type
145       // have to advance our initial byte pointer and trim down the number of
146       // bytes since the MSByte is first
147       offset = byte_size - limit_byte_size;
148       byte_size = limit_byte_size;
149     }
150   }
151 
152   data.SetData(std::move(buffer_up), offset, byte_size);
153   data.SetByteOrder(endian::InlHostByteOrder());
154   return true;
155 }
156 
157 void Scalar::GetBytes(llvm::MutableArrayRef<uint8_t> storage) const {
158   assert(storage.size() >= GetByteSize());
159 
160   const auto &store = [&](const llvm::APInt val) {
161     StoreIntToMemory(val, storage.data(), (val.getBitWidth() + 7) / 8);
162   };
163   switch (GetCategory(m_type)) {
164   case Category::Void:
165     break;
166   case Category::Integral:
167     store(m_integer);
168     break;
169   case Category::Float:
170     store(m_float.bitcastToAPInt());
171     break;
172   }
173 }
174 
175 size_t Scalar::GetByteSize() const {
176   switch (m_type) {
177   case e_void:
178     break;
179   case e_sint:
180   case e_uint:
181   case e_slong:
182   case e_ulong:
183   case e_slonglong:
184   case e_ulonglong:
185   case e_sint128:
186   case e_uint128:
187   case e_sint256:
188   case e_uint256:
189   case e_sint512:
190   case e_uint512:
191     return (m_integer.getBitWidth() / 8);
192   case e_float:
193     return sizeof(float_t);
194   case e_double:
195     return sizeof(double_t);
196   case e_long_double:
197     return sizeof(long_double_t);
198   }
199   return 0;
200 }
201 
202 bool Scalar::IsZero() const {
203   switch (GetCategory(m_type)) {
204   case Category::Void:
205     break;
206   case Category::Integral:
207     return m_integer.isNullValue();
208   case Category::Float:
209     return m_float.isZero();
210   }
211   return false;
212 }
213 
214 void Scalar::GetValue(Stream *s, bool show_type) const {
215   if (show_type)
216     s->Printf("(%s) ", GetTypeAsCString());
217 
218   switch (GetCategory(m_type)) {
219   case Category::Void:
220     break;
221   case Category::Integral:
222     s->PutCString(m_integer.toString(10, IsSigned(m_type)));
223     break;
224   case Category::Float:
225     llvm::SmallString<24> string;
226     m_float.toString(string);
227     s->PutCString(string);
228     break;
229   }
230 }
231 
232 Scalar::~Scalar() = default;
233 
234 Scalar::Type Scalar::GetBestTypeForBitSize(size_t bit_size, bool sign) {
235   // Scalar types are always host types, hence the sizeof().
236   if (sign) {
237     if (bit_size <= sizeof(int)*8) return Scalar::e_sint;
238     if (bit_size <= sizeof(long)*8) return Scalar::e_slong;
239     if (bit_size <= sizeof(long long)*8) return Scalar::e_slonglong;
240     if (bit_size <= 128) return Scalar::e_sint128;
241     if (bit_size <= 256) return Scalar::e_sint256;
242     if (bit_size <= 512) return Scalar::e_sint512;
243   } else {
244     if (bit_size <= sizeof(unsigned int)*8) return Scalar::e_uint;
245     if (bit_size <= sizeof(unsigned long)*8) return Scalar::e_ulong;
246     if (bit_size <= sizeof(unsigned long long)*8) return Scalar::e_ulonglong;
247     if (bit_size <= 128) return Scalar::e_uint128;
248     if (bit_size <= 256) return Scalar::e_uint256;
249     if (bit_size <= 512) return Scalar::e_uint512;
250   }
251   return Scalar::e_void;
252 }
253 
254 void Scalar::TruncOrExtendTo(uint16_t bits, bool sign) {
255   m_integer = sign ? m_integer.sextOrTrunc(bits) : m_integer.zextOrTrunc(bits);
256   m_type = GetBestTypeForBitSize(bits, sign);
257 }
258 
259 static size_t GetBitSize(Scalar::Type type) {
260   switch (type) {
261   case Scalar::e_void:
262     return 0;
263   case Scalar::e_sint:
264     return 8 * sizeof(int);
265   case Scalar::e_uint:
266     return 8 * sizeof(unsigned int);
267   case Scalar::e_slong:
268     return 8 * sizeof(long);
269   case Scalar::e_ulong:
270     return 8 * sizeof(unsigned long);
271   case Scalar::e_slonglong:
272     return 8 * sizeof(long long);
273   case Scalar::e_ulonglong:
274     return 8 * sizeof(unsigned long long);
275   case Scalar::e_sint128:
276   case Scalar::e_uint128:
277     return BITWIDTH_INT128;
278   case Scalar::e_sint256:
279   case Scalar::e_uint256:
280     return BITWIDTH_INT256;
281   case Scalar::e_sint512:
282   case Scalar::e_uint512:
283     return BITWIDTH_INT512;
284   case Scalar::e_float:
285     return 8 * sizeof(float);
286   case Scalar::e_double:
287     return 8 * sizeof(double);
288   case Scalar::e_long_double:
289     return 8 * sizeof(long double);
290   }
291   llvm_unreachable("Unhandled type!");
292 }
293 
294 static const llvm::fltSemantics &GetFltSemantics(Scalar::Type type) {
295   switch (type) {
296   case Scalar::e_void:
297   case Scalar::e_sint:
298   case Scalar::e_slong:
299   case Scalar::e_slonglong:
300   case Scalar::e_sint128:
301   case Scalar::e_sint256:
302   case Scalar::e_sint512:
303   case Scalar::e_uint:
304   case Scalar::e_ulong:
305   case Scalar::e_ulonglong:
306   case Scalar::e_uint128:
307   case Scalar::e_uint256:
308   case Scalar::e_uint512:
309     llvm_unreachable("Only floating point types supported!");
310   case Scalar::e_float:
311     return llvm::APFloat::IEEEsingle();
312   case Scalar::e_double:
313     return llvm::APFloat::IEEEdouble();
314   case Scalar::e_long_double:
315     return llvm::APFloat::x87DoubleExtended();
316   }
317   llvm_unreachable("Unhandled type!");
318 }
319 
320 bool Scalar::Promote(Scalar::Type type) {
321   bool success = false;
322   switch (GetCategory(m_type)) {
323   case Category::Void:
324     break;
325   case Category::Integral:
326     switch (GetCategory(type)) {
327     case Category::Void:
328       break;
329     case Category::Integral:
330       if (type < m_type)
331         break;
332       success = true;
333       if (IsSigned(m_type))
334         m_integer = m_integer.sextOrTrunc(GetBitSize(type));
335       else
336         m_integer = m_integer.zextOrTrunc(GetBitSize(type));
337       break;
338     case Category::Float:
339       m_float = llvm::APFloat(GetFltSemantics(type));
340       m_float.convertFromAPInt(m_integer, IsSigned(m_type),
341                                llvm::APFloat::rmNearestTiesToEven);
342       success = true;
343       break;
344     }
345     break;
346   case Category::Float:
347     switch (GetCategory(type)) {
348     case Category::Void:
349     case Category::Integral:
350       break;
351     case Category::Float:
352       if (type < m_type)
353         break;
354       bool ignore;
355       success = true;
356       m_float.convert(GetFltSemantics(type), llvm::APFloat::rmNearestTiesToEven,
357                       &ignore);
358     }
359   }
360 
361   if (success)
362     m_type = type;
363   return success;
364 }
365 
366 const char *Scalar::GetValueTypeAsCString(Scalar::Type type) {
367   switch (type) {
368   case e_void:
369     return "void";
370   case e_sint:
371     return "int";
372   case e_uint:
373     return "unsigned int";
374   case e_slong:
375     return "long";
376   case e_ulong:
377     return "unsigned long";
378   case e_slonglong:
379     return "long long";
380   case e_ulonglong:
381     return "unsigned long long";
382   case e_float:
383     return "float";
384   case e_double:
385     return "double";
386   case e_long_double:
387     return "long double";
388   case e_sint128:
389     return "int128_t";
390   case e_uint128:
391     return "uint128_t";
392   case e_sint256:
393     return "int256_t";
394   case e_uint256:
395     return "uint256_t";
396   case e_sint512:
397     return "int512_t";
398   case e_uint512:
399     return "uint512_t";
400   }
401   return "???";
402 }
403 
404 Scalar::Type
405 Scalar::GetValueTypeForSignedIntegerWithByteSize(size_t byte_size) {
406   if (byte_size <= sizeof(sint_t))
407     return e_sint;
408   if (byte_size <= sizeof(slong_t))
409     return e_slong;
410   if (byte_size <= sizeof(slonglong_t))
411     return e_slonglong;
412   return e_void;
413 }
414 
415 Scalar::Type
416 Scalar::GetValueTypeForUnsignedIntegerWithByteSize(size_t byte_size) {
417   if (byte_size <= sizeof(uint_t))
418     return e_uint;
419   if (byte_size <= sizeof(ulong_t))
420     return e_ulong;
421   if (byte_size <= sizeof(ulonglong_t))
422     return e_ulonglong;
423   return e_void;
424 }
425 
426 Scalar::Type Scalar::GetValueTypeForFloatWithByteSize(size_t byte_size) {
427   if (byte_size == sizeof(float_t))
428     return e_float;
429   if (byte_size == sizeof(double_t))
430     return e_double;
431   if (byte_size == sizeof(long_double_t))
432     return e_long_double;
433   return e_void;
434 }
435 
436 bool Scalar::MakeSigned() {
437   bool success = false;
438 
439   switch (m_type) {
440   case e_void:
441     break;
442   case e_sint:
443     success = true;
444     break;
445   case e_uint:
446     m_type = e_sint;
447     success = true;
448     break;
449   case e_slong:
450     success = true;
451     break;
452   case e_ulong:
453     m_type = e_slong;
454     success = true;
455     break;
456   case e_slonglong:
457     success = true;
458     break;
459   case e_ulonglong:
460     m_type = e_slonglong;
461     success = true;
462     break;
463   case e_sint128:
464     success = true;
465     break;
466   case e_uint128:
467     m_type = e_sint128;
468     success = true;
469     break;
470   case e_sint256:
471     success = true;
472     break;
473   case e_uint256:
474     m_type = e_sint256;
475     success = true;
476     break;
477   case e_sint512:
478     success = true;
479     break;
480   case e_uint512:
481     m_type = e_sint512;
482     success = true;
483     break;
484   case e_float:
485     success = true;
486     break;
487   case e_double:
488     success = true;
489     break;
490   case e_long_double:
491     success = true;
492     break;
493   }
494 
495   return success;
496 }
497 
498 bool Scalar::MakeUnsigned() {
499   bool success = false;
500 
501   switch (m_type) {
502   case e_void:
503     break;
504   case e_sint:
505     m_type = e_uint;
506     success = true;
507     break;
508   case e_uint:
509     success = true;
510     break;
511   case e_slong:
512     m_type = e_ulong;
513     success = true;
514     break;
515   case e_ulong:
516     success = true;
517     break;
518   case e_slonglong:
519     m_type = e_ulonglong;
520     success = true;
521     break;
522   case e_ulonglong:
523     success = true;
524     break;
525   case e_sint128:
526     m_type = e_uint128;
527     success = true;
528     break;
529   case e_uint128:
530     success = true;
531     break;
532   case e_sint256:
533     m_type = e_uint256;
534     success = true;
535     break;
536   case e_uint256:
537     success = true;
538     break;
539   case e_sint512:
540     m_type = e_uint512;
541     success = true;
542     break;
543   case e_uint512:
544     success = true;
545     break;
546   case e_float:
547     success = true;
548     break;
549   case e_double:
550     success = true;
551     break;
552   case e_long_double:
553     success = true;
554     break;
555   }
556 
557   return success;
558 }
559 
560 static llvm::APInt ToAPInt(const llvm::APFloat &f, unsigned bits,
561                            bool is_unsigned) {
562   llvm::APSInt result(bits, is_unsigned);
563   bool isExact;
564   f.convertToInteger(result, llvm::APFloat::rmTowardZero, &isExact);
565   return std::move(result);
566 }
567 
568 template <typename T> T Scalar::GetAs(T fail_value) const {
569   switch (GetCategory(m_type)) {
570   case Category::Void:
571     break;
572   case Category::Integral:
573     if (IsSigned(m_type))
574       return m_integer.sextOrTrunc(sizeof(T) * 8).getSExtValue();
575     return m_integer.zextOrTrunc(sizeof(T) * 8).getZExtValue();
576   case Category::Float:
577     return ToAPInt(m_float, sizeof(T) * 8, std::is_unsigned<T>::value)
578         .getSExtValue();
579   }
580   return fail_value;
581 }
582 
583 signed char Scalar::SChar(signed char fail_value) const {
584   return GetAs<signed char>(fail_value);
585 }
586 
587 unsigned char Scalar::UChar(unsigned char fail_value) const {
588   return GetAs<unsigned char>(fail_value);
589 }
590 
591 short Scalar::SShort(short fail_value) const {
592   return GetAs<short>(fail_value);
593 }
594 
595 unsigned short Scalar::UShort(unsigned short fail_value) const {
596   return GetAs<unsigned short>(fail_value);
597 }
598 
599 int Scalar::SInt(int fail_value) const { return GetAs<int>(fail_value); }
600 
601 unsigned int Scalar::UInt(unsigned int fail_value) const {
602   return GetAs<unsigned int>(fail_value);
603 }
604 
605 long Scalar::SLong(long fail_value) const { return GetAs<long>(fail_value); }
606 
607 unsigned long Scalar::ULong(unsigned long fail_value) const {
608   return GetAs<unsigned long>(fail_value);
609 }
610 
611 long long Scalar::SLongLong(long long fail_value) const {
612   return GetAs<long long>(fail_value);
613 }
614 
615 unsigned long long Scalar::ULongLong(unsigned long long fail_value) const {
616   return GetAs<unsigned long long>(fail_value);
617 }
618 
619 llvm::APInt Scalar::SInt128(const llvm::APInt &fail_value) const {
620   switch (GetCategory(m_type)) {
621   case Category::Void:
622     break;
623   case Category::Integral:
624     return m_integer;
625   case Category::Float:
626     return ToAPInt(m_float, 128, /*is_unsigned=*/false);
627   }
628   return fail_value;
629 }
630 
631 llvm::APInt Scalar::UInt128(const llvm::APInt &fail_value) const {
632   switch (GetCategory(m_type)) {
633   case Category::Void:
634     break;
635   case Category::Integral:
636     return m_integer;
637   case Category::Float:
638     return ToAPInt(m_float, 128, /*is_unsigned=*/true);
639   }
640   return fail_value;
641 }
642 
643 float Scalar::Float(float fail_value) const {
644   switch (m_type) {
645   case e_void:
646     break;
647   case e_sint:
648   case e_slong:
649   case e_slonglong:
650   case e_sint128:
651   case e_sint256:
652   case e_sint512:
653     return llvm::APIntOps::RoundSignedAPIntToFloat(m_integer);
654 
655   case e_uint:
656   case e_ulong:
657   case e_ulonglong:
658   case e_uint128:
659   case e_uint256:
660   case e_uint512:
661     return llvm::APIntOps::RoundAPIntToFloat(m_integer);
662 
663   case e_float:
664     return m_float.convertToFloat();
665   case e_double:
666     return static_cast<float_t>(m_float.convertToDouble());
667   case e_long_double:
668     llvm::APInt ldbl_val = m_float.bitcastToAPInt();
669     return ldbl_val.bitsToFloat();
670   }
671   return fail_value;
672 }
673 
674 double Scalar::Double(double fail_value) const {
675   switch (m_type) {
676   case e_void:
677     break;
678   case e_sint:
679   case e_slong:
680   case e_slonglong:
681   case e_sint128:
682   case e_sint256:
683   case e_sint512:
684     return llvm::APIntOps::RoundSignedAPIntToDouble(m_integer);
685 
686   case e_uint:
687   case e_ulong:
688   case e_ulonglong:
689   case e_uint128:
690   case e_uint256:
691   case e_uint512:
692     return llvm::APIntOps::RoundAPIntToDouble(m_integer);
693 
694   case e_float:
695     return static_cast<double_t>(m_float.convertToFloat());
696   case e_double:
697     return m_float.convertToDouble();
698   case e_long_double:
699     llvm::APInt ldbl_val = m_float.bitcastToAPInt();
700     return ldbl_val.bitsToFloat();
701   }
702   return fail_value;
703 }
704 
705 long double Scalar::LongDouble(long double fail_value) const {
706   switch (m_type) {
707   case e_void:
708     break;
709   case e_sint:
710   case e_slong:
711   case e_slonglong:
712   case e_sint128:
713   case e_sint256:
714   case e_sint512:
715     return static_cast<long_double_t>(
716         llvm::APIntOps::RoundSignedAPIntToDouble(m_integer));
717 
718   case e_uint:
719   case e_ulong:
720   case e_ulonglong:
721   case e_uint128:
722   case e_uint256:
723   case e_uint512:
724     return static_cast<long_double_t>(
725         llvm::APIntOps::RoundAPIntToDouble(m_integer));
726 
727   case e_float:
728     return static_cast<long_double_t>(m_float.convertToFloat());
729   case e_double:
730     return static_cast<long_double_t>(m_float.convertToDouble());
731   case e_long_double:
732     llvm::APInt ldbl_val = m_float.bitcastToAPInt();
733     return static_cast<long_double_t>(ldbl_val.bitsToDouble());
734   }
735   return fail_value;
736 }
737 
738 Scalar &Scalar::operator+=(const Scalar &rhs) {
739   Scalar temp_value;
740   const Scalar *a;
741   const Scalar *b;
742   if ((m_type = PromoteToMaxType(*this, rhs, temp_value, a, b)) !=
743       Scalar::e_void) {
744     switch (GetCategory(m_type)) {
745     case Category::Void:
746       break;
747     case Category::Integral:
748       m_integer = a->m_integer + b->m_integer;
749       break;
750 
751     case Category::Float:
752       m_float = a->m_float + b->m_float;
753       break;
754     }
755   }
756   return *this;
757 }
758 
759 Scalar &Scalar::operator<<=(const Scalar &rhs) {
760   if (GetCategory(m_type) == Category::Integral &&
761       GetCategory(rhs.m_type) == Category::Integral)
762     m_integer <<= rhs.m_integer;
763   else
764     m_type = e_void;
765   return *this;
766 }
767 
768 bool Scalar::ShiftRightLogical(const Scalar &rhs) {
769   if (GetCategory(m_type) == Category::Integral &&
770       GetCategory(rhs.m_type) == Category::Integral) {
771     m_integer = m_integer.lshr(rhs.m_integer);
772     return true;
773   }
774   m_type = e_void;
775   return false;
776 }
777 
778 Scalar &Scalar::operator>>=(const Scalar &rhs) {
779   switch (m_type) {
780   case e_void:
781   case e_float:
782   case e_double:
783   case e_long_double:
784     m_type = e_void;
785     break;
786 
787   case e_sint:
788   case e_uint:
789   case e_slong:
790   case e_ulong:
791   case e_slonglong:
792   case e_ulonglong:
793   case e_sint128:
794   case e_uint128:
795   case e_sint256:
796   case e_uint256:
797   case e_sint512:
798   case e_uint512:
799     switch (rhs.m_type) {
800     case e_void:
801     case e_float:
802     case e_double:
803     case e_long_double:
804       m_type = 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       m_integer = m_integer.ashr(rhs.m_integer);
819       break;
820     }
821     break;
822   }
823   return *this;
824 }
825 
826 Scalar &Scalar::operator&=(const Scalar &rhs) {
827   if (GetCategory(m_type) == Category::Integral &&
828       GetCategory(rhs.m_type) == Category::Integral)
829     m_integer &= rhs.m_integer;
830   else
831     m_type = e_void;
832   return *this;
833 }
834 
835 bool Scalar::AbsoluteValue() {
836   switch (m_type) {
837   case e_void:
838     break;
839 
840   case e_sint:
841   case e_slong:
842   case e_slonglong:
843   case e_sint128:
844   case e_sint256:
845   case e_sint512:
846     if (m_integer.isNegative())
847       m_integer = -m_integer;
848     return true;
849 
850   case e_uint:
851   case e_ulong:
852   case e_ulonglong:
853     return true;
854   case e_uint128:
855   case e_uint256:
856   case e_uint512:
857   case e_float:
858   case e_double:
859   case e_long_double:
860     m_float.clearSign();
861     return true;
862   }
863   return false;
864 }
865 
866 bool Scalar::UnaryNegate() {
867   switch (GetCategory(m_type)) {
868   case Category::Void:
869     break;
870   case Category::Integral:
871     m_integer = -m_integer;
872     return true;
873   case Category::Float:
874     m_float.changeSign();
875     return true;
876   }
877   return false;
878 }
879 
880 bool Scalar::OnesComplement() {
881   if (GetCategory(m_type) == Category::Integral) {
882     m_integer = ~m_integer;
883     return true;
884   }
885 
886   return false;
887 }
888 
889 const Scalar lldb_private::operator+(const Scalar &lhs, const Scalar &rhs) {
890   Scalar result = lhs;
891   result += rhs;
892   return result;
893 }
894 
895 const Scalar lldb_private::operator-(const Scalar &lhs, const Scalar &rhs) {
896   Scalar result;
897   Scalar temp_value;
898   const Scalar *a;
899   const Scalar *b;
900   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
901       Scalar::e_void) {
902     switch (GetCategory(result.m_type)) {
903     case Category::Void:
904       break;
905     case Category::Integral:
906       result.m_integer = a->m_integer - b->m_integer;
907       break;
908     case Category::Float:
909       result.m_float = a->m_float - b->m_float;
910       break;
911     }
912   }
913   return result;
914 }
915 
916 const Scalar lldb_private::operator/(const Scalar &lhs, const Scalar &rhs) {
917   Scalar result;
918   Scalar temp_value;
919   const Scalar *a;
920   const Scalar *b;
921   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
922           Scalar::e_void &&
923       !b->IsZero()) {
924     switch (GetCategory(result.m_type)) {
925     case Category::Void:
926       break;
927     case Category::Integral:
928       if (IsSigned(result.m_type))
929         result.m_integer = a->m_integer.sdiv(b->m_integer);
930       else
931         result.m_integer = a->m_integer.udiv(b->m_integer);
932       return result;
933     case Category::Float:
934       result.m_float = a->m_float / b->m_float;
935       return result;
936     }
937   }
938   // For division only, the only way it should make it here is if a promotion
939   // failed, or if we are trying to do a divide by zero.
940   result.m_type = Scalar::e_void;
941   return result;
942 }
943 
944 const Scalar lldb_private::operator*(const Scalar &lhs, const Scalar &rhs) {
945   Scalar result;
946   Scalar temp_value;
947   const Scalar *a;
948   const Scalar *b;
949   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
950       Scalar::e_void) {
951     switch (GetCategory(result.m_type)) {
952     case Category::Void:
953       break;
954     case Category::Integral:
955       result.m_integer = a->m_integer * b->m_integer;
956       break;
957     case Category::Float:
958       result.m_float = a->m_float * b->m_float;
959       break;
960     }
961   }
962   return result;
963 }
964 
965 const Scalar lldb_private::operator&(const Scalar &lhs, const Scalar &rhs) {
966   Scalar result;
967   Scalar temp_value;
968   const Scalar *a;
969   const Scalar *b;
970   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
971       Scalar::e_void) {
972     if (GetCategory(result.m_type) == Category::Integral)
973       result.m_integer = a->m_integer & b->m_integer;
974     else
975       result.m_type = Scalar::e_void;
976   }
977   return result;
978 }
979 
980 const Scalar lldb_private::operator|(const Scalar &lhs, const Scalar &rhs) {
981   Scalar result;
982   Scalar temp_value;
983   const Scalar *a;
984   const Scalar *b;
985   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
986       Scalar::e_void) {
987     if (GetCategory(result.m_type) == Category::Integral)
988       result.m_integer = a->m_integer | b->m_integer;
989     else
990       result.m_type = Scalar::e_void;
991   }
992   return result;
993 }
994 
995 const Scalar lldb_private::operator%(const Scalar &lhs, const Scalar &rhs) {
996   Scalar result;
997   Scalar temp_value;
998   const Scalar *a;
999   const Scalar *b;
1000   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
1001       Scalar::e_void) {
1002     if (!b->IsZero() && GetCategory(result.m_type) == Category::Integral) {
1003       if (IsSigned(result.m_type))
1004         result.m_integer = a->m_integer.srem(b->m_integer);
1005       else
1006         result.m_integer = a->m_integer.urem(b->m_integer);
1007       return result;
1008     }
1009   }
1010   result.m_type = Scalar::e_void;
1011   return result;
1012 }
1013 
1014 const Scalar lldb_private::operator^(const Scalar &lhs, const Scalar &rhs) {
1015   Scalar result;
1016   Scalar temp_value;
1017   const Scalar *a;
1018   const Scalar *b;
1019   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
1020       Scalar::e_void) {
1021     if (GetCategory(result.m_type) == Category::Integral)
1022       result.m_integer = a->m_integer ^ b->m_integer;
1023     else
1024       result.m_type = Scalar::e_void;
1025   }
1026   return result;
1027 }
1028 
1029 const Scalar lldb_private::operator<<(const Scalar &lhs, const Scalar &rhs) {
1030   Scalar result = lhs;
1031   result <<= rhs;
1032   return result;
1033 }
1034 
1035 const Scalar lldb_private::operator>>(const Scalar &lhs, const Scalar &rhs) {
1036   Scalar result = lhs;
1037   result >>= rhs;
1038   return result;
1039 }
1040 
1041 Status Scalar::SetValueFromCString(const char *value_str, Encoding encoding,
1042                                    size_t byte_size) {
1043   Status error;
1044   if (value_str == nullptr || value_str[0] == '\0') {
1045     error.SetErrorString("Invalid c-string value string.");
1046     return error;
1047   }
1048   switch (encoding) {
1049   case eEncodingInvalid:
1050     error.SetErrorString("Invalid encoding.");
1051     break;
1052 
1053   case eEncodingUint:
1054     if (byte_size <= sizeof(uint64_t)) {
1055       uint64_t uval64;
1056       if (!llvm::to_integer(value_str, uval64))
1057         error.SetErrorStringWithFormat(
1058             "'%s' is not a valid unsigned integer string value", value_str);
1059       else if (!UIntValueIsValidForSize(uval64, byte_size))
1060         error.SetErrorStringWithFormat(
1061             "value 0x%" PRIx64 " is too large to fit in a %" PRIu64
1062             " byte unsigned integer value",
1063             uval64, static_cast<uint64_t>(byte_size));
1064       else {
1065         m_type = Scalar::GetValueTypeForUnsignedIntegerWithByteSize(byte_size);
1066         switch (m_type) {
1067         case e_uint:
1068           m_integer = llvm::APInt(sizeof(uint_t) * 8, uval64, false);
1069           break;
1070         case e_ulong:
1071           m_integer = llvm::APInt(sizeof(ulong_t) * 8, uval64, false);
1072           break;
1073         case e_ulonglong:
1074           m_integer = llvm::APInt(sizeof(ulonglong_t) * 8, uval64, false);
1075           break;
1076         default:
1077           error.SetErrorStringWithFormat(
1078               "unsupported unsigned integer byte size: %" PRIu64 "",
1079               static_cast<uint64_t>(byte_size));
1080           break;
1081         }
1082       }
1083     } else {
1084       error.SetErrorStringWithFormat(
1085           "unsupported unsigned integer byte size: %" PRIu64 "",
1086           static_cast<uint64_t>(byte_size));
1087       return error;
1088     }
1089     break;
1090 
1091   case eEncodingSint:
1092     if (byte_size <= sizeof(int64_t)) {
1093       int64_t sval64;
1094       if (!llvm::to_integer(value_str, sval64))
1095         error.SetErrorStringWithFormat(
1096             "'%s' is not a valid signed integer string value", value_str);
1097       else if (!SIntValueIsValidForSize(sval64, byte_size))
1098         error.SetErrorStringWithFormat(
1099             "value 0x%" PRIx64 " is too large to fit in a %" PRIu64
1100             " byte signed integer value",
1101             sval64, static_cast<uint64_t>(byte_size));
1102       else {
1103         m_type = Scalar::GetValueTypeForSignedIntegerWithByteSize(byte_size);
1104         switch (m_type) {
1105         case e_sint:
1106           m_integer = llvm::APInt(sizeof(sint_t) * 8, sval64, true);
1107           break;
1108         case e_slong:
1109           m_integer = llvm::APInt(sizeof(slong_t) * 8, sval64, true);
1110           break;
1111         case e_slonglong:
1112           m_integer = llvm::APInt(sizeof(slonglong_t) * 8, sval64, true);
1113           break;
1114         default:
1115           error.SetErrorStringWithFormat(
1116               "unsupported signed integer byte size: %" PRIu64 "",
1117               static_cast<uint64_t>(byte_size));
1118           break;
1119         }
1120       }
1121     } else {
1122       error.SetErrorStringWithFormat(
1123           "unsupported signed integer byte size: %" PRIu64 "",
1124           static_cast<uint64_t>(byte_size));
1125       return error;
1126     }
1127     break;
1128 
1129   case eEncodingIEEE754:
1130     static float f_val;
1131     static double d_val;
1132     static long double l_val;
1133     if (byte_size == sizeof(float)) {
1134       if (::sscanf(value_str, "%f", &f_val) == 1) {
1135         m_float = llvm::APFloat(f_val);
1136         m_type = e_float;
1137       } else
1138         error.SetErrorStringWithFormat("'%s' is not a valid float string value",
1139                                        value_str);
1140     } else if (byte_size == sizeof(double)) {
1141       if (::sscanf(value_str, "%lf", &d_val) == 1) {
1142         m_float = llvm::APFloat(d_val);
1143         m_type = e_double;
1144       } else
1145         error.SetErrorStringWithFormat("'%s' is not a valid float string value",
1146                                        value_str);
1147     } else if (byte_size == sizeof(long double)) {
1148       if (::sscanf(value_str, "%Lf", &l_val) == 1) {
1149         m_float = llvm::APFloat(
1150             llvm::APFloat::x87DoubleExtended(),
1151             llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
1152                         (reinterpret_cast<type128 *>(&l_val))->x));
1153         m_type = e_long_double;
1154       } else
1155         error.SetErrorStringWithFormat("'%s' is not a valid float string value",
1156                                        value_str);
1157     } else {
1158       error.SetErrorStringWithFormat("unsupported float byte size: %" PRIu64 "",
1159                                      static_cast<uint64_t>(byte_size));
1160       return error;
1161     }
1162     break;
1163 
1164   case eEncodingVector:
1165     error.SetErrorString("vector encoding unsupported.");
1166     break;
1167   }
1168   if (error.Fail())
1169     m_type = e_void;
1170 
1171   return error;
1172 }
1173 
1174 Status Scalar::SetValueFromData(DataExtractor &data, lldb::Encoding encoding,
1175                                 size_t byte_size) {
1176   Status error;
1177 
1178   type128 int128;
1179   type256 int256;
1180   switch (encoding) {
1181   case lldb::eEncodingInvalid:
1182     error.SetErrorString("invalid encoding");
1183     break;
1184   case lldb::eEncodingVector:
1185     error.SetErrorString("vector encoding unsupported");
1186     break;
1187   case lldb::eEncodingUint: {
1188     lldb::offset_t offset = 0;
1189 
1190     switch (byte_size) {
1191     case 1:
1192       operator=(data.GetU8(&offset));
1193       break;
1194     case 2:
1195       operator=(data.GetU16(&offset));
1196       break;
1197     case 4:
1198       operator=(data.GetU32(&offset));
1199       break;
1200     case 8:
1201       operator=(data.GetU64(&offset));
1202       break;
1203     case 16:
1204       if (data.GetByteOrder() == eByteOrderBig) {
1205         int128.x[1] = data.GetU64(&offset);
1206         int128.x[0] = data.GetU64(&offset);
1207       } else {
1208         int128.x[0] = data.GetU64(&offset);
1209         int128.x[1] = data.GetU64(&offset);
1210       }
1211       operator=(llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128, int128.x));
1212       break;
1213     case 32:
1214       if (data.GetByteOrder() == eByteOrderBig) {
1215         int256.x[3] = data.GetU64(&offset);
1216         int256.x[2] = data.GetU64(&offset);
1217         int256.x[1] = data.GetU64(&offset);
1218         int256.x[0] = data.GetU64(&offset);
1219       } else {
1220         int256.x[0] = data.GetU64(&offset);
1221         int256.x[1] = data.GetU64(&offset);
1222         int256.x[2] = data.GetU64(&offset);
1223         int256.x[3] = data.GetU64(&offset);
1224       }
1225       operator=(llvm::APInt(BITWIDTH_INT256, NUM_OF_WORDS_INT256, int256.x));
1226       break;
1227     default:
1228       error.SetErrorStringWithFormat(
1229           "unsupported unsigned integer byte size: %" PRIu64 "",
1230           static_cast<uint64_t>(byte_size));
1231       break;
1232     }
1233   } break;
1234   case lldb::eEncodingSint: {
1235     lldb::offset_t offset = 0;
1236 
1237     switch (byte_size) {
1238     case 1:
1239       operator=(static_cast<int8_t>(data.GetU8(&offset)));
1240       break;
1241     case 2:
1242       operator=(static_cast<int16_t>(data.GetU16(&offset)));
1243       break;
1244     case 4:
1245       operator=(static_cast<int32_t>(data.GetU32(&offset)));
1246       break;
1247     case 8:
1248       operator=(static_cast<int64_t>(data.GetU64(&offset)));
1249       break;
1250     case 16:
1251       if (data.GetByteOrder() == eByteOrderBig) {
1252         int128.x[1] = data.GetU64(&offset);
1253         int128.x[0] = data.GetU64(&offset);
1254       } else {
1255         int128.x[0] = data.GetU64(&offset);
1256         int128.x[1] = data.GetU64(&offset);
1257       }
1258       operator=(llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128, int128.x));
1259       break;
1260     case 32:
1261       if (data.GetByteOrder() == eByteOrderBig) {
1262         int256.x[3] = data.GetU64(&offset);
1263         int256.x[2] = data.GetU64(&offset);
1264         int256.x[1] = data.GetU64(&offset);
1265         int256.x[0] = data.GetU64(&offset);
1266       } else {
1267         int256.x[0] = data.GetU64(&offset);
1268         int256.x[1] = data.GetU64(&offset);
1269         int256.x[2] = data.GetU64(&offset);
1270         int256.x[3] = data.GetU64(&offset);
1271       }
1272       operator=(llvm::APInt(BITWIDTH_INT256, NUM_OF_WORDS_INT256, int256.x));
1273       break;
1274     default:
1275       error.SetErrorStringWithFormat(
1276           "unsupported signed integer byte size: %" PRIu64 "",
1277           static_cast<uint64_t>(byte_size));
1278       break;
1279     }
1280   } break;
1281   case lldb::eEncodingIEEE754: {
1282     lldb::offset_t offset = 0;
1283 
1284     if (byte_size == sizeof(float))
1285       operator=(data.GetFloat(&offset));
1286     else if (byte_size == sizeof(double))
1287       operator=(data.GetDouble(&offset));
1288     else if (byte_size == sizeof(long double))
1289       operator=(data.GetLongDouble(&offset));
1290     else
1291       error.SetErrorStringWithFormat("unsupported float byte size: %" PRIu64 "",
1292                                      static_cast<uint64_t>(byte_size));
1293   } break;
1294   }
1295 
1296   return error;
1297 }
1298 
1299 bool Scalar::SignExtend(uint32_t sign_bit_pos) {
1300   const uint32_t max_bit_pos = GetByteSize() * 8;
1301 
1302   if (sign_bit_pos < max_bit_pos) {
1303     switch (m_type) {
1304     case Scalar::e_void:
1305     case Scalar::e_float:
1306     case Scalar::e_double:
1307     case Scalar::e_long_double:
1308       return false;
1309 
1310     case Scalar::e_sint:
1311     case Scalar::e_uint:
1312     case Scalar::e_slong:
1313     case Scalar::e_ulong:
1314     case Scalar::e_slonglong:
1315     case Scalar::e_ulonglong:
1316     case Scalar::e_sint128:
1317     case Scalar::e_uint128:
1318     case Scalar::e_sint256:
1319     case Scalar::e_uint256:
1320     case Scalar::e_sint512:
1321     case Scalar::e_uint512:
1322       if (max_bit_pos == sign_bit_pos)
1323         return true;
1324       else if (sign_bit_pos < (max_bit_pos - 1)) {
1325         llvm::APInt sign_bit = llvm::APInt::getSignMask(sign_bit_pos + 1);
1326         llvm::APInt bitwize_and = m_integer & sign_bit;
1327         if (bitwize_and.getBoolValue()) {
1328           const llvm::APInt mask =
1329               ~(sign_bit) + llvm::APInt(m_integer.getBitWidth(), 1);
1330           m_integer |= mask;
1331         }
1332         return true;
1333       }
1334       break;
1335     }
1336   }
1337   return false;
1338 }
1339 
1340 size_t Scalar::GetAsMemoryData(void *dst, size_t dst_len,
1341                                lldb::ByteOrder dst_byte_order,
1342                                Status &error) const {
1343   // Get a data extractor that points to the native scalar data
1344   DataExtractor data;
1345   if (!GetData(data)) {
1346     error.SetErrorString("invalid scalar value");
1347     return 0;
1348   }
1349 
1350   const size_t src_len = data.GetByteSize();
1351 
1352   // Prepare a memory buffer that contains some or all of the register value
1353   const size_t bytes_copied =
1354       data.CopyByteOrderedData(0,               // src offset
1355                                src_len,         // src length
1356                                dst,             // dst buffer
1357                                dst_len,         // dst length
1358                                dst_byte_order); // dst byte order
1359   if (bytes_copied == 0)
1360     error.SetErrorString("failed to copy data");
1361 
1362   return bytes_copied;
1363 }
1364 
1365 bool Scalar::ExtractBitfield(uint32_t bit_size, uint32_t bit_offset) {
1366   if (bit_size == 0)
1367     return true;
1368 
1369   switch (m_type) {
1370   case Scalar::e_void:
1371   case Scalar::e_float:
1372   case Scalar::e_double:
1373   case Scalar::e_long_double:
1374     break;
1375 
1376   case Scalar::e_sint:
1377   case Scalar::e_slong:
1378   case Scalar::e_slonglong:
1379   case Scalar::e_sint128:
1380   case Scalar::e_sint256:
1381   case Scalar::e_sint512:
1382     m_integer = m_integer.ashr(bit_offset)
1383                     .sextOrTrunc(bit_size)
1384                     .sextOrSelf(8 * GetByteSize());
1385     return true;
1386 
1387   case Scalar::e_uint:
1388   case Scalar::e_ulong:
1389   case Scalar::e_ulonglong:
1390   case Scalar::e_uint128:
1391   case Scalar::e_uint256:
1392   case Scalar::e_uint512:
1393     m_integer = m_integer.lshr(bit_offset)
1394                     .zextOrTrunc(bit_size)
1395                     .zextOrSelf(8 * GetByteSize());
1396     return true;
1397   }
1398   return false;
1399 }
1400 
1401 bool lldb_private::operator==(const Scalar &lhs, const Scalar &rhs) {
1402   // If either entry is void then we can just compare the types
1403   if (lhs.m_type == Scalar::e_void || rhs.m_type == Scalar::e_void)
1404     return lhs.m_type == rhs.m_type;
1405 
1406   Scalar temp_value;
1407   const Scalar *a;
1408   const Scalar *b;
1409   llvm::APFloat::cmpResult result;
1410   switch (PromoteToMaxType(lhs, rhs, temp_value, a, b)) {
1411   case Scalar::e_void:
1412     break;
1413   case Scalar::e_sint:
1414   case Scalar::e_uint:
1415   case Scalar::e_slong:
1416   case Scalar::e_ulong:
1417   case Scalar::e_slonglong:
1418   case Scalar::e_ulonglong:
1419   case Scalar::e_sint128:
1420   case Scalar::e_uint128:
1421   case Scalar::e_sint256:
1422   case Scalar::e_uint256:
1423   case Scalar::e_sint512:
1424   case Scalar::e_uint512:
1425     return a->m_integer == b->m_integer;
1426   case Scalar::e_float:
1427   case Scalar::e_double:
1428   case Scalar::e_long_double:
1429     result = a->m_float.compare(b->m_float);
1430     if (result == llvm::APFloat::cmpEqual)
1431       return true;
1432   }
1433   return false;
1434 }
1435 
1436 bool lldb_private::operator!=(const Scalar &lhs, const Scalar &rhs) {
1437   return !(lhs == rhs);
1438 }
1439 
1440 bool lldb_private::operator<(const Scalar &lhs, const Scalar &rhs) {
1441   if (lhs.m_type == Scalar::e_void || rhs.m_type == Scalar::e_void)
1442     return false;
1443 
1444   Scalar temp_value;
1445   const Scalar *a;
1446   const Scalar *b;
1447   llvm::APFloat::cmpResult result;
1448   switch (PromoteToMaxType(lhs, rhs, temp_value, a, b)) {
1449   case Scalar::e_void:
1450     break;
1451   case Scalar::e_sint:
1452   case Scalar::e_slong:
1453   case Scalar::e_slonglong:
1454   case Scalar::e_sint128:
1455   case Scalar::e_sint256:
1456   case Scalar::e_sint512:
1457   case Scalar::e_uint512:
1458     return a->m_integer.slt(b->m_integer);
1459   case Scalar::e_uint:
1460   case Scalar::e_ulong:
1461   case Scalar::e_ulonglong:
1462   case Scalar::e_uint128:
1463   case Scalar::e_uint256:
1464     return a->m_integer.ult(b->m_integer);
1465   case Scalar::e_float:
1466   case Scalar::e_double:
1467   case Scalar::e_long_double:
1468     result = a->m_float.compare(b->m_float);
1469     if (result == llvm::APFloat::cmpLessThan)
1470       return true;
1471   }
1472   return false;
1473 }
1474 
1475 bool lldb_private::operator<=(const Scalar &lhs, const Scalar &rhs) {
1476   return !(rhs < lhs);
1477 }
1478 
1479 bool lldb_private::operator>(const Scalar &lhs, const Scalar &rhs) {
1480   return rhs < lhs;
1481 }
1482 
1483 bool lldb_private::operator>=(const Scalar &lhs, const Scalar &rhs) {
1484   return !(lhs < rhs);
1485 }
1486 
1487 bool Scalar::ClearBit(uint32_t bit) {
1488   switch (m_type) {
1489   case e_void:
1490     break;
1491   case e_sint:
1492   case e_uint:
1493   case e_slong:
1494   case e_ulong:
1495   case e_slonglong:
1496   case e_ulonglong:
1497   case e_sint128:
1498   case e_uint128:
1499   case e_sint256:
1500   case e_uint256:
1501   case e_sint512:
1502   case e_uint512:
1503     m_integer.clearBit(bit);
1504     return true;
1505   case e_float:
1506   case e_double:
1507   case e_long_double:
1508     break;
1509   }
1510   return false;
1511 }
1512 
1513 bool Scalar::SetBit(uint32_t bit) {
1514   switch (m_type) {
1515   case e_void:
1516     break;
1517   case e_sint:
1518   case e_uint:
1519   case e_slong:
1520   case e_ulong:
1521   case e_slonglong:
1522   case e_ulonglong:
1523   case e_sint128:
1524   case e_uint128:
1525   case e_sint256:
1526   case e_uint256:
1527   case e_sint512:
1528   case e_uint512:
1529     m_integer.setBit(bit);
1530     return true;
1531   case e_float:
1532   case e_double:
1533   case e_long_double:
1534     break;
1535   }
1536   return false;
1537 }
1538 
1539 llvm::raw_ostream &lldb_private::operator<<(llvm::raw_ostream &os, const Scalar &scalar) {
1540   StreamString s;
1541   scalar.GetValue(&s, /*show_type*/ true);
1542   return os << s.GetString();
1543 }
1544