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