xref: /freebsd-src/contrib/llvm-project/llvm/lib/DebugInfo/GSYM/ExtractRanges.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
1*81ad6265SDimitry Andric //===- ExtractRanges.cpp ----------------------------------------*- C++ -*-===//
2*81ad6265SDimitry Andric //
3*81ad6265SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*81ad6265SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*81ad6265SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*81ad6265SDimitry Andric //
7*81ad6265SDimitry Andric //===----------------------------------------------------------------------===//
8*81ad6265SDimitry Andric 
9*81ad6265SDimitry Andric #include "llvm/DebugInfo/GSYM/ExtractRanges.h"
10*81ad6265SDimitry Andric #include "llvm/DebugInfo/GSYM/FileWriter.h"
11*81ad6265SDimitry Andric #include "llvm/Support/DataExtractor.h"
12*81ad6265SDimitry Andric #include <algorithm>
13*81ad6265SDimitry Andric #include <inttypes.h>
14*81ad6265SDimitry Andric 
15*81ad6265SDimitry Andric namespace llvm {
16*81ad6265SDimitry Andric namespace gsym {
17*81ad6265SDimitry Andric 
encodeRange(const AddressRange & Range,FileWriter & O,uint64_t BaseAddr)18*81ad6265SDimitry Andric void encodeRange(const AddressRange &Range, FileWriter &O, uint64_t BaseAddr) {
19*81ad6265SDimitry Andric   assert(Range.start() >= BaseAddr);
20*81ad6265SDimitry Andric   O.writeULEB(Range.start() - BaseAddr);
21*81ad6265SDimitry Andric   O.writeULEB(Range.size());
22*81ad6265SDimitry Andric }
23*81ad6265SDimitry Andric 
decodeRange(DataExtractor & Data,uint64_t BaseAddr,uint64_t & Offset)24*81ad6265SDimitry Andric AddressRange decodeRange(DataExtractor &Data, uint64_t BaseAddr,
25*81ad6265SDimitry Andric                          uint64_t &Offset) {
26*81ad6265SDimitry Andric   const uint64_t AddrOffset = Data.getULEB128(&Offset);
27*81ad6265SDimitry Andric   const uint64_t Size = Data.getULEB128(&Offset);
28*81ad6265SDimitry Andric   const uint64_t StartAddr = BaseAddr + AddrOffset;
29*81ad6265SDimitry Andric 
30*81ad6265SDimitry Andric   return {StartAddr, StartAddr + Size};
31*81ad6265SDimitry Andric }
32*81ad6265SDimitry Andric 
encodeRanges(const AddressRanges & Ranges,FileWriter & O,uint64_t BaseAddr)33*81ad6265SDimitry Andric void encodeRanges(const AddressRanges &Ranges, FileWriter &O,
34*81ad6265SDimitry Andric                   uint64_t BaseAddr) {
35*81ad6265SDimitry Andric   O.writeULEB(Ranges.size());
36*81ad6265SDimitry Andric   if (Ranges.empty())
37*81ad6265SDimitry Andric     return;
38*81ad6265SDimitry Andric   for (auto Range : Ranges)
39*81ad6265SDimitry Andric     encodeRange(Range, O, BaseAddr);
40*81ad6265SDimitry Andric }
41*81ad6265SDimitry Andric 
decodeRanges(AddressRanges & Ranges,DataExtractor & Data,uint64_t BaseAddr,uint64_t & Offset)42*81ad6265SDimitry Andric void decodeRanges(AddressRanges &Ranges, DataExtractor &Data, uint64_t BaseAddr,
43*81ad6265SDimitry Andric                   uint64_t &Offset) {
44*81ad6265SDimitry Andric   Ranges.clear();
45*81ad6265SDimitry Andric   uint64_t NumRanges = Data.getULEB128(&Offset);
46*81ad6265SDimitry Andric   Ranges.reserve(NumRanges);
47*81ad6265SDimitry Andric   for (uint64_t RangeIdx = 0; RangeIdx < NumRanges; RangeIdx++)
48*81ad6265SDimitry Andric     Ranges.insert(decodeRange(Data, BaseAddr, Offset));
49*81ad6265SDimitry Andric }
50*81ad6265SDimitry Andric 
skipRange(DataExtractor & Data,uint64_t & Offset)51*81ad6265SDimitry Andric void skipRange(DataExtractor &Data, uint64_t &Offset) {
52*81ad6265SDimitry Andric   Data.getULEB128(&Offset);
53*81ad6265SDimitry Andric   Data.getULEB128(&Offset);
54*81ad6265SDimitry Andric }
55*81ad6265SDimitry Andric 
skipRanges(DataExtractor & Data,uint64_t & Offset)56*81ad6265SDimitry Andric uint64_t skipRanges(DataExtractor &Data, uint64_t &Offset) {
57*81ad6265SDimitry Andric   uint64_t NumRanges = Data.getULEB128(&Offset);
58*81ad6265SDimitry Andric   for (uint64_t I = 0; I < NumRanges; ++I)
59*81ad6265SDimitry Andric     skipRange(Data, Offset);
60*81ad6265SDimitry Andric   return NumRanges;
61*81ad6265SDimitry Andric }
62*81ad6265SDimitry Andric 
63*81ad6265SDimitry Andric } // namespace gsym
64*81ad6265SDimitry Andric 
operator <<(raw_ostream & OS,const AddressRange & R)65*81ad6265SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const AddressRange &R) {
66*81ad6265SDimitry Andric   return OS << '[' << HEX64(R.start()) << " - " << HEX64(R.end()) << ")";
67*81ad6265SDimitry Andric }
68*81ad6265SDimitry Andric 
operator <<(raw_ostream & OS,const AddressRanges & AR)69*81ad6265SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const AddressRanges &AR) {
70*81ad6265SDimitry Andric   size_t Size = AR.size();
71*81ad6265SDimitry Andric   for (size_t I = 0; I < Size; ++I) {
72*81ad6265SDimitry Andric     if (I)
73*81ad6265SDimitry Andric       OS << ' ';
74*81ad6265SDimitry Andric     OS << AR[I];
75*81ad6265SDimitry Andric   }
76*81ad6265SDimitry Andric   return OS;
77*81ad6265SDimitry Andric }
78*81ad6265SDimitry Andric 
79*81ad6265SDimitry Andric } // namespace llvm
80