180814287SRaphael Isemann //===-- DIERef.cpp --------------------------------------------------------===//
2eb882fc1STamas Berghammer //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6eb882fc1STamas Berghammer //
7eb882fc1STamas Berghammer //===----------------------------------------------------------------------===//
8eb882fc1STamas Berghammer 
9eb882fc1STamas Berghammer #include "DIERef.h"
10*a2154b19SGreg Clayton #include "lldb/Utility/DataEncoder.h"
11*a2154b19SGreg Clayton #include "lldb/Utility/DataExtractor.h"
120de98ebdSPavel Labath #include "llvm/Support/Format.h"
130de98ebdSPavel Labath 
14*a2154b19SGreg Clayton using namespace lldb;
15*a2154b19SGreg Clayton using namespace lldb_private;
16*a2154b19SGreg Clayton 
format(const DIERef & ref,raw_ostream & OS,StringRef Style)170de98ebdSPavel Labath void llvm::format_provider<DIERef>::format(const DIERef &ref, raw_ostream &OS,
180de98ebdSPavel Labath                                            StringRef Style) {
193b926988SPavel Labath   if (ref.dwo_num())
203b926988SPavel Labath     OS << format_hex_no_prefix(*ref.dwo_num(), 8) << "/";
210de98ebdSPavel Labath   OS << (ref.section() == DIERef::DebugInfo ? "INFO" : "TYPE");
220de98ebdSPavel Labath   OS << "/" << format_hex_no_prefix(ref.die_offset(), 8);
230de98ebdSPavel Labath }
24*a2154b19SGreg Clayton 
25*a2154b19SGreg Clayton constexpr uint32_t k_dwo_num_mask = 0x3FFFFFFF;
26*a2154b19SGreg Clayton constexpr uint32_t k_dwo_num_valid_bitmask = (1u << 30);
27*a2154b19SGreg Clayton constexpr uint32_t k_section_bitmask = (1u << 31);
28*a2154b19SGreg Clayton 
Decode(const DataExtractor & data,lldb::offset_t * offset_ptr)29*a2154b19SGreg Clayton llvm::Optional<DIERef> DIERef::Decode(const DataExtractor &data,
30*a2154b19SGreg Clayton                                       lldb::offset_t *offset_ptr) {
31*a2154b19SGreg Clayton   const uint32_t bitfield_storage = data.GetU32(offset_ptr);
32*a2154b19SGreg Clayton   uint32_t dwo_num = bitfield_storage & k_dwo_num_mask;
33*a2154b19SGreg Clayton   bool dwo_num_valid = (bitfield_storage & (k_dwo_num_valid_bitmask)) != 0;
34*a2154b19SGreg Clayton   Section section = (Section)((bitfield_storage & (k_section_bitmask)) != 0);
35*a2154b19SGreg Clayton   // DIE offsets can't be zero and if we fail to decode something from data,
36*a2154b19SGreg Clayton   // it will return 0
37*a2154b19SGreg Clayton   dw_offset_t die_offset = data.GetU32(offset_ptr);
38*a2154b19SGreg Clayton   if (die_offset == 0)
39*a2154b19SGreg Clayton     return llvm::None;
40*a2154b19SGreg Clayton   if (dwo_num_valid)
41*a2154b19SGreg Clayton     return DIERef(dwo_num, section, die_offset);
42*a2154b19SGreg Clayton   else
43*a2154b19SGreg Clayton     return DIERef(llvm::None, section, die_offset);
44*a2154b19SGreg Clayton }
45*a2154b19SGreg Clayton 
Encode(DataEncoder & encoder) const46*a2154b19SGreg Clayton void DIERef::Encode(DataEncoder &encoder) const {
47*a2154b19SGreg Clayton   uint32_t bitfield_storage = m_dwo_num;
48*a2154b19SGreg Clayton   if (m_dwo_num_valid)
49*a2154b19SGreg Clayton     bitfield_storage |= k_dwo_num_valid_bitmask;
50*a2154b19SGreg Clayton   if (m_section)
51*a2154b19SGreg Clayton     bitfield_storage |= k_section_bitmask;
52*a2154b19SGreg Clayton   encoder.AppendU32(bitfield_storage);
53*a2154b19SGreg Clayton   static_assert(sizeof(m_die_offset) == 4, "m_die_offset must be 4 bytes");
54*a2154b19SGreg Clayton   encoder.AppendU32(m_die_offset);
55*a2154b19SGreg Clayton }
56