Bug Summary

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

Annotated Source Code

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