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