Scalar.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //===-- Scalar.h ------------------------------------------------*- C++ -*-===//
  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. #ifndef LLDB_UTILITY_SCALAR_H
  9. #define LLDB_UTILITY_SCALAR_H
  10. #include "lldb/Utility/LLDBAssert.h"
  11. #include "lldb/Utility/Status.h"
  12. #include "lldb/lldb-enumerations.h"
  13. #include "lldb/lldb-private-types.h"
  14. #include "llvm/ADT/APFloat.h"
  15. #include "llvm/ADT/APSInt.h"
  16. #include <cstddef>
  17. #include <cstdint>
  18. #include <utility>
  19. namespace lldb_private {
  20. class DataExtractor;
  21. class Stream;
  22. #define NUM_OF_WORDS_INT128 2
  23. #define BITWIDTH_INT128 128
  24. // A class designed to hold onto values and their corresponding types.
  25. // Operators are defined and Scalar objects will correctly promote their types
  26. // and values before performing these operations. Type promotion currently
  27. // follows the ANSI C type promotion rules.
  28. class Scalar {
  29. template<typename T>
  30. static llvm::APSInt MakeAPSInt(T v) {
  31. static_assert(std::is_integral<T>::value, "");
  32. static_assert(sizeof(T) <= sizeof(uint64_t), "Conversion loses precision!");
  33. return llvm::APSInt(
  34. llvm::APInt(sizeof(T) * 8, uint64_t(v), std::is_signed<T>::value),
  35. std::is_unsigned<T>::value);
  36. }
  37. public:
  38. enum Type {
  39. e_void = 0,
  40. e_int,
  41. e_float,
  42. };
  43. // Constructors and Destructors
  44. Scalar() : m_type(e_void), m_float(0.0f) {}
  45. Scalar(int v) : m_type(e_int), m_integer(MakeAPSInt(v)), m_float(0.0f) {}
  46. Scalar(unsigned int v)
  47. : m_type(e_int), m_integer(MakeAPSInt(v)), m_float(0.0f) {}
  48. Scalar(long v) : m_type(e_int), m_integer(MakeAPSInt(v)), m_float(0.0f) {}
  49. Scalar(unsigned long v)
  50. : m_type(e_int), m_integer(MakeAPSInt(v)), m_float(0.0f) {}
  51. Scalar(long long v)
  52. : m_type(e_int), m_integer(MakeAPSInt(v)), m_float(0.0f) {}
  53. Scalar(unsigned long long v)
  54. : m_type(e_int), m_integer(MakeAPSInt(v)), m_float(0.0f) {}
  55. Scalar(float v) : m_type(e_float), m_float(v) {}
  56. Scalar(double v) : m_type(e_float), m_float(v) {}
  57. Scalar(long double v) : m_type(e_float), m_float(double(v)) {
  58. bool ignore;
  59. m_float.convert(llvm::APFloat::x87DoubleExtended(),
  60. llvm::APFloat::rmNearestTiesToEven, &ignore);
  61. }
  62. Scalar(llvm::APInt v)
  63. : m_type(e_int), m_integer(std::move(v), false), m_float(0.0f) {}
  64. Scalar(llvm::APSInt v)
  65. : m_type(e_int), m_integer(std::move(v)), m_float(0.0f) {}
  66. bool SignExtend(uint32_t bit_pos);
  67. bool ExtractBitfield(uint32_t bit_size, uint32_t bit_offset);
  68. bool SetBit(uint32_t bit);
  69. bool ClearBit(uint32_t bit);
  70. /// Store the binary representation of this value into the given storage.
  71. /// Exactly GetByteSize() bytes will be stored, and the buffer must be large
  72. /// enough to hold this data.
  73. void GetBytes(llvm::MutableArrayRef<uint8_t> storage) const;
  74. size_t GetByteSize() const;
  75. bool GetData(DataExtractor &data, size_t limit_byte_size = UINT32_MAX) const;
  76. size_t GetAsMemoryData(void *dst, size_t dst_len,
  77. lldb::ByteOrder dst_byte_order, Status &error) const;
  78. bool IsZero() const;
  79. void Clear() {
  80. m_type = e_void;
  81. m_integer.clearAllBits();
  82. }
  83. const char *GetTypeAsCString() const { return GetValueTypeAsCString(m_type); }
  84. void GetValue(Stream *s, bool show_type) const;
  85. bool IsValid() const { return (m_type >= e_int) && (m_type <= e_float); }
  86. /// Convert to an integer with \p bits and the given signedness.
  87. void TruncOrExtendTo(uint16_t bits, bool sign);
  88. bool IntegralPromote(uint16_t bits, bool sign);
  89. bool FloatPromote(const llvm::fltSemantics &semantics);
  90. bool IsSigned() const;
  91. bool MakeSigned();
  92. bool MakeUnsigned();
  93. static const char *GetValueTypeAsCString(Scalar::Type value_type);
  94. // All operators can benefits from the implicit conversions that will happen
  95. // automagically by the compiler, so no temporary objects will need to be
  96. // created. As a result, we currently don't need a variety of overloaded set
  97. // value accessors.
  98. Scalar &operator+=(Scalar rhs);
  99. Scalar &operator<<=(const Scalar &rhs); // Shift left
  100. Scalar &operator>>=(const Scalar &rhs); // Shift right (arithmetic)
  101. Scalar &operator&=(const Scalar &rhs);
  102. // Shifts the current value to the right without maintaining the current sign
  103. // of the value (if it is signed).
  104. bool ShiftRightLogical(const Scalar &rhs); // Returns true on success
  105. // Takes the absolute value of the current value if it is signed, else the
  106. // value remains unchanged. Returns false if the contained value has a void
  107. // type.
  108. bool AbsoluteValue(); // Returns true on success
  109. // Negates the current value (even for unsigned values). Returns false if the
  110. // contained value has a void type.
  111. bool UnaryNegate(); // Returns true on success
  112. // Inverts all bits in the current value as long as it isn't void or a
  113. // float/double/long double type. Returns false if the contained value has a
  114. // void/float/double/long double type, else the value is inverted and true is
  115. // returned.
  116. bool OnesComplement(); // Returns true on success
  117. // Access the type of the current value.
  118. Scalar::Type GetType() const { return m_type; }
  119. // Returns a casted value of the current contained data without modifying the
  120. // current value. FAIL_VALUE will be returned if the type of the value is
  121. // void or invalid.
  122. int SInt(int fail_value = 0) const;
  123. unsigned char UChar(unsigned char fail_value = 0) const;
  124. signed char SChar(signed char fail_value = 0) const;
  125. unsigned short UShort(unsigned short fail_value = 0) const;
  126. short SShort(short fail_value = 0) const;
  127. unsigned int UInt(unsigned int fail_value = 0) const;
  128. long SLong(long fail_value = 0) const;
  129. unsigned long ULong(unsigned long fail_value = 0) const;
  130. long long SLongLong(long long fail_value = 0) const;
  131. unsigned long long ULongLong(unsigned long long fail_value = 0) const;
  132. llvm::APInt SInt128(const llvm::APInt &fail_value) const;
  133. llvm::APInt UInt128(const llvm::APInt &fail_value) const;
  134. float Float(float fail_value = 0.0f) const;
  135. double Double(double fail_value = 0.0) const;
  136. long double LongDouble(long double fail_value = 0.0) const;
  137. Status SetValueFromCString(const char *s, lldb::Encoding encoding,
  138. size_t byte_size);
  139. Status SetValueFromData(const DataExtractor &data, lldb::Encoding encoding,
  140. size_t byte_size);
  141. protected:
  142. Scalar::Type m_type;
  143. llvm::APSInt m_integer;
  144. llvm::APFloat m_float;
  145. template <typename T> T GetAs(T fail_value) const;
  146. static Type PromoteToMaxType(Scalar &lhs, Scalar &rhs);
  147. using PromotionKey = std::tuple<Type, unsigned, bool>;
  148. PromotionKey GetPromoKey() const;
  149. static PromotionKey GetFloatPromoKey(const llvm::fltSemantics &semantics);
  150. private:
  151. friend const Scalar operator+(const Scalar &lhs, const Scalar &rhs);
  152. friend const Scalar operator-(Scalar lhs, Scalar rhs);
  153. friend const Scalar operator/(Scalar lhs, Scalar rhs);
  154. friend const Scalar operator*(Scalar lhs, Scalar rhs);
  155. friend const Scalar operator&(Scalar lhs, Scalar rhs);
  156. friend const Scalar operator|(Scalar lhs, Scalar rhs);
  157. friend const Scalar operator%(Scalar lhs, Scalar rhs);
  158. friend const Scalar operator^(Scalar lhs, Scalar rhs);
  159. friend const Scalar operator<<(const Scalar &lhs, const Scalar &rhs);
  160. friend const Scalar operator>>(const Scalar &lhs, const Scalar &rhs);
  161. friend bool operator==(Scalar lhs, Scalar rhs);
  162. friend bool operator!=(const Scalar &lhs, const Scalar &rhs);
  163. friend bool operator<(Scalar lhs, Scalar rhs);
  164. friend bool operator<=(const Scalar &lhs, const Scalar &rhs);
  165. friend bool operator>(const Scalar &lhs, const Scalar &rhs);
  166. friend bool operator>=(const Scalar &lhs, const Scalar &rhs);
  167. };
  168. // Split out the operators into a format where the compiler will be able to
  169. // implicitly convert numbers into Scalar objects.
  170. //
  171. // This allows code like:
  172. // Scalar two(2);
  173. // Scalar four = two * 2;
  174. // Scalar eight = 2 * four; // This would cause an error if the
  175. // // operator* was implemented as a
  176. // // member function.
  177. // SEE:
  178. // Item 19 of "Effective C++ Second Edition" by Scott Meyers
  179. // Differentiate among members functions, non-member functions, and
  180. // friend functions
  181. const Scalar operator+(const Scalar &lhs, const Scalar &rhs);
  182. const Scalar operator-(Scalar lhs, Scalar rhs);
  183. const Scalar operator/(Scalar lhs, Scalar rhs);
  184. const Scalar operator*(Scalar lhs, Scalar rhs);
  185. const Scalar operator&(Scalar lhs, Scalar rhs);
  186. const Scalar operator|(Scalar lhs, Scalar rhs);
  187. const Scalar operator%(Scalar lhs, Scalar rhs);
  188. const Scalar operator^(Scalar lhs, Scalar rhs);
  189. const Scalar operator<<(const Scalar &lhs, const Scalar &rhs);
  190. const Scalar operator>>(const Scalar &lhs, const Scalar &rhs);
  191. bool operator==(Scalar lhs, Scalar rhs);
  192. bool operator!=(const Scalar &lhs, const Scalar &rhs);
  193. bool operator<(Scalar lhs, Scalar rhs);
  194. bool operator<=(const Scalar &lhs, const Scalar &rhs);
  195. bool operator>(const Scalar &lhs, const Scalar &rhs);
  196. bool operator>=(const Scalar &lhs, const Scalar &rhs);
  197. llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const Scalar &scalar);
  198. } // namespace lldb_private
  199. #endif // LLDB_UTILITY_SCALAR_H