1 //===-- Scalar.h ------------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLDB_UTILITY_SCALAR_H
10 #define LLDB_UTILITY_SCALAR_H
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"
21 namespace lldb_private {
24 } // namespace lldb_private
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
33 namespace lldb_private {
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.
60 // Constructors and Destructors
62 Scalar(int v) : m_type(e_sint), m_float(static_cast<float>(0)) {
63 m_integer = llvm::APInt(sizeof(int) * 8, v, true);
65 Scalar(unsigned int v) : m_type(e_uint), m_float(static_cast<float>(0)) {
66 m_integer = llvm::APInt(sizeof(int) * 8, v);
68 Scalar(long v) : m_type(e_slong), m_float(static_cast<float>(0)) {
69 m_integer = llvm::APInt(sizeof(long) * 8, v, true);
71 Scalar(unsigned long v) : m_type(e_ulong), m_float(static_cast<float>(0)) {
72 m_integer = llvm::APInt(sizeof(long) * 8, v);
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);
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);
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);
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) {
90 llvm::APFloat(llvm::APFloat::IEEEquad(),
91 llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
92 (reinterpret_cast<type128 *>(&v))->x));
95 llvm::APFloat(llvm::APFloat::x87DoubleExtended(),
96 llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
97 (reinterpret_cast<type128 *>(&v))->x));
99 Scalar(llvm::APInt v) : m_type(), m_float(static_cast<float>(0)) {
100 m_integer = llvm::APInt(v);
101 switch (m_integer.getBitWidth()) {
108 m_type = e_slonglong;
120 lldbassert(false && "unsupported bitwidth");
122 // Scalar(const RegisterValue& reg_value);
125 bool SignExtend(uint32_t bit_pos);
127 bool ExtractBitfield(uint32_t bit_size, uint32_t bit_offset);
129 bool SetBit(uint32_t bit);
131 bool ClearBit(uint32_t bit);
133 const void *GetBytes() const;
135 size_t GetByteSize() const;
137 bool GetData(DataExtractor &data, size_t limit_byte_size = UINT32_MAX) const;
139 size_t GetAsMemoryData(void *dst, size_t dst_len,
140 lldb::ByteOrder dst_byte_order, Status &error) const;
146 m_integer.clearAllBits();
149 const char *GetTypeAsCString() const;
151 void GetValue(Stream *s, bool show_type) const;
153 bool IsValid() const {
154 return (m_type >= e_sint) && (m_type <= e_long_double);
157 bool Promote(Scalar::Type type);
163 static const char *GetValueTypeAsCString(Scalar::Type value_type);
166 GetValueTypeForSignedIntegerWithByteSize(size_t byte_size);
169 GetValueTypeForUnsignedIntegerWithByteSize(size_t byte_size);
171 static Scalar::Type GetValueTypeForFloatWithByteSize(size_t byte_size);
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
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);
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
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
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
208 bool OnesComplement(); // Returns true on success
210 // Access the type of the current value.
211 Scalar::Type GetType() const { return m_type; }
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
216 int SInt(int fail_value = 0) const;
218 unsigned char UChar(unsigned char fail_value = 0) const;
220 signed char SChar(char fail_value = 0) const;
222 unsigned short UShort(unsigned short fail_value = 0) const;
224 short SShort(short fail_value = 0) const;
226 unsigned int UInt(unsigned int fail_value = 0) const;
228 long SLong(long fail_value = 0) const;
230 unsigned long ULong(unsigned long fail_value = 0) const;
232 long long SLongLong(long long fail_value = 0) const;
234 unsigned long long ULongLong(unsigned long long fail_value = 0) const;
236 llvm::APInt SInt128(llvm::APInt &fail_value) const;
238 llvm::APInt UInt128(const llvm::APInt &fail_value) const;
240 float Float(float fail_value = 0.0f) const;
242 double Double(double fail_value = 0.0) const;
244 long double LongDouble(long double fail_value = 0.0) const;
246 Status SetValueFromCString(const char *s, lldb::Encoding encoding,
249 Status SetValueFromData(DataExtractor &data, lldb::Encoding encoding,
252 static bool UIntValueIsValidForSize(uint64_t uval64, size_t total_byte_size) {
253 if (total_byte_size > 8)
256 if (total_byte_size == 8)
259 const uint64_t max = (static_cast<uint64_t>(1)
260 << static_cast<uint64_t>(total_byte_size * 8)) -
262 return uval64 <= max;
265 static bool SIntValueIsValidForSize(int64_t sval64, size_t total_byte_size) {
266 if (total_byte_size > 8)
269 if (total_byte_size == 8)
272 const int64_t max = (static_cast<int64_t>(1)
273 << static_cast<uint64_t>(total_byte_size * 8 - 1)) -
275 const int64_t min = ~(max);
276 return min <= sval64 && sval64 <= max;
280 typedef char schar_t;
281 typedef unsigned char uchar_t;
282 typedef short sshort_t;
283 typedef unsigned short ushort_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;
294 // Classes that inherit from Scalar can see and modify these
296 llvm::APInt m_integer;
297 llvm::APFloat m_float;
298 bool m_ieee_quad = false;
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);
319 // Split out the operators into a format where the compiler will be able to
320 // implicitly convert numbers into Scalar objects.
322 // This allows code like:
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.
329 // Item 19 of "Effective C++ Second Edition" by Scott Meyers
330 // Differentiate among members functions, non-member functions, and
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);
349 llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const Scalar &scalar);
351 } // namespace lldb_private
353 #endif // LLDB_UTILITY_SCALAR_H