summaryrefslogtreecommitdiff
path: root/include/lldb/Utility/Scalar.h
blob: c5b46dd605527c688c9cbffbb5a54b544b361479 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//===-- Scalar.h ------------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLDB_UTILITY_SCALAR_H
#define LLDB_UTILITY_SCALAR_H

#include "lldb/Utility/Status.h"
#include "lldb/lldb-enumerations.h"
#include "lldb/lldb-private-types.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/APInt.h"
#include <cstddef>
#include <cstdint>

namespace lldb_private {
class DataExtractor;
class Stream;
} // namespace lldb_private

#define NUM_OF_WORDS_INT128 2
#define BITWIDTH_INT128 128
#define NUM_OF_WORDS_INT256 4
#define BITWIDTH_INT256 256

namespace lldb_private {

//----------------------------------------------------------------------
// A class designed to hold onto values and their corresponding types.
// Operators are defined and Scalar objects will correctly promote their types
// and values before performing these operations. Type promotion currently
// follows the ANSI C type promotion rules.
//----------------------------------------------------------------------
class Scalar {
public:
  enum Type {
    e_void = 0,
    e_sint,
    e_uint,
    e_slong,
    e_ulong,
    e_slonglong,
    e_ulonglong,
    e_sint128,
    e_uint128,
    e_sint256,
    e_uint256,
    e_float,
    e_double,
    e_long_double
  };

