Bug Summary

File:tools/lldb/source/Core/Scalar.cpp
Warning:line 219, column 5
Address of stack memory associated with local variable 'ldbl_val' returned to caller

Annotated Source Code

1//===-- Scalar.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Core/Scalar.h"
11
12#include "lldb/Host/StringConvert.h"
13#include "lldb/Utility/DataExtractor.h"
14#include "lldb/Utility/Endian.h"
15#include "lldb/Utility/Error.h"
16#include "lldb/Utility/Stream.h"
17#include "lldb/lldb-types.h" // for offset_t
18
19#include "llvm/ADT/SmallString.h"
20
21#include <cinttypes>
22#include <cstdio>
23
24using namespace lldb;
25using namespace lldb_private;
26
27//----------------------------------------------------------------------
28// Promote to max type currently follows the ANSI C rule for type
29// promotion in expressions.
30//----------------------------------------------------------------------
31static Scalar::Type PromoteToMaxType(
32 const Scalar &lhs, // The const left hand side object
33 const Scalar &rhs, // The const right hand side object
34 Scalar &temp_value, // A modifiable temp value than can be used to hold
35 // either the promoted lhs or rhs object
36 const Scalar *&promoted_lhs_ptr, // Pointer to the resulting possibly
37 // promoted value of lhs (at most one of
38 // lhs/rhs will get promoted)
39 const Scalar *&promoted_rhs_ptr // Pointer to the resulting possibly
40 // promoted value of rhs (at most one of
41 // lhs/rhs will get promoted)
42 ) {
43 Scalar result;
44 // Initialize the promoted values for both the right and left hand side values
45 // to be the objects themselves. If no promotion is needed (both right and
46 // left
47 // have the same type), then the temp_value will not get used.
48 promoted_lhs_ptr = &lhs;
49 promoted_rhs_ptr = &rhs;
50 // Extract the types of both the right and left hand side values
51 Scalar::Type lhs_type = lhs.GetType();
52 Scalar::Type rhs_type = rhs.GetType();
53
54 if (lhs_type > rhs_type) {
55 // Right hand side need to be promoted
56 temp_value = rhs; // Copy right hand side into the temp value
57 if (temp_value.Promote(lhs_type)) // Promote it
58 promoted_rhs_ptr =
59 &temp_value; // Update the pointer for the promoted right hand side
60 } else if (lhs_type < rhs_type) {
61 // Left hand side need to be promoted
62 temp_value = lhs; // Copy left hand side value into the temp value
63 if (temp_value.Promote(rhs_type)) // Promote it
64 promoted_lhs_ptr =
65 &temp_value; // Update the pointer for the promoted left hand side
66 }
67
68 // Make sure our type promotion worked as expected
69 if (promoted_lhs_ptr->GetType() == promoted_rhs_ptr->GetType())
70 return promoted_lhs_ptr->GetType(); // Return the resulting max type
71
72 // Return the void type (zero) if we fail to promote either of the values.
73 return Scalar::e_void;
74}
75
76Scalar::Scalar() : m_type(e_void), m_float((float)0) {}
77
78Scalar::Scalar(const Scalar &rhs)
79 : m_type(rhs.m_type), m_integer(rhs.m_integer), m_float(rhs.m_float) {}
80
81// Scalar::Scalar(const RegisterValue& reg) :
82// m_type(e_void),
83// m_data()
84//{
85// switch (reg.info.encoding)
86// {
87// case eEncodingUint: // unsigned integer
88// switch (reg.info.byte_size)
89// {
90// case 1: m_type = e_uint; m_data.uint = reg.value.uint8; break;
91// case 2: m_type = e_uint; m_data.uint = reg.value.uint16; break;
92// case 4: m_type = e_uint; m_data.uint = reg.value.uint32; break;
93// case 8: m_type = e_ulonglong; m_data.ulonglong = reg.value.uint64;
94// break;
95// break;
96// }
97// break;
98//
99// case eEncodingSint: // signed integer
100// switch (reg.info.byte_size)
101// {
102// case 1: m_type = e_sint; m_data.sint = reg.value.sint8; break;
103// case 2: m_type = e_sint; m_data.sint = reg.value.sint16; break;
104// case 4: m_type = e_sint; m_data.sint = reg.value.sint32; break;
105// case 8: m_type = e_slonglong; m_data.slonglong = reg.value.sint64;
106// break;
107// break;
108// }
109// break;
110//
111// case eEncodingIEEE754: // float
112// switch (reg.info.byte_size)
113// {
114// case 4: m_type = e_float; m_data.flt = reg.value.float32; break;
115// case 8: m_type = e_double; m_data.dbl = reg.value.float64; break;
116// break;
117// }
118// break;
119// case eEncodingVector: // vector registers
120// break;
121// }
122//}
123
124bool Scalar::GetData(DataExtractor &data, size_t limit_byte_size) const {
125 size_t byte_size = GetByteSize();
126 if (byte_size > 0) {
2
Taking true branch
127 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(GetBytes());
3
Calling 'Scalar::GetBytes'
128
129 if (limit_byte_size < byte_size) {
130 if (endian::InlHostByteOrder() == eByteOrderLittle) {
131 // On little endian systems if we want fewer bytes from the
132 // current type we just specify fewer bytes since the LSByte
133 // is first...
134 byte_size = limit_byte_size;
135 } else if (endian::InlHostByteOrder() == eByteOrderBig) {
136 // On big endian systems if we want fewer bytes from the
137 // current type have to advance our initial byte pointer and
138 // trim down the number of bytes since the MSByte is first
139 bytes += byte_size - limit_byte_size;
140 byte_size = limit_byte_size;
141 }
142 }
143
144 data.SetData(bytes, byte_size, endian::InlHostByteOrder());
145 return true;
146 }
147 data.Clear();
148 return false;
149}
150
151const void *Scalar::GetBytes() const {
152 const uint64_t *apint_words;
153 const uint8_t *bytes;
154 static float_t flt_val;
155 static double_t dbl_val;
156 static uint64_t swapped_words[4];
157 switch (m_type) {
4
Control jumps to 'case e_long_double:' at line 208
158 case e_void:
159 break;
160 case e_sint:
161 case e_uint:
162 case e_slong:
163 case e_ulong:
164 case e_slonglong:
165 case e_ulonglong:
166 bytes = reinterpret_cast<const uint8_t *>(m_integer.getRawData());
167 // getRawData always returns a pointer to an uint64_t. If we have a smaller
168 // type,
169 // we need to update the pointer on big-endian systems.
170 if (endian::InlHostByteOrder() == eByteOrderBig) {
171 size_t byte_size = m_integer.getBitWidth() / 8;
172 if (byte_size < 8)
173 bytes += 8 - byte_size;
174 }
175 return bytes;
176 case e_sint128:
177 case e_uint128:
178 apint_words = m_integer.getRawData();
179 // getRawData always returns a pointer to an array of two uint64_t values,
180 // where the least-significant word always comes first. On big-endian
181 // systems we need to swap the two words.
182 if (endian::InlHostByteOrder() == eByteOrderBig) {
183 swapped_words[0] = apint_words[1];
184 swapped_words[1] = apint_words[0];
185 apint_words = swapped_words;
186 }
187 return reinterpret_cast<const void *>(apint_words);
188 case e_sint256:
189 case e_uint256:
190 apint_words = m_integer.getRawData();
191 // getRawData always returns a pointer to an array of four uint64_t values,
192 // where the least-significant word always comes first. On big-endian
193 // systems we need to swap the four words.
194 if (endian::InlHostByteOrder() == eByteOrderBig) {
195 swapped_words[0] = apint_words[3];
196 swapped_words[1] = apint_words[2];
197 swapped_words[2] = apint_words[1];
198 swapped_words[3] = apint_words[0];
199 apint_words = swapped_words;
200 }
201 return reinterpret_cast<const void *>(apint_words);
202 case e_float:
203 flt_val = m_float.convertToFloat();
204 return reinterpret_cast<const void *>(&flt_val);
205 case e_double:
206 dbl_val = m_float.convertToDouble();
207 return reinterpret_cast<const void *>(&dbl_val);
208 case e_long_double:
209 llvm::APInt ldbl_val = m_float.bitcastToAPInt();
210 apint_words = ldbl_val.getRawData();
211 // getRawData always returns a pointer to an array of two uint64_t values,
212 // where the least-significant word always comes first. On big-endian
213 // systems we need to swap the two words.
214 if (endian::InlHostByteOrder() == eByteOrderBig) {
5
Taking false branch
215 swapped_words[0] = apint_words[1];
216 swapped_words[1] = apint_words[0];
217 apint_words = swapped_words;
218 }
219 return reinterpret_cast<const void *>(apint_words);
6
Address of stack memory associated with local variable 'ldbl_val' returned to caller
220 }
221 return nullptr;
222}
223
224size_t Scalar::GetByteSize() const {
225 switch (m_type) {
226 case e_void:
227 break;
228 case e_sint:
229 case e_uint:
230 case e_slong:
231 case e_ulong:
232 case e_slonglong:
233 case e_ulonglong:
234 case e_sint128:
235 case e_uint128:
236 case e_sint256:
237 case e_uint256:
238 return (m_integer.getBitWidth() / 8);
239 case e_float:
240 return sizeof(float_t);
241 case e_double:
242 return sizeof(double_t);
243 case e_long_double:
244 return sizeof(long_double_t);
245 }
246 return 0;
247}
248
249bool Scalar::IsZero() const {
250 llvm::APInt zero_int = llvm::APInt::getNullValue(m_integer.getBitWidth() / 8);
251 switch (m_type) {
252 case e_void:
253 break;
254 case e_sint:
255 case e_uint:
256 case e_slong:
257 case e_ulong:
258 case e_slonglong:
259 case e_ulonglong:
260 case e_sint128:
261 case e_uint128:
262 case e_sint256:
263 case e_uint256:
264 return llvm::APInt::isSameValue(zero_int, m_integer);
265 case e_float:
266 case e_double:
267 case e_long_double:
268 return m_float.isZero();
269 }
270 return false;
271}
272
273void Scalar::GetValue(Stream *s, bool show_type) const {
274 if (show_type)
275 s->Printf("(%s) ", GetTypeAsCString());
276
277 switch (m_type) {
278 case e_void:
279 break;
280 case e_sint:
281 case e_slong:
282 case e_slonglong:
283 case e_sint128:
284 case e_sint256:
285 s->PutCString(m_integer.toString(10, true));
286 break;
287 case e_uint:
288 case e_ulong:
289 case e_ulonglong:
290 case e_uint128:
291 case e_uint256:
292 s->PutCString(m_integer.toString(10, false));
293 break;
294 case e_float:
295 case e_double:
296 case e_long_double:
297 llvm::SmallString<24> string;
298 m_float.toString(string);
299 s->Printf("%s", string.c_str());
300 break;
301 }
302}
303
304const char *Scalar::GetTypeAsCString() const {
305 switch (m_type) {
306 case e_void:
307 return "void";
308 case e_sint:
309 return "int";
310 case e_uint:
311 return "unsigned int";
312 case e_slong:
313 return "long";
314 case e_ulong:
315 return "unsigned long";
316 case e_slonglong:
317 return "long long";
318 case e_ulonglong:
319 return "unsigned long long";
320 case e_sint128:
321 return "int128_t";
322 case e_uint128:
323 return "unsigned int128_t";
324 case e_sint256:
325 return "int256_t";
326 case e_uint256:
327 return "unsigned int256_t";
328 case e_float:
329 return "float";
330 case e_double:
331 return "double";
332 case e_long_double:
333 return "long double";
334 }
335 return "<invalid Scalar type>";
336}
337
338Scalar &Scalar::operator=(const Scalar &rhs) {
339 if (this != &rhs) {
340 m_type = rhs.m_type;
341 m_integer = llvm::APInt(rhs.m_integer);
342 m_float = rhs.m_float;
343 }
344 return *this;
345}
346
347Scalar &Scalar::operator=(const int v) {
348 m_type = e_sint;
349 m_integer = llvm::APInt(sizeof(int) * 8, v, true);
350 return *this;
351}
352
353Scalar &Scalar::operator=(unsigned int v) {
354 m_type = e_uint;
355 m_integer = llvm::APInt(sizeof(int) * 8, v);
356 return *this;
357}
358
359Scalar &Scalar::operator=(long v) {
360 m_type = e_slong;
361 m_integer = llvm::APInt(sizeof(long) * 8, v, true);
362 return *this;
363}
364
365Scalar &Scalar::operator=(unsigned long v) {
366 m_type = e_ulong;
367 m_integer = llvm::APInt(sizeof(long) * 8, v);
368 return *this;
369}
370
371Scalar &Scalar::operator=(long long v) {
372 m_type = e_slonglong;
373 m_integer = llvm::APInt(sizeof(long) * 8, v, true);
374 return *this;
375}
376
377Scalar &Scalar::operator=(unsigned long long v) {
378 m_type = e_ulonglong;
379 m_integer = llvm::APInt(sizeof(long long) * 8, v);
380 return *this;
381}
382
383Scalar &Scalar::operator=(float v) {
384 m_type = e_float;
385 m_float = llvm::APFloat(v);
386 return *this;
387}
388
389Scalar &Scalar::operator=(double v) {
390 m_type = e_double;
391 m_float = llvm::APFloat(v);
392 return *this;
393}
394
395Scalar &Scalar::operator=(long double v) {
396 m_type = e_long_double;
397 if (m_ieee_quad)
398 m_float = llvm::APFloat(
399 llvm::APFloat::IEEEquad(),
400 llvm::APInt(BITWIDTH_INT128128, NUM_OF_WORDS_INT1282, ((type128 *)&v)->x));
401 else
402 m_float = llvm::APFloat(
403 llvm::APFloat::x87DoubleExtended(),
404 llvm::APInt(BITWIDTH_INT128128, NUM_OF_WORDS_INT1282, ((type128 *)&v)->x));
405 return *this;
406}
407
408Scalar &Scalar::operator=(llvm::APInt rhs) {
409 m_integer = llvm::APInt(rhs);
410 switch (m_integer.getBitWidth()) {
411 case 8:
412 case 16:
413 case 32:
414 if (m_integer.isSignedIntN(sizeof(sint_t) * 8))
415 m_type = e_sint;
416 else
417 m_type = e_uint;
418 break;
419 case 64:
420 if (m_integer.isSignedIntN(sizeof(slonglong_t) * 8))
421 m_type = e_slonglong;
422 else
423 m_type = e_ulonglong;
424 break;
425 case 128:
426 if (m_integer.isSignedIntN(BITWIDTH_INT128128))
427 m_type = e_sint128;
428 else
429 m_type = e_uint128;
430 break;
431 case 256:
432 if (m_integer.isSignedIntN(BITWIDTH_INT256256))
433 m_type = e_sint256;
434 else
435 m_type = e_uint256;
436 break;
437 }
438 return *this;
439}
440
441Scalar::~Scalar() = default;
442
443bool Scalar::Promote(Scalar::Type type) {
444 bool success = false;
445 switch (m_type) {
446 case e_void:
447 break;
448
449 case e_sint:
450 switch (type) {
451 case e_void:
452 break;
453 case e_sint:
454 success = true;
455 break;
456 case e_uint:
457 m_integer = m_integer.sextOrTrunc(sizeof(uint_t) * 8);
458 success = true;
459 break;
460
461 case e_slong:
462 m_integer = m_integer.sextOrTrunc(sizeof(slong_t) * 8);
463 success = true;
464 break;
465
466 case e_ulong:
467 m_integer = m_integer.sextOrTrunc(sizeof(ulong_t) * 8);
468 success = true;
469 break;
470
471 case e_slonglong:
472 m_integer = m_integer.sextOrTrunc(sizeof(slonglong_t) * 8);
473 success = true;
474 break;
475
476 case e_ulonglong:
477 m_integer = m_integer.sextOrTrunc(sizeof(ulonglong_t) * 8);
478 success = true;
479 break;
480
481 case e_sint128:
482 case e_uint128:
483 m_integer = m_integer.sextOrTrunc(BITWIDTH_INT128128);
484 success = true;
485 break;
486
487 case e_sint256:
488 case e_uint256:
489 m_integer = m_integer.sextOrTrunc(BITWIDTH_INT256256);
490 success = true;
491 break;
492
493 case e_float:
494 m_float = llvm::APFloat(m_integer.bitsToFloat());
495 success = true;
496 break;
497
498 case e_double:
499 m_float = llvm::APFloat(m_integer.bitsToDouble());
500 success = true;
501 break;
502
503 case e_long_double:
504 if (m_ieee_quad)
505 m_float = llvm::APFloat(llvm::APFloat::IEEEquad(), m_integer);
506 else
507 m_float = llvm::APFloat(llvm::APFloat::x87DoubleExtended(), m_integer);
508 success = true;
509 break;
510 }
511 break;
512
513 case e_uint:
514 switch (type) {
515 case e_void:
516 case e_sint:
517 break;
518 case e_uint:
519 success = true;
520 break;
521 case e_slong:
522 m_integer = m_integer.zextOrTrunc(sizeof(slong_t) * 8);
523 success = true;
524 break;
525
526 case e_ulong:
527 m_integer = m_integer.zextOrTrunc(sizeof(ulong_t) * 8);
528 success = true;
529 break;
530
531 case e_slonglong:
532 m_integer = m_integer.zextOrTrunc(sizeof(slonglong_t) * 8);
533 success = true;
534 break;
535
536 case e_ulonglong:
537 m_integer = m_integer.zextOrTrunc(sizeof(ulonglong_t) * 8);
538 success = true;
539 break;
540
541 case e_sint128:
542 case e_uint128:
543 m_integer = m_integer.zextOrTrunc(BITWIDTH_INT128128);
544 success = true;
545 break;
546
547 case e_sint256:
548 case e_uint256:
549 m_integer = m_integer.zextOrTrunc(BITWIDTH_INT256256);
550 success = true;
551 break;
552
553 case e_float:
554 m_float = llvm::APFloat(m_integer.bitsToFloat());
555 success = true;
556 break;
557
558 case e_double:
559 m_float = llvm::APFloat(m_integer.bitsToDouble());
560 success = true;
561 break;
562
563 case e_long_double:
564 if (m_ieee_quad)
565 m_float = llvm::APFloat(llvm::APFloat::IEEEquad(), m_integer);
566 else
567 m_float = llvm::APFloat(llvm::APFloat::x87DoubleExtended(), m_integer);
568 success = true;
569 break;
570 }
571 break;
572
573 case e_slong:
574 switch (type) {
575 case e_void:
576 case e_sint:
577 case e_uint:
578 break;
579 case e_slong:
580 success = true;
581 break;
582 case e_ulong:
583 m_integer = m_integer.sextOrTrunc(sizeof(ulong_t) * 8);
584 success = true;
585 break;
586
587 case e_slonglong:
588 m_integer = m_integer.sextOrTrunc(sizeof(slonglong_t) * 8);
589 success = true;
590 break;
591
592 case e_ulonglong:
593 m_integer = m_integer.sextOrTrunc(sizeof(ulonglong_t) * 8);
594 success = true;
595 break;
596
597 case e_sint128:
598 case e_uint128:
599 m_integer = m_integer.sextOrTrunc(BITWIDTH_INT128128);
600 success = true;
601 break;
602
603 case e_sint256:
604 case e_uint256:
605 m_integer = m_integer.sextOrTrunc(BITWIDTH_INT256256);
606 success = true;
607 break;
608
609 case e_float:
610 m_float = llvm::APFloat(m_integer.bitsToFloat());
611 success = true;
612 break;
613
614 case e_double:
615 m_float = llvm::APFloat(m_integer.bitsToDouble());
616 success = true;
617 break;
618
619 case e_long_double:
620 if (m_ieee_quad)
621 m_float = llvm::APFloat(llvm::APFloat::IEEEquad(), m_integer);
622 else
623 m_float = llvm::APFloat(llvm::APFloat::x87DoubleExtended(), m_integer);
624 success = true;
625 break;
626 }
627 break;
628
629 case e_ulong:
630 switch (type) {
631 case e_void:
632 case e_sint:
633 case e_uint:
634 case e_slong:
635 break;
636 case e_ulong:
637 success = true;
638 break;
639 case e_slonglong:
640 m_integer = m_integer.zextOrTrunc(sizeof(slonglong_t) * 8);
641 success = true;
642 break;
643
644 case e_ulonglong:
645 m_integer = m_integer.zextOrTrunc(sizeof(ulonglong_t) * 8);
646 success = true;
647 break;
648
649 case e_sint128:
650 case e_uint128:
651 m_integer = m_integer.zextOrTrunc(BITWIDTH_INT128128);
652 success = true;
653 break;
654
655 case e_sint256:
656 case e_uint256:
657 m_integer = m_integer.zextOrTrunc(BITWIDTH_INT256256);
658 success = true;
659 break;
660
661 case e_float:
662 m_float = llvm::APFloat(m_integer.bitsToFloat());
663 success = true;
664 break;
665
666 case e_double:
667 m_float = llvm::APFloat(m_integer.bitsToDouble());
668 success = true;
669 break;
670
671 case e_long_double:
672 if (m_ieee_quad)
673 m_float = llvm::APFloat(llvm::APFloat::IEEEquad(), m_integer);
674 else
675 m_float = llvm::APFloat(llvm::APFloat::x87DoubleExtended(), m_integer);
676 success = true;
677 break;
678 }
679 break;
680
681 case e_slonglong:
682 switch (type) {
683 case e_void:
684 case e_sint:
685 case e_uint:
686 case e_slong:
687 case e_ulong:
688 break;
689 case e_slonglong:
690 success = true;
691 break;
692 case e_ulonglong:
693 m_integer = m_integer.sextOrTrunc(sizeof(ulonglong_t) * 8);
694 success = true;
695 break;
696
697 case e_sint128:
698 case e_uint128:
699 m_integer = m_integer.sextOrTrunc(BITWIDTH_INT128128);
700 success = true;
701 break;
702
703 case e_sint256:
704 case e_uint256:
705 m_integer = m_integer.sextOrTrunc(BITWIDTH_INT256256);
706 success = true;
707 break;
708
709 case e_float:
710 m_float = llvm::APFloat(m_integer.bitsToFloat());
711 success = true;
712 break;
713
714 case e_double:
715 m_float = llvm::APFloat(m_integer.bitsToDouble());
716 success = true;
717 break;
718
719 case e_long_double:
720 if (m_ieee_quad)
721 m_float = llvm::APFloat(llvm::APFloat::IEEEquad(), m_integer);
722 else
723 m_float = llvm::APFloat(llvm::APFloat::x87DoubleExtended(), m_integer);
724 success = true;
725 break;
726 }
727 break;
728
729 case e_ulonglong:
730 switch (type) {
731 case e_void:
732 case e_sint:
733 case e_uint:
734 case e_slong:
735 case e_ulong:
736 case e_slonglong:
737 break;
738 case e_ulonglong:
739 success = true;
740 break;
741 case e_sint128:
742 case e_uint128:
743 m_integer = m_integer.zextOrTrunc(BITWIDTH_INT128128);
744 success = true;
745 break;
746
747 case e_sint256:
748 case e_uint256:
749 m_integer = m_integer.zextOrTrunc(BITWIDTH_INT256256);
750 success = true;
751 break;
752
753 case e_float:
754 m_float = llvm::APFloat(m_integer.bitsToFloat());
755 success = true;
756 break;
757
758 case e_double:
759 m_float = llvm::APFloat(m_integer.bitsToDouble());
760 success = true;
761 break;
762
763 case e_long_double:
764 if (m_ieee_quad)
765 m_float = llvm::APFloat(llvm::APFloat::IEEEquad(), m_integer);
766 else
767 m_float = llvm::APFloat(llvm::APFloat::x87DoubleExtended(), m_integer);
768 success = true;
769 break;
770 }
771 break;
772
773 case e_sint128:
774 switch (type) {
775 case e_void:
776 case e_sint:
777 case e_uint:
778 case e_slong:
779 case e_ulong:
780 case e_slonglong:
781 case e_ulonglong:
782 break;
783 case e_sint128:
784 success = true;
785 break;
786 case e_uint128:
787 m_integer = m_integer.sextOrTrunc(BITWIDTH_INT128128);
788 success = true;
789 break;
790
791 case e_sint256:
792 case e_uint256:
793 m_integer = m_integer.sextOrTrunc(BITWIDTH_INT256256);
794 success = true;
795 break;
796
797 case e_float:
798 m_float = llvm::APFloat(m_integer.bitsToFloat());
799 success = true;
800 break;
801
802 case e_double:
803 m_float = llvm::APFloat(m_integer.bitsToDouble());
804 success = true;
805 break;
806
807 case e_long_double:
808 if (m_ieee_quad)
809 m_float = llvm::APFloat(llvm::APFloat::IEEEquad(), m_integer);
810 else
811 m_float = llvm::APFloat(llvm::APFloat::x87DoubleExtended(), m_integer);
812 success = true;
813 break;
814 }
815 break;
816
817 case e_uint128:
818 switch (type) {
819 case e_void:
820 case e_sint:
821 case e_uint:
822 case e_slong:
823 case e_ulong:
824 case e_slonglong:
825 case e_ulonglong:
826 case e_sint128:
827 break;
828 case e_uint128:
829 success = true;
830 break;
831 case e_sint256:
832 case e_uint256:
833 m_integer = m_integer.zextOrTrunc(BITWIDTH_INT256256);
834 success = true;
835 break;
836
837 case e_float:
838 m_float = llvm::APFloat(m_integer.bitsToFloat());
839 success = true;
840 break;
841
842 case e_double:
843 m_float = llvm::APFloat(m_integer.bitsToDouble());
844 success = true;
845 break;
846
847 case e_long_double:
848 if (m_ieee_quad)
849 m_float = llvm::APFloat(llvm::APFloat::IEEEquad(), m_integer);
850 else
851 m_float = llvm::APFloat(llvm::APFloat::x87DoubleExtended(), m_integer);
852 success = true;
853 break;
854 }
855 break;
856
857 case e_sint256:
858 switch (type) {
859 case e_void:
860 case e_sint:
861 case e_uint:
862 case e_slong:
863 case e_ulong:
864 case e_slonglong:
865 case e_ulonglong:
866 case e_sint128:
867 case e_uint128:
868 break;
869 case e_sint256:
870 success = true;
871 break;
872 case e_uint256:
873 m_integer = m_integer.sextOrTrunc(BITWIDTH_INT256256);
874 success = true;
875 break;
876
877 case e_float:
878 m_float = llvm::APFloat(m_integer.bitsToFloat());
879 success = true;
880 break;
881
882 case e_double:
883 m_float = llvm::APFloat(m_integer.bitsToDouble());
884 success = true;
885 break;
886
887 case e_long_double:
888 if (m_ieee_quad)
889 m_float = llvm::APFloat(llvm::APFloat::IEEEquad(), m_integer);
890 else
891 m_float = llvm::APFloat(llvm::APFloat::x87DoubleExtended(), m_integer);
892 success = true;
893 break;
894 }
895 break;
896
897 case e_uint256:
898 switch (type) {
899 case e_void:
900 case e_sint:
901 case e_uint:
902 case e_slong:
903 case e_ulong:
904 case e_slonglong:
905 case e_ulonglong:
906 case e_sint128:
907 case e_uint128:
908 case e_sint256:
909 break;
910 case e_uint256:
911 success = true;
912 break;
913 case e_float:
914 m_float = llvm::APFloat(m_integer.bitsToFloat());
915 success = true;
916 break;
917
918 case e_double:
919 m_float = llvm::APFloat(m_integer.bitsToDouble());
920 success = true;
921 break;
922
923 case e_long_double:
924 if (m_ieee_quad)
925 m_float = llvm::APFloat(llvm::APFloat::IEEEquad(), m_integer);
926 else
927 m_float = llvm::APFloat(llvm::APFloat::x87DoubleExtended(), m_integer);
928 success = true;
929 break;
930 }
931 break;
932
933 case e_float:
934 switch (type) {
935 case e_void:
936 case e_sint:
937 case e_uint:
938 case e_slong:
939 case e_ulong:
940 case e_slonglong:
941 case e_ulonglong:
942 case e_sint128:
943 case e_uint128:
944 case e_sint256:
945 case e_uint256:
946 break;
947 case e_float:
948 success = true;
949 break;
950 case e_double:
951 m_float = llvm::APFloat((float_t)m_float.convertToFloat());
952 success = true;
953 break;
954
955 case e_long_double:
956 if (m_ieee_quad)
957 m_float =
958 llvm::APFloat(llvm::APFloat::IEEEquad(), m_float.bitcastToAPInt());
959 else
960 m_float = llvm::APFloat(llvm::APFloat::x87DoubleExtended(),
961 m_float.bitcastToAPInt());
962 success = true;
963 break;
964 }
965 break;
966
967 case e_double:
968 switch (type) {
969 case e_void:
970 case e_sint:
971 case e_uint:
972 case e_slong:
973 case e_ulong:
974 case e_slonglong:
975 case e_ulonglong:
976 case e_sint128:
977 case e_uint128:
978 case e_sint256:
979 case e_uint256:
980 case e_float:
981 break;
982 case e_double:
983 success = true;
984 break;
985 case e_long_double:
986 if (m_ieee_quad)
987 m_float =
988 llvm::APFloat(llvm::APFloat::IEEEquad(), m_float.bitcastToAPInt());
989 else
990 m_float = llvm::APFloat(llvm::APFloat::x87DoubleExtended(),
991 m_float.bitcastToAPInt());
992 success = true;
993 break;
994 }
995 break;
996
997 case e_long_double:
998 switch (type) {
999 case e_void:
1000 case e_sint:
1001 case e_uint:
1002 case e_slong:
1003 case e_ulong:
1004 case e_slonglong:
1005 case e_ulonglong:
1006 case e_sint128:
1007 case e_uint128:
1008 case e_sint256:
1009 case e_uint256:
1010 case e_float:
1011 case e_double:
1012 break;
1013 case e_long_double:
1014 success = true;
1015 break;
1016 }
1017 break;
1018 }
1019
1020 if (success)
1021 m_type = type;
1022 return success;
1023}
1024
1025const char *Scalar::GetValueTypeAsCString(Scalar::Type type) {
1026 switch (type) {
1027 case e_void:
1028 return "void";
1029 case e_sint:
1030 return "int";
1031 case e_uint:
1032 return "unsigned int";
1033 case e_slong:
1034 return "long";
1035 case e_ulong:
1036 return "unsigned long";
1037 case e_slonglong:
1038 return "long long";
1039 case e_ulonglong:
1040 return "unsigned long long";
1041 case e_float:
1042 return "float";
1043 case e_double:
1044 return "double";
1045 case e_long_double:
1046 return "long double";
1047 case e_sint128:
1048 return "int128_t";
1049 case e_uint128:
1050 return "uint128_t";
1051 case e_sint256:
1052 return "int256_t";
1053 case e_uint256:
1054 return "uint256_t";
1055 }
1056 return "???";
1057}
1058
1059Scalar::Type
1060Scalar::GetValueTypeForSignedIntegerWithByteSize(size_t byte_size) {
1061 if (byte_size <= sizeof(sint_t))
1062 return e_sint;
1063 if (byte_size <= sizeof(slong_t))
1064 return e_slong;
1065 if (byte_size <= sizeof(slonglong_t))
1066 return e_slonglong;
1067 return e_void;
1068}
1069
1070Scalar::Type
1071Scalar::GetValueTypeForUnsignedIntegerWithByteSize(size_t byte_size) {
1072 if (byte_size <= sizeof(uint_t))
1073 return e_uint;
1074 if (byte_size <= sizeof(ulong_t))
1075 return e_ulong;
1076 if (byte_size <= sizeof(ulonglong_t))
1077 return e_ulonglong;
1078 return e_void;
1079}
1080
1081Scalar::Type Scalar::GetValueTypeForFloatWithByteSize(size_t byte_size) {
1082 if (byte_size == sizeof(float_t))
1083 return e_float;
1084 if (byte_size == sizeof(double_t))
1085 return e_double;
1086 if (byte_size == sizeof(long_double_t))
1087 return e_long_double;
1088 return e_void;
1089}
1090
1091bool Scalar::Cast(Scalar::Type type) {
1092 bool success = false;
1093 switch (m_type) {
1094 case e_void:
1095 break;
1096
1097 case e_sint:
1098 case e_uint:
1099 case e_slong:
1100 case e_ulong:
1101 case e_slonglong:
1102 case e_ulonglong:
1103 case e_sint128:
1104 case e_uint128:
1105 case e_sint256:
1106 case e_uint256:
1107 switch (type) {
1108 case e_void:
1109 break;
1110 case e_sint:
1111 m_integer = m_integer.sextOrTrunc(sizeof(sint_t) * 8);
1112 success = true;
1113 break;
1114
1115 case e_uint:
1116 m_integer = m_integer.zextOrTrunc(sizeof(sint_t) * 8);
1117 success = true;
1118 break;
1119
1120 case e_slong:
1121 m_integer = m_integer.sextOrTrunc(sizeof(slong_t) * 8);
1122 success = true;
1123 break;
1124
1125 case e_ulong:
1126 m_integer = m_integer.zextOrTrunc(sizeof(slong_t) * 8);
1127 success = true;
1128 break;
1129
1130 case e_slonglong:
1131 m_integer = m_integer.sextOrTrunc(sizeof(slonglong_t) * 8);
1132 success = true;
1133 break;
1134
1135 case e_ulonglong:
1136 m_integer = m_integer.zextOrTrunc(sizeof(slonglong_t) * 8);
1137 success = true;
1138 break;
1139
1140 case e_sint128:
1141 m_integer = m_integer.sextOrTrunc(BITWIDTH_INT128128);
1142 success = true;
1143 break;
1144
1145 case e_uint128:
1146 m_integer = m_integer.zextOrTrunc(BITWIDTH_INT128128);
1147 success = true;
1148 break;
1149
1150 case e_sint256:
1151 m_integer = m_integer.sextOrTrunc(BITWIDTH_INT256256);
1152 success = true;
1153 break;
1154
1155 case e_uint256:
1156 m_integer = m_integer.zextOrTrunc(BITWIDTH_INT256256);
1157 success = true;
1158 break;
1159
1160 case e_float:
1161 m_float = llvm::APFloat(m_integer.bitsToFloat());
1162 success = true;
1163 break;
1164
1165 case e_double:
1166 m_float = llvm::APFloat(m_integer.bitsToDouble());
1167 success = true;
1168 break;
1169
1170 case e_long_double:
1171 if (m_ieee_quad)
1172 m_float = llvm::APFloat(llvm::APFloat::IEEEquad(), m_integer);
1173 else
1174 m_float = llvm::APFloat(llvm::APFloat::x87DoubleExtended(), m_integer);
1175 success = true;
1176 break;
1177 }
1178 break;
1179
1180 case e_float:
1181 switch (type) {
1182 case e_void:
1183 break;
1184 case e_sint:
1185 case e_uint:
1186 case e_slong:
1187 case e_ulong:
1188 case e_slonglong:
1189 case e_ulonglong:
1190 case e_sint128:
1191 case e_uint128:
1192 case e_sint256:
1193 case e_uint256:
1194 m_integer = m_float.bitcastToAPInt();
1195 success = true;
1196 break;
1197 case e_float:
1198 m_float = llvm::APFloat(m_float.convertToFloat());
1199 success = true;
1200 break;
1201 case e_double:
1202 m_float = llvm::APFloat(m_float.convertToFloat());
1203 success = true;
1204 break;
1205 case e_long_double:
1206 if (m_ieee_quad)
1207 m_float =
1208 llvm::APFloat(llvm::APFloat::IEEEquad(), m_float.bitcastToAPInt());
1209 else
1210 m_float = llvm::APFloat(llvm::APFloat::x87DoubleExtended(),
1211 m_float.bitcastToAPInt());
1212 success = true;
1213 break;
1214 }
1215 break;
1216
1217 case e_double:
1218 switch (type) {
1219 case e_void:
1220 break;
1221 case e_sint:
1222 case e_uint:
1223 case e_slong:
1224 case e_ulong:
1225 case e_slonglong:
1226 case e_ulonglong:
1227 case e_sint128:
1228 case e_uint128:
1229 case e_sint256:
1230 case e_uint256:
1231 m_integer = m_float.bitcastToAPInt();
1232 success = true;
1233 break;
1234 case e_float:
1235 m_float = llvm::APFloat(m_float.convertToDouble());
1236 success = true;
1237 break;
1238 case e_double:
1239 m_float = llvm::APFloat(m_float.convertToDouble());
1240 success = true;
1241 break;
1242 case e_long_double:
1243 if (m_ieee_quad)
1244 m_float =
1245 llvm::APFloat(llvm::APFloat::IEEEquad(), m_float.bitcastToAPInt());
1246 else
1247 m_float = llvm::APFloat(llvm::APFloat::x87DoubleExtended(),
1248 m_float.bitcastToAPInt());
1249 success = true;
1250 break;
1251 }
1252 break;
1253
1254 case e_long_double:
1255 switch (type) {
1256 case e_void:
1257 break;
1258 case e_sint:
1259 m_integer = m_float.bitcastToAPInt();
1260 m_integer = m_integer.sextOrTrunc(sizeof(sint_t) * 8);
1261 success = true;
1262 break;
1263
1264 case e_uint:
1265 m_integer = m_float.bitcastToAPInt();
1266 m_integer = m_integer.zextOrTrunc(sizeof(sint_t) * 8);
1267 success = true;
1268 break;
1269
1270 case e_slong:
1271 m_integer = m_float.bitcastToAPInt();
1272 m_integer = m_integer.sextOrTrunc(sizeof(slong_t) * 8);
1273 success = true;
1274 break;
1275
1276 case e_ulong:
1277 m_integer = m_float.bitcastToAPInt();
1278 m_integer = m_integer.zextOrTrunc(sizeof(slong_t) * 8);
1279 success = true;
1280 break;
1281
1282 case e_slonglong:
1283 m_integer = m_float.bitcastToAPInt();
1284 m_integer = m_integer.sextOrTrunc(sizeof(slonglong_t) * 8);
1285 success = true;
1286 break;
1287
1288 case e_ulonglong:
1289 m_integer = m_float.bitcastToAPInt();
1290 m_integer = m_integer.zextOrTrunc(sizeof(slonglong_t) * 8);
1291 success = true;
1292 break;
1293
1294 case e_sint128:
1295 m_integer = m_float.bitcastToAPInt();
1296 m_integer = m_integer.sextOrTrunc(BITWIDTH_INT128128);
1297 success = true;
1298 break;
1299
1300 case e_uint128:
1301 m_integer = m_float.bitcastToAPInt();
1302 m_integer = m_integer.zextOrTrunc(BITWIDTH_INT128128);
1303 success = true;
1304 break;
1305
1306 case e_sint256:
1307 m_integer = m_float.bitcastToAPInt();
1308 m_integer = m_integer.sextOrTrunc(BITWIDTH_INT256256);
1309 success = true;
1310 break;
1311
1312 case e_uint256:
1313 m_integer = m_float.bitcastToAPInt();
1314 m_integer = m_integer.zextOrTrunc(BITWIDTH_INT256256);
1315 success = true;
1316 break;
1317
1318 case e_float:
1319 m_float = llvm::APFloat(m_float.convertToFloat());
1320 success = true;
1321 break;
1322 case e_double:
1323 m_float = llvm::APFloat(m_float.convertToFloat());
1324 success = true;
1325 break;
1326 case e_long_double:
1327 success = true;
1328 break;
1329 }
1330 break;
1331 }
1332
1333 if (success)
1334 m_type = type;
1335 return success;
1336}
1337
1338bool Scalar::MakeSigned() {
1339 bool success = false;
1340
1341 switch (m_type) {
1342 case e_void:
1343 break;
1344 case e_sint:
1345 success = true;
1346 break;
1347 case e_uint:
1348 m_type = e_sint;
1349 success = true;
1350 break;
1351 case e_slong:
1352 success = true;
1353 break;
1354 case e_ulong:
1355 m_type = e_slong;
1356 success = true;
1357 break;
1358 case e_slonglong:
1359 success = true;
1360 break;
1361 case e_ulonglong:
1362 m_type = e_slonglong;
1363 success = true;
1364 break;
1365 case e_sint128:
1366 success = true;
1367 break;
1368 case e_uint128:
1369 m_type = e_sint128;
1370 success = true;
1371 break;
1372 case e_sint256:
1373 success = true;
1374 break;
1375 case e_uint256:
1376 m_type = e_sint256;
1377 success = true;
1378 break;
1379 case e_float:
1380 success = true;
1381 break;
1382 case e_double:
1383 success = true;
1384 break;
1385 case e_long_double:
1386 success = true;
1387 break;
1388 }
1389
1390 return success;
1391}
1392
1393bool Scalar::MakeUnsigned() {
1394 bool success = false;
1395
1396 switch (m_type) {
1397 case e_void:
1398 break;
1399 case e_sint:
1400 success = true;
1401 break;
1402 case e_uint:
1403 m_type = e_uint;
1404 success = true;
1405 break;
1406 case e_slong:
1407 success = true;
1408 break;
1409 case e_ulong:
1410 m_type = e_ulong;
1411 success = true;
1412 break;
1413 case e_slonglong:
1414 success = true;
1415 break;
1416 case e_ulonglong:
1417 m_type = e_ulonglong;
1418 success = true;
1419 break;
1420 case e_sint128:
1421 success = true;
1422 break;
1423 case e_uint128:
1424 m_type = e_uint128;
1425 success = true;
1426 break;
1427 case e_sint256:
1428 success = true;
1429 break;
1430 case e_uint256:
1431 m_type = e_uint256;
1432 success = true;
1433 break;
1434 case e_float:
1435 success = true;
1436 break;
1437 case e_double:
1438 success = true;
1439 break;
1440 case e_long_double:
1441 success = true;
1442 break;
1443 }
1444
1445 return success;
1446}
1447
1448signed char Scalar::SChar(char fail_value) const {
1449 switch (m_type) {
1450 case e_void:
1451 break;
1452 case e_sint:
1453 case e_uint:
1454 case e_slong:
1455 case e_ulong:
1456 case e_slonglong:
1457 case e_ulonglong:
1458 case e_sint128:
1459 case e_uint128:
1460 case e_sint256:
1461 case e_uint256:
1462 return (schar_t)(m_integer.sextOrTrunc(sizeof(schar_t) * 8)).getSExtValue();
1463 case e_float:
1464 return (schar_t)m_float.convertToFloat();
1465 case e_double:
1466 return (schar_t)m_float.convertToDouble();
1467 case e_long_double:
1468 llvm::APInt ldbl_val = m_float.bitcastToAPInt();
1469 return (schar_t)(ldbl_val.sextOrTrunc(sizeof(schar_t) * 8)).getSExtValue();
1470 }
1471 return fail_value;
1472}
1473
1474unsigned char Scalar::UChar(unsigned char fail_value) const {
1475 switch (m_type) {
1476 case e_void:
1477 break;
1478 case e_sint:
1479 case e_uint:
1480 case e_slong:
1481 case e_ulong:
1482 case e_slonglong:
1483 case e_ulonglong:
1484 case e_sint128:
1485 case e_uint128:
1486 case e_sint256:
1487 case e_uint256:
1488 return (uchar_t)(m_integer.zextOrTrunc(sizeof(uchar_t) * 8)).getZExtValue();
1489 case e_float:
1490 return (uchar_t)m_float.convertToFloat();
1491 case e_double:
1492 return (uchar_t)m_float.convertToDouble();
1493 case e_long_double:
1494 llvm::APInt ldbl_val = m_float.bitcastToAPInt();
1495 return (uchar_t)(ldbl_val.zextOrTrunc(sizeof(uchar_t) * 8)).getZExtValue();
1496 }
1497 return fail_value;
1498}
1499
1500short Scalar::SShort(short fail_value) const {
1501 switch (m_type) {
1502 case e_void:
1503 break;
1504 case e_sint:
1505 case e_uint:
1506 case e_slong:
1507 case e_ulong:
1508 case e_slonglong:
1509 case e_ulonglong:
1510 case e_sint128:
1511 case e_uint128:
1512 case e_sint256:
1513 case e_uint256:
1514 return (sshort_t)(m_integer.sextOrTrunc(sizeof(sshort_t) * 8))
1515 .getSExtValue();
1516 case e_float:
1517 return (sshort_t)m_float.convertToFloat();
1518 case e_double:
1519 return (sshort_t)m_float.convertToDouble();
1520 case e_long_double:
1521 llvm::APInt ldbl_val = m_float.bitcastToAPInt();
1522 return (sshort_t)(ldbl_val.sextOrTrunc(sizeof(sshort_t) * 8))
1523 .getSExtValue();
1524 }
1525 return fail_value;
1526}
1527
1528unsigned short Scalar::UShort(unsigned short fail_value) const {
1529 switch (m_type) {
1530 case e_void:
1531 break;
1532 case e_sint:
1533 case e_uint:
1534 case e_slong:
1535 case e_ulong:
1536 case e_slonglong:
1537 case e_ulonglong:
1538 case e_sint128:
1539 case e_uint128:
1540 case e_sint256:
1541 case e_uint256:
1542 return (ushort_t)(m_integer.zextOrTrunc(sizeof(ushort_t) * 8))
1543 .getZExtValue();
1544 case e_float:
1545 return (ushort_t)m_float.convertToFloat();
1546 case e_double:
1547 return (ushort_t)m_float.convertToDouble();
1548 case e_long_double:
1549 llvm::APInt ldbl_val = m_float.bitcastToAPInt();
1550 return (ushort_t)(ldbl_val.zextOrTrunc(sizeof(ushort_t) * 8))
1551 .getZExtValue();
1552 }
1553 return fail_value;
1554}
1555
1556int Scalar::SInt(int fail_value) const {
1557 switch (m_type) {
1558 case e_void:
1559 break;
1560 case e_sint:
1561 case e_uint:
1562 case e_slong:
1563 case e_ulong:
1564 case e_slonglong:
1565 case e_ulonglong:
1566 case e_sint128:
1567 case e_uint128:
1568 case e_sint256:
1569 case e_uint256:
1570 return (sint_t)(m_integer.sextOrTrunc(sizeof(sint_t) * 8)).getSExtValue();
1571 case e_float:
1572 return (sint_t)m_float.convertToFloat();
1573 case e_double:
1574 return (sint_t)m_float.convertToDouble();
1575 case e_long_double:
1576 llvm::APInt ldbl_val = m_float.bitcastToAPInt();
1577 return (sint_t)(ldbl_val.sextOrTrunc(sizeof(sint_t) * 8)).getSExtValue();
1578 }
1579 return fail_value;
1580}
1581
1582unsigned int Scalar::UInt(unsigned int fail_value) const {
1583 switch (m_type) {
1584 case e_void:
1585 break;
1586 case e_sint:
1587 case e_uint:
1588 case e_slong:
1589 case e_ulong:
1590 case e_slonglong:
1591 case e_ulonglong:
1592 case e_sint128:
1593 case e_uint128:
1594 case e_sint256:
1595 case e_uint256:
1596 return (uint_t)(m_integer.zextOrTrunc(sizeof(uint_t) * 8)).getZExtValue();
1597 case e_float:
1598 return (uint_t)m_float.convertToFloat();
1599 case e_double:
1600 return (uint_t)m_float.convertToDouble();
1601 case e_long_double:
1602 llvm::APInt ldbl_val = m_float.bitcastToAPInt();
1603 return (uint_t)(ldbl_val.zextOrTrunc(sizeof(uint_t) * 8)).getZExtValue();
1604 }
1605 return fail_value;
1606}
1607
1608long Scalar::SLong(long fail_value) const {
1609 switch (m_type) {
1610 case e_void:
1611 break;
1612 case e_sint:
1613 case e_uint:
1614 case e_slong:
1615 case e_ulong:
1616 case e_slonglong:
1617 case e_ulonglong:
1618 case e_sint128:
1619 case e_uint128:
1620 case e_sint256:
1621 case e_uint256:
1622 return (slong_t)(m_integer.sextOrTrunc(sizeof(slong_t) * 8)).getSExtValue();
1623 case e_float:
1624 return (slong_t)m_float.convertToFloat();
1625 case e_double:
1626 return (slong_t)m_float.convertToDouble();
1627 case e_long_double:
1628 llvm::APInt ldbl_val = m_float.bitcastToAPInt();
1629 return (slong_t)(ldbl_val.sextOrTrunc(sizeof(slong_t) * 8)).getSExtValue();
1630 }
1631 return fail_value;
1632}
1633
1634unsigned long Scalar::ULong(unsigned long fail_value) const {
1635 switch (m_type) {
1636 case e_void:
1637 break;
1638 case e_sint:
1639 case e_uint:
1640 case e_slong:
1641 case e_ulong:
1642 case e_slonglong:
1643 case e_ulonglong:
1644 case e_sint128:
1645 case e_uint128:
1646 case e_sint256:
1647 case e_uint256:
1648 return (ulong_t)(m_integer.zextOrTrunc(sizeof(ulong_t) * 8)).getZExtValue();
1649 case e_float:
1650 return (ulong_t)m_float.convertToFloat();
1651 case e_double:
1652 return (ulong_t)m_float.convertToDouble();
1653 case e_long_double:
1654 llvm::APInt ldbl_val = m_float.bitcastToAPInt();
1655 return (ulong_t)(ldbl_val.zextOrTrunc(sizeof(ulong_t) * 8)).getZExtValue();
1656 }
1657 return fail_value;
1658}
1659
1660long long Scalar::SLongLong(long long 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:
1674 return (slonglong_t)(m_integer.sextOrTrunc(sizeof(slonglong_t) * 8))
1675 .getSExtValue();
1676 case e_float:
1677 return (slonglong_t)m_float.convertToFloat();
1678 case e_double:
1679 return (slonglong_t)m_float.convertToDouble();
1680 case e_long_double:
1681 llvm::APInt ldbl_val = m_float.bitcastToAPInt();
1682 return (slonglong_t)(ldbl_val.sextOrTrunc(sizeof(slonglong_t) * 8))
1683 .getSExtValue();
1684 }
1685 return fail_value;
1686}
1687
1688unsigned long long Scalar::ULongLong(unsigned long long 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:
1702 return (ulonglong_t)(m_integer.zextOrTrunc(sizeof(ulonglong_t) * 8))
1703 .getZExtValue();
1704 case e_float:
1705 return (ulonglong_t)m_float.convertToFloat();
1706 case e_double:
1707 return (ulonglong_t)m_float.convertToDouble();
1708 case e_long_double:
1709 llvm::APInt ldbl_val = m_float.bitcastToAPInt();
1710 return (ulonglong_t)(ldbl_val.zextOrTrunc(sizeof(ulonglong_t) * 8))
1711 .getZExtValue();
1712 }
1713 return fail_value;
1714}
1715
1716llvm::APInt Scalar::SInt128(llvm::APInt &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:
1730 return m_integer;
1731 case e_float:
1732 case e_double:
1733 case e_long_double:
1734 return m_float.bitcastToAPInt();
1735 }
1736 return fail_value;
1737}
1738
1739llvm::APInt Scalar::UInt128(const llvm::APInt &fail_value) const {
1740 switch (m_type) {
1741 case e_void:
1742 break;
1743 case e_sint:
1744 case e_uint:
1745 case e_slong:
1746 case e_ulong:
1747 case e_slonglong:
1748 case e_ulonglong:
1749 case e_sint128:
1750 case e_uint128:
1751 case e_sint256:
1752 case e_uint256:
1753 return m_integer;
1754 case e_float:
1755 case e_double:
1756 case e_long_double:
1757 return m_float.bitcastToAPInt();
1758 }
1759 return fail_value;
1760}
1761
1762llvm::APInt Scalar::SInt256(llvm::APInt &fail_value) const {
1763 switch (m_type) {
1764 case e_void:
1765 break;
1766 case e_sint:
1767 case e_uint:
1768 case e_slong:
1769 case e_ulong:
1770 case e_slonglong:
1771 case e_ulonglong:
1772 case e_sint128:
1773 case e_uint128:
1774 case e_sint256:
1775 case e_uint256:
1776 return m_integer;
1777 case e_float:
1778 case e_double:
1779 case e_long_double:
1780 return m_float.bitcastToAPInt();
1781 }
1782 return fail_value;
1783}
1784
1785llvm::APInt Scalar::UInt256(const llvm::APInt &fail_value) const {
1786 switch (m_type) {
1787 case e_void:
1788 break;
1789 case e_sint:
1790 case e_uint:
1791 case e_slong:
1792 case e_ulong:
1793 case e_slonglong:
1794 case e_ulonglong:
1795 case e_sint128:
1796 case e_uint128:
1797 case e_sint256:
1798 case e_uint256:
1799 return m_integer;
1800 case e_float:
1801 case e_double:
1802 case e_long_double:
1803 return m_float.bitcastToAPInt();
1804 }
1805 return fail_value;
1806}
1807
1808float Scalar::Float(float fail_value) const {
1809 switch (m_type) {
1810 case e_void:
1811 break;
1812 case e_sint:
1813 case e_uint:
1814 case e_slong:
1815 case e_ulong:
1816 case e_slonglong:
1817 case e_ulonglong:
1818 case e_sint128:
1819 case e_uint128:
1820 case e_sint256:
1821 case e_uint256:
1822 return m_integer.bitsToFloat();
1823 case e_float:
1824 return m_float.convertToFloat();
1825 case e_double:
1826 return (float_t)m_float.convertToDouble();
1827 case e_long_double:
1828 llvm::APInt ldbl_val = m_float.bitcastToAPInt();
1829 return ldbl_val.bitsToFloat();
1830 }
1831 return fail_value;
1832}
1833
1834double Scalar::Double(double fail_value) const {
1835 switch (m_type) {
1836 case e_void:
1837 break;
1838 case e_sint:
1839 case e_uint:
1840 case e_slong:
1841 case e_ulong:
1842 case e_slonglong:
1843 case e_ulonglong:
1844 case e_sint128:
1845 case e_uint128:
1846 case e_sint256:
1847 case e_uint256:
1848 return m_integer.bitsToDouble();
1849 case e_float:
1850 return (double_t)m_float.convertToFloat();
1851 case e_double:
1852 return m_float.convertToDouble();
1853 case e_long_double:
1854 llvm::APInt ldbl_val = m_float.bitcastToAPInt();
1855 return ldbl_val.bitsToFloat();
1856 }
1857 return fail_value;
1858}
1859
1860long double Scalar::LongDouble(long double fail_value) const {
1861 switch (m_type) {
1862 case e_void:
1863 break;
1864 case e_sint:
1865 case e_uint:
1866 case e_slong:
1867 case e_ulong:
1868 case e_slonglong:
1869 case e_ulonglong:
1870 case e_sint128:
1871 case e_uint128:
1872 case e_sint256:
1873 case e_uint256:
1874 return (long_double_t)m_integer.bitsToDouble();
1875 case e_float:
1876 return (long_double_t)m_float.convertToFloat();
1877 case e_double:
1878 return (long_double_t)m_float.convertToDouble();
1879 case e_long_double:
1880 llvm::APInt ldbl_val = m_float.bitcastToAPInt();
1881 return (long_double_t)ldbl_val.bitsToDouble();
1882 }
1883 return fail_value;
1884}
1885
1886Scalar &Scalar::operator+=(const Scalar &rhs) {
1887 Scalar temp_value;
1888 const Scalar *a;
1889 const Scalar *b;
1890 if ((m_type = PromoteToMaxType(*this, rhs, temp_value, a, b)) !=
1891 Scalar::e_void) {
1892 switch (m_type) {
1893 case e_void:
1894 break;
1895 case e_sint:
1896 case e_uint:
1897 case e_slong:
1898 case e_ulong:
1899 case e_slonglong:
1900 case e_ulonglong:
1901 case e_sint128:
1902 case e_uint128:
1903 case e_sint256:
1904 case e_uint256:
1905 m_integer = a->m_integer + b->m_integer;
1906 break;
1907
1908 case e_float:
1909 case e_double:
1910 case e_long_double:
1911 m_float = a->m_float + b->m_float;
1912 break;
1913 }
1914 }
1915 return *this;
1916}
1917
1918Scalar &Scalar::operator<<=(const Scalar &rhs) {
1919 switch (m_type) {
1920 case e_void:
1921 case e_float:
1922 case e_double:
1923 case e_long_double:
1924 m_type = e_void;
1925 break;
1926
1927 case e_sint:
1928 case e_uint:
1929 case e_slong:
1930 case e_ulong:
1931 case e_slonglong:
1932 case e_ulonglong:
1933 case e_sint128:
1934 case e_uint128:
1935 case e_sint256:
1936 case e_uint256:
1937 switch (rhs.m_type) {
1938 case e_void:
1939 case e_float:
1940 case e_double:
1941 case e_long_double:
1942 m_type = e_void;
1943 break;
1944 case e_sint:
1945 case e_uint:
1946 case e_slong:
1947 case e_ulong:
1948 case e_slonglong:
1949 case e_ulonglong:
1950 case e_sint128:
1951 case e_uint128:
1952 case e_sint256:
1953 case e_uint256:
1954 m_integer = m_integer << rhs.m_integer;
1955 break;
1956 }
1957 break;
1958 }
1959 return *this;
1960}
1961
1962bool Scalar::ShiftRightLogical(const Scalar &rhs) {
1963 switch (m_type) {
1964 case e_void:
1965 case e_float:
1966 case e_double:
1967 case e_long_double:
1968 m_type = e_void;
1969 break;
1970
1971 case e_sint:
1972 case e_uint:
1973 case e_slong:
1974 case e_ulong:
1975 case e_slonglong:
1976 case e_ulonglong:
1977 case e_sint128:
1978 case e_uint128:
1979 case e_sint256:
1980 case e_uint256:
1981 switch (rhs.m_type) {
1982 case e_void:
1983 case e_float:
1984 case e_double:
1985 case e_long_double:
1986 m_type = e_void;
1987 break;
1988 case e_sint:
1989 case e_uint:
1990 case e_slong:
1991 case e_ulong:
1992 case e_slonglong:
1993 case e_ulonglong:
1994 case e_sint128:
1995 case e_uint128:
1996 case e_sint256:
1997 case e_uint256:
1998 m_integer = m_integer.lshr(rhs.m_integer);
1999 break;
2000 }
2001 break;
2002 }
2003 return m_type != e_void;
2004}
2005
2006Scalar &Scalar::operator>>=(const Scalar &rhs) {
2007 switch (m_type) {
2008 case e_void:
2009 case e_float:
2010 case e_double:
2011 case e_long_double:
2012 m_type = e_void;
2013 break;
2014
2015 case e_sint:
2016 case e_uint:
2017 case e_slong:
2018 case e_ulong:
2019 case e_slonglong:
2020 case e_ulonglong:
2021 case e_sint128:
2022 case e_uint128:
2023 case e_sint256:
2024 case e_uint256:
2025 switch (rhs.m_type) {
2026 case e_void:
2027 case e_float:
2028 case e_double:
2029 case e_long_double:
2030 m_type = e_void;
2031 break;
2032 case e_sint:
2033 case e_uint:
2034 case e_slong:
2035 case e_ulong:
2036 case e_slonglong:
2037 case e_ulonglong:
2038 case e_sint128:
2039 case e_uint128:
2040 case e_sint256:
2041 case e_uint256:
2042 m_integer = m_integer.ashr(rhs.m_integer);
2043 break;
2044 }
2045 break;
2046 }
2047 return *this;
2048}
2049
2050Scalar &Scalar::operator&=(const Scalar &rhs) {
2051 switch (m_type) {
2052 case e_void:
2053 case e_float:
2054 case e_double:
2055 case e_long_double:
2056 m_type = e_void;
2057 break;
2058
2059 case e_sint:
2060 case e_uint:
2061 case e_slong:
2062 case e_ulong:
2063 case e_slonglong:
2064 case e_ulonglong:
2065 case e_sint128:
2066 case e_uint128:
2067 case e_sint256:
2068 case e_uint256:
2069 switch (rhs.m_type) {
2070 case e_void:
2071 case e_float:
2072 case e_double:
2073 case e_long_double:
2074 m_type = e_void;
2075 break;
2076 case e_sint:
2077 case e_uint:
2078 case e_slong:
2079 case e_ulong:
2080 case e_slonglong:
2081 case e_ulonglong:
2082 case e_sint128:
2083 case e_uint128:
2084 case e_sint256:
2085 case e_uint256:
2086 m_integer &= rhs.m_integer;
2087 break;
2088 }
2089 break;
2090 }
2091 return *this;
2092}
2093
2094bool Scalar::AbsoluteValue() {
2095 switch (m_type) {
2096 case e_void:
2097 break;
2098
2099 case e_sint:
2100 case e_slong:
2101 case e_slonglong:
2102 case e_sint128:
2103 case e_sint256:
2104 if (m_integer.isNegative())
2105 m_integer = -m_integer;
2106 return true;
2107
2108 case e_uint:
2109 case e_ulong:
2110 case e_ulonglong:
2111 return true;
2112 case e_uint128:
2113 case e_uint256:
2114 case e_float:
2115 case e_double:
2116 case e_long_double:
2117 m_float.clearSign();
2118 return true;
2119 }
2120 return false;
2121}
2122
2123bool Scalar::UnaryNegate() {
2124 switch (m_type) {
2125 case e_void:
2126 break;
2127 case e_sint:
2128 case e_uint:
2129 case e_slong:
2130 case e_ulong:
2131 case e_slonglong:
2132 case e_ulonglong:
2133 case e_sint128:
2134 case e_uint128:
2135 case e_sint256:
2136 case e_uint256:
2137 m_integer = -m_integer;
2138 return true;
2139 case e_float:
2140 case e_double:
2141 case e_long_double:
2142 m_float.changeSign();
2143 return true;
2144 }
2145 return false;
2146}
2147
2148bool Scalar::OnesComplement() {
2149 switch (m_type) {
2150 case e_sint:
2151 case e_uint:
2152 case e_slong:
2153 case e_ulong:
2154 case e_slonglong:
2155 case e_ulonglong:
2156 case e_sint128:
2157 case e_uint128:
2158 case e_sint256:
2159 case e_uint256:
2160 m_integer = ~m_integer;
2161 return true;
2162
2163 case e_void:
2164 case e_float:
2165 case e_double:
2166 case e_long_double:
2167 break;
2168 }
2169 return false;
2170}
2171
2172const Scalar lldb_private::operator+(const Scalar &lhs, const Scalar &rhs) {
2173 Scalar result;
2174 Scalar temp_value;
2175 const Scalar *a;
2176 const Scalar *b;
2177 if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
2178 Scalar::e_void) {
2179 switch (result.m_type) {
2180 case Scalar::e_void:
2181 break;
2182 case Scalar::e_sint:
2183 case Scalar::e_uint:
2184 case Scalar::e_slong:
2185 case Scalar::e_ulong:
2186 case Scalar::e_slonglong:
2187 case Scalar::e_ulonglong:
2188 case Scalar::e_sint128:
2189 case Scalar::e_uint128:
2190 case Scalar::e_sint256:
2191 case Scalar::e_uint256:
2192 result.m_integer = a->m_integer + b->m_integer;
2193 break;
2194 case Scalar::e_float:
2195 case Scalar::e_double:
2196 case Scalar::e_long_double:
2197 result.m_float = a->m_float + b->m_float;
2198 break;
2199 }
2200 }
2201 return result;
2202}
2203
2204const Scalar lldb_private::operator-(const Scalar &lhs, const Scalar &rhs) {
2205 Scalar result;
2206 Scalar temp_value;
2207 const Scalar *a;
2208 const Scalar *b;
2209 if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
2210 Scalar::e_void) {
2211 switch (result.m_type) {
2212 case Scalar::e_void:
2213 break;
2214 case Scalar::e_sint:
2215 case Scalar::e_uint:
2216 case Scalar::e_slong:
2217 case Scalar::e_ulong:
2218 case Scalar::e_slonglong:
2219 case Scalar::e_ulonglong:
2220 case Scalar::e_sint128:
2221 case Scalar::e_uint128:
2222 case Scalar::e_sint256:
2223 case Scalar::e_uint256:
2224 result.m_integer = a->m_integer - b->m_integer;
2225 break;
2226 case Scalar::e_float:
2227 case Scalar::e_double:
2228 case Scalar::e_long_double:
2229 result.m_float = a->m_float - b->m_float;
2230 break;
2231 }
2232 }
2233 return result;
2234}
2235
2236const Scalar lldb_private::operator/(const Scalar &lhs, const Scalar &rhs) {
2237 Scalar result;
2238 Scalar temp_value;
2239 const Scalar *a;
2240 const Scalar *b;
2241 if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
2242 Scalar::e_void) {
2243 switch (result.m_type) {
2244 case Scalar::e_void:
2245 break;
2246 case Scalar::e_sint:
2247 case Scalar::e_slong:
2248 case Scalar::e_slonglong:
2249 case Scalar::e_sint128:
2250 case Scalar::e_sint256:
2251 if (b->m_integer != 0) {
2252 result.m_integer = a->m_integer.sdiv(b->m_integer);
2253 return result;
2254 }
2255 break;
2256 case Scalar::e_uint:
2257 case Scalar::e_ulong:
2258 case Scalar::e_ulonglong:
2259 case Scalar::e_uint128:
2260 case Scalar::e_uint256:
2261 if (b->m_integer != 0) {
2262 result.m_integer = a->m_integer.udiv(b->m_integer);
2263 return result;
2264 }
2265 break;
2266 case Scalar::e_float:
2267 case Scalar::e_double:
2268 case Scalar::e_long_double:
2269 if (b->m_float.isZero()) {
2270 result.m_float = a->m_float / b->m_float;
2271 return result;
2272 }
2273 break;
2274 }
2275 }
2276 // For division only, the only way it should make it here is if a promotion
2277 // failed,
2278 // or if we are trying to do a divide by zero.
2279 result.m_type = Scalar::e_void;
2280 return result;
2281}
2282
2283const Scalar lldb_private::operator*(const Scalar &lhs, const Scalar &rhs) {
2284 Scalar result;
2285 Scalar temp_value;
2286 const Scalar *a;
2287 const Scalar *b;
2288 if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
2289 Scalar::e_void) {
2290 switch (result.m_type) {
2291 case Scalar::e_void:
2292 break;
2293 case Scalar::e_sint:
2294 case Scalar::e_uint:
2295 case Scalar::e_slong:
2296 case Scalar::e_ulong:
2297 case Scalar::e_slonglong:
2298 case Scalar::e_ulonglong:
2299 case Scalar::e_sint128:
2300 case Scalar::e_uint128:
2301 case Scalar::e_sint256:
2302 case Scalar::e_uint256:
2303 result.m_integer = a->m_integer * b->m_integer;
2304 break;
2305 case Scalar::e_float:
2306 case Scalar::e_double:
2307 case Scalar::e_long_double:
2308 result.m_float = a->m_float * b->m_float;
2309 break;
2310 }
2311 }
2312 return result;
2313}
2314
2315const Scalar lldb_private::operator&(const Scalar &lhs, const Scalar &rhs) {
2316 Scalar result;
2317 Scalar temp_value;
2318 const Scalar *a;
2319 const Scalar *b;
2320 if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
2321 Scalar::e_void) {
2322 switch (result.m_type) {
2323 case Scalar::e_sint:
2324 case Scalar::e_uint:
2325 case Scalar::e_slong:
2326 case Scalar::e_ulong:
2327 case Scalar::e_slonglong:
2328 case Scalar::e_ulonglong:
2329 case Scalar::e_sint128:
2330 case Scalar::e_uint128:
2331 case Scalar::e_sint256:
2332 case Scalar::e_uint256:
2333 result.m_integer = a->m_integer & b->m_integer;
2334 break;
2335 case Scalar::e_void:
2336 case Scalar::e_float:
2337 case Scalar::e_double:
2338 case Scalar::e_long_double:
2339 // No bitwise AND on floats, doubles of long doubles
2340 result.m_type = Scalar::e_void;
2341 break;
2342 }
2343 }
2344 return result;
2345}
2346
2347const Scalar lldb_private::operator|(const Scalar &lhs, const Scalar &rhs) {
2348 Scalar result;
2349 Scalar temp_value;
2350 const Scalar *a;
2351 const Scalar *b;
2352 if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
2353 Scalar::e_void) {
2354 switch (result.m_type) {
2355 case Scalar::e_sint:
2356 case Scalar::e_uint:
2357 case Scalar::e_slong:
2358 case Scalar::e_ulong:
2359 case Scalar::e_slonglong:
2360 case Scalar::e_ulonglong:
2361 case Scalar::e_sint128:
2362 case Scalar::e_uint128:
2363 case Scalar::e_sint256:
2364 case Scalar::e_uint256:
2365 result.m_integer = a->m_integer | b->m_integer;
2366 break;
2367
2368 case Scalar::e_void:
2369 case Scalar::e_float:
2370 case Scalar::e_double:
2371 case Scalar::e_long_double:
2372 // No bitwise AND on floats, doubles of long doubles
2373 result.m_type = Scalar::e_void;
2374 break;
2375 }
2376 }
2377 return result;
2378}
2379
2380const Scalar lldb_private::operator%(const Scalar &lhs, const Scalar &rhs) {
2381 Scalar result;
2382 Scalar temp_value;
2383 const Scalar *a;
2384 const Scalar *b;
2385 if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
2386 Scalar::e_void) {
2387 switch (result.m_type) {
2388 default:
2389 break;
2390 case Scalar::e_void:
2391 break;
2392 case Scalar::e_sint:
2393 case Scalar::e_slong:
2394 case Scalar::e_slonglong:
2395 case Scalar::e_sint128:
2396 case Scalar::e_sint256:
2397 if (b->m_integer != 0) {
2398 result.m_integer = a->m_integer.srem(b->m_integer);
2399 return result;
2400 }
2401 break;
2402 case Scalar::e_uint:
2403 case Scalar::e_ulong:
2404 case Scalar::e_ulonglong:
2405 case Scalar::e_uint128:
2406 case Scalar::e_uint256:
2407 if (b->m_integer != 0) {
2408 result.m_integer = a->m_integer.urem(b->m_integer);
2409 return result;
2410 }
2411 break;
2412 }
2413 }
2414 result.m_type = Scalar::e_void;
2415 return result;
2416}
2417
2418const Scalar lldb_private::operator^(const Scalar &lhs, const Scalar &rhs) {
2419 Scalar result;
2420 Scalar temp_value;
2421 const Scalar *a;
2422 const Scalar *b;
2423 if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) !=
2424 Scalar::e_void) {
2425 switch (result.m_type) {
2426 case Scalar::e_sint:
2427 case Scalar::e_uint:
2428 case Scalar::e_slong:
2429 case Scalar::e_ulong:
2430 case Scalar::e_slonglong:
2431 case Scalar::e_ulonglong:
2432 case Scalar::e_sint128:
2433 case Scalar::e_uint128:
2434 case Scalar::e_sint256:
2435 case Scalar::e_uint256:
2436 result.m_integer = a->m_integer ^ b->m_integer;
2437 break;
2438
2439 case Scalar::e_void:
2440 case Scalar::e_float:
2441 case Scalar::e_double:
2442 case Scalar::e_long_double:
2443 // No bitwise AND on floats, doubles of long doubles
2444 result.m_type = Scalar::e_void;
2445 break;
2446 }
2447 }
2448 return result;
2449}
2450
2451const Scalar lldb_private::operator<<(const Scalar &lhs, const Scalar &rhs) {
2452 Scalar result = lhs;
2453 result <<= rhs;
2454 return result;
2455}
2456
2457const Scalar lldb_private::operator>>(const Scalar &lhs, const Scalar &rhs) {
2458 Scalar result = lhs;
2459 result >>= rhs;
2460 return result;
2461}
2462
2463Error Scalar::SetValueFromCString(const char *value_str, Encoding encoding,
2464 size_t byte_size) {
2465 Error error;
2466 if (value_str == nullptr || value_str[0] == '\0') {
2467 error.SetErrorString("Invalid c-string value string.");
2468 return error;
2469 }
2470 bool success = false;
2471 switch (encoding) {
2472 case eEncodingInvalid:
2473 error.SetErrorString("Invalid encoding.");
2474 break;
2475
2476 case eEncodingUint:
2477 if (byte_size <= sizeof(unsigned long long)) {
2478 uint64_t uval64 =
2479 StringConvert::ToUInt64(value_str, UINT64_MAX(18446744073709551615UL), 0, &success);
2480 if (!success)
2481 error.SetErrorStringWithFormat(
2482 "'%s' is not a valid unsigned integer string value", value_str);
2483 else if (!UIntValueIsValidForSize(uval64, byte_size))
2484 error.SetErrorStringWithFormat("value 0x%" PRIx64"l" "x"
2485 " is too large to fit in a %" PRIu64"l" "u"
2486 " byte unsigned integer value",
2487 uval64, (uint64_t)byte_size);
2488 else {
2489 m_type = Scalar::GetValueTypeForUnsignedIntegerWithByteSize(byte_size);
2490 switch (m_type) {
2491 case e_uint:
2492 m_integer = llvm::APInt(sizeof(uint_t) * 8, uval64, false);
2493 break;
2494 case e_ulong:
2495 m_integer = llvm::APInt(sizeof(ulong_t) * 8, uval64, false);
2496 break;
2497 case e_ulonglong:
2498 m_integer = llvm::APInt(sizeof(ulonglong_t) * 8, uval64, false);
2499 break;
2500 default:
2501 error.SetErrorStringWithFormat(
2502 "unsupported unsigned integer byte size: %" PRIu64"l" "u" "",
2503 (uint64_t)byte_size);
2504 break;
2505 }
2506 }
2507 } else {
2508 error.SetErrorStringWithFormat(
2509 "unsupported unsigned integer byte size: %" PRIu64"l" "u" "",
2510 (uint64_t)byte_size);
2511 return error;
2512 }
2513 break;
2514
2515 case eEncodingSint:
2516 if (byte_size <= sizeof(long long)) {
2517 uint64_t sval64 =
2518 StringConvert::ToSInt64(value_str, INT64_MAX(9223372036854775807L), 0, &success);
2519 if (!success)
2520 error.SetErrorStringWithFormat(
2521 "'%s' is not a valid signed integer string value", value_str);
2522 else if (!SIntValueIsValidForSize(sval64, byte_size))
2523 error.SetErrorStringWithFormat("value 0x%" PRIx64"l" "x"
2524 " is too large to fit in a %" PRIu64"l" "u"
2525 " byte signed integer value",
2526 sval64, (uint64_t)byte_size);
2527 else {
2528 m_type = Scalar::GetValueTypeForSignedIntegerWithByteSize(byte_size);
2529 switch (m_type) {
2530 case e_sint:
2531 m_integer = llvm::APInt(sizeof(sint_t) * 8, sval64, true);
2532 break;
2533 case e_slong:
2534 m_integer = llvm::APInt(sizeof(slong_t) * 8, sval64, true);
2535 break;
2536 case e_slonglong:
2537 m_integer = llvm::APInt(sizeof(slonglong_t) * 8, sval64, true);
2538 break;
2539 default:
2540 error.SetErrorStringWithFormat(
2541 "unsupported signed integer byte size: %" PRIu64"l" "u" "",
2542 (uint64_t)byte_size);
2543 break;
2544 }
2545 }
2546 } else {
2547 error.SetErrorStringWithFormat(
2548 "unsupported signed integer byte size: %" PRIu64"l" "u" "",
2549 (uint64_t)byte_size);
2550 return error;
2551 }
2552 break;
2553
2554 case eEncodingIEEE754:
2555 static float f_val;
2556 static double d_val;
2557 static long double l_val;
2558 if (byte_size == sizeof(float)) {
2559 if (::sscanf(value_str, "%f", &f_val) == 1) {
2560 m_float = llvm::APFloat(f_val);
2561 m_type = e_float;
2562 } else
2563 error.SetErrorStringWithFormat("'%s' is not a valid float string value",
2564 value_str);
2565 } else if (byte_size == sizeof(double)) {
2566 if (::sscanf(value_str, "%lf", &d_val) == 1) {
2567 m_float = llvm::APFloat(d_val);
2568 m_type = e_double;
2569 } else
2570 error.SetErrorStringWithFormat("'%s' is not a valid float string value",
2571 value_str);
2572 } else if (byte_size == sizeof(long double)) {
2573 if (::sscanf(value_str, "%Lf", &l_val) == 1) {
2574 m_float =
2575 llvm::APFloat(llvm::APFloat::x87DoubleExtended(),
2576 llvm::APInt(BITWIDTH_INT128128, NUM_OF_WORDS_INT1282,
2577 ((type128 *)&l_val)->x));
2578 m_type = e_long_double;
2579 } else
2580 error.SetErrorStringWithFormat("'%s' is not a valid float string value",
2581 value_str);
2582 } else {
2583 error.SetErrorStringWithFormat("unsupported float byte size: %" PRIu64"l" "u" "",
2584 (uint64_t)byte_size);
2585 return error;
2586 }
2587 break;
2588
2589 case eEncodingVector:
2590 error.SetErrorString("vector encoding unsupported.");
2591 break;
2592 }
2593 if (error.Fail())
2594 m_type = e_void;
2595
2596 return error;
2597}
2598
2599Error Scalar::SetValueFromData(DataExtractor &data, lldb::Encoding encoding,
2600 size_t byte_size) {
2601 Error error;
2602
2603 type128 int128;
2604 type256 int256;
2605 switch (encoding) {
2606 case lldb::eEncodingInvalid:
2607 error.SetErrorString("invalid encoding");
2608 break;
2609 case lldb::eEncodingVector:
2610 error.SetErrorString("vector encoding unsupported");
2611 break;
2612 case lldb::eEncodingUint: {
2613 lldb::offset_t offset = 0;
2614
2615 switch (byte_size) {
2616 case 1:
2617 operator=((uint8_t)data.GetU8(&offset));
2618 break;
2619 case 2:
2620 operator=((uint16_t)data.GetU16(&offset));
2621 break;
2622 case 4:
2623 operator=((uint32_t)data.GetU32(&offset));
2624 break;
2625 case 8:
2626 operator=((uint64_t)data.GetU64(&offset));
2627 break;
2628 case 16:
2629 if (data.GetByteOrder() == eByteOrderBig) {
2630 int128.x[1] = (uint64_t)data.GetU64(&offset);
2631 int128.x[0] = (uint64_t)data.GetU64(&offset);
2632 } else {
2633 int128.x[0] = (uint64_t)data.GetU64(&offset);
2634 int128.x[1] = (uint64_t)data.GetU64(&offset);
2635 }
2636 operator=(llvm::APInt(BITWIDTH_INT128128, NUM_OF_WORDS_INT1282, int128.x));
2637 break;
2638 case 32:
2639 if (data.GetByteOrder() == eByteOrderBig) {
2640 int256.x[3] = (uint64_t)data.GetU64(&offset);
2641 int256.x[2] = (uint64_t)data.GetU64(&offset);
2642 int256.x[1] = (uint64_t)data.GetU64(&offset);
2643 int256.x[0] = (uint64_t)data.GetU64(&offset);
2644 } else {
2645 int256.x[0] = (uint64_t)data.GetU64(&offset);
2646 int256.x[1] = (uint64_t)data.GetU64(&offset);
2647 int256.x[2] = (uint64_t)data.GetU64(&offset);
2648 int256.x[3] = (uint64_t)data.GetU64(&offset);
2649 }
2650 operator=(llvm::APInt(BITWIDTH_INT256256, NUM_OF_WORDS_INT2564, int256.x));
2651 break;
2652 default:
2653 error.SetErrorStringWithFormat(
2654 "unsupported unsigned integer byte size: %" PRIu64"l" "u" "",
2655 (uint64_t)byte_size);
2656 break;
2657 }
2658 } break;
2659 case lldb::eEncodingSint: {
2660 lldb::offset_t offset = 0;
2661
2662 switch (byte_size) {
2663 case 1:
2664 operator=((int8_t)data.GetU8(&offset));
2665 break;
2666 case 2:
2667 operator=((int16_t)data.GetU16(&offset));
2668 break;
2669 case 4:
2670 operator=((int32_t)data.GetU32(&offset));
2671 break;
2672 case 8:
2673 operator=((int64_t)data.GetU64(&offset));
2674 break;
2675 case 16:
2676 if (data.GetByteOrder() == eByteOrderBig) {
2677 int128.x[1] = (uint64_t)data.GetU64(&offset);
2678 int128.x[0] = (uint64_t)data.GetU64(&offset);
2679 } else {
2680 int128.x[0] = (uint64_t)data.GetU64(&offset);
2681 int128.x[1] = (uint64_t)data.GetU64(&offset);
2682 }
2683 operator=(llvm::APInt(BITWIDTH_INT128128, NUM_OF_WORDS_INT1282, int128.x));
2684 break;
2685 case 32:
2686 if (data.GetByteOrder() == eByteOrderBig) {
2687 int256.x[3] = (uint64_t)data.GetU64(&offset);
2688 int256.x[2] = (uint64_t)data.GetU64(&offset);
2689 int256.x[1] = (uint64_t)data.GetU64(&offset);
2690 int256.x[0] = (uint64_t)data.GetU64(&offset);
2691 } else {
2692 int256.x[0] = (uint64_t)data.GetU64(&offset);
2693 int256.x[1] = (uint64_t)data.GetU64(&offset);
2694 int256.x[2] = (uint64_t)data.GetU64(&offset);
2695 int256.x[3] = (uint64_t)data.GetU64(&offset);
2696 }
2697 operator=(llvm::APInt(BITWIDTH_INT256256, NUM_OF_WORDS_INT2564, int256.x));
2698 break;
2699 default:
2700 error.SetErrorStringWithFormat(
2701 "unsupported signed integer byte size: %" PRIu64"l" "u" "",
2702 (uint64_t)byte_size);
2703 break;
2704 }
2705 } break;
2706 case lldb::eEncodingIEEE754: {
2707 lldb::offset_t offset = 0;
2708
2709 if (byte_size == sizeof(float))
2710 operator=((float)data.GetFloat(&offset));
2711 else if (byte_size == sizeof(double))
2712 operator=((double)data.GetDouble(&offset));
2713 else if (byte_size == sizeof(long double))
2714 operator=((long double)data.GetLongDouble(&offset));
2715 else
2716 error.SetErrorStringWithFormat("unsupported float byte size: %" PRIu64"l" "u" "",
2717 (uint64_t)byte_size);
2718 } break;
2719 }
2720
2721 return error;
2722}
2723
2724bool Scalar::SignExtend(uint32_t sign_bit_pos) {
2725 const uint32_t max_bit_pos = GetByteSize() * 8;
2726
2727 if (sign_bit_pos < max_bit_pos) {
2728 switch (m_type) {
2729 case Scalar::e_void:
2730 case Scalar::e_float:
2731 case Scalar::e_double:
2732 case Scalar::e_long_double:
2733 return false;
2734
2735 case Scalar::e_sint:
2736 case Scalar::e_uint:
2737 case Scalar::e_slong:
2738 case Scalar::e_ulong:
2739 case Scalar::e_slonglong:
2740 case Scalar::e_ulonglong:
2741 case Scalar::e_sint128:
2742 case Scalar::e_uint128:
2743 case Scalar::e_sint256:
2744 case Scalar::e_uint256:
2745 if (max_bit_pos == sign_bit_pos)
2746 return true;
2747 else if (sign_bit_pos < (max_bit_pos - 1)) {
2748 llvm::APInt sign_bit = llvm::APInt::getSignMask(sign_bit_pos + 1);
2749 llvm::APInt bitwize_and = m_integer & sign_bit;
2750 if (bitwize_and.getBoolValue()) {
2751 const llvm::APInt mask =
2752 ~(sign_bit) + llvm::APInt(m_integer.getBitWidth(), 1);
2753 m_integer |= mask;
2754 }
2755 return true;
2756 }
2757 break;
2758 }
2759 }
2760 return false;
2761}
2762
2763size_t Scalar::GetAsMemoryData(void *dst, size_t dst_len,
2764 lldb::ByteOrder dst_byte_order,
2765 Error &error) const {
2766 // Get a data extractor that points to the native scalar data
2767 DataExtractor data;
2768 if (!GetData(data)) {
1
Calling 'Scalar::GetData'
2769 error.SetErrorString("invalid scalar value");
2770 return 0;
2771 }
2772
2773 const size_t src_len = data.GetByteSize();
2774
2775 // Prepare a memory buffer that contains some or all of the register value
2776 const size_t bytes_copied =
2777 data.CopyByteOrderedData(0, // src offset
2778 src_len, // src length
2779 dst, // dst buffer
2780 dst_len, // dst length
2781 dst_byte_order); // dst byte order
2782 if (bytes_copied == 0)
2783 error.SetErrorString("failed to copy data");
2784
2785 return bytes_copied;
2786}
2787
2788bool Scalar::ExtractBitfield(uint32_t bit_size, uint32_t bit_offset) {
2789 if (bit_size == 0)
2790 return true;
2791
2792 switch (m_type) {
2793 case Scalar::e_void:
2794 case Scalar::e_float:
2795 case Scalar::e_double:
2796 case Scalar::e_long_double:
2797 break;
2798
2799 case Scalar::e_sint:
2800 case Scalar::e_slong:
2801 case Scalar::e_slonglong:
2802 case Scalar::e_sint128:
2803 case Scalar::e_sint256:
2804 m_integer = m_integer.ashr(bit_offset)
2805 .sextOrTrunc(bit_size)
2806 .sextOrSelf(8 * GetByteSize());
2807 return true;
2808
2809 case Scalar::e_uint:
2810 case Scalar::e_ulong:
2811 case Scalar::e_ulonglong:
2812 case Scalar::e_uint128:
2813 case Scalar::e_uint256:
2814 m_integer = m_integer.lshr(bit_offset)
2815 .zextOrTrunc(bit_size)
2816 .zextOrSelf(8 * GetByteSize());
2817 return true;
2818 }
2819 return false;
2820}
2821
2822bool lldb_private::operator==(const Scalar &lhs, const Scalar &rhs) {
2823 // If either entry is void then we can just compare the types
2824 if (lhs.m_type == Scalar::e_void || rhs.m_type == Scalar::e_void)
2825 return lhs.m_type == rhs.m_type;
2826
2827 Scalar temp_value;
2828 const Scalar *a;
2829 const Scalar *b;
2830 llvm::APFloat::cmpResult result;
2831 switch (PromoteToMaxType(lhs, rhs, temp_value, a, b)) {
2832 case Scalar::e_void:
2833 break;
2834 case Scalar::e_sint:
2835 case Scalar::e_uint:
2836 case Scalar::e_slong:
2837 case Scalar::e_ulong:
2838 case Scalar::e_slonglong:
2839 case Scalar::e_ulonglong:
2840 case Scalar::e_sint128:
2841 case Scalar::e_uint128:
2842 case Scalar::e_sint256:
2843 case Scalar::e_uint256:
2844 return a->m_integer == b->m_integer;
2845 case Scalar::e_float:
2846 case Scalar::e_double:
2847 case Scalar::e_long_double:
2848 result = a->m_float.compare(b->m_float);
2849 if (result == llvm::APFloat::cmpEqual)
2850 return true;
2851 }
2852 return false;
2853}
2854
2855bool lldb_private::operator!=(const Scalar &lhs, const Scalar &rhs) {
2856 // If either entry is void then we can just compare the types
2857 if (lhs.m_type == Scalar::e_void || rhs.m_type == Scalar::e_void)
2858 return lhs.m_type != rhs.m_type;
2859
2860 Scalar
2861 temp_value; // A temp value that might get a copy of either promoted value
2862 const Scalar *a;
2863 const Scalar *b;
2864 llvm::APFloat::cmpResult result;
2865 switch (PromoteToMaxType(lhs, rhs, temp_value, a, b)) {
2866 case Scalar::e_void:
2867 break;
2868 case Scalar::e_sint:
2869 case Scalar::e_uint:
2870 case Scalar::e_slong:
2871 case Scalar::e_ulong:
2872 case Scalar::e_slonglong:
2873 case Scalar::e_ulonglong:
2874 case Scalar::e_sint128:
2875 case Scalar::e_uint128:
2876 case Scalar::e_sint256:
2877 case Scalar::e_uint256:
2878 return a->m_integer != b->m_integer;
2879 case Scalar::e_float:
2880 case Scalar::e_double:
2881 case Scalar::e_long_double:
2882 result = a->m_float.compare(b->m_float);
2883 if (result != llvm::APFloat::cmpEqual)
2884 return true;
2885 }
2886 return true;
2887}
2888
2889bool lldb_private::operator<(const Scalar &lhs, const Scalar &rhs) {
2890 if (lhs.m_type == Scalar::e_void || rhs.m_type == Scalar::e_void)
2891 return false;
2892
2893 Scalar temp_value;
2894 const Scalar *a;
2895 const Scalar *b;
2896 llvm::APFloat::cmpResult result;
2897 switch (PromoteToMaxType(lhs, rhs, temp_value, a, b)) {
2898 case Scalar::e_void:
2899 break;
2900 case Scalar::e_sint:
2901 case Scalar::e_slong:
2902 case Scalar::e_slonglong:
2903 case Scalar::e_sint128:
2904 case Scalar::e_sint256:
2905 return a->m_integer.slt(b->m_integer);
2906 case Scalar::e_uint:
2907 case Scalar::e_ulong:
2908 case Scalar::e_ulonglong:
2909 case Scalar::e_uint128:
2910 case Scalar::e_uint256:
2911 return a->m_integer.ult(b->m_integer);
2912 case Scalar::e_float:
2913 case Scalar::e_double:
2914 case Scalar::e_long_double:
2915 result = a->m_float.compare(b->m_float);
2916 if (result == llvm::APFloat::cmpLessThan)
2917 return true;
2918 }
2919 return false;
2920}
2921
2922bool lldb_private::operator<=(const Scalar &lhs, const Scalar &rhs) {
2923 if (lhs.m_type == Scalar::e_void || rhs.m_type == Scalar::e_void)
2924 return false;
2925
2926 Scalar temp_value;
2927 const Scalar *a;
2928 const Scalar *b;
2929 llvm::APFloat::cmpResult result;
2930 switch (PromoteToMaxType(lhs, rhs, temp_value, a, b)) {
2931 case Scalar::e_void:
2932 break;
2933 case Scalar::e_sint:
2934 case Scalar::e_slong:
2935 case Scalar::e_slonglong:
2936 case Scalar::e_sint128:
2937 case Scalar::e_sint256:
2938 return a->m_integer.sle(b->m_integer);
2939 case Scalar::e_uint:
2940 case Scalar::e_ulong:
2941 case Scalar::e_ulonglong:
2942 case Scalar::e_uint128:
2943 case Scalar::e_uint256:
2944 return a->m_integer.ule(b->m_integer);
2945 case Scalar::e_float:
2946 case Scalar::e_double:
2947 case Scalar::e_long_double:
2948 result = a->m_float.compare(b->m_float);
2949 if (result == llvm::APFloat::cmpLessThan ||
2950 result == llvm::APFloat::cmpEqual)
2951 return true;
2952 }
2953 return false;
2954}
2955
2956bool lldb_private::operator>(const Scalar &lhs, const Scalar &rhs) {
2957 if (lhs.m_type == Scalar::e_void || rhs.m_type == Scalar::e_void)
2958 return false;
2959
2960 Scalar temp_value;
2961 const Scalar *a;
2962 const Scalar *b;
2963 llvm::APFloat::cmpResult result;
2964 switch (PromoteToMaxType(lhs, rhs, temp_value, a, b)) {
2965 case Scalar::e_void:
2966 break;
2967 case Scalar::e_sint:
2968 case Scalar::e_slong:
2969 case Scalar::e_slonglong:
2970 case Scalar::e_sint128:
2971 case Scalar::e_sint256:
2972 return a->m_integer.sgt(b->m_integer);
2973 case Scalar::e_uint:
2974 case Scalar::e_ulong:
2975 case Scalar::e_ulonglong:
2976 case Scalar::e_uint128:
2977 case Scalar::e_uint256:
2978 return a->m_integer.ugt(b->m_integer);
2979 case Scalar::e_float:
2980 case Scalar::e_double:
2981 case Scalar::e_long_double:
2982 result = a->m_float.compare(b->m_float);
2983 if (result == llvm::APFloat::cmpGreaterThan)
2984 return true;
2985 }
2986 return false;
2987}
2988
2989bool lldb_private::operator>=(const Scalar &lhs, const Scalar &rhs) {
2990 if (lhs.m_type == Scalar::e_void || rhs.m_type == Scalar::e_void)
2991 return false;
2992
2993 Scalar temp_value;
2994 const Scalar *a;
2995 const Scalar *b;
2996 llvm::APFloat::cmpResult result;
2997 switch (PromoteToMaxType(lhs, rhs, temp_value, a, b)) {
2998 case Scalar::e_void:
2999 break;
3000 case Scalar::e_sint:
3001 case Scalar::e_slong:
3002 case Scalar::e_slonglong:
3003 case Scalar::e_sint128:
3004 case Scalar::e_sint256:
3005 return a->m_integer.sge(b->m_integer);
3006 case Scalar::e_uint:
3007 case Scalar::e_ulong:
3008 case Scalar::e_ulonglong:
3009 case Scalar::e_uint128:
3010 case Scalar::e_uint256:
3011 return a->m_integer.uge(b->m_integer);
3012 case Scalar::e_float:
3013 case Scalar::e_double:
3014 case Scalar::e_long_double:
3015 result = a->m_float.compare(b->m_float);
3016 if (result == llvm::APFloat::cmpGreaterThan ||
3017 result == llvm::APFloat::cmpEqual)
3018 return true;
3019 }
3020 return false;
3021}
3022
3023bool Scalar::ClearBit(uint32_t bit) {
3024 switch (m_type) {
3025 case e_void:
3026 break;
3027 case e_sint:
3028 case e_uint:
3029 case e_slong:
3030 case e_ulong:
3031 case e_slonglong:
3032 case e_ulonglong:
3033 case e_sint128:
3034 case e_uint128:
3035 case e_sint256:
3036 case e_uint256:
3037 m_integer.clearBit(bit);
3038 return true;
3039 case e_float:
3040 case e_double:
3041 case e_long_double:
3042 break;
3043 }
3044 return false;
3045}
3046
3047bool Scalar::SetBit(uint32_t bit) {
3048 switch (m_type) {
3049 case e_void:
3050 break;
3051 case e_sint:
3052 case e_uint:
3053 case e_slong:
3054 case e_ulong:
3055 case e_slonglong:
3056 case e_ulonglong:
3057 case e_sint128:
3058 case e_uint128:
3059 case e_sint256:
3060 case e_uint256:
3061 m_integer.setBit(bit);
3062 return true;
3063 case e_float:
3064 case e_double:
3065 case e_long_double:
3066 break;
3067 }
3068 return false;
3069}