Bug Summary

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