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