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