Implement DW_OP_convert
This patch adds basic support for DW_OP_convert[1] for integer
types. Recent versions of LLVM's optimizer may insert this opcode into
DWARF expressions. DW_OP_convert is effectively a type cast operation
that takes a reference to a base type DIE (or zero) and then casts the
value at the top of the DWARF stack to that type. Internally this
works by changing the bit size of the APInt that is used as backing
storage for LLDB's DWARF stack.
I managed to write a unit test for this by implementing a mock YAML
object file / module that takes debug info sections in yaml2obj
format.
[1] Typed DWARF stack. http://www.dwarfstd.org/ShowIssue.php?issue=140425.1
<rdar://problem/48167864>
Differential Revision: https://reviews.llvm.org/D67369
llvm-svn: 371532
diff --git a/lldb/source/Utility/Scalar.cpp b/lldb/source/Utility/Scalar.cpp
index 23d50b9..3393af9 100644
--- a/lldb/source/Utility/Scalar.cpp
+++ b/lldb/source/Utility/Scalar.cpp
@@ -416,6 +416,51 @@
Scalar::~Scalar() = default;
+Scalar::Type Scalar::GetBestTypeForBitSize(size_t bit_size, bool sign) {
+ // Scalar types are always host types, hence the sizeof().
+ if (sign) {
+ if (bit_size <= sizeof(int)*8) return Scalar::e_sint;
+ if (bit_size <= sizeof(long)*8) return Scalar::e_slong;
+ if (bit_size <= sizeof(long long)*8) return Scalar::e_slonglong;
+ if (bit_size <= 128) return Scalar::e_sint128;
+ if (bit_size <= 256) return Scalar::e_sint256;
+ if (bit_size <= 512) return Scalar::e_sint512;
+ } else {
+ if (bit_size <= sizeof(unsigned int)*8) return Scalar::e_uint;
+ if (bit_size <= sizeof(unsigned long)*8) return Scalar::e_ulong;
+ if (bit_size <= sizeof(unsigned long long)*8) return Scalar::e_ulonglong;
+ if (bit_size <= 128) return Scalar::e_uint128;
+ if (bit_size <= 256) return Scalar::e_uint256;
+ if (bit_size <= 512) return Scalar::e_uint512;
+ }
+ return Scalar::e_void;
+};
+
+void Scalar::TruncOrExtendTo(Scalar::Type type, uint16_t bits) {
+ switch (type) {
+ case e_sint:
+ case e_slong:
+ case e_slonglong:
+ case e_sint128:
+ case e_sint256:
+ case e_sint512:
+ m_integer = m_integer.sextOrTrunc(bits);
+ break;
+ case e_uint:
+ case e_ulong:
+ case e_ulonglong:
+ case e_uint128:
+ case e_uint256:
+ case e_uint512:
+ m_integer = m_integer.zextOrTrunc(bits);
+ break;
+ default:
+ llvm_unreachable("Promoting a Scalar to a specific number of bits is only "
+ "supported for integer types.");
+ }
+ m_type = type;
+}
+
bool Scalar::Promote(Scalar::Type type) {
bool success = false;
switch (m_type) {