]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/lldb/include/lldb/Utility/Scalar.h
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm-project / lldb / include / lldb / Utility / Scalar.h
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
9 #ifndef LLDB_UTILITY_SCALAR_H
10 #define LLDB_UTILITY_SCALAR_H
11
12 #include "lldb/Utility/Status.h"
13 #include "lldb/lldb-enumerations.h"
14 #include "lldb/lldb-private-types.h"
15 #include "lldb/Utility/LLDBAssert.h"
16 #include "llvm/ADT/APFloat.h"
17 #include "llvm/ADT/APInt.h"
18 #include <cstddef>
19 #include <cstdint>
20
21 namespace lldb_private {
22 class DataExtractor;
23 class Stream;
24 } // namespace lldb_private
25
26 #define NUM_OF_WORDS_INT128 2
27 #define BITWIDTH_INT128 128
28 #define NUM_OF_WORDS_INT256 4
29 #define BITWIDTH_INT256 256
30 #define NUM_OF_WORDS_INT512 8
31 #define BITWIDTH_INT512 512
32
33 namespace lldb_private {
34
35 // A class designed to hold onto values and their corresponding types.
36 // Operators are defined and Scalar objects will correctly promote their types
37 // and values before performing these operations. Type promotion currently
38 // follows the ANSI C type promotion rules.
39 class Scalar {
40 public:
41   enum Type {
42     e_void = 0,
43     e_sint,
44     e_uint,
45     e_slong,
46     e_ulong,
47     e_slonglong,
48     e_ulonglong,
49     e_sint128,
50     e_uint128,
51     e_sint256,
52     e_uint256,
53     e_sint512,
54     e_uint512,
55     e_float,
56     e_double,
57     e_long_double
58   };
59
60   // Constructors and Destructors
61   Scalar();
62   Scalar(int v) : m_type(e_sint), m_float(static_cast<float>(0)) {
63     m_integer = llvm::APInt(sizeof(int) * 8, v, true);
64   }
65   Scalar(unsigned int v) : m_type(e_uint), m_float(static_cast<float>(0)) {
66     m_integer = llvm::APInt(sizeof(int) * 8, v);
67   }
68   Scalar(long v) : m_type(e_slong), m_float(static_cast<float>(0)) {
69     m_integer = llvm::APInt(sizeof(long) * 8, v, true);
70   }
71   Scalar(unsigned long v) : m_type(e_ulong), m_float(static_cast<float>(0)) {
72     m_integer = llvm::APInt(sizeof(long) * 8, v);
73   }
74   Scalar(long long v) : m_type(e_slonglong), m_float(static_cast<float>(0)) {
75     m_integer = llvm::APInt(sizeof(long long) * 8, v, true);
76   }
77   Scalar(unsigned long long v)
78       : m_type(e_ulonglong), m_float(static_cast<float>(0)) {
79     m_integer = llvm::APInt(sizeof(long long) * 8, v);
80   }
81   Scalar(float v) : m_type(e_float), m_float(v) { m_float = llvm::APFloat(v); }
82   Scalar(double v) : m_type(e_double), m_float(v) {
83     m_float = llvm::APFloat(v);
84   }
85   Scalar(long double v, bool ieee_quad)
86       : m_type(e_long_double), m_float(static_cast<float>(0)),
87         m_ieee_quad(ieee_quad) {
88     if (ieee_quad)
89       m_float =
90           llvm::APFloat(llvm::APFloat::IEEEquad(),
91                         llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
92                                     (reinterpret_cast<type128 *>(&v))->x));
93     else
94       m_float =
95           llvm::APFloat(llvm::APFloat::x87DoubleExtended(),
96                         llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
97                                     (reinterpret_cast<type128 *>(&v))->x));
98   }
99   Scalar(llvm::APInt v) : m_type(), m_float(static_cast<float>(0)) {
100     m_integer = llvm::APInt(v);
101     switch (m_integer.getBitWidth()) {
102     case 8:
103     case 16:
104     case 32:
105       m_type = e_sint;
106       return;
107     case 64:
108       m_type = e_slonglong;
109       return;
110     case 128:
111       m_type = e_sint128;
112       return;
113     case 256:
114       m_type = e_sint256;
115       return;
116     case 512:
117       m_type = e_sint512;
118       return;
119     }
120     lldbassert(false && "unsupported bitwidth");
121   }
122   // Scalar(const RegisterValue& reg_value);
123   virtual ~Scalar();
124
125   bool SignExtend(uint32_t bit_pos);
126
127   bool ExtractBitfield(uint32_t bit_size, uint32_t bit_offset);
128
129   bool SetBit(uint32_t bit);
130
131   bool ClearBit(uint32_t bit);
132
133   const void *GetBytes() const;
134
135   size_t GetByteSize() const;
136
137   bool GetData(DataExtractor &data, size_t limit_byte_size = UINT32_MAX) const;
138
139   size_t GetAsMemoryData(void *dst, size_t dst_len,
140                          lldb::ByteOrder dst_byte_order, Status &error) const;
141
142   bool IsZero() const;
143
144   void Clear() {
145     m_type = e_void;
146     m_integer.clearAllBits();
147   }
148
149   const char *GetTypeAsCString() const;
150
151   void GetValue(Stream *s, bool show_type) const;
152
153   bool IsValid() const {
154     return (m_type >= e_sint) && (m_type <= e_long_double);
155   }
156
157   bool Promote(Scalar::Type type);
158
159   bool MakeSigned();
160
161   bool MakeUnsigned();
162
163   static const char *GetValueTypeAsCString(Scalar::Type value_type);
164
165   static Scalar::Type
166   GetValueTypeForSignedIntegerWithByteSize(size_t byte_size);
167
168   static Scalar::Type
169   GetValueTypeForUnsignedIntegerWithByteSize(size_t byte_size);
170
171   static Scalar::Type GetValueTypeForFloatWithByteSize(size_t byte_size);
172
173   // All operators can benefits from the implicit conversions that will happen
174   // automagically by the compiler, so no temporary objects will need to be
175   // created. As a result, we currently don't need a variety of overloaded set
176   // value accessors.
177   Scalar &operator=(const int i);
178   Scalar &operator=(unsigned int v);
179   Scalar &operator=(long v);
180   Scalar &operator=(unsigned long v);
181   Scalar &operator=(long long v);
182   Scalar &operator=(unsigned long long v);
183   Scalar &operator=(float v);
184   Scalar &operator=(double v);
185   Scalar &operator=(long double v);
186   Scalar &operator=(llvm::APInt v);
187   Scalar &operator=(const Scalar &rhs); // Assignment operator
188   Scalar &operator+=(const Scalar &rhs);
189   Scalar &operator<<=(const Scalar &rhs); // Shift left
190   Scalar &operator>>=(const Scalar &rhs); // Shift right (arithmetic)
191   Scalar &operator&=(const Scalar &rhs);
192
193   // Shifts the current value to the right without maintaining the current sign
194   // of the value (if it is signed).
195   bool ShiftRightLogical(const Scalar &rhs); // Returns true on success
196
197   // Takes the absolute value of the current value if it is signed, else the
198   // value remains unchanged. Returns false if the contained value has a void
199   // type.
200   bool AbsoluteValue(); // Returns true on success
201   // Negates the current value (even for unsigned values). Returns false if the
202   // contained value has a void type.
203   bool UnaryNegate(); // Returns true on success
204   // Inverts all bits in the current value as long as it isn't void or a
205   // float/double/long double type. Returns false if the contained value has a
206   // void/float/double/long double type, else the value is inverted and true is
207   // returned.
208   bool OnesComplement(); // Returns true on success
209
210   // Access the type of the current value.
211   Scalar::Type GetType() const { return m_type; }
212
213   // Returns a casted value of the current contained data without modifying the
214   // current value. FAIL_VALUE will be returned if the type of the value is
215   // void or invalid.
216   int SInt(int fail_value = 0) const;
217
218   unsigned char UChar(unsigned char fail_value = 0) const;
219
220   signed char SChar(char fail_value = 0) const;
221
222   unsigned short UShort(unsigned short fail_value = 0) const;
223
224   short SShort(short fail_value = 0) const;
225
226   unsigned int UInt(unsigned int fail_value = 0) const;
227
228   long SLong(long fail_value = 0) const;
229
230   unsigned long ULong(unsigned long fail_value = 0) const;
231
232   long long SLongLong(long long fail_value = 0) const;
233
234   unsigned long long ULongLong(unsigned long long fail_value = 0) const;
235
236   llvm::APInt SInt128(llvm::APInt &fail_value) const;
237
238   llvm::APInt UInt128(const llvm::APInt &fail_value) const;
239
240   float Float(float fail_value = 0.0f) const;
241
242   double Double(double fail_value = 0.0) const;
243
244   long double LongDouble(long double fail_value = 0.0) const;
245
246   Status SetValueFromCString(const char *s, lldb::Encoding encoding,
247                              size_t byte_size);
248
249   Status SetValueFromData(DataExtractor &data, lldb::Encoding encoding,
250                           size_t byte_size);
251
252   static bool UIntValueIsValidForSize(uint64_t uval64, size_t total_byte_size) {
253     if (total_byte_size > 8)
254       return false;
255
256     if (total_byte_size == 8)
257       return true;
258
259     const uint64_t max = (static_cast<uint64_t>(1)
260                           << static_cast<uint64_t>(total_byte_size * 8)) -
261                          1;
262     return uval64 <= max;
263   }
264
265   static bool SIntValueIsValidForSize(int64_t sval64, size_t total_byte_size) {
266     if (total_byte_size > 8)
267       return false;
268
269     if (total_byte_size == 8)
270       return true;
271
272     const int64_t max = (static_cast<int64_t>(1)
273                          << static_cast<uint64_t>(total_byte_size * 8 - 1)) -
274                         1;
275     const int64_t min = ~(max);
276     return min <= sval64 && sval64 <= max;
277   }
278
279 protected:
280   typedef char schar_t;
281   typedef unsigned char uchar_t;
282   typedef short sshort_t;
283   typedef unsigned short ushort_t;
284   typedef int sint_t;
285   typedef unsigned int uint_t;
286   typedef long slong_t;
287   typedef unsigned long ulong_t;
288   typedef long long slonglong_t;
289   typedef unsigned long long ulonglong_t;
290   typedef float float_t;
291   typedef double double_t;
292   typedef long double long_double_t;
293
294   // Classes that inherit from Scalar can see and modify these
295   Scalar::Type m_type;
296   llvm::APInt m_integer;
297   llvm::APFloat m_float;
298   bool m_ieee_quad = false;
299
300 private:
301   friend const Scalar operator+(const Scalar &lhs, const Scalar &rhs);
302   friend const Scalar operator-(const Scalar &lhs, const Scalar &rhs);
303   friend const Scalar operator/(const Scalar &lhs, const Scalar &rhs);
304   friend const Scalar operator*(const Scalar &lhs, const Scalar &rhs);
305   friend const Scalar operator&(const Scalar &lhs, const Scalar &rhs);
306   friend const Scalar operator|(const Scalar &lhs, const Scalar &rhs);
307   friend const Scalar operator%(const Scalar &lhs, const Scalar &rhs);
308   friend const Scalar operator^(const Scalar &lhs, const Scalar &rhs);
309   friend const Scalar operator<<(const Scalar &lhs, const Scalar &rhs);
310   friend const Scalar operator>>(const Scalar &lhs, const Scalar &rhs);
311   friend bool operator==(const Scalar &lhs, const Scalar &rhs);
312   friend bool operator!=(const Scalar &lhs, const Scalar &rhs);
313   friend bool operator<(const Scalar &lhs, const Scalar &rhs);
314   friend bool operator<=(const Scalar &lhs, const Scalar &rhs);
315   friend bool operator>(const Scalar &lhs, const Scalar &rhs);
316   friend bool operator>=(const Scalar &lhs, const Scalar &rhs);
317 };
318
319 // Split out the operators into a format where the compiler will be able to
320 // implicitly convert numbers into Scalar objects.
321 //
322 // This allows code like:
323 //      Scalar two(2);
324 //      Scalar four = two * 2;
325 //      Scalar eight = 2 * four;    // This would cause an error if the
326 //                                  // operator* was implemented as a
327 //                                  // member function.
328 // SEE:
329 //  Item 19 of "Effective C++ Second Edition" by Scott Meyers
330 //  Differentiate among members functions, non-member functions, and
331 //  friend functions
332 const Scalar operator+(const Scalar &lhs, const Scalar &rhs);
333 const Scalar operator-(const Scalar &lhs, const Scalar &rhs);
334 const Scalar operator/(const Scalar &lhs, const Scalar &rhs);
335 const Scalar operator*(const Scalar &lhs, const Scalar &rhs);
336 const Scalar operator&(const Scalar &lhs, const Scalar &rhs);
337 const Scalar operator|(const Scalar &lhs, const Scalar &rhs);
338 const Scalar operator%(const Scalar &lhs, const Scalar &rhs);
339 const Scalar operator^(const Scalar &lhs, const Scalar &rhs);
340 const Scalar operator<<(const Scalar &lhs, const Scalar &rhs);
341 const Scalar operator>>(const Scalar &lhs, const Scalar &rhs);
342 bool operator==(const Scalar &lhs, const Scalar &rhs);
343 bool operator!=(const Scalar &lhs, const Scalar &rhs);
344 bool operator<(const Scalar &lhs, const Scalar &rhs);
345 bool operator<=(const Scalar &lhs, const Scalar &rhs);
346 bool operator>(const Scalar &lhs, const Scalar &rhs);
347 bool operator>=(const Scalar &lhs, const Scalar &rhs);
348
349 llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const Scalar &scalar);
350
351 } // namespace lldb_private
352
353 #endif // LLDB_UTILITY_SCALAR_H