1 //===-- DIERef.cpp --------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
10 #include "lldb/Utility/DataEncoder.h"
11 #include "lldb/Utility/DataExtractor.h"
12 #include "llvm/Support/Format.h"
16 using namespace lldb_private;
18 void llvm::format_provider<DIERef>::format(const DIERef &ref, raw_ostream &OS,
21 OS << format_hex_no_prefix(*ref.dwo_num(), 8) << "/";
22 OS << (ref.section() == DIERef::DebugInfo ? "INFO" : "TYPE");
23 OS << "/" << format_hex_no_prefix(ref.die_offset(), 8);
26 constexpr uint32_t k_dwo_num_mask = 0x3FFFFFFF;
27 constexpr uint32_t k_dwo_num_valid_bitmask = (1u << 30);
28 constexpr uint32_t k_section_bitmask = (1u << 31);
30 std::optional<DIERef> DIERef::Decode(const DataExtractor &data,
31 lldb::offset_t *offset_ptr) {
32 const uint32_t bitfield_storage = data.GetU32(offset_ptr);
33 uint32_t dwo_num = bitfield_storage & k_dwo_num_mask;
34 bool dwo_num_valid = (bitfield_storage & (k_dwo_num_valid_bitmask)) != 0;
35 Section section = (Section)((bitfield_storage & (k_section_bitmask)) != 0);
36 // DIE offsets can't be zero and if we fail to decode something from data,
38 dw_offset_t die_offset = data.GetU32(offset_ptr);
42 return DIERef(dwo_num, section, die_offset);
44 return DIERef(std::nullopt, section, die_offset);
47 void DIERef::Encode(DataEncoder &encoder) const {
48 uint32_t bitfield_storage = m_dwo_num;
50 bitfield_storage |= k_dwo_num_valid_bitmask;
52 bitfield_storage |= k_section_bitmask;
53 encoder.AppendU32(bitfield_storage);
54 static_assert(sizeof(m_die_offset) == 4, "m_die_offset must be 4 bytes");
55 encoder.AppendU32(m_die_offset);