1 //===-- Scalar.cpp --------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lldb/Utility/Scalar.h" 10 #include "lldb/Utility/DataBufferHeap.h" 11 #include "lldb/Utility/DataExtractor.h" 12 #include "lldb/Utility/Endian.h" 13 #include "lldb/Utility/Status.h" 14 #include "lldb/Utility/Stream.h" 15 #include "lldb/Utility/StreamString.h" 16 #include "lldb/lldb-types.h" 17 #include "llvm/ADT/APSInt.h" 18 #include "llvm/ADT/SmallString.h" 19 20 #include <cinttypes> 21 #include <cstdio> 22 23 using namespace lldb; 24 using namespace lldb_private; 25 26 using llvm::APFloat; 27 28 namespace { 29 enum class Category { Void, Integral, Float }; 30 } 31 32 static Category GetCategory(Scalar::Type type) { 33 switch (type) { 34 case Scalar::e_void: 35 return Category::Void; 36 case Scalar::e_float: 37 case Scalar::e_double: 38 case Scalar::e_long_double: 39 return Category::Float; 40 case Scalar::e_sint: 41 case Scalar::e_slong: 42 case Scalar::e_slonglong: 43 case Scalar::e_sint128: 44 case Scalar::e_sint256: 45 case Scalar::e_sint512: 46 case Scalar::e_uint: 47 case Scalar::e_ulong: 48 case Scalar::e_ulonglong: 49 case Scalar::e_uint128: 50 case Scalar::e_uint256: 51 case Scalar::e_uint512: 52 return Category::Integral; 53 } 54 llvm_unreachable("Unhandled type!"); 55 } 56 57 static bool IsSigned(Scalar::Type type) { 58 switch (type) { 59 case Scalar::e_void: 60 case Scalar::e_uint: 61 case Scalar::e_ulong: 62 case Scalar::e_ulonglong: 63 case Scalar::e_uint128: 64 case Scalar::e_uint256: 65 case Scalar::e_uint512: 66 return false; 67 case Scalar::e_sint: 68 case Scalar::e_slong: 69 case Scalar::e_slonglong: 70 case Scalar::e_sint128: 71 case Scalar::e_sint256: 72 case Scalar::e_sint512: 73 case Scalar::e_float: 74 case Scalar::e_double: 75 case Scalar::e_long_double: 76 return true; 77 } 78 llvm_unreachable("Unhandled type!"); 79 } 80 81 82 // Promote to max type currently follows the ANSI C rule for type promotion in 83 // expressions. 84 static Scalar::Type PromoteToMaxType( 85 const Scalar &lhs, // The const left hand side object 86 const Scalar &rhs, // The const right hand side object 87 Scalar &temp_value, // A modifiable temp value than can be used to hold 88 // either the promoted lhs or rhs object 89 const Scalar *&promoted_lhs_ptr, // Pointer to the resulting possibly 90 // promoted value of lhs (at most one of 91 // lhs/rhs will get promoted) 92 const Scalar *&promoted_rhs_ptr // Pointer to the resulting possibly 93 // promoted value of rhs (at most one of 94 // lhs/rhs will get promoted) 95 ) { 96 Scalar result; 97 // Initialize the promoted values for both the right and left hand side 98 // values to be the objects themselves. If no promotion is needed (both right 99 // and left have the same type), then the temp_value will not get used. 100 promoted_lhs_ptr = &lhs; 101 promoted_rhs_ptr = &rhs; 102 // Extract the types of both the right and left hand side values 103 Scalar::Type lhs_type = lhs.GetType(); 104 Scalar::Type rhs_type = rhs.GetType(); 105 106 if (lhs_type > rhs_type) { 107 // Right hand side need to be promoted 108 temp_value = rhs; // Copy right hand side into the temp value 109 if (temp_value.Promote(lhs_type)) // Promote it 110 promoted_rhs_ptr = 111 &temp_value; // Update the pointer for the promoted right hand side 112 } else if (lhs_type < rhs_type) { 113 // Left hand side need to be promoted 114 temp_value = lhs; // Copy left hand side value into the temp value 115 if (temp_value.Promote(rhs_type)) // Promote it 116 promoted_lhs_ptr = 117 &temp_value; // Update the pointer for the promoted left hand side 118 } 119 120 // Make sure our type promotion worked as expected 121 if (promoted_lhs_ptr->GetType() == promoted_rhs_ptr->GetType()) 122 return promoted_lhs_ptr->GetType(); // Return the resulting max type 123 124 // Return the void type (zero) if we fail to promote either of the values. 125 return Scalar::e_void; 126 } 127 128 Scalar::Scalar() : m_type(e_void), m_float(static_cast<float>(0)) {} 129 130 bool Scalar::GetData(DataExtractor &data, size_t limit_byte_size) const { 131 size_t byte_size = GetByteSize(); 132 if (byte_size == 0) { 133 data.Clear(); 134 return false; 135 } 136 auto buffer_up = std::make_unique<DataBufferHeap>(byte_size, 0); 137 GetBytes(buffer_up->GetData()); 138 lldb::offset_t offset = 0; 139 140 if (limit_byte_size < byte_size) { 141 if (endian::InlHostByteOrder() == eByteOrderLittle) { 142 // On little endian systems if we want fewer bytes from the current 143 // type we just specify fewer bytes since the LSByte is first... 144 byte_size = limit_byte_size; 145 } else if (endian::InlHostByteOrder() == eByteOrderBig) { 146 // On big endian systems if we want fewer bytes from the current type 147 // have to advance our initial byte pointer and trim down the number of 148 // bytes since the MSByte is first 149 offset = byte_size - limit_byte_size; 150 byte_size = limit_byte_size; 151 } 152 } 153 154 data.SetData(std::move(buffer_up), offset, byte_size); 155 data.SetByteOrder(endian::InlHostByteOrder()); 156 return true; 157 } 158 159 void Scalar::GetBytes(llvm::MutableArrayRef<uint8_t> storage) const { 160 assert(storage.size() >= GetByteSize()); 161 162 const auto &store = [&](const llvm::APInt val) { 163 StoreIntToMemory(val, storage.data(), (val.getBitWidth() + 7) / 8); 164 }; 165 switch (GetCategory(m_type)) { 166 case Category::Void: 167 break; 168 case Category::Integral: 169 store(m_integer); 170 break; 171 case Category::Float: 172 store(m_float.bitcastToAPInt()); 173 break; 174 } 175 } 176 177 size_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 case e_sint512: 192 case e_uint512: 193 return (m_integer.getBitWidth() / 8); 194 case e_float: 195 return sizeof(float_t); 196 case e_double: 197 return sizeof(double_t); 198 case e_long_double: 199 return sizeof(long_double_t); 200 } 201 return 0; 202 } 203 204 bool Scalar::IsZero() const { 205 switch (GetCategory(m_type)) { 206 case Category::Void: 207 break; 208 case Category::Integral: 209 return m_integer.isNullValue(); 210 case Category::Float: 211 return m_float.isZero(); 212 } 213 return false; 214 } 215 216 void Scalar::GetValue(Stream *s, bool show_type) const { 217 if (show_type) 218 s->Printf("(%s) ", GetTypeAsCString()); 219 220 switch (GetCategory(m_type)) { 221 case Category::Void: 222 break; 223 case Category::Integral: 224 s->PutCString(m_integer.toString(10, IsSigned(m_type))); 225 break; 226 case Category::Float: 227 llvm::SmallString<24> string; 228 m_float.toString(string); 229 s->PutCString(string); 230 break; 231 } 232 } 233 234 Scalar::~Scalar() = default; 235 236 Scalar::Type Scalar::GetBestTypeForBitSize(size_t bit_size, bool sign) { 237 // Scalar types are always host types, hence the sizeof(). 238 if (sign) { 239 if (bit_size <= sizeof(int)*8) return Scalar::e_sint; 240 if (bit_size <= sizeof(long)*8) return Scalar::e_slong; 241 if (bit_size <= sizeof(long long)*8) return Scalar::e_slonglong; 242 if (bit_size <= 128) return Scalar::e_sint128; 243 if (bit_size <= 256) return Scalar::e_sint256; 244 if (bit_size <= 512) return Scalar::e_sint512; 245 } else { 246 if (bit_size <= sizeof(unsigned int)*8) return Scalar::e_uint; 247 if (bit_size <= sizeof(unsigned long)*8) return Scalar::e_ulong; 248 if (bit_size <= sizeof(unsigned long long)*8) return Scalar::e_ulonglong; 249 if (bit_size <= 128) return Scalar::e_uint128; 250 if (bit_size <= 256) return Scalar::e_uint256; 251 if (bit_size <= 512) return Scalar::e_uint512; 252 } 253 return Scalar::e_void; 254 } 255 256 void Scalar::TruncOrExtendTo(uint16_t bits, bool sign) { 257 m_integer = sign ? m_integer.sextOrTrunc(bits) : m_integer.zextOrTrunc(bits); 258 m_type = GetBestTypeForBitSize(bits, sign); 259 } 260 261 static size_t GetBitSize(Scalar::Type type) { 262 switch (type) { 263 case Scalar::e_void: 264 return 0; 265 case Scalar::e_sint: 266 return 8 * sizeof(int); 267 case Scalar::e_uint: 268 return 8 * sizeof(unsigned int); 269 case Scalar::e_slong: 270 return 8 * sizeof(long); 271 case Scalar::e_ulong: 272 return 8 * sizeof(unsigned long); 273 case Scalar::e_slonglong: 274 return 8 * sizeof(long long); 275 case Scalar::e_ulonglong: 276 return 8 * sizeof(unsigned long long); 277 case Scalar::e_sint128: 278 case Scalar::e_uint128: 279 return BITWIDTH_INT128; 280 case Scalar::e_sint256: 281 case Scalar::e_uint256: 282 return BITWIDTH_INT256; 283 case Scalar::e_sint512: 284 case Scalar::e_uint512: 285 return BITWIDTH_INT512; 286 case Scalar::e_float: 287 return 8 * sizeof(float); 288 case Scalar::e_double: 289 return 8 * sizeof(double); 290 case Scalar::e_long_double: 291 return 8 * sizeof(long double); 292 } 293 llvm_unreachable("Unhandled type!"); 294 } 295 296 static const llvm::fltSemantics &GetFltSemantics(Scalar::Type type) { 297 switch (type) { 298 case Scalar::e_void: 299 case Scalar::e_sint: 300 case Scalar::e_slong: 301 case Scalar::e_slonglong: 302 case Scalar::e_sint128: 303 case Scalar::e_sint256: 304 case Scalar::e_sint512: 305 case Scalar::e_uint: 306 case Scalar::e_ulong: 307 case Scalar::e_ulonglong: 308 case Scalar::e_uint128: 309 case Scalar::e_uint256: 310 case Scalar::e_uint512: 311 llvm_unreachable("Only floating point types supported!"); 312 case Scalar::e_float: 313 return llvm::APFloat::IEEEsingle(); 314 case Scalar::e_double: 315 return llvm::APFloat::IEEEdouble(); 316 case Scalar::e_long_double: 317 return llvm::APFloat::x87DoubleExtended(); 318 } 319 llvm_unreachable("Unhandled type!"); 320 } 321 322 bool Scalar::Promote(Scalar::Type type) { 323 bool success = false; 324 switch (GetCategory(m_type)) { 325 case Category::Void: 326 break; 327 case Category::Integral: 328 switch (GetCategory(type)) { 329 case Category::Void: 330 break; 331 case Category::Integral: 332 if (type < m_type) 333 break; 334 success = true; 335 if (IsSigned(m_type)) 336 m_integer = m_integer.sextOrTrunc(GetBitSize(type)); 337 else 338 m_integer = m_integer.zextOrTrunc(GetBitSize(type)); 339 break; 340 case Category::Float: 341 m_float = llvm::APFloat(GetFltSemantics(type)); 342 m_float.convertFromAPInt(m_integer, IsSigned(m_type), 343 llvm::APFloat::rmNearestTiesToEven); 344 success = true; 345 break; 346 } 347 break; 348 case Category::Float: 349 switch (GetCategory(type)) { 350 case Category::Void: 351 case Category::Integral: 352 break; 353 case Category::Float: 354 if (type < m_type) 355 break; 356 bool ignore; 357 success = true; 358 m_float.convert(GetFltSemantics(type), llvm::APFloat::rmNearestTiesToEven, 359 &ignore); 360 } 361 } 362 363 if (success) 364 m_type = type; 365 return success; 366 } 367 368 const char *Scalar::GetValueTypeAsCString(Scalar::Type type) { 369 switch (type) { 370 case e_void: 371 return "void"; 372 case e_sint: 373 return "int"; 374 case e_uint: 375 return "unsigned int"; 376 case e_slong: 377 return "long"; 378 case e_ulong: 379 return "unsigned long"; 380 case e_slonglong: 381 return "long long"; 382 case e_ulonglong: 383 return "unsigned long long"; 384 case e_float: 385 return "float"; 386 case e_double: 387 return "double"; 388 case e_long_double: 389 return "long double"; 390 case e_sint128: 391 return "int128_t"; 392 case e_uint128: 393 return "uint128_t"; 394 case e_sint256: 395 return "int256_t"; 396 case e_uint256: 397 return "uint256_t"; 398 case e_sint512: 399 return "int512_t"; 400 case e_uint512: 401 return "uint512_t"; 402 } 403 return "???"; 404 } 405 406 Scalar::Type 407 Scalar::GetValueTypeForSignedIntegerWithByteSize(size_t byte_size) { 408 if (byte_size <= sizeof(sint_t)) 409 return e_sint; 410 if (byte_size <= sizeof(slong_t)) 411 return e_slong; 412 if (byte_size <= sizeof(slonglong_t)) 413 return e_slonglong; 414 return e_void; 415 } 416 417 Scalar::Type 418 Scalar::GetValueTypeForUnsignedIntegerWithByteSize(size_t byte_size) { 419 if (byte_size <= sizeof(uint_t)) 420 return e_uint; 421 if (byte_size <= sizeof(ulong_t)) 422 return e_ulong; 423 if (byte_size <= sizeof(ulonglong_t)) 424 return e_ulonglong; 425 return e_void; 426 } 427 428 Scalar::Type Scalar::GetValueTypeForFloatWithByteSize(size_t byte_size) { 429 if (byte_size == sizeof(float_t)) 430 return e_float; 431 if (byte_size == sizeof(double_t)) 432 return e_double; 433 if (byte_size == sizeof(long_double_t)) 434 return e_long_double; 435 return e_void; 436 } 437 438 bool Scalar::MakeSigned() { 439 bool success = false; 440 441 switch (m_type) { 442 case e_void: 443 break; 444 case e_sint: 445 success = true; 446 break; 447 case e_uint: 448 m_type = e_sint; 449 success = true; 450 break; 451 case e_slong: 452 success = true; 453 break; 454 case e_ulong: 455 m_type = e_slong; 456 success = true; 457 break; 458 case e_slonglong: 459 success = true; 460 break; 461 case e_ulonglong: 462 m_type = e_slonglong; 463 success = true; 464 break; 465 case e_sint128: 466 success = true; 467 break; 468 case e_uint128: 469 m_type = e_sint128; 470 success = true; 471 break; 472 case e_sint256: 473 success = true; 474 break; 475 case e_uint256: 476 m_type = e_sint256; 477 success = true; 478 break; 479 case e_sint512: 480 success = true; 481 break; 482 case e_uint512: 483 m_type = e_sint512; 484 success = true; 485 break; 486 case e_float: 487 success = true; 488 break; 489 case e_double: 490 success = true; 491 break; 492 case e_long_double: 493 success = true; 494 break; 495 } 496 497 return success; 498 } 499 500 bool Scalar::MakeUnsigned() { 501 bool success = false; 502 503 switch (m_type) { 504 case e_void: 505 break; 506 case e_sint: 507 m_type = e_uint; 508 success = true; 509 break; 510 case e_uint: 511 success = true; 512 break; 513 case e_slong: 514 m_type = e_ulong; 515 success = true; 516 break; 517 case e_ulong: 518 success = true; 519 break; 520 case e_slonglong: 521 m_type = e_ulonglong; 522 success = true; 523 break; 524 case e_ulonglong: 525 success = true; 526 break; 527 case e_sint128: 528 m_type = e_uint128; 529 success = true; 530 break; 531 case e_uint128: 532 success = true; 533 break; 534 case e_sint256: 535 m_type = e_uint256; 536 success = true; 537 break; 538 case e_uint256: 539 success = true; 540 break; 541 case e_sint512: 542 m_type = e_uint512; 543 success = true; 544 break; 545 case e_uint512: 546 success = true; 547 break; 548 case e_float: 549 success = true; 550 break; 551 case e_double: 552 success = true; 553 break; 554 case e_long_double: 555 success = true; 556 break; 557 } 558 559 return success; 560 } 561 562 static llvm::APInt ToAPInt(const llvm::APFloat &f, unsigned bits, 563 bool is_unsigned) { 564 llvm::APSInt result(bits, is_unsigned); 565 bool isExact; 566 f.convertToInteger(result, llvm::APFloat::rmTowardZero, &isExact); 567 return std::move(result); 568 } 569 570 template <typename T> T Scalar::GetAs(T fail_value) const { 571 switch (GetCategory(m_type)) { 572 case Category::Void: 573 break; 574 case Category::Integral: 575 if (IsSigned(m_type)) 576 return m_integer.sextOrTrunc(sizeof(T) * 8).getSExtValue(); 577 return m_integer.zextOrTrunc(sizeof(T) * 8).getZExtValue(); 578 case Category::Float: 579 return ToAPInt(m_float, sizeof(T) * 8, std::is_unsigned<T>::value) 580 .getSExtValue(); 581 } 582 return fail_value; 583 } 584 585 signed char Scalar::SChar(signed char fail_value) const { 586 return GetAs<signed char>(fail_value); 587 } 588 589 unsigned char Scalar::UChar(unsigned char fail_value) const { 590 return GetAs<unsigned char>(fail_value); 591 } 592 593 short Scalar::SShort(short fail_value) const { 594 return GetAs<short>(fail_value); 595 } 596 597 unsigned short Scalar::UShort(unsigned short fail_value) const { 598 return GetAs<unsigned short>(fail_value); 599 } 600 601 int Scalar::SInt(int fail_value) const { return GetAs<int>(fail_value); } 602 603 unsigned int Scalar::UInt(unsigned int fail_value) const { 604 return GetAs<unsigned int>(fail_value); 605 } 606 607 long Scalar::SLong(long fail_value) const { return GetAs<long>(fail_value); } 608 609 unsigned long Scalar::ULong(unsigned long fail_value) const { 610 return GetAs<unsigned long>(fail_value); 611 } 612 613 long long Scalar::SLongLong(long long fail_value) const { 614 return GetAs<long long>(fail_value); 615 } 616 617 unsigned long long Scalar::ULongLong(unsigned long long fail_value) const { 618 return GetAs<unsigned long long>(fail_value); 619 } 620 621 llvm::APInt Scalar::SInt128(const llvm::APInt &fail_value) const { 622 switch (GetCategory(m_type)) { 623 case Category::Void: 624 break; 625 case Category::Integral: 626 return m_integer; 627 case Category::Float: 628 return ToAPInt(m_float, 128, /*is_unsigned=*/false); 629 } 630 return fail_value; 631 } 632 633 llvm::APInt Scalar::UInt128(const llvm::APInt &fail_value) const { 634 switch (GetCategory(m_type)) { 635 case Category::Void: 636 break; 637 case Category::Integral: 638 return m_integer; 639 case Category::Float: 640 return ToAPInt(m_float, 128, /*is_unsigned=*/true); 641 } 642 return fail_value; 643 } 644 645 float Scalar::Float(float fail_value) const { 646 switch (GetCategory(m_type)) { 647 case Category::Void: 648 break; 649 case Category::Integral: 650 if (IsSigned(m_type)) 651 return llvm::APIntOps::RoundSignedAPIntToFloat(m_integer); 652 return llvm::APIntOps::RoundAPIntToFloat(m_integer); 653 654 case Category::Float: { 655 APFloat result = m_float; 656 bool losesInfo; 657 result.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, 658 &losesInfo); 659 return result.convertToFloat(); 660 } 661 } 662 return fail_value; 663 } 664 665 double Scalar::Double(double fail_value) const { 666 switch (GetCategory(m_type)) { 667 case Category::Void: 668 break; 669 case Category::Integral: 670 if (IsSigned(m_type)) 671 return llvm::APIntOps::RoundSignedAPIntToDouble(m_integer); 672 return llvm::APIntOps::RoundAPIntToDouble(m_integer); 673 674 case Category::Float: { 675 APFloat result = m_float; 676 bool losesInfo; 677 result.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, 678 &losesInfo); 679 return result.convertToDouble(); 680 } 681 } 682 return fail_value; 683 } 684 685 long double Scalar::LongDouble(long double fail_value) const { 686 /// No way to get more precision at the moment. 687 return static_cast<long double>(Double(fail_value)); 688 } 689 690 Scalar &Scalar::operator+=(const Scalar &rhs) { 691 Scalar temp_value; 692 const Scalar *a; 693 const Scalar *b; 694 if ((m_type = PromoteToMaxType(*this, rhs, temp_value, a, b)) != 695 Scalar::e_void) { 696 switch (GetCategory(m_type)) { 697 case Category::Void: 698 break; 699 case Category::Integral: 700 m_integer = a->m_integer + b->m_integer; 701 break; 702 703 case Category::Float: 704 m_float = a->m_float + b->m_float; 705 break; 706 } 707 } 708 return *this; 709 } 710 711 Scalar &Scalar::operator<<=(const Scalar &rhs) { 712 if (GetCategory(m_type) == Category::Integral && 713 GetCategory(rhs.m_type) == Category::Integral) 714 m_integer <<= rhs.m_integer; 715 else 716 m_type = e_void; 717 return *this; 718 } 719 720 bool Scalar::ShiftRightLogical(const Scalar &rhs) { 721 if (GetCategory(m_type) == Category::Integral && 722 GetCategory(rhs.m_type) == Category::Integral) { 723 m_integer = m_integer.lshr(rhs.m_integer); 724 return true; 725 } 726 m_type = e_void; 727 return false; 728 } 729 730 Scalar &Scalar::operator>>=(const Scalar &rhs) { 731 switch (m_type) { 732 case e_void: 733 case e_float: 734 case e_double: 735 case e_long_double: 736 m_type = e_void; 737 break; 738 739 case e_sint: 740 case e_uint: 741 case e_slong: 742 case e_ulong: 743 case e_slonglong: 744 case e_ulonglong: 745 case e_sint128: 746 case e_uint128: 747 case e_sint256: 748 case e_uint256: 749 case e_sint512: 750 case e_uint512: 751 switch (rhs.m_type) { 752 case e_void: 753 case e_float: 754 case e_double: 755 case e_long_double: 756 m_type = e_void; 757 break; 758 case e_sint: 759 case e_uint: 760 case e_slong: 761 case e_ulong: 762 case e_slonglong: 763 case e_ulonglong: 764 case e_sint128: 765 case e_uint128: 766 case e_sint256: 767 case e_uint256: 768 case e_sint512: 769 case e_uint512: 770 m_integer = m_integer.ashr(rhs.m_integer); 771 break; 772 } 773 break; 774 } 775 return *this; 776 } 777 778 Scalar &Scalar::operator&=(const Scalar &rhs) { 779 if (GetCategory(m_type) == Category::Integral && 780 GetCategory(rhs.m_type) == Category::Integral) 781 m_integer &= rhs.m_integer; 782 else 783 m_type = e_void; 784 return *this; 785 } 786 787 bool Scalar::AbsoluteValue() { 788 switch (m_type) { 789 case e_void: 790 break; 791 792 case e_sint: 793 case e_slong: 794 case e_slonglong: 795 case e_sint128: 796 case e_sint256: 797 case e_sint512: 798 if (m_integer.isNegative()) 799 m_integer = -m_integer; 800 return true; 801 802 case e_uint: 803 case e_ulong: 804 case e_ulonglong: 805 return true; 806 case e_uint128: 807 case e_uint256: 808 case e_uint512: 809 case e_float: 810 case e_double: 811 case e_long_double: 812 m_float.clearSign(); 813 return true; 814 } 815 return false; 816 } 817 818 bool Scalar::UnaryNegate() { 819 switch (GetCategory(m_type)) { 820 case Category::Void: 821 break; 822 case Category::Integral: 823 m_integer = -m_integer; 824 return true; 825 case Category::Float: 826 m_float.changeSign(); 827 return true; 828 } 829 return false; 830 } 831 832 bool Scalar::OnesComplement() { 833 if (GetCategory(m_type) == Category::Integral) { 834 m_integer = ~m_integer; 835 return true; 836 } 837 838 return false; 839 } 840 841 const Scalar lldb_private::operator+(const Scalar &lhs, const Scalar &rhs) { 842 Scalar result = lhs; 843 result += rhs; 844 return result; 845 } 846 847 const Scalar lldb_private::operator-(const Scalar &lhs, const Scalar &rhs) { 848 Scalar result; 849 Scalar temp_value; 850 const Scalar *a; 851 const Scalar *b; 852 if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) != 853 Scalar::e_void) { 854 switch (GetCategory(result.m_type)) { 855 case Category::Void: 856 break; 857 case Category::Integral: 858 result.m_integer = a->m_integer - b->m_integer; 859 break; 860 case Category::Float: 861 result.m_float = a->m_float - b->m_float; 862 break; 863 } 864 } 865 return result; 866 } 867 868 const Scalar lldb_private::operator/(const Scalar &lhs, const Scalar &rhs) { 869 Scalar result; 870 Scalar temp_value; 871 const Scalar *a; 872 const Scalar *b; 873 if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) != 874 Scalar::e_void && 875 !b->IsZero()) { 876 switch (GetCategory(result.m_type)) { 877 case Category::Void: 878 break; 879 case Category::Integral: 880 if (IsSigned(result.m_type)) 881 result.m_integer = a->m_integer.sdiv(b->m_integer); 882 else 883 result.m_integer = a->m_integer.udiv(b->m_integer); 884 return result; 885 case Category::Float: 886 result.m_float = a->m_float / b->m_float; 887 return result; 888 } 889 } 890 // For division only, the only way it should make it here is if a promotion 891 // failed, or if we are trying to do a divide by zero. 892 result.m_type = Scalar::e_void; 893 return result; 894 } 895 896 const Scalar lldb_private::operator*(const Scalar &lhs, const Scalar &rhs) { 897 Scalar result; 898 Scalar temp_value; 899 const Scalar *a; 900 const Scalar *b; 901 if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) != 902 Scalar::e_void) { 903 switch (GetCategory(result.m_type)) { 904 case Category::Void: 905 break; 906 case Category::Integral: 907 result.m_integer = a->m_integer * b->m_integer; 908 break; 909 case Category::Float: 910 result.m_float = a->m_float * b->m_float; 911 break; 912 } 913 } 914 return result; 915 } 916 917 const Scalar lldb_private::operator&(const Scalar &lhs, const Scalar &rhs) { 918 Scalar result; 919 Scalar temp_value; 920 const Scalar *a; 921 const Scalar *b; 922 if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) != 923 Scalar::e_void) { 924 if (GetCategory(result.m_type) == Category::Integral) 925 result.m_integer = a->m_integer & b->m_integer; 926 else 927 result.m_type = Scalar::e_void; 928 } 929 return result; 930 } 931 932 const Scalar lldb_private::operator|(const Scalar &lhs, const Scalar &rhs) { 933 Scalar result; 934 Scalar temp_value; 935 const Scalar *a; 936 const Scalar *b; 937 if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) != 938 Scalar::e_void) { 939 if (GetCategory(result.m_type) == Category::Integral) 940 result.m_integer = a->m_integer | b->m_integer; 941 else 942 result.m_type = Scalar::e_void; 943 } 944 return result; 945 } 946 947 const Scalar lldb_private::operator%(const Scalar &lhs, const Scalar &rhs) { 948 Scalar result; 949 Scalar temp_value; 950 const Scalar *a; 951 const Scalar *b; 952 if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) != 953 Scalar::e_void) { 954 if (!b->IsZero() && GetCategory(result.m_type) == Category::Integral) { 955 if (IsSigned(result.m_type)) 956 result.m_integer = a->m_integer.srem(b->m_integer); 957 else 958 result.m_integer = a->m_integer.urem(b->m_integer); 959 return result; 960 } 961 } 962 result.m_type = Scalar::e_void; 963 return result; 964 } 965 966 const Scalar lldb_private::operator^(const Scalar &lhs, const Scalar &rhs) { 967 Scalar result; 968 Scalar temp_value; 969 const Scalar *a; 970 const Scalar *b; 971 if ((result.m_type = PromoteToMaxType(lhs, rhs, temp_value, a, b)) != 972 Scalar::e_void) { 973 if (GetCategory(result.m_type) == Category::Integral) 974 result.m_integer = a->m_integer ^ b->m_integer; 975 else 976 result.m_type = Scalar::e_void; 977 } 978 return result; 979 } 980 981 const Scalar lldb_private::operator<<(const Scalar &lhs, const Scalar &rhs) { 982 Scalar result = lhs; 983 result <<= rhs; 984 return result; 985 } 986 987 const Scalar lldb_private::operator>>(const Scalar &lhs, const Scalar &rhs) { 988 Scalar result = lhs; 989 result >>= rhs; 990 return result; 991 } 992 993 Status Scalar::SetValueFromCString(const char *value_str, Encoding encoding, 994 size_t byte_size) { 995 Status error; 996 if (value_str == nullptr || value_str[0] == '\0') { 997 error.SetErrorString("Invalid c-string value string."); 998 return error; 999 } 1000 switch (encoding) { 1001 case eEncodingInvalid: 1002 error.SetErrorString("Invalid encoding."); 1003 break; 1004 1005 case eEncodingUint: 1006 if (byte_size <= sizeof(uint64_t)) { 1007 uint64_t uval64; 1008 if (!llvm::to_integer(value_str, uval64)) 1009 error.SetErrorStringWithFormat( 1010 "'%s' is not a valid unsigned integer string value", value_str); 1011 else if (!UIntValueIsValidForSize(uval64, byte_size)) 1012 error.SetErrorStringWithFormat( 1013 "value 0x%" PRIx64 " is too large to fit in a %" PRIu64 1014 " byte unsigned integer value", 1015 uval64, static_cast<uint64_t>(byte_size)); 1016 else { 1017 m_type = Scalar::GetValueTypeForUnsignedIntegerWithByteSize(byte_size); 1018 switch (m_type) { 1019 case e_uint: 1020 m_integer = llvm::APInt(sizeof(uint_t) * 8, uval64, false); 1021 break; 1022 case e_ulong: 1023 m_integer = llvm::APInt(sizeof(ulong_t) * 8, uval64, false); 1024 break; 1025 case e_ulonglong: 1026 m_integer = llvm::APInt(sizeof(ulonglong_t) * 8, uval64, false); 1027 break; 1028 default: 1029 error.SetErrorStringWithFormat( 1030 "unsupported unsigned integer byte size: %" PRIu64 "", 1031 static_cast<uint64_t>(byte_size)); 1032 break; 1033 } 1034 } 1035 } else { 1036 error.SetErrorStringWithFormat( 1037 "unsupported unsigned integer byte size: %" PRIu64 "", 1038 static_cast<uint64_t>(byte_size)); 1039 return error; 1040 } 1041 break; 1042 1043 case eEncodingSint: 1044 if (byte_size <= sizeof(int64_t)) { 1045 int64_t sval64; 1046 if (!llvm::to_integer(value_str, sval64)) 1047 error.SetErrorStringWithFormat( 1048 "'%s' is not a valid signed integer string value", value_str); 1049 else if (!SIntValueIsValidForSize(sval64, byte_size)) 1050 error.SetErrorStringWithFormat( 1051 "value 0x%" PRIx64 " is too large to fit in a %" PRIu64 1052 " byte signed integer value", 1053 sval64, static_cast<uint64_t>(byte_size)); 1054 else { 1055 m_type = Scalar::GetValueTypeForSignedIntegerWithByteSize(byte_size); 1056 switch (m_type) { 1057 case e_sint: 1058 m_integer = llvm::APInt(sizeof(sint_t) * 8, sval64, true); 1059 break; 1060 case e_slong: 1061 m_integer = llvm::APInt(sizeof(slong_t) * 8, sval64, true); 1062 break; 1063 case e_slonglong: 1064 m_integer = llvm::APInt(sizeof(slonglong_t) * 8, sval64, true); 1065 break; 1066 default: 1067 error.SetErrorStringWithFormat( 1068 "unsupported signed integer byte size: %" PRIu64 "", 1069 static_cast<uint64_t>(byte_size)); 1070 break; 1071 } 1072 } 1073 } else { 1074 error.SetErrorStringWithFormat( 1075 "unsupported signed integer byte size: %" PRIu64 "", 1076 static_cast<uint64_t>(byte_size)); 1077 return error; 1078 } 1079 break; 1080 1081 case eEncodingIEEE754: 1082 static float f_val; 1083 static double d_val; 1084 static long double l_val; 1085 if (byte_size == sizeof(float)) { 1086 if (::sscanf(value_str, "%f", &f_val) == 1) { 1087 m_float = llvm::APFloat(f_val); 1088 m_type = e_float; 1089 } else 1090 error.SetErrorStringWithFormat("'%s' is not a valid float string value", 1091 value_str); 1092 } else if (byte_size == sizeof(double)) { 1093 if (::sscanf(value_str, "%lf", &d_val) == 1) { 1094 m_float = llvm::APFloat(d_val); 1095 m_type = e_double; 1096 } else 1097 error.SetErrorStringWithFormat("'%s' is not a valid float string value", 1098 value_str); 1099 } else if (byte_size == sizeof(long double)) { 1100 if (::sscanf(value_str, "%Lf", &l_val) == 1) { 1101 m_float = llvm::APFloat( 1102 llvm::APFloat::x87DoubleExtended(), 1103 llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128, 1104 (reinterpret_cast<type128 *>(&l_val))->x)); 1105 m_type = e_long_double; 1106 } else 1107 error.SetErrorStringWithFormat("'%s' is not a valid float string value", 1108 value_str); 1109 } else { 1110 error.SetErrorStringWithFormat("unsupported float byte size: %" PRIu64 "", 1111 static_cast<uint64_t>(byte_size)); 1112 return error; 1113 } 1114 break; 1115 1116 case eEncodingVector: 1117 error.SetErrorString("vector encoding unsupported."); 1118 break; 1119 } 1120 if (error.Fail()) 1121 m_type = e_void; 1122 1123 return error; 1124 } 1125 1126 Status Scalar::SetValueFromData(DataExtractor &data, lldb::Encoding encoding, 1127 size_t byte_size) { 1128 Status error; 1129 1130 type128 int128; 1131 type256 int256; 1132 switch (encoding) { 1133 case lldb::eEncodingInvalid: 1134 error.SetErrorString("invalid encoding"); 1135 break; 1136 case lldb::eEncodingVector: 1137 error.SetErrorString("vector encoding unsupported"); 1138 break; 1139 case lldb::eEncodingUint: { 1140 lldb::offset_t offset = 0; 1141 1142 switch (byte_size) { 1143 case 1: 1144 operator=(data.GetU8(&offset)); 1145 break; 1146 case 2: 1147 operator=(data.GetU16(&offset)); 1148 break; 1149 case 4: 1150 operator=(data.GetU32(&offset)); 1151 break; 1152 case 8: 1153 operator=(data.GetU64(&offset)); 1154 break; 1155 case 16: 1156 if (data.GetByteOrder() == eByteOrderBig) { 1157 int128.x[1] = data.GetU64(&offset); 1158 int128.x[0] = data.GetU64(&offset); 1159 } else { 1160 int128.x[0] = data.GetU64(&offset); 1161 int128.x[1] = data.GetU64(&offset); 1162 } 1163 operator=(llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128, int128.x)); 1164 break; 1165 case 32: 1166 if (data.GetByteOrder() == eByteOrderBig) { 1167 int256.x[3] = data.GetU64(&offset); 1168 int256.x[2] = data.GetU64(&offset); 1169 int256.x[1] = data.GetU64(&offset); 1170 int256.x[0] = data.GetU64(&offset); 1171 } else { 1172 int256.x[0] = data.GetU64(&offset); 1173 int256.x[1] = data.GetU64(&offset); 1174 int256.x[2] = data.GetU64(&offset); 1175 int256.x[3] = data.GetU64(&offset); 1176 } 1177 operator=(llvm::APInt(BITWIDTH_INT256, NUM_OF_WORDS_INT256, int256.x)); 1178 break; 1179 default: 1180 error.SetErrorStringWithFormat( 1181 "unsupported unsigned integer byte size: %" PRIu64 "", 1182 static_cast<uint64_t>(byte_size)); 1183 break; 1184 } 1185 } break; 1186 case lldb::eEncodingSint: { 1187 lldb::offset_t offset = 0; 1188 1189 switch (byte_size) { 1190 case 1: 1191 operator=(static_cast<int8_t>(data.GetU8(&offset))); 1192 break; 1193 case 2: 1194 operator=(static_cast<int16_t>(data.GetU16(&offset))); 1195 break; 1196 case 4: 1197 operator=(static_cast<int32_t>(data.GetU32(&offset))); 1198 break; 1199 case 8: 1200 operator=(static_cast<int64_t>(data.GetU64(&offset))); 1201 break; 1202 case 16: 1203 if (data.GetByteOrder() == eByteOrderBig) { 1204 int128.x[1] = data.GetU64(&offset); 1205 int128.x[0] = data.GetU64(&offset); 1206 } else { 1207 int128.x[0] = data.GetU64(&offset); 1208 int128.x[1] = data.GetU64(&offset); 1209 } 1210 operator=(llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128, int128.x)); 1211 break; 1212 case 32: 1213 if (data.GetByteOrder() == eByteOrderBig) { 1214 int256.x[3] = data.GetU64(&offset); 1215 int256.x[2] = data.GetU64(&offset); 1216 int256.x[1] = data.GetU64(&offset); 1217 int256.x[0] = data.GetU64(&offset); 1218 } else { 1219 int256.x[0] = data.GetU64(&offset); 1220 int256.x[1] = data.GetU64(&offset); 1221 int256.x[2] = data.GetU64(&offset); 1222 int256.x[3] = data.GetU64(&offset); 1223 } 1224 operator=(llvm::APInt(BITWIDTH_INT256, NUM_OF_WORDS_INT256, int256.x)); 1225 break; 1226 default: 1227 error.SetErrorStringWithFormat( 1228 "unsupported signed integer byte size: %" PRIu64 "", 1229 static_cast<uint64_t>(byte_size)); 1230 break; 1231 } 1232 } break; 1233 case lldb::eEncodingIEEE754: { 1234 lldb::offset_t offset = 0; 1235 1236 if (byte_size == sizeof(float)) 1237 operator=(data.GetFloat(&offset)); 1238 else if (byte_size == sizeof(double)) 1239 operator=(data.GetDouble(&offset)); 1240 else if (byte_size == sizeof(long double)) 1241 operator=(data.GetLongDouble(&offset)); 1242 else 1243 error.SetErrorStringWithFormat("unsupported float byte size: %" PRIu64 "", 1244 static_cast<uint64_t>(byte_size)); 1245 } break; 1246 } 1247 1248 return error; 1249 } 1250 1251 bool Scalar::SignExtend(uint32_t sign_bit_pos) { 1252 const uint32_t max_bit_pos = GetByteSize() * 8; 1253 1254 if (sign_bit_pos < max_bit_pos) { 1255 switch (m_type) { 1256 case Scalar::e_void: 1257 case Scalar::e_float: 1258 case Scalar::e_double: 1259 case Scalar::e_long_double: 1260 return false; 1261 1262 case Scalar::e_sint: 1263 case Scalar::e_uint: 1264 case Scalar::e_slong: 1265 case Scalar::e_ulong: 1266 case Scalar::e_slonglong: 1267 case Scalar::e_ulonglong: 1268 case Scalar::e_sint128: 1269 case Scalar::e_uint128: 1270 case Scalar::e_sint256: 1271 case Scalar::e_uint256: 1272 case Scalar::e_sint512: 1273 case Scalar::e_uint512: 1274 if (max_bit_pos == sign_bit_pos) 1275 return true; 1276 else if (sign_bit_pos < (max_bit_pos - 1)) { 1277 llvm::APInt sign_bit = llvm::APInt::getSignMask(sign_bit_pos + 1); 1278 llvm::APInt bitwize_and = m_integer & sign_bit; 1279 if (bitwize_and.getBoolValue()) { 1280 const llvm::APInt mask = 1281 ~(sign_bit) + llvm::APInt(m_integer.getBitWidth(), 1); 1282 m_integer |= mask; 1283 } 1284 return true; 1285 } 1286 break; 1287 } 1288 } 1289 return false; 1290 } 1291 1292 size_t Scalar::GetAsMemoryData(void *dst, size_t dst_len, 1293 lldb::ByteOrder dst_byte_order, 1294 Status &error) const { 1295 // Get a data extractor that points to the native scalar data 1296 DataExtractor data; 1297 if (!GetData(data)) { 1298 error.SetErrorString("invalid scalar value"); 1299 return 0; 1300 } 1301 1302 const size_t src_len = data.GetByteSize(); 1303 1304 // Prepare a memory buffer that contains some or all of the register value 1305 const size_t bytes_copied = 1306 data.CopyByteOrderedData(0, // src offset 1307 src_len, // src length 1308 dst, // dst buffer 1309 dst_len, // dst length 1310 dst_byte_order); // dst byte order 1311 if (bytes_copied == 0) 1312 error.SetErrorString("failed to copy data"); 1313 1314 return bytes_copied; 1315 } 1316 1317 bool Scalar::ExtractBitfield(uint32_t bit_size, uint32_t bit_offset) { 1318 if (bit_size == 0) 1319 return true; 1320 1321 switch (m_type) { 1322 case Scalar::e_void: 1323 case Scalar::e_float: 1324 case Scalar::e_double: 1325 case Scalar::e_long_double: 1326 break; 1327 1328 case Scalar::e_sint: 1329 case Scalar::e_slong: 1330 case Scalar::e_slonglong: 1331 case Scalar::e_sint128: 1332 case Scalar::e_sint256: 1333 case Scalar::e_sint512: 1334 m_integer = m_integer.ashr(bit_offset) 1335 .sextOrTrunc(bit_size) 1336 .sextOrSelf(8 * GetByteSize()); 1337 return true; 1338 1339 case Scalar::e_uint: 1340 case Scalar::e_ulong: 1341 case Scalar::e_ulonglong: 1342 case Scalar::e_uint128: 1343 case Scalar::e_uint256: 1344 case Scalar::e_uint512: 1345 m_integer = m_integer.lshr(bit_offset) 1346 .zextOrTrunc(bit_size) 1347 .zextOrSelf(8 * GetByteSize()); 1348 return true; 1349 } 1350 return false; 1351 } 1352 1353 bool lldb_private::operator==(const Scalar &lhs, const Scalar &rhs) { 1354 // If either entry is void then we can just compare the types 1355 if (lhs.m_type == Scalar::e_void || rhs.m_type == Scalar::e_void) 1356 return lhs.m_type == rhs.m_type; 1357 1358 Scalar temp_value; 1359 const Scalar *a; 1360 const Scalar *b; 1361 llvm::APFloat::cmpResult result; 1362 switch (PromoteToMaxType(lhs, rhs, temp_value, a, b)) { 1363 case Scalar::e_void: 1364 break; 1365 case Scalar::e_sint: 1366 case Scalar::e_uint: 1367 case Scalar::e_slong: 1368 case Scalar::e_ulong: 1369 case Scalar::e_slonglong: 1370 case Scalar::e_ulonglong: 1371 case Scalar::e_sint128: 1372 case Scalar::e_uint128: 1373 case Scalar::e_sint256: 1374 case Scalar::e_uint256: 1375 case Scalar::e_sint512: 1376 case Scalar::e_uint512: 1377 return a->m_integer == b->m_integer; 1378 case Scalar::e_float: 1379 case Scalar::e_double: 1380 case Scalar::e_long_double: 1381 result = a->m_float.compare(b->m_float); 1382 if (result == llvm::APFloat::cmpEqual) 1383 return true; 1384 } 1385 return false; 1386 } 1387 1388 bool lldb_private::operator!=(const Scalar &lhs, const Scalar &rhs) { 1389 return !(lhs == rhs); 1390 } 1391 1392 bool lldb_private::operator<(const Scalar &lhs, const Scalar &rhs) { 1393 if (lhs.m_type == Scalar::e_void || rhs.m_type == Scalar::e_void) 1394 return false; 1395 1396 Scalar temp_value; 1397 const Scalar *a; 1398 const Scalar *b; 1399 llvm::APFloat::cmpResult result; 1400 switch (PromoteToMaxType(lhs, rhs, temp_value, a, b)) { 1401 case Scalar::e_void: 1402 break; 1403 case Scalar::e_sint: 1404 case Scalar::e_slong: 1405 case Scalar::e_slonglong: 1406 case Scalar::e_sint128: 1407 case Scalar::e_sint256: 1408 case Scalar::e_sint512: 1409 case Scalar::e_uint512: 1410 return a->m_integer.slt(b->m_integer); 1411 case Scalar::e_uint: 1412 case Scalar::e_ulong: 1413 case Scalar::e_ulonglong: 1414 case Scalar::e_uint128: 1415 case Scalar::e_uint256: 1416 return a->m_integer.ult(b->m_integer); 1417 case Scalar::e_float: 1418 case Scalar::e_double: 1419 case Scalar::e_long_double: 1420 result = a->m_float.compare(b->m_float); 1421 if (result == llvm::APFloat::cmpLessThan) 1422 return true; 1423 } 1424 return false; 1425 } 1426 1427 bool lldb_private::operator<=(const Scalar &lhs, const Scalar &rhs) { 1428 return !(rhs < lhs); 1429 } 1430 1431 bool lldb_private::operator>(const Scalar &lhs, const Scalar &rhs) { 1432 return rhs < lhs; 1433 } 1434 1435 bool lldb_private::operator>=(const Scalar &lhs, const Scalar &rhs) { 1436 return !(lhs < rhs); 1437 } 1438 1439 bool Scalar::ClearBit(uint32_t bit) { 1440 switch (m_type) { 1441 case e_void: 1442 break; 1443 case e_sint: 1444 case e_uint: 1445 case e_slong: 1446 case e_ulong: 1447 case e_slonglong: 1448 case e_ulonglong: 1449 case e_sint128: 1450 case e_uint128: 1451 case e_sint256: 1452 case e_uint256: 1453 case e_sint512: 1454 case e_uint512: 1455 m_integer.clearBit(bit); 1456 return true; 1457 case e_float: 1458 case e_double: 1459 case e_long_double: 1460 break; 1461 } 1462 return false; 1463 } 1464 1465 bool Scalar::SetBit(uint32_t bit) { 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 case e_sint512: 1480 case e_uint512: 1481 m_integer.setBit(bit); 1482 return true; 1483 case e_float: 1484 case e_double: 1485 case e_long_double: 1486 break; 1487 } 1488 return false; 1489 } 1490 1491 llvm::raw_ostream &lldb_private::operator<<(llvm::raw_ostream &os, const Scalar &scalar) { 1492 StreamString s; 1493 scalar.GetValue(&s, /*show_type*/ true); 1494 return os << s.GetString(); 1495 } 1496