1 //===-- Scalar.h ------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_Scalar_h_
11 #define liblldb_Scalar_h_
13 #include "lldb/Utility/Error.h" // for Error
14 #include "lldb/lldb-enumerations.h" // for Encoding, ByteOrder
15 #include "lldb/lldb-private-types.h" // for type128
17 #include "llvm/ADT/APFloat.h"
18 #include "llvm/ADT/APInt.h"
20 #include <stddef.h> // for size_t
21 #include <stdint.h> // for uint32_t, uint64_t, int64_t
23 namespace lldb_private {
26 namespace lldb_private {
30 #define NUM_OF_WORDS_INT128 2
31 #define BITWIDTH_INT128 128
32 #define NUM_OF_WORDS_INT256 4
33 #define BITWIDTH_INT256 256
35 namespace lldb_private {
37 //----------------------------------------------------------------------
38 // A class designed to hold onto values and their corresponding types.
39 // Operators are defined and Scalar objects will correctly promote
40 // their types and values before performing these operations. Type
41 // promotion currently follows the ANSI C type promotion rules.
42 //----------------------------------------------------------------------
62 //------------------------------------------------------------------
63 // Constructors and Destructors
64 //------------------------------------------------------------------
66 Scalar(int v) : m_type(e_sint), m_float((float)0) {
67 m_integer = llvm::APInt(sizeof(int) * 8, v, true);
69 Scalar(unsigned int v) : m_type(e_uint), m_float((float)0) {
70 m_integer = llvm::APInt(sizeof(int) * 8, v);
72 Scalar(long v) : m_type(e_slong), m_float((float)0) {
73 m_integer = llvm::APInt(sizeof(long) * 8, v, true);
75 Scalar(unsigned long v) : m_type(e_ulong), m_float((float)0) {
76 m_integer = llvm::APInt(sizeof(long) * 8, v);
78 Scalar(long long v) : m_type(e_slonglong), m_float((float)0) {
79 m_integer = llvm::APInt(sizeof(long long) * 8, v, true);
81 Scalar(unsigned long long v) : m_type(e_ulonglong), m_float((float)0) {
82 m_integer = llvm::APInt(sizeof(long long) * 8, v);
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);
88 Scalar(long double v, bool ieee_quad)
89 : m_type(e_long_double), m_float((float)0), m_ieee_quad(ieee_quad) {
91 m_float = llvm::APFloat(llvm::APFloat::IEEEquad(),
92 llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
95 m_float = llvm::APFloat(llvm::APFloat::x87DoubleExtended(),
96 llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
99 Scalar(llvm::APInt v) : m_type(), m_float((float)0) {
100 m_integer = llvm::APInt(v);
101 switch (m_integer.getBitWidth()) {
105 if (m_integer.isSignedIntN(sizeof(sint_t) * 8))
111 if (m_integer.isSignedIntN(sizeof(slonglong_t) * 8))
112 m_type = e_slonglong;
114 m_type = e_ulonglong;
117 if (m_integer.isSignedIntN(BITWIDTH_INT128))
123 if (m_integer.isSignedIntN(BITWIDTH_INT256))
130 Scalar(const Scalar &rhs);
131 // Scalar(const RegisterValue& reg_value);
134 bool SignExtend(uint32_t bit_pos);
136 bool ExtractBitfield(uint32_t bit_size, uint32_t bit_offset);
138 bool SetBit(uint32_t bit);
140 bool ClearBit(uint32_t bit);
142 const void *GetBytes() const;
144 size_t GetByteSize() const;
146 bool GetData(DataExtractor &data, size_t limit_byte_size = UINT32_MAX) const;
148 size_t GetAsMemoryData(void *dst, size_t dst_len,
149 lldb::ByteOrder dst_byte_order, Error &error) const;
155 m_integer.clearAllBits();
158 const char *GetTypeAsCString() const;
160 void GetValue(Stream *s, bool show_type) const;
162 bool IsValid() const {
163 return (m_type >= e_sint) && (m_type <= e_long_double);
166 bool Promote(Scalar::Type type);
168 bool Cast(Scalar::Type type);
174 static const char *GetValueTypeAsCString(Scalar::Type value_type);
177 GetValueTypeForSignedIntegerWithByteSize(size_t byte_size);
180 GetValueTypeForUnsignedIntegerWithByteSize(size_t byte_size);
182 static Scalar::Type GetValueTypeForFloatWithByteSize(size_t byte_size);
184 //----------------------------------------------------------------------
185 // All operators can benefits from the implicit conversions that will
186 // happen automagically by the compiler, so no temporary objects will
187 // need to be created. As a result, we currently don't need a variety of
188 // overloaded set value accessors.
189 //----------------------------------------------------------------------
190 Scalar &operator=(const int i);
191 Scalar &operator=(unsigned int v);
192 Scalar &operator=(long v);
193 Scalar &operator=(unsigned long v);
194 Scalar &operator=(long long v);
195 Scalar &operator=(unsigned long long v);
196 Scalar &operator=(float v);
197 Scalar &operator=(double v);
198 Scalar &operator=(long double v);
199 Scalar &operator=(llvm::APInt v);
200 Scalar &operator=(const Scalar &rhs); // Assignment operator
201 Scalar &operator+=(const Scalar &rhs);
202 Scalar &operator<<=(const Scalar &rhs); // Shift left
203 Scalar &operator>>=(const Scalar &rhs); // Shift right (arithmetic)
204 Scalar &operator&=(const Scalar &rhs);
206 //----------------------------------------------------------------------
207 // Shifts the current value to the right without maintaining the current
208 // sign of the value (if it is signed).
209 //----------------------------------------------------------------------
210 bool ShiftRightLogical(const Scalar &rhs); // Returns true on success
212 //----------------------------------------------------------------------
213 // Takes the absolute value of the current value if it is signed, else
214 // the value remains unchanged.
215 // Returns false if the contained value has a void type.
216 //----------------------------------------------------------------------
217 bool AbsoluteValue(); // Returns true on success
218 //----------------------------------------------------------------------
219 // Negates the current value (even for unsigned values).
220 // Returns false if the contained value has a void type.
221 //----------------------------------------------------------------------
222 bool UnaryNegate(); // Returns true on success
223 //----------------------------------------------------------------------
224 // Inverts all bits in the current value as long as it isn't void or
225 // a float/double/long double type.
226 // Returns false if the contained value has a void/float/double/long
227 // double type, else the value is inverted and true is returned.
228 //----------------------------------------------------------------------
229 bool OnesComplement(); // Returns true on success
231 //----------------------------------------------------------------------
232 // Access the type of the current value.
233 //----------------------------------------------------------------------
234 Scalar::Type GetType() const { return m_type; }
236 //----------------------------------------------------------------------
237 // Returns a casted value of the current contained data without
238 // modifying the current value. FAIL_VALUE will be returned if the type
239 // of the value is void or invalid.
240 //----------------------------------------------------------------------
241 int SInt(int fail_value = 0) const;
243 unsigned char UChar(unsigned char fail_value = 0) const;
245 signed char SChar(char fail_value = 0) const;
247 unsigned short UShort(unsigned short fail_value = 0) const;
249 short SShort(short fail_value = 0) const;
251 unsigned int UInt(unsigned int fail_value = 0) const;
253 long SLong(long fail_value = 0) const;
255 unsigned long ULong(unsigned long fail_value = 0) const;
257 long long SLongLong(long long fail_value = 0) const;
259 unsigned long long ULongLong(unsigned long long fail_value = 0) const;
261 llvm::APInt SInt128(llvm::APInt &fail_value) const;
263 llvm::APInt UInt128(const llvm::APInt &fail_value) const;
265 llvm::APInt SInt256(llvm::APInt &fail_value) const;
267 llvm::APInt UInt256(const llvm::APInt &fail_value) const;
269 float Float(float fail_value = 0.0f) const;
271 double Double(double fail_value = 0.0) const;
273 long double LongDouble(long double fail_value = 0.0) const;
275 Error SetValueFromCString(const char *s, lldb::Encoding encoding,
278 Error SetValueFromData(DataExtractor &data, lldb::Encoding encoding,
281 static bool UIntValueIsValidForSize(uint64_t uval64, size_t total_byte_size) {
282 if (total_byte_size > 8)
285 if (total_byte_size == 8)
288 const uint64_t max = ((uint64_t)1 << (uint64_t)(total_byte_size * 8)) - 1;
289 return uval64 <= max;
292 static bool SIntValueIsValidForSize(int64_t sval64, size_t total_byte_size) {
293 if (total_byte_size > 8)
296 if (total_byte_size == 8)
299 const int64_t max = ((int64_t)1 << (uint64_t)(total_byte_size * 8 - 1)) - 1;
300 const int64_t min = ~(max);
301 return min <= sval64 && sval64 <= max;
305 typedef char schar_t;
306 typedef unsigned char uchar_t;
307 typedef short sshort_t;
308 typedef unsigned short ushort_t;
310 typedef unsigned int uint_t;
311 typedef long slong_t;
312 typedef unsigned long ulong_t;
313 typedef long long slonglong_t;
314 typedef unsigned long long ulonglong_t;
315 typedef float float_t;
316 typedef double double_t;
317 typedef long double long_double_t;
319 //------------------------------------------------------------------
320 // Classes that inherit from Scalar can see and modify these
321 //------------------------------------------------------------------
323 llvm::APInt m_integer;
324 llvm::APFloat m_float;
325 bool m_ieee_quad = false;
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 const Scalar operator<<(const Scalar &lhs, const Scalar &rhs);
337 friend const Scalar 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 friend bool operator>(const Scalar &lhs, const Scalar &rhs);
343 friend bool operator>=(const Scalar &lhs, const Scalar &rhs);
346 //----------------------------------------------------------------------
347 // Split out the operators into a format where the compiler will be able
348 // to implicitly convert numbers into Scalar objects.
350 // This allows code like:
352 // Scalar four = two * 2;
353 // Scalar eight = 2 * four; // This would cause an error if the
354 // // operator* was implemented as a
355 // // member function.
357 // Item 19 of "Effective C++ Second Edition" by Scott Meyers
358 // Differentiate among members functions, non-member functions, and
360 //----------------------------------------------------------------------
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 const Scalar operator<<(const Scalar &lhs, const Scalar &rhs);
370 const Scalar 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 bool operator>(const Scalar &lhs, const Scalar &rhs);
376 bool operator>=(const Scalar &lhs, const Scalar &rhs);
378 } // namespace lldb_private
380 #endif // liblldb_Scalar_h_