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/Status.h" // for Status
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 their types
40 // and values before performing these operations. Type promotion currently
41 // 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, Status &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);
172 static const char *GetValueTypeAsCString(Scalar::Type value_type);
175 GetValueTypeForSignedIntegerWithByteSize(size_t byte_size);
178 GetValueTypeForUnsignedIntegerWithByteSize(size_t byte_size);
180 static Scalar::Type GetValueTypeForFloatWithByteSize(size_t byte_size);
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
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);
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
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
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
226 //----------------------------------------------------------------------
227 bool OnesComplement(); // Returns true on success
229 //----------------------------------------------------------------------
230 // Access the type of the current value.
231 //----------------------------------------------------------------------
232 Scalar::Type GetType() const { return m_type; }
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
238 //----------------------------------------------------------------------
239 int SInt(int fail_value = 0) const;
241 unsigned char UChar(unsigned char fail_value = 0) const;
243 signed char SChar(char fail_value = 0) const;
245 unsigned short UShort(unsigned short fail_value = 0) const;
247 short SShort(short fail_value = 0) const;
249 unsigned int UInt(unsigned int fail_value = 0) const;
251 long SLong(long fail_value = 0) const;
253 unsigned long ULong(unsigned long fail_value = 0) const;
255 long long SLongLong(long long fail_value = 0) const;
257 unsigned long long ULongLong(unsigned long long fail_value = 0) const;
259 llvm::APInt SInt128(llvm::APInt &fail_value) const;
261 llvm::APInt UInt128(const llvm::APInt &fail_value) const;
263 llvm::APInt SInt256(llvm::APInt &fail_value) const;
265 llvm::APInt UInt256(const llvm::APInt &fail_value) const;
267 float Float(float fail_value = 0.0f) const;
269 double Double(double fail_value = 0.0) const;
271 long double LongDouble(long double fail_value = 0.0) const;
273 Status SetValueFromCString(const char *s, lldb::Encoding encoding,
276 Status SetValueFromData(DataExtractor &data, lldb::Encoding encoding,
279 static bool UIntValueIsValidForSize(uint64_t uval64, size_t total_byte_size) {
280 if (total_byte_size > 8)
283 if (total_byte_size == 8)
286 const uint64_t max = ((uint64_t)1 << (uint64_t)(total_byte_size * 8)) - 1;
287 return uval64 <= max;
290 static bool SIntValueIsValidForSize(int64_t sval64, size_t total_byte_size) {
291 if (total_byte_size > 8)
294 if (total_byte_size == 8)
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;
303 typedef char schar_t;
304 typedef unsigned char uchar_t;
305 typedef short sshort_t;
306 typedef unsigned short ushort_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;
317 //------------------------------------------------------------------
318 // Classes that inherit from Scalar can see and modify these
319 //------------------------------------------------------------------
321 llvm::APInt m_integer;
322 llvm::APFloat m_float;
323 bool m_ieee_quad = false;
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);
344 //----------------------------------------------------------------------
345 // Split out the operators into a format where the compiler will be able to
346 // implicitly convert numbers into Scalar objects.
348 // This allows code like:
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.
355 // Item 19 of "Effective C++ Second Edition" by Scott Meyers
356 // Differentiate among members functions, non-member functions, and
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);
376 } // namespace lldb_private
378 #endif // liblldb_Scalar_h_