  //------------------------------------------------------------------
  // Constructors and Destructors
  //------------------------------------------------------------------
  Scalar();
  Scalar(int v) : m_type(e_sint), m_float((float)0) {
    m_integer = llvm::APInt(sizeof(int) * 8, v, true);
  }
  Scalar(unsigned int v) : m_type(e_uint), m_float((float)0) {
    m_integer = llvm::APInt(sizeof(int) * 8, v);
  }
  Scalar(long v) : m_type(e_slong), m_float((float)0) {
    m_integer = llvm::APInt(sizeof(long) * 8, v, true);
  }
  Scalar(unsigned long v) : m_type(e_ulong), m_float((float)0) {
    m_integer = llvm::APInt(sizeof(long) * 8, v);
  }
  Scalar(long long v) : m_type(e_slonglong), m_float((float)0) {
    m_integer = llvm::APInt(sizeof(long long) * 8, v, true);
  }
  Scalar(unsigned long long v) : m_type(e_ulonglong), m_float((float)0) {
    m_integer = llvm::APInt(sizeof(long long) * 8, v);
  }
  Scalar(float v) : m_type(e_float), m_float(v) { m_float = llvm::APFloat(v); }
  Scalar(double v) : m_type(e_double), m_float(v) {
    m_float = llvm::APFloat(v);
  }
  Scalar(long double v, bool ieee_quad)
      : m_type(e_long_double), m_float((float)0), m_ieee_quad(ieee_quad) {
    if (ieee_quad)
      m_float = llvm::APFloat(llvm::APFloat::IEEEquad(),
                              llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
                                          ((type128 *)&v)->x));
    else
      m_float = llvm::APFloat(llvm::APFloat::x87DoubleExtended(),
                              llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
                                          ((type128 *)&v)->x));
  }
  Scalar(llvm::APInt v) : m_type(), m_float((float)0) {
    m_integer = llvm::APInt(v);
    switch (m_integer.getBitWidth()) {
    case 8:
    case 16:
    case 32:
      if (m_integer.isSignedIntN(sizeof(sint_t) * 8))
        m_type = e_sint;
      else
        m_type = e_uint;
      break;
    case 64:
      if (m_integer.isSignedIntN(sizeof(slonglong_t) * 8))
        m_type = e_slonglong;
      else
        m_type = e_ulonglong;
      break;
    case 128:
      if (m_integer.isSignedIntN(BITWIDTH_INT128))
        m_type = e_sint128;
      else
        m_type = e_uint128;
      break;
    case 256:
      if (m_integer.isSignedIntN(BITWIDTH_INT256))
        m_type = e_sint256;
      else
        m_type = e_uint256;
      break;
    }
  }
  Scalar(const Scalar &rhs);
  // Scalar(const RegisterValue& reg_value);
  virtual ~Scalar();

  bool SignExtend(uint32_t bit_pos);

  bool ExtractBitfield(uint32_t bit_size, uint32_t bit_offset);

  bool SetBit(uint32_t bit);

  bool ClearBit(uint32_t bit);

  const void *GetBytes() const;

  size_t GetByteSize() const;

  bool GetData(DataExtractor &data, size_t limit_byte_size = UINT32_MAX) const;

  size_t GetAsMemoryData(void *dst, size_t dst_len,
                         lldb::ByteOrder dst_byte_order, Status &error) const;

  bool IsZero() const;

  void Clear() {
    m_type = e_void;
    m_integer.clearAllBits();
  }

  const char *GetTypeAsCString() const;

  void GetValue(Stream *s, bool show_type) const;

  bool IsValid() const {
    return (m_type >= e_sint) && (m_type <= e_long_double);
  }

  bool Promote(Scalar::Type type);

  bool MakeSigned();

  bool MakeUnsigned();

  static const char *GetValueTypeAsCString(Scalar::Type value_type);

  static Scalar::Type
  GetValueTypeForSignedIntegerWithByteSize(size_t byte_size);

  static Scalar::Type
  GetValueTypeForUnsignedIntegerWithByteSize(size_t byte_size);

  static Scalar::Type GetValueTypeForFloatWithByteSize(size_t byte_size);

  //----------------------------------------------------------------------
  // All operators can benefits from the implicit conversions that will happen
  // automagically by the compiler, so no temporary objects will need to be
  // created. As a result, we currently don't need a variety of overloaded set
  // value accessors.
  //----------------------------------------------------------------------
  Scalar &operator=(const int i);
  Scalar &operator=(unsigned int v);
  Scalar &operator=(long v);
  Scalar &operator=(unsigned long v);
  Scalar &operator=(long long v);
  Scalar &operator=(unsigned long long v);
  Scalar &operator=(float v);
  Scalar &operator=(double v);
  Scalar &operator=(long double v);
  Scalar &operator=(llvm::APInt v);
  Scalar &operator=(const Scalar &rhs); // Assignment operator
  Scalar &operator+=(const Scalar &rhs);
  Scalar &operator<<=(const Scalar &rhs); // Shift left
  Scalar &operator>>=(const Scalar &rhs); // Shift right (arithmetic)
  Scalar &operator&=(const Scalar &rhs);

  //----------------------------------------------------------------------
  // Shifts the current value to the right without maintaining the current sign
  // of the value (if it is signed).
  //----------------------------------------------------------------------
  bool ShiftRightLogical(const Scalar &rhs); // Returns true on success

  //----------------------------------------------------------------------
  // Takes the absolute value of the current value if it is signed, else the
  // value remains unchanged. Returns false if the contained value has a void
  // type.
  //----------------------------------------------------------------------
  bool AbsoluteValue(); // Returns true on success
  //----------------------------------------------------------------------
  // Negates the current value (even for unsigned values). Returns false if the
  // contained value has a void type.
  //----------------------------------------------------------------------
  bool UnaryNegate(); // Returns true on success
  //----------------------------------------------------------------------
  // Inverts all bits in the current value as long as it isn't void or a
  // float/double/long double type. Returns false if the contained value has a
  // void/float/double/long double type, else the value is inverted and true is
  // returned.
  //----------------------------------------------------------------------
  bool OnesComplement(); // Returns true on success

  //----------------------------------------------------------------------
  // Access the type of the current value.
  //----------------------------------------------------------------------
  Scalar::Type GetType() const { return m_type; }

  //----------------------------------------------------------------------
  // Returns a casted value of the current contained data without modifying the
  // current value. FAIL_VALUE will be returned if the type of the value is
  // void or invalid.
  //----------------------------------------------------------------------
  int SInt(int fail_value = 0) const;

  unsigned char UChar(unsigned char fail_value = 0) const;

  signed char SChar(char fail_value = 0) const;

  unsigned short UShort(unsigned short fail_value = 0) const;

  short SShort(short fail_value = 0) const;

  unsigned int UInt(unsigned int fail_value = 0) const;

  long SLong(long fail_value = 0) const;

  unsigned long ULong(unsigned long fail_value = 0) const;

  long long SLongLong(long long fail_value = 0) const;

  unsigned long long ULongLong(unsigned long long fail_value = 0) const;

  llvm::APInt SInt128(llvm::APInt &fail_value) const;

  llvm::APInt UInt128(const llvm::APInt &fail_value) const;

  llvm::APInt SInt256(llvm::APInt &fail_value) const;

  llvm::APInt UInt256(const llvm::APInt &fail_value) const;

  float Float(float fail_value = 0.0f) const;

  double Double(double fail_value = 0.0) const;

  long double LongDouble(long double fail_value = 0.0) const;

  Status SetValueFromCString(const char *s, lldb::Encoding encoding,
                             size_t byte_size);

  Status SetValueFromData(DataExtractor &data, lldb::Encoding encoding,
                          size_t byte_size);

  static bool UIntValueIsValidForSize(uint64_t uval64, size_t total_byte_size) {
    if (total_byte_size > 8)
      return false;

    if (total_byte_size == 8)
      return true;

    const uint64_t max = ((uint64_t)1 << (uint64_t)(total_byte_size * 8)) - 1;
    return uval64 <= max;
  }

  static bool SIntValueIsValidForSize(int64_t sval64, size_t total_byte_size) {
    if (total_byte_size > 8)
      return false;

    if (total_byte_size == 8)
      return true;

    const int64_t max = ((int64_t)1 << (uint64_t)(total_byte_size * 8 - 1)) - 1;
    const int64_t min = ~(max);
    return min <= sval64 && sval64 <= max;
  }

