11cf11a4cSAlexey Lapshin //===-- NonRelocatableStringpool.cpp --------------------------------------===//
29e8c799eSAlexey Lapshin //
39e8c799eSAlexey Lapshin // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
49e8c799eSAlexey Lapshin // See https://llvm.org/LICENSE.txt for license information.
59e8c799eSAlexey Lapshin // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
69e8c799eSAlexey Lapshin //
79e8c799eSAlexey Lapshin //===----------------------------------------------------------------------===//
89e8c799eSAlexey Lapshin 
99e8c799eSAlexey Lapshin #include "llvm/CodeGen/NonRelocatableStringpool.h"
105f290c09Sserge-sans-paille #include "llvm/ADT/STLExtras.h"
119e8c799eSAlexey Lapshin 
129e8c799eSAlexey Lapshin namespace llvm {
139e8c799eSAlexey Lapshin 
getEntry(StringRef S)149e8c799eSAlexey Lapshin DwarfStringPoolEntryRef NonRelocatableStringpool::getEntry(StringRef S) {
159e8c799eSAlexey Lapshin   if (S.empty() && !Strings.empty())
169e8c799eSAlexey Lapshin     return EmptyString;
179e8c799eSAlexey Lapshin 
189e8c799eSAlexey Lapshin   if (Translator)
199e8c799eSAlexey Lapshin     S = Translator(S);
209e8c799eSAlexey Lapshin   auto I = Strings.insert({S, DwarfStringPoolEntry()});
219e8c799eSAlexey Lapshin   auto &Entry = I.first->second;
229e8c799eSAlexey Lapshin   if (I.second || !Entry.isIndexed()) {
239e8c799eSAlexey Lapshin     Entry.Index = NumEntries++;
249e8c799eSAlexey Lapshin     Entry.Offset = CurrentEndOffset;
259e8c799eSAlexey Lapshin     Entry.Symbol = nullptr;
269e8c799eSAlexey Lapshin     CurrentEndOffset += S.size() + 1;
279e8c799eSAlexey Lapshin   }
28*501d5b24SAlexey Lapshin   return DwarfStringPoolEntryRef(*I.first);
299e8c799eSAlexey Lapshin }
309e8c799eSAlexey Lapshin 
internString(StringRef S)319e8c799eSAlexey Lapshin StringRef NonRelocatableStringpool::internString(StringRef S) {
329e8c799eSAlexey Lapshin   DwarfStringPoolEntry Entry{nullptr, 0, DwarfStringPoolEntry::NotIndexed};
339e8c799eSAlexey Lapshin 
349e8c799eSAlexey Lapshin   if (Translator)
359e8c799eSAlexey Lapshin     S = Translator(S);
369e8c799eSAlexey Lapshin 
379e8c799eSAlexey Lapshin   auto InsertResult = Strings.insert({S, Entry});
389e8c799eSAlexey Lapshin   return InsertResult.first->getKey();
399e8c799eSAlexey Lapshin }
409e8c799eSAlexey Lapshin 
419e8c799eSAlexey Lapshin std::vector<DwarfStringPoolEntryRef>
getEntriesForEmission() const429e8c799eSAlexey Lapshin NonRelocatableStringpool::getEntriesForEmission() const {
439e8c799eSAlexey Lapshin   std::vector<DwarfStringPoolEntryRef> Result;
449e8c799eSAlexey Lapshin   Result.reserve(Strings.size());
459e8c799eSAlexey Lapshin   for (const auto &E : Strings)
469e8c799eSAlexey Lapshin     if (E.getValue().isIndexed())
47*501d5b24SAlexey Lapshin       Result.emplace_back(E);
489e8c799eSAlexey Lapshin   llvm::sort(Result, [](const DwarfStringPoolEntryRef A,
499e8c799eSAlexey Lapshin                         const DwarfStringPoolEntryRef B) {
509e8c799eSAlexey Lapshin     return A.getIndex() < B.getIndex();
519e8c799eSAlexey Lapshin   });
529e8c799eSAlexey Lapshin   return Result;
539e8c799eSAlexey Lapshin }
549e8c799eSAlexey Lapshin 
559e8c799eSAlexey Lapshin } // namespace llvm
56