blob: 23d50b9eaba509526cc68a41f6dc237032b44062 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Scalar.cpp ----------------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Pavel Labathd821c992018-08-07 11:07:21 +00009#include "lldb/Utility/Scalar.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010
Zachary Turner666cc0b2017-03-04 01:30:05 +000011#include "lldb/Utility/DataExtractor.h"
Zachary Turner01c32432017-02-14 19:06:07 +000012#include "lldb/Utility/Endian.h"
Zachary Turner97206d52017-05-12 04:51:55 +000013#include "lldb/Utility/Status.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000014#include "lldb/Utility/Stream.h"
Pavel Labathb07a7992019-04-29 10:55:22 +000015#include "lldb/Utility/StreamString.h"
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000016#include "lldb/lldb-types.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000017
18#include "llvm/ADT/SmallString.h"
19
20#include <cinttypes>
21#include <cstdio>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022
23using namespace lldb;
24using namespace lldb_private;
25
Adrian Prantl05097242018-04-30 16:49:04 +000026// Promote to max type currently follows the ANSI C rule for type promotion in
27// expressions.
Kate Stoneb9c1b512016-09-06 20:57:50 +000028static Scalar::Type PromoteToMaxType(
29 const Scalar &lhs, // The const left hand side object
30 const Scalar &rhs, // The const right hand side object
31 Scalar &temp_value, // A modifiable temp value than can be used to hold
32 // either the promoted lhs or rhs object
33 const Scalar *&promoted_lhs_ptr, // Pointer to the resulting possibly
34 // promoted value of lhs (at most one of
35 // lhs/rhs will get promoted)
36 const Scalar *&promoted_rhs_ptr // Pointer to the resulting possibly
37 // promoted value of rhs (at most one of
38 // lhs/rhs will get promoted)
Pavel Labathd821c992018-08-07 11:07:21 +000039) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000040 Scalar result;
Adrian Prantl05097242018-04-30 16:49:04 +000041 // Initialize the promoted values for both the right and left hand side
42 // values to be the objects themselves. If no promotion is needed (both right
43 // and left have the same type), then the temp_value will not get used.
Kate Stoneb9c1b512016-09-06 20:57:50 +000044 promoted_lhs_ptr = &lhs;
45 promoted_rhs_ptr = &rhs;
46 // Extract the types of both the right and left hand side values
47 Scalar::Type lhs_type = lhs.GetType();
48 Scalar::Type rhs_type = rhs.GetType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049
Kate Stoneb9c1b512016-09-06 20:57:50 +000050 if (lhs_type > rhs_type) {
51 // Right hand side need to be promoted
52 temp_value = rhs; // Copy right hand side into the temp value
53 if (temp_value.Promote(lhs_type)) // Promote it
54 promoted_rhs_ptr =
55 &temp_value; // Update the pointer for the promoted right hand side
56 } else if (lhs_type < rhs_type) {
57 // Left hand side need to be promoted
58 temp_value = lhs; // Copy left hand side value into the temp value
59 if (temp_value.Promote(rhs_type)) // Promote it
60 promoted_lhs_ptr =
61 &temp_value; // Update the pointer for the promoted left hand side
62 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 // Make sure our type promotion worked as expected
65 if (promoted_lhs_ptr->GetType() == promoted_rhs_ptr->GetType())
66 return promoted_lhs_ptr->GetType(); // Return the resulting max type
Chris Lattner30fdc8d2010-06-08 16:52:24 +000067
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 // Return the void type (zero) if we fail to promote either of the values.
69 return Scalar::e_void;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000070}
71
Jonas Devlieghere24374ae2019-05-23 05:12:11 +000072Scalar::Scalar() : m_type(e_void), m_float(static_cast<float>(0)) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073
Kate Stoneb9c1b512016-09-06 20:57:50 +000074bool Scalar::GetData(DataExtractor &data, size_t limit_byte_size) const {
75 size_t byte_size = GetByteSize();
76 if (byte_size > 0) {
77 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(GetBytes());
Ulrich Weigand9521ad22016-04-15 09:55:52 +000078
Kate Stoneb9c1b512016-09-06 20:57:50 +000079 if (limit_byte_size < byte_size) {
80 if (endian::InlHostByteOrder() == eByteOrderLittle) {
Adrian Prantl05097242018-04-30 16:49:04 +000081 // On little endian systems if we want fewer bytes from the current
82 // type we just specify fewer bytes since the LSByte is first...
Kate Stoneb9c1b512016-09-06 20:57:50 +000083 byte_size = limit_byte_size;
84 } else if (endian::InlHostByteOrder() == eByteOrderBig) {
Adrian Prantl05097242018-04-30 16:49:04 +000085 // On big endian systems if we want fewer bytes from the current type
86 // have to advance our initial byte pointer and trim down the number of
87 // bytes since the MSByte is first
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 bytes += byte_size - limit_byte_size;
89 byte_size = limit_byte_size;
90 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000092
93 data.SetData(bytes, byte_size, endian::InlHostByteOrder());
94 return true;
95 }
96 data.Clear();
97 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098}
99
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100const void *Scalar::GetBytes() const {
101 const uint64_t *apint_words;
102 const uint8_t *bytes;
103 static float_t flt_val;
104 static double_t dbl_val;
Davide Italiano51d46bd2019-01-30 18:05:36 +0000105 static uint64_t swapped_words[8];
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106 switch (m_type) {
107 case e_void:
108 break;
109 case e_sint:
110 case e_uint:
111 case e_slong:
112 case e_ulong:
113 case e_slonglong:
114 case e_ulonglong:
115 bytes = reinterpret_cast<const uint8_t *>(m_integer.getRawData());
Adrian Prantl05097242018-04-30 16:49:04 +0000116 // getRawData always returns a pointer to an uint64_t. If we have a
117 // smaller type, we need to update the pointer on big-endian systems.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118 if (endian::InlHostByteOrder() == eByteOrderBig) {
119 size_t byte_size = m_integer.getBitWidth() / 8;
120 if (byte_size < 8)
121 bytes += 8 - byte_size;
Sagar Thakur8536fd12015-08-20 09:12:46 +0000122 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 return bytes;
Davide Italiano92a470e2019-01-30 18:24:16 +0000124 // getRawData always returns a pointer to an array of uint64_t values,
125 // where the least-significant word always comes first. On big-endian
126 // systems we need to swap the words.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127 case e_sint128:
128 case e_uint128:
129 apint_words = m_integer.getRawData();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 if (endian::InlHostByteOrder() == eByteOrderBig) {
131 swapped_words[0] = apint_words[1];
132 swapped_words[1] = apint_words[0];
133 apint_words = swapped_words;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135 return reinterpret_cast<const void *>(apint_words);
136 case e_sint256:
137 case e_uint256:
138 apint_words = m_integer.getRawData();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 if (endian::InlHostByteOrder() == eByteOrderBig) {
140 swapped_words[0] = apint_words[3];
141 swapped_words[1] = apint_words[2];
142 swapped_words[2] = apint_words[1];
143 swapped_words[3] = apint_words[0];
144 apint_words = swapped_words;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000145 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146 return reinterpret_cast<const void *>(apint_words);
Davide Italiano51d46bd2019-01-30 18:05:36 +0000147 case e_sint512:
148 case e_uint512:
149 apint_words = m_integer.getRawData();
Davide Italiano51d46bd2019-01-30 18:05:36 +0000150 if (endian::InlHostByteOrder() == eByteOrderBig) {
151 swapped_words[0] = apint_words[7];
152 swapped_words[1] = apint_words[6];
153 swapped_words[2] = apint_words[5];
154 swapped_words[3] = apint_words[4];
155 swapped_words[4] = apint_words[3];
156 swapped_words[5] = apint_words[2];
157 swapped_words[6] = apint_words[1];
158 swapped_words[7] = apint_words[0];
159 apint_words = swapped_words;
160 }
161 return reinterpret_cast<const void *>(apint_words);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162 case e_float:
163 flt_val = m_float.convertToFloat();
164 return reinterpret_cast<const void *>(&flt_val);
165 case e_double:
166 dbl_val = m_float.convertToDouble();
167 return reinterpret_cast<const void *>(&dbl_val);
168 case e_long_double:
169 llvm::APInt ldbl_val = m_float.bitcastToAPInt();
170 apint_words = ldbl_val.getRawData();
171 // getRawData always returns a pointer to an array of two uint64_t values,
172 // where the least-significant word always comes first. On big-endian
173 // systems we need to swap the two words.
174 if (endian::InlHostByteOrder() == eByteOrderBig) {
175 swapped_words[0] = apint_words[1];
176 swapped_words[1] = apint_words[0];
177 apint_words = swapped_words;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000178 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179 return reinterpret_cast<const void *>(apint_words);
180 }
181 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000182}
183
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184size_t Scalar::GetByteSize() const {
185 switch (m_type) {
186 case e_void:
187 break;
188 case e_sint:
189 case e_uint:
190 case e_slong:
191 case e_ulong:
192 case e_slonglong:
193 case e_ulonglong:
194 case e_sint128:
195 case e_uint128:
196 case e_sint256:
197 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +0000198 case e_sint512:
199 case e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000200 return (m_integer.getBitWidth() / 8);
201 case e_float:
202 return sizeof(float_t);
203 case e_double:
204 return sizeof(double_t);
205 case e_long_double:
206 return sizeof(long_double_t);
207 }
208 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000209}
210
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211bool Scalar::IsZero() const {
212 llvm::APInt zero_int = llvm::APInt::getNullValue(m_integer.getBitWidth() / 8);
213 switch (m_type) {
214 case e_void:
215 break;
216 case e_sint:
217 case e_uint:
218 case e_slong:
219 case e_ulong:
220 case e_slonglong:
221 case e_ulonglong:
222 case e_sint128:
223 case e_uint128:
224 case e_sint256:
225 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +0000226 case e_uint512:
227 case e_sint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000228 return llvm::APInt::isSameValue(zero_int, m_integer);
229 case e_float:
230 case e_double:
231 case e_long_double:
232 return m_float.isZero();
233 }
234 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000235}
236
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237void Scalar::GetValue(Stream *s, bool show_type) const {
238 if (show_type)
239 s->Printf("(%s) ", GetTypeAsCString());
240
241 switch (m_type) {
242 case e_void:
243 break;
244 case e_sint:
245 case e_slong:
246 case e_slonglong:
247 case e_sint128:
248 case e_sint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +0000249 case e_sint512:
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000250 s->PutCString(m_integer.toString(10, true));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000251 break;
252 case e_uint:
253 case e_ulong:
254 case e_ulonglong:
255 case e_uint128:
256 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +0000257 case e_uint512:
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000258 s->PutCString(m_integer.toString(10, false));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259 break;
260 case e_float:
261 case e_double:
262 case e_long_double:
263 llvm::SmallString<24> string;
264 m_float.toString(string);
265 s->Printf("%s", string.c_str());
266 break;
267 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000268}
269
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270const char *Scalar::GetTypeAsCString() const {
271 switch (m_type) {
272 case e_void:
273 return "void";
274 case e_sint:
275 return "int";
276 case e_uint:
277 return "unsigned int";
278 case e_slong:
279 return "long";
280 case e_ulong:
281 return "unsigned long";
282 case e_slonglong:
283 return "long long";
284 case e_ulonglong:
285 return "unsigned long long";
286 case e_sint128:
287 return "int128_t";
288 case e_uint128:
289 return "unsigned int128_t";
290 case e_sint256:
291 return "int256_t";
292 case e_uint256:
293 return "unsigned int256_t";
Davide Italiano51d46bd2019-01-30 18:05:36 +0000294 case e_sint512:
295 return "int512_t";
296 case e_uint512:
297 return "unsigned int512_t";
Kate Stoneb9c1b512016-09-06 20:57:50 +0000298 case e_float:
299 return "float";
300 case e_double:
301 return "double";
302 case e_long_double:
303 return "long double";
304 }
305 return "<invalid Scalar type>";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000306}
307
Kate Stoneb9c1b512016-09-06 20:57:50 +0000308Scalar &Scalar::operator=(const Scalar &rhs) {
309 if (this != &rhs) {
310 m_type = rhs.m_type;
311 m_integer = llvm::APInt(rhs.m_integer);
312 m_float = rhs.m_float;
313 }
314 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000315}
316
Kate Stoneb9c1b512016-09-06 20:57:50 +0000317Scalar &Scalar::operator=(const int v) {
318 m_type = e_sint;
319 m_integer = llvm::APInt(sizeof(int) * 8, v, true);
320 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321}
322
Kate Stoneb9c1b512016-09-06 20:57:50 +0000323Scalar &Scalar::operator=(unsigned int v) {
324 m_type = e_uint;
325 m_integer = llvm::APInt(sizeof(int) * 8, v);
326 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000327}
328
Kate Stoneb9c1b512016-09-06 20:57:50 +0000329Scalar &Scalar::operator=(long v) {
330 m_type = e_slong;
331 m_integer = llvm::APInt(sizeof(long) * 8, v, true);
332 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000333}
334
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335Scalar &Scalar::operator=(unsigned long v) {
336 m_type = e_ulong;
337 m_integer = llvm::APInt(sizeof(long) * 8, v);
338 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000339}
340
Kate Stoneb9c1b512016-09-06 20:57:50 +0000341Scalar &Scalar::operator=(long long v) {
342 m_type = e_slonglong;
343 m_integer = llvm::APInt(sizeof(long) * 8, v, true);
344 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000345}
346
Kate Stoneb9c1b512016-09-06 20:57:50 +0000347Scalar &Scalar::operator=(unsigned long long v) {
348 m_type = e_ulonglong;
349 m_integer = llvm::APInt(sizeof(long long) * 8, v);
350 return *this;
351}
352
353Scalar &Scalar::operator=(float v) {
354 m_type = e_float;
355 m_float = llvm::APFloat(v);
356 return *this;
357}
358
359Scalar &Scalar::operator=(double v) {
360 m_type = e_double;
361 m_float = llvm::APFloat(v);
362 return *this;
363}
364
365Scalar &Scalar::operator=(long double v) {
366 m_type = e_long_double;
367 if (m_ieee_quad)
Jonas Devlieghere24374ae2019-05-23 05:12:11 +0000368 m_float = llvm::APFloat(llvm::APFloat::IEEEquad(),
369 llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
370 (reinterpret_cast<type128 *>(&v))->x));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000371 else
Jonas Devlieghere24374ae2019-05-23 05:12:11 +0000372 m_float = llvm::APFloat(llvm::APFloat::x87DoubleExtended(),
373 llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
374 (reinterpret_cast<type128 *>(&v))->x));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000375 return *this;
376}
377
378Scalar &Scalar::operator=(llvm::APInt rhs) {
379 m_integer = llvm::APInt(rhs);
380 switch (m_integer.getBitWidth()) {
381 case 8:
382 case 16:
383 case 32:
384 if (m_integer.isSignedIntN(sizeof(sint_t) * 8))
385 m_type = e_sint;
Sagar Thakur8536fd12015-08-20 09:12:46 +0000386 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000387 m_type = e_uint;
388 break;
389 case 64:
390 if (m_integer.isSignedIntN(sizeof(slonglong_t) * 8))
391 m_type = e_slonglong;
392 else
393 m_type = e_ulonglong;
394 break;
395 case 128:
396 if (m_integer.isSignedIntN(BITWIDTH_INT128))
397 m_type = e_sint128;
398 else
399 m_type = e_uint128;
400 break;
401 case 256:
402 if (m_integer.isSignedIntN(BITWIDTH_INT256))
403 m_type = e_sint256;
404 else
405 m_type = e_uint256;
406 break;
Davide Italiano51d46bd2019-01-30 18:05:36 +0000407 case 512:
408 if (m_integer.isSignedIntN(BITWIDTH_INT512))
409 m_type = e_sint512;
410 else
411 m_type = e_uint512;
412 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000413 }
414 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415}
416
Eugene Zelenko0e28a192016-03-12 00:31:13 +0000417Scalar::~Scalar() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000418
Kate Stoneb9c1b512016-09-06 20:57:50 +0000419bool Scalar::Promote(Scalar::Type type) {
420 bool success = false;
421 switch (m_type) {
422 case e_void:
423 break;
424
425 case e_sint:
426 switch (type) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000427 case e_void:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000428 break;
Oleksiy Vyalov9dcdd2e2015-08-10 21:49:50 +0000429 case e_sint:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000430 success = true;
431 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432 case e_uint:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000433 m_integer = m_integer.sextOrTrunc(sizeof(uint_t) * 8);
434 success = true;
435 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000436
437 case e_slong:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000438 m_integer = m_integer.sextOrTrunc(sizeof(slong_t) * 8);
439 success = true;
440 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000441
442 case e_ulong:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000443 m_integer = m_integer.sextOrTrunc(sizeof(ulong_t) * 8);
444 success = true;
445 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000446
447 case e_slonglong:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000448 m_integer = m_integer.sextOrTrunc(sizeof(slonglong_t) * 8);
449 success = true;
450 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000451
452 case e_ulonglong:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000453 m_integer = m_integer.sextOrTrunc(sizeof(ulonglong_t) * 8);
454 success = true;
455 break;
Sagar Thakur8536fd12015-08-20 09:12:46 +0000456
457 case e_sint128:
Sagar Thakur8536fd12015-08-20 09:12:46 +0000458 case e_uint128:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000459 m_integer = m_integer.sextOrTrunc(BITWIDTH_INT128);
460 success = true;
461 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000462
Enrico Granata391075c2016-03-10 00:14:29 +0000463 case e_sint256:
Enrico Granata391075c2016-03-10 00:14:29 +0000464 case e_uint256:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000465 m_integer = m_integer.sextOrTrunc(BITWIDTH_INT256);
466 success = true;
467 break;
Eugene Zelenko0e28a192016-03-12 00:31:13 +0000468
Davide Italiano51d46bd2019-01-30 18:05:36 +0000469 case e_sint512:
470 case e_uint512:
471 m_integer = m_integer.sextOrTrunc(BITWIDTH_INT512);
472 success = true;
473 break;
474
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000475 case e_float:
Davide Italiano49d80282018-04-02 16:50:54 +0000476 m_float = llvm::APFloat(llvm::APFloat::IEEEsingle());
477 m_float.convertFromAPInt(m_integer, true,
478 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000479 success = true;
480 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000481
482 case e_double:
Davide Italiano49d80282018-04-02 16:50:54 +0000483 m_float = llvm::APFloat(llvm::APFloat::IEEEdouble());
484 m_float.convertFromAPInt(m_integer, true,
485 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000486 success = true;
487 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488
489 case e_long_double:
Davide Italiano49d80282018-04-02 16:50:54 +0000490 m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad()
491 : llvm::APFloat::x87DoubleExtended());
492 m_float.convertFromAPInt(m_integer, true,
493 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000494 success = true;
495 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000496 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000497 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000498
Kate Stoneb9c1b512016-09-06 20:57:50 +0000499 case e_uint:
500 switch (type) {
501 case e_void:
502 case e_sint:
503 break;
504 case e_uint:
505 success = true;
506 break;
507 case e_slong:
508 m_integer = m_integer.zextOrTrunc(sizeof(slong_t) * 8);
509 success = true;
510 break;
511
512 case e_ulong:
513 m_integer = m_integer.zextOrTrunc(sizeof(ulong_t) * 8);
514 success = true;
515 break;
516
517 case e_slonglong:
518 m_integer = m_integer.zextOrTrunc(sizeof(slonglong_t) * 8);
519 success = true;
520 break;
521
522 case e_ulonglong:
523 m_integer = m_integer.zextOrTrunc(sizeof(ulonglong_t) * 8);
524 success = true;
525 break;
526
527 case e_sint128:
528 case e_uint128:
529 m_integer = m_integer.zextOrTrunc(BITWIDTH_INT128);
530 success = true;
531 break;
532
533 case e_sint256:
534 case e_uint256:
535 m_integer = m_integer.zextOrTrunc(BITWIDTH_INT256);
536 success = true;
537 break;
538
Davide Italiano51d46bd2019-01-30 18:05:36 +0000539 case e_sint512:
540 case e_uint512:
541 m_integer = m_integer.zextOrTrunc(BITWIDTH_INT512);
542 success = true;
543 break;
544
Kate Stoneb9c1b512016-09-06 20:57:50 +0000545 case e_float:
Davide Italiano49d80282018-04-02 16:50:54 +0000546 m_float = llvm::APFloat(llvm::APFloat::IEEEsingle());
547 m_float.convertFromAPInt(m_integer, false,
548 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000549 success = true;
550 break;
551
552 case e_double:
Davide Italiano49d80282018-04-02 16:50:54 +0000553 m_float = llvm::APFloat(llvm::APFloat::IEEEdouble());
554 m_float.convertFromAPInt(m_integer, false,
555 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000556 success = true;
557 break;
558
559 case e_long_double:
Davide Italiano49d80282018-04-02 16:50:54 +0000560 m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad()
561 : llvm::APFloat::x87DoubleExtended());
562 m_float.convertFromAPInt(m_integer, false,
563 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000564 success = true;
565 break;
566 }
567 break;
568
569 case e_slong:
570 switch (type) {
571 case e_void:
572 case e_sint:
573 case e_uint:
574 break;
575 case e_slong:
576 success = true;
577 break;
578 case e_ulong:
579 m_integer = m_integer.sextOrTrunc(sizeof(ulong_t) * 8);
580 success = true;
581 break;
582
583 case e_slonglong:
584 m_integer = m_integer.sextOrTrunc(sizeof(slonglong_t) * 8);
585 success = true;
586 break;
587
588 case e_ulonglong:
589 m_integer = m_integer.sextOrTrunc(sizeof(ulonglong_t) * 8);
590 success = true;
591 break;
592
593 case e_sint128:
594 case e_uint128:
595 m_integer = m_integer.sextOrTrunc(BITWIDTH_INT128);
596 success = true;
597 break;
598
599 case e_sint256:
600 case e_uint256:
601 m_integer = m_integer.sextOrTrunc(BITWIDTH_INT256);
602 success = true;
603 break;
604
Davide Italiano51d46bd2019-01-30 18:05:36 +0000605 case e_sint512:
606 case e_uint512:
607 m_integer = m_integer.sextOrTrunc(BITWIDTH_INT512);
608 success = true;
609 break;
610
Kate Stoneb9c1b512016-09-06 20:57:50 +0000611 case e_float:
Davide Italiano49d80282018-04-02 16:50:54 +0000612 m_float = llvm::APFloat(llvm::APFloat::IEEEsingle());
613 m_float.convertFromAPInt(m_integer, true,
614 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000615 success = true;
616 break;
617
618 case e_double:
Davide Italiano49d80282018-04-02 16:50:54 +0000619 m_float = llvm::APFloat(llvm::APFloat::IEEEdouble());
620 m_float.convertFromAPInt(m_integer, true,
621 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000622 success = true;
623 break;
624
625 case e_long_double:
Davide Italiano49d80282018-04-02 16:50:54 +0000626 m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad()
627 : llvm::APFloat::x87DoubleExtended());
628 m_float.convertFromAPInt(m_integer, true,
629 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000630 success = true;
631 break;
632 }
633 break;
634
635 case e_ulong:
636 switch (type) {
637 case e_void:
638 case e_sint:
639 case e_uint:
640 case e_slong:
641 break;
642 case e_ulong:
643 success = true;
644 break;
645 case e_slonglong:
646 m_integer = m_integer.zextOrTrunc(sizeof(slonglong_t) * 8);
647 success = true;
648 break;
649
650 case e_ulonglong:
651 m_integer = m_integer.zextOrTrunc(sizeof(ulonglong_t) * 8);
652 success = true;
653 break;
654
655 case e_sint128:
656 case e_uint128:
657 m_integer = m_integer.zextOrTrunc(BITWIDTH_INT128);
658 success = true;
659 break;
660
661 case e_sint256:
662 case e_uint256:
663 m_integer = m_integer.zextOrTrunc(BITWIDTH_INT256);
664 success = true;
665 break;
666
Davide Italiano51d46bd2019-01-30 18:05:36 +0000667 case e_sint512:
668 case e_uint512:
669 m_integer = m_integer.zextOrTrunc(BITWIDTH_INT512);
670 success = true;
671 break;
672
Kate Stoneb9c1b512016-09-06 20:57:50 +0000673 case e_float:
Davide Italiano49d80282018-04-02 16:50:54 +0000674 m_float = llvm::APFloat(llvm::APFloat::IEEEsingle());
675 m_float.convertFromAPInt(m_integer, false,
676 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000677 success = true;
678 break;
679
680 case e_double:
Davide Italiano49d80282018-04-02 16:50:54 +0000681 m_float = llvm::APFloat(llvm::APFloat::IEEEdouble());
682 m_float.convertFromAPInt(m_integer, false,
683 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000684 success = true;
685 break;
686
687 case e_long_double:
Davide Italiano49d80282018-04-02 16:50:54 +0000688 m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad()
689 : llvm::APFloat::x87DoubleExtended());
690 m_float.convertFromAPInt(m_integer, false,
691 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000692 success = true;
693 break;
694 }
695 break;
696
697 case e_slonglong:
698 switch (type) {
699 case e_void:
700 case e_sint:
701 case e_uint:
702 case e_slong:
703 case e_ulong:
704 break;
705 case e_slonglong:
706 success = true;
707 break;
708 case e_ulonglong:
709 m_integer = m_integer.sextOrTrunc(sizeof(ulonglong_t) * 8);
710 success = true;
711 break;
712
713 case e_sint128:
714 case e_uint128:
715 m_integer = m_integer.sextOrTrunc(BITWIDTH_INT128);
716 success = true;
717 break;
718
719 case e_sint256:
720 case e_uint256:
721 m_integer = m_integer.sextOrTrunc(BITWIDTH_INT256);
722 success = true;
723 break;
724
Davide Italiano51d46bd2019-01-30 18:05:36 +0000725 case e_sint512:
726 case e_uint512:
727 m_integer = m_integer.sextOrTrunc(BITWIDTH_INT512);
728 success = true;
729 break;
730
Kate Stoneb9c1b512016-09-06 20:57:50 +0000731 case e_float:
Davide Italiano49d80282018-04-02 16:50:54 +0000732 m_float = llvm::APFloat(llvm::APFloat::IEEEsingle());
733 m_float.convertFromAPInt(m_integer, true,
734 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000735 success = true;
736 break;
737
738 case e_double:
Davide Italiano49d80282018-04-02 16:50:54 +0000739 m_float = llvm::APFloat(llvm::APFloat::IEEEdouble());
740 m_float.convertFromAPInt(m_integer, true,
741 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000742 success = true;
743 break;
744
745 case e_long_double:
Davide Italiano49d80282018-04-02 16:50:54 +0000746 m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad()
747 : llvm::APFloat::x87DoubleExtended());
748 m_float.convertFromAPInt(m_integer, true,
749 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000750 success = true;
751 break;
752 }
753 break;
754
755 case e_ulonglong:
756 switch (type) {
757 case e_void:
758 case e_sint:
759 case e_uint:
760 case e_slong:
761 case e_ulong:
762 case e_slonglong:
763 break;
764 case e_ulonglong:
765 success = true;
766 break;
767 case e_sint128:
768 case e_uint128:
769 m_integer = m_integer.zextOrTrunc(BITWIDTH_INT128);
770 success = true;
771 break;
772
773 case e_sint256:
774 case e_uint256:
775 m_integer = m_integer.zextOrTrunc(BITWIDTH_INT256);
776 success = true;
777 break;
778
Davide Italiano51d46bd2019-01-30 18:05:36 +0000779 case e_sint512:
780 case e_uint512:
781 m_integer = m_integer.zextOrTrunc(BITWIDTH_INT512);
782 success = true;
783 break;
784
Kate Stoneb9c1b512016-09-06 20:57:50 +0000785 case e_float:
Davide Italiano49d80282018-04-02 16:50:54 +0000786 m_float = llvm::APFloat(llvm::APFloat::IEEEsingle());
787 m_float.convertFromAPInt(m_integer, false,
788 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000789 success = true;
790 break;
791
792 case e_double:
Davide Italiano49d80282018-04-02 16:50:54 +0000793 m_float = llvm::APFloat(llvm::APFloat::IEEEdouble());
794 m_float.convertFromAPInt(m_integer, false,
795 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000796 success = true;
797 break;
798
799 case e_long_double:
Davide Italiano49d80282018-04-02 16:50:54 +0000800 m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad()
801 : llvm::APFloat::x87DoubleExtended());
802 m_float.convertFromAPInt(m_integer, false,
803 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000804 success = true;
805 break;
806 }
807 break;
808
809 case e_sint128:
810 switch (type) {
811 case e_void:
812 case e_sint:
813 case e_uint:
814 case e_slong:
815 case e_ulong:
816 case e_slonglong:
817 case e_ulonglong:
818 break;
819 case e_sint128:
820 success = true;
821 break;
822 case e_uint128:
823 m_integer = m_integer.sextOrTrunc(BITWIDTH_INT128);
824 success = true;
825 break;
826
827 case e_sint256:
828 case e_uint256:
829 m_integer = m_integer.sextOrTrunc(BITWIDTH_INT256);
830 success = true;
831 break;
832
Davide Italiano51d46bd2019-01-30 18:05:36 +0000833 case e_sint512:
834 case e_uint512:
835 m_integer = m_integer.sextOrTrunc(BITWIDTH_INT512);
836 success = true;
837 break;
838
Kate Stoneb9c1b512016-09-06 20:57:50 +0000839 case e_float:
Davide Italiano49d80282018-04-02 16:50:54 +0000840 m_float = llvm::APFloat(llvm::APFloat::IEEEsingle());
841 m_float.convertFromAPInt(m_integer, true,
842 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000843 success = true;
844 break;
845
846 case e_double:
Davide Italiano49d80282018-04-02 16:50:54 +0000847 m_float = llvm::APFloat(llvm::APFloat::IEEEdouble());
848 m_float.convertFromAPInt(m_integer, true,
849 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000850 success = true;
851 break;
852
853 case e_long_double:
Davide Italiano49d80282018-04-02 16:50:54 +0000854 m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad()
855 : llvm::APFloat::x87DoubleExtended());
856 m_float.convertFromAPInt(m_integer, true,
857 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000858 success = true;
859 break;
860 }
861 break;
862
863 case e_uint128:
864 switch (type) {
865 case e_void:
866 case e_sint:
867 case e_uint:
868 case e_slong:
869 case e_ulong:
870 case e_slonglong:
871 case e_ulonglong:
872 case e_sint128:
873 break;
874 case e_uint128:
875 success = true;
876 break;
877 case e_sint256:
878 case e_uint256:
879 m_integer = m_integer.zextOrTrunc(BITWIDTH_INT256);
880 success = true;
881 break;
882
Davide Italiano51d46bd2019-01-30 18:05:36 +0000883 case e_sint512:
884 case e_uint512:
885 m_integer = m_integer.zextOrTrunc(BITWIDTH_INT512);
886 success = true;
887 break;
888
Kate Stoneb9c1b512016-09-06 20:57:50 +0000889 case e_float:
Davide Italiano49d80282018-04-02 16:50:54 +0000890 m_float = llvm::APFloat(llvm::APFloat::IEEEsingle());
891 m_float.convertFromAPInt(m_integer, false,
892 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000893 success = true;
894 break;
895
896 case e_double:
Davide Italiano49d80282018-04-02 16:50:54 +0000897 m_float = llvm::APFloat(llvm::APFloat::IEEEdouble());
898 m_float.convertFromAPInt(m_integer, false,
899 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000900 success = true;
901 break;
902
903 case e_long_double:
Davide Italiano49d80282018-04-02 16:50:54 +0000904 m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad()
905 : llvm::APFloat::x87DoubleExtended());
906 m_float.convertFromAPInt(m_integer, false,
907 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000908 success = true;
909 break;
910 }
911 break;
912
913 case e_sint256:
914 switch (type) {
915 case e_void:
916 case e_sint:
917 case e_uint:
918 case e_slong:
919 case e_ulong:
920 case e_slonglong:
921 case e_ulonglong:
922 case e_sint128:
923 case e_uint128:
924 break;
925 case e_sint256:
926 success = true;
927 break;
928 case e_uint256:
929 m_integer = m_integer.sextOrTrunc(BITWIDTH_INT256);
930 success = true;
931 break;
932
Davide Italiano51d46bd2019-01-30 18:05:36 +0000933 case e_sint512:
934 case e_uint512:
935 m_integer = m_integer.zextOrTrunc(BITWIDTH_INT512);
936 success = true;
937 break;
938
Kate Stoneb9c1b512016-09-06 20:57:50 +0000939 case e_float:
Davide Italiano49d80282018-04-02 16:50:54 +0000940 m_float = llvm::APFloat(llvm::APFloat::IEEEsingle());
941 m_float.convertFromAPInt(m_integer, true,
942 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000943 success = true;
944 break;
945
946 case e_double:
Davide Italiano49d80282018-04-02 16:50:54 +0000947 m_float = llvm::APFloat(llvm::APFloat::IEEEdouble());
948 m_float.convertFromAPInt(m_integer, true,
949 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000950 success = true;
951 break;
952
953 case e_long_double:
Davide Italiano49d80282018-04-02 16:50:54 +0000954 m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad()
955 : llvm::APFloat::x87DoubleExtended());
956 m_float.convertFromAPInt(m_integer, true,
957 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000958 success = true;
959 break;
960 }
961 break;
962
963 case e_uint256:
964 switch (type) {
965 case e_void:
966 case e_sint:
967 case e_uint:
968 case e_slong:
969 case e_ulong:
970 case e_slonglong:
971 case e_ulonglong:
972 case e_sint128:
973 case e_uint128:
974 case e_sint256:
975 break;
976 case e_uint256:
977 success = true;
978 break;
Davide Italiano51d46bd2019-01-30 18:05:36 +0000979
980 case e_sint512:
981 case e_uint512:
982 m_integer = m_integer.zextOrTrunc(BITWIDTH_INT512);
983 success = true;
984 break;
985
Kate Stoneb9c1b512016-09-06 20:57:50 +0000986 case e_float:
Davide Italiano49d80282018-04-02 16:50:54 +0000987 m_float = llvm::APFloat(llvm::APFloat::IEEEsingle());
988 m_float.convertFromAPInt(m_integer, false,
989 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000990 success = true;
991 break;
992
993 case e_double:
Davide Italiano49d80282018-04-02 16:50:54 +0000994 m_float = llvm::APFloat(llvm::APFloat::IEEEdouble());
995 m_float.convertFromAPInt(m_integer, false,
996 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000997 success = true;
998 break;
999
1000 case e_long_double:
Davide Italiano49d80282018-04-02 16:50:54 +00001001 m_float = llvm::APFloat(m_ieee_quad ? llvm::APFloat::IEEEquad()
1002 : llvm::APFloat::x87DoubleExtended());
1003 m_float.convertFromAPInt(m_integer, false,
1004 llvm::APFloat::rmNearestTiesToEven);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001005 success = true;
1006 break;
1007 }
1008 break;
1009
Davide Italiano51d46bd2019-01-30 18:05:36 +00001010 case e_sint512:
1011 case e_uint512:
1012 lldbassert(false && "unimplemented");
1013 break;
1014
Kate Stoneb9c1b512016-09-06 20:57:50 +00001015 case e_float:
1016 switch (type) {
1017 case e_void:
1018 case e_sint:
1019 case e_uint:
1020 case e_slong:
1021 case e_ulong:
1022 case e_slonglong:
1023 case e_ulonglong:
1024 case e_sint128:
1025 case e_uint128:
1026 case e_sint256:
1027 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00001028 case e_uint512:
1029 case e_sint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001030 break;
1031 case e_float:
1032 success = true;
1033 break;
1034 case e_double:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001035 m_float = llvm::APFloat(static_cast<double_t>(m_float.convertToFloat()));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001036 success = true;
1037 break;
1038
Davide Italiano49d80282018-04-02 16:50:54 +00001039 case e_long_double: {
1040 bool ignore;
1041 m_float.convert(m_ieee_quad ? llvm::APFloat::IEEEquad()
1042 : llvm::APFloat::x87DoubleExtended(),
1043 llvm::APFloat::rmNearestTiesToEven, &ignore);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001044 success = true;
1045 break;
1046 }
Davide Italiano49d80282018-04-02 16:50:54 +00001047 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001048 break;
1049
1050 case e_double:
1051 switch (type) {
1052 case e_void:
1053 case e_sint:
1054 case e_uint:
1055 case e_slong:
1056 case e_ulong:
1057 case e_slonglong:
1058 case e_ulonglong:
1059 case e_sint128:
1060 case e_uint128:
1061 case e_sint256:
1062 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00001063 case e_sint512:
1064 case e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001065 case e_float:
1066 break;
1067 case e_double:
1068 success = true;
1069 break;
Davide Italiano49d80282018-04-02 16:50:54 +00001070 case e_long_double: {
1071 bool ignore;
1072 m_float.convert(m_ieee_quad ? llvm::APFloat::IEEEquad()
1073 : llvm::APFloat::x87DoubleExtended(),
1074 llvm::APFloat::rmNearestTiesToEven, &ignore);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001075 success = true;
1076 break;
1077 }
Davide Italiano49d80282018-04-02 16:50:54 +00001078 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001079 break;
1080
1081 case e_long_double:
1082 switch (type) {
1083 case e_void:
1084 case e_sint:
1085 case e_uint:
1086 case e_slong:
1087 case e_ulong:
1088 case e_slonglong:
1089 case e_ulonglong:
1090 case e_sint128:
1091 case e_uint128:
1092 case e_sint256:
1093 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00001094 case e_sint512:
1095 case e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001096 case e_float:
1097 case e_double:
1098 break;
1099 case e_long_double:
1100 success = true;
1101 break;
1102 }
1103 break;
1104 }
1105
1106 if (success)
1107 m_type = type;
1108 return success;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001109}
1110
Kate Stoneb9c1b512016-09-06 20:57:50 +00001111const char *Scalar::GetValueTypeAsCString(Scalar::Type type) {
1112 switch (type) {
1113 case e_void:
1114 return "void";
1115 case e_sint:
1116 return "int";
1117 case e_uint:
1118 return "unsigned int";
1119 case e_slong:
1120 return "long";
1121 case e_ulong:
1122 return "unsigned long";
1123 case e_slonglong:
1124 return "long long";
1125 case e_ulonglong:
1126 return "unsigned long long";
1127 case e_float:
1128 return "float";
1129 case e_double:
1130 return "double";
1131 case e_long_double:
1132 return "long double";
1133 case e_sint128:
1134 return "int128_t";
1135 case e_uint128:
1136 return "uint128_t";
1137 case e_sint256:
1138 return "int256_t";
1139 case e_uint256:
1140 return "uint256_t";
Davide Italiano51d46bd2019-01-30 18:05:36 +00001141 case e_sint512:
1142 return "int512_t";
1143 case e_uint512:
1144 return "uint512_t";
Kate Stoneb9c1b512016-09-06 20:57:50 +00001145 }
1146 return "???";
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001147}
1148
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001149Scalar::Type
Kate Stoneb9c1b512016-09-06 20:57:50 +00001150Scalar::GetValueTypeForSignedIntegerWithByteSize(size_t byte_size) {
1151 if (byte_size <= sizeof(sint_t))
1152 return e_sint;
1153 if (byte_size <= sizeof(slong_t))
1154 return e_slong;
1155 if (byte_size <= sizeof(slonglong_t))
1156 return e_slonglong;
1157 return e_void;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001158}
1159
1160Scalar::Type
Kate Stoneb9c1b512016-09-06 20:57:50 +00001161Scalar::GetValueTypeForUnsignedIntegerWithByteSize(size_t byte_size) {
1162 if (byte_size <= sizeof(uint_t))
1163 return e_uint;
1164 if (byte_size <= sizeof(ulong_t))
1165 return e_ulong;
1166 if (byte_size <= sizeof(ulonglong_t))
1167 return e_ulonglong;
1168 return e_void;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001169}
1170
Kate Stoneb9c1b512016-09-06 20:57:50 +00001171Scalar::Type Scalar::GetValueTypeForFloatWithByteSize(size_t byte_size) {
1172 if (byte_size == sizeof(float_t))
1173 return e_float;
1174 if (byte_size == sizeof(double_t))
1175 return e_double;
1176 if (byte_size == sizeof(long_double_t))
1177 return e_long_double;
1178 return e_void;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001179}
1180
Kate Stoneb9c1b512016-09-06 20:57:50 +00001181bool Scalar::MakeSigned() {
1182 bool success = false;
1183
1184 switch (m_type) {
1185 case e_void:
1186 break;
1187 case e_sint:
1188 success = true;
1189 break;
1190 case e_uint:
1191 m_type = e_sint;
1192 success = true;
1193 break;
1194 case e_slong:
1195 success = true;
1196 break;
1197 case e_ulong:
1198 m_type = e_slong;
1199 success = true;
1200 break;
1201 case e_slonglong:
1202 success = true;
1203 break;
1204 case e_ulonglong:
1205 m_type = e_slonglong;
1206 success = true;
1207 break;
1208 case e_sint128:
1209 success = true;
1210 break;
1211 case e_uint128:
1212 m_type = e_sint128;
1213 success = true;
1214 break;
1215 case e_sint256:
1216 success = true;
1217 break;
1218 case e_uint256:
1219 m_type = e_sint256;
1220 success = true;
1221 break;
Davide Italiano51d46bd2019-01-30 18:05:36 +00001222 case e_sint512:
1223 success = true;
1224 break;
1225 case e_uint512:
1226 m_type = e_sint512;
1227 success = true;
1228 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001229 case e_float:
1230 success = true;
1231 break;
1232 case e_double:
1233 success = true;
1234 break;
1235 case e_long_double:
1236 success = true;
1237 break;
1238 }
1239
1240 return success;
1241}
1242
1243bool Scalar::MakeUnsigned() {
1244 bool success = false;
1245
1246 switch (m_type) {
1247 case e_void:
1248 break;
1249 case e_sint:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001250 m_type = e_uint;
1251 success = true;
1252 break;
Davide Italianoa9d84cb2018-07-12 00:31:04 +00001253 case e_uint:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001254 success = true;
1255 break;
Davide Italianoa9d84cb2018-07-12 00:31:04 +00001256 case e_slong:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001257 m_type = e_ulong;
1258 success = true;
1259 break;
Davide Italianoa9d84cb2018-07-12 00:31:04 +00001260 case e_ulong:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001261 success = true;
1262 break;
Davide Italianoa9d84cb2018-07-12 00:31:04 +00001263 case e_slonglong:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001264 m_type = e_ulonglong;
1265 success = true;
1266 break;
Davide Italianoa9d84cb2018-07-12 00:31:04 +00001267 case e_ulonglong:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001268 success = true;
1269 break;
Davide Italianoa9d84cb2018-07-12 00:31:04 +00001270 case e_sint128:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001271 m_type = e_uint128;
1272 success = true;
1273 break;
Davide Italianoa9d84cb2018-07-12 00:31:04 +00001274 case e_uint128:
1275 success = true;
1276 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001277 case e_sint256:
Davide Italianoa9d84cb2018-07-12 00:31:04 +00001278 m_type = e_uint256;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001279 success = true;
1280 break;
1281 case e_uint256:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001282 success = true;
1283 break;
Davide Italiano51d46bd2019-01-30 18:05:36 +00001284 case e_sint512:
1285 m_type = e_uint512;
1286 success = true;
1287 break;
1288 case e_uint512:
1289 success = true;
1290 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001291 case e_float:
1292 success = true;
1293 break;
1294 case e_double:
1295 success = true;
1296 break;
1297 case e_long_double:
1298 success = true;
1299 break;
1300 }
1301
1302 return success;
1303}
1304
1305signed char Scalar::SChar(char fail_value) const {
1306 switch (m_type) {
1307 case e_void:
1308 break;
1309 case e_sint:
1310 case e_uint:
1311 case e_slong:
1312 case e_ulong:
1313 case e_slonglong:
1314 case e_ulonglong:
1315 case e_sint128:
1316 case e_uint128:
1317 case e_sint256:
1318 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00001319 case e_sint512:
1320 case e_uint512:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001321 return static_cast<schar_t>(
1322 (m_integer.sextOrTrunc(sizeof(schar_t) * 8)).getSExtValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001323 case e_float:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001324 return static_cast<schar_t>(m_float.convertToFloat());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001325 case e_double:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001326 return static_cast<schar_t>(m_float.convertToDouble());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001327 case e_long_double:
1328 llvm::APInt ldbl_val = m_float.bitcastToAPInt();
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001329 return static_cast<schar_t>(
1330 (ldbl_val.sextOrTrunc(sizeof(schar_t) * 8)).getSExtValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001331 }
1332 return fail_value;
1333}
1334
1335unsigned char Scalar::UChar(unsigned char fail_value) const {
1336 switch (m_type) {
1337 case e_void:
1338 break;
1339 case e_sint:
1340 case e_uint:
1341 case e_slong:
1342 case e_ulong:
1343 case e_slonglong:
1344 case e_ulonglong:
1345 case e_sint128:
1346 case e_uint128:
1347 case e_sint256:
1348 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00001349 case e_sint512:
1350 case e_uint512:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001351 return static_cast<uchar_t>(
1352 (m_integer.zextOrTrunc(sizeof(uchar_t) * 8)).getZExtValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001353 case e_float:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001354 return static_cast<uchar_t>(m_float.convertToFloat());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001355 case e_double:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001356 return static_cast<uchar_t>(m_float.convertToDouble());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001357 case e_long_double:
1358 llvm::APInt ldbl_val = m_float.bitcastToAPInt();
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001359 return static_cast<uchar_t>(
1360 (ldbl_val.zextOrTrunc(sizeof(uchar_t) * 8)).getZExtValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001361 }
1362 return fail_value;
1363}
1364
1365short Scalar::SShort(short fail_value) const {
1366 switch (m_type) {
1367 case e_void:
1368 break;
1369 case e_sint:
1370 case e_uint:
1371 case e_slong:
1372 case e_ulong:
1373 case e_slonglong:
1374 case e_ulonglong:
1375 case e_sint128:
1376 case e_uint128:
1377 case e_sint256:
1378 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00001379 case e_sint512:
1380 case e_uint512:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001381 return static_cast<sshort_t>(
1382 (m_integer.sextOrTrunc(sizeof(sshort_t) * 8)).getSExtValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001383 case e_float:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001384 return static_cast<sshort_t>(m_float.convertToFloat());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001385 case e_double:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001386 return static_cast<sshort_t>(m_float.convertToDouble());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001387 case e_long_double:
1388 llvm::APInt ldbl_val = m_float.bitcastToAPInt();
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001389 return static_cast<sshort_t>(
1390 (ldbl_val.sextOrTrunc(sizeof(sshort_t) * 8)).getSExtValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001391 }
1392 return fail_value;
1393}
1394
1395unsigned short Scalar::UShort(unsigned short fail_value) const {
1396 switch (m_type) {
1397 case e_void:
1398 break;
1399 case e_sint:
1400 case e_uint:
1401 case e_slong:
1402 case e_ulong:
1403 case e_slonglong:
1404 case e_ulonglong:
1405 case e_sint128:
1406 case e_uint128:
1407 case e_sint256:
1408 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00001409 case e_sint512:
1410 case e_uint512:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001411 return static_cast<ushort_t>(
1412 (m_integer.zextOrTrunc(sizeof(ushort_t) * 8)).getZExtValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001413 case e_float:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001414 return static_cast<ushort_t>(m_float.convertToFloat());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001415 case e_double:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001416 return static_cast<ushort_t>(m_float.convertToDouble());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001417 case e_long_double:
1418 llvm::APInt ldbl_val = m_float.bitcastToAPInt();
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001419 return static_cast<ushort_t>(
1420 (ldbl_val.zextOrTrunc(sizeof(ushort_t) * 8)).getZExtValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001421 }
1422 return fail_value;
1423}
1424
1425int Scalar::SInt(int fail_value) const {
1426 switch (m_type) {
1427 case e_void:
1428 break;
1429 case e_sint:
1430 case e_uint:
1431 case e_slong:
1432 case e_ulong:
1433 case e_slonglong:
1434 case e_ulonglong:
1435 case e_sint128:
1436 case e_uint128:
1437 case e_sint256:
1438 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00001439 case e_sint512:
1440 case e_uint512:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001441 return static_cast<sint_t>(
1442 (m_integer.sextOrTrunc(sizeof(sint_t) * 8)).getSExtValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001443 case e_float:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001444 return static_cast<sint_t>(m_float.convertToFloat());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001445 case e_double:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001446 return static_cast<sint_t>(m_float.convertToDouble());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001447 case e_long_double:
1448 llvm::APInt ldbl_val = m_float.bitcastToAPInt();
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001449 return static_cast<sint_t>(
1450 (ldbl_val.sextOrTrunc(sizeof(sint_t) * 8)).getSExtValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001451 }
1452 return fail_value;
1453}
1454
1455unsigned int Scalar::UInt(unsigned int fail_value) const {
1456 switch (m_type) {
1457 case e_void:
1458 break;
1459 case e_sint:
1460 case e_uint:
1461 case e_slong:
1462 case e_ulong:
1463 case e_slonglong:
1464 case e_ulonglong:
1465 case e_sint128:
1466 case e_uint128:
1467 case e_sint256:
1468 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00001469 case e_sint512:
1470 case e_uint512:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001471 return static_cast<uint_t>(
1472 (m_integer.zextOrTrunc(sizeof(uint_t) * 8)).getZExtValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001473 case e_float:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001474 return static_cast<uint_t>(m_float.convertToFloat());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001475 case e_double:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001476 return static_cast<uint_t>(m_float.convertToDouble());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001477 case e_long_double:
1478 llvm::APInt ldbl_val = m_float.bitcastToAPInt();
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001479 return static_cast<uint_t>(
1480 (ldbl_val.zextOrTrunc(sizeof(uint_t) * 8)).getZExtValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001481 }
1482 return fail_value;
1483}
1484
1485long Scalar::SLong(long fail_value) const {
1486 switch (m_type) {
1487 case e_void:
1488 break;
1489 case e_sint:
1490 case e_uint:
1491 case e_slong:
1492 case e_ulong:
1493 case e_slonglong:
1494 case e_ulonglong:
1495 case e_sint128:
1496 case e_uint128:
1497 case e_sint256:
1498 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00001499 case e_sint512:
1500 case e_uint512:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001501 return static_cast<slong_t>(
1502 (m_integer.sextOrTrunc(sizeof(slong_t) * 8)).getSExtValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001503 case e_float:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001504 return static_cast<slong_t>(m_float.convertToFloat());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001505 case e_double:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001506 return static_cast<slong_t>(m_float.convertToDouble());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001507 case e_long_double:
1508 llvm::APInt ldbl_val = m_float.bitcastToAPInt();
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001509 return static_cast<slong_t>(
1510 (ldbl_val.sextOrTrunc(sizeof(slong_t) * 8)).getSExtValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001511 }
1512 return fail_value;
1513}
1514
1515unsigned long Scalar::ULong(unsigned long fail_value) const {
1516 switch (m_type) {
1517 case e_void:
1518 break;
1519 case e_sint:
1520 case e_uint:
1521 case e_slong:
1522 case e_ulong:
1523 case e_slonglong:
1524 case e_ulonglong:
1525 case e_sint128:
1526 case e_uint128:
1527 case e_sint256:
1528 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00001529 case e_sint512:
1530 case e_uint512:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001531 return static_cast<ulong_t>(
1532 (m_integer.zextOrTrunc(sizeof(ulong_t) * 8)).getZExtValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001533 case e_float:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001534 return static_cast<ulong_t>(m_float.convertToFloat());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001535 case e_double:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001536 return static_cast<ulong_t>(m_float.convertToDouble());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001537 case e_long_double:
1538 llvm::APInt ldbl_val = m_float.bitcastToAPInt();
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001539 return static_cast<ulong_t>(
1540 (ldbl_val.zextOrTrunc(sizeof(ulong_t) * 8)).getZExtValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001541 }
1542 return fail_value;
1543}
1544
1545long long Scalar::SLongLong(long long fail_value) const {
1546 switch (m_type) {
1547 case e_void:
1548 break;
1549 case e_sint:
1550 case e_uint:
1551 case e_slong:
1552 case e_ulong:
1553 case e_slonglong:
1554 case e_ulonglong:
1555 case e_sint128:
1556 case e_uint128:
1557 case e_sint256:
1558 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00001559 case e_sint512:
1560 case e_uint512:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001561 return static_cast<slonglong_t>(
1562 (m_integer.sextOrTrunc(sizeof(slonglong_t) * 8)).getSExtValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001563 case e_float:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001564 return static_cast<slonglong_t>(m_float.convertToFloat());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001565 case e_double:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001566 return static_cast<slonglong_t>(m_float.convertToDouble());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001567 case e_long_double:
1568 llvm::APInt ldbl_val = m_float.bitcastToAPInt();
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001569 return static_cast<slonglong_t>(
1570 (ldbl_val.sextOrTrunc(sizeof(slonglong_t) * 8)).getSExtValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001571 }
1572 return fail_value;
1573}
1574
1575unsigned long long Scalar::ULongLong(unsigned long long fail_value) const {
1576 switch (m_type) {
1577 case e_void:
1578 break;
1579 case e_sint:
1580 case e_uint:
1581 case e_slong:
1582 case e_ulong:
1583 case e_slonglong:
1584 case e_ulonglong:
1585 case e_sint128:
1586 case e_uint128:
1587 case e_sint256:
1588 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00001589 case e_sint512:
1590 case e_uint512:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001591 return static_cast<ulonglong_t>(
1592 (m_integer.zextOrTrunc(sizeof(ulonglong_t) * 8)).getZExtValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001593 case e_float:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001594 return static_cast<ulonglong_t>(m_float.convertToFloat());
Davide Italianof06ffee2018-09-07 18:22:27 +00001595 case e_double: {
1596 double d_val = m_float.convertToDouble();
1597 llvm::APInt rounded_double =
1598 llvm::APIntOps::RoundDoubleToAPInt(d_val, sizeof(ulonglong_t) * 8);
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001599 return static_cast<ulonglong_t>(
1600 (rounded_double.zextOrTrunc(sizeof(ulonglong_t) * 8)).getZExtValue());
Davide Italianof06ffee2018-09-07 18:22:27 +00001601 }
1602 case e_long_double:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001603 llvm::APInt ldbl_val = m_float.bitcastToAPInt();
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001604 return static_cast<ulonglong_t>(
1605 (ldbl_val.zextOrTrunc(sizeof(ulonglong_t) * 8)).getZExtValue());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001606 }
1607 return fail_value;
1608}
1609
1610llvm::APInt Scalar::SInt128(llvm::APInt &fail_value) const {
1611 switch (m_type) {
1612 case e_void:
1613 break;
1614 case e_sint:
1615 case e_uint:
1616 case e_slong:
1617 case e_ulong:
1618 case e_slonglong:
1619 case e_ulonglong:
1620 case e_sint128:
1621 case e_uint128:
1622 case e_sint256:
1623 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00001624 case e_sint512:
1625 case e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001626 return m_integer;
1627 case e_float:
1628 case e_double:
1629 case e_long_double:
1630 return m_float.bitcastToAPInt();
1631 }
1632 return fail_value;
1633}
1634
1635llvm::APInt Scalar::UInt128(const llvm::APInt &fail_value) const {
1636 switch (m_type) {
1637 case e_void:
1638 break;
1639 case e_sint:
1640 case e_uint:
1641 case e_slong:
1642 case e_ulong:
1643 case e_slonglong:
1644 case e_ulonglong:
1645 case e_sint128:
1646 case e_uint128:
1647 case e_sint256:
1648 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00001649 case e_sint512:
1650 case e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001651 return m_integer;
1652 case e_float:
1653 case e_double:
1654 case e_long_double:
1655 return m_float.bitcastToAPInt();
1656 }
1657 return fail_value;
1658}
1659
Kate Stoneb9c1b512016-09-06 20:57:50 +00001660float Scalar::Float(float fail_value) const {
1661 switch (m_type) {
1662 case e_void:
1663 break;
1664 case e_sint:
1665 case e_uint:
1666 case e_slong:
1667 case e_ulong:
1668 case e_slonglong:
1669 case e_ulonglong:
1670 case e_sint128:
1671 case e_uint128:
1672 case e_sint256:
1673 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00001674 case e_sint512:
1675 case e_uint512:
Davide Italiano49d80282018-04-02 16:50:54 +00001676 return llvm::APIntOps::RoundAPIntToFloat(m_integer);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001677 case e_float:
1678 return m_float.convertToFloat();
1679 case e_double:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001680 return static_cast<float_t>(m_float.convertToDouble());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001681 case e_long_double:
1682 llvm::APInt ldbl_val = m_float.bitcastToAPInt();
1683 return ldbl_val.bitsToFloat();
1684 }
1685 return fail_value;
1686}
1687
1688double Scalar::Double(double fail_value) const {
1689 switch (m_type) {
1690 case e_void:
1691 break;
1692 case e_sint:
1693 case e_uint:
1694 case e_slong:
1695 case e_ulong:
1696 case e_slonglong:
1697 case e_ulonglong:
1698 case e_sint128:
1699 case e_uint128:
1700 case e_sint256:
1701 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00001702 case e_sint512:
1703 case e_uint512:
Davide Italiano49d80282018-04-02 16:50:54 +00001704 return llvm::APIntOps::RoundAPIntToDouble(m_integer);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001705 case e_float:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001706 return static_cast<double_t>(m_float.convertToFloat());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001707 case e_double:
1708 return m_float.convertToDouble();
1709 case e_long_double:
1710 llvm::APInt ldbl_val = m_float.bitcastToAPInt();
1711 return ldbl_val.bitsToFloat();
1712 }
1713 return fail_value;
1714}
1715
1716long double Scalar::LongDouble(long double fail_value) const {
1717 switch (m_type) {
1718 case e_void:
1719 break;
1720 case e_sint:
1721 case e_uint:
1722 case e_slong:
1723 case e_ulong:
1724 case e_slonglong:
1725 case e_ulonglong:
1726 case e_sint128:
1727 case e_uint128:
1728 case e_sint256:
1729 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00001730 case e_sint512:
1731 case e_uint512:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001732 return static_cast<long_double_t>(
1733 llvm::APIntOps::RoundAPIntToDouble(m_integer));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001734 case e_float:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001735 return static_cast<long_double_t>(m_float.convertToFloat());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001736 case e_double:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001737 return static_cast<long_double_t>(m_float.convertToDouble());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001738 case e_long_double:
1739 llvm::APInt ldbl_val = m_float.bitcastToAPInt();
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00001740 return static_cast<long_double_t>(ldbl_val.bitsToDouble());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001741 }
1742 return fail_value;
1743}
1744
1745Scalar &Scalar::operator+=(const Scalar &rhs) {
1746 Scalar temp_value;
1747 const Scalar *a;
1748 const Scalar *b;
1749 if ((m_type = PromoteToMaxType(*this, rhs, temp_value, a, b)) !=
1750 Scalar::e_void) {
1751 switch (m_type) {
1752 case e_void:
1753 break;
Sagar Thakur8536fd12015-08-20 09:12:46 +00001754 case e_sint:
1755 case e_uint:
1756 case e_slong:
1757 case e_ulong:
1758 case e_slonglong:
1759 case e_ulonglong:
1760 case e_sint128:
1761 case e_uint128:
Enrico Granata391075c2016-03-10 00:14:29 +00001762 case e_sint256:
1763 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00001764 case e_sint512:
1765 case e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001766 m_integer = a->m_integer + b->m_integer;
1767 break;
Sagar Thakur8536fd12015-08-20 09:12:46 +00001768
Sagar Thakur8536fd12015-08-20 09:12:46 +00001769 case e_float:
1770 case e_double:
1771 case e_long_double:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001772 m_float = a->m_float + b->m_float;
1773 break;
Sagar Thakur8536fd12015-08-20 09:12:46 +00001774 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001775 }
1776 return *this;
Sagar Thakur8536fd12015-08-20 09:12:46 +00001777}
1778
Kate Stoneb9c1b512016-09-06 20:57:50 +00001779Scalar &Scalar::operator<<=(const Scalar &rhs) {
1780 switch (m_type) {
1781 case e_void:
1782 case e_float:
1783 case e_double:
1784 case e_long_double:
1785 m_type = e_void;
1786 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001787
Kate Stoneb9c1b512016-09-06 20:57:50 +00001788 case e_sint:
1789 case e_uint:
1790 case e_slong:
1791 case e_ulong:
1792 case e_slonglong:
1793 case e_ulonglong:
1794 case e_sint128:
1795 case e_uint128:
1796 case e_sint256:
1797 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00001798 case e_sint512:
1799 case e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001800 switch (rhs.m_type) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001801 case e_void:
1802 case e_float:
1803 case e_double:
1804 case e_long_double:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001805 m_type = e_void;
1806 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001807 case e_sint:
Pavel Labathbb467f62015-08-17 15:28:05 +00001808 case e_uint:
Pavel Labathbb467f62015-08-17 15:28:05 +00001809 case e_slong:
Pavel Labathbb467f62015-08-17 15:28:05 +00001810 case e_ulong:
Pavel Labathbb467f62015-08-17 15:28:05 +00001811 case e_slonglong:
Pavel Labathbb467f62015-08-17 15:28:05 +00001812 case e_ulonglong:
Sagar Thakur8536fd12015-08-20 09:12:46 +00001813 case e_sint128:
1814 case e_uint128:
Enrico Granata391075c2016-03-10 00:14:29 +00001815 case e_sint256:
1816 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00001817 case e_sint512:
1818 case e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001819 m_integer = m_integer << rhs.m_integer;
1820 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001821 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001822 break;
1823 }
1824 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001825}
1826
Kate Stoneb9c1b512016-09-06 20:57:50 +00001827bool Scalar::ShiftRightLogical(const Scalar &rhs) {
1828 switch (m_type) {
1829 case e_void:
1830 case e_float:
1831 case e_double:
1832 case e_long_double:
1833 m_type = e_void;
1834 break;
1835
1836 case e_sint:
1837 case e_uint:
1838 case e_slong:
1839 case e_ulong:
1840 case e_slonglong:
1841 case e_ulonglong:
1842 case e_sint128:
1843 case e_uint128:
1844 case e_sint256:
1845 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00001846 case e_sint512:
1847 case e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001848 switch (rhs.m_type) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001849 case e_void:
1850 case e_float:
1851 case e_double:
1852 case e_long_double:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001853 m_type = e_void;
1854 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001855 case e_sint:
1856 case e_uint:
Pavel Labathbb467f62015-08-17 15:28:05 +00001857 case e_slong:
1858 case e_ulong:
Pavel Labathbb467f62015-08-17 15:28:05 +00001859 case e_slonglong:
1860 case e_ulonglong:
Sagar Thakur8536fd12015-08-20 09:12:46 +00001861 case e_sint128:
1862 case e_uint128:
Enrico Granata391075c2016-03-10 00:14:29 +00001863 case e_sint256:
1864 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00001865 case e_sint512:
1866 case e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001867 m_integer = m_integer.lshr(rhs.m_integer);
1868 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001869 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001870 break;
1871 }
1872 return m_type != e_void;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001873}
1874
Kate Stoneb9c1b512016-09-06 20:57:50 +00001875Scalar &Scalar::operator>>=(const Scalar &rhs) {
1876 switch (m_type) {
1877 case e_void:
1878 case e_float:
1879 case e_double:
1880 case e_long_double:
1881 m_type = e_void;
1882 break;
1883
1884 case e_sint:
1885 case e_uint:
1886 case e_slong:
1887 case e_ulong:
1888 case e_slonglong:
1889 case e_ulonglong:
1890 case e_sint128:
1891 case e_uint128:
1892 case e_sint256:
1893 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00001894 case e_sint512:
1895 case e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001896 switch (rhs.m_type) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001897 case e_void:
1898 case e_float:
1899 case e_double:
1900 case e_long_double:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001901 m_type = e_void;
1902 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001903 case e_sint:
Pavel Labathbb467f62015-08-17 15:28:05 +00001904 case e_uint:
Pavel Labathbb467f62015-08-17 15:28:05 +00001905 case e_slong:
Pavel Labathbb467f62015-08-17 15:28:05 +00001906 case e_ulong:
Pavel Labathbb467f62015-08-17 15:28:05 +00001907 case e_slonglong:
Pavel Labathbb467f62015-08-17 15:28:05 +00001908 case e_ulonglong:
Sagar Thakur8536fd12015-08-20 09:12:46 +00001909 case e_sint128:
1910 case e_uint128:
Enrico Granata391075c2016-03-10 00:14:29 +00001911 case e_sint256:
1912 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00001913 case e_sint512:
1914 case e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001915 m_integer = m_integer.ashr(rhs.m_integer);
1916 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001917 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001918 break;
1919 }
1920 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001921}
1922
Kate Stoneb9c1b512016-09-06 20:57:50 +00001923Scalar &Scalar::operator&=(const Scalar &rhs) {
1924 switch (m_type) {
1925 case e_void:
1926 case e_float:
1927 case e_double:
1928 case e_long_double:
1929 m_type = e_void;
1930 break;
1931
1932 case e_sint:
1933 case e_uint:
1934 case e_slong:
1935 case e_ulong:
1936 case e_slonglong:
1937 case e_ulonglong:
1938 case e_sint128:
1939 case e_uint128:
1940 case e_sint256:
1941 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00001942 case e_sint512:
1943 case e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001944 switch (rhs.m_type) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001945 case e_void:
1946 case e_float:
1947 case e_double:
1948 case e_long_double:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001949 m_type = e_void;
1950 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001951 case e_sint:
Pavel Labathbb467f62015-08-17 15:28:05 +00001952 case e_uint:
Pavel Labathbb467f62015-08-17 15:28:05 +00001953 case e_slong:
Pavel Labathbb467f62015-08-17 15:28:05 +00001954 case e_ulong:
Pavel Labathbb467f62015-08-17 15:28:05 +00001955 case e_slonglong:
Pavel Labathbb467f62015-08-17 15:28:05 +00001956 case e_ulonglong:
Sagar Thakur8536fd12015-08-20 09:12:46 +00001957 case e_sint128:
1958 case e_uint128:
Enrico Granata391075c2016-03-10 00:14:29 +00001959 case e_sint256:
1960 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00001961 case e_sint512:
1962 case e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001963 m_integer &= rhs.m_integer;
1964 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001965 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001966 break;
1967 }
1968 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001969}
1970
Kate Stoneb9c1b512016-09-06 20:57:50 +00001971bool Scalar::AbsoluteValue() {
1972 switch (m_type) {
1973 case e_void:
1974 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001975
Kate Stoneb9c1b512016-09-06 20:57:50 +00001976 case e_sint:
1977 case e_slong:
1978 case e_slonglong:
1979 case e_sint128:
1980 case e_sint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00001981 case e_sint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001982 if (m_integer.isNegative())
1983 m_integer = -m_integer;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001984 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001985
1986 case e_uint:
1987 case e_ulong:
1988 case e_ulonglong:
1989 return true;
1990 case e_uint128:
1991 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00001992 case e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001993 case e_float:
1994 case e_double:
1995 case e_long_double:
1996 m_float.clearSign();
1997 return true;
1998 }
1999 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002000}
2001
Kate Stoneb9c1b512016-09-06 20:57:50 +00002002bool Scalar::UnaryNegate() {
2003 switch (m_type) {
2004 case e_void:
2005 break;
2006 case e_sint:
2007 case e_uint:
2008 case e_slong:
2009 case e_ulong:
2010 case e_slonglong:
2011 case e_ulonglong:
2012 case e_sint128:
2013 case e_uint128:
2014 case e_sint256:
2015 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00002016 case e_sint512:
2017 case e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002018 m_integer = -m_integer;
2019 return true;
2020 case e_float:
2021 case e_double:
2022 case e_long_double:
2023 m_float.changeSign();
2024 return true;
2025 }
2026 return false;
2027}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002028
Kate Stoneb9c1b512016-09-06 20:57:50 +00002029bool Scalar::OnesComplement() {
2030 switch (m_type) {
2031 case e_sint:
2032 case e_uint:
2033 case e_slong:
2034 case e_ulong:
2035 case e_slonglong:
2036 case e_ulonglong:
2037 case e_sint128:
2038 case e_uint128:
2039 case e_sint256:
2040 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00002041 case e_sint512:
2042 case e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002043 m_integer = ~m_integer;
2044 return true;
2045
2046 case e_void:
2047 case e_float:
2048 case e_double:
2049 case e_long_double:
2050 break;
2051 }
2052 return false;
2053}
2054
2055const Scalar lldb_private::operator+(const Scalar &lhs, const Scalar &rhs) {
2056 Scalar result;
2057 Scalar temp_value;
2058 const Scalar *a;
2059 const Scalar *b;
2060 if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
2061 Scalar::e_void) {
2062 switch (result.m_type) {
2063 case Scalar::e_void:
2064 break;
2065 case Scalar::e_sint:
2066 case Scalar::e_uint:
2067 case Scalar::e_slong:
2068 case Scalar::e_ulong:
2069 case Scalar::e_slonglong:
2070 case Scalar::e_ulonglong:
2071 case Scalar::e_sint128:
2072 case Scalar::e_uint128:
2073 case Scalar::e_sint256:
2074 case Scalar::e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00002075 case Scalar::e_sint512:
2076 case Scalar::e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002077 result.m_integer = a->m_integer + b->m_integer;
2078 break;
2079 case Scalar::e_float:
2080 case Scalar::e_double:
2081 case Scalar::e_long_double:
2082 result.m_float = a->m_float + b->m_float;
2083 break;
2084 }
2085 }
2086 return result;
2087}
2088
2089const Scalar lldb_private::operator-(const Scalar &lhs, const Scalar &rhs) {
2090 Scalar result;
2091 Scalar temp_value;
2092 const Scalar *a;
2093 const Scalar *b;
2094 if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
2095 Scalar::e_void) {
2096 switch (result.m_type) {
2097 case Scalar::e_void:
2098 break;
2099 case Scalar::e_sint:
2100 case Scalar::e_uint:
2101 case Scalar::e_slong:
2102 case Scalar::e_ulong:
2103 case Scalar::e_slonglong:
2104 case Scalar::e_ulonglong:
2105 case Scalar::e_sint128:
2106 case Scalar::e_uint128:
2107 case Scalar::e_sint256:
2108 case Scalar::e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00002109 case Scalar::e_sint512:
2110 case Scalar::e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002111 result.m_integer = a->m_integer - b->m_integer;
2112 break;
2113 case Scalar::e_float:
2114 case Scalar::e_double:
2115 case Scalar::e_long_double:
2116 result.m_float = a->m_float - b->m_float;
2117 break;
2118 }
2119 }
2120 return result;
2121}
2122
2123const Scalar lldb_private::operator/(const Scalar &lhs, const Scalar &rhs) {
2124 Scalar result;
2125 Scalar temp_value;
2126 const Scalar *a;
2127 const Scalar *b;
2128 if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
2129 Scalar::e_void) {
2130 switch (result.m_type) {
2131 case Scalar::e_void:
2132 break;
Sagar Thakur8536fd12015-08-20 09:12:46 +00002133 case Scalar::e_sint:
2134 case Scalar::e_slong:
2135 case Scalar::e_slonglong:
2136 case Scalar::e_sint128:
Enrico Granata391075c2016-03-10 00:14:29 +00002137 case Scalar::e_sint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00002138 case Scalar::e_sint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002139 if (b->m_integer != 0) {
2140 result.m_integer = a->m_integer.sdiv(b->m_integer);
2141 return result;
2142 }
2143 break;
Sagar Thakur8536fd12015-08-20 09:12:46 +00002144 case Scalar::e_uint:
2145 case Scalar::e_ulong:
2146 case Scalar::e_ulonglong:
2147 case Scalar::e_uint128:
Enrico Granata391075c2016-03-10 00:14:29 +00002148 case Scalar::e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00002149 case Scalar::e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002150 if (b->m_integer != 0) {
2151 result.m_integer = a->m_integer.udiv(b->m_integer);
2152 return result;
2153 }
2154 break;
Sagar Thakur8536fd12015-08-20 09:12:46 +00002155 case Scalar::e_float:
2156 case Scalar::e_double:
2157 case Scalar::e_long_double:
Davide Italiano01c33b82018-03-27 18:37:54 +00002158 if (!b->m_float.isZero()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002159 result.m_float = a->m_float / b->m_float;
2160 return result;
2161 }
2162 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002163 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002164 }
2165 // For division only, the only way it should make it here is if a promotion
Adrian Prantl05097242018-04-30 16:49:04 +00002166 // failed, or if we are trying to do a divide by zero.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002167 result.m_type = Scalar::e_void;
2168 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002169}
2170
Kate Stoneb9c1b512016-09-06 20:57:50 +00002171const Scalar lldb_private::operator*(const Scalar &lhs, const Scalar &rhs) {
2172 Scalar result;
2173 Scalar temp_value;
2174 const Scalar *a;
2175 const Scalar *b;
2176 if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
2177 Scalar::e_void) {
2178 switch (result.m_type) {
2179 case Scalar::e_void:
2180 break;
2181 case Scalar::e_sint:
2182 case Scalar::e_uint:
2183 case Scalar::e_slong:
2184 case Scalar::e_ulong:
2185 case Scalar::e_slonglong:
2186 case Scalar::e_ulonglong:
2187 case Scalar::e_sint128:
2188 case Scalar::e_uint128:
2189 case Scalar::e_sint256:
2190 case Scalar::e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00002191 case Scalar::e_sint512:
2192 case Scalar::e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002193 result.m_integer = a->m_integer * b->m_integer;
2194 break;
2195 case Scalar::e_float:
2196 case Scalar::e_double:
2197 case Scalar::e_long_double:
2198 result.m_float = a->m_float * b->m_float;
2199 break;
2200 }
2201 }
2202 return result;
2203}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002204
Kate Stoneb9c1b512016-09-06 20:57:50 +00002205const Scalar lldb_private::operator&(const Scalar &lhs, const Scalar &rhs) {
2206 Scalar result;
2207 Scalar temp_value;
2208 const Scalar *a;
2209 const Scalar *b;
2210 if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
2211 Scalar::e_void) {
2212 switch (result.m_type) {
2213 case Scalar::e_sint:
2214 case Scalar::e_uint:
2215 case Scalar::e_slong:
2216 case Scalar::e_ulong:
2217 case Scalar::e_slonglong:
2218 case Scalar::e_ulonglong:
2219 case Scalar::e_sint128:
2220 case Scalar::e_uint128:
2221 case Scalar::e_sint256:
2222 case Scalar::e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00002223 case Scalar::e_sint512:
2224 case Scalar::e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002225 result.m_integer = a->m_integer & b->m_integer;
2226 break;
2227 case Scalar::e_void:
2228 case Scalar::e_float:
2229 case Scalar::e_double:
2230 case Scalar::e_long_double:
2231 // No bitwise AND on floats, doubles of long doubles
2232 result.m_type = Scalar::e_void;
2233 break;
2234 }
2235 }
2236 return result;
2237}
2238
2239const Scalar lldb_private::operator|(const Scalar &lhs, const Scalar &rhs) {
2240 Scalar result;
2241 Scalar temp_value;
2242 const Scalar *a;
2243 const Scalar *b;
2244 if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
2245 Scalar::e_void) {
2246 switch (result.m_type) {
2247 case Scalar::e_sint:
2248 case Scalar::e_uint:
2249 case Scalar::e_slong:
2250 case Scalar::e_ulong:
2251 case Scalar::e_slonglong:
2252 case Scalar::e_ulonglong:
2253 case Scalar::e_sint128:
2254 case Scalar::e_uint128:
2255 case Scalar::e_sint256:
2256 case Scalar::e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00002257 case Scalar::e_sint512:
2258 case Scalar::e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002259 result.m_integer = a->m_integer | b->m_integer;
2260 break;
2261
2262 case Scalar::e_void:
2263 case Scalar::e_float:
2264 case Scalar::e_double:
2265 case Scalar::e_long_double:
2266 // No bitwise AND on floats, doubles of long doubles
2267 result.m_type = Scalar::e_void;
2268 break;
2269 }
2270 }
2271 return result;
2272}
2273
2274const Scalar lldb_private::operator%(const Scalar &lhs, const Scalar &rhs) {
2275 Scalar result;
2276 Scalar temp_value;
2277 const Scalar *a;
2278 const Scalar *b;
2279 if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
2280 Scalar::e_void) {
2281 switch (result.m_type) {
2282 default:
2283 break;
2284 case Scalar::e_void:
2285 break;
Sagar Thakur8536fd12015-08-20 09:12:46 +00002286 case Scalar::e_sint:
2287 case Scalar::e_slong:
2288 case Scalar::e_slonglong:
2289 case Scalar::e_sint128:
Enrico Granata391075c2016-03-10 00:14:29 +00002290 case Scalar::e_sint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00002291 case Scalar::e_sint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002292 if (b->m_integer != 0) {
2293 result.m_integer = a->m_integer.srem(b->m_integer);
2294 return result;
2295 }
2296 break;
Sagar Thakur8536fd12015-08-20 09:12:46 +00002297 case Scalar::e_uint:
2298 case Scalar::e_ulong:
2299 case Scalar::e_ulonglong:
2300 case Scalar::e_uint128:
Enrico Granata391075c2016-03-10 00:14:29 +00002301 case Scalar::e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00002302 case Scalar::e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002303 if (b->m_integer != 0) {
2304 result.m_integer = a->m_integer.urem(b->m_integer);
2305 return result;
2306 }
2307 break;
2308 }
2309 }
2310 result.m_type = Scalar::e_void;
2311 return result;
2312}
2313
2314const Scalar lldb_private::operator^(const Scalar &lhs, const Scalar &rhs) {
2315 Scalar result;
2316 Scalar temp_value;
2317 const Scalar *a;
2318 const Scalar *b;
2319 if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
2320 Scalar::e_void) {
2321 switch (result.m_type) {
2322 case Scalar::e_sint:
2323 case Scalar::e_uint:
2324 case Scalar::e_slong:
2325 case Scalar::e_ulong:
2326 case Scalar::e_slonglong:
2327 case Scalar::e_ulonglong:
2328 case Scalar::e_sint128:
2329 case Scalar::e_uint128:
2330 case Scalar::e_sint256:
2331 case Scalar::e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00002332 case Scalar::e_sint512:
2333 case Scalar::e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002334 result.m_integer = a->m_integer ^ b->m_integer;
2335 break;
2336
2337 case Scalar::e_void:
Sagar Thakur8536fd12015-08-20 09:12:46 +00002338 case Scalar::e_float:
2339 case Scalar::e_double:
2340 case Scalar::e_long_double:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002341 // No bitwise AND on floats, doubles of long doubles
2342 result.m_type = Scalar::e_void;
2343 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002344 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002345 }
2346 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002347}
2348
Kate Stoneb9c1b512016-09-06 20:57:50 +00002349const Scalar lldb_private::operator<<(const Scalar &lhs, const Scalar &rhs) {
2350 Scalar result = lhs;
2351 result <<= rhs;
2352 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002353}
2354
Kate Stoneb9c1b512016-09-06 20:57:50 +00002355const Scalar lldb_private::operator>>(const Scalar &lhs, const Scalar &rhs) {
2356 Scalar result = lhs;
2357 result >>= rhs;
2358 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002359}
2360
Zachary Turner97206d52017-05-12 04:51:55 +00002361Status Scalar::SetValueFromCString(const char *value_str, Encoding encoding,
2362 size_t byte_size) {
2363 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002364 if (value_str == nullptr || value_str[0] == '\0') {
2365 error.SetErrorString("Invalid c-string value string.");
2366 return error;
2367 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002368 switch (encoding) {
2369 case eEncodingInvalid:
2370 error.SetErrorString("Invalid encoding.");
2371 break;
2372
2373 case eEncodingUint:
Pavel Labath61547252018-06-19 17:24:03 +00002374 if (byte_size <= sizeof(uint64_t)) {
2375 uint64_t uval64;
2376 if (!llvm::to_integer(value_str, uval64))
Kate Stoneb9c1b512016-09-06 20:57:50 +00002377 error.SetErrorStringWithFormat(
2378 "'%s' is not a valid unsigned integer string value", value_str);
2379 else if (!UIntValueIsValidForSize(uval64, byte_size))
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002380 error.SetErrorStringWithFormat(
2381 "value 0x%" PRIx64 " is too large to fit in a %" PRIu64
2382 " byte unsigned integer value",
2383 uval64, static_cast<uint64_t>(byte_size));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002384 else {
2385 m_type = Scalar::GetValueTypeForUnsignedIntegerWithByteSize(byte_size);
2386 switch (m_type) {
2387 case e_uint:
2388 m_integer = llvm::APInt(sizeof(uint_t) * 8, uval64, false);
2389 break;
2390 case e_ulong:
2391 m_integer = llvm::APInt(sizeof(ulong_t) * 8, uval64, false);
2392 break;
2393 case e_ulonglong:
2394 m_integer = llvm::APInt(sizeof(ulonglong_t) * 8, uval64, false);
2395 break;
2396 default:
2397 error.SetErrorStringWithFormat(
2398 "unsupported unsigned integer byte size: %" PRIu64 "",
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002399 static_cast<uint64_t>(byte_size));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002400 break;
2401 }
2402 }
2403 } else {
2404 error.SetErrorStringWithFormat(
2405 "unsupported unsigned integer byte size: %" PRIu64 "",
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002406 static_cast<uint64_t>(byte_size));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002407 return error;
Sagar Thakur8536fd12015-08-20 09:12:46 +00002408 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002409 break;
2410
2411 case eEncodingSint:
Pavel Labath61547252018-06-19 17:24:03 +00002412 if (byte_size <= sizeof(int64_t)) {
2413 int64_t sval64;
2414 if (!llvm::to_integer(value_str, sval64))
Kate Stoneb9c1b512016-09-06 20:57:50 +00002415 error.SetErrorStringWithFormat(
2416 "'%s' is not a valid signed integer string value", value_str);
2417 else if (!SIntValueIsValidForSize(sval64, byte_size))
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002418 error.SetErrorStringWithFormat(
2419 "value 0x%" PRIx64 " is too large to fit in a %" PRIu64
2420 " byte signed integer value",
2421 sval64, static_cast<uint64_t>(byte_size));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002422 else {
2423 m_type = Scalar::GetValueTypeForSignedIntegerWithByteSize(byte_size);
2424 switch (m_type) {
2425 case e_sint:
2426 m_integer = llvm::APInt(sizeof(sint_t) * 8, sval64, true);
2427 break;
2428 case e_slong:
2429 m_integer = llvm::APInt(sizeof(slong_t) * 8, sval64, true);
2430 break;
2431 case e_slonglong:
2432 m_integer = llvm::APInt(sizeof(slonglong_t) * 8, sval64, true);
2433 break;
2434 default:
2435 error.SetErrorStringWithFormat(
2436 "unsupported signed integer byte size: %" PRIu64 "",
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002437 static_cast<uint64_t>(byte_size));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002438 break;
2439 }
2440 }
2441 } else {
2442 error.SetErrorStringWithFormat(
2443 "unsupported signed integer byte size: %" PRIu64 "",
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002444 static_cast<uint64_t>(byte_size));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002445 return error;
2446 }
2447 break;
2448
2449 case eEncodingIEEE754:
2450 static float f_val;
2451 static double d_val;
2452 static long double l_val;
2453 if (byte_size == sizeof(float)) {
2454 if (::sscanf(value_str, "%f", &f_val) == 1) {
2455 m_float = llvm::APFloat(f_val);
2456 m_type = e_float;
2457 } else
2458 error.SetErrorStringWithFormat("'%s' is not a valid float string value",
2459 value_str);
2460 } else if (byte_size == sizeof(double)) {
2461 if (::sscanf(value_str, "%lf", &d_val) == 1) {
2462 m_float = llvm::APFloat(d_val);
2463 m_type = e_double;
2464 } else
2465 error.SetErrorStringWithFormat("'%s' is not a valid float string value",
2466 value_str);
2467 } else if (byte_size == sizeof(long double)) {
2468 if (::sscanf(value_str, "%Lf", &l_val) == 1) {
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002469 m_float = llvm::APFloat(
2470 llvm::APFloat::x87DoubleExtended(),
2471 llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
2472 (reinterpret_cast<type128 *>(&l_val))->x));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002473 m_type = e_long_double;
2474 } else
2475 error.SetErrorStringWithFormat("'%s' is not a valid float string value",
2476 value_str);
2477 } else {
2478 error.SetErrorStringWithFormat("unsupported float byte size: %" PRIu64 "",
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002479 static_cast<uint64_t>(byte_size));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002480 return error;
2481 }
2482 break;
2483
2484 case eEncodingVector:
2485 error.SetErrorString("vector encoding unsupported.");
2486 break;
2487 }
2488 if (error.Fail())
2489 m_type = e_void;
2490
2491 return error;
Sagar Thakur8536fd12015-08-20 09:12:46 +00002492}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002493
Zachary Turner97206d52017-05-12 04:51:55 +00002494Status Scalar::SetValueFromData(DataExtractor &data, lldb::Encoding encoding,
2495 size_t byte_size) {
2496 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002497
2498 type128 int128;
2499 type256 int256;
2500 switch (encoding) {
2501 case lldb::eEncodingInvalid:
2502 error.SetErrorString("invalid encoding");
2503 break;
2504 case lldb::eEncodingVector:
2505 error.SetErrorString("vector encoding unsupported");
2506 break;
2507 case lldb::eEncodingUint: {
2508 lldb::offset_t offset = 0;
2509
2510 switch (byte_size) {
2511 case 1:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002512 operator=(data.GetU8(&offset));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002513 break;
2514 case 2:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002515 operator=(data.GetU16(&offset));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002516 break;
2517 case 4:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002518 operator=(data.GetU32(&offset));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002519 break;
2520 case 8:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002521 operator=(data.GetU64(&offset));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002522 break;
2523 case 16:
2524 if (data.GetByteOrder() == eByteOrderBig) {
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002525 int128.x[1] = data.GetU64(&offset);
2526 int128.x[0] = data.GetU64(&offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002527 } else {
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002528 int128.x[0] = data.GetU64(&offset);
2529 int128.x[1] = data.GetU64(&offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002530 }
2531 operator=(llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128, int128.x));
2532 break;
2533 case 32:
2534 if (data.GetByteOrder() == eByteOrderBig) {
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002535 int256.x[3] = data.GetU64(&offset);
2536 int256.x[2] = data.GetU64(&offset);
2537 int256.x[1] = data.GetU64(&offset);
2538 int256.x[0] = data.GetU64(&offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002539 } else {
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002540 int256.x[0] = data.GetU64(&offset);
2541 int256.x[1] = data.GetU64(&offset);
2542 int256.x[2] = data.GetU64(&offset);
2543 int256.x[3] = data.GetU64(&offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002544 }
2545 operator=(llvm::APInt(BITWIDTH_INT256, NUM_OF_WORDS_INT256, int256.x));
2546 break;
2547 default:
2548 error.SetErrorStringWithFormat(
2549 "unsupported unsigned integer byte size: %" PRIu64 "",
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002550 static_cast<uint64_t>(byte_size));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002551 break;
Sagar Thakur8536fd12015-08-20 09:12:46 +00002552 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002553 } break;
2554 case lldb::eEncodingSint: {
2555 lldb::offset_t offset = 0;
2556
2557 switch (byte_size) {
2558 case 1:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002559 operator=(static_cast<int8_t>(data.GetU8(&offset)));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002560 break;
2561 case 2:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002562 operator=(static_cast<int16_t>(data.GetU16(&offset)));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002563 break;
2564 case 4:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002565 operator=(static_cast<int32_t>(data.GetU32(&offset)));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002566 break;
2567 case 8:
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002568 operator=(static_cast<int64_t>(data.GetU64(&offset)));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002569 break;
2570 case 16:
2571 if (data.GetByteOrder() == eByteOrderBig) {
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002572 int128.x[1] = data.GetU64(&offset);
2573 int128.x[0] = data.GetU64(&offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002574 } else {
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002575 int128.x[0] = data.GetU64(&offset);
2576 int128.x[1] = data.GetU64(&offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002577 }
2578 operator=(llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128, int128.x));
2579 break;
2580 case 32:
2581 if (data.GetByteOrder() == eByteOrderBig) {
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002582 int256.x[3] = data.GetU64(&offset);
2583 int256.x[2] = data.GetU64(&offset);
2584 int256.x[1] = data.GetU64(&offset);
2585 int256.x[0] = data.GetU64(&offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002586 } else {
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002587 int256.x[0] = data.GetU64(&offset);
2588 int256.x[1] = data.GetU64(&offset);
2589 int256.x[2] = data.GetU64(&offset);
2590 int256.x[3] = data.GetU64(&offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002591 }
2592 operator=(llvm::APInt(BITWIDTH_INT256, NUM_OF_WORDS_INT256, int256.x));
2593 break;
2594 default:
2595 error.SetErrorStringWithFormat(
2596 "unsupported signed integer byte size: %" PRIu64 "",
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002597 static_cast<uint64_t>(byte_size));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002598 break;
2599 }
2600 } break;
2601 case lldb::eEncodingIEEE754: {
2602 lldb::offset_t offset = 0;
2603
2604 if (byte_size == sizeof(float))
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002605 operator=(data.GetFloat(&offset));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002606 else if (byte_size == sizeof(double))
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002607 operator=(data.GetDouble(&offset));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002608 else if (byte_size == sizeof(long double))
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002609 operator=(data.GetLongDouble(&offset));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002610 else
2611 error.SetErrorStringWithFormat("unsupported float byte size: %" PRIu64 "",
Jonas Devlieghere24374ae2019-05-23 05:12:11 +00002612 static_cast<uint64_t>(byte_size));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002613 } break;
2614 }
2615
2616 return error;
Sagar Thakur8536fd12015-08-20 09:12:46 +00002617}
Oleksiy Vyalov9dcdd2e2015-08-10 21:49:50 +00002618
Kate Stoneb9c1b512016-09-06 20:57:50 +00002619bool Scalar::SignExtend(uint32_t sign_bit_pos) {
2620 const uint32_t max_bit_pos = GetByteSize() * 8;
2621
2622 if (sign_bit_pos < max_bit_pos) {
2623 switch (m_type) {
2624 case Scalar::e_void:
2625 case Scalar::e_float:
2626 case Scalar::e_double:
2627 case Scalar::e_long_double:
2628 return false;
2629
2630 case Scalar::e_sint:
2631 case Scalar::e_uint:
2632 case Scalar::e_slong:
2633 case Scalar::e_ulong:
2634 case Scalar::e_slonglong:
2635 case Scalar::e_ulonglong:
2636 case Scalar::e_sint128:
2637 case Scalar::e_uint128:
2638 case Scalar::e_sint256:
2639 case Scalar::e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00002640 case Scalar::e_sint512:
2641 case Scalar::e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002642 if (max_bit_pos == sign_bit_pos)
2643 return true;
2644 else if (sign_bit_pos < (max_bit_pos - 1)) {
Sean Callananac3254a2017-04-20 18:07:51 +00002645 llvm::APInt sign_bit = llvm::APInt::getSignMask(sign_bit_pos + 1);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002646 llvm::APInt bitwize_and = m_integer & sign_bit;
2647 if (bitwize_and.getBoolValue()) {
2648 const llvm::APInt mask =
2649 ~(sign_bit) + llvm::APInt(m_integer.getBitWidth(), 1);
2650 m_integer |= mask;
2651 }
2652 return true;
2653 }
2654 break;
2655 }
2656 }
2657 return false;
2658}
2659
2660size_t Scalar::GetAsMemoryData(void *dst, size_t dst_len,
2661 lldb::ByteOrder dst_byte_order,
Zachary Turner97206d52017-05-12 04:51:55 +00002662 Status &error) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002663 // Get a data extractor that points to the native scalar data
2664 DataExtractor data;
2665 if (!GetData(data)) {
2666 error.SetErrorString("invalid scalar value");
2667 return 0;
2668 }
2669
2670 const size_t src_len = data.GetByteSize();
2671
2672 // Prepare a memory buffer that contains some or all of the register value
2673 const size_t bytes_copied =
2674 data.CopyByteOrderedData(0, // src offset
2675 src_len, // src length
2676 dst, // dst buffer
2677 dst_len, // dst length
2678 dst_byte_order); // dst byte order
2679 if (bytes_copied == 0)
2680 error.SetErrorString("failed to copy data");
2681
2682 return bytes_copied;
2683}
2684
2685bool Scalar::ExtractBitfield(uint32_t bit_size, uint32_t bit_offset) {
2686 if (bit_size == 0)
2687 return true;
2688
2689 switch (m_type) {
2690 case Scalar::e_void:
2691 case Scalar::e_float:
2692 case Scalar::e_double:
2693 case Scalar::e_long_double:
2694 break;
2695
2696 case Scalar::e_sint:
2697 case Scalar::e_slong:
2698 case Scalar::e_slonglong:
2699 case Scalar::e_sint128:
2700 case Scalar::e_sint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00002701 case Scalar::e_sint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002702 m_integer = m_integer.ashr(bit_offset)
2703 .sextOrTrunc(bit_size)
2704 .sextOrSelf(8 * GetByteSize());
2705 return true;
2706
2707 case Scalar::e_uint:
2708 case Scalar::e_ulong:
2709 case Scalar::e_ulonglong:
2710 case Scalar::e_uint128:
2711 case Scalar::e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00002712 case Scalar::e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002713 m_integer = m_integer.lshr(bit_offset)
2714 .zextOrTrunc(bit_size)
2715 .zextOrSelf(8 * GetByteSize());
2716 return true;
2717 }
2718 return false;
2719}
2720
2721bool lldb_private::operator==(const Scalar &lhs, const Scalar &rhs) {
2722 // If either entry is void then we can just compare the types
2723 if (lhs.m_type == Scalar::e_void || rhs.m_type == Scalar::e_void)
2724 return lhs.m_type == rhs.m_type;
2725
2726 Scalar temp_value;
2727 const Scalar *a;
2728 const Scalar *b;
2729 llvm::APFloat::cmpResult result;
2730 switch (PromoteToMaxType(lhs, rhs, temp_value, a, b)) {
2731 case Scalar::e_void:
2732 break;
2733 case Scalar::e_sint:
2734 case Scalar::e_uint:
2735 case Scalar::e_slong:
2736 case Scalar::e_ulong:
2737 case Scalar::e_slonglong:
2738 case Scalar::e_ulonglong:
2739 case Scalar::e_sint128:
2740 case Scalar::e_uint128:
2741 case Scalar::e_sint256:
2742 case Scalar::e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00002743 case Scalar::e_sint512:
2744 case Scalar::e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002745 return a->m_integer == b->m_integer;
2746 case Scalar::e_float:
2747 case Scalar::e_double:
2748 case Scalar::e_long_double:
2749 result = a->m_float.compare(b->m_float);
2750 if (result == llvm::APFloat::cmpEqual)
2751 return true;
2752 }
2753 return false;
2754}
2755
2756bool lldb_private::operator!=(const Scalar &lhs, const Scalar &rhs) {
Davide Italiano18a0ce92018-12-21 22:42:00 +00002757 return !(lhs == rhs);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002758}
2759
2760bool lldb_private::operator<(const Scalar &lhs, const Scalar &rhs) {
2761 if (lhs.m_type == Scalar::e_void || rhs.m_type == Scalar::e_void)
2762 return false;
2763
2764 Scalar temp_value;
2765 const Scalar *a;
2766 const Scalar *b;
2767 llvm::APFloat::cmpResult result;
2768 switch (PromoteToMaxType(lhs, rhs, temp_value, a, b)) {
2769 case Scalar::e_void:
2770 break;
2771 case Scalar::e_sint:
2772 case Scalar::e_slong:
2773 case Scalar::e_slonglong:
2774 case Scalar::e_sint128:
2775 case Scalar::e_sint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00002776 case Scalar::e_sint512:
2777 case Scalar::e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002778 return a->m_integer.slt(b->m_integer);
2779 case Scalar::e_uint:
2780 case Scalar::e_ulong:
2781 case Scalar::e_ulonglong:
2782 case Scalar::e_uint128:
2783 case Scalar::e_uint256:
2784 return a->m_integer.ult(b->m_integer);
2785 case Scalar::e_float:
2786 case Scalar::e_double:
2787 case Scalar::e_long_double:
2788 result = a->m_float.compare(b->m_float);
2789 if (result == llvm::APFloat::cmpLessThan)
2790 return true;
2791 }
2792 return false;
2793}
2794
2795bool lldb_private::operator<=(const Scalar &lhs, const Scalar &rhs) {
Davide Italianoff92a1a2019-01-04 19:23:52 +00002796 return !(rhs < lhs);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002797}
2798
2799bool lldb_private::operator>(const Scalar &lhs, const Scalar &rhs) {
Davide Italianoff92a1a2019-01-04 19:23:52 +00002800 return rhs < lhs;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002801}
2802
2803bool lldb_private::operator>=(const Scalar &lhs, const Scalar &rhs) {
Davide Italianoff92a1a2019-01-04 19:23:52 +00002804 return !(lhs < rhs);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002805}
2806
2807bool Scalar::ClearBit(uint32_t bit) {
2808 switch (m_type) {
2809 case e_void:
2810 break;
2811 case e_sint:
2812 case e_uint:
2813 case e_slong:
2814 case e_ulong:
2815 case e_slonglong:
2816 case e_ulonglong:
2817 case e_sint128:
2818 case e_uint128:
2819 case e_sint256:
2820 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00002821 case e_sint512:
2822 case e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002823 m_integer.clearBit(bit);
2824 return true;
2825 case e_float:
2826 case e_double:
2827 case e_long_double:
2828 break;
2829 }
2830 return false;
2831}
2832
2833bool Scalar::SetBit(uint32_t bit) {
2834 switch (m_type) {
2835 case e_void:
2836 break;
2837 case e_sint:
2838 case e_uint:
2839 case e_slong:
2840 case e_ulong:
2841 case e_slonglong:
2842 case e_ulonglong:
2843 case e_sint128:
2844 case e_uint128:
2845 case e_sint256:
2846 case e_uint256:
Davide Italiano51d46bd2019-01-30 18:05:36 +00002847 case e_sint512:
2848 case e_uint512:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002849 m_integer.setBit(bit);
2850 return true;
2851 case e_float:
2852 case e_double:
2853 case e_long_double:
2854 break;
2855 }
2856 return false;
2857}
Pavel Labathb07a7992019-04-29 10:55:22 +00002858
2859llvm::raw_ostream &lldb_private::operator<<(llvm::raw_ostream &os, const Scalar &scalar) {
2860 StreamString s;
2861 scalar.GetValue(&s, /*show_type*/ true);
2862 return os << s.GetString();
2863}