xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/NonRelocatableStringpool.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1480093f4SDimitry Andric //===-- NonRelocatableStringpool.cpp --------------------------------------===//
2480093f4SDimitry Andric //
3480093f4SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4480093f4SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5480093f4SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6480093f4SDimitry Andric //
7480093f4SDimitry Andric //===----------------------------------------------------------------------===//
8480093f4SDimitry Andric 
9480093f4SDimitry Andric #include "llvm/CodeGen/NonRelocatableStringpool.h"
1004eeddc0SDimitry Andric #include "llvm/ADT/STLExtras.h"
11480093f4SDimitry Andric 
12480093f4SDimitry Andric namespace llvm {
13480093f4SDimitry Andric 
getEntry(StringRef S)14480093f4SDimitry Andric DwarfStringPoolEntryRef NonRelocatableStringpool::getEntry(StringRef S) {
15480093f4SDimitry Andric   auto I = Strings.insert({S, DwarfStringPoolEntry()});
16480093f4SDimitry Andric   auto &Entry = I.first->second;
17480093f4SDimitry Andric   if (I.second || !Entry.isIndexed()) {
18480093f4SDimitry Andric     Entry.Index = NumEntries++;
19480093f4SDimitry Andric     Entry.Offset = CurrentEndOffset;
20480093f4SDimitry Andric     Entry.Symbol = nullptr;
21480093f4SDimitry Andric     CurrentEndOffset += S.size() + 1;
22480093f4SDimitry Andric   }
23*81ad6265SDimitry Andric   return DwarfStringPoolEntryRef(*I.first);
24480093f4SDimitry Andric }
25480093f4SDimitry Andric 
internString(StringRef S)26480093f4SDimitry Andric StringRef NonRelocatableStringpool::internString(StringRef S) {
27480093f4SDimitry Andric   DwarfStringPoolEntry Entry{nullptr, 0, DwarfStringPoolEntry::NotIndexed};
28480093f4SDimitry Andric 
29480093f4SDimitry Andric   auto InsertResult = Strings.insert({S, Entry});
30480093f4SDimitry Andric   return InsertResult.first->getKey();
31480093f4SDimitry Andric }
32480093f4SDimitry Andric 
33480093f4SDimitry Andric std::vector<DwarfStringPoolEntryRef>
getEntriesForEmission() const34480093f4SDimitry Andric NonRelocatableStringpool::getEntriesForEmission() const {
35480093f4SDimitry Andric   std::vector<DwarfStringPoolEntryRef> Result;
36480093f4SDimitry Andric   Result.reserve(Strings.size());
37480093f4SDimitry Andric   for (const auto &E : Strings)
38480093f4SDimitry Andric     if (E.getValue().isIndexed())
39*81ad6265SDimitry Andric       Result.emplace_back(E);
40480093f4SDimitry Andric   llvm::sort(Result, [](const DwarfStringPoolEntryRef A,
41480093f4SDimitry Andric                         const DwarfStringPoolEntryRef B) {
42480093f4SDimitry Andric     return A.getIndex() < B.getIndex();
43480093f4SDimitry Andric   });
44480093f4SDimitry Andric   return Result;
45480093f4SDimitry Andric }
46480093f4SDimitry Andric 
47480093f4SDimitry Andric } // namespace llvm
48