1*9e8c799eSAlexey Lapshin //===-- llvm/CodeGen/NonRelocatableStringpool.cpp - A simple stringpool  --===//
2*9e8c799eSAlexey Lapshin //
3*9e8c799eSAlexey Lapshin // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*9e8c799eSAlexey Lapshin // See https://llvm.org/LICENSE.txt for license information.
5*9e8c799eSAlexey Lapshin // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*9e8c799eSAlexey Lapshin //
7*9e8c799eSAlexey Lapshin //===----------------------------------------------------------------------===//
8*9e8c799eSAlexey Lapshin 
9*9e8c799eSAlexey Lapshin #include "llvm/CodeGen/NonRelocatableStringpool.h"
10*9e8c799eSAlexey Lapshin 
11*9e8c799eSAlexey Lapshin namespace llvm {
12*9e8c799eSAlexey Lapshin 
13*9e8c799eSAlexey Lapshin DwarfStringPoolEntryRef NonRelocatableStringpool::getEntry(StringRef S) {
14*9e8c799eSAlexey Lapshin   if (S.empty() && !Strings.empty())
15*9e8c799eSAlexey Lapshin     return EmptyString;
16*9e8c799eSAlexey Lapshin 
17*9e8c799eSAlexey Lapshin   if (Translator)
18*9e8c799eSAlexey Lapshin     S = Translator(S);
19*9e8c799eSAlexey Lapshin   auto I = Strings.insert({S, DwarfStringPoolEntry()});
20*9e8c799eSAlexey Lapshin   auto &Entry = I.first->second;
21*9e8c799eSAlexey Lapshin   if (I.second || !Entry.isIndexed()) {
22*9e8c799eSAlexey Lapshin     Entry.Index = NumEntries++;
23*9e8c799eSAlexey Lapshin     Entry.Offset = CurrentEndOffset;
24*9e8c799eSAlexey Lapshin     Entry.Symbol = nullptr;
25*9e8c799eSAlexey Lapshin     CurrentEndOffset += S.size() + 1;
26*9e8c799eSAlexey Lapshin   }
27*9e8c799eSAlexey Lapshin   return DwarfStringPoolEntryRef(*I.first, true);
28*9e8c799eSAlexey Lapshin }
29*9e8c799eSAlexey Lapshin 
30*9e8c799eSAlexey Lapshin StringRef NonRelocatableStringpool::internString(StringRef S) {
31*9e8c799eSAlexey Lapshin   DwarfStringPoolEntry Entry{nullptr, 0, DwarfStringPoolEntry::NotIndexed};
32*9e8c799eSAlexey Lapshin 
33*9e8c799eSAlexey Lapshin   if (Translator)
34*9e8c799eSAlexey Lapshin     S = Translator(S);
35*9e8c799eSAlexey Lapshin 
36*9e8c799eSAlexey Lapshin   auto InsertResult = Strings.insert({S, Entry});
37*9e8c799eSAlexey Lapshin   return InsertResult.first->getKey();
38*9e8c799eSAlexey Lapshin }
39*9e8c799eSAlexey Lapshin 
40*9e8c799eSAlexey Lapshin std::vector<DwarfStringPoolEntryRef>
41*9e8c799eSAlexey Lapshin NonRelocatableStringpool::getEntriesForEmission() const {
42*9e8c799eSAlexey Lapshin   std::vector<DwarfStringPoolEntryRef> Result;
43*9e8c799eSAlexey Lapshin   Result.reserve(Strings.size());
44*9e8c799eSAlexey Lapshin   for (const auto &E : Strings)
45*9e8c799eSAlexey Lapshin     if (E.getValue().isIndexed())
46*9e8c799eSAlexey Lapshin       Result.emplace_back(E, true);
47*9e8c799eSAlexey Lapshin   llvm::sort(Result, [](const DwarfStringPoolEntryRef A,
48*9e8c799eSAlexey Lapshin                         const DwarfStringPoolEntryRef B) {
49*9e8c799eSAlexey Lapshin     return A.getIndex() < B.getIndex();
50*9e8c799eSAlexey Lapshin   });
51*9e8c799eSAlexey Lapshin   return Result;
52*9e8c799eSAlexey Lapshin }
53*9e8c799eSAlexey Lapshin 
54*9e8c799eSAlexey Lapshin } // namespace llvm
55