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/lldb-private.h"
14 #include "llvm/ADT/APInt.h"
15 #include "llvm/ADT/APFloat.h"
17 #define NUM_OF_WORDS_INT128 2
18 #define BITWIDTH_INT128 128
20 namespace lldb_private {
22 //----------------------------------------------------------------------
23 // A class designed to hold onto values and their corresponding types.
24 // Operators are defined and Scalar objects will correctly promote
25 // their types and values before performing these operations. Type
26 // promotion currently follows the ANSI C type promotion rules.
27 //----------------------------------------------------------------------
47 //------------------------------------------------------------------
48 // Constructors and Destructors
49 //------------------------------------------------------------------
51 Scalar(int v) : m_type(e_sint), m_float((float)0) { m_integer = llvm::APInt(sizeof(int) * 8, v, true);}
52 Scalar(unsigned int v) : m_type(e_uint), m_float((float)0) { m_integer = llvm::APInt(sizeof(int) * 8, v);}
53 Scalar(long v) : m_type(e_slong), m_float((float)0) { m_integer = llvm::APInt(sizeof(long) * 8, v, true);}
54 Scalar(unsigned long v) : m_type(e_ulong), m_float((float)0) { m_integer = llvm::APInt(sizeof(long) * 8, v);}
55 Scalar(long long v) : m_type(e_slonglong), m_float((float)0) { m_integer = llvm::APInt(sizeof(long long) * 8, v, true);}
56 Scalar(unsigned long long v): m_type(e_ulonglong), m_float((float)0) { m_integer = llvm::APInt(sizeof(long long) * 8, v);}
57 Scalar(float v) : m_type(e_float), m_float(v) { m_float = llvm::APFloat(v); }
58 Scalar(double v) : m_type(e_double), m_float(v) { m_float = llvm::APFloat(v); }
59 Scalar(long double v, bool ieee_quad)
60 : m_type(e_long_double), m_float((float)0), m_ieee_quad(ieee_quad)
63 m_float = llvm::APFloat(llvm::APFloat::IEEEquad, llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128, ((type128 *)&v)->x));
65 m_float = llvm::APFloat(llvm::APFloat::x87DoubleExtended, llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128, ((type128 *)&v)->x));
67 Scalar(llvm::APInt v) :
71 m_integer = llvm::APInt(v);
72 switch(m_integer.getBitWidth())
77 if(m_integer.isSignedIntN(sizeof(sint_t) * 8))
83 if(m_integer.isSignedIntN(sizeof(slonglong_t) * 8))
89 if(m_integer.isSignedIntN(BITWIDTH_INT128))
96 Scalar(const Scalar& rhs);
97 //Scalar(const RegisterValue& reg_value);
101 SignExtend (uint32_t bit_pos);
104 ExtractBitfield (uint32_t bit_size,
105 uint32_t bit_offset);
108 SetBit(uint32_t bit);
111 ClearBit(uint32_t bit);
120 GetData (DataExtractor &data, size_t limit_byte_size = UINT32_MAX) const;
123 GetAsMemoryData (void *dst,
125 lldb::ByteOrder dst_byte_order,
132 Clear() { m_type = e_void; m_integer.clearAllBits(); }
135 GetTypeAsCString() const;
138 GetValue (Stream *s, bool show_type) const;
143 return (m_type >= e_sint) && (m_type <= e_long_double);
147 Promote(Scalar::Type type);
150 Cast (Scalar::Type type);
156 GetValueTypeAsCString (Scalar::Type value_type);
159 GetValueTypeForSignedIntegerWithByteSize (size_t byte_size);
162 GetValueTypeForUnsignedIntegerWithByteSize (size_t byte_size);
165 GetValueTypeForFloatWithByteSize (size_t byte_size);
167 //----------------------------------------------------------------------
168 // All operators can benefits from the implicit conversions that will
169 // happen automagically by the compiler, so no temporary objects will
170 // need to be created. As a result, we currently don't need a variety of
171 // overloaded set value accessors.
172 //----------------------------------------------------------------------
173 Scalar& operator= (const int i);
174 Scalar& operator= (unsigned int v);
175 Scalar& operator= (long v);
176 Scalar& operator= (unsigned long v);
177 Scalar& operator= (long long v);
178 Scalar& operator= (unsigned long long v);
179 Scalar& operator= (float v);
180 Scalar& operator= (double v);
181 Scalar& operator= (long double v);
182 Scalar& operator= (llvm::APInt v);
183 Scalar& operator= (const Scalar& rhs); // Assignment operator
184 Scalar& operator+= (const Scalar& rhs);
185 Scalar& operator<<= (const Scalar& rhs); // Shift left
186 Scalar& operator>>= (const Scalar& rhs); // Shift right (arithmetic)
187 Scalar& operator&= (const Scalar& rhs);
189 //----------------------------------------------------------------------
190 // Shifts the current value to the right without maintaining the current
191 // sign of the value (if it is signed).
192 //----------------------------------------------------------------------
194 ShiftRightLogical(const Scalar& rhs); // Returns true on success
196 //----------------------------------------------------------------------
197 // Takes the absolute value of the current value if it is signed, else
198 // the value remains unchanged.
199 // Returns false if the contained value has a void type.
200 //----------------------------------------------------------------------
202 AbsoluteValue(); // Returns true on success
203 //----------------------------------------------------------------------
204 // Negates the current value (even for unsigned values).
205 // Returns false if the contained value has a void type.
206 //----------------------------------------------------------------------
208 UnaryNegate(); // Returns true on success
209 //----------------------------------------------------------------------
210 // Inverts all bits in the current value as long as it isn't void or
211 // a float/double/long double type.
212 // Returns false if the contained value has a void/float/double/long
213 // double type, else the value is inverted and true is returned.
214 //----------------------------------------------------------------------
216 OnesComplement(); // Returns true on success
218 //----------------------------------------------------------------------
219 // Access the type of the current value.
220 //----------------------------------------------------------------------
222 GetType() const { return m_type; }
225 SetType(const RegisterInfo*);
227 //----------------------------------------------------------------------
228 // Returns a casted value of the current contained data without
229 // modifying the current value. FAIL_VALUE will be returned if the type
230 // of the value is void or invalid.
231 //----------------------------------------------------------------------
233 SInt(int fail_value = 0) const;
235 // Return the raw unsigned integer without any casting or conversion
239 // Return the raw unsigned long without any casting or conversion
243 // Return the raw unsigned long long without any casting or conversion
245 RawULongLong () const;
248 UChar(unsigned char fail_value = 0) const;
251 SChar(char fail_value = 0) const;
254 UShort(unsigned short fail_value = 0) const;
257 SShort(short fail_value = 0) const;
260 UInt(unsigned int fail_value = 0) const;
263 SLong(long fail_value = 0) const;
266 ULong(unsigned long fail_value = 0) const;
269 SLongLong(long long fail_value = 0) const;
272 ULongLong(unsigned long long fail_value = 0) const;
275 SInt128(llvm::APInt& fail_value) const;
278 UInt128(const llvm::APInt& fail_value) const;
281 Float(float fail_value = 0.0f) const;
284 Double(double fail_value = 0.0) const;
287 LongDouble(long double fail_value = 0.0) const;
290 GetRawBits64 (uint64_t fail_value) const;
293 SetValueFromCString (const char *s, lldb::Encoding encoding, size_t byte_size);
296 SetValueFromData (DataExtractor &data, lldb::Encoding encoding, size_t byte_size);
299 UIntValueIsValidForSize (uint64_t uval64, size_t total_byte_size)
301 if (total_byte_size > 8)
304 if (total_byte_size == 8)
307 const uint64_t max = ((uint64_t)1 << (uint64_t)(total_byte_size * 8)) - 1;
308 return uval64 <= max;
312 SIntValueIsValidForSize (int64_t sval64, size_t total_byte_size)
314 if (total_byte_size > 8)
317 if (total_byte_size == 8)
320 const int64_t max = ((int64_t)1 << (uint64_t)(total_byte_size * 8 - 1)) - 1;
321 const int64_t min = ~(max);
322 return min <= sval64 && sval64 <= max;
326 typedef char schar_t;
327 typedef unsigned char uchar_t;
328 typedef short sshort_t;
329 typedef unsigned short ushort_t;
331 typedef unsigned int uint_t;
332 typedef long slong_t;
333 typedef unsigned long ulong_t;
334 typedef long long slonglong_t;
335 typedef unsigned long long ulonglong_t;
336 typedef float float_t;
337 typedef double double_t;
338 typedef long double long_double_t;
340 //------------------------------------------------------------------
341 // Classes that inherit from Scalar can see and modify these
342 //------------------------------------------------------------------
344 llvm::APInt m_integer;
345 llvm::APFloat m_float;
346 bool m_ieee_quad = false;
349 friend const Scalar operator+ (const Scalar& lhs, const Scalar& rhs);
350 friend const Scalar operator- (const Scalar& lhs, const Scalar& rhs);
351 friend const Scalar operator/ (const Scalar& lhs, const Scalar& rhs);
352 friend const Scalar operator* (const Scalar& lhs, const Scalar& rhs);
353 friend const Scalar operator& (const Scalar& lhs, const Scalar& rhs);
354 friend const Scalar operator| (const Scalar& lhs, const Scalar& rhs);
355 friend const Scalar operator% (const Scalar& lhs, const Scalar& rhs);
356 friend const Scalar operator^ (const Scalar& lhs, const Scalar& rhs);
357 friend const Scalar operator<< (const Scalar& lhs, const Scalar& rhs);
358 friend const Scalar operator>> (const Scalar& lhs, const Scalar& rhs);
359 friend bool operator== (const Scalar& lhs, const Scalar& rhs);
360 friend bool operator!= (const Scalar& lhs, const Scalar& rhs);
361 friend bool operator< (const Scalar& lhs, const Scalar& rhs);
362 friend bool operator<= (const Scalar& lhs, const Scalar& rhs);
363 friend bool operator> (const Scalar& lhs, const Scalar& rhs);
364 friend bool operator>= (const Scalar& lhs, const Scalar& rhs);
368 //----------------------------------------------------------------------
369 // Split out the operators into a format where the compiler will be able
370 // to implicitly convert numbers into Scalar objects.
372 // This allows code like:
374 // Scalar four = two * 2;
375 // Scalar eight = 2 * four; // This would cause an error if the
376 // // operator* was implemented as a
377 // // member function.
379 // Item 19 of "Effective C++ Second Edition" by Scott Meyers
380 // Differentiate among members functions, non-member functions, and
382 //----------------------------------------------------------------------
383 const Scalar operator+ (const Scalar& lhs, const Scalar& rhs);
384 const Scalar operator- (const Scalar& lhs, const Scalar& rhs);
385 const Scalar operator/ (const Scalar& lhs, const Scalar& rhs);
386 const Scalar operator* (const Scalar& lhs, const Scalar& rhs);
387 const Scalar operator& (const Scalar& lhs, const Scalar& rhs);
388 const Scalar operator| (const Scalar& lhs, const Scalar& rhs);
389 const Scalar operator% (const Scalar& lhs, const Scalar& rhs);
390 const Scalar operator^ (const Scalar& lhs, const Scalar& rhs);
391 const Scalar operator<< (const Scalar& lhs, const Scalar& rhs);
392 const Scalar operator>> (const Scalar& lhs, const Scalar& rhs);
393 bool operator== (const Scalar& lhs, const Scalar& rhs);
394 bool operator!= (const Scalar& lhs, const Scalar& rhs);
395 bool operator< (const Scalar& lhs, const Scalar& rhs);
396 bool operator<= (const Scalar& lhs, const Scalar& rhs);
397 bool operator> (const Scalar& lhs, const Scalar& rhs);
398 bool operator>= (const Scalar& lhs, const Scalar& rhs);
400 } // namespace lldb_private
402 #endif // liblldb_Scalar_h_