Author: Pavel Labath Date: 2020-08-28T11:51:25+02:00 New Revision: 9b50546b0b4087ec2ee8479d68e70b89c7956aaa
URL: https://github.com/llvm/llvm-project/commit/9b50546b0b4087ec2ee8479d68e70b89c7956aaa DIFF: https://github.com/llvm/llvm-project/commit/9b50546b0b4087ec2ee8479d68e70b89c7956aaa.diff LOG: [lldb/Utility] Polish the Scalar class This patch is mostly about removing the "Category" enum, which was very useful when the Type enum contained a large number of types, but now the two are completely identical. It also removes some other artifacts like unused typedefs and macros. Added: Modified: lldb/include/lldb/Utility/Scalar.h lldb/source/Utility/Scalar.cpp Removed: ################################################################################ diff --git a/lldb/include/lldb/Utility/Scalar.h b/lldb/include/lldb/Utility/Scalar.h index 332bb00489d0..7403fd1d9717 100644 --- a/lldb/include/lldb/Utility/Scalar.h +++ b/lldb/include/lldb/Utility/Scalar.h @@ -20,18 +20,12 @@ #include <utility> namespace lldb_private { + class DataExtractor; class Stream; -} // namespace lldb_private #define NUM_OF_WORDS_INT128 2 #define BITWIDTH_INT128 128 -#define NUM_OF_WORDS_INT256 4 -#define BITWIDTH_INT256 256 -#define NUM_OF_WORDS_INT512 8 -#define BITWIDTH_INT512 512 - -namespace lldb_private { // A class designed to hold onto values and their corresponding types. // Operators are defined and Scalar objects will correctly promote their types @@ -48,7 +42,6 @@ class Scalar { } public: - // FIXME: These are host types which seems to be an odd choice. enum Type { e_void = 0, e_int, @@ -219,21 +212,6 @@ class Scalar { } protected: - typedef char schar_t; - typedef unsigned char uchar_t; - typedef short sshort_t; - typedef unsigned short ushort_t; - typedef int sint_t; - typedef unsigned int uint_t; - typedef long slong_t; - typedef unsigned long ulong_t; - typedef long long slonglong_t; - typedef unsigned long long ulonglong_t; - typedef float float_t; - typedef double double_t; - typedef long double long_double_t; - - // Classes that inherit from Scalar can see and modify these Scalar::Type m_type; llvm::APSInt m_integer; llvm::APFloat m_float; @@ -242,10 +220,7 @@ class Scalar { static Type PromoteToMaxType(Scalar &lhs, Scalar &rhs); - enum class Category { Void, Integral, Float }; - static Category GetCategory(Scalar::Type type); - - using PromotionKey = std::tuple<Category, unsigned, bool>; + using PromotionKey = std::tuple<Type, unsigned, bool>; PromotionKey GetPromoKey() const; static PromotionKey GetFloatPromoKey(const llvm::fltSemantics &semantics); diff --git a/lldb/source/Utility/Scalar.cpp b/lldb/source/Utility/Scalar.cpp index 3dd2813b5eb0..9bf633d0c4e0 100644 --- a/lldb/source/Utility/Scalar.cpp +++ b/lldb/source/Utility/Scalar.cpp @@ -27,26 +27,13 @@ using llvm::APFloat; using llvm::APInt; using llvm::APSInt; -Scalar::Category Scalar::GetCategory(Scalar::Type type) { - switch (type) { - case Scalar::e_void: - return Category::Void; - case Scalar::e_float: - return Category::Float; - case Scalar::e_int: - return Category::Integral; - } - llvm_unreachable("Unhandled type!"); -} - Scalar::PromotionKey Scalar::GetPromoKey() const { - Category cat = GetCategory(m_type); - switch (cat) { - case Category::Void: - return PromotionKey{cat, 0, false}; - case Category::Integral: - return PromotionKey{cat, m_integer.getBitWidth(), m_integer.isUnsigned()}; - case Category::Float: + switch (m_type) { + case e_void: + return PromotionKey{e_void, 0, false}; + case e_int: + return PromotionKey{e_int, m_integer.getBitWidth(), m_integer.isUnsigned()}; + case e_float: return GetFloatPromoKey(m_float.getSemantics()); } llvm_unreachable("Unhandled category!"); @@ -58,7 +45,7 @@ Scalar::PromotionKey Scalar::GetFloatPromoKey(const llvm::fltSemantics &sem) { &APFloat::x87DoubleExtended()}; for (const auto &entry : llvm::enumerate(order)) { if (entry.value() == &sem) - return PromotionKey{Category::Float, entry.index(), false}; + return PromotionKey{e_float, entry.index(), false}; } llvm_unreachable("Unsupported semantics!"); } @@ -67,13 +54,13 @@ Scalar::PromotionKey Scalar::GetFloatPromoKey(const llvm::fltSemantics &sem) { // expressions. Scalar::Type Scalar::PromoteToMaxType(Scalar &lhs, Scalar &rhs) { const auto &Promote = [](Scalar &a, const Scalar &b) { - switch (GetCategory(b.GetType())) { - case Category::Void: + switch (b.GetType()) { + case e_void: break; - case Category::Integral: + case e_int: a.IntegralPromote(b.m_integer.getBitWidth(), b.m_integer.isSigned()); break; - case Category::Float: + case e_float: a.FloatPromote(b.m_float.getSemantics()); } }; @@ -129,13 +116,13 @@ void Scalar::GetBytes(llvm::MutableArrayRef<uint8_t> storage) const { const auto &store = [&](const llvm::APInt &val) { StoreIntToMemory(val, storage.data(), (val.getBitWidth() + 7) / 8); }; - switch (GetCategory(m_type)) { - case Category::Void: + switch (m_type) { + case e_void: break; - case Category::Integral: + case e_int: store(m_integer); break; - case Category::Float: + case e_float: store(m_float.bitcastToAPInt()); break; } @@ -154,12 +141,12 @@ size_t Scalar::GetByteSize() const { } bool Scalar::IsZero() const { - switch (GetCategory(m_type)) { - case Category::Void: + switch (m_type) { + case e_void: break; - case Category::Integral: + case e_int: return m_integer.isNullValue(); - case Category::Float: + case e_float: return m_float.isZero(); } return false; @@ -169,13 +156,13 @@ void Scalar::GetValue(Stream *s, bool show_type) const { if (show_type) s->Printf("(%s) ", GetTypeAsCString()); - switch (GetCategory(m_type)) { - case Category::Void: + switch (m_type) { + case e_void: break; - case Category::Integral: + case e_int: s->PutCString(m_integer.toString(10)); break; - case Category::Float: + case e_float: llvm::SmallString<24> string; m_float.toString(string); s->PutCString(string); @@ -189,12 +176,12 @@ void Scalar::TruncOrExtendTo(uint16_t bits, bool sign) { } bool Scalar::IntegralPromote(uint16_t bits, bool sign) { - switch (GetCategory(m_type)) { - case Category::Void: - case Category::Float: + switch (m_type) { + case e_void: + case e_float: break; - case Category::Integral: - if (GetPromoKey() > PromotionKey(Category::Integral, bits, !sign)) + case e_int: + if (GetPromoKey() > PromotionKey(e_int, bits, !sign)) break; m_integer = m_integer.extOrTrunc(bits); m_integer.setIsSigned(sign); @@ -205,16 +192,16 @@ bool Scalar::IntegralPromote(uint16_t bits, bool sign) { bool Scalar::FloatPromote(const llvm::fltSemantics &semantics) { bool success = false; - switch (GetCategory(m_type)) { - case Category::Void: + switch (m_type) { + case e_void: break; - case Category::Integral: + case e_int: m_float = llvm::APFloat(semantics); m_float.convertFromAPInt(m_integer, m_integer.isSigned(), llvm::APFloat::rmNearestTiesToEven); success = true; break; - case Category::Float: + case e_float: if (GetFloatPromoKey(semantics) < GetFloatPromoKey(m_float.getSemantics())) break; bool ignore; @@ -296,16 +283,16 @@ static llvm::APInt ToAPInt(const llvm::APFloat &f, unsigned bits, } template <typename T> T Scalar::GetAs(T fail_value) const { - switch (GetCategory(m_type)) { - case Category::Void: + switch (m_type) { + case e_void: break; - case Category::Integral: { + case e_int: { APSInt ext = m_integer.extOrTrunc(sizeof(T) * 8); if (ext.isSigned()) return ext.getSExtValue(); return ext.getZExtValue(); } - case Category::Float: + case e_float: return ToAPInt(m_float, sizeof(T) * 8, std::is_unsigned<T>::value) .getSExtValue(); } @@ -349,39 +336,39 @@ unsigned long long Scalar::ULongLong(unsigned long long fail_value) const { } llvm::APInt Scalar::SInt128(const llvm::APInt &fail_value) const { - switch (GetCategory(m_type)) { - case Category::Void: + switch (m_type) { + case e_void: break; - case Category::Integral: + case e_int: return m_integer; - case Category::Float: + case e_float: return ToAPInt(m_float, 128, /*is_unsigned=*/false); } return fail_value; } llvm::APInt Scalar::UInt128(const llvm::APInt &fail_value) const { - switch (GetCategory(m_type)) { - case Category::Void: + switch (m_type) { + case e_void: break; - case Category::Integral: + case e_int: return m_integer; - case Category::Float: + case e_float: return ToAPInt(m_float, 128, /*is_unsigned=*/true); } return fail_value; } float Scalar::Float(float fail_value) const { - switch (GetCategory(m_type)) { - case Category::Void: + switch (m_type) { + case e_void: break; - case Category::Integral: + case e_int: if (m_integer.isSigned()) return llvm::APIntOps::RoundSignedAPIntToFloat(m_integer); return llvm::APIntOps::RoundAPIntToFloat(m_integer); - case Category::Float: { + case e_float: { APFloat result = m_float; bool losesInfo; result.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, @@ -393,15 +380,15 @@ float Scalar::Float(float fail_value) const { } double Scalar::Double(double fail_value) const { - switch (GetCategory(m_type)) { - case Category::Void: + switch (m_type) { + case e_void: break; - case Category::Integral: + case e_int: if (m_integer.isSigned()) return llvm::APIntOps::RoundSignedAPIntToDouble(m_integer); return llvm::APIntOps::RoundAPIntToDouble(m_integer); - case Category::Float: { + case e_float: { APFloat result = m_float; bool losesInfo; result.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, @@ -420,14 +407,14 @@ long double Scalar::LongDouble(long double fail_value) const { Scalar &Scalar::operator+=(Scalar rhs) { Scalar copy = *this; if ((m_type = PromoteToMaxType(copy, rhs)) != Scalar::e_void) { - switch (GetCategory(m_type)) { - case Category::Void: + switch (m_type) { + case e_void: break; - case Category::Integral: + case e_int: m_integer = copy.m_integer + rhs.m_integer; break; - case Category::Float: + case e_float: m_float = copy.m_float + rhs.m_float; break; } @@ -436,8 +423,7 @@ Scalar &Scalar::operator+=(Scalar rhs) { } Scalar &Scalar::operator<<=(const Scalar &rhs) { - if (GetCategory(m_type) == Category::Integral && - GetCategory(rhs.m_type) == Category::Integral) + if (m_type == e_int && rhs.m_type == e_int) static_cast<APInt &>(m_integer) <<= rhs.m_integer; else m_type = e_void; @@ -445,8 +431,7 @@ Scalar &Scalar::operator<<=(const Scalar &rhs) { } bool Scalar::ShiftRightLogical(const Scalar &rhs) { - if (GetCategory(m_type) == Category::Integral && - GetCategory(rhs.m_type) == Category::Integral) { + if (m_type == e_int && rhs.m_type == e_int) { m_integer = m_integer.lshr(rhs.m_integer); return true; } @@ -477,8 +462,7 @@ Scalar &Scalar::operator>>=(const Scalar &rhs) { } Scalar &Scalar::operator&=(const Scalar &rhs) { - if (GetCategory(m_type) == Category::Integral && - GetCategory(rhs.m_type) == Category::Integral) + if (m_type == e_int && rhs.m_type == e_int) m_integer &= rhs.m_integer; else m_type = e_void; @@ -503,13 +487,13 @@ bool Scalar::AbsoluteValue() { } bool Scalar::UnaryNegate() { - switch (GetCategory(m_type)) { - case Category::Void: + switch (m_type) { + case e_void: break; - case Category::Integral: + case e_int: m_integer = -m_integer; return true; - case Category::Float: + case e_float: m_float.changeSign(); return true; } @@ -517,7 +501,7 @@ bool Scalar::UnaryNegate() { } bool Scalar::OnesComplement() { - if (GetCategory(m_type) == Category::Integral) { + if (m_type == e_int) { m_integer = ~m_integer; return true; } @@ -534,13 +518,13 @@ const Scalar lldb_private::operator+(const Scalar &lhs, const Scalar &rhs) { const Scalar lldb_private::operator-(Scalar lhs, Scalar rhs) { Scalar result; if ((result.m_type = Scalar::PromoteToMaxType(lhs, rhs)) != Scalar::e_void) { - switch (Scalar::GetCategory(result.m_type)) { - case Scalar::Category::Void: + switch (result.m_type) { + case Scalar::e_void: break; - case Scalar::Category::Integral: + case Scalar::e_int: result.m_integer = lhs.m_integer - rhs.m_integer; break; - case Scalar::Category::Float: + case Scalar::e_float: result.m_float = lhs.m_float - rhs.m_float; break; } @@ -552,13 +536,13 @@ const Scalar lldb_private::operator/(Scalar lhs, Scalar rhs) { Scalar result; if ((result.m_type = Scalar::PromoteToMaxType(lhs, rhs)) != Scalar::e_void && !rhs.IsZero()) { - switch (Scalar::GetCategory(result.m_type)) { - case Scalar::Category::Void: + switch (result.m_type) { + case Scalar::e_void: break; - case Scalar::Category::Integral: + case Scalar::e_int: result.m_integer = lhs.m_integer / rhs.m_integer; return result; - case Scalar::Category::Float: + case Scalar::e_float: result.m_float = lhs.m_float / rhs.m_float; return result; } @@ -572,13 +556,13 @@ const Scalar lldb_private::operator/(Scalar lhs, Scalar rhs) { const Scalar lldb_private::operator*(Scalar lhs, Scalar rhs) { Scalar result; if ((result.m_type = Scalar::PromoteToMaxType(lhs, rhs)) != Scalar::e_void) { - switch (Scalar::GetCategory(result.m_type)) { - case Scalar::Category::Void: + switch (result.m_type) { + case Scalar::e_void: break; - case Scalar::Category::Integral: + case Scalar::e_int: result.m_integer = lhs.m_integer * rhs.m_integer; break; - case Scalar::Category::Float: + case Scalar::e_float: result.m_float = lhs.m_float * rhs.m_float; break; } @@ -589,7 +573,7 @@ const Scalar lldb_private::operator*(Scalar lhs, Scalar rhs) { const Scalar lldb_private::operator&(Scalar lhs, Scalar rhs) { Scalar result; if ((result.m_type = Scalar::PromoteToMaxType(lhs, rhs)) != Scalar::e_void) { - if (Scalar::GetCategory(result.m_type) == Scalar::Category::Integral) + if (result.m_type == Scalar::e_int) result.m_integer = lhs.m_integer & rhs.m_integer; else result.m_type = Scalar::e_void; @@ -600,7 +584,7 @@ const Scalar lldb_private::operator&(Scalar lhs, Scalar rhs) { const Scalar lldb_private::operator|(Scalar lhs, Scalar rhs) { Scalar result; if ((result.m_type = Scalar::PromoteToMaxType(lhs, rhs)) != Scalar::e_void) { - if (Scalar::GetCategory(result.m_type) == Scalar::Category::Integral) + if (result.m_type == Scalar::e_int) result.m_integer = lhs.m_integer | rhs.m_integer; else result.m_type = Scalar::e_void; @@ -611,8 +595,7 @@ const Scalar lldb_private::operator|(Scalar lhs, Scalar rhs) { const Scalar lldb_private::operator%(Scalar lhs, Scalar rhs) { Scalar result; if ((result.m_type = Scalar::PromoteToMaxType(lhs, rhs)) != Scalar::e_void) { - if (!rhs.IsZero() && - Scalar::GetCategory(result.m_type) == Scalar::Category::Integral) { + if (!rhs.IsZero() && result.m_type == Scalar::e_int) { result.m_integer = lhs.m_integer % rhs.m_integer; return result; } @@ -624,7 +607,7 @@ const Scalar lldb_private::operator%(Scalar lhs, Scalar rhs) { const Scalar lldb_private::operator^(Scalar lhs, Scalar rhs) { Scalar result; if ((result.m_type = Scalar::PromoteToMaxType(lhs, rhs)) != Scalar::e_void) { - if (Scalar::GetCategory(result.m_type) == Scalar::Category::Integral) + if (result.m_type == Scalar::e_int) result.m_integer = lhs.m_integer ^ rhs.m_integer; else result.m_type = Scalar::e_void; _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits