17#include "llvm/ADT/APSInt.h"
18#include "llvm/ADT/SmallString.h"
19#include "llvm/ADT/StringExtras.h"
40 llvm_unreachable(
"Unhandled category!");
44 static const llvm::fltSemantics *
const order[] = {
45 &APFloat::IEEEsingle(), &APFloat::IEEEdouble(),
46 &APFloat::x87DoubleExtended()};
47 for (
const auto &entry : llvm::enumerate(order)) {
48 if (entry.value() == &sem)
51 llvm_unreachable(
"Unsupported semantics!");
58 switch (b.GetType()) {
72 if (lhs_key > rhs_key)
74 else if (rhs_key > lhs_key)
91 auto buffer_up = std::make_unique<DataBufferHeap>(byte_size, 0);
93 data.
SetData(std::move(buffer_up), 0, byte_size);
100 if (byte_size == 0 || result_byte_size == 0) {
109 if (result_byte_size <= byte_size) {
110 auto buffer_up = std::make_unique<DataBufferHeap>(byte_size, 0);
112 auto offset = byte_size - result_byte_size;
113 data.
SetData(std::move(buffer_up), offset, result_byte_size);
117 auto buffer_up = std::make_unique<DataBufferHeap>(result_byte_size, 0);
118 auto offset = result_byte_size - byte_size;
119 GetBytes(buffer_up->GetBytes() + offset, byte_size);
120 data.
SetData(std::move(buffer_up), 0, result_byte_size);
128 if (byte_size < result_byte_size)
129 byte_size = result_byte_size;
130 auto buffer_up = std::make_unique<DataBufferHeap>(byte_size, 0);
132 data.
SetData(std::move(buffer_up), 0, result_byte_size);
140 llvm::MutableArrayRef<uint8_t> storage_ref(storage, size);
147 const auto &store = [&](
const llvm::APInt &val) {
148 StoreIntToMemory(val, storage.data(), (val.getBitWidth() + 7) / 8);
157 store(
m_float.bitcastToAPInt());
167 return (
m_integer.getBitWidth() + 7) / 8;
169 return (
m_float.bitcastToAPInt().getBitWidth() + 7) / 8;
197 llvm::SmallString<24> string;
225 bool success =
false;
230 m_float = llvm::APFloat(semantics);
232 llvm::APFloat::rmNearestTiesToEven);
240 m_float.convert(semantics, llvm::APFloat::rmNearestTiesToEven, &ignore);
269 llvm_unreachable(
"Unrecognized type!");
273 bool success =
false;
291 bool success =
false;
310 llvm::APSInt result(
bits, is_unsigned);
312 f.convertToInteger(result, llvm::APFloat::rmTowardZero, &isExact);
313 return std::move(result);
321 APSInt ext =
m_integer.extOrTrunc(
sizeof(T) * 8);
323 return ext.getSExtValue();
324 return ext.getZExtValue();
327 return ToAPInt(
m_float,
sizeof(T) * 8, std::is_unsigned<T>::value)
399 return llvm::APIntOps::RoundSignedAPIntToFloat(
m_integer);
400 return llvm::APIntOps::RoundAPIntToFloat(
m_integer);
405 result.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven,
407 return result.convertToFloat();
419 return llvm::APIntOps::RoundSignedAPIntToDouble(
m_integer);
420 return llvm::APIntOps::RoundAPIntToDouble(
m_integer);
425 result.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven,
427 return result.convertToDouble();
435 return static_cast<long double>(
Double(fail_value));
651 if (value_str ==
nullptr || value_str[0] ==
'\0') {
661 llvm::StringRef str = value_str;
663 bool is_negative = is_signed && str.consume_front(
"-");
665 if (str.getAsInteger(0,
integer)) {
667 "'{0}' is not a valid integer string value", value_str);
675 fits =
integer.isSignedIntN(byte_size * 8);
677 fits =
integer.isIntN(byte_size * 8);
680 "value {0} is too large to fit in a {1} byte integer value",
681 value_str, byte_size);
686 APSInt(std::move(
integer), !is_signed).extOrTrunc(8 * byte_size);
694 const llvm::fltSemantics &sem =
695 byte_size <= 4 ? APFloat::IEEEsingle()
696 : byte_size <= 8 ? APFloat::IEEEdouble()
697 : APFloat::x87DoubleExtended();
699 if (llvm::Expected<APFloat::opStatus> op =
700 f.convertFromString(value_str, APFloat::rmNearestTiesToEven)) {
734 APSInt(APInt::getZero(8 * byte_size), encoding ==
eEncodingUint);
738 std::vector<uint8_t> buffer(byte_size);
739 std::copy_n(data.
GetDataStart(), byte_size, buffer.rbegin());
740 llvm::LoadIntFromMemory(
m_integer, buffer.data(), byte_size);
747 if (byte_size ==
sizeof(
float))
749 else if (byte_size ==
sizeof(
double))
751 else if (byte_size ==
sizeof(
long double))
755 "unsupported float byte size: {0}",
static_cast<uint64_t
>(byte_size));
765 if (sign_bit_pos < max_bit_pos) {
772 if (sign_bit_pos < (max_bit_pos - 1)) {
773 llvm::APInt sign_bit = llvm::APInt::getSignMask(sign_bit_pos + 1);
774 llvm::APInt bitwize_and =
m_integer & sign_bit;
775 if (bitwize_and.getBoolValue()) {
777 ~(sign_bit) + llvm::APInt(
m_integer.getBitWidth(), 1);
801 const size_t bytes_copied =
807 if (bytes_copied == 0)
831 switch (basic_type) {
833 return llvm::APFloat(
835 ? llvm::APIntOps::RoundSignedAPIntToFloat(
m_integer)
836 : llvm::APIntOps::RoundAPIntToFloat(
m_integer));
840 return llvm::APFloat(
842 ? llvm::APIntOps::RoundSignedAPIntToDouble(
m_integer)
843 : llvm::APIntOps::RoundAPIntToDouble(
m_integer));
845 const llvm::fltSemantics &sem = APFloat::IEEEsingle();
846 return llvm::APFloat::getNaN(sem);
851 switch (basic_type) {
854 m_float.convert(llvm::APFloat::IEEEsingle(),
855 llvm::APFloat::rmNearestTiesToEven, &loses_info);
862 m_float.convert(llvm::APFloat::IEEEdouble(),
863 llvm::APFloat::rmNearestTiesToEven, &loses_info);
867 const llvm::fltSemantics &sem = APFloat::IEEEsingle();
868 return llvm::APFloat::getNaN(sem);
875 return lhs.
m_type == rhs.
m_type ? APFloat::cmpEqual : APFloat::cmpUnordered;
882 return APFloat::cmpLessThan;
884 return APFloat::cmpGreaterThan;
885 return APFloat::cmpEqual;
889 return APFloat::cmpUnordered;
893 return compare(lhs, rhs) == APFloat::cmpEqual;
897 return compare(lhs, rhs) != APFloat::cmpEqual;
901 return compare(lhs, rhs) == APFloat::cmpLessThan;
905 APFloat::cmpResult Res =
compare(lhs, rhs);
906 return Res == APFloat::cmpLessThan || Res == APFloat::cmpEqual;
910 return compare(lhs, rhs) == APFloat::cmpGreaterThan;
914 APFloat::cmpResult Res =
compare(lhs, rhs);
915 return Res == APFloat::cmpGreaterThan || Res == APFloat::cmpEqual;
static llvm::raw_ostream & error(Stream &strm)
static llvm::APInt ToAPInt(const llvm::APFloat &f, unsigned bits, bool is_unsigned)
unsigned int UInt(unsigned int fail_value=0) const
llvm::APFloat CreateAPFloatFromAPFloat(lldb::BasicType basic_type)
void GetValue(Stream &s, bool show_type) const
llvm::APFloat CreateAPFloatFromAPSInt(lldb::BasicType basic_type)
const char * GetTypeAsCString() const
bool IntegralPromote(uint16_t bits, bool sign)
bool SetBit(uint32_t bit)
int SInt(int fail_value=0) const
long SLong(long fail_value=0) const
size_t GetByteSize() const
unsigned char UChar(unsigned char fail_value=0) const
Status SetValueFromData(const DataExtractor &data, lldb::Encoding encoding, size_t byte_size)
bool SignExtend(uint32_t bit_pos)
short SShort(short fail_value=0) const
T GetAs(T fail_value) const
void GetBytes(uint8_t *storage, size_t length) const
Store the binary representation of this value into the given storage.
void TruncOrExtendTo(uint16_t bits, bool sign)
Convert to an integer with bits and the given signedness.
unsigned long long ULongLong(unsigned long long fail_value=0) const
Scalar::Type GetType() const
unsigned long ULong(unsigned long fail_value=0) const
size_t GetAsMemoryData(void *dst, size_t dst_len, lldb::ByteOrder dst_byte_order, Status &error) const
signed char SChar(signed char fail_value=0) const
bool FloatPromote(const llvm::fltSemantics &semantics)
bool ClearBit(uint32_t bit)
static PromotionKey GetFloatPromoKey(const llvm::fltSemantics &semantics)
float Float(float fail_value=0.0f) const
Scalar & operator&=(const Scalar &rhs)
long long SLongLong(long long fail_value=0) const
static const char * GetValueTypeAsCString(Scalar::Type value_type)
bool ExtractBitfield(uint32_t bit_size, uint32_t bit_offset)
std::tuple< Type, unsigned, bool > PromotionKey
double Double(double fail_value=0.0) const
Scalar & operator<<=(const Scalar &rhs)
Status SetValueFromCString(const char *s, lldb::Encoding encoding, size_t byte_size)
bool GetData(DataExtractor &data) const
Get data with a byte size of GetByteSize().
static Type PromoteToMaxType(Scalar &lhs, Scalar &rhs)
llvm::APInt SInt128(const llvm::APInt &fail_value) const
unsigned short UShort(unsigned short fail_value=0) const
Scalar & operator+=(Scalar rhs)
bool ShiftRightLogical(const Scalar &rhs)
PromotionKey GetPromoKey() const
long double LongDouble(long double fail_value=0.0) const
Scalar & operator>>=(const Scalar &rhs)
llvm::APInt UInt128(const llvm::APInt &fail_value) const
static Status FromErrorString(const char *str)
static Status static Status FromErrorStringWithFormatv(const char *format, Args &&...args)
static Status FromError(llvm::Error error)
Avoid using this in new code. Migrate APIs to llvm::Expected instead.
llvm::StringRef GetString() const
A stream class that can stream formatted output to a file.
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
lldb::ByteOrder InlHostByteOrder()
A class that represents a running process on the host machine.
const Scalar operator&(Scalar lhs, Scalar rhs)
const Scalar operator*(Scalar lhs, Scalar rhs)
bool operator<=(const Scalar &lhs, const Scalar &rhs)
static uint32_t bit(const uint32_t val, const uint32_t msbit)
const Scalar operator/(Scalar lhs, Scalar rhs)
bool operator!=(const Address &lhs, const Address &rhs)
bool operator>(const Address &lhs, const Address &rhs)
const Scalar operator-(Scalar lhs, Scalar rhs)
const Scalar operator>>(const Scalar &lhs, const Scalar &rhs)
const Scalar operator%(Scalar lhs, Scalar rhs)
const Scalar operator+(const Scalar &lhs, const Scalar &rhs)
llvm::APFloat::cmpResult compare(Scalar lhs, Scalar rhs)
Stream & operator<<(Stream &s, const Mangled &obj)
const Scalar operator^(Scalar lhs, Scalar rhs)
bool operator==(const Address &lhs, const Address &rhs)
const Scalar operator|(Scalar lhs, Scalar rhs)
bool operator<(const Address &lhs, const Address &rhs)
static uint32_t bits(const uint32_t val, const uint32_t msbit, const uint32_t lsbit)
bool operator>=(const Scalar &lhs, const Scalar &rhs)
BasicType
Basic types enumeration for the public API SBType::GetBasicType().
Encoding
Register encoding definitions.
@ eEncodingVector
vector registers
@ eEncodingUint
unsigned integer
@ eEncodingSint
signed integer
ByteOrder
Byte ordering definitions.