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 template <typename T> T Scalar::GetAs(T fail_value) const {
561   switch (GetCategory(m_type)) {
562   case Category::Void:
563     break;
564   case Category::Integral:
565     if (IsSigned(m_type))
566       return m_integer.sextOrTrunc(sizeof(T) * 8).getSExtValue();
567     return m_integer.zextOrTrunc(sizeof(T) * 8).getZExtValue();
568   case Category::Float: {
569     llvm::APSInt result(sizeof(T) * 8, std::is_unsigned<T>::value);
570     bool isExact;
571     m_float.convertToInteger(result, llvm::APFloat::rmTowardZero, &isExact);
572     return result.getSExtValue();
573   }
574   }
575   return fail_value;
576 }
577 
578 signed char Scalar::SChar(signed char fail_value) const {
579   return GetAs<signed char>(fail_value);
580 }
581 
582 unsigned char Scalar::UChar(unsigned char fail_value) const {
583   return GetAs<unsigned char>(fail_value);
584 }
585 
586 short Scalar::SShort(short fail_value) const {
587   return GetAs<short>(fail_value);
588 }
589 
590 unsigned short Scalar::UShort(unsigned short fail_value) const {
591   return GetAs<unsigned short>(fail_value);
592 }
593 
594 int Scalar::SInt(int fail_value) const { return GetAs<int>(fail_value); }
595 
596 unsigned int Scalar::UInt(unsigned int fail_value) const {
597   return GetAs<unsigned int>(fail_value);
598 }
599 
600 long Scalar::SLong(long fail_value) const { return GetAs<long>(fail_value); }
601 
602 unsigned long Scalar::ULong(unsigned long fail_value) const {
603   return GetAs<unsigned long>(fail_value);
604 }
605 
606 long long Scalar::SLongLong(long long fail_value) const {
607   return GetAs<long long>(fail_value);
608 }
609 
610 unsigned long long Scalar::ULongLong(unsigned long long fail_value) const {
611   return GetAs<unsigned long long>(fail_value);
612 }
613 
614 llvm::APInt Scalar::SInt128(const llvm::APInt &fail_value) const {
615   switch (m_type) {
616   case e_void:
617     break;
618   case e_sint:
619   case e_uint:
620   case e_slong:
621   case e_ulong:
622   case e_slonglong:
623   case e_ulonglong:
624   case e_sint128:
625   case e_uint128:
626   case e_sint256:
627   case e_uint256:
628   case e_sint512:
629   case e_uint512:
630     return m_integer;
631   case e_float:
632   case e_double:
633   case e_long_double:
634     return m_float.bitcastToAPInt();
635   }
636   return fail_value;
637 }
638 
639 llvm::APInt Scalar::UInt128(const llvm::APInt &fail_value) const {
640   switch (m_type) {
641   case e_void:
642     break;
643   case e_sint:
644   case e_uint:
645   case e_slong:
646   case e_ulong:
647   case e_slonglong:
648   case e_ulonglong:
649   case e_sint128:
650   case e_uint128:
651   case e_sint256:
652   case e_uint256:
653   case e_sint512:
654   case e_uint512:
655     return m_integer;
656   case e_float:
657   case e_double:
658   case e_long_double:
659     return m_float.bitcastToAPInt();
660   }
661   return fail_value;
662 }
663 
664 float Scalar::Float(float fail_value) const {
665   switch (m_type) {
666   case e_void:
667     break;
668   case e_sint:
669   case e_slong:
670   case e_slonglong:
671   case e_sint128:
672   case e_sint256:
673   case e_sint512:
674     return llvm::APIntOps::RoundSignedAPIntToFloat(m_integer);
675 
676   case e_uint:
677   case e_ulong:
678   case e_ulonglong:
679   case e_uint128:
680   case e_uint256:
681   case e_uint512:
682     return llvm::APIntOps::RoundAPIntToFloat(m_integer);
683 
684   case e_float:
685     return m_float.convertToFloat();
686   case e_double:
687     return static_cast<float_t>(m_float.convertToDouble());
688   case e_long_double:
689     llvm::APInt ldbl_val = m_float.bitcastToAPInt();
690     return ldbl_val.bitsToFloat();
691   }
692   return fail_value;
693 }
694 
695 double Scalar::Double(double fail_value) const {
696   switch (m_type) {
697   case e_void:
698     break;
699   case e_sint:
700   case e_slong:
701   case e_slonglong:
702   case e_sint128:
703   case e_sint256:
704   case e_sint512:
705     return llvm::APIntOps::RoundSignedAPIntToDouble(m_integer);
706 
707   case e_uint:
708   case e_ulong:
709   case e_ulonglong:
710   case e_uint128:
711   case e_uint256:
712   case e_uint512:
713     return llvm::APIntOps::RoundAPIntToDouble(m_integer);
714 
715   case e_float:
716     return static_cast<double_t>(m_float.convertToFloat());
717   case e_double:
718     return m_float.convertToDouble();
719   case e_long_double:
720     llvm::APInt ldbl_val = m_float.bitcastToAPInt();
721     return ldbl_val.bitsToFloat();
722   }
723   return fail_value;
724 }
725 
726 long double Scalar::LongDouble(long double fail_value) const {
727   switch (m_type) {
728   case e_void:
729     break;
730   case e_sint:
731   case e_slong:
732   case e_slonglong:
733   case e_sint128:
734   case e_sint256:
735   case e_sint512:
736     return static_cast<long_double_t>(
737         llvm::APIntOps::RoundSignedAPIntToDouble(m_integer));
738 
739   case e_uint:
740   case e_ulong:
741   case e_ulonglong:
742   case e_uint128:
743   case e_uint256:
744   case e_uint512:
745     return static_cast<long_double_t>(
746         llvm::APIntOps::RoundAPIntToDouble(m_integer));
747 
748   case e_float:
749     return static_cast<long_double_t>(m_float.convertToFloat());
750   case e_double:
751     return static_cast<long_double_t>(m_float.convertToDouble());
752   case e_long_double:
753     llvm::APInt ldbl_val = m_float.bitcastToAPInt();
754     return static_cast<long_double_t>(ldbl_val.bitsToDouble());
755   }
756   return fail_value;
757 }
758 
759 Scalar &Scalar::operator+=(const Scalar &rhs) {
760   Scalar temp_value;
761   const Scalar *a;
762   const Scalar *b;
763   if ((m_type = PromoteToMaxType(*this, rhs, temp_value, a, b)) !=
764       Scalar::e_void) {
765     switch (GetCategory(m_type)) {
766     case Category::Void:
767       break;
768     case Category::Integral:
769       m_integer = a->m_integer + b->m_integer;
770       break;
771 
772     case Category::Float:
773       m_float = a->m_float + b->m_float;
774       break;
775     }
776   }
777   return *this;
778 }
779 
780 Scalar &Scalar::operator<<=(const Scalar &rhs) {
781   if (GetCategory(m_type) == Category::Integral &&
782       GetCategory(rhs.m_type) == Category::Integral)
783     m_integer <<= rhs.m_integer;
784   else
785     m_type = e_void;
786   return *this;
787 }
788 
789 bool Scalar::ShiftRightLogical(const Scalar &rhs) {
790   if (GetCategory(m_type) == Category::Integral &&
791       GetCategory(rhs.m_type) == Category::Integral) {
792     m_integer = m_integer.lshr(rhs.m_integer);
793     return true;
794   }
795   m_type = e_void;
796   return false;
797 }
798 
799 Scalar &Scalar::operator>>=(const Scalar &rhs) {
800   switch (m_type) {
801   case e_void:
802   case e_float:
803   case e_double:
804   case e_long_double:
805     m_type = e_void;
806     break;
807 
808   case e_sint:
809   case e_uint:
810   case e_slong:
811   case e_ulong:
812   case e_slonglong:
813   case e_ulonglong:
814   case e_sint128:
815   case e_uint128:
816   case e_sint256:
817   case e_uint256:
818   case e_sint512:
819   case e_uint512:
820     switch (rhs.m_type) {
821     case e_void:
822     case e_float:
823     case e_double:
824     case e_long_double:
825       m_type = e_void;
826       break;
827     case e_sint:
828     case e_uint:
829     case e_slong:
830     case e_ulong:
831     case e_slonglong:
832     case e_ulonglong:
833     case e_sint128:
834     case e_uint128:
835     case e_sint256:
836     case e_uint256:
837     case e_sint512:
838     case e_uint512:
839       m_integer = m_integer.ashr(rhs.m_integer);
840       break;
841     }
842     break;
843   }
844   return *this;
845 }
846 
847 Scalar &Scalar::operator&=(const Scalar &rhs) {
848   if (GetCategory(m_type) == Category::Integral &&
849       GetCategory(rhs.m_type) == Category::Integral)
850     m_integer &= rhs.m_integer;
851   else
852     m_type = e_void;
853   return *this;
854 }
855 
856 bool Scalar::AbsoluteValue() {
857   switch (m_type) {
858   case e_void:
859     break;
860 
861   case e_sint:
862   case e_slong:
863   case e_slonglong:
864   case e_sint128:
865   case e_sint256:
866   case e_sint512:
867     if (m_integer.isNegative())
868       m_integer = -m_integer;
869     return true;
870 
871   case e_uint:
872   case e_ulong:
873   case e_ulonglong:
874     return true;
875   case e_uint128:
876   case e_uint256:
877   case e_uint512:
878   case e_float:
879   case e_double:
880   case e_long_double:
881     m_float.clearSign();
882     return true;
883   }
884   return false;
885 }
886 
887 bool Scalar::UnaryNegate() {
888   switch (GetCategory(m_type)) {
889   case Category::Void:
890     break;
891   case Category::Integral:
892     m_integer = -m_integer;
893     return true;
894   case Category::Float:
895     m_float.changeSign();
896     return true;
897   }
898   return false;
899 }
900 
901 bool Scalar::OnesComplement() {
902   if (GetCategory(m_type) == Category::Integral) {
903     m_integer = ~m_integer;
904     return true;
905   }
906 
907   return false;
908 }
909 
910 const Scalar lldb_private::operator+(const Scalar &lhs, const Scalar &rhs) {
911   Scalar result = lhs;
912   result += rhs;
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     switch (GetCategory(result.m_type)) {
924     case Category::Void:
925       break;
926     case Category::Integral:
927       result.m_integer = a->m_integer - b->m_integer;
928       break;
929     case Category::Float:
930       result.m_float = a->m_float - b->m_float;
931       break;
932     }
933   }
934   return result;
935 }
936 
937 const Scalar lldb_private::operator/(const Scalar &lhs, const Scalar &rhs) {
938   Scalar result;
939   Scalar temp_value;
940   const Scalar *a;
941   const Scalar *b;
942   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
943           Scalar::e_void &&
944       !b->IsZero()) {
945     switch (GetCategory(result.m_type)) {
946     case Category::Void:
947       break;
948     case Category::Integral:
949       if (IsSigned(result.m_type))
950         result.m_integer = a->m_integer.sdiv(b->m_integer);
951       else
952         result.m_integer = a->m_integer.udiv(b->m_integer);
953       return result;
954     case Category::Float:
955       result.m_float = a->m_float / b->m_float;
956       return result;
957     }
958   }
959   // For division only, the only way it should make it here is if a promotion
960   // failed, or if we are trying to do a divide by zero.
961   result.m_type = Scalar::e_void;
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     switch (GetCategory(result.m_type)) {
973     case Category::Void:
974       break;
975     case Category::Integral:
976       result.m_integer = a->m_integer * b->m_integer;
977       break;
978     case Category::Float:
979       result.m_float = a->m_float * b->m_float;
980       break;
981     }
982   }
983   return result;
984 }
985 
986 const Scalar lldb_private::operator&(const Scalar &lhs, const Scalar &rhs) {
987   Scalar result;
988   Scalar temp_value;
989   const Scalar *a;
990   const Scalar *b;
991   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
992       Scalar::e_void) {
993     if (GetCategory(result.m_type) == Category::Integral)
994       result.m_integer = a->m_integer & b->m_integer;
995     else
996       result.m_type = Scalar::e_void;
997   }
998   return result;
999 }
1000 
1001 const Scalar lldb_private::operator|(const Scalar &lhs, const Scalar &rhs) {
1002   Scalar result;
1003   Scalar temp_value;
1004   const Scalar *a;
1005   const Scalar *b;
1006   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
1007       Scalar::e_void) {
1008     if (GetCategory(result.m_type) == Category::Integral)
1009       result.m_integer = a->m_integer | b->m_integer;
1010     else
1011       result.m_type = Scalar::e_void;
1012   }
1013   return result;
1014 }
1015 
1016 const Scalar lldb_private::operator%(const Scalar &lhs, const Scalar &rhs) {
1017   Scalar result;
1018   Scalar temp_value;
1019   const Scalar *a;
1020   const Scalar *b;
1021   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
1022       Scalar::e_void) {
1023     if (!b->IsZero() && GetCategory(result.m_type) == Category::Integral) {
1024       if (IsSigned(result.m_type))
1025         result.m_integer = a->m_integer.srem(b->m_integer);
1026       else
1027         result.m_integer = a->m_integer.urem(b->m_integer);
1028       return result;
1029     }
1030   }
1031   result.m_type = Scalar::e_void;
1032   return result;
1033 }
1034 
1035 const Scalar lldb_private::operator^(const Scalar &lhs, const Scalar &rhs) {
1036   Scalar result;
1037   Scalar temp_value;
1038   const Scalar *a;
1039   const Scalar *b;
1040   if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
1041       Scalar::e_void) {
1042     if (GetCategory(result.m_type) == Category::Integral)
1043       result.m_integer = a->m_integer ^ b->m_integer;
1044     else
1045       result.m_type = Scalar::e_void;
1046   }
1047   return result;
1048 }
1049 
1050 const Scalar lldb_private::operator<<(const Scalar &lhs, const Scalar &rhs) {
1051   Scalar result = lhs;
1052   result <<= rhs;
1053   return result;
1054 }
1055 
1056 const Scalar lldb_private::operator>>(const Scalar &lhs, const Scalar &rhs) {
1057   Scalar result = lhs;
1058   result >>= rhs;
1059   return result;
1060 }
1061 
1062 Status Scalar::SetValueFromCString(const char *value_str, Encoding encoding,
1063                                    size_t byte_size) {
1064   Status error;
1065   if (value_str == nullptr || value_str[0] == '\0') {
1066     error.SetErrorString("Invalid c-string value string.");
1067     return error;
1068   }
1069   switch (encoding) {
1070   case eEncodingInvalid:
1071     error.SetErrorString("Invalid encoding.");
1072     break;
1073 
1074   case eEncodingUint:
1075     if (byte_size <= sizeof(uint64_t)) {
1076       uint64_t uval64;
1077       if (!llvm::to_integer(value_str, uval64))
1078         error.SetErrorStringWithFormat(
1079             "'%s' is not a valid unsigned integer string value", value_str);
1080       else if (!UIntValueIsValidForSize(uval64, byte_size))
1081         error.SetErrorStringWithFormat(
1082             "value 0x%" PRIx64 " is too large to fit in a %" PRIu64
1083             " byte unsigned integer value",
1084             uval64, static_cast<uint64_t>(byte_size));
1085       else {
1086         m_type = Scalar::GetValueTypeForUnsignedIntegerWithByteSize(byte_size);
1087         switch (m_type) {
1088         case e_uint:
1089           m_integer = llvm::APInt(sizeof(uint_t) * 8, uval64, false);
1090           break;
1091         case e_ulong:
1092           m_integer = llvm::APInt(sizeof(ulong_t) * 8, uval64, false);
1093           break;
1094         case e_ulonglong:
1095           m_integer = llvm::APInt(sizeof(ulonglong_t) * 8, uval64, false);
1096           break;
1097         default:
1098           error.SetErrorStringWithFormat(
1099               "unsupported unsigned integer byte size: %" PRIu64 "",
1100               static_cast<uint64_t>(byte_size));
1101           break;
1102         }
1103       }
1104     } else {
1105       error.SetErrorStringWithFormat(
1106           "unsupported unsigned integer byte size: %" PRIu64 "",
1107           static_cast<uint64_t>(byte_size));
1108       return error;
1109     }
1110     break;
1111 
1112   case eEncodingSint:
1113     if (byte_size <= sizeof(int64_t)) {
1114       int64_t sval64;
1115       if (!llvm::to_integer(value_str, sval64))
1116         error.SetErrorStringWithFormat(
1117             "'%s' is not a valid signed integer string value", value_str);
1118       else if (!SIntValueIsValidForSize(sval64, byte_size))
1119         error.SetErrorStringWithFormat(
1120             "value 0x%" PRIx64 " is too large to fit in a %" PRIu64
1121             " byte signed integer value",
1122             sval64, static_cast<uint64_t>(byte_size));
1123       else {
1124         m_type = Scalar::GetValueTypeForSignedIntegerWithByteSize(byte_size);
1125         switch (m_type) {
1126         case e_sint:
1127           m_integer = llvm::APInt(sizeof(sint_t) * 8, sval64, true);
1128           break;
1129         case e_slong:
1130           m_integer = llvm::APInt(sizeof(slong_t) * 8, sval64, true);
1131           break;
1132         case e_slonglong:
1133           m_integer = llvm::APInt(sizeof(slonglong_t) * 8, sval64, true);
1134           break;
1135         default:
1136           error.SetErrorStringWithFormat(
1137               "unsupported signed integer byte size: %" PRIu64 "",
1138               static_cast<uint64_t>(byte_size));
1139           break;
1140         }
1141       }
1142     } else {
1143       error.SetErrorStringWithFormat(
1144           "unsupported signed integer byte size: %" PRIu64 "",
1145           static_cast<uint64_t>(byte_size));
1146       return error;
1147     }
1148     break;
1149 
1150   case eEncodingIEEE754:
1151     static float f_val;
1152     static double d_val;
1153     static long double l_val;
1154     if (byte_size == sizeof(float)) {
1155       if (::sscanf(value_str, "%f", &f_val) == 1) {
1156         m_float = llvm::APFloat(f_val);
1157         m_type = e_float;
1158       } else
1159         error.SetErrorStringWithFormat("'%s' is not a valid float string value",
1160                                        value_str);
1161     } else if (byte_size == sizeof(double)) {
1162       if (::sscanf(value_str, "%lf", &d_val) == 1) {
1163         m_float = llvm::APFloat(d_val);
1164         m_type = e_double;
1165       } else
1166         error.SetErrorStringWithFormat("'%s' is not a valid float string value",
1167                                        value_str);
1168     } else if (byte_size == sizeof(long double)) {
1169       if (::sscanf(value_str, "%Lf", &l_val) == 1) {
1170         m_float = llvm::APFloat(
1171             llvm::APFloat::x87DoubleExtended(),
1172             llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
1173                         (reinterpret_cast<type128 *>(&l_val))->x));
1174         m_type = e_long_double;
1175       } else
1176         error.SetErrorStringWithFormat("'%s' is not a valid float string value",
1177                                        value_str);
1178     } else {
1179       error.SetErrorStringWithFormat("unsupported float byte size: %" PRIu64 "",
1180                                      static_cast<uint64_t>(byte_size));
1181       return error;
1182     }
1183     break;
1184 
1185   case eEncodingVector:
1186     error.SetErrorString("vector encoding unsupported.");
1187     break;
1188   }
1189   if (error.Fail())
1190     m_type = e_void;
1191 
1192   return error;
1193 }
1194 
1195 Status Scalar::SetValueFromData(DataExtractor &data, lldb::Encoding encoding,
1196                                 size_t byte_size) {
1197   Status error;
1198 
1199   type128 int128;
1200   type256 int256;
1201   switch (encoding) {
1202   case lldb::eEncodingInvalid:
1203     error.SetErrorString("invalid encoding");
1204     break;
1205   case lldb::eEncodingVector:
1206     error.SetErrorString("vector encoding unsupported");
1207     break;
1208   case lldb::eEncodingUint: {
1209     lldb::offset_t offset = 0;
1210 
1211     switch (byte_size) {
1212     case 1:
1213       operator=(data.GetU8(&offset));
1214       break;
1215     case 2:
1216       operator=(data.GetU16(&offset));
1217       break;
1218     case 4:
1219       operator=(data.GetU32(&offset));
1220       break;
1221     case 8:
1222       operator=(data.GetU64(&offset));
1223       break;
1224     case 16:
1225       if (data.GetByteOrder() == eByteOrderBig) {
1226         int128.x[1] = data.GetU64(&offset);
1227         int128.x[0] = data.GetU64(&offset);
1228       } else {
1229         int128.x[0] = data.GetU64(&offset);
1230         int128.x[1] = data.GetU64(&offset);
1231       }
1232       operator=(llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128, int128.x));
1233       break;
1234     case 32:
1235       if (data.GetByteOrder() == eByteOrderBig) {
1236         int256.x[3] = data.GetU64(&offset);
1237         int256.x[2] = data.GetU64(&offset);
1238         int256.x[1] = data.GetU64(&offset);
1239         int256.x[0] = data.GetU64(&offset);
1240       } else {
1241         int256.x[0] = data.GetU64(&offset);
1242         int256.x[1] = data.GetU64(&offset);
1243         int256.x[2] = data.GetU64(&offset);
1244         int256.x[3] = data.GetU64(&offset);
1245       }
1246       operator=(llvm::APInt(BITWIDTH_INT256, NUM_OF_WORDS_INT256, int256.x));
1247       break;
1248     default:
1249       error.SetErrorStringWithFormat(
1250           "unsupported unsigned integer byte size: %" PRIu64 "",
1251           static_cast<uint64_t>(byte_size));
1252       break;
1253     }
1254   } break;
1255   case lldb::eEncodingSint: {
1256     lldb::offset_t offset = 0;
1257 
1258     switch (byte_size) {
1259     case 1:
1260       operator=(static_cast<int8_t>(data.GetU8(&offset)));
1261       break;
1262     case 2:
1263       operator=(static_cast<int16_t>(data.GetU16(&offset)));
1264       break;
1265     case 4:
1266       operator=(static_cast<int32_t>(data.GetU32(&offset)));
1267       break;
1268     case 8:
1269       operator=(static_cast<int64_t>(data.GetU64(&offset)));
1270       break;
1271     case 16:
1272       if (data.GetByteOrder() == eByteOrderBig) {
1273         int128.x[1] = data.GetU64(&offset);
1274         int128.x[0] = data.GetU64(&offset);
1275       } else {
1276         int128.x[0] = data.GetU64(&offset);
1277         int128.x[1] = data.GetU64(&offset);
1278       }
1279       operator=(llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128, int128.x));
1280       break;
1281     case 32:
1282       if (data.GetByteOrder() == eByteOrderBig) {
1283         int256.x[3] = data.GetU64(&offset);
1284         int256.x[2] = data.GetU64(&offset);
1285         int256.x[1] = data.GetU64(&offset);
1286         int256.x[0] = data.GetU64(&offset);
1287       } else {
1288         int256.x[0] = data.GetU64(&offset);
1289         int256.x[1] = data.GetU64(&offset);
1290         int256.x[2] = data.GetU64(&offset);
1291         int256.x[3] = data.GetU64(&offset);
1292       }
1293       operator=(llvm::APInt(BITWIDTH_INT256, NUM_OF_WORDS_INT256, int256.x));
1294       break;
1295     default:
1296       error.SetErrorStringWithFormat(
1297           "unsupported signed integer byte size: %" PRIu64 "",
1298           static_cast<uint64_t>(byte_size));
1299       break;
1300     }
1301   } break;
1302   case lldb::eEncodingIEEE754: {
1303     lldb::offset_t offset = 0;
1304 
1305     if (byte_size == sizeof(float))
1306       operator=(data.GetFloat(&offset));
1307     else if (byte_size == sizeof(double))
1308       operator=(data.GetDouble(&offset));
1309     else if (byte_size == sizeof(long double))
1310       operator=(data.GetLongDouble(&offset));
1311     else
1312       error.SetErrorStringWithFormat("unsupported float byte size: %" PRIu64 "",
1313                                      static_cast<uint64_t>(byte_size));
1314   } break;
1315   }
1316 
1317   return error;
1318 }
1319 
1320 bool Scalar::SignExtend(uint32_t sign_bit_pos) {
1321   const uint32_t max_bit_pos = GetByteSize() * 8;
1322 
1323   if (sign_bit_pos < max_bit_pos) {
1324     switch (m_type) {
1325     case Scalar::e_void:
1326     case Scalar::e_float:
1327     case Scalar::e_double:
1328     case Scalar::e_long_double:
1329       return false;
1330 
1331     case Scalar::e_sint:
1332     case Scalar::e_uint:
1333     case Scalar::e_slong:
1334     case Scalar::e_ulong:
1335     case Scalar::e_slonglong:
1336     case Scalar::e_ulonglong:
1337     case Scalar::e_sint128:
1338     case Scalar::e_uint128:
1339     case Scalar::e_sint256:
1340     case Scalar::e_uint256:
1341     case Scalar::e_sint512:
1342     case Scalar::e_uint512:
1343       if (max_bit_pos == sign_bit_pos)
1344         return true;
1345       else if (sign_bit_pos < (max_bit_pos - 1)) {
1346         llvm::APInt sign_bit = llvm::APInt::getSignMask(sign_bit_pos + 1);
1347         llvm::APInt bitwize_and = m_integer & sign_bit;
1348         if (bitwize_and.getBoolValue()) {
1349           const llvm::APInt mask =
1350               ~(sign_bit) + llvm::APInt(m_integer.getBitWidth(), 1);
1351           m_integer |= mask;
1352         }
1353         return true;
1354       }
1355       break;
1356     }
1357   }
1358   return false;
1359 }
1360 
1361 size_t Scalar::GetAsMemoryData(void *dst, size_t dst_len,
1362                                lldb::ByteOrder dst_byte_order,
1363                                Status &error) const {
1364   // Get a data extractor that points to the native scalar data
1365   DataExtractor data;
1366   if (!GetData(data)) {
1367     error.SetErrorString("invalid scalar value");
1368     return 0;
1369   }
1370 
1371   const size_t src_len = data.GetByteSize();
1372 
1373   // Prepare a memory buffer that contains some or all of the register value
1374   const size_t bytes_copied =
1375       data.CopyByteOrderedData(0,               // src offset
1376                                src_len,         // src length
1377                                dst,             // dst buffer
1378                                dst_len,         // dst length
1379                                dst_byte_order); // dst byte order
1380   if (bytes_copied == 0)
1381     error.SetErrorString("failed to copy data");
1382 
1383   return bytes_copied;
1384 }
1385 
1386 bool Scalar::ExtractBitfield(uint32_t bit_size, uint32_t bit_offset) {
1387   if (bit_size == 0)
1388     return true;
1389 
1390   switch (m_type) {
1391   case Scalar::e_void:
1392   case Scalar::e_float:
1393   case Scalar::e_double:
1394   case Scalar::e_long_double:
1395     break;
1396 
1397   case Scalar::e_sint:
1398   case Scalar::e_slong:
1399   case Scalar::e_slonglong:
1400   case Scalar::e_sint128:
1401   case Scalar::e_sint256:
1402   case Scalar::e_sint512:
1403     m_integer = m_integer.ashr(bit_offset)
1404                     .sextOrTrunc(bit_size)
1405                     .sextOrSelf(8 * GetByteSize());
1406     return true;
1407 
1408   case Scalar::e_uint:
1409   case Scalar::e_ulong:
1410   case Scalar::e_ulonglong:
1411   case Scalar::e_uint128:
1412   case Scalar::e_uint256:
1413   case Scalar::e_uint512:
1414     m_integer = m_integer.lshr(bit_offset)
1415                     .zextOrTrunc(bit_size)
1416                     .zextOrSelf(8 * GetByteSize());
1417     return true;
1418   }
1419   return false;
1420 }
1421 
1422 bool lldb_private::operator==(const Scalar &lhs, const Scalar &rhs) {
1423   // If either entry is void then we can just compare the types
1424   if (lhs.m_type == Scalar::e_void || rhs.m_type == Scalar::e_void)
1425     return lhs.m_type == rhs.m_type;
1426 
1427   Scalar temp_value;
1428   const Scalar *a;
1429   const Scalar *b;
1430   llvm::APFloat::cmpResult result;
1431   switch (PromoteToMaxType(lhs, rhs, temp_value, a, b)) {
1432   case Scalar::e_void:
1433     break;
1434   case Scalar::e_sint:
1435   case Scalar::e_uint:
1436   case Scalar::e_slong:
1437   case Scalar::e_ulong:
1438   case Scalar::e_slonglong:
1439   case Scalar::e_ulonglong:
1440   case Scalar::e_sint128:
1441   case Scalar::e_uint128:
1442   case Scalar::e_sint256:
1443   case Scalar::e_uint256:
1444   case Scalar::e_sint512:
1445   case Scalar::e_uint512:
1446     return a->m_integer == b->m_integer;
1447   case Scalar::e_float:
1448   case Scalar::e_double:
1449   case Scalar::e_long_double:
1450     result = a->m_float.compare(b->m_float);
1451     if (result == llvm::APFloat::cmpEqual)
1452       return true;
1453   }
1454   return false;
1455 }
1456 
1457 bool lldb_private::operator!=(const Scalar &lhs, const Scalar &rhs) {
1458   return !(lhs == rhs);
1459 }
1460 
1461 bool lldb_private::operator<(const Scalar &lhs, const Scalar &rhs) {
1462   if (lhs.m_type == Scalar::e_void || rhs.m_type == Scalar::e_void)
1463     return false;
1464 
1465   Scalar temp_value;
1466   const Scalar *a;
1467   const Scalar *b;
1468   llvm::APFloat::cmpResult result;
1469   switch (PromoteToMaxType(lhs, rhs, temp_value, a, b)) {
1470   case Scalar::e_void:
1471     break;
1472   case Scalar::e_sint:
1473   case Scalar::e_slong:
1474   case Scalar::e_slonglong:
1475   case Scalar::e_sint128:
1476   case Scalar::e_sint256:
1477   case Scalar::e_sint512:
1478   case Scalar::e_uint512:
1479     return a->m_integer.slt(b->m_integer);
1480   case Scalar::e_uint:
1481   case Scalar::e_ulong:
1482   case Scalar::e_ulonglong:
1483   case Scalar::e_uint128:
1484   case Scalar::e_uint256:
1485     return a->m_integer.ult(b->m_integer);
1486   case Scalar::e_float:
1487   case Scalar::e_double:
1488   case Scalar::e_long_double:
1489     result = a->m_float.compare(b->m_float);
1490     if (result == llvm::APFloat::cmpLessThan)
1491       return true;
1492   }
1493   return false;
1494 }
1495 
1496 bool lldb_private::operator<=(const Scalar &lhs, const Scalar &rhs) {
1497   return !(rhs < lhs);
1498 }
1499 
1500 bool lldb_private::operator>(const Scalar &lhs, const Scalar &rhs) {
1501   return rhs < lhs;
1502 }
1503 
1504 bool lldb_private::operator>=(const Scalar &lhs, const Scalar &rhs) {
1505   return !(lhs < rhs);
1506 }
1507 
1508 bool Scalar::ClearBit(uint32_t bit) {
1509   switch (m_type) {
1510   case e_void:
1511     break;
1512   case e_sint:
1513   case e_uint:
1514   case e_slong:
1515   case e_ulong:
1516   case e_slonglong:
1517   case e_ulonglong:
1518   case e_sint128:
1519   case e_uint128:
1520   case e_sint256:
1521   case e_uint256:
1522   case e_sint512:
1523   case e_uint512:
1524     m_integer.clearBit(bit);
1525     return true;
1526   case e_float:
1527   case e_double:
1528   case e_long_double:
1529     break;
1530   }
1531   return false;
1532 }
1533 
1534 bool Scalar::SetBit(uint32_t bit) {
1535   switch (m_type) {
1536   case e_void:
1537     break;
1538   case e_sint:
1539   case e_uint:
1540   case e_slong:
1541   case e_ulong:
1542   case e_slonglong:
1543   case e_ulonglong:
1544   case e_sint128:
1545   case e_uint128:
1546   case e_sint256:
1547   case e_uint256:
1548   case e_sint512:
1549   case e_uint512:
1550     m_integer.setBit(bit);
1551     return true;
1552   case e_float:
1553   case e_double:
1554   case e_long_double:
1555     break;
1556   }
1557   return false;
1558 }
1559 
1560 llvm::raw_ostream &lldb_private::operator<<(llvm::raw_ostream &os, const Scalar &scalar) {
1561   StreamString s;
1562   scalar.GetValue(&s, /*show_type*/ true);
1563   return os << s.GetString();
1564 }
1565