xref: /src/contrib/llvm-project/llvm/lib/CodeGen/NonRelocatableStringpool.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583) !
1706b4fc4SDimitry Andric //===-- NonRelocatableStringpool.cpp --------------------------------------===//
2706b4fc4SDimitry Andric //
3706b4fc4SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4706b4fc4SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5706b4fc4SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6706b4fc4SDimitry Andric //
7706b4fc4SDimitry Andric //===----------------------------------------------------------------------===//
8706b4fc4SDimitry Andric 
9706b4fc4SDimitry Andric #include "llvm/CodeGen/NonRelocatableStringpool.h"
106f8fc217SDimitry Andric #include "llvm/ADT/STLExtras.h"
11706b4fc4SDimitry Andric 
12706b4fc4SDimitry Andric namespace llvm {
13706b4fc4SDimitry Andric 
getEntry(StringRef S)14706b4fc4SDimitry Andric DwarfStringPoolEntryRef NonRelocatableStringpool::getEntry(StringRef S) {
15706b4fc4SDimitry Andric   auto I = Strings.insert({S, DwarfStringPoolEntry()});
16706b4fc4SDimitry Andric   auto &Entry = I.first->second;
17706b4fc4SDimitry Andric   if (I.second || !Entry.isIndexed()) {
18706b4fc4SDimitry Andric     Entry.Index = NumEntries++;
19706b4fc4SDimitry Andric     Entry.Offset = CurrentEndOffset;
20706b4fc4SDimitry Andric     Entry.Symbol = nullptr;
21706b4fc4SDimitry Andric     CurrentEndOffset += S.size() + 1;
22706b4fc4SDimitry Andric   }
23145449b1SDimitry Andric   return DwarfStringPoolEntryRef(*I.first);
24706b4fc4SDimitry Andric }
25706b4fc4SDimitry Andric 
internString(StringRef S)26706b4fc4SDimitry Andric StringRef NonRelocatableStringpool::internString(StringRef S) {
27706b4fc4SDimitry Andric   DwarfStringPoolEntry Entry{nullptr, 0, DwarfStringPoolEntry::NotIndexed};
28706b4fc4SDimitry Andric 
29706b4fc4SDimitry Andric   auto InsertResult = Strings.insert({S, Entry});
30706b4fc4SDimitry Andric   return InsertResult.first->getKey();
31706b4fc4SDimitry Andric }
32706b4fc4SDimitry Andric 
33706b4fc4SDimitry Andric std::vector<DwarfStringPoolEntryRef>
getEntriesForEmission() const34706b4fc4SDimitry Andric NonRelocatableStringpool::getEntriesForEmission() const {
35706b4fc4SDimitry Andric   std::vector<DwarfStringPoolEntryRef> Result;
36706b4fc4SDimitry Andric   Result.reserve(Strings.size());
37706b4fc4SDimitry Andric   for (const auto &E : Strings)
38706b4fc4SDimitry Andric     if (E.getValue().isIndexed())
39145449b1SDimitry Andric       Result.emplace_back(E);
40706b4fc4SDimitry Andric   llvm::sort(Result, [](const DwarfStringPoolEntryRef A,
41706b4fc4SDimitry Andric                         const DwarfStringPoolEntryRef B) {
42706b4fc4SDimitry Andric     return A.getIndex() < B.getIndex();
43706b4fc4SDimitry Andric   });
44706b4fc4SDimitry Andric   return Result;
45706b4fc4SDimitry Andric }
46706b4fc4SDimitry Andric 
47706b4fc4SDimitry Andric } // namespace llvm
48