xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/NonRelocatableStringpool.cpp (revision 6b96bb739548a8b8c2b13bd46234f70a29be12fb)
1  //===-- NonRelocatableStringpool.cpp --------------------------------------===//
2  //
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
6  //
7  //===----------------------------------------------------------------------===//
8  
9  #include "llvm/CodeGen/NonRelocatableStringpool.h"
10  
11  namespace llvm {
12  
13  DwarfStringPoolEntryRef NonRelocatableStringpool::getEntry(StringRef S) {
14    if (S.empty() && !Strings.empty())
15      return EmptyString;
16  
17    if (Translator)
18      S = Translator(S);
19    auto I = Strings.insert({S, DwarfStringPoolEntry()});
20    auto &Entry = I.first->second;
21    if (I.second || !Entry.isIndexed()) {
22      Entry.Index = NumEntries++;
23      Entry.Offset = CurrentEndOffset;
24      Entry.Symbol = nullptr;
25      CurrentEndOffset += S.size() + 1;
26    }
27    return DwarfStringPoolEntryRef(*I.first, true);
28  }
29  
30  StringRef NonRelocatableStringpool::internString(StringRef S) {
31    DwarfStringPoolEntry Entry{nullptr, 0, DwarfStringPoolEntry::NotIndexed};
32  
33    if (Translator)
34      S = Translator(S);
35  
36    auto InsertResult = Strings.insert({S, Entry});
37    return InsertResult.first->getKey();
38  }
39  
40  std::vector<DwarfStringPoolEntryRef>
41  NonRelocatableStringpool::getEntriesForEmission() const {
42    std::vector<DwarfStringPoolEntryRef> Result;
43    Result.reserve(Strings.size());
44    for (const auto &E : Strings)
45      if (E.getValue().isIndexed())
46        Result.emplace_back(E, true);
47    llvm::sort(Result, [](const DwarfStringPoolEntryRef A,
48                          const DwarfStringPoolEntryRef B) {
49      return A.getIndex() < B.getIndex();
50    });
51    return Result;
52  }
53  
54  } // namespace llvm
55