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
12namespace llvm {
13
14DwarfStringPoolEntryRef NonRelocatableStringpool::getEntry(StringRef S) {
15 auto I = Strings.insert(KV: {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
26StringRef NonRelocatableStringpool::internString(StringRef S) {
27 DwarfStringPoolEntry Entry{.Symbol: nullptr, .Offset: 0, .Index: DwarfStringPoolEntry::NotIndexed};
28
29 auto InsertResult = Strings.insert(KV: {S, Entry});
30 return InsertResult.first->getKey();
31}
32
33std::vector<DwarfStringPoolEntryRef>
34NonRelocatableStringpool::getEntriesForEmission() const {
35 std::vector<DwarfStringPoolEntryRef> Result;
36 Result.reserve(n: Strings.size());
37 for (const auto &E : Strings)
38 if (E.getValue().isIndexed())
39 Result.emplace_back(args: E);
40 llvm::sort(C&: Result, Comp: [](const DwarfStringPoolEntryRef A,
41 const DwarfStringPoolEntryRef B) {
42 return A.getIndex() < B.getIndex();
43 });
44 return Result;
45}
46
47} // namespace llvm
48