protected:
  typedef char schar_t;
  typedef unsigned char uchar_t;
  typedef short sshort_t;
  typedef unsigned short ushort_t;
  typedef int sint_t;
  typedef unsigned int uint_t;
  typedef long slong_t;
  typedef unsigned long ulong_t;
  typedef long long slonglong_t;
  typedef unsigned long long ulonglong_t;
  typedef float float_t;
  typedef double double_t;
  typedef long double long_double_t;

  //------------------------------------------------------------------
  // Classes that inherit from Scalar can see and modify these
  //------------------------------------------------------------------
  Scalar::Type m_type;
  llvm::APInt m_integer;
  llvm::APFloat m_float;
  bool m_ieee_quad = false;

private:
  friend const Scalar operator+(const Scalar &lhs, const Scalar &rhs);
  friend const Scalar operator-(const Scalar &lhs, const Scalar &rhs);
  friend const Scalar operator/(const Scalar &lhs, const Scalar &rhs);
  friend const Scalar operator*(const Scalar &lhs, const Scalar &rhs);
  friend const Scalar operator&(const Scalar &lhs, const Scalar &rhs);
  friend const Scalar operator|(const Scalar &lhs, const Scalar &rhs);
  friend const Scalar operator%(const Scalar &lhs, const Scalar &rhs);
  friend const Scalar operator^(const Scalar &lhs, const Scalar &rhs);
  friend const Scalar operator<<(const Scalar &lhs, const Scalar &rhs);
  friend const Scalar operator>>(const Scalar &lhs, const Scalar &rhs);
  friend bool operator==(const Scalar &lhs, const Scalar &rhs);
  friend bool operator!=(const Scalar &lhs, const Scalar &rhs);
  friend bool operator<(const Scalar &lhs, const Scalar &rhs);
  friend bool operator<=(const Scalar &lhs, const Scalar &rhs);
  friend bool operator>(const Scalar &lhs, const Scalar &rhs);
  friend bool operator>=(const Scalar &lhs, const Scalar &rhs);
};

//----------------------------------------------------------------------
// Split out the operators into a format where the compiler will be able to
// implicitly convert numbers into Scalar objects.
//
// This allows code like:
//      Scalar two(2);
//      Scalar four = two * 2;
//      Scalar eight = 2 * four;    // This would cause an error if the
//                                  // operator* was implemented as a
//                                  // member function.
// SEE:
//  Item 19 of "Effective C++ Second Edition" by Scott Meyers
//  Differentiate among members functions, non-member functions, and
//  friend functions
//----------------------------------------------------------------------
const Scalar operator+(const Scalar &lhs, const Scalar &rhs);
const Scalar operator-(const Scalar &lhs, const Scalar &rhs);
const Scalar operator/(const Scalar &lhs, const Scalar &rhs);
const Scalar operator*(const Scalar &lhs, const Scalar &rhs);
const Scalar operator&(const Scalar &lhs, const Scalar &rhs);
const Scalar operator|(const Scalar &lhs, const Scalar &rhs);
const Scalar operator%(const Scalar &lhs, const Scalar &rhs);
const Scalar operator^(const Scalar &lhs, const Scalar &rhs);
const Scalar operator<<(const Scalar &lhs, const Scalar &rhs);
const Scalar operator>>(const Scalar &lhs, const Scalar &rhs);
bool operator==(const Scalar &lhs, const Scalar &rhs);
bool operator!=(const Scalar &lhs, const Scalar &rhs);
bool operator<(const Scalar &lhs, const Scalar &rhs);
bool operator<=(const Scalar &lhs, const Scalar &rhs);
bool operator>(const Scalar &lhs, const Scalar &rhs);
bool operator>=(const Scalar &lhs, const Scalar &rhs);

} // namespace lldb_private

#endif // LLDB_UTILITY_SCALAR_H