blob: d28399f239cecd9aab056d7d9319119b6dd3265f [file] [log] [blame]
Alexey Lapshin9e8c7992019-12-05 13:11:32 +03001//===-- llvm/CodeGen/NonRelocatableStringpool.cpp - A simple stringpool --===//
Jonas Devliegherecaf26f22018-03-01 10:05:54 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Jonas Devliegherecaf26f22018-03-01 10:05:54 +00006//
7//===----------------------------------------------------------------------===//
8
Alexey Lapshin9e8c7992019-12-05 13:11:32 +03009#include "llvm/CodeGen/NonRelocatableStringpool.h"
Jonas Devliegherecaf26f22018-03-01 10:05:54 +000010
11namespace llvm {
Jonas Devliegherecaf26f22018-03-01 10:05:54 +000012
13DwarfStringPoolEntryRef NonRelocatableStringpool::getEntry(StringRef S) {
14 if (S.empty() && !Strings.empty())
15 return EmptyString;
16
Jonas Devlieghere91b43ad2019-01-07 23:27:25 +000017 if (Translator)
18 S = Translator(S);
Jonas Devliegherecaf26f22018-03-01 10:05:54 +000019 auto I = Strings.insert({S, DwarfStringPoolEntry()});
20 auto &Entry = I.first->second;
Pavel Labath2f088112018-08-07 09:54:52 +000021 if (I.second || !Entry.isIndexed()) {
Jonas Devliegherecaf26f22018-03-01 10:05:54 +000022 Entry.Index = NumEntries++;
23 Entry.Offset = CurrentEndOffset;
24 Entry.Symbol = nullptr;
25 CurrentEndOffset += S.size() + 1;
26 }
Pavel Labath2f088112018-08-07 09:54:52 +000027 return DwarfStringPoolEntryRef(*I.first, true);
Jonas Devliegherecaf26f22018-03-01 10:05:54 +000028}
29
30StringRef NonRelocatableStringpool::internString(StringRef S) {
Pavel Labath2f088112018-08-07 09:54:52 +000031 DwarfStringPoolEntry Entry{nullptr, 0, DwarfStringPoolEntry::NotIndexed};
Jonas Devlieghere91b43ad2019-01-07 23:27:25 +000032
33 if (Translator)
34 S = Translator(S);
35
Jonas Devliegherecaf26f22018-03-01 10:05:54 +000036 auto InsertResult = Strings.insert({S, Entry});
37 return InsertResult.first->getKey();
38}
39
40std::vector<DwarfStringPoolEntryRef>
Pavel Labath2f088112018-08-07 09:54:52 +000041NonRelocatableStringpool::getEntriesForEmission() const {
Jonas Devliegherecaf26f22018-03-01 10:05:54 +000042 std::vector<DwarfStringPoolEntryRef> Result;
43 Result.reserve(Strings.size());
44 for (const auto &E : Strings)
Pavel Labath2f088112018-08-07 09:54:52 +000045 if (E.getValue().isIndexed())
46 Result.emplace_back(E, true);
Fangrui Song3507c6e2018-09-30 22:31:29 +000047 llvm::sort(Result, [](const DwarfStringPoolEntryRef A,
48 const DwarfStringPoolEntryRef B) {
49 return A.getIndex() < B.getIndex();
50 });
Jonas Devliegherecaf26f22018-03-01 10:05:54 +000051 return Result;
52}
53
Jonas Devliegherecaf26f22018-03-01 10:05:54 +000054} // namespace llvm