Bug Summary

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

Annotated Source Code

Press '?' to see keyboard shortcuts

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