1*1cf11a4cSAlexey 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"
109e8c799eSAlexey Lapshin 
119e8c799eSAlexey Lapshin namespace llvm {
129e8c799eSAlexey Lapshin 
139e8c799eSAlexey Lapshin DwarfStringPoolEntryRef NonRelocatableStringpool::getEntry(StringRef S) {
149e8c799eSAlexey Lapshin   if (S.empty() && !Strings.empty())
159e8c799eSAlexey Lapshin     return EmptyString;
169e8c799eSAlexey Lapshin 
179e8c799eSAlexey Lapshin   if (Translator)
189e8c799eSAlexey Lapshin     S = Translator(S);
199e8c799eSAlexey Lapshin   auto I = Strings.insert({S, DwarfStringPoolEntry()});
209e8c799eSAlexey Lapshin   auto &Entry = I.first->second;
219e8c799eSAlexey Lapshin   if (I.second || !Entry.isIndexed()) {
229e8c799eSAlexey Lapshin     Entry.Index = NumEntries++;
239e8c799eSAlexey Lapshin     Entry.Offset = CurrentEndOffset;
249e8c799eSAlexey Lapshin     Entry.Symbol = nullptr;
259e8c799eSAlexey Lapshin     CurrentEndOffset += S.size() + 1;
269e8c799eSAlexey Lapshin   }
279e8c799eSAlexey Lapshin   return DwarfStringPoolEntryRef(*I.first, true);
289e8c799eSAlexey Lapshin }
299e8c799eSAlexey Lapshin 
309e8c799eSAlexey Lapshin StringRef NonRelocatableStringpool::internString(StringRef S) {
319e8c799eSAlexey Lapshin   DwarfStringPoolEntry Entry{nullptr, 0, DwarfStringPoolEntry::NotIndexed};
329e8c799eSAlexey Lapshin 
339e8c799eSAlexey Lapshin   if (Translator)
349e8c799eSAlexey Lapshin     S = Translator(S);
359e8c799eSAlexey Lapshin 
369e8c799eSAlexey Lapshin   auto InsertResult = Strings.insert({S, Entry});
379e8c799eSAlexey Lapshin   return InsertResult.first->getKey();
389e8c799eSAlexey Lapshin }
399e8c799eSAlexey Lapshin 
409e8c799eSAlexey Lapshin std::vector<DwarfStringPoolEntryRef>
419e8c799eSAlexey Lapshin NonRelocatableStringpool::getEntriesForEmission() const {
429e8c799eSAlexey Lapshin   std::vector<DwarfStringPoolEntryRef> Result;
439e8c799eSAlexey Lapshin   Result.reserve(Strings.size());
449e8c799eSAlexey Lapshin   for (const auto &E : Strings)
459e8c799eSAlexey Lapshin     if (E.getValue().isIndexed())
469e8c799eSAlexey Lapshin       Result.emplace_back(E, true);
479e8c799eSAlexey Lapshin   llvm::sort(Result, [](const DwarfStringPoolEntryRef A,
489e8c799eSAlexey Lapshin                         const DwarfStringPoolEntryRef B) {
499e8c799eSAlexey Lapshin     return A.getIndex() < B.getIndex();
509e8c799eSAlexey Lapshin   });
519e8c799eSAlexey Lapshin   return Result;
529e8c799eSAlexey Lapshin }
539e8c799eSAlexey Lapshin 
549e8c799eSAlexey Lapshin } // namespace llvm